|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r59477 - in sandbox/statistics/detail/assign: boost/assign libs/assign/example libs/assign/src
From: erwann.rogard_at_[hidden]
Date: 2010-02-04 14:05:43
Author: e_r
Date: 2010-02-04 14:05:42 EST (Thu, 04 Feb 2010)
New Revision: 59477
URL: http://svn.boost.org/trac/boost/changeset/59477
Log:
m
Text files modified:
sandbox/statistics/detail/assign/boost/assign/cref_list_of2.hpp | 317 +++++++++++++++++++--------------------
sandbox/statistics/detail/assign/libs/assign/example/cref_list_of2.cpp | 7
sandbox/statistics/detail/assign/libs/assign/example/cref_list_of2.h | 2
sandbox/statistics/detail/assign/libs/assign/src/main.cpp | 2
4 files changed, 161 insertions(+), 167 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 14:05:42 EST (Thu, 04 Feb 2010)
@@ -1,5 +1,5 @@
//////////////////////////////////////////////////////////////////////////////
-// assign::cref_list_of2.cpp //
+// assign::cref_list_of2.hpp //
// //
// (C) Copyright 2010 Erwann Rogard //
// Use, modification and distribution are subject to the //
@@ -8,12 +8,11 @@
//////////////////////////////////////////////////////////////////////////////
#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/if.hpp>
#include <boost/mpl/bool.hpp>
+#include <boost/mpl/equal_to.hpp>
+#include <boost/shared_ptr.hpp>
#include <boost/assign/list_of.hpp>
#include <boost/type_traits.hpp>
#include <boost/ref.hpp>
@@ -24,169 +23,159 @@
namespace boost{
namespace assign{
-// Usage : vec = cref_list2_of(a)(b)(c)
-// The idea of this class was developed in collaboration with P.M.
-// Complexity of cref_list2_of for size N : A total of N reference_wrappers
-// are stored. Another N are constructed at conversion.
-//
-// TODO perhaps the stored ref_ could be used as a private base;
-template<typename T,int N, typename B>
-class cref_impl : protected B{
-
- typedef boost::assign_detail::assign_reference<const T> ref_;
-
- typedef cref_impl<T,N,B> this_;
-
- typedef typename boost::mpl::plus<
- boost::mpl::int_<N>,
- boost::mpl::int_<1>
- >::type incr_n_;
-
- 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_;
-
- template<typename T1>
- struct array{ typedef boost::array<T1,N+1> type; };
-
- // because reference_wrapper has no default constructor, assign_reference
- // is needed
- 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_;
-
- protected:
+ // Usage : vec = cref_list2_of(a)(b)(c)
+ // The idea of this class was developed in collaboration with M.P.
- 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());
- }
+namespace cref_list_of2_impl{
+
+ typedef boost::mpl::void_ top_;
+
+ template<typename T>
+ struct ref{
+ typedef boost::assign_detail::assign_reference<const T> type;
+ };
+
+ template<typename T,int N>
+ struct ref_array{
+ typedef boost::array<typename ref<T>::type,N> type;
+ };
+
+ template<typename L,typename R,int N>
+ struct expr;
+
+ template<typename E,typename T,int N>
+ struct next{
+ typedef expr<E,T,N> expr_;
+ typedef expr<expr_,T,N+1> type;
+ };
+
+ template<typename E,typename T,int N>
+ struct expr{
+ typedef boost::mpl::int_<N> int_n_;
+ typedef boost::mpl::int_<1> int_1_;
+ typedef typename boost::mpl::equal_to<int_n_,int_1_>::type is_first_;
+
+ typedef typename boost::mpl::if_<is_first_,E,E&>::type previous_;
+
+ typedef typename ref<T>::type ref_;
+ typedef typename ref_array<T,N>::type ref_array_;
+
+
+ previous_ previous;
+ ref_ ref;
+
+ typedef typename next<E,T,N>::type next_;
+
+ // public:
+ typedef next_ result_type;
+
+ expr(const T& t):ref(t){} // only for N == 1
+ expr(E& p,const T& t):previous(p),ref(t){}
+
+ next_ operator()(const T& t){ return next_(*this,t); }
+
+ template<typename T1>
+ operator boost::array<T1,N>(){
+ typedef boost::array<T1,N> ar_;
+ ar_ ar;
+ write_to_array(ar,*this);
+ return ar;
+ }
+
+ template<typename C>
+ operator C()
+ {
+ ref_array_ ref_array;
+ write_to_array(ref_array,*this);
+ return C(boost::begin(ref_array),boost::end(ref_array));
+ }
+
+ // -------- as container ---- //
+
+ 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());
+ }
+
+ // private:
+ void alloc(){
+ this->ptr = smart_ptr_(new ref_array_);
+ write_to_array(*ptr,*this);
+ }
+
+ void alloc_if(){
+ if(!ptr){
+ return this->alloc();
+ }
+ }
+
+ typedef boost::shared_ptr<ref_array_> smart_ptr_;
+ mutable smart_ptr_ ptr;
+
+ };
+
+ typedef boost::mpl::bool_<false> false_;
+ typedef boost::mpl::bool_<true> true_;
+
+ template<typename A,typename E,typename T,int N>
+ void write_to_array(A& a,expr<E,T,N>& e){
+ typedef expr<E,T,N> expr_;
+ typedef typename expr_::is_first_ exit_;
+ write_to_array(a,e,exit_());
+ }
+
+ template<typename A,typename E,typename T,int N>
+ void write_to_array(A& a,expr<E,T,N>& e,false_ exit){
+ a[N-1] = e.ref;
+ write_to_array(a,e.previous);
+ }
+
+ template<typename A,typename E,typename T,int N>
+ void write_to_array(A& a,expr<E,T,N>& e,true_ exit){
+ a[N-1] = e.ref;
+ }
+
+ template<typename T>
+ struct first{
+ typedef cref_list_of2_impl::expr<
+ cref_list_of2_impl::top_,T,1> type;
+ };
+
+
+}// cref_list_of2_impl
- cref_impl(const T& t):ref(t){}
-
- cref_impl(const B& b,const T& t):B(b),ref(t){}
- next_ operator()(const T& t)const{
- return next_(*this,t);
- }
-
- template<typename T1>
- operator typename array<T1>::type(){
- typedef typename array<T1>::type ar_;
- ar_ ar;
- this->write_to_array(ar,exit_());
- return ar;
- }
-
- // Requirement: C(begin,end) is a valid constructor
- template<typename C>
- operator C(){
- // might consider instead:
- // C c; c.reserve(N+1)
- // and recursively calling c.push_front(ref)
-
- typedef typename boost::range_value<C>::type val_;
- 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;
- }
-
- template<typename A>
- void write_to_array(A& ar,false_)const{
- 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){
- typedef typename B::exit_ exit_;
- return b.write_to_array(ar,exit_());
+ template<typename T>
+ typename cref_list_of2_impl::first<T>::type
+ cref_list_of2(const T& t){
+ typedef typename cref_list_of2_impl::first<T>::type expr_;
+ return expr_(t);
}
-
- ref_ ref;
-};
-
-template<typename T>
-cref_impl<T,0,boost::mpl::empty_base>
-cref_list_of2(const T& t){
- typedef cref_impl<T,0,boost::mpl::empty_base> res_;
- return res_(t);
-}
-
+
}// assign
}// boost
-
+
#endif
+
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 14:05:42 EST (Thu, 04 Feb 2010)
@@ -1,5 +1,5 @@
//////////////////////////////////////////////////////////////////////////////
-// example::list_of2.cpp //
+// example::cref_list_of2.cpp //
// //
// (C) Copyright 2010 Erwann Rogard //
// Use, modification and distribution are subject to the //
@@ -34,12 +34,15 @@
BOOST_ASSERT(ints[0] == a);
BOOST_ASSERT(ints[1] == b);
BOOST_ASSERT(ints[2] == 3);
-
+
+ ints = cref_list_of2(a)(b)(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);
Modified: sandbox/statistics/detail/assign/libs/assign/example/cref_list_of2.h
==============================================================================
--- sandbox/statistics/detail/assign/libs/assign/example/cref_list_of2.h (original)
+++ sandbox/statistics/detail/assign/libs/assign/example/cref_list_of2.h 2010-02-04 14:05:42 EST (Thu, 04 Feb 2010)
@@ -1,5 +1,5 @@
//////////////////////////////////////////////////////////////////////////////
-// example::list_of2.h //
+// example::cerf_list_of2.h //
// //
// (C) Copyright 2010 Erwann Rogard //
// Use, modification and distribution are subject to the //
Modified: sandbox/statistics/detail/assign/libs/assign/src/main.cpp
==============================================================================
--- sandbox/statistics/detail/assign/libs/assign/src/main.cpp (original)
+++ sandbox/statistics/detail/assign/libs/assign/src/main.cpp 2010-02-04 14:05:42 EST (Thu, 04 Feb 2010)
@@ -1,9 +1,11 @@
#include <iostream>
#include <libs/assign/example/cref_list_of2.h>
+#include <libs/assign/test/cref_list_of2_speed.h>
int main (int argc, char * const argv[]) {
example_cref_list_of2(std::cout);
+ test_cref_list_of2_speed(std::cout);
return 0;
}
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