OPAL (Object Oriented Parallel Accelerator Library) 2022.1
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
31template<class T>
33
34public:
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
93private:
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
106template<class T>
108
109public:
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
168private:
169
170 mutable const T *cursor;
171 mutable std::ptrdiff_t stride;
172};
173
174
175// Ancillary functions.
176// ------------------------------------------------------------------------
177
178template<class T>
179inline std::random_access_iterator_tag
181 return std::random_access_iterator_tag();
182}
183
184template<class T>
185inline std::random_access_iterator_tag
187 return std::random_access_iterator_tag();
188}
189
190template<class T>
191inline T *value_type(const SliceIterator<T> &) {
192 return (T *)(0);
193}
194
195template<class T>
197 return (T *)(0);
198}
199
200
201// Template function implementation for SliceIterator.
202// ------------------------------------------------------------------------
203
204template<class T>
205inline SliceIterator<T>::SliceIterator(T *array, std::ptrdiff_t n):
206 cursor(array), stride(n)
207{}
208
209template<class T>
211 cursor(rhs.cursor), stride(rhs.stride)
212{}
213
214template<class T>
216(const SliceIterator<T> &rhs) {
217 cursor = rhs.cursor;
218 stride = rhs.stride;
219 return *this;
220}
221
222template<class T>
223inline bool SliceIterator<T>::operator==(const SliceIterator<T> &rhs) const {
224 return cursor == rhs.cursor;
225}
226
227template<class T>
228inline bool SliceIterator<T>::operator!=(const SliceIterator<T> &rhs) const {
229 return cursor != rhs.cursor;
230}
231
232template<class T>
234 cursor += stride;
235 return *this;
236}
237
238template<class T>
240 SliceIterator<T> tmp(*this);
241 cursor += stride;
242 return tmp;
243}
244
245template<class T>
247 cursor -= stride;
248 return *this;
249}
250
251template<class T>
253 SliceIterator<T> tmp(*this);
254 cursor -= stride;
255 return tmp;
256}
257
258template<class T>
260 cursor += n * stride;
261 return *this;
262}
263
264template<class T>
266 cursor -= n * stride;
267 return *this;
268}
269
270template<class T>
272 SliceIterator<T> tmp(*this);
273 return tmp += n;
274}
275
276template<class T>
278 SliceIterator<T> tmp(*this);
279 return tmp -= n;
280}
281
282template<class T>
285 return (cursor - rhs.cursor) / stride;
286}
287
288template<class T>
290 return *cursor;
291}
292
293template<class T>
294inline T &SliceIterator<T>::operator[](int n) const {
295 return cursor[n*stride];
296}
297
298
299// Template function implementation for ConstSliceIterator.
300// ------------------------------------------------------------------------
301
302template<class T>
304(const T *array, std::ptrdiff_t n):
305 cursor(array), stride(n)
306{}
307
308template<class T>
310(const ConstSliceIterator<T> &rhs):
311 cursor(rhs.cursor), stride(rhs.stride)
312{}
313
314template<class T>
316(const ConstSliceIterator<T> &rhs) {
317 cursor = rhs.cursor;
318 stride = rhs.stride;
319 return *this;
320}
321
322template<class T>
324(const ConstSliceIterator<T> &rhs) const {
325 return cursor == rhs.cursor;
326}
327
328template<class T>
330(const ConstSliceIterator<T> &rhs) const {
331 return cursor != rhs.cursor;
332}
333
334template<class T>
336 cursor += stride;
337 return *this;
338}
339
340template<class T>
342 ConstSliceIterator<T> tmp(*this);
343 cursor += stride;
344 return tmp;
345}
346
347template<class T>
349 cursor -= stride;
350 return *this;
351}
352
353template<class T>
355 ConstSliceIterator<T> tmp(*this);
356 cursor -= stride;
357 return tmp;
358}
359
360template<class T>
362(std::ptrdiff_t n) {
363 cursor += n * stride;
364 return *this;
365}
366
367template<class T>
369(std::ptrdiff_t n) {
370 cursor -= n * stride;
371 return *this;
372}
373
374template<class T> inline
376 ConstSliceIterator<T> tmp(*this);
377 return tmp += n;
378}
379
380template<class T> inline
382 ConstSliceIterator<T> tmp(*this);
383 return tmp -= n;
384}
385
386template<class T>
389 return (cursor - rhs.cursor) / stride;
390}
391
392template<class T>
393inline const T &ConstSliceIterator<T>::operator*() const {
394 return *cursor;
395}
396
397template<class T>
398inline const T &ConstSliceIterator<T>::operator[](int n) const {
399 return cursor[n*stride];
400}
401
402#endif // CLASSIC_SliceIterator_HH
403
T * value_type(const SliceIterator< T > &)
std::random_access_iterator_tag iterator_category(const SliceIterator< T > &)
Iterator for array slice.
Definition: SliceIterator.h:32
bool operator!=(const SliceIterator< T > &rhs) const
SliceIterator< T > & operator++()
Increment (iterate forward).
T * pointer
The pointer type.
Definition: SliceIterator.h:46
SliceIterator< T > operator+(std::ptrdiff_t)
Add multiple stride.
std::ptrdiff_t difference_type
The pointer difference type.
Definition: SliceIterator.h:43
bool operator==(const SliceIterator< T > &rhs) const
T & operator[](int) const
Delegate.
SliceIterator< T > & operator+=(std::ptrdiff_t)
Increment by multiple stride.
std::random_access_iterator_tag iterator_category
The iterator tag, taken from the standard template library.
Definition: SliceIterator.h:37
SliceIterator< T > operator-(std::ptrdiff_t)
Subtract multiple stride.
SliceIterator< T > & operator--()
Decrement (iterate backward).
T & reference
The reference type.
Definition: SliceIterator.h:49
T & operator*() const
Dereference.
SliceIterator< T > & operator-=(std::ptrdiff_t)
Decrement by multiple stride.
SliceIterator< T > & operator=(const SliceIterator< T > &)
SliceIterator(T *array, std::ptrdiff_t stride)
Constructor.
std::ptrdiff_t stride
Definition: SliceIterator.h:96
T value_type
The value type.
Definition: SliceIterator.h:40
Iterator for array slice.
std::ptrdiff_t stride
ConstSliceIterator< T > & operator++()
Increment (iterate forward).
const T & reference
The reference type.
ConstSliceIterator< T > & operator=(const ConstSliceIterator< T > &)
const T & operator*() const
Dereference.
const T value_type
The value type.
bool operator==(const ConstSliceIterator< T > &rhs) const
ConstSliceIterator< T > & operator+=(std::ptrdiff_t)
Add multiple stride and assign.
bool operator!=(const ConstSliceIterator< T > &rhs) const
std::random_access_iterator_tag iterator_category
The iterator tag, taken from the standard template library.
const T * pointer
The pointer type.
ConstSliceIterator< T > operator+(std::ptrdiff_t) const
Add multiple stride.
ConstSliceIterator< T > operator-(std::ptrdiff_t) const
Subtract multiple stride.
ConstSliceIterator< T > & operator-=(std::ptrdiff_t)
Subtract multiple stride and assign.
ConstSliceIterator< T > & operator--()
Decrement (iterate backward).
const T & operator[](int) const
Delegate.
ConstSliceIterator(const T *array, std::ptrdiff_t stride)
Constructor.
std::ptrdiff_t difference_type
The pointer difference type.