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