Loading...
Searching...
No Matches
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-2025 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 Since this range is low-level, the IO operators are defined
34 in the separate IntRangeIO.H header.
35
36SourceFiles
37 IntRangeI.H
38 IntRangeIO.H
39
40\*---------------------------------------------------------------------------*/
41
42#ifndef Foam_IntRange_H
43#define Foam_IntRange_H
44
45#include "IndexIterator.H"
46#include "labelFwd.H"
47
48// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
49
50namespace Foam
51{
52
53// Forward Declarations
54template<class T> class IntRange;
55class Istream;
56class Ostream;
57
58//- Read from Istream as bracketed (start size) tuple
59template<class T> Istream& operator>>(Istream&, IntRange<T>&);
60
61//- Write to Ostream as bracketed (start size) tuple
62template<class T> Ostream& operator<<(Ostream&, const IntRange<T>&);
63
64/*---------------------------------------------------------------------------*\
65 Class IntRange Declaration
66\*---------------------------------------------------------------------------*/
67
68template<class IntType>
69class IntRange
70{
71 static_assert(std::is_integral_v<IntType>, "Integral required");
72
73 // Private Data
74
75 //- The start of the interval
76 IntType start_;
77
78 //- The length of the interval
79 IntType size_;
80
81
82 // Private Member Functions
83
84 //- The value "at" the local index \p idx, with bounds checking.
85 // If index is out-of-bounds, returns the end_value()
86 inline IntType at_value(IntType idx) const noexcept;
87
88public:
89
90 // STL type definitions
91
92 //- Type of values the range contains
93 typedef IntType value_type;
94
95 //- The type that can represent the size of the range
96 typedef IntType size_type;
97
98 //- Input iterator with const access
100
101 //- Reverse input iterator with const access
103
104
105 // Public Classes
107 //- Unary predicate for greater than 0 (int values)
108 struct gt0
109 {
110 template<class Int>
111 constexpr bool operator()(Int val) const noexcept
112 {
113 return (val > 0);
114 }
115 };
117 //- Unary predicate for less than 0 (int values)
118 struct lt0
119 {
120 template<class Int>
121 constexpr bool operator()(Int val) const noexcept
122 {
123 return (val < 0);
125 };
126
127 //- Unary predicate for greater-equal 0 (int values)
128 struct ge0
129 {
130 template<class Int>
131 constexpr bool operator()(Int val) const noexcept
132 {
133 return (val >= 0);
134 }
135 };
137 //- Unary predicate for less-equal 0 (int values)
138 struct le0
140 template<class Int>
141 constexpr bool operator()(Int val) const noexcept
142 {
143 return (val <= 0);
144 }
145 };
146
147
148 // Generated Methods: copy/move construct, copy/move assignment
149
150
151 // Constructors
152
153 //- Default construct an empty range (0,0)
154 inline constexpr IntRange() noexcept;
155
156 //- Construct a range with specified length, starting at zero (0,len)
157 inline explicit constexpr IntRange(IntType len) noexcept;
158
159 //- Construct a range from start/length, no checks
160 inline constexpr IntRange(IntType beg, IntType len) noexcept;
161
162
163 // Member Functions
164
165 // Access
166
167 //- True if range is empty (zero-sized)
168 bool empty() const noexcept { return !size_; }
169
170 //- True if range has size greater than zero
171 bool good() const noexcept { return (size_ > 0); }
172
173 //- The size of the range
174 IntType size() const noexcept { return size_; }
175
176 //- Non-const access to size of the range
177 IntType& size() noexcept { return size_; }
179 //- The (inclusive) lower value of the range
180 IntType start() const noexcept { return start_; }
181
182 //- Non-const access to start of the range
183 IntType& start() noexcept { return start_; }
184
185 //- The (inclusive) lower value of the range,
186 //- same as start(), begin_value()
187 IntType min() const noexcept { return start_; }
189 //- The (inclusive) upper value of the range,
190 //- same as rbegin_value(). Ill-defined for an empty range
191 IntType max() const noexcept { return (start_ + (size_ - 1)); }
192
193
194 // Edit
195
196 //- Reset to (0,0)
197 inline void clear() noexcept ;
199 //- Reset to (0,0)
200 inline void reset() noexcept;
201
202 //- Reset start and length, no checks
203 inline void reset(IntType beg, IntType len) noexcept;
204
205 //- Set the start position, no checks
206 inline void setStart(IntType i) noexcept;
207
208 //- Change the size, no checks. Identical to resize()
209 inline void setSize(IntType n) noexcept;
210
211 //- Change the size, no checks. Identical to setSize()
212 inline void resize(IntType n) noexcept;
214 //- Enforce non-negative size
215 inline void clampSize() noexcept;
216
217
218 // Search
219
220 //- True if the (global) value is within the range
221 inline bool contains(IntType value) const noexcept;
222
223 //- True if the (global) value is within the range
224 bool found(IntType val) const noexcept { return contains(val); }
225
226
227 // Member Operators
228
229 //- Return const_iterator to a position within the range,
230 //- with bounds checking.
231 // \return iterator at the requested position, or end() for
232 // out-of-bounds
233 const_iterator at(IntType i) const { return at_value(i); }
234
235 //- Offset dereference, without bounds checking
236 inline constexpr IntType operator[](IntType i) const noexcept;
237
238 //- True if the global value is located within the range.
239 // Behaviour identical to contains() - usable as a predicate
240 bool operator()(IntType i) const noexcept { return contains(i); }
241
242 //- Increase the size by 1.
243 inline IntType operator++() noexcept;
244 inline IntType operator++(int) noexcept;
245
246 //- Increase the size by n.
247 inline IntType operator+=(IntType n) noexcept;
249 //- Decrease the size by 1, but never below 0.
250 inline IntType operator--() noexcept;
251 inline IntType operator--(int) noexcept;
252
253 //- Decrease the size by n, but never below 0.
254 inline IntType operator-=(IntType n) noexcept;
255
256 //- True if range has size greater than zero. Same as good()
257 explicit operator bool() const noexcept { return (size_ > 0); }
259
260 // Iterator ranges
261
262 //- The value at the beginning of the range - same as min(), start()
263 inline IntType begin_value() const noexcept;
264
265 //- The value 1 beyond the end of the range.
266 inline IntType end_value() const noexcept;
267
268 //- The max value of the range.
269 inline IntType rbegin_value() const noexcept;
270
271 //- The value 1 before the begin of range
272 inline IntType rend_value() const noexcept;
274
275 // Bidirectional input iterators (const)
276
277 //- A const_iterator set to the beginning of the range
278 const_iterator begin() const noexcept { return begin_value(); }
279
280 //- A const_iterator set to the beginning of the range
282
283 //- A const_iterator set to 1 beyond the end of the range.
284 const_iterator cend() const noexcept { return end_value(); }
285
286 //- A const_iterator set to 1 beyond the end of the range.
287 const_iterator end() const noexcept { return end_value(); }
288
289
290 // Bidirectional reverse input iterators (const)
291
292 //- A const_reverse_iterator set to 1 before the end of range
294 rbegin() const noexcept { return rbegin_value(); }
295
296 //- A const_reverse_iterator set to 1 before the end of range
298 crbegin() const noexcept { return rbegin_value(); }
299
300 //- A const_reverse_iterator set to 1 before the begin of range
301 const_reverse_iterator rend() const noexcept { return rend_value(); }
302
303 //- A const_reverse_iterator set to 1 before the begin of range
304 const_reverse_iterator crend() const noexcept { return rend_value(); }
305};
306
307
308// * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
309
310//- Test for equality of begin/size values
311template<class IntType>
312inline constexpr bool operator==
313(
314 const IntRange<IntType>& a,
316) noexcept
317{
318 return (a.start() == b.start() && a.size() == b.size());
319}
320
322//- Comparison function for sorting, compares the start.
323// If the start values are equal, also compares the size.
324template<class IntType>
325inline constexpr bool operator<
328 const IntRange<IntType>& b
329) noexcept
330{
331 return
333 a.start() < b.start()
334 ||
335 (
336 !(b.start() < a.start())
337 && a.size() < b.size()
338 )
339 );
340}
341
342
343// Derived comparisons
344
345template<class IntType>
346inline constexpr bool operator!=
347(
348 const IntRange<IntType>& a,
349 const IntRange<IntType>& b
350) noexcept
351{
352 return !(a == b);
353}
354
355template<class IntType>
356inline constexpr bool operator<=
357(
358 const IntRange<IntType>& a,
359 const IntRange<IntType>& b
360) noexcept
361{
362 return !(b < a);
363}
364
365template<class IntType>
366inline constexpr bool operator>
367(
369 const IntRange<IntType>& b
370) noexcept
371{
372 return (b < a);
374
375template<class IntType>
376inline constexpr bool operator>=
377(
379 const IntRange<IntType>& b
380) noexcept
381
382{
383 return !(a < b);
384}
385
386
387// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
388
389} // End namespace Foam
390
391// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
393#include "IntRangeI.H"
394
395#endif
396
397// ************************************************************************* //
label n
A random-access, integer-like, input iterator for integral values.
An interval of (signed) integers defined by a start and a size.
Definition IntRange.H:69
void clear() noexcept
Reset to (0,0).
Definition IntRangeI.H:91
IntType operator++() noexcept
Increase the size by 1.
Definition IntRangeI.H:163
constexpr IntType operator[](IntType i) const noexcept
Offset dereference, without bounds checking.
Definition IntRangeI.H:155
const_iterator begin() const noexcept
Definition IntRange.H:368
void reset() noexcept
int rend_value() const noexcept
bool empty() const noexcept
Definition IntRange.H:198
IndexIterator< int > const_iterator
Definition IntRange.H:111
IntType max() const noexcept
The (inclusive) upper value of the range, same as rbegin_value(). Ill-defined for an empty range.
Definition IntRange.H:235
IntType & start() noexcept
Non-const access to start of the range.
Definition IntRange.H:223
IntType start() const noexcept
The (inclusive) lower value of the range.
Definition IntRange.H:218
bool found(int val) const noexcept
Definition IntRange.H:286
bool good() const noexcept
True if range has size greater than zero.
Definition IntRange.H:203
const_iterator end() const noexcept
A const_iterator set to 1 beyond the end of the range.
Definition IntRange.H:383
void setStart(int i) noexcept
int rbegin_value() const noexcept
IntType size() const noexcept
The size of the range.
Definition IntRange.H:208
ReverseIndexIterator< int > const_reverse_iterator
Definition IntRange.H:116
void resize(int n) noexcept
void clampSize() noexcept
bool contains(int value) const noexcept
const_iterator cend() const noexcept
A const_iterator set to 1 beyond the end of the range.
Definition IntRange.H:378
bool operator()(IntType i) const noexcept
True if the global value is located within the range.
Definition IntRange.H:310
IntType min() const noexcept
The (inclusive) lower value of the range, same as start(), begin_value().
Definition IntRange.H:229
IntType & size() noexcept
Non-const access to size of the range.
Definition IntRange.H:213
IntType begin_value() const noexcept
The value at the beginning of the range - same as min(), start().
Definition IntRangeI.H:61
const_reverse_iterator crbegin() const noexcept
A const_reverse_iterator set to 1 before the end of range.
Definition IntRange.H:398
constexpr IntRange() noexcept
Default construct an empty range (0,0).
Definition IntRangeI.H:24
const_iterator cbegin() const noexcept
A const_iterator set to the beginning of the range.
Definition IntRange.H:373
const_reverse_iterator rend() const noexcept
A const_reverse_iterator set to 1 before the begin of range.
Definition IntRange.H:403
const_reverse_iterator crend() const noexcept
A const_reverse_iterator set to 1 before the begin of range.
Definition IntRange.H:408
int end_value() const noexcept
const_iterator at(IntType i) const
Return const_iterator to a position within the range, with bounds checking.
Definition IntRange.H:298
void setSize(int n) noexcept
const_reverse_iterator rbegin() const noexcept
A const_reverse_iterator set to 1 before the end of range.
Definition IntRange.H:392
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
A random-access, integer-like, input iterator for integral values that behaves like a reverse iterato...
Typedefs for label/uLabel without requiring label.H.
Namespace for OpenFOAM.
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces).
Istream & operator>>(Istream &, directionInfo &)
const direction noexcept
Definition scalarImpl.H:265
volScalarField & b
Unary predicate for greater-equal 0 (int values).
Definition IntRange.H:149
constexpr bool operator()(Int val) const noexcept
Definition IntRange.H:151
Unary predicate for greater than 0 (int values).
Definition IntRange.H:125
constexpr bool operator()(Int val) const noexcept
Definition IntRange.H:127
Unary predicate for less-equal 0 (int values).
Definition IntRange.H:161
constexpr bool operator()(Int val) const noexcept
Definition IntRange.H:163
Unary predicate for less than 0 (int values).
Definition IntRange.H:137
constexpr bool operator()(Int val) const noexcept
Definition IntRange.H:139