IntRange.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) 2020-2021 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::IntRange
28 
29 Description
30  An interval of (signed) integers defined by a start and a size.
31 
32 Note
33  Only a minimum of IO operators are defined, to avoid incurring
34  too many dependencies or cyclic dependencies.
35 
36 SourceFiles
37  IntRangeI.H
38  IntRanges.C
39 
40 \*---------------------------------------------------------------------------*/
41 
42 #ifndef IntRange_H
43 #define IntRange_H
44 
45 #include "labelFwd.H"
46 #include <iterator>
47 #include <type_traits>
48 
49 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
50 
51 namespace Foam
52 {
53 
54 // Forward Declarations
55 class Istream;
56 class Ostream;
57 template<class T> class List;
58 
59 /*---------------------------------------------------------------------------*\
60  Class IntRange Declaration
61 \*---------------------------------------------------------------------------*/
62 
63 template<class IntType>
64 class IntRange
65 {
66  static_assert(std::is_integral<IntType>::value, "Integral required");
67 
68  // Private Data
69 
70  //- The start of the interval
71  IntType start_;
72 
73  //- The length of the interval
74  IntType size_;
75 
76 
77 protected:
78 
79  // Protected Member Functions
80 
81  //- The value 1 before the start of the range.
82  // Corresponds to the value of the rend() iterator
83  inline IntType rend_value() const noexcept;
84 
85  //- The value 1 beyond the end of the range.
86  // Corresponds to the value of the end() iterator
87  inline IntType end_value() const noexcept;
88 
89 
90 public:
91 
92  // STL type definitions
93 
94  //- Type of values the range contains
95  typedef IntType value_type;
96 
97  //- The type that can represent the size of the range
98  typedef IntType size_type;
99 
100  //- Input iterator with const access
101  class const_iterator;
102 
103  //- Reverse input iterator with const access
104  class const_reverse_iterator;
105 
106 
107  // Generated Methods: copy/move construct, copy/move assignment
108 
109 
110  // Constructors
111 
112  //- Default construct an empty range (0,0)
113  inline constexpr IntRange() noexcept;
114 
115  //- Construct a range with specified length, starting at zero (0,len)
116  inline explicit constexpr IntRange(const IntType len) noexcept;
117 
118  //- Construct a range from start/length, no checks
119  inline constexpr IntRange
120  (
121  const IntType beg,
122  const IntType len
123  ) noexcept;
124 
125 
126  // Member Functions
127 
128  // Access
129 
130  //- The (inclusive) lower value of the range
131  inline IntType start() const noexcept;
132 
133  //- The size of the range
134  inline IntType size() const noexcept;
135 
136  //- Non-const access to start of the range
137  inline IntType& start() noexcept;
138 
139  //- Non-const access to size of the range
140  inline IntType& size() noexcept;
141 
142  //- True if range is empty (zero-sized)
143  inline bool empty() const noexcept;
144 
145  //- The (inclusive) lower value of the range. Same as start()
146  inline IntType first() const noexcept;
147 
148  //- The (inclusive) upper value of the range
149  inline IntType last() const noexcept;
150 
151 
152  // Edit
153 
154  //- Reset to zero start and zero size
155  inline void clear() noexcept;
156 
157  //- Reset start and length, no checks
158  inline void reset(const IntType beg, const IntType len) noexcept;
159 
160  //- Set the start position, no checks
161  inline void setStart(const IntType i) noexcept;
162 
163  //- Change the size, no checks. Identical to resize()
164  inline void setSize(const IntType n) noexcept;
165 
166  //- Change the size, no checks. Identical to setSize()
167  inline void resize(const IntType n) noexcept;
168 
169  //- Enforce non-negative size
170  inline void clampSize() noexcept;
171 
172 
173  // Search
174 
175  //- True if the (global) value is located within the range
176  inline bool found(const IntType value) const noexcept;
177 
178 
179  // Member Operators
180 
181  //- Offset dereference, without bounds checking
182  inline constexpr IntType operator[](const IntType i) const noexcept;
183 
184  //- True if the global value is located within the range.
185  // Behaviour identical to found() - usable as a predicate
186  inline bool operator()(const IntType value) const noexcept;
187 
188  //- Increase the size by 1.
189  inline IntType operator++() noexcept;
190  inline IntType operator++(int) noexcept;
191 
192  //- Increase the size by n.
193  inline IntType operator+=(const IntType n) noexcept;
194 
195  //- Decrease the size by 1, but never below 0.
196  inline IntType operator--() noexcept;
197  inline IntType operator--(int) noexcept;
198 
199  //- Decrease the size by n, but never below 0.
200  inline IntType operator-=(const IntType n) noexcept;
201 
202  //- True if range is non-empty
203  explicit operator bool() const noexcept { return bool(size_); }
204 
205 
206  // Bidirectional input iterators (const)
207 
208  //- Return const_iterator to a position within the range,
209  //- with bounds checking.
210  // \return iterator at the requested position, or end() for
211  // out-of-bounds
212  inline const_iterator at(const IntType i) const;
213 
214  //- A const_iterator set to the beginning of the range
215  inline const_iterator begin() const noexcept;
216 
217  //- A const_iterator set to the beginning of the range
218  inline const_iterator cbegin() const noexcept;
219 
220  //- A const_iterator set to 1 beyond the end of the range.
221  inline const_iterator cend() const noexcept;
222 
223  //- A const_iterator set to 1 beyond the end of the range.
224  inline const_iterator end() const noexcept;
225 
226 
227  // Bidirectional reverse input iterators (const)
228 
229  //- A const_reverse_iterator set to 1 before the end of range
230  inline const_reverse_iterator rbegin() const noexcept;
231 
232  //- A const_reverse_iterator set to 1 before the end of range
233  inline const_reverse_iterator crbegin() const noexcept;
234 
235  //- A const_reverse_iterator set to 1 before the begin of range
236  inline const_reverse_iterator rend() const noexcept;
237 
238  //- A const_reverse_iterator set to 1 before the begin of range
239  inline const_reverse_iterator crend() const noexcept;
240 
241 
242  // Iterators
243 
244  //- Random-access input iterator with const access
245  // \note No operator+(IntType, iterator) since this provokes
246  // misleading resolution errors
247  class const_iterator
248  {
249  //- The global value
250  IntType value_;
251 
252  public:
253 
254  // STL definitions (as per std::iterator)
255  typedef std::random_access_iterator_tag iterator_category;
256  typedef IntType value_type;
257  typedef IntType difference_type;
258  typedef const IntType* pointer;
259  typedef IntType reference;
260 
261 
262  // Constructors
263 
264  //- Construct with specified value, or default construct
265  inline explicit constexpr const_iterator
266  (
267  const IntType val = 0
268  ) noexcept;
269 
270 
271  // Member Operators
272 
273  //- Return the value
274  inline constexpr IntType operator*() const noexcept;
275 
276  //- Offset dereference operator
277  inline constexpr IntType operator[](const IntType n) const noexcept;
278 
279  //- Prefix increment
280  inline const_iterator& operator++() noexcept;
281 
282  //- Postfix increment
283  inline const_iterator operator++(int) noexcept;
284 
285  //- Prefix decrement
286  inline const_iterator& operator--() noexcept;
287 
288  //- Postfix decrement
289  inline const_iterator operator--(int) noexcept;
290 
291  //- Arbitrary increment
292  inline const_iterator& operator+=(const IntType n) noexcept;
293 
294  //- Arbitrary decrement
295  inline const_iterator& operator-=(const IntType n) noexcept;
296 
297  //- Return iterator with offset
298  inline constexpr const_iterator operator+
299  (
300  const IntType n
301  ) const noexcept;
302 
303  //- Return iterator with offset
304  inline constexpr const_iterator operator-
305  (
306  const IntType n
307  ) const noexcept;
308 
309  //- Difference operator
310  inline constexpr IntType operator-
311  (
312  const const_iterator& iter
313  ) const noexcept;
314 
315 
316  // Comparison
317 
318  //- Test for equality of values
319  inline constexpr bool operator==(const const_iterator& iter)
320  const noexcept;
321 
322  //- Compare less-than
323  inline constexpr bool operator<(const const_iterator& iter)
324  const noexcept;
325 
326 
327  // Derived comparisons
328 
329  constexpr bool operator!=(const const_iterator& iter)
330  const noexcept
331  {
332  return !(*this == iter);
333  }
334 
335  constexpr bool operator<=(const const_iterator& iter)
336  const noexcept
337  {
338  return !(iter < *this);
339  }
340 
341  constexpr bool operator>(const const_iterator& iter)
342  const noexcept
343  {
344  return (iter < *this);
345  }
346 
347  constexpr bool operator>=(const const_iterator& iter)
348  const noexcept
349  {
350  return !(*this < iter);
351  }
352  };
353 
354 
355  //- Random-access reverse input iterator with const access
356  // \note No operator+(IntType, iterator) since this provokes
357  // misleading resolution errors
359  {
360  //- The global value
361  IntType value_;
362 
363  public:
364 
365  // STL definitions (as per std::iterator)
366  typedef std::random_access_iterator_tag iterator_category;
367  typedef IntType value_type;
368  typedef IntType difference_type;
369  typedef const IntType* pointer;
370  typedef IntType reference;
371 
372 
373  // Constructors
374 
375  //- Construct with specified value, or default construct
376  inline explicit constexpr const_reverse_iterator
377  (
378  const IntType val = 0
379  ) noexcept;
380 
381 
382  // Member Operators
383 
384  //- Return the value
385  inline constexpr IntType operator*() const noexcept;
386 
387  //- Offset dereference operator
388  inline constexpr IntType operator[](const IntType n) const noexcept;
389 
390  //- Prefix increment
391  inline const_reverse_iterator& operator++() noexcept;
392 
393  //- Postfix increment
394  inline const_reverse_iterator operator++(int) noexcept;
395 
396  //- Prefix decrement
397  inline const_reverse_iterator& operator--() noexcept;
398 
399  //- Postfix decrement
400  inline const_reverse_iterator operator--(int) noexcept;
401 
402  //- Arbitrary increment
403  inline const_reverse_iterator& operator+=(const IntType n) noexcept;
404 
405  //- Arbitrary decrement
406  inline const_reverse_iterator& operator-=(const IntType n) noexcept;
407 
408  //- Return iterator with offset
409  inline constexpr const_reverse_iterator operator+
410  (
411  const IntType n
412  ) const noexcept;
413 
414  //- Return iterator with offset
415  inline constexpr const_reverse_iterator operator-
416  (
417  const IntType n
418  ) const noexcept;
419 
420  //- Difference operator
421  inline constexpr IntType operator-
422  (
423  const const_reverse_iterator& iter
424  )const noexcept;
425 
426 
427  // Comparison
428 
429  //- Test for equality of values
430  inline constexpr bool operator==(const const_reverse_iterator& iter)
431  const noexcept;
432 
433  //- Reverse compare less-than
434  inline constexpr bool operator<(const const_reverse_iterator& iter)
435  const noexcept;
436 
437 
438  // Derived comparisons
439 
440  constexpr bool operator!=(const const_reverse_iterator& iter)
441  const noexcept
442  {
443  return !(*this == iter);
444  }
445 
446  constexpr bool operator<=(const const_reverse_iterator& iter)
447  const noexcept
448  {
449  return !(iter < *this);
450  }
451 
452  constexpr bool operator>(const const_reverse_iterator& iter)
453  const noexcept
454  {
455  return (iter < *this);
456  }
457 
458  constexpr bool operator>=(const const_reverse_iterator& iter)
459  const noexcept
460  {
461  return !(*this < iter);
462  }
463  };
464 };
465 
466 
467 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
468 
469 // Identity function for common integer types
470 
471 //- Identity map from an int32_t IntRange
472 List<label> identity(const IntRange<int32_t>& range);
473 
474 #if defined(WM_LABEL_SIZE) && (WM_LABEL_SIZE >= 64)
475 //- Identity map from an int64_t IntRange
476 List<label> identity(const IntRange<int64_t>& range);
477 #endif
478 
479 
480 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
481 
482 // Input operators for common integer types
483 
484 //- Read IntRange from Istream as bracketed (start size) tuple, no checks
485 Istream& operator>>(Istream& os, IntRange<int32_t>& range);
486 
487 //- Read IntRange from Istream as bracketed (start size) tuple, no checks
488 Istream& operator>>(Istream& os, IntRange<int64_t>& range);
489 
490 
491 // Output operators for common integer types
492 
493 //- Write IntRange to Ostream as bracketed (start size) tuple
494 Ostream& operator<<(Ostream& os, const IntRange<int32_t>& range);
495 
496 //- Write IntRange to Ostream as bracketed (start size) tuple
497 Ostream& operator<<(Ostream& os, const IntRange<int64_t>& range);
498 
499 
500 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
501 
502 //- Test for equality of begin/size values
503 template<class IntType>
504 inline constexpr bool operator==
505 (
506  const IntRange<IntType>& a,
507  const IntRange<IntType>& b
508 ) noexcept
509 {
510  return (a.first() == b.first() && a.size() == b.size());
511 }
512 
513 
514 //- Comparison function for sorting, compares the start.
515 // If the start values are equal, also compares the size.
516 template<class IntType>
517 inline constexpr bool operator<
518 (
519  const IntRange<IntType>& a,
520  const IntRange<IntType>& b
521 ) noexcept
522 {
523  return
524  (
525  a.first() < b.first()
526  ||
527  (
528  !(b.first() < a.first())
529  && a.size() < b.size()
530  )
531  );
532 }
533 
534 
535 // Derived comparisons
536 
537 template<class IntType>
538 inline constexpr bool operator!=
539 (
540  const IntRange<IntType>& a,
541  const IntRange<IntType>& b
542 ) noexcept
543 {
544  return !(a == b);
545 }
546 
547 template<class IntType>
548 inline constexpr bool operator<=
549 (
550  const IntRange<IntType>& a,
551  const IntRange<IntType>& b
552 ) noexcept
553 {
554  return !(b < a);
555 }
556 
557 template<class IntType>
558 inline constexpr bool operator>
559 (
560  const IntRange<IntType>& a,
561  const IntRange<IntType>& b
562 ) noexcept
563 {
564  return (b < a);
565 }
566 
567 template<class IntType>
568 inline constexpr bool operator>=
569 (
570  const IntRange<IntType>& a,
571  const IntRange<IntType>& b
572 ) noexcept
573 
574 {
575  return !(a < b);
576 }
577 
578 
579 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
580 
581 } // End namespace Foam
582 
583 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
584 
585 #include "IntRangeI.H"
586 
587 #endif
588 
589 // ************************************************************************* //
Foam::IntRange::crbegin
const_reverse_iterator crbegin() const noexcept
A const_reverse_iterator set to 1 before the end of range.
Definition: IntRangeI.H:383
Foam::IntRange::const_reverse_iterator::operator>
constexpr bool operator>(const const_reverse_iterator &iter) const noexcept
Definition: IntRange.H:451
Foam::IntRange::const_iterator::operator>=
constexpr bool operator>=(const const_iterator &iter) const noexcept
Definition: IntRange.H:346
Foam::IntRange::rend_value
IntType rend_value() const noexcept
The value 1 before the start of the range.
Definition: IntRangeI.H:457
Foam::operator<=
bool operator<=(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A same or older than B.
Definition: IOstreamOption.H:408
Foam::IntRange::at
const_iterator at(const IntType i) const
Definition: IntRangeI.H:335
Foam::IntRange
An interval of (signed) integers defined by a start and a size.
Definition: IntRange.H:63
Foam::IntRange::end
const_iterator end() const noexcept
A const_iterator set to 1 beyond the end of the range.
Definition: IntRangeI.H:359
Foam::IntRange::const_reverse_iterator::const_reverse_iterator
constexpr const_reverse_iterator(const IntType val=0) noexcept
Construct with specified value, or default construct.
Definition: IntRangeI.H:200
Foam::IntRange::rbegin
const_reverse_iterator rbegin() const noexcept
A const_reverse_iterator set to 1 before the end of range.
Definition: IntRangeI.H:375
Foam::IntRange::const_iterator::iterator_category
std::random_access_iterator_tag iterator_category
Definition: IntRange.H:254
Foam::IntRange::setStart
void setStart(const IntType i) noexcept
Set the start position, no checks.
Definition: IntRangeI.H:490
Foam::IntRange::clear
void clear() noexcept
Reset to zero start and zero size.
Definition: IntRangeI.H:471
Foam::IntRange::found
bool found(const IntType value) const noexcept
True if the (global) value is located within the range.
Definition: IntRangeI.H:518
Foam::IntRange::const_iterator::operator<=
constexpr bool operator<=(const const_iterator &iter) const noexcept
Definition: IntRange.H:334
Foam::IntRange::rend
const_reverse_iterator rend() const noexcept
A const_reverse_iterator set to 1 before the begin of range.
Definition: IntRangeI.H:391
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
Foam::IntRange::const_reverse_iterator
Random-access reverse input iterator with const access.
Definition: IntRange.H:357
Foam::IntRange::const_reverse_iterator::reference
IntType reference
Definition: IntRange.H:369
Foam::IntRange::cend
const_iterator cend() const noexcept
A const_iterator set to 1 beyond the end of the range.
Definition: IntRangeI.H:367
Foam::IntRange::const_reverse_iterator::operator<=
constexpr bool operator<=(const const_reverse_iterator &iter) const noexcept
Definition: IntRange.H:445
Foam::IntRange::resize
void resize(const IntType n) noexcept
Change the size, no checks. Identical to setSize()
Definition: IntRangeI.H:504
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::IntRange::size
IntType size() const noexcept
The size of the range.
Definition: IntRangeI.H:415
Foam::constant::physicoChemical::b
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
Foam::IntRange::end_value
IntType end_value() const noexcept
The value 1 beyond the end of the range.
Definition: IntRangeI.H:464
Foam::IntRange::const_iterator::operator>
constexpr bool operator>(const const_iterator &iter) const noexcept
Definition: IntRange.H:340
Foam::IntRange::crend
const_reverse_iterator crend() const noexcept
A const_reverse_iterator set to 1 before the begin of range.
Definition: IntRangeI.H:399
Foam::IntRange::empty
bool empty() const noexcept
True if range is empty (zero-sized)
Definition: IntRangeI.H:436
Foam::IntRange::const_reverse_iterator::pointer
const typedef IntType * pointer
Definition: IntRange.H:368
Foam::IntRange::const_reverse_iterator::operator>=
constexpr bool operator>=(const const_reverse_iterator &iter) const noexcept
Definition: IntRange.H:457
Foam::IntRange::const_reverse_iterator::difference_type
IntType difference_type
Definition: IntRange.H:367
labelFwd.H
Typedefs for label/uLabel without requiring label.H.
os
OBJstream os(runTime.globalPath()/outputName)
Foam::operator>=
bool operator>=(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A same or newer than B.
Definition: IOstreamOption.H:428
Foam::IntRange::const_iterator::difference_type
IntType difference_type
Definition: IntRange.H:256
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::IntRange::begin
const_iterator begin() const noexcept
A const_iterator set to the beginning of the range.
Definition: IntRangeI.H:343
Foam::IntRange::const_iterator::reference
IntType reference
Definition: IntRange.H:258
Foam::IntRange::const_iterator::value_type
IntType value_type
Definition: IntRange.H:255
Foam::IntRange::IntRange
constexpr IntRange() noexcept
Default construct an empty range (0,0)
Definition: IntRangeI.H:31
range
scalar range
Definition: LISASMDCalcMethod1.H:12
Foam::IntRange::size_type
IntType size_type
The type that can represent the size of the range.
Definition: IntRange.H:97
Foam::IntRange::first
IntType first() const noexcept
The (inclusive) lower value of the range. Same as start()
Definition: IntRangeI.H:443
Foam::IntRange::cbegin
const_iterator cbegin() const noexcept
A const_iterator set to the beginning of the range.
Definition: IntRangeI.H:351
Foam::IntRange::reset
void reset(const IntType beg, const IntType len) noexcept
Reset start and length, no checks.
Definition: IntRangeI.H:479
Foam::IntRange::const_reverse_iterator::iterator_category
std::random_access_iterator_tag iterator_category
Definition: IntRange.H:365
Foam::identity
labelList identity(const label len, label start=0)
Create identity map of the given length with (map[i] == i)
Definition: labelList.C:38
bool
bool
Definition: EEqn.H:20
Foam::IntRange::const_reverse_iterator::value_type
IntType value_type
Definition: IntRange.H:366
Foam::IntRange::const_iterator
Random-access input iterator with const access.
Definition: IntRange.H:246
Foam::IntRange::value_type
IntType value_type
Type of values the range contains.
Definition: IntRange.H:94
Foam::IntRange::clampSize
void clampSize() noexcept
Enforce non-negative size.
Definition: IntRangeI.H:511
Foam::IntRange::setSize
void setSize(const IntType n) noexcept
Change the size, no checks. Identical to resize()
Definition: IntRangeI.H:497
Foam::IntRange::start
IntType start() const noexcept
The (inclusive) lower value of the range.
Definition: IntRangeI.H:408
IntRangeI.H
Foam::IntRange::const_iterator::pointer
const typedef IntType * pointer
Definition: IntRange.H:257
Foam::IntRange::last
IntType last() const noexcept
The (inclusive) upper value of the range.
Definition: IntRangeI.H:450
Foam::operator>
bool operator>(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A newer than B.
Definition: IOstreamOption.H:418