Changeset 1041


Ignore:
Timestamp:
Feb 6, 2008, 6:36:54 PM (13 years ago)
Author:
Peter
Message:

generalized StrideIterator? to work with Iterators as well - not only pointers

Location:
trunk/yat/utility
Files:
4 edited

Legend:

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

    r1038 r1041  
    2525*/
    2626
     27#include "iterator_traits.h"
    2728#include "yat_assert.h"
    2829
     
    3334namespace utility {
    3435
     36  // forward declaration
     37  template<typename Iter>
     38  class StrideIterator;
     39
    3540  /**
    36      @brief Stride Iterator
    37 
    38      Works as a pointer except that all arithmetic is using the stride value
     41     StrideIterator is weighted if underlying iterator is weighted.
     42   */
     43  template <typename Iter>
     44  struct weighted_iterator_traits<StrideIterator<Iter> > {
     45    typedef typename weighted_iterator_traits<Iter>::type type;
     46  };
     47
     48  /**
     49     @brief Adaptor using a stride on underlying iterator
     50
     51     Works as underlying iterator except that all arithmetic uses the
     52     stride, so e.g., ++StrideIterator returns underlying
     53     _iterator+stride
     54
    3955  */
    40   template<typename T>
     56  template<typename Iter>
    4157  class StrideIterator
    42     : public std::iterator<std::random_access_iterator_tag, T>
     58    : public std::iterator<typename std::iterator_traits<Iter>::iterator_category,
     59                           typename std::iterator_traits<Iter>::value_type,
     60                           typename std::iterator_traits<Iter>::difference_type,
     61                           typename std::iterator_traits<Iter>::pointer,
     62                           typename std::iterator_traits<Iter>::reference>
    4363  {
    4464  public:
    45     typedef typename std::iterator_traits<T*>::difference_type difference_type;
    46     typedef typename std::iterator_traits<T*>::reference reference;
    47     typedef typename std::iterator_traits<T*>::pointer pointer;
     65    typedef typename std::iterator_traits<Iter>::difference_type difference_type;
     66    typedef typename std::iterator_traits<Iter>::reference reference;
     67    typedef typename std::iterator_traits<Iter>::pointer pointer;
     68    typedef typename yat::utility::weighted_iterator_traits<Iter>::type w_type;
    4869
    4970    /**
    5071       \brief Constructor
    5172    */
    52     explicit StrideIterator(pointer p, difference_type stride=1)
    53       : pointer_(p), stride_(stride) {}
    54 
    55     operator StrideIterator<const T>()
    56     { return StrideIterator<const T>(pointer_, stride_); }
     73    explicit StrideIterator(Iter p, difference_type stride=1)
     74      : iterator_(p), stride_(stride) {}
     75
     76   
     77    /**
     78     */
     79    // Peter, this is ugly hack to provide iteartor to const_iterator
     80    // for our gsl_iterators - solution? use Boost:iterator_adaptor
     81    operator StrideIterator<const double*>()
     82    { return StrideIterator<const double*>(base(), stride_); }
     83
     84    /**
     85       \return underlying iterator
     86     */
     87    inline Iter base(void) const { return iterator_; }
     88
     89    /**
     90       \return stride
     91     */
     92    inline size_t stride(void) const { return stride_; }
    5793
    5894    /**
     
    6096     */
    6197    reference operator*(void) const
    62     { return *pointer_; }
     98    { return *iterator_; }
    6399
    64100    /**
     
    71107    reference operator[](difference_type n) const
    72108    {
    73       return pointer_[stride_*n];
     109      return iterator_[stride_*n];
    74110    }
    75111
     
    79115       \return reference to *this
    80116     */
    81     StrideIterator& operator++(void) { pointer_+=stride_; return *this; }
     117    StrideIterator& operator++(void) { iterator_+=stride_; return *this; }
    82118
    83119    /**
     
    87123     */
    88124    StrideIterator operator++(int)
    89     { StrideIterator tmp(*this); pointer_+=stride_; return tmp;}
     125    { StrideIterator tmp(*this); iterator_+=stride_; return tmp;}
    90126
    91127    /**
     
    94130       \return reference to resulting iterator
    95131     */
    96     StrideIterator& operator+=(int n) { pointer_+=stride_*n; return *this; }
     132    StrideIterator& operator+=(int n) { iterator_+=stride_*n; return *this; }
    97133
    98134    /**
     
    102138     */
    103139    StrideIterator operator--(int)
    104     { StrideIterator tmp(*this); pointer_-=stride_; return tmp;}
     140    { StrideIterator tmp(*this); iterator_-=stride_; return tmp;}
    105141
    106142    /**
     
    109145       \return reference to resulting iterator
    110146     */
    111     StrideIterator& operator--(void) { pointer_-=stride_; return *this; }
     147    StrideIterator& operator--(void) { iterator_-=stride_; return *this; }
    112148
    113149    /**
     
    116152       \return reference to resulting iterator
    117153     */
    118     StrideIterator& operator-=(int n) { pointer_-=n*stride_; return *this; }
     154    StrideIterator& operator-=(int n) { iterator_-=n*stride_; return *this; }
    119155
    120156    /**
     
    145181      yat_assert<std::runtime_error>(lhs.stride_==rhs.stride_,
    146182        "StrideIterator::operator- different stride in lhs and rhs.");
    147       return static_cast<difference_type>( (lhs.pointer_-rhs.pointer_)/
     183      return static_cast<difference_type>( (lhs.iterator_-rhs.iterator_)/
    148184                                           lhs.stride_);
    149185    }
     
    156192     */
    157193    friend bool operator==(const StrideIterator& lhs, const StrideIterator& rhs)
    158     { return lhs.pointer_==rhs.pointer_; }
     194    { return lhs.iterator_==rhs.iterator_; }
    159195   
    160196    /**
     
    164200     */
    165201    friend bool operator!=(const StrideIterator& lhs, const StrideIterator& rhs)
    166     { return !(lhs==rhs); }
     202    { return lhs.iterator_!=rhs.iterator_; }
    167203   
    168204    /**
     
    170206     */
    171207    friend bool operator<(const StrideIterator& lhs, const StrideIterator& rhs)
    172     { return lhs.pointer_<rhs.pointer_; }
     208    { return lhs.iterator_<rhs.iterator_; }
    173209   
    174210    /**
     
    176212     */
    177213    friend bool operator<=(const StrideIterator& lhs, const StrideIterator& rhs)
    178     { return !(rhs<lhs); }
     214    { return lhs.iterator_<=rhs.iterator_; }
    179215   
    180216    /**
     
    182218     */
    183219    friend bool operator>(const StrideIterator& lhs, const StrideIterator& rhs)
    184     { return rhs<lhs; }
     220    { return lhs.iterator_>rhs.iterator_; }
    185221   
    186222    /**
     
    188224     */
    189225    friend bool operator>=(const StrideIterator& lhs, const StrideIterator& rhs)
    190     { return !(lhs<rhs); }
     226    { return lhs.iterator_>=rhs.iterator_; }
    191227   
    192228  private:
    193     T* pointer_;
     229    Iter iterator_;
    194230    size_t stride_;
    195231
     
    198234    //StrideIterator& operator=(const StrideIterator&);
    199235  };
     236
     237
     238
     239
     240
    200241}}} // of namespace utility, yat, and theplu
    201242
  • trunk/yat/utility/VectorBase.h

    r1038 r1041  
    9090  public:
    9191    /// \brief VectorBase::const_iterator
    92     typedef StrideIterator<const double> const_iterator;
     92    typedef StrideIterator<const double*> const_iterator;
    9393
    9494    /**
  • trunk/yat/utility/VectorMutable.h

    r1038 r1041  
    9393       \brief mutable iterator
    9494    */
    95     typedef StrideIterator<double> iterator;
     95    typedef StrideIterator<double*> iterator;
    9696
    9797    /**
  • trunk/yat/utility/iterator_traits.h

    r1000 r1041  
    2525*/
    2626
    27 #include "yat/utility/IteratorWeighted.h"
     27#include "IteratorWeighted.h"
    2828
    2929namespace theplu {
     
    6363    typedef weighted_type type;
    6464  };
    65 
    66 
    6765
    6866  /**
Note: See TracChangeset for help on using the changeset viewer.