VectorSTLIterator.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef CASA_VECTORSTLITERATOR_H
00029 #define CASA_VECTORSTLITERATOR_H
00030
00031
00032 #include <casacore/casa/aips.h>
00033 #include <casacore/casa/Arrays/Vector.h>
00034 #include <iterator>
00035
00036 namespace casacore {
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065 template <class T>
00066 class VectorSTLIterator
00067 : public std::iterator<std::random_access_iterator_tag, T> {
00068 public:
00069 typedef T value_type;
00070 typedef value_type* pointer;
00071 typedef const value_type* const_pointer;
00072 typedef VectorSTLIterator<T> iterator;
00073 typedef const VectorSTLIterator<T> const_iterator;
00074 typedef value_type& reference;
00075 typedef const value_type& const_reference;
00076 typedef std::size_t size_type;
00077 typedef ptrdiff_t difference_type;
00078
00079
00080
00081
00082 explicit VectorSTLIterator(const Vector<T> &c)
00083 : start_p(const_cast<T*>(c.data())),
00084 step_p (std::max(ssize_t(1), c.steps()(0))),
00085 iter_p (const_cast<T*>(c.data()))
00086 {}
00087 VectorSTLIterator() : start_p(0), step_p(1), iter_p(0)
00088 {}
00089 VectorSTLIterator(const typename Array<T>::IteratorSTL &c)
00090 : start_p(c.pos()),
00091 step_p (std::max(ssize_t(1), c.steps()(0))),
00092 iter_p (start_p)
00093 {}
00094
00095
00096 VectorSTLIterator(const VectorSTLIterator<T>& that)
00097 : std::iterator<std::random_access_iterator_tag, T>(that),
00098 start_p(that.start_p), step_p(that.step_p),
00099 iter_p(that.iter_p)
00100 {}
00101
00102
00103 VectorSTLIterator<T>& operator=(const VectorSTLIterator<T>& that)
00104 {
00105 if (this != &that) {
00106 std::iterator<std::random_access_iterator_tag, T>::operator=(that);
00107 start_p = that.start_p;
00108 step_p = that.step_p;
00109 iter_p = that.iter_p;
00110 }
00111 return *this;
00112 }
00113
00114
00115 ~VectorSTLIterator() {;}
00116
00117
00118 reference operator[](uInt i) { return *(start_p+i*step_p); };
00119 const_reference operator[](uInt i) const {
00120 return *(start_p+i*step_p); };
00121 reference operator*() { return *iter_p; };
00122 const_reference operator*() const { return *iter_p; };
00123 pointer pos() const {return iter_p; }
00124
00125
00126
00127 const iterator &operator++() { iter_p+=step_p; return *this; };
00128 iterator operator++(int) {
00129 iterator t = *this; iter_p+=step_p; return t; };
00130 iterator &operator--() { iter_p-=step_p; return *this; };
00131 iterator operator--(int) {
00132 VectorSTLIterator<T> t = *this;iter_p-=step_p; return t; };
00133 iterator &operator+=(difference_type i) {
00134 iter_p+=i*step_p; return *this; };
00135 iterator &operator-=(difference_type i) {
00136 iter_p-=i*step_p; return *this; };
00137 iterator operator+(difference_type i) const {
00138 VectorSTLIterator<T> t = *this; return t+=i; };
00139 iterator operator-(difference_type i) const {
00140 VectorSTLIterator<T> t = *this; return t-=i; };
00141
00142
00143
00144 difference_type operator-(const VectorSTLIterator<T> &x) const {
00145 return (iter_p-x.iter_p)/step_p; };
00146
00147
00148
00149 Bool operator== (const iterator &other) const {
00150 return iter_p == other.iter_p; };
00151 Bool operator!= (const iterator other) const {
00152 return iter_p != other.iter_p; };
00153 Bool operator< (const iterator &other) const {
00154 return iter_p < other.iter_p; };
00155 Bool operator== (const_pointer const pos) const {
00156 return iter_p == pos; };
00157 Bool operator!= (const_pointer const pos) const {
00158 return iter_p != pos; };
00159 Bool operator< (const_pointer const pos) const {
00160 return iter_p < pos; };
00161
00162 protected:
00163
00164 pointer const start_p;
00165
00166 difference_type step_p;
00167
00168 pointer iter_p;
00169 };
00170
00171
00172 }
00173
00174 #endif