StrideIterator.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2025 OpenCFD Ltd.
9  Copyright (C) 2026 Keysight Technologies
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Class
28  Foam::StrideIndexer
29 
30 Description
31  Indexer or generator for strided integral values.
32  The extended base functionality (eg, prev, next) allows its reuse for
33  StrideIterator etc.
34 
35 Class
36  Foam::StrideIterator
37 
38 Description
39  A random-access, integer-like, input iterator for integral values with
40  a stride.
41  If the stride is 1, the Foam::IndexIterator would be a better choice.
42 
43 Class
44  Foam::RevStrideIterator
45 
46 Description
47  A random-access, integer-like, input iterator for integral values with
48  a stride that behaves like a reverse iterator.
49 
50 \*---------------------------------------------------------------------------*/
51 
52 #ifndef Foam_StrideIterator_H
53 #define Foam_StrideIterator_H
54 
55 #include <iterator>
56 #include <type_traits>
57 
58 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
59 
60 namespace Foam
61 {
62 
63 /*---------------------------------------------------------------------------*\
64  Class StrideIndexer Declaration
65 \*---------------------------------------------------------------------------*/
66 
67 template<class IntType>
68 class StrideIndexer
69 {
70  static_assert(std::is_integral_v<IntType>, "Integral required");
71 
72  // Private Data
73 
74  //- The current value
75  IntType value_;
76 
77  //- The indexing stride
78  IntType stride_;
79 
80 public:
81 
82  // STL definitions (as per std::iterator)
83  typedef IntType value_type;
84  typedef IntType difference_type;
85 
86 
87  // Constructors
88 
89  //- Default construct as zero, stride=1
90  constexpr StrideIndexer() noexcept
91  :
92  value_(0), stride_(1)
93  {}
94 
95  //- Construct with value zero and specified stride.
96  //- No checks (ie, possible to have a 0 stride!)
98  :
99  value_(0), stride_(stride)
100  {}
101 
102  //- Construct with specified value and stride.
103  //- No checks (ie, possible to have a 0 stride!)
105  :
106  value_(start), stride_(stride)
107  {}
108 
109 
110  // Member Functions
111 
112  //- The current value
113  constexpr value_type value() const noexcept { return value_; }
114 
115  //- The stride
116  constexpr value_type stride() const noexcept { return stride_; }
117 
118  //- Return the value offset by \p n strides
119  constexpr value_type value(value_type n) const noexcept
120  {
121  return value_ + (n * stride_);
122  }
124  //- Decrement the value by the stride
125  void prev() noexcept { value_ -= stride_; }
126 
127  //- Increment the value by the stride
128  void next() noexcept { value_ += stride_; }
129 
130  //- Decrease value by \p n strides
131  void prev(value_type n) noexcept { value_ -= (n * stride_); }
132 
133  //- Increase value by \p n strides
134  void next(value_type n) noexcept { value_ += (n * stride_); }
135 
137  // Member Operators
138 
139  //- Return the value
140  constexpr value_type operator*() const noexcept { return value_; }
142  //- Apply a postfix increment and return the current value.
143  // This operator definition is required for a generator -
144  // see std::generate()
146  {
147  auto curr(value_);
148  next();
149  return curr;
150  }
151 };
152 
153 
154 /*---------------------------------------------------------------------------*\
155  Class StrideIterator Declaration
156 \*---------------------------------------------------------------------------*/
157 
158 template<class IntType>
160 :
161  private StrideIndexer<IntType>
162 {
163 public:
164 
165  // STL definitions (as per std::iterator)
166  typedef std::random_access_iterator_tag iterator_category;
167  typedef IntType value_type;
168  typedef IntType difference_type;
169  typedef const IntType* pointer;
170  typedef IntType reference;
171 
172 
173  // Constructors
174 
175  //- Default construct as zero, stride 1
176  constexpr StrideIterator() noexcept
177  :
178  StrideIndexer<IntType>()
179  {}
180 
181  // Not yet useful:
182  //
183  // //- Construct with value zero and specified stride.
184  // //- No checks (ie, possible to have a 0 stride!)
185  // constexpr StrideIterator(value_type stride) noexcept
186  // :
187  // StrideIndexer<IntType>(stride)
188  // {}
190  //- Construct with specified value and stride.
191  //- No checks (ie, possible to have a 0 stride!)
193  :
195  {}
196 
197 
198  // Member Operators
199 
200  //- Return the value
201  constexpr value_type operator*() const noexcept
202  {
203  return this->value();
204  }
205 
206  //- Offset dereference operator
207  constexpr value_type operator[](value_type n) const noexcept
208  {
209  return this->value(n);
210  }
211 
212  //- Prefix increment
214  {
215  this->next();
216  return *this;
217  }
219  //- Postfix increment
221  {
222  auto old(*this);
223  this->next();
224  return old;
225  }
226 
227  //- Prefix decrement
229  {
230  this->prev();
231  return *this;
232  }
233 
234  //- Postfix decrement
236  {
237  auto old(*this);
238  this->prev();
239  return old;
240  }
241 
242  //- Arbitrary increment
244  {
245  this->next(n);
246  return *this;
247  }
248 
249  //- Arbitrary decrement
251  {
252  this->prev(n);
253  return *this;
254  }
255 
256  //- Return iterator with offset
257  constexpr StrideIterator
259  {
260  return { this->value(n), this->stride() };
261  }
262 
263  //- Return iterator with offset
264  constexpr StrideIterator
266  {
267  return { this->value(-n), this->stride() };
268  }
269 
270  //- Difference operator
271  constexpr value_type
272  operator-(const StrideIterator& iter) const noexcept
273  {
274  return
275  (
276  this->stride()
277  ? ((this->value() - iter.value()) / this->stride())
278  : 0
279  );
280  }
281 
282 
283  // Comparison
284 
285  //- Test for equality of values (ignores stride)
286  constexpr bool
287  operator==(const StrideIterator& iter) const noexcept
288  {
289  return (this->value() == iter.value());
290  }
291 
292  //- Compare less-than values (ignores stride)
293  constexpr bool
294  operator<(const StrideIterator& iter) const noexcept
295  {
296  return (this->value() < iter.value());
297  }
298 
299 
300  // Derived comparisons
301 
302  constexpr bool
303  operator!=(const StrideIterator& iter) const noexcept
304  {
305  return !(*this == iter);
306  }
307 
308  constexpr bool
309  operator<=(const StrideIterator& iter) const noexcept
310  {
311  return !(iter < *this);
312  }
313 
314  constexpr bool
315  operator>(const StrideIterator& iter) const noexcept
316  {
317  return (iter < *this);
318  }
319 
320  constexpr bool
321  operator>=(const StrideIterator& iter) const noexcept
322  {
323  return !(*this < iter);
324  }
325 };
326 
327 
328 /*---------------------------------------------------------------------------*\
329  Class RevStrideIterator Declaration
330 \*---------------------------------------------------------------------------*/
331 
332 template<class IntType>
333 class RevStrideIterator
334 :
335  private StrideIndexer<IntType>
336 {
337 public:
338 
339  // STL definitions (as per std::iterator)
340  typedef std::random_access_iterator_tag iterator_category;
341  typedef IntType value_type;
342  typedef IntType difference_type;
343  typedef const IntType* pointer;
344  typedef IntType reference;
345 
347  // Constructors
348 
349  //- Default construct as zero, stride 1
350  constexpr RevStrideIterator() noexcept
351  :
352  StrideIndexer<IntType>()
353  {}
354 
355  //- Construct with specified value and stride.
356  //- No checks (ie, possible to have a 0 stride!)
358  :
360  {}
362 
363  // Member Operators
364 
365  //- Return the value
366  constexpr value_type operator*() const noexcept
367  {
368  return this->value();
369  }
370 
371  //- Offset dereference operator
372  constexpr value_type operator[](value_type n) const noexcept
373  {
374  return this->value(-n);
375  }
376 
377  //- Prefix increment
379  {
380  this->prev();
381  return *this;
382  }
383 
384  //- Postfix increment
386  {
387  auto old(*this);
388  this->prev();
389  return old;
390  }
391 
392  //- Prefix decrement
394  {
395  this->next();
396  return *this;
397  }
398 
399  //- Postfix decrement
401  {
402  auto old(*this);
403  this->next();
404  return old;
405  }
406 
407  //- Arbitrary increment
409  {
410  this->prev(n);
411  return *this;
412  }
414  //- Arbitrary decrement
416  {
417  this->next(n);
418  return *this;
419  }
420 
421  //- Return iterator with offset
422  constexpr RevStrideIterator
424  {
425  return { this->value(-n), this->stride() };
426  }
427 
428  //- Return iterator with offset
429  constexpr RevStrideIterator
431  {
432  return { this->value(n), this->stride() };
433  }
434 
435  //- Difference operator
436  constexpr value_type
437  operator-(const RevStrideIterator& iter) const noexcept
438  {
439  return
440  (
441  this->stride()
442  ? ((iter.value() - this->value()) / this->stride())
443  : 0
444  );
445  }
446 
447 
448  // Comparison
450  //- Test for equality of values (ignore stride)
451  constexpr bool
452  operator==(const RevStrideIterator& iter) const noexcept
453  {
454  return (this->value() == iter.value());
455  }
456 
457  //- Compare less-than values (ignore stride)
458  constexpr bool
460  {
461  return (iter.value() < this->value());
462  }
463 
464 
465  // Derived comparisons
466 
467  constexpr bool
469  {
470  return !(*this == iter);
471  }
472 
473  constexpr bool
474  operator<=(const RevStrideIterator& iter) const noexcept
475  {
476  return !(iter < *this);
477  }
479  constexpr bool
480  operator>(const RevStrideIterator& iter) const noexcept
481  {
482  return (iter < *this);
483  }
484 
485  constexpr bool
486  operator>=(const RevStrideIterator& iter) const noexcept
487  {
488  return !(*this < iter);
489  }
490 };
491 
492 
493 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
494 
495 } // End namespace Foam
496 
497 #endif
498 
499 // ************************************************************************* //
constexpr value_type operator*() const noexcept
Return the value.
void prev() noexcept
Decrement the value by the stride.
StrideIterator & operator+=(value_type n) noexcept
Arbitrary increment.
StrideIterator & operator++() noexcept
Prefix increment.
constexpr RevStrideIterator operator-(value_type n) const noexcept
Return iterator with offset.
constexpr bool operator>=(const StrideIterator &iter) const noexcept
const IntType * pointer
const IntType * pointer
void next() noexcept
Increment the value by the stride.
constexpr bool operator<=(const RevStrideIterator &iter) const noexcept
constexpr bool operator>(const RevStrideIterator &iter) const noexcept
constexpr RevStrideIterator() noexcept
Default construct as zero, stride 1.
value_type operator()() noexcept
Apply a postfix increment and return the current value.
constexpr RevStrideIterator operator+(value_type n) const noexcept
Return iterator with offset.
constexpr value_type operator*() const noexcept
Return the value.
constexpr bool operator!=(const RevStrideIterator &iter) const noexcept
constexpr value_type stride() const noexcept
The stride.
StrideIterator & operator-=(value_type n) noexcept
Arbitrary decrement.
constexpr bool operator<=(const StrideIterator &iter) const noexcept
RevStrideIterator & operator-=(value_type n) noexcept
Arbitrary decrement.
constexpr value_type value() const noexcept
The current value.
constexpr value_type operator[](value_type n) const noexcept
Offset dereference operator.
constexpr bool operator>=(const RevStrideIterator &iter) const noexcept
A random-access, integer-like, input iterator for integral values with a stride that behaves like a r...
constexpr bool operator==(const RevStrideIterator &iter) const noexcept
Test for equality of values (ignore stride)
constexpr StrideIndexer() noexcept
Default construct as zero, stride=1.
constexpr bool operator!=(const StrideIterator &iter) const noexcept
constexpr bool operator==(const StrideIterator &iter) const noexcept
Test for equality of values (ignores stride)
constexpr bool operator<(const RevStrideIterator &iter) const noexcept
Compare less-than values (ignore stride)
A random-access, integer-like, input iterator for integral values with a stride. If the stride is 1...
constexpr StrideIterator operator-(value_type n) const noexcept
Return iterator with offset.
RevStrideIterator & operator--() noexcept
Prefix decrement.
std::random_access_iterator_tag iterator_category
RevStrideIterator & operator+=(value_type n) noexcept
Arbitrary increment.
const direction noexcept
Definition: scalarImpl.H:265
Indexer or generator for strided integral values. The extended base functionality (eg...
std::random_access_iterator_tag iterator_category
constexpr bool operator>(const StrideIterator &iter) const noexcept
RevStrideIterator & operator++() noexcept
Prefix increment.
label n
constexpr value_type operator[](value_type n) const noexcept
Offset dereference operator.
constexpr value_type operator*() const noexcept
Return the value.
constexpr StrideIterator() noexcept
Default construct as zero, stride 1.
constexpr bool operator<(const StrideIterator &iter) const noexcept
Compare less-than values (ignores stride)
constexpr StrideIterator operator+(value_type n) const noexcept
Return iterator with offset.
Namespace for OpenFOAM.
StrideIterator & operator--() noexcept
Prefix decrement.