// // $COPYRIGHT$ // // //=========================================================================== #ifndef MTL_STRIDED_ITERATOR_H #define MTL_STRIDED_ITERATOR_H #include "mtl/mtl_iterator.h" #include "mtl/mtl_config.h" #include "mtl/meta_if.h" namespace mtl { //: strided iterator // // This iterator moves a constant stride for each increment or // decrement operator invoked. The strided iterator is used // to implement a row-view to column oriented matrices, or // column-views to row oriented matrices. // //!category: iterators, adaptors //!component: type //!defined: strided_iterator.h //!models: RandomAccessIterator //!tparams: RandomAccessIterator - the base iterator type template class strided_iterator { typedef strided_iterator self; public: #if !defined( _MSVCPP_ ) //: The type for the difference between two iterators typedef typename std::iterator_traits::difference_type difference_type; #else typedef typename std::iterator_traits::distance_type difference_type; typedef difference_type distance_type; #endif //: The value type pointed to by this iterator type typedef typename std::iterator_traits::value_type value_type; //: The iterator category for this iterator typedef typename std::iterator_traits::iterator_category iterator_category; #if !defined( _MSVCPP_ ) //: The type for references to the value type typedef typename std::iterator_traits::reference reference; //: The type for pointers to the value type typedef typename std::iterator_traits::pointer pointer; #else typedef typename IF::RET reference; typedef typename IF::RET pointer; #endif typedef difference_type Distance; //: The underlying iterator type typedef RandomAccessIterator iterator_type; //: Default Constructor inline strided_iterator() : stride(0), pos(0) { } //: Construct from the underlying iterator inline strided_iterator(const RandomAccessIterator& x, int s, int p) : iter(x), stride(s), pos(p) { } //: Copy Constructor inline strided_iterator(const self& x) : iter(x.iter), stride(x.stride), pos(x.pos) { } //: Assignment Operator inline self& operator=(const self& x) { iter = x.iter; stride = x.stride; pos = x.pos; return *this; } //: Return the index of the element this iterator points to //!wheredef: IndexedIterator inline int index() const { return pos; } //: Convert to the underlying iterator inline operator RandomAccessIterator () const { return iter; } inline RandomAccessIterator base() const { return iter; } //: Dereference, return the element currently pointed to inline reference operator*() const { return *iter; } //: Pre-increment operator inline self& operator++ (){ ++pos; iter += stride; return *this; } //: Post-increment operator inline self operator++ (int){ self tmp = *this; ++pos; iter += stride; return tmp; } //: Pre-decrement operator inline self& operator-- (){ --pos; iter -= stride; return *this; } //: Post-decrement operator inline self operator-- (int){ self tmp = *this; --pos; iter -= stride; return tmp; } //: Add this iterator and n inline self operator+ (Distance n) const { return self (iter + n*stride, stride, pos + n); } //: Add distance n to this iterator inline self& operator+= (Distance n) { iter += n*stride; pos += n; return *this; } //: Subtract this iterator and distance n inline self operator- (Distance n) const { return self (iter - n*stride, stride, pos - n); } //: Subtract distance n from this iterator inline self& operator-= (Distance n) { iter -= n*stride; pos -= n; return *this; } //: Add this iterator and iterator x inline self operator+ (const self& x) const { return self(iter + x.iter, stride, pos + x.pos); } //: Return this distance between this iterator and iterator x inline Distance operator- (const self& x) const { return pos - x.pos; } //: Return *(i + n) inline reference operator[] (Distance n) const { return *(*this+n); } //: Return whether this iterator is equal to iterator x inline bool operator==(const self& x) const { return pos == x.pos; } //: Return whether this iterator is not equal to iterator x inline bool operator!=(const self& x) const { return pos != x.pos; } //: Return whether this iterator is less than iterator x inline bool operator<(const self& x) const { return pos < x.pos; } protected: RandomAccessIterator iter; int stride; int pos; }; #if 0 template inline strided_iterator str(const RandomAccessIterator& x, int stride = 1) { return strided_iterator(x, stride); } #endif } /* namespace mtl */ #endif