synaps/base/stride_iterator.h

00001 #ifndef SYNAPS_UTIL_STRIDE_ITERATOR_H
00002 #define SYNAPS_UTIL_STRIDE_ITERATOR_H
00003 
00004 #include <synaps/init.h>
00005 
00006 __BEGIN_NAMESPACE_SYNAPS
00007 
00008 template <class coef_t, class pointer_t= coef_t*, class stride_t=unsigned , class reference_t = coef_t& >
00009 class stride_iterator {
00010         public:
00011 
00012                 typedef std::random_access_iterator_tag  iterator_category;
00013                 typedef coef_t                           value_type;
00014                 typedef int                              difference_type;
00015                 typedef pointer_t                        pointer;
00016                 typedef reference_t                      reference;
00017 
00018                 typedef stride_iterator<coef_t,pointer_t,stride_t,reference_t> self_t;
00019 
00020                 stride_iterator(): my_rep(0), my_stride(1) {}
00021                 stride_iterator(pointer_t const ptr,  stride_t const stride)
00022                         : my_rep(ptr), my_stride(stride)         {}
00023 
00024                 self_t& operator++()
00025                 {my_rep += my_stride; return *this;}
00026 
00027                 self_t  operator+(int i)
00028                 {self_t tmp(*this); tmp.my_rep+=my_stride*i; return tmp;}
00029                 self_t  operator+(int i)  const
00030                 {self_t tmp(*this); tmp.my_rep+=my_stride*i; return tmp;}
00031 
00032                 self_t& operator=(const self_t & it)
00033                 {my_rep = it.my_rep; my_stride= it.my_stride; return *this;}
00034 
00035                 reference_t operator* () {return(*my_rep);}
00036                 pointer_t   operator->() {return  my_rep;}
00037 
00038                 bool operator!=( self_t const& rhs) { return my_rep != rhs.my_rep; }
00039                 bool operator<=( self_t const& rhs) { return my_rep <= rhs.my_rep; }
00040 
00041         private:
00042                 pointer_t my_rep;
00043                 stride_t  my_stride;
00044 };
00045 //----------------------------------------------------------------------
00046 template <class T, class T_ptr, class T_stride, class T_ref>
00047 stride_iterator<T,T_ptr,T_stride,T_ref>&
00048 operator++(const stride_iterator<T,T_ptr,T_stride,T_ref> & it)   {return ++it;}
00049 //----------------------------------------------------------------------
00050 
00051 __END_NAMESPACE_SYNAPS
00052 
00053 #endif //stride_iterator_H
00054 

SYNAPS DOCUMENTATION
logo