Loading...
Searching...
No Matches
OffsetRange.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) 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::OffsetRange
28
29Description
30 A tuple of integers comprising start, size, total.
31
32 This frequently corresponds what is termed a "hyperslab", a "slice"
33 or a "selection" for MPI addressing. For this type of use, the
34 OffsetRange would represent the local rank addressing. Apart from
35 the total size, it would not have information about any other ranks
36 (like Foam::globalIndex does).
37
38Note
39 Since this range is low-level, the IO operators are defined
40 in the separate OffsetRangeIO.H header.
41
42SeeAlso
43 Foam::globalIndex
44 Foam::GlobalOffset
45
46SourceFiles
47 OffsetRangeI.H
48 OffsetRangeIO.H
49
50\*---------------------------------------------------------------------------*/
51
52#ifndef Foam_OffsetRange_H
53#define Foam_OffsetRange_H
54
55#include "IndexIterator.H"
56#include "IntRange.H"
57
58// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
59
60namespace Foam
61{
62
63// Forward Declarations
64template<class T> class OffsetRange;
65class Istream;
66class Ostream;
67
68//- Read from Istream as bracketed (start size total) tuple
69template<class T> Istream& operator>>(Istream&, OffsetRange<T>&);
70
71//- Write to Ostream as bracketed (start size total) tuple
72template<class T> Ostream& operator<<(Ostream&, const OffsetRange<T>&);
73
74// Common Types
75// FUTURE?: offsetRange, labelOffsetRange, ...?
76
77/*---------------------------------------------------------------------------*\
78 Class OffsetRange Declaration
79\*---------------------------------------------------------------------------*/
80
81template<class IntType>
82class OffsetRange
83{
84 static_assert(std::is_integral_v<IntType>, "Integral required");
85
86 // Private Data
87
88 //- The start (offset)
89 IntType start_;
90
91 //- The size
92 IntType size_;
93
94 //- The total size
95 IntType total_;
96
97
98 // Private Member Functions
99
100 //- The value "at" the local index \p idx, with bounds checking.
101 // If index is out-of-bounds, returns the end_value()
102 inline IntType at_value(IntType idx) const noexcept;
103
104public:
105
106 // STL type definitions
107
108 //- Type of values contained
109 typedef IntType value_type;
110
111 //- The type that can represent the size
112 typedef IntType size_type;
113
114 //- Input iterator with const access
116
117 //- Reverse input iterator with const access
120
121 // Generated Methods
122
123 //- Copy construct
124 OffsetRange(const OffsetRange&) noexcept = default;
125
126 //- Move construct
127 OffsetRange(OffsetRange&&) noexcept = default;
128
129 //- Copy assignment
130 OffsetRange& operator=(const OffsetRange&) noexcept = default;
131
132 //- Move assignment
133 OffsetRange& operator=(OffsetRange&&) noexcept = default;
135
136 // Constructors
137
138 //- Default construct as (0,0,0)
139 inline constexpr OffsetRange() noexcept;
140
141 //- Construct with start=0 and specified size, assigns total=size
142 inline constexpr OffsetRange(IntType len) noexcept;
143
144 // No construct from (start, size).
145 // Unclear if the total should be [size] or [start+size]
146
147 //- Construct from all components, no checks
148 inline constexpr OffsetRange
149 (
150 IntType beg,
151 IntType len,
152 IntType tot
153 ) noexcept;
154
155
156 // Member Functions
158 // Access
159
160 //- True if zero-sized
161 bool empty() const noexcept { return !size_; }
162
163 //- The size
164 IntType size() const noexcept { return size_; }
165
166 //- Non-const access to the size
167 IntType& size() noexcept { return size_; }
168
169 //- The lower value of the range
170 IntType start() const noexcept { return start_; }
171
172 //- Non-const access to start of the range
173 IntType& start() noexcept { return start_; }
174
175 //- The total size
176 IntType total() const noexcept { return total_; }
177
178 //- Non-const access to the total size
179 IntType& total() noexcept { return total_; }
180
181 //- The (state,size) range
182 IntRange<IntType> range() const noexcept { return { start_, size_ }; }
183
184
185 // Edit
186
187 //- Reset to zero
188 inline void clear() noexcept;
189
190 //- Reset to the specified size, with start=0 and total=size
191 // Can also use operator= assignment
192 inline void reset(IntType len) noexcept;
194 // No reset from (start, size).
195 // Unclear if the total should be [size] or [start+size]
196
197 //- Reset all components
198 inline void reset(IntType beg, IntType len, IntType tot) noexcept;
199
200
201 // Comparison
202
203 //- Test equality of start/size/total.
204 inline bool equals(const OffsetRange& b) const noexcept;
205
206 //- Compare start/size/total in that order.
207 // \return -1,0,+1
208 inline int compare(const OffsetRange& b) const noexcept;
209
210
211 // Operators
212
213 //- Assign to the specified size, with start=0 and total=size
214 void operator=(IntType len) noexcept
215 {
216 reset(len);
217 }
219 //- Assign from an OffsetRange with the same or lower representation
220 template
221 <
222 class IntT2,
223 class = std::enable_if_t
224 <std::is_integral_v<IntT2> && (sizeof(IntT2) <= sizeof(IntType))>
225 >
226 void operator=(const OffsetRange<IntT2>& rhs) noexcept
227 {
228 start_ = rhs.start(); size_ = rhs.size(); total_ = rhs.total();
229 }
230
231
232 //- Return const_iterator to a position within the range,
233 //- with bounds checking.
234 // \return iterator at the requested position, or end() for
235 // out-of-bounds
236 const_iterator at(IntType i) const { return at_value(i); }
237
238 //- Offset dereference, without bounds checking
239 inline constexpr IntType operator[](IntType i) const noexcept;
240
241 //- True if the value is between the start and size range.
242 // Behaviour identical to contains() - usable as a predicate
243 bool operator()(IntType i) const noexcept { return contains(i); }
244
245
246 // Search
247
248 //- True if the value is between the start and size range range.
249 inline bool contains(IntType value) const noexcept;
250
251
252 // Iterator range values
253
254 //- The value at the beginning of the start/size range - same as start()
255 inline IntType begin_value() const noexcept;
256
257 //- The value 1 beyond the end of the start/size range.
258 inline IntType end_value() const noexcept;
259
260 //- The max value of the start/size range.
261 inline IntType rbegin_value() const noexcept;
262
263 //- The value 1 before the begin of start/size range
264 inline IntType rend_value() const noexcept;
265
266
267 // Bidirectional input iterators (const)
268
269 //- A const_iterator set to the beginning of the range
270 const_iterator begin() const noexcept { return begin_value(); }
271
272 //- A const_iterator set to the beginning of the range
273 const_iterator cbegin() const noexcept { return begin_value(); }
275 //- A const_iterator set to 1 beyond the end of the range.
276 const_iterator cend() const noexcept { return end_value(); }
277
278 //- A const_iterator set to 1 beyond the end of the range.
279 const_iterator end() const noexcept { return end_value(); }
280
281
282 // Bidirectional reverse input iterators (const)
283
284 //- A const_reverse_iterator set to 1 before the end of range
286 rbegin() const noexcept { return rbegin_value(); }
287
288 //- A const_reverse_iterator set to 1 before the end of range
290 crbegin() const noexcept { return rbegin_value(); }
291
292 //- A const_reverse_iterator set to 1 before the begin of range
293 const_reverse_iterator rend() const noexcept { return rend_value(); }
294
295 //- A const_reverse_iterator set to 1 before the begin of range
296 const_reverse_iterator crend() const noexcept { return rend_value(); }
297};
298
299// * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
300
301//- Test for equality of begin/size/total values
302template<class IntType>
303inline bool operator==
304(
305 const OffsetRange<IntType>& a,
307) noexcept
308{
309 return a.equals(b);
310}
311
312
313//- Comparison function for sorting, compares the start.
314// If the start values are equal, also compares the size, etc
315template<class IntType>
316inline bool operator<
317(
318 const OffsetRange<IntType>& a,
320) noexcept
321{
322 return (a.compare(b) < 0);
323}
324
325
326// Derived comparisons
327
328template<class IntType>
329inline bool operator!=
330(
331 const OffsetRange<IntType>& a,
333) noexcept
334{
335 return !a.equals(b);
336}
337
338template<class IntType>
339inline bool operator<=
340(
341 const OffsetRange<IntType>& a,
342 const OffsetRange<IntType>& b
343) noexcept
344{
345 return (a.compare(b) <= 0);
346}
347
348template<class IntType>
349inline bool operator>
350(
351 const OffsetRange<IntType>& a,
353) noexcept
354{
355 return (a.compare(b) > 0);
356}
358template<class IntType>
359inline bool operator>=
360(
361 const OffsetRange<IntType>& a,
363) noexcept
364{
365 return (a.compare(b) >= 0);
366}
368
369// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
370
371} // End namespace Foam
372
373// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
374
375#include "OffsetRangeI.H"
377// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
378
379#endif
380
381// ************************************************************************* //
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
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
A tuple of integers comprising start, size, total.
Definition OffsetRange.H:82
void clear() noexcept
Reset to zero.
constexpr IntType operator[](IntType i) const noexcept
Offset dereference, without bounds checking.
const_iterator begin() const noexcept
A const_iterator set to the beginning of the range.
IntRange< IntType > range() const noexcept
The (state,size) range.
IntType rend_value() const noexcept
The value 1 before the begin of start/size range.
bool empty() const noexcept
True if zero-sized.
OffsetRange(const OffsetRange &) noexcept=default
Copy construct.
IndexIterator< IntType > const_iterator
Input iterator with const access.
int compare(const OffsetRange &b) const noexcept
Compare start/size/total in that order.
IntType & start() noexcept
Non-const access to start of the range.
IntType start() const noexcept
The lower value of the range.
bool equals(const OffsetRange &b) const noexcept
Test equality of start/size/total.
const_iterator end() const noexcept
A const_iterator set to 1 beyond the end of the range.
IntType size_type
The type that can represent the size.
IntType rbegin_value() const noexcept
The max value of the start/size range.
IntType size() const noexcept
The size.
ReverseIndexIterator< IntType > const_reverse_iterator
Reverse input iterator with const access.
constexpr OffsetRange() noexcept
Default construct as (0,0,0).
IntType total() const noexcept
The total size.
IntType & total() noexcept
Non-const access to the total size.
bool contains(IntType value) const noexcept
True if the value is between the start and size range range.
const_iterator cend() const noexcept
A const_iterator set to 1 beyond the end of the range.
bool operator()(IntType i) const noexcept
True if the value is between the start and size range.
OffsetRange(OffsetRange &&) noexcept=default
Move construct.
IntType & size() noexcept
Non-const access to the size.
IntType begin_value() const noexcept
The value at the beginning of the start/size range - same as start().
IntType value_type
Type of values contained.
const_reverse_iterator crbegin() const noexcept
A const_reverse_iterator set to 1 before the end of range.
const_iterator cbegin() const noexcept
A const_iterator set to the beginning of the range.
const_reverse_iterator rend() const noexcept
A const_reverse_iterator set to 1 before the begin of range.
const_reverse_iterator crend() const noexcept
A const_reverse_iterator set to 1 before the begin of range.
IntType end_value() const noexcept
The value 1 beyond the end of the start/size range.
const_iterator at(IntType i) const
Return const_iterator to a position within the range, with bounds checking.
void reset(IntType len) noexcept
Reset to the specified size, with start=0 and total=size.
const_reverse_iterator rbegin() const noexcept
A const_reverse_iterator set to 1 before the end of range.
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...
limits reset(1/(limits.max()+VSMALL), 1/(limits.min()+VSMALL))
Namespace for OpenFOAM.
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces).
Istream & operator>>(Istream &, directionInfo &)
void rhs(fvMatrix< typename Expr::value_type > &m, const Expr &expression)
const direction noexcept
Definition scalarImpl.H:265
volScalarField & b