OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
FSlice.h
Go to the documentation of this file.
1#ifndef CLASSIC_FSlice_HH
2#define CLASSIC_FSlice_HH
3
4// ------------------------------------------------------------------------
5// $RCSfile: FSlice.h,v $
6// ------------------------------------------------------------------------
7// $Revision: 1.1.1.1.2.2 $
8// ------------------------------------------------------------------------
9// Copyright: see Copyright.readme
10// ------------------------------------------------------------------------
11//
12// Template class: FSlice
13//
14// ------------------------------------------------------------------------
15// Class category: FixedAlgebra
16// ------------------------------------------------------------------------
17//
18// $Date: 2004/11/12 18:57:54 $
19// $Author: adelmann $
20//
21// ------------------------------------------------------------------------
22
23#include <iterator>
24
25// Class FSlice.
26// ------------------------------------------------------------------------
28
29template<class T, int S>
30class FSlice {
31
32public:
33
35 typedef std::random_access_iterator_tag iterator_category;
36
38 typedef T value_type;
39
41 typedef std::ptrdiff_t difference_type;
42
44 typedef T *pointer;
45
47 typedef T &reference;
48
50 explicit FSlice(T *);
51
52 FSlice(const FSlice &);
53 FSlice &operator=(const FSlice &);
54 bool operator==(const FSlice &rhs) const;
55 bool operator!=(const FSlice &rhs) const;
56
59
61 FSlice operator++(int);
62
65
67 FSlice operator--(int);
68
70 FSlice &operator+=(std::ptrdiff_t);
71
73 FSlice &operator-=(std::ptrdiff_t);
74
76 FSlice operator+(std::ptrdiff_t);
77
79 FSlice operator-(std::ptrdiff_t);
80
82 difference_type operator-(const FSlice &) const;
83
85 T &operator*() const;
86
88 T &operator[](int) const;
89
90private:
91
93};
94
95
96// Class FConstSlice.
97// ------------------------------------------------------------------------
99
100template<class T, int S>
102
103public:
104
106 typedef std::random_access_iterator_tag iterator_category;
107
109 typedef const T value_type;
110
112 typedef std::ptrdiff_t difference_type;
113
115 typedef const T *pointer;
116
118 typedef const T &reference;
119
121 FConstSlice(const T *);
122
123 FConstSlice(const FConstSlice &rhs);
125 bool operator==(const FConstSlice &rhs) const;
126 bool operator!=(const FConstSlice &rhs) const;
127
130
133
136
139
141 FConstSlice &operator+=(std::ptrdiff_t);
142
144 FConstSlice &operator-=(std::ptrdiff_t);
145
147 FConstSlice operator+(std::ptrdiff_t) const;
148
150 FConstSlice operator-(std::ptrdiff_t) const;
151
154
156 const T &operator*() const;
157
159 const T &operator[](int) const;
160
161private:
162
163 mutable const T *cursor;
164};
165
166
167// Ancillary functions.
168// ------------------------------------------------------------------------
169
170template<class T, int S>
171inline std::random_access_iterator_tag
173 return std::random_access_iterator_tag();
174}
175
176template<class T, int S>
177inline std::random_access_iterator_tag
179 return std::random_access_iterator_tag();
180}
181
182template<class T, int S>
183inline T *value_type(const FSlice<T, S> &) {
184 return (T *)(0);
185}
186
187template<class T, int S>
189 return (T *)(0);
190}
191
192
193// Template function implementation for FSlice.
194// ------------------------------------------------------------------------
195
196template<class T, int S>
197inline FSlice<T, S>::FSlice(T *array):
198 cursor(array)
199{}
200
201template<class T, int S>
202inline FSlice<T, S>::FSlice(const FSlice &rhs):
203 cursor(rhs.cursor)
204{}
205
206template<class T, int S>
208 cursor = rhs.cursor;
209 return *this;
210}
211
212template<class T, int S>
213inline bool FSlice<T, S>::operator==(const FSlice &rhs) const {
214 return cursor == rhs.cursor;
215}
216
217template<class T, int S>
218inline bool FSlice<T, S>::operator!=(const FSlice &rhs) const {
219 return cursor != rhs.cursor;
220}
221
222template<class T, int S>
224 cursor += S;
225 return *this;
226}
227
228template<class T, int S>
230 FSlice tmp(*this);
231 cursor += S;
232 return tmp;
233}
234
235template<class T, int S>
237 cursor -= S;
238 return *this;
239}
240
241template<class T, int S>
243 FSlice tmp(*this);
244 cursor -= S;
245 return tmp;
246}
247
248template<class T, int S>
249inline FSlice<T, S> &FSlice<T, S>::operator+=(std::ptrdiff_t n) {
250 cursor += n * S;
251 return *this;
252}
253
254template<class T, int S>
255inline FSlice<T, S> &FSlice<T, S>::operator-=(std::ptrdiff_t n) {
256 cursor -= n * S;
257 return *this;
258}
259
260template<class T, int S>
261inline FSlice<T, S> FSlice<T, S>::operator+(std::ptrdiff_t n) {
262 FSlice tmp(*this);
263 return tmp += n;
264}
265
266template<class T, int S>
267inline FSlice<T, S> FSlice<T, S>::operator-(std::ptrdiff_t n) {
268 FSlice tmp(*this);
269 return tmp -= n;
270}
271
272template<class T, int S>
273inline typename FSlice<T, S>::difference_type
275 return (cursor - rhs.cursor) / S;
276}
277
278template<class T, int S>
279inline T &FSlice<T, S>::operator*() const {
280 return *cursor;
281}
282
283template<class T, int S>
284inline T &FSlice<T, S>::operator[](int n) const {
285 return cursor[n*S];
286}
287
288
289// Template function implementation for FConstSlice.
290// ------------------------------------------------------------------------
291
292template<class T, int S>
294 cursor(array)
295{}
296
297template<class T, int S>
299 cursor(rhs.cursor)
300{}
301
302template<class T, int S>
304 cursor = rhs.cursor;
305 return *this;
306}
307
308template<class T, int S>
309inline bool FConstSlice<T, S>::operator==(const FConstSlice &rhs) const {
310 return cursor == rhs.cursor;
311}
312
313template<class T, int S>
314inline bool FConstSlice<T, S>::operator!=(const FConstSlice &rhs) const {
315 return cursor != rhs.cursor;
316}
317
318template<class T, int S>
320 cursor += S;
321 return *this;
322}
323
324template<class T, int S>
326 FConstSlice tmp(*this);
327 cursor += S;
328 return tmp;
329}
330
331template<class T, int S>
333 cursor -= S;
334 return *this;
335}
336
337template<class T, int S>
339 FConstSlice tmp(*this);
340 cursor -= S;
341 return tmp;
342}
343
344template<class T, int S>
346 cursor += n * S;
347 return *this;
348}
349
350template<class T, int S>
352 cursor -= n * S;
353 return *this;
354}
355
356template<class T, int S> inline
358 FConstSlice tmp(*this);
359 return tmp += n;
360}
361
362template<class T, int S> inline
364 FConstSlice tmp(*this);
365 return tmp -= n;
366}
367
368template<class T, int S>
371 return (cursor - rhs.cursor) / S;
372}
373
374template<class T, int S>
375inline const T &FConstSlice<T, S>::operator*() const {
376 return *cursor;
377}
378
379template<class T, int S>
380inline const T &FConstSlice<T, S>::operator[](int n) const {
381 return cursor[n*S];
382}
383
384#endif // CLASSIC_FSlice_HH
385
T * value_type(const FSlice< T, S > &)
Definition: FSlice.h:183
std::random_access_iterator_tag iterator_category(const FSlice< T, S > &)
Definition: FSlice.h:172
An iterator permitting to iterate with a stride different from 1.
Definition: FSlice.h:30
T & operator[](int) const
Delegate.
Definition: FSlice.h:284
FSlice & operator--()
Decrement (iterate backward).
Definition: FSlice.h:236
T * cursor
Definition: FSlice.h:92
FSlice & operator+=(std::ptrdiff_t)
Increment by multiple stride.
Definition: FSlice.h:249
FSlice & operator=(const FSlice &)
Definition: FSlice.h:207
FSlice & operator-=(std::ptrdiff_t)
Decrement by multiple stride.
Definition: FSlice.h:255
FSlice operator+(std::ptrdiff_t)
Add multiple stride.
Definition: FSlice.h:261
T & operator*() const
Dereference.
Definition: FSlice.h:279
bool operator==(const FSlice &rhs) const
Definition: FSlice.h:213
T * pointer
The pointer type.
Definition: FSlice.h:44
FSlice & operator++()
Increment (iterate forward).
Definition: FSlice.h:223
std::random_access_iterator_tag iterator_category
The iterator tag, taken from the standard template library.
Definition: FSlice.h:35
std::ptrdiff_t difference_type
The pointer difference type.
Definition: FSlice.h:41
bool operator!=(const FSlice &rhs) const
Definition: FSlice.h:218
FSlice(T *)
Constructor for array.
Definition: FSlice.h:197
T value_type
The value type.
Definition: FSlice.h:38
T & reference
The reference type.
Definition: FSlice.h:47
FSlice operator-(std::ptrdiff_t)
Subtract multiple stride.
Definition: FSlice.h:267
Constant version of FSlice.
Definition: FSlice.h:101
FConstSlice & operator++()
Increment (iterate forward).
Definition: FSlice.h:319
bool operator!=(const FConstSlice &rhs) const
Definition: FSlice.h:314
const T & operator[](int) const
Delegate.
Definition: FSlice.h:380
const T & reference
The reference type.
Definition: FSlice.h:118
const T value_type
The value type.
Definition: FSlice.h:109
FConstSlice & operator--()
Decrement (iterate backward).
Definition: FSlice.h:332
bool operator==(const FConstSlice &rhs) const
Definition: FSlice.h:309
std::random_access_iterator_tag iterator_category
The iterator tag, taken from the standard template library.
Definition: FSlice.h:106
const T & operator*() const
Dereference.
Definition: FSlice.h:375
FConstSlice & operator+=(std::ptrdiff_t)
Add multiple stride and assign.
Definition: FSlice.h:345
FConstSlice operator-(std::ptrdiff_t) const
Subtract multiple stride.
Definition: FSlice.h:363
FConstSlice & operator=(const FConstSlice &)
Definition: FSlice.h:303
FConstSlice operator+(std::ptrdiff_t) const
Add multiple stride.
Definition: FSlice.h:357
FConstSlice(const T *)
Constructor from array and stride.
Definition: FSlice.h:293
const T * cursor
Definition: FSlice.h:163
std::ptrdiff_t difference_type
The pointer difference type.
Definition: FSlice.h:112
FConstSlice & operator-=(std::ptrdiff_t)
Subtract multiple stride and assign.
Definition: FSlice.h:351
const T * pointer
The pointer type.
Definition: FSlice.h:115