Changeset 1390 for trunk


Ignore:
Timestamp:
Jul 28, 2008, 4:42:13 AM (13 years ago)
Author:
Peter
Message:

fixing #402. Making StrideIterator? inherit from boost::iterator_adaptor solved the problem.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/yat/utility/StrideIterator.h

    r1386 r1390  
    2727
    2828#include "iterator_traits.h"
    29 #include "yat_assert.h"
    3029
    3130#include <boost/iterator/iterator_adaptor.hpp>
    32 
    33 #include <iterator>
    34 #include <stdexcept>
    3531
    3632namespace theplu {
     
    9288  template<typename Iter>
    9389  class StrideIterator
    94     : public std::iterator<typename std::iterator_traits<Iter>::iterator_category,
    95                            typename std::iterator_traits<Iter>::value_type,
    96                            typename std::iterator_traits<Iter>::difference_type,
    97                            typename std::iterator_traits<Iter>::pointer,
    98                            typename std::iterator_traits<Iter>::reference>
     90    : public boost::iterator_adaptor<StrideIterator<Iter>, Iter>
    9991  {
    10092  public:
     
    117109       Using default constructor of BASE iterator.
    118110     */
    119     StrideIterator(difference_type stride=1)
    120       : stride_(stride) {}
     111    StrideIterator(size_t stride=1)
     112      : StrideIterator::iterator_adaptor_(), stride_(stride) {}
    121113
    122114    /**
    123115       \brief Constructor
    124116    */
    125     explicit StrideIterator(Iter p, difference_type stride=1)
    126       : iterator_(p), stride_(stride) {}
     117    explicit StrideIterator(Iter p, size_t stride=1)
     118      : StrideIterator::iterator_adaptor_(p), stride_(stride) {}
    127119
    128120   
     
    137129    StrideIterator(StrideIterator<I2> other,
    138130                   typename boost::enable_if_convertible<I2, Iter>::type* = 0 )
    139       : iterator_(other.base()), stride_(other.stride()) {}
    140 
    141     /**
    142        \return underlying iterator
    143      */
    144     inline Iter base(void) const { return iterator_; }
     131      : StrideIterator::iterator_adaptor_(other.base()),
     132        stride_(other.stride()) {}
    145133
    146134    /**
     
    149137    inline size_t stride(void) const { return stride_; }
    150138
    151     /**
    152        \return element
    153      */
    154     reference operator*(void) const
    155     { return *iterator_; }
     139  private:
     140    // to give base class access to private parts
     141    friend class boost::iterator_core_access;
     142   
     143    size_t stride_;
    156144
    157     /**
    158        \return pointer
    159      */
    160     pointer operator->() const { return &(operator*()); }
     145    typedef typename StrideIterator::iterator_adaptor_::difference_type
     146    difference_t;
    161147
    162     /**
    163        \return element \a n steps forward
    164      */
    165     reference operator[](difference_type n) const
    166     {
    167       return iterator_[stride_*n];
    168     }
     148    void advance(difference_t n){ this->base_reference() += stride_*n; }
     149    void decrement(void) { this->base_reference()-=stride_; }
    169150
    170     /**
    171        \brief pre-increment
     151    template <class OtherIterator>
     152    difference_t distance_type(const StrideIterator<OtherIterator>& other) const
     153    { return (other.base() - this->base())/stride_; }
    172154
    173        \return reference to *this
    174      */
    175     StrideIterator& operator++(void) { iterator_+=stride_; return *this; }
    176 
    177     /**
    178        \brief post-increment
    179 
    180        \return copy of iterator prior increment
    181      */
    182     StrideIterator operator++(int)
    183     { StrideIterator tmp(*this); iterator_+=stride_; return tmp;}
    184 
    185     /**
    186        \brief iterate \f$ n \f$ steps forward
    187 
    188        \return reference to resulting iterator
    189      */
    190     StrideIterator& operator+=(int n) { iterator_+=stride_*n; return *this; }
    191 
    192     /**
    193        \brief post-decrement
    194 
    195        \return copy of iterator prior decrement
    196      */
    197     StrideIterator operator--(int)
    198     { StrideIterator tmp(*this); iterator_-=stride_; return tmp;}
    199 
    200     /**
    201        \brief pre-decrement
    202 
    203        \return reference to resulting iterator
    204      */
    205     StrideIterator& operator--(void) { iterator_-=stride_; return *this; }
    206 
    207     /**
    208        \brief iterate \f$ n \f$ steps backwards
    209 
    210        \return reference to resulting iterator
    211      */
    212     StrideIterator& operator-=(int n) { iterator_-=n*stride_; return *this; }
    213 
    214     /**
    215        \brief addition operator
    216 
    217        \return copy of resulting iterator
    218      */
    219     friend StrideIterator operator+(const StrideIterator& lhs,difference_type n)
    220     { StrideIterator tmp(lhs); tmp+=n; return tmp; }
    221 
    222     /**
    223        \brief subtraction operator
    224 
    225        \return copy of resulting iterator
    226      */
    227     friend StrideIterator operator-(const StrideIterator& lhs,
    228                                     difference_type n)
    229     { StrideIterator tmp(lhs); tmp-=n; return tmp; }
    230 
    231     /**
    232        \brief difference operator
    233 
    234        \return distance between \a lhs and \a rhs
    235      */
    236     friend difference_type operator-(const StrideIterator& lhs,
    237                                      const StrideIterator& rhs)
    238     {
    239       yat_assert<std::runtime_error>(lhs.stride_==rhs.stride_,
    240         "StrideIterator::operator- different stride in lhs and rhs.");
    241       return static_cast<difference_type>( (lhs.iterator_-rhs.iterator_)/
    242                                            lhs.stride_);
    243     }
    244 
    245    
    246     /**
    247        \brief Equality operator
    248 
    249        \return True if \a lhs and \a rhs are pointing to same element
    250      */
    251     friend bool operator==(const StrideIterator& lhs, const StrideIterator& rhs)
    252     { return lhs.iterator_==rhs.iterator_; }
    253    
    254     /**
    255        \brief Non-equality operator
    256 
    257        \return False if \a lhs and \a rhs are pointing to same element
    258      */
    259     friend bool operator!=(const StrideIterator& lhs, const StrideIterator& rhs)
    260     { return lhs.iterator_!=rhs.iterator_; }
    261    
    262     /**
    263        \brief Less operator
    264      */
    265     friend bool operator<(const StrideIterator& lhs, const StrideIterator& rhs)
    266     { return lhs.iterator_<rhs.iterator_; }
    267    
    268     /**
    269        \brief Less equal operator
    270      */
    271     friend bool operator<=(const StrideIterator& lhs, const StrideIterator& rhs)
    272     { return lhs.iterator_<=rhs.iterator_; }
    273    
    274     /**
    275        \brief Larger operator
    276      */
    277     friend bool operator>(const StrideIterator& lhs, const StrideIterator& rhs)
    278     { return lhs.iterator_>rhs.iterator_; }
    279    
    280     /**
    281        \brief Larger equal operator
    282      */
    283     friend bool operator>=(const StrideIterator& lhs, const StrideIterator& rhs)
    284     { return lhs.iterator_>=rhs.iterator_; }
    285    
    286   private:
    287     Iter iterator_;
    288     size_t stride_;
     155    void increment(void) { this->base_reference()+=stride_; }
    289156
    290157    // Using compiler generated copy
Note: See TracChangeset for help on using the changeset viewer.