Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r59469 - in sandbox/statistics/detail/assign: boost/assign libs/assign/example
From: erwann.rogard_at_[hidden]
Date: 2010-02-04 10:42:41


Author: e_r
Date: 2010-02-04 10:42:41 EST (Thu, 04 Feb 2010)
New Revision: 59469
URL: http://svn.boost.org/trac/boost/changeset/59469

Log:
m
Text files modified:
   sandbox/statistics/detail/assign/boost/assign/cref_list_of2.hpp | 85 ++++++++++++++++++++++++++++++++-------
   sandbox/statistics/detail/assign/libs/assign/example/cref_list_of2.cpp | 30 +++++++++++++
   2 files changed, 98 insertions(+), 17 deletions(-)

Modified: sandbox/statistics/detail/assign/boost/assign/cref_list_of2.hpp
==============================================================================
--- sandbox/statistics/detail/assign/boost/assign/cref_list_of2.hpp (original)
+++ sandbox/statistics/detail/assign/boost/assign/cref_list_of2.hpp 2010-02-04 10:42:41 EST (Thu, 04 Feb 2010)
@@ -9,6 +9,8 @@
 #ifndef BOOST_ASSIGN_CREF_LIST_OF2_ER_2010_HPP
 #define BOOST_ASSIGN_CREF_LIST_OF2_ER_2010_HPP
 #include <boost/mpl/empty_base.hpp>
+//#include <boost/scoped_ptr.hpp>
+#include <boost/shared_ptr.hpp>
 #include <boost/mpl/void.hpp>
 #include <boost/mpl/plus.hpp>
 #include <boost/mpl/bool.hpp>
@@ -29,7 +31,7 @@
 //
 // TODO perhaps the stored ref_ could be used as a private base;
 template<typename T,int N, typename B>
-class cref_impl : B{
+class cref_impl : protected B{
     
     typedef boost::assign_detail::assign_reference<const T> ref_;
 
@@ -43,6 +45,8 @@
     template<typename T1>
     struct next{ typedef cref_impl<T1,incr_n_::value,this_> type; };
 
+ typedef typename next<T>::type next_;
+
         typedef boost::mpl::bool_<false> false_;
         typedef boost::mpl::bool_<true> true_;
 
@@ -54,30 +58,66 @@
         template<typename T1>
     struct ref_array
             : array<boost::assign_detail::assign_reference<const T1> >{};
+
+ typedef typename array<T>::type array_;
+ typedef typename ref_array<T>::type ref_array_;
+
+ typedef boost::shared_ptr<ref_array_> smart_ptr_;
     
         typedef typename boost::is_same<
             boost::mpl::int_<N>,
             boost::mpl::int_<0>
>::type exit_impl_;
 
- public:
+ protected:
 
         typedef boost::mpl::bool_<exit_impl_::value> exit_;
+
+ public:
+ typedef next_ result_type;
+
+ typedef ref_ value_type;
+ typedef typename boost::range_iterator<
+ ref_array_
+ >::type iterator;
+ typedef typename boost::range_iterator<
+ const ref_array_
+ >::type const_iterator;
+ typedef typename boost::range_size<
+ ref_array_
+ >::type size_type;
+ typedef typename boost::range_difference<
+ ref_array_
+ >::type difference_type;
+
+ iterator begin()
+ {
+ this->alloc_if();
+ return boost::begin(*ptr);
+ }
+
+ iterator end()
+ {
+ this->alloc_if();
+ return boost::end(*ptr);
+ }
+
     
+ size_type size() const
+ {
+ return ref_array_::size();
+ }
+
+ bool empty() const
+ {
+ return !(this->size());
+ }
+
         cref_impl(const T& t):ref(t){}
 
         cref_impl(const B& b,const T& t):B(b),ref(t){}
         
- template<typename S>
- struct result{};
-
- template<typename F,typename T1>
- struct result<F(const T1&)> : next<T1>{};
-
- template<typename T1>
- typename next<T1>::type
- operator()(const T1& t)const{
- typedef typename next<T1>::type next_;
+ next_ operator()(const T& t)const{
         return next_(*this,t);
     }
 
@@ -97,12 +137,13 @@
         // and recursively calling c.push_front(ref)
     
             typedef typename boost::range_value<C>::type val_;
- typedef typename ref_array<val_>::type ar_;
- ar_ ar;
+ ref_array_ ar;
         this->write_to_array(ar,exit_());
         return C(boost::begin(ar),boost::end(ar));
     }
     
+ protected:
+
         template<typename A>
         void write_to_array(A& ar,true_)const{
         ar[N] = this->ref;
@@ -113,6 +154,21 @@
         ar[N] = this->ref;
                 return forward(*this,ar);
     }
+
+ void alloc(){
+ this->ptr = smart_ptr_(new ref_array_);
+ this->write_to_array(*ptr,exit_());
+ }
+
+ void alloc_if(){
+ if(!ptr){
+ return this->alloc();
+ }
+ }
+
+ mutable smart_ptr_ ptr;
+
+ private:
     
         template<typename A>
     static void forward(const B& b,A& ar){
@@ -123,7 +179,6 @@
         ref_ ref;
 };
 
-
 template<typename T>
 cref_impl<T,0,boost::mpl::empty_base>
 cref_list_of2(const T& t){

Modified: sandbox/statistics/detail/assign/libs/assign/example/cref_list_of2.cpp
==============================================================================
--- sandbox/statistics/detail/assign/libs/assign/example/cref_list_of2.cpp (original)
+++ sandbox/statistics/detail/assign/libs/assign/example/cref_list_of2.cpp 2010-02-04 10:42:41 EST (Thu, 04 Feb 2010)
@@ -7,8 +7,9 @@
 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) //
 //////////////////////////////////////////////////////////////////////////////
 #include <boost/timer.hpp>
-#include <iostream>
+#include <ostream>
 #include <vector>
+#include <boost/typeof/typeof.hpp>
 #include <boost/assign/list_of.hpp>
 #include <boost/assign/cref_list_of2.hpp>
 #include <libs/assign/example/cref_list_of2.h>
@@ -33,6 +34,16 @@
                 BOOST_ASSERT(ints[0] == a);
                 BOOST_ASSERT(ints[1] == b);
                 BOOST_ASSERT(ints[2] == 3);
+
+ BOOST_AUTO(
+ tmp,
+ cref_list_of2(a)(b)(3)
+ );
+ ints = ints_(boost::begin(tmp),boost::end(tmp));
+ BOOST_ASSERT(ints[0] == a);
+ BOOST_ASSERT(ints[1] == b);
+ BOOST_ASSERT(ints[2] == 3);
+
     }
     {
                 const int n = 100 * 1000;
@@ -54,13 +65,28 @@
                         t.restart();
                         vec_ints_ vec_ints = cref_list_of2( a )( b )( ints_( n ) );
             double t_val = t.elapsed();
- os << "cref_list_of2 : t = " << t_val << std::endl;
+ os << "conversion cref_list_of2 : t = " << t_val << std::endl;
             BOOST_ASSERT(vec_ints[0] == a);
             BOOST_ASSERT(vec_ints[1] == b);
             BOOST_ASSERT(vec_ints[2] == ints_( n ));
         }
+ {
+ t.restart();
+ BOOST_AUTO(
+ tmp,
+ cref_list_of2( a )( b )( ints_( n ) )
+ );
+ vec_ints_ vec_ints(boost::begin(tmp),boost::end(tmp));
+ double t_val = t.elapsed();
+ os << "copy cref_list_of2 : t = " << t_val << std::endl;
+ BOOST_ASSERT(vec_ints[0] == a);
+ BOOST_ASSERT(vec_ints[1] == b);
+ BOOST_ASSERT(vec_ints[2] == ints_( n ));
+ }
+
         }
 
+
         os << "<- " << std::endl;
     
 };


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk