OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
SliceIterator.h
Go to the documentation of this file.
1 #ifndef CLASSIC_SliceIterator_HH
2 #define CLASSIC_SliceIterator_HH
3 
4 // ------------------------------------------------------------------------
5 // $RCSfile: SliceIterator.h,v $
6 // ------------------------------------------------------------------------
7 // $Revision: 1.1.1.1 $
8 // ------------------------------------------------------------------------
9 // Copyright: see Copyright.readme
10 // ------------------------------------------------------------------------
11 //
12 // Template class: SliceIterator
13 //
14 // ------------------------------------------------------------------------
15 // Class category: Algebra
16 // ------------------------------------------------------------------------
17 //
18 // $Date: 2000/03/27 09:32:32 $
19 // $Author: fci $
20 //
21 // ------------------------------------------------------------------------
22 
23 #include <iterator>
24 
25 // Class SliceIterator.
26 // ------------------------------------------------------------------------
28 // An iterator permitting to iterate on a non-constant array
29 // with a stride different from 1.
30 
31 template<class T>
33 
34 public:
35 
37  typedef std::random_access_iterator_tag iterator_category;
38 
40  typedef T value_type;
41 
43  typedef std::ptrdiff_t difference_type;
44 
46  typedef T *pointer;
47 
49  typedef T &reference;
50 
52  // Construct iterator for [b]array[/b], given a [b]stride[/b].
53  SliceIterator(T *array, std::ptrdiff_t stride);
54 
57  bool operator==(const SliceIterator<T> &rhs) const;
58  bool operator!=(const SliceIterator<T> &rhs) const;
59 
62 
65 
68 
71 
73  SliceIterator<T> &operator+=(std::ptrdiff_t);
74 
76  SliceIterator<T> &operator-=(std::ptrdiff_t);
77 
79  SliceIterator<T> operator+(std::ptrdiff_t);
80 
82  SliceIterator<T> operator-(std::ptrdiff_t);
83 
86 
88  T &operator*() const;
89 
91  T &operator[](int) const;
92 
93 private:
94 
96  std::ptrdiff_t stride;
97 };
98 
99 
100 // Class ConstSliceIterator.
101 // ------------------------------------------------------------------------
103 // An iterator permitting to iterate on a constant array
104 // with a stride different from 1.
105 
106 template<class T>
108 
109 public:
110 
112  typedef std::random_access_iterator_tag iterator_category;
113 
115  typedef const T value_type;
116 
118  typedef std::ptrdiff_t difference_type;
119 
121  typedef const T *pointer;
122 
124  typedef const T &reference;
125 
127  // Construct iterator for [b]array[/b], given a [b]stride[/b].
128  ConstSliceIterator(const T *array, std::ptrdiff_t stride);
129 
132  bool operator==(const ConstSliceIterator<T> &rhs) const;
133  bool operator!=(const ConstSliceIterator<T> &rhs) const;
134 
137 
140 
143 
146 
148  ConstSliceIterator<T> &operator+=(std::ptrdiff_t);
149 
151  ConstSliceIterator<T> &operator-=(std::ptrdiff_t);
152 
154  ConstSliceIterator<T> operator+(std::ptrdiff_t) const;
155 
157  ConstSliceIterator<T> operator-(std::ptrdiff_t) const;
158 
161 
163  const T &operator*() const;
164 
166  const T &operator[](int) const;
167 
168 private:
169 
170  mutable const T *cursor;
171  mutable std::ptrdiff_t stride;
172 };
173 
174 
175 // Ancillary functions.
176 // ------------------------------------------------------------------------
177 
178 template<class T>
179 inline std::random_access_iterator_tag
181  return std::random_access_iterator_tag();
182 }
183 
184 template<class T>
185 inline std::random_access_iterator_tag
187  return std::random_access_iterator_tag();
188 }
189 
190 template<class T>
191 inline T *value_type(const SliceIterator<T> &) {
192  return (T *)(0);
193 }
194 
195 template<class T>
197  return (T *)(0);
198 }
199 
200 
201 // Template function implementation for SliceIterator.
202 // ------------------------------------------------------------------------
203 
204 template<class T>
205 inline SliceIterator<T>::SliceIterator(T *array, std::ptrdiff_t n):
206  cursor(array), stride(n)
207 {}
208 
209 template<class T>
211  cursor(rhs.cursor), stride(rhs.stride)
212 {}
213 
214 template<class T>
216 (const SliceIterator<T> &rhs) {
217  cursor = rhs.cursor;
218  stride = rhs.stride;
219  return *this;
220 }
221 
222 template<class T>
223 inline bool SliceIterator<T>::operator==(const SliceIterator<T> &rhs) const {
224  return cursor == rhs.cursor;
225 }
226 
227 template<class T>
228 inline bool SliceIterator<T>::operator!=(const SliceIterator<T> &rhs) const {
229  return cursor != rhs.cursor;
230 }
231 
232 template<class T>
234  cursor += stride;
235  return *this;
236 }
237 
238 template<class T>
240  SliceIterator<T> tmp(*this);
241  cursor += stride;
242  return tmp;
243 }
244 
245 template<class T>
247  cursor -= stride;
248  return *this;
249 }
250 
251 template<class T>
253  SliceIterator<T> tmp(*this);
254  cursor -= stride;
255  return tmp;
256 }
257 
258 template<class T>
260  cursor += n * stride;
261  return *this;
262 }
263 
264 template<class T>
266  cursor -= n * stride;
267  return *this;
268 }
269 
270 template<class T>
272  SliceIterator<T> tmp(*this);
273  return tmp += n;
274 }
275 
276 template<class T>
278  SliceIterator<T> tmp(*this);
279  return tmp -= n;
280 }
281 
282 template<class T>
283 inline typename SliceIterator<T>::difference_type
285  return (cursor - rhs.cursor) / stride;
286 }
287 
288 template<class T>
290  return *cursor;
291 }
292 
293 template<class T>
294 inline T &SliceIterator<T>::operator[](int n) const {
295  return cursor[n*stride];
296 }
297 
298 
299 // Template function implementation for ConstSliceIterator.
300 // ------------------------------------------------------------------------
301 
302 template<class T>
304 (const T *array, std::ptrdiff_t n):
305  cursor(array), stride(n)
306 {}
307 
308 template<class T>
311  cursor(rhs.cursor), stride(rhs.stride)
312 {}
313 
314 template<class T>
316 (const ConstSliceIterator<T> &rhs) {
317  cursor = rhs.cursor;
318  stride = rhs.stride;
319  return *this;
320 }
321 
322 template<class T>
323 inline bool ConstSliceIterator<T>::operator==
324 (const ConstSliceIterator<T> &rhs) const {
325  return cursor == rhs.cursor;
326 }
327 
328 template<class T>
329 inline bool ConstSliceIterator<T>::operator!=
330 (const ConstSliceIterator<T> &rhs) const {
331  return cursor != rhs.cursor;
332 }
333 
334 template<class T>
336  cursor += stride;
337  return *this;
338 }
339 
340 template<class T>
342  ConstSliceIterator<T> tmp(*this);
343  cursor += stride;
344  return tmp;
345 }
346 
347 template<class T>
349  cursor -= stride;
350  return *this;
351 }
352 
353 template<class T>
355  ConstSliceIterator<T> tmp(*this);
356  cursor -= stride;
357  return tmp;
358 }
359 
360 template<class T>
361 inline ConstSliceIterator<T> &ConstSliceIterator<T>::operator+=
362 (std::ptrdiff_t n) {
363  cursor += n * stride;
364  return *this;
365 }
366 
367 template<class T>
368 inline ConstSliceIterator<T> &ConstSliceIterator<T>::operator-=
369 (std::ptrdiff_t n) {
370  cursor -= n * stride;
371  return *this;
372 }
373 
374 template<class T> inline
376  ConstSliceIterator<T> tmp(*this);
377  return tmp += n;
378 }
379 
380 template<class T> inline
382  ConstSliceIterator<T> tmp(*this);
383  return tmp -= n;
384 }
385 
386 template<class T>
389  return (cursor - rhs.cursor) / stride;
390 }
391 
392 template<class T>
393 inline const T &ConstSliceIterator<T>::operator*() const {
394  return *cursor;
395 }
396 
397 template<class T>
398 inline const T &ConstSliceIterator<T>::operator[](int n) const {
399  return cursor[n*stride];
400 }
401 
402 #endif // CLASSIC_SliceIterator_HH
403 
T * pointer
The pointer type.
Definition: SliceIterator.h:46
T & operator*() const
Dereference.
Iterator for array slice.
Definition: SliceIterator.h:32
const T * pointer
The pointer type.
ConstSliceIterator< T > operator+(std::ptrdiff_t) const
Add multiple stride.
Definition: rbendmap.h:8
std::ptrdiff_t stride
Definition: SliceIterator.h:96
bool operator==(const ConstSliceIterator< T > &rhs) const
SliceIterator< T > & operator=(const SliceIterator< T > &)
const T & reference
The reference type.
const T & operator[](int) const
Delegate.
bool operator!=(const ConstSliceIterator< T > &rhs) const
SliceIterator< T > operator-(std::ptrdiff_t)
Subtract multiple stride.
ConstSliceIterator< T > operator-(std::ptrdiff_t) const
Subtract multiple stride.
std::random_access_iterator_tag iterator_category(const SliceIterator< T > &)
SliceIterator< T > & operator-=(std::ptrdiff_t)
Decrement by multiple stride.
Iterator for array slice.
std::ptrdiff_t difference_type
The pointer difference type.
ConstSliceIterator< T > & operator-=(std::ptrdiff_t)
Subtract multiple stride and assign.
SliceIterator< T > & operator+=(std::ptrdiff_t)
Increment by multiple stride.
T & reference
The reference type.
Definition: SliceIterator.h:49
bool operator!=(const SliceIterator< T > &rhs) const
ConstSliceIterator< T > & operator=(const ConstSliceIterator< T > &)
std::random_access_iterator_tag iterator_category
The iterator tag, taken from the standard template library.
Definition: SliceIterator.h:37
const T & operator*() const
Dereference.
ConstSliceIterator< T > & operator--()
Decrement (iterate backward).
std::ptrdiff_t stride
std::random_access_iterator_tag iterator_category
The iterator tag, taken from the standard template library.
T * value_type(const SliceIterator< T > &)
T value_type
The value type.
Definition: SliceIterator.h:40
SliceIterator< T > & operator++()
Increment (iterate forward).
const T value_type
The value type.
ConstSliceIterator< T > & operator+=(std::ptrdiff_t)
Add multiple stride and assign.
SliceIterator< T > operator+(std::ptrdiff_t)
Add multiple stride.
ConstSliceIterator(const T *array, std::ptrdiff_t stride)
Constructor.
bool operator==(const SliceIterator< T > &rhs) const
std::ptrdiff_t difference_type
The pointer difference type.
Definition: SliceIterator.h:43
T & operator[](int) const
Delegate.
ConstSliceIterator< T > & operator++()
Increment (iterate forward).
SliceIterator< T > & operator--()
Decrement (iterate backward).
SliceIterator(T *array, std::ptrdiff_t stride)
Constructor.