sliceRange.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) 2019-2023 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 Class
27  Foam::sliceRange
28 
29 Description
30  A set of labels defined by a start, a length and a stride.
31 
32 SourceFiles
33  sliceRange.C
34  sliceRangeI.H
35 
36 \*---------------------------------------------------------------------------*/
37 
38 #ifndef Foam_sliceRange_H
39 #define Foam_sliceRange_H
40 
41 #include "label.H"
42 #include <iterator>
43 
44 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45 
46 namespace Foam
47 {
48 
49 // Forward Declarations
50 template<class T> class List;
51 template<class T, unsigned N> class FixedList;
52 
53 /*---------------------------------------------------------------------------*\
54  Class sliceRange Declaration
55 \*---------------------------------------------------------------------------*/
56 
57 class sliceRange
58 {
59  // Private Data
60 
61  //- The start of the interval
62  label start_;
63 
64  //- The length of the interval
65  label size_;
66 
67  //- The stride for the interval
68  label stride_;
69 
70 
71 public:
72 
73  // STL type definitions
74 
75  //- Type of values the range contains
76  typedef label value_type;
77 
78  //- The type that can represent the size of the range
79  typedef label size_type;
80 
81  //- Input iterator with const access
82  class const_iterator;
83 
84  //- Reverse input iterator with const access
86 
87 
88  // Generated Methods: copy/move construct, copy/move assignment
89 
90 
91  // Constructors
92 
93  //- Default construct an empty slice (0,0,0)
94  inline constexpr sliceRange() noexcept;
95 
96  //- Construct slice from start/size/stride, no checks
97  inline constexpr sliceRange
98  (
99  const label beg,
100  const label len,
101  const label stride
102  ) noexcept;
103 
104  //- Construct slice from start/size/stride coefficients,
105  //- enforce non-negative size and stride.
106  explicit sliceRange(const FixedList<label,3>& coeffs);
107 
108 
109  // Member Functions
110 
111  // Access
112 
113  //- The start value in the range
114  constexpr label start() const noexcept { return start_; }
115 
116  //- The size of the range
117  constexpr label size() const noexcept { return size_; }
118 
119  //- The stride for the range
120  constexpr label stride() const noexcept { return stride_; }
121 
122  //- True if range is empty (zero-sized)
123  bool empty() const noexcept { return !size_; }
124 
125  //- The (inclusive) lower value of the range.
126  constexpr label min() const noexcept { return start_; }
127 
128  //- The (inclusive) upper value of the range.
129  //- Ill-defined for an empty range
130  label max() const noexcept { return start_ + (size_-1) * stride_; }
132  //- Return list of labels corresponding to the slice
133  List<label> labels() const;
134 
135 
136  // Member Operators
137 
138  //- Return element in the range, without bounds checking
139  label operator[](const label i) const noexcept
140  {
141  return start_ + i * stride_;
142  }
143 
144  //- True if range is non-empty
145  explicit operator bool() const noexcept { return bool(size_); }
147 
148  // Iteration / Generation
149 
150  //- A value indexer, for iteration or generation
151  class indexer;
152 
153  //- Return a forward values generator
154  inline indexer generator() const;
155 
156  //- Return const_iterator to a position within the range,
157  //- with bounds checking.
158  // \return iterator at the requested position, or end() for
159  // out-of-bounds
160  inline const_iterator at(const label i) const;
161 
162  //- A const_iterator set to the beginning of the range
163  inline const_iterator begin() const noexcept;
164 
165  //- A const_iterator set to the beginning of the range
166  inline const_iterator cbegin() const noexcept;
167 
168  //- A const_iterator set to 1 beyond the end of the range.
169  inline const_iterator cend() const noexcept;
171  //- A const_iterator set to 1 beyond the end of the range.
172  inline const_iterator end() const noexcept;
173 
174  //- A const_reverse_iterator set to 1 before the end of range
175  inline const_reverse_iterator rbegin() const noexcept;
176 
177  //- A const_reverse_iterator set to 1 before the end of range
179 
180  //- A const_reverse_iterator set to 1 before the begin of range
181  inline const_reverse_iterator rend() const noexcept;
182 
183  //- A const_reverse_iterator set to 1 before the begin of range
184  inline const_reverse_iterator crend() const noexcept;
185 
186 
187  // Iterators
188 
189  //- A value indexer, for iteration or generation
190  class indexer
191  {
192  //- The stride when indexing
193  const label stride_;
194 
195  //- The global value
196  label value_;
197 
198  public:
199 
200  // STL definitions (as per std::iterator)
201  typedef label value_type;
202  typedef label difference_type;
203  typedef const label* pointer;
204  typedef label reference;
205 
206 
207  // Constructors
208 
209  //- Default construct with zero value and stride = 1
210  inline constexpr indexer() noexcept;
211 
212  //- Construct with specified value and stride
213  inline constexpr indexer
214  (
215  const label val,
216  const label stride
217  ) noexcept;
218 
219 
220  // Member Functions
221 
222  //- The current value
223  constexpr label value() const noexcept { return value_; }
224 
225  //- The stride
226  constexpr label stride() const noexcept { return stride_; }
227 
228  //- Value with offset
229  constexpr label value(const label n) const noexcept
230  {
231  return value_ + (n * stride_);
232  }
233 
234  //- Decrement value
235  void prev() noexcept { value_ -= stride_; }
236 
237  //- Decrease value
238  void prev(const label n) noexcept { value_ -= (n * stride_); }
239 
240  //- Increment value
241  void next() noexcept { value_ += stride_; }
242 
243  //- Increase value
244  void next(const label n) noexcept { value_ += (n * stride_); }
245 
246 
247  // Member Operators
249  //- Return the value
250  constexpr label operator*() const noexcept { return value_; }
251 
252  //- Apply a postfix increment and return the current value.
253  // This operator definition is required for a generator -
254  // see std::generate()
255  inline label operator()() noexcept;
256  };
257 
258 
259  //- Bidirectional input iterator with const access
260  class const_iterator
261  :
262  public indexer
263  {
264  public:
266  // STL definitions (as per std::iterator)
267  typedef std::random_access_iterator_tag iterator_category;
268 
269 
270  // Constructors
271 
272  //- Inherit constructors from indexer
273  using indexer::indexer;
274 
275 
276  // Member Operators
277 
278  //- Offset dereference operator
279  inline constexpr label operator[](const label n) const noexcept;
280 
281  //- Prefix increment
282  inline const_iterator& operator++() noexcept;
283 
284  //- Postfix increment
285  inline const_iterator operator++(int) noexcept;
286 
287  //- Prefix decrement
288  inline const_iterator& operator--() noexcept;
289 
290  //- Postfix decrement
291  inline const_iterator operator--(int) noexcept;
292 
293  //- Arbitrary increment
294  inline const_iterator& operator+=(const label n) noexcept;
295 
296  //- Arbitrary decrement
297  inline const_iterator& operator-=(const label n) noexcept;
298 
299  //- Return iterator with offset
300  inline constexpr const_iterator operator+
301  (
302  const label n
303  ) const noexcept;
304 
305  //- Return iterator with offset
306  inline constexpr const_iterator operator-
307  (
308  const label n
309  ) const noexcept;
310 
311  //- Difference operator
312  inline constexpr label operator-
313  (
314  const const_iterator& iter
315  ) const noexcept;
316 
317 
318  // Comparison
320  //- Test for equality of values (ignore stride)
321  inline constexpr bool operator==(const const_iterator& iter)
322  const noexcept;
323 
324  //- Compare less-than values (ignore stride)
325  inline constexpr bool operator<(const const_iterator& iter)
326  const noexcept;
327 
328 
329  // Derived comparisons
330 
331  constexpr bool operator!=(const const_iterator& iter)
332  const noexcept
333  {
334  return !(*this == iter);
335  }
336 
337  constexpr bool operator<=(const const_iterator& iter)
338  const noexcept
339  {
340  return !(iter < *this);
341  }
342 
343  constexpr bool operator>(const const_iterator& iter)
344  const noexcept
345  {
346  return (iter < *this);
347  }
348 
349  constexpr bool operator>=(const const_iterator& iter)
350  const noexcept
351  {
352  return !(*this < iter);
353  }
354  };
355 
356 
357  //- Bidirectional reverse input iterator with const access
359  :
360  public indexer
361  {
362  public:
363 
364  // STL definitions (as per std::iterator)
365  typedef std::random_access_iterator_tag iterator_category;
366 
367 
368  // Constructors
369 
370  //- Inherit constructors from indexer
371  using indexer::indexer;
372 
373 
374  // Member Operators
375 
376  //- Offset dereference operator
377  inline constexpr label operator[](const label n) const noexcept;
378 
379  //- Prefix increment
381 
382  //- Postfix increment
384 
385  //- Prefix decrement
387 
388  //- Postfix decrement
390 
391  //- Arbitrary increment
392  inline const_reverse_iterator& operator+=(const label n) noexcept;
393 
394  //- Arbitrary decrement
395  inline const_reverse_iterator& operator-=(const label n) noexcept;
396 
397  //- Return iterator with offset
398  inline constexpr const_reverse_iterator operator+
399  (
400  const label n
401  ) const noexcept;
402 
403  //- Return iterator with offset
404  inline constexpr const_reverse_iterator operator-
405  (
406  const label n
407  ) const noexcept;
408 
409  //- Difference operator
410  inline constexpr label operator-
411  (
412  const const_reverse_iterator& iter
413  ) const noexcept;
414 
415 
416  // Comparison
417 
418  //- Test for equality of values (ignore stride)
419  inline constexpr bool operator==(const const_reverse_iterator& iter)
420  const noexcept;
421 
422  //- Reverse compare less-than values (ignore stride)
423  inline constexpr bool operator<(const const_reverse_iterator& iter)
424  const noexcept;
425 
426 
427  // Derived comparisons
428 
429  constexpr bool operator!=(const const_reverse_iterator& iter)
430  const noexcept
431  {
432  return !(*this == iter);
433  }
434 
435  constexpr bool operator<=(const const_reverse_iterator& iter)
436  const noexcept
437  {
438  return !(iter < *this);
439  }
440 
441  constexpr bool operator>(const const_reverse_iterator& iter)
442  const noexcept
443  {
444  return (iter < *this);
445  }
446 
447  constexpr bool operator>=(const const_reverse_iterator& iter)
448  const noexcept
449  {
450  return !(*this < iter);
451  }
452  };
453 };
454 
455 
456 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
457 
458 //- Read sliceRange from Istream as (start size stride) tuple, no checks
460 
461 //- Write sliceRange to Ostream as (start size stride) tuple
463 
464 
465 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
466 
467 //- Test for equality of begin/size/slice values
468 inline constexpr bool operator==
469 (
470  const sliceRange& a,
471  const sliceRange& b
472 ) noexcept
473 {
474  return
475  (
476  a.start() == b.start()
477  && a.size() == b.size()
478  && a.stride() == b.stride()
479  );
480 }
481 
482 
483 //- Comparison function for sorting, compares the start.
484 // If the start values are equal, compare the sizes.
485 // If the sizes are also equal, compare the strides.
486 inline constexpr bool operator<
487 (
488  const sliceRange& a,
489  const sliceRange& b
490 ) noexcept
491 {
492  return
493  (
494  a.start() < b.start()
495  ||
496  (
497  !(b.start() < a.start())
498  &&
499  (
500  a.size() < b.size()
501  ||
502  (
503  !(b.size() < a.size())
504  && a.stride() < b.stride()
505  )
506  )
507  )
508  );
509 }
510 
511 
512 // Derived comparisons
513 
514 inline constexpr bool operator!=
515 (
516  const sliceRange& a,
517  const sliceRange& b
518 ) noexcept
519 {
520  return !(a == b);
521 }
522 
523 
524 inline constexpr bool operator<=
525 (
526  const sliceRange& a,
527  const sliceRange& b
528 ) noexcept
529 {
530  return !(b < a);
531 }
532 
533 inline constexpr bool operator>
534 (
535  const sliceRange& a,
536  const sliceRange& b
537 ) noexcept
538 {
539  return (b < a);
540 }
541 
542 inline constexpr bool operator>=
543 (
544  const sliceRange& a,
545  const sliceRange& b
546 ) noexcept
547 {
548  return !(a < b);
549 }
550 
551 
552 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
553 
554 } // End namespace Foam
555 
556 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
557 
558 #include "sliceRangeI.H"
559 
560 #endif
561 
562 // ************************************************************************* //
const_iterator end() const noexcept
A const_iterator set to 1 beyond the end of the range.
Definition: sliceRangeI.H:322
const_iterator at(const label i) const
Return const_iterator to a position within the range, with bounds checking.
Definition: sliceRangeI.H:296
constexpr indexer() noexcept
Default construct with zero value and stride = 1.
Definition: sliceRangeI.H:46
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: HashTable.H:101
std::random_access_iterator_tag iterator_category
Definition: sliceRange.H:480
const_iterator cbegin() const noexcept
A const_iterator set to the beginning of the range.
Definition: sliceRangeI.H:315
constexpr bool operator==(const const_reverse_iterator &iter) const noexcept
Test for equality of values (ignore stride)
Definition: sliceRangeI.H:272
constexpr bool operator>=(const const_reverse_iterator &iter) const noexcept
Definition: sliceRange.H:588
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
A value indexer, for iteration or generation.
Definition: sliceRange.H:248
bool empty() const noexcept
True if range is empty (zero-sized)
Definition: sliceRange.H:146
bool operator>(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A newer than B.
const_reverse_iterator crend() const noexcept
A const_reverse_iterator set to 1 before the begin of range.
Definition: sliceRangeI.H:357
List< label > labels() const
Return list of labels corresponding to the slice.
Definition: sliceRange.C:38
label max() const noexcept
The (inclusive) upper value of the range. Ill-defined for an empty range.
Definition: sliceRange.H:157
label size_type
The type that can represent the size of the range.
Definition: sliceRange.H:84
Bidirectional input iterator with const access.
Definition: sliceRange.H:347
constexpr label operator[](const label n) const noexcept
Offset dereference operator.
Definition: sliceRangeI.H:184
bool operator>=(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A same or newer than B.
scalar range
label value_type
Type of values the range contains.
Definition: sliceRange.H:79
bool operator<=(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A same or older than B.
Bidirectional reverse input iterator with const access.
Definition: sliceRange.H:473
constexpr label size() const noexcept
The size of the range.
Definition: sliceRange.H:136
tmp< faMatrix< Type > > operator*(const areaScalarField::Internal &, const faMatrix< Type > &)
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
constexpr label min() const noexcept
The (inclusive) lower value of the range.
Definition: sliceRange.H:151
Istream & operator>>(Istream &, directionInfo &)
constexpr bool operator<(const const_reverse_iterator &iter) const noexcept
Reverse compare less-than values (ignore stride)
Definition: sliceRangeI.H:280
const_iterator begin() const noexcept
A const_iterator set to the beginning of the range.
Definition: sliceRangeI.H:308
constexpr bool operator!=(const const_reverse_iterator &iter) const noexcept
Definition: sliceRange.H:570
label operator[](const label i) const noexcept
Return element in the range, without bounds checking.
Definition: sliceRange.H:170
constexpr sliceRange() noexcept
Default construct an empty slice (0,0,0)
Definition: sliceRangeI.H:23
A set of labels defined by a start, a length and a stride.
Definition: sliceRange.H:52
const_reverse_iterator & operator++() noexcept
Prefix increment.
Definition: sliceRangeI.H:192
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:55
const direction noexcept
Definition: Scalar.H:258
OBJstream os(runTime.globalPath()/outputName)
const_reverse_iterator & operator+=(const label n) noexcept
Arbitrary increment.
Definition: sliceRangeI.H:230
const_reverse_iterator & operator-=(const label n) noexcept
Arbitrary decrement.
Definition: sliceRangeI.H:239
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:76
const_reverse_iterator rbegin() const noexcept
A const_reverse_iterator set to 1 before the end of range.
Definition: sliceRangeI.H:336
const_reverse_iterator & operator--() noexcept
Prefix decrement.
Definition: sliceRangeI.H:211
constexpr label start() const noexcept
The start value in the range.
Definition: sliceRange.H:131
label n
const_reverse_iterator rend() const noexcept
A const_reverse_iterator set to 1 before the begin of range.
Definition: sliceRangeI.H:350
constexpr bool operator>(const const_reverse_iterator &iter) const noexcept
Definition: sliceRange.H:582
const_reverse_iterator crbegin() const noexcept
A const_reverse_iterator set to 1 before the end of range.
Definition: sliceRangeI.H:343
constexpr bool operator<=(const const_reverse_iterator &iter) const noexcept
Definition: sliceRange.H:576
Namespace for OpenFOAM.
const_iterator cend() const noexcept
A const_iterator set to 1 beyond the end of the range.
Definition: sliceRangeI.H:329
indexer generator() const
Return a forward values generator.
Definition: sliceRangeI.H:289
constexpr label stride() const noexcept
The stride for the range.
Definition: sliceRange.H:141