C++ mpi module for stochmagnet_main Package
CORE_ConstantStrideIterator.h
1 #ifndef CORE_ConstantStrideIterator_HPP
2 #define CORE_ConstantStrideIterator_HPP
3 
4 #include <iterator>
5 
11 template<class Ptr_Type, tInt Step>
13 
14 public:
15 
18  typedef typename std::iterator_traits<Ptr_Type>::value_type value_type;
21  typedef typename std::iterator_traits<Ptr_Type>::reference reference;
24  typedef typename std::iterator_traits<Ptr_Type>::pointer pointer;
25 
28  typedef typename std::iterator_traits<Ptr_Type>::difference_type difference_type;
31  typedef std::random_access_iterator_tag iterator_category;
32 
33 private:
36  typedef CORE_ConstantStrideIterator self;
37 
38 private:
39 
40  Ptr_Type mPointer;
41 
42 public:
43 
44  //constructors
45  //============
46 
49  CORE_ConstantStrideIterator() : mPointer(NULL) {}
50 
54  explicit CORE_ConstantStrideIterator(Ptr_Type x) : mPointer(x) {}
55 
59  CORE_ConstantStrideIterator(const self& x) : mPointer(x.mPointer) {}
60 
64  }
65 
66  //accessor operators
67  //=====================
71  const reference operator*() { return *mPointer; }
72 
76  const reference operator[](difference_type n) { return mPointer[n * Step]; }
77 
78 
79  //compount assignement operators
80  //===============================
81 
86  mPointer += x * Step;
87  return *this;
88  }
89 
90 
91  //increment & decrement operators
92  //================================
95  self& operator++() { mPointer += Step; return *this; }
96 
100  self operator++(int s) { self tmp = *this; mPointer += Step; return tmp; }
101 
104  self& operator--() { mPointer -= Step; return *this; }
105 
109  self operator--(int s) { self tmp = *this; mPointer -= Step; return tmp; }
110 
111 
112  //arithmetic operators
113  //====================
114 
115 
121  friend self operator+(self x, difference_type y) {
122  return x += y ;
123  }
129  friend self operator+(difference_type x, self y) {
130  return y += x ;
131  }
136  friend difference_type operator-(self x, self y) {
137  return (x.mPointer - y.mPointer) / Step;
138 
139  }
140 
141  //relational & comparison operators
142  //=================================
143 
144 
149  friend bool operator==(self x, self y) {
150  //std::cout<<"== x:"<<x.mPointer<<" y:"<<y.mPointer<<" y-x:"<<y.mPointer-x.mPointer<<"\n";
151  return x.mPointer == y.mPointer;
152  }
157  friend bool operator!=(self x, self y) {
158  //std::cout<<"!= x:"<<x.mPointer<<" y:"<<y.mPointer<<" y-x:"<<y.mPointer-x.mPointer<<"\n";
159  return x.mPointer != y.mPointer;
160  }
165  friend bool operator<(self x, self y) {
166  //std::cout<<"< x:"<<x.mPointer<<" y:"<<y.mPointer<<" y-x:"<<y.mPointer-x.mPointer<<"\n";
167  return x.mPointer < y.mPointer;
168  }
169 
170  //logical operators
171  //=================
172 
173  //bitwise operators
174  //=================
175 
176 
177 
178 
179 
180 };
181 
182 #endif
this class describes a const iterator with a constant stride
Definition: CORE_ConstantStrideIterator.h:12
std::iterator_traits< Ptr_Type >::reference reference
reference value type
Definition: CORE_ConstantStrideIterator.h:21
self operator++(int s)
return the current element and set the iterator to s next element
Definition: CORE_ConstantStrideIterator.h:100
friend bool operator==(self x, self y)
compare the 2 iterator if equal
Definition: CORE_ConstantStrideIterator.h:149
std::iterator_traits< Ptr_Type >::value_type value_type
copy value type
Definition: CORE_ConstantStrideIterator.h:18
self operator--(int s)
return the current element and set the iterator to previous element
Definition: CORE_ConstantStrideIterator.h:109
self & operator+=(difference_type x)
move the iterator on x elements
Definition: CORE_ConstantStrideIterator.h:85
friend difference_type operator-(self x, self y)
return the number of difference elements number between 2 iterators
Definition: CORE_ConstantStrideIterator.h:136
self & operator--()
return the previous element and set the iterator to previous element
Definition: CORE_ConstantStrideIterator.h:104
self & operator++()
return the next element and set the iterator to next element
Definition: CORE_ConstantStrideIterator.h:95
virtual ~CORE_ConstantStrideIterator()
destructor of class
Definition: CORE_ConstantStrideIterator.h:63
std::iterator_traits< Ptr_Type >::difference_type difference_type
displacement type
Definition: CORE_ConstantStrideIterator.h:28
friend self operator+(difference_type x, self y)
add x elements to y iterator : y:=y+x
Definition: CORE_ConstantStrideIterator.h:129
CORE_ConstantStrideIterator()
default constructor
Definition: CORE_ConstantStrideIterator.h:49
friend self operator+(self x, difference_type y)
add y element to x iterator : x:=x+y
Definition: CORE_ConstantStrideIterator.h:121
CORE_ConstantStrideIterator(Ptr_Type x)
initializer constructor
Definition: CORE_ConstantStrideIterator.h:54
CORE_ConstantStrideIterator(const self &x)
copy constructor
Definition: CORE_ConstantStrideIterator.h:59
const reference operator[](difference_type n)
access to n-th element of the list
Definition: CORE_ConstantStrideIterator.h:76
friend bool operator<(self x, self y)
compare the 2 iterators on same values
Definition: CORE_ConstantStrideIterator.h:165
const reference operator*()
return the pointer of the iterator
Definition: CORE_ConstantStrideIterator.h:71
std::iterator_traits< Ptr_Type >::pointer pointer
pointer value type
Definition: CORE_ConstantStrideIterator.h:24
friend bool operator!=(self x, self y)
compare the 2 iterator if not equal
Definition: CORE_ConstantStrideIterator.h:157
std::random_access_iterator_tag iterator_category
type of iterator
Definition: CORE_ConstantStrideIterator.h:31