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