Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r59327 - in sandbox/statistics/iterator: boost/iterator libs/iterator/example
From: erwann.rogard_at_[hidden]
Date: 2010-01-28 00:55:47


Author: e_r
Date: 2010-01-28 00:55:46 EST (Thu, 28 Jan 2010)
New Revision: 59327
URL: http://svn.boost.org/trac/boost/changeset/59327

Log:
m
Text files modified:
   sandbox/statistics/iterator/boost/iterator/flatten_iterator.hpp | 94 +++++++++++++++++++++++++--------------
   sandbox/statistics/iterator/libs/iterator/example/flatten_iterator.cpp | 27 +++++++++--
   2 files changed, 81 insertions(+), 40 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-28 00:55:46 EST (Thu, 28 Jan 2010)
@@ -32,6 +32,7 @@
         template<
             class It,
         bool is_ref = true,
+ typename C = random_access_traversal_tag,
         typename R = typename boost::mpl::if_c<
                 is_ref,
             typename flatten_iterator_nested<It>::ref_,
@@ -40,9 +41,9 @@
         typename D = typename flatten_iterator_nested<It>::diff_
>
         class flatten_iterator : public boost::iterator_facade<
- flatten_iterator<It,is_ref,R,D>
+ flatten_iterator<It,is_ref,C,R,D>
       , typename flatten_iterator_nested<It>::val_
- , boost::forward_traversal_tag
+ , C
       , R
       , D
>{
@@ -50,37 +51,38 @@
                 typedef typename nested_::it_ nit_;
 
                 typedef typename boost::iterator_facade<
- flatten_iterator<It,is_ref,R,D>
+ flatten_iterator<It,is_ref,C,R,D>
                       , typename flatten_iterator_nested<It>::val_
- , boost::forward_traversal_tag
+ , C
                       , R
                       , D
> super_;
 
- typedef typename super_::difference_type diff_;
- typedef typename super_::reference ref_;
+ flatten_iterator();
         
                 public:
+ typedef typename super_::difference_type diff_;
+ typedef typename super_::reference ref_;
         
- flatten_iterator():super_(){}
                         
         explicit flatten_iterator(It b,It e):super_(),b_(b),e_(e){
                 this->update();
         }
 
         flatten_iterator(const flatten_iterator& that)
- :super_(),b_(that.b_),e_(that.e_)
+ :super_(that),b_(that.b_),e_(that.e_)
         {
- this->update();
+ this->update();
         }
 
- flatten_iterator& operator=(const flatten_iterator& that){
+ flatten_iterator&
+ operator=(const flatten_iterator& that){
                 if(that!=&this){
                     super_::operator=(that);
                                 this->b_ = that.b_;
                 this->e_ = that.e_;
+ this->update();
             }
- this->update();
             return (*this);
         }
 
@@ -88,8 +90,10 @@
         friend class boost::iterator_core_access;
                         
         void increment(){
+ BOOST_ASSERT(this->b_!=this->e_);
+ ++this->nb_;
                 if(
- (++(this->nb_)) == (this->ne_)
+ (this->nb_) == (this->ne_)
             )
                 {
                     ++this->b_;
@@ -111,9 +115,10 @@
                         if(n<k){
                     this->nb_ += boost::next(this->nb_,n);
             }else{
+ diff_ nk = std::distance(this->nb_,this->ne_);
                     ++this->b_;
                 this->update();
- return this->advance(n-k);
+ return this->advance(n-nk);
             }
         }
 
@@ -122,40 +127,53 @@
                 return (*this->nb_);
             }
 
+ public: // temporary
+
             diff_ distance_to(const flatten_iterator& other)const
             {
- if(this->b_ == other.b_){
- return std::distance(this->nb_,other.nb_);
- }
+
+ diff_ d;
             if(this->b_ < other.b_){
- return dist_impl(other,*this);
+ d = dist_impl(*this,other);
             }
- if(this->b_ < other.b_){
- return dist_impl(*this,other);
+ if(this->b_ == other.b_){
+ d = std::distance(this->nb_,other.nb_);
             }
- throw std::runtime_error(
- "flatten_iterator::distance_to"
- );
+ if(this->b_ > other.b_){
+ d = -dist_impl(other,*this);
+ }
+
+ return d;
             }
 
             bool equal(const flatten_iterator& rhs)const
             {
- bool is_end1 = (this->b_ == this->e_);
- bool is_end2 = (rhs.b_ == rhs.e_);
-
- if(is_end1 != is_end2)
- return false;
- if(is_end1 && is_end2)
- return true;
- return (this->nb_ == this->ne_);
+ // Beware : assumes the traversal over the same matrix.
+ // TODO check it?
+
+ BOOST_ASSERT(this->e_==rhs.e_);
+
+ if(this->b_ == rhs.b_){
+ if(this->b_ == this->e_){
+ return true;
+ }else{
+ BOOST_ASSERT(this->ne_ == rhs.ne_);
+ return (this->nb_ == rhs.nb_);
+ }
+ }else{
+ return false;
+ }
+
             }
 
         mutable It b_,e_;
         mutable nit_ nb_,ne_; // nested in *b_
 
+ bool is_end()const{ return ((this->b_) == (this->e_)); }
+
             void update()const
             {
- if((this->b_) != (this->e_)){
+ if(!this->is_end()){
                     this->nb_ = boost::begin(*this->b_);
                     this->ne_ = boost::end(*this->b_);
                 }
@@ -166,16 +184,22 @@
             const flatten_iterator& y
         ){
                 BOOST_ASSERT(x.b_ < y.b_);
+
             diff_ d = std::distance(x.nb_,x.ne_);
             It it = x.b_;
- while((++it)<y.b_){
- d += std::distance(boost::begin(*it),boost::end(*it));
+ ++it;
+ while(it!=y.b_){
+ nit_ nb = boost::begin(*it);
+ nit_ ne = boost::end(*it);
+ d += std::distance(nb,ne);
+ ++it;
             }
                 BOOST_ASSERT(it == y.b_);
                         if(it!=x.e_){
- d += std::distance(boost::begin(*it),boost::end(x.nb_));
+ nit_ nb = boost::begin(*it);
+ d += std::distance(nb,x.nb_);
             }
-
+ return d;
         }
 
         

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-28 00:55:46 EST (Thu, 28 Jan 2010)
@@ -32,23 +32,40 @@
         {
             typedef boost::range_iterator<mat_>::type it_mat_;
                 typedef flatten_iterator<it_mat_> flat_it_;
+ typedef flat_it_::difference_type diff_;
         
                 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);
+ os << "n=" << n << std::endl;
             BOOST_ASSERT(
                     n == boost::size(mat[0]) + boost::size(mat[1]) + boost::size(mat[2])
             );
- for(flat_it_ i = b; i!=e; i++, j++){
- typedef flat_it_::difference_type diff_;
+ flat_it_ i = b;
+ BOOST_ASSERT(i==b);
+ ++i;
+ BOOST_ASSERT(i!=b);
+ // temporary --->
+ //n = b.distance_to(i);
+ //BOOST_ASSERT(n==1);
+ //n = i.distance_to(b);
+ //BOOST_ASSERT(n==-1);
+ // <---
+ n = std::distance(b,i); // fails : should be 1
+ os << "n=" << n << std::endl;
+
+
+/*
+ for(; i!=e; i++, j++){
                 val_ val = *i;
                            BOOST_ASSERT(val == j+1);
- diff_ d = std::distance(b,e);
- // std::cout << "d = " << d << std::endl;
- //BOOST_ASSERT(d==n-j);
+ diff_ d = std::distance(i,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