stridedPointer.h

Go to the documentation of this file.
00001 
00014 #ifndef _DLR_COMMON_STRIDEDPOINTER_H_
00015 #define _DLR_COMMON_STRIDEDPOINTER_H_
00016 
00017 #include <iterator>
00018 
00019 namespace dlr {
00020 
00021   namespace common {
00022     
00031     template <class Type>
00032     class StridedPointer
00033       : public std::iterator<std::random_access_iterator_tag, Type, ptrdiff_t>
00034     {
00035     public:
00040       StridedPointer() : m_pointer(0), m_stride(1) {}
00041 
00049       StridedPointer(Type* pointer) : m_pointer(pointer), m_stride(1) {}
00050     
00060       StridedPointer(Type* pointer, int stride)
00061         : m_pointer(pointer), m_stride(stride) {}
00062 
00069       StridedPointer(const StridedPointer<Type>& source)
00070         : m_pointer(source.m_pointer), m_stride(source.m_stride) {}
00071 
00075       ~StridedPointer() {}
00076 
00090       Type&
00091       operator*() {return *m_pointer;}
00092 
00105       Type* operator->() {return m_pointer;}
00106 
00115       StridedPointer<Type>& operator++() {m_pointer += m_stride; return *this;}
00116     
00124       StridedPointer<Type> operator++(int) {
00125         StridedPointer<Type> result(*this);
00126         ++(*this);
00127         return result;
00128       }
00129 
00136       StridedPointer<Type>& operator--() {m_pointer -= m_stride; return *this;}
00137 
00144       StridedPointer<Type> operator--(int) {
00145         StridedPointer<Type> result(*this);
00146         --(*this);
00147         return result;
00148       }
00149 
00161       StridedPointer<Type> operator+(ptrdiff_t offset) {
00162         StridedPointer<Type> result(*this);
00163         result += offset;
00164         return result;
00165       }
00166 
00175       StridedPointer<Type> operator-(ptrdiff_t offset) {
00176         StridedPointer<Type> result(*this);
00177         result -= offset;
00178         return result;
00179       }
00180 
00193       ptrdiff_t operator-(const StridedPointer<Type>& other) {
00194         ptrdiff_t distance = m_pointer - other.m_pointer;
00195         return distance / m_stride;
00196       }
00197     
00206       StridedPointer<Type>& operator+=(ptrdiff_t offset) {
00207         m_pointer += offset * m_stride;
00208         return *this;
00209       }
00210 
00219       StridedPointer<Type>& operator-=(ptrdiff_t offset) {
00220         m_pointer -= offset * m_stride;
00221         return *this;
00222       }
00223 
00232       bool operator==(const StridedPointer<Type>& other) const {
00233         return m_pointer == other.m_pointer;
00234       }
00235 
00244       bool operator!=(const StridedPointer<Type>& other) const {
00245         return m_pointer != other.m_pointer;
00246       }
00247 
00259       bool operator<(const StridedPointer<Type>& other) const {
00260         return m_pointer < other.m_pointer;
00261       }
00262 
00274       bool operator>(const StridedPointer<Type>& other) const {
00275         return m_pointer > other.m_pointer;
00276       }
00277 
00289       bool operator<=(const StridedPointer<Type>& other) const {
00290         return m_pointer <= other.m_pointer;
00291       }
00292 
00304       bool operator>=(const StridedPointer<Type>& other) const {
00305         return m_pointer >= other.m_pointer;
00306       }
00307 
00315       StridedPointer<Type>& operator=(const StridedPointer<Type>& source) {
00316         m_pointer = source.m_pointer;
00317         m_stride = source.m_stride;
00318         return *this;
00319       }
00320 
00321     private:
00322       Type* m_pointer;
00323       int m_stride;
00324     };
00325 
00326   } // namespace common
00327   
00328 } // namespace dlr
00329 
00330 
00331 /* ======= Declarations to maintain compatibility with legacy code. ======= */
00332 
00333 namespace dlr {
00334 
00335   using common::StridedPointer;
00336   
00337 } // namespace dlr
00338 
00339 #endif // #ifndef _DLR_COMMON_STRIDEDPOINTER_H_

Generated on Tue Nov 24 23:57:55 2009 for dlrCommon Utility Library by  doxygen 1.5.8