Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r59326 - in sandbox/statistics/iterator: boost/iterator libs/iterator/example
From: erwann.rogard_at_[hidden]
Date: 2010-01-27 21:33:41


Author: e_r
Date: 2010-01-27 21:33:41 EST (Wed, 27 Jan 2010)
New Revision: 59326
URL: http://svn.boost.org/trac/boost/changeset/59326

Log:
m
Text files modified:
   sandbox/statistics/iterator/boost/iterator/flatten_iterator.hpp | 66 +++++++++++++++++++++++++--------------
   sandbox/statistics/iterator/libs/iterator/example/flatten_iterator.cpp | 9 +++--
   2 files changed, 47 insertions(+), 28 deletions(-)

Modified: sandbox/statistics/iterator/boost/iterator/flatten_iterator.hpp
==============================================================================
--- sandbox/statistics/iterator/boost/iterator/flatten_iterator.hpp (original)
+++ sandbox/statistics/iterator/boost/iterator/flatten_iterator.hpp 2010-01-27 21:33:41 EST (Wed, 27 Jan 2010)
@@ -5,41 +5,53 @@
 #ifndef BOOST_ITERATOR_FLATTEN_ITERATOR_HPP_ER_2010
 #define BOOST_ITERATOR_FLATTEN_ITERATOR_HPP_ER_2010
 #include <stdexcept>
+#include <boost/mpl/if.hpp>
 #include <boost/next_prior.hpp>
 #include <boost/iterator/iterator_traits.hpp>
 #include <boost/range.hpp>
 #include <boost/iterator/iterator_facade.hpp>
 
-// As per M.P.'suggestion
+// As per M.P.'s suggestion
 
 namespace boost{
 
+
         template<typename It>
- struct flatten_iterator_default{
- typedef typename boost::iterator_value<It>::type r_;
- typedef typename boost::range_iterator<r_>::type it_;
- typedef typename boost::iterator_reference<it_>::type reference_;
+ struct flatten_iterator_nested
+ {
+ typedef typename boost::iterator_reference<It>::type ref_range_;
+ typedef typename boost::remove_reference<ref_range_>::type range_;
+ typedef typename boost::range_iterator<range_>::type it_;
+ typedef typename boost::iterator_reference<it_>::type ref_;
+ typedef typename boost::remove_cv<
+ typename boost::remove_reference<ref_>::type
+ >::type val_;
         typedef typename boost::iterator_difference<it_>::type diff_;
     };
-
+
         template<
             class It,
- typename R = typename flatten_iterator_default<It>::reference_,
- typename D = typename flatten_iterator_default<It>::diff_
+ bool is_ref = true,
+ typename R = typename boost::mpl::if_c<
+ is_ref,
+ typename flatten_iterator_nested<It>::ref_,
+ typename flatten_iterator_nested<It>::val_
+ >::type,
+ typename D = typename flatten_iterator_nested<It>::diff_
>
         class flatten_iterator : public boost::iterator_facade<
- flatten_iterator<It,R,D>
- , It
+ flatten_iterator<It,is_ref,R,D>
+ , typename flatten_iterator_nested<It>::val_
       , boost::forward_traversal_tag
       , R
       , D
>{
- typedef typename boost::iterator_value<It>::type nested_range_;
- typedef typename boost::range_iterator<nested_range_>::type nested_;
+ typedef flatten_iterator_nested<It> nested_;
+ typedef typename nested_::it_ nit_;
 
                 typedef typename boost::iterator_facade<
- flatten_iterator<It,R,D>
- , It
+ flatten_iterator<It,is_ref,R,D>
+ , typename flatten_iterator_nested<It>::val_
                       , boost::forward_traversal_tag
                       , R
                       , D
@@ -110,10 +122,19 @@
                 return (*this->nb_);
             }
 
- diff_ distance_to(flatten_iterator other)
+ diff_ distance_to(const flatten_iterator& other)const
             {
- throw std::runtime_error(
- "flatten_iterator::distance_to"
+ if(this->b_ == other.b_){
+ return std::distance(this->nb_,other.nb_);
+ }
+ if(this->b_ < other.b_){
+ return dist_impl(other,*this);
+ }
+ if(this->b_ < other.b_){
+ return dist_impl(*this,other);
+ }
+ throw std::runtime_error(
+ "flatten_iterator::distance_to"
             );
             }
 
@@ -129,11 +150,10 @@
                 return (this->nb_ == this->ne_);
             }
 
- It b_,e_;
- bool ready_;
- nested_ nb_,ne_; // nested in b_
+ mutable It b_,e_;
+ mutable nit_ nb_,ne_; // nested in *b_
 
- void update()
+ void update()const
             {
                 if((this->b_) != (this->e_)){
                     this->nb_ = boost::begin(*this->b_);
@@ -141,8 +161,7 @@
                 }
             }
 
-/*
- diff_ dist_impl(
+ static diff_ dist_impl(
                 const flatten_iterator& x,
             const flatten_iterator& y
         ){
@@ -158,7 +177,6 @@
             }
                         
         }
-*/
 
         
         };

Modified: sandbox/statistics/iterator/libs/iterator/example/flatten_iterator.cpp
==============================================================================
--- sandbox/statistics/iterator/libs/iterator/example/flatten_iterator.cpp (original)
+++ sandbox/statistics/iterator/libs/iterator/example/flatten_iterator.cpp 2010-01-27 21:33:41 EST (Wed, 27 Jan 2010)
@@ -33,8 +33,8 @@
             typedef boost::range_iterator<mat_>::type it_mat_;
                 typedef flatten_iterator<it_mat_> flat_it_;
         
- flat_it_ b = make_flatten_iterator(boost::begin(mat),boost::end(mat));
- flat_it_ e = make_flatten_iterator(boost::end(mat),boost::end(mat));
+ flat_it_ b(boost::begin(mat),boost::end(mat));
+ flat_it_ e(boost::end(mat),boost::end(mat));
 
                 int j = 0;
             int n = std::distance(b,e);
@@ -45,8 +45,9 @@
                 typedef flat_it_::difference_type diff_;
                 val_ val = *i;
                            BOOST_ASSERT(val == j+1);
- // diff_ d = std::distance(b,e);
- //BOOST_ASSERT(std::distance(b,e)==n-j); //not required for single pass
+ diff_ d = std::distance(b,e);
+ // std::cout << "d = " << d << std::endl;
+ //BOOST_ASSERT(d==n-j);
             }
     }
 /*


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