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