IntRangeI.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-2022 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 \*---------------------------------------------------------------------------*/
27 
28 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
29 
30 template<class IntType>
32 :
33  start_(0),
34  size_(0)
35 {}
36 
37 
38 template<class IntType>
39 inline constexpr Foam::IntRange<IntType>::IntRange
40 (
41  const IntType len
42 ) noexcept
43 :
44  start_(0),
45  size_(len)
46 {}
47 
48 
49 template<class IntType>
50 inline constexpr Foam::IntRange<IntType>::IntRange
51 (
52  const IntType beg,
53  const IntType len
54 ) noexcept
55 :
56  start_(beg),
57  size_(len)
58 {}
59 
60 
61 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
62 
63 template<class IntType>
65 {
66  return (start_);
67 }
68 
69 
70 template<class IntType>
72 {
73  return (start_ + size_);
74 }
75 
76 
77 template<class IntType>
79 {
80  return (start_ + (size_ - 1));
81 }
82 
83 
84 template<class IntType>
85 inline IntType Foam::IntRange<IntType>::rend_value() const noexcept
86 {
87  return (start_ - 1);
88 }
89 
90 
91 // * * * * * * * * * * * * * * Forward Iterators * * * * * * * * * * * * * * //
92 
93 template<class IntType>
94 inline constexpr IntType
96 operator[](const IntType n) const noexcept
97 {
98  return (value_ + n);
99 }
100 
101 
102 template<class IntType>
106 {
107  ++value_;
108  return *this;
109 }
110 
111 
112 template<class IntType>
115 operator++(int) noexcept
116 {
117  const_iterator old(*this);
118  ++value_;
119  return old;
120 }
121 
122 
123 template<class IntType>
127 {
128  --value_;
129  return *this;
130 }
131 
132 
133 template<class IntType>
136 operator--(int) noexcept
137 {
138  const_iterator old(*this);
139  --value_;
140  return old;
141 }
142 
143 
144 template<class IntType>
147 operator+=(const IntType n) noexcept
148 {
149  value_ += n;
150  return *this;
151 }
152 
153 
154 template<class IntType>
157 operator-=(const IntType n) noexcept
158 {
159  value_ -= n;
160  return *this;
161 }
162 
163 
164 template<class IntType>
165 inline constexpr typename Foam::IntRange<IntType>::const_iterator
167 operator+(const IntType n) const noexcept
168 {
169  return const_iterator(value_ + n);
170 }
171 
172 
173 template<class IntType>
174 inline constexpr typename Foam::IntRange<IntType>::const_iterator
176 operator-(const IntType n) const noexcept
177 {
178  return const_iterator(value_ - n);
179 }
180 
181 
182 template<class IntType>
183 inline constexpr IntType
185 operator-(const const_iterator& iter) const noexcept
186 {
187  return (value_ - iter.value_);
188 }
189 
190 
191 template<class IntType>
192 inline constexpr bool
194 operator==(const const_iterator& iter) const noexcept
195 {
196  return (value_ == iter.value_);
197 }
198 
199 
200 template<class IntType>
201 inline constexpr bool
203 operator<(const const_iterator& iter) const noexcept
204 {
205  return (value_ < iter.value_);
206 }
208 
209 // * * * * * * * * * * * * * * Reverse Iterators * * * * * * * * * * * * * * //
210 
211 template<class IntType>
212 inline constexpr IntType
214 operator[](const IntType n) const noexcept
215 {
216  return (value_ - n);
217 }
218 
219 
220 template<class IntType>
224 {
225  --value_;
226  return *this;
227 }
228 
229 
230 template<class IntType>
233 operator++(int) noexcept
234 {
235  const_reverse_iterator old(*this);
236  --value_;
237  return old;
238 }
239 
240 
241 template<class IntType>
245 {
246  ++value_;
247  return *this;
248 }
249 
250 
251 template<class IntType>
254 operator--(int) noexcept
255 {
256  const_reverse_iterator old(*this);
257  ++value_;
258  return old;
259 }
260 
261 
262 template<class IntType>
265 operator+=(const IntType n) noexcept
266 {
267  value_ -= n;
268  return *this;
269 }
270 
271 
272 template<class IntType>
275 operator-=(const IntType n) noexcept
276 {
277  value_ += n;
278  return *this;
279 }
280 
281 
282 template<class IntType>
283 inline constexpr typename Foam::IntRange<IntType>::const_reverse_iterator
285 operator+(const IntType n) const noexcept
286 {
287  return const_reverse_iterator(value_ - n);
288 }
289 
290 
291 template<class IntType>
292 inline constexpr typename Foam::IntRange<IntType>::const_reverse_iterator
294 operator-(const IntType n) const noexcept
295 {
296  return const_reverse_iterator(value_ + n);
297 }
298 
299 
300 template<class IntType>
301 inline constexpr IntType
303 operator-(const const_reverse_iterator& iter) const noexcept
304 {
305  return (iter.value_ - value_);
306 }
307 
308 
309 template<class IntType>
310 inline constexpr bool
312 operator==(const const_reverse_iterator& iter) const noexcept
313 {
314  return (value_ == iter.value_);
315 }
316 
317 
318 template<class IntType>
319 inline constexpr bool
321 operator<(const const_reverse_iterator& iter) const noexcept
322 {
323  return (value_ > iter.value_);
324 }
325 
326 
327 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
328 
329 template<class IntType>
331 Foam::IntRange<IntType>::at(const IntType i) const
332 {
333  return const_iterator(start_ + ((i < 0 || i > size_) ? size_ : i));
334 }
335 
336 
337 template<class IntType>
340 {
341  return const_iterator(begin_value());
342 }
343 
344 
345 template<class IntType>
348 {
349  return const_iterator(begin_value());
350 }
351 
352 
353 template<class IntType>
356 {
357  return const_iterator(end_value());
358 }
359 
360 
361 template<class IntType>
364 {
365  return const_iterator(end_value());
366 }
367 
368 
369 template<class IntType>
372 {
373  return const_reverse_iterator(rbegin_value());
374 }
375 
376 
377 template<class IntType>
380 {
381  return const_reverse_iterator(rbegin_value());
382 }
383 
384 
385 template<class IntType>
388 {
389  return const_reverse_iterator(rend_value());
390 }
391 
392 
393 template<class IntType>
396 {
397  return const_reverse_iterator(rend_value());
398 }
399 
400 
401 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
402 
403 template<class IntType>
405 {
406  start_ = size_ = 0;
407 }
408 
409 
410 template<class IntType>
412 (
413  const IntType beg,
414  const IntType len
415 ) noexcept
416 {
417  start_ = beg;
418  size_ = len;
419 }
420 
421 
422 template<class IntType>
423 inline void Foam::IntRange<IntType>::setStart(const IntType i) noexcept
424 {
425  start_ = i;
426 }
427 
428 
429 template<class IntType>
430 inline void Foam::IntRange<IntType>::setSize(const IntType n) noexcept
431 {
432  size_ = n;
433 }
434 
435 
436 template<class IntType>
437 inline void Foam::IntRange<IntType>::resize(const IntType n) noexcept
438 {
439  size_ = n;
440 }
441 
442 
443 template<class IntType>
445 {
446  if (size_ < 0) size_ = 0;
447 }
448 
449 
450 template<class IntType>
451 inline bool Foam::IntRange<IntType>::contains(const IntType value)
452 const noexcept
453 {
454  // Avoid overflow risk:
455  // (value < (start_ + size_)) --> ((value - start_) < size_)
456 
457  return (size_ && start_ <= value && (value - start_) < size_);
458 }
459 
460 
461 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
462 
463 template<class IntType>
464 inline constexpr IntType Foam::IntRange<IntType>::
465 operator[](const IntType i) const noexcept
466 {
467  return start_ + i;
468 }
469 
470 
471 template<class IntType>
472 inline IntType Foam::IntRange<IntType>::
474 {
475  return ++size_;
476 }
477 
478 
479 template<class IntType>
480 inline IntType Foam::IntRange<IntType>::
481 operator++(int) noexcept
482 {
483  return size_++;
484 }
485 
486 
487 template<class IntType>
488 inline IntType Foam::IntRange<IntType>::
490 {
491  --size_;
493  return size_;
494 }
495 
496 
497 template<class IntType>
498 inline IntType Foam::IntRange<IntType>::
499 operator--(int) noexcept
500 {
501  const IntType old(size_);
502  --size_;
504  return old;
505 }
506 
507 
508 template<class IntType>
509 inline IntType Foam::IntRange<IntType>::
510 operator+=(const IntType n) noexcept
511 {
512  size_ += n;
513  return size_;
514 }
515 
516 
517 template<class IntType>
518 inline IntType Foam::IntRange<IntType>::
519 operator-=(const IntType n) noexcept
520 {
521  size_ -= n;
522  clampSize();
523  return size_;
524 }
525 
526 
527 // ************************************************************************* //
constexpr const_reverse_iterator operator-(const IntType n) const noexcept
Return iterator with offset.
Definition: IntRangeI.H:287
constexpr bool operator==(const const_iterator &iter) const noexcept
Test for equality of values.
Definition: IntRangeI.H:187
IntType operator--() noexcept
Decrease the size by 1, but never below 0.
Definition: IntRangeI.H:482
Random-access input iterator with const access.
Definition: IntRange.H:357
const_iterator & operator+=(const IntType n) noexcept
Arbitrary increment.
Definition: IntRangeI.H:140
constexpr IntType operator[](const IntType i) const noexcept
Offset dereference, without bounds checking.
Definition: IntRangeI.H:458
void setSize(const IntType n) noexcept
Change the size, no checks. Identical to resize()
Definition: IntRangeI.H:423
void resize(const IntType n) noexcept
Change the size, no checks. Identical to setSize()
Definition: IntRangeI.H:430
const_reverse_iterator crbegin() const noexcept
A const_reverse_iterator set to 1 before the end of range.
Definition: IntRangeI.H:372
bool contains(const IntType value) const noexcept
True if the (global) value is within the range.
Definition: IntRangeI.H:444
constexpr IntType operator[](const IntType n) const noexcept
Offset dereference operator.
Definition: IntRangeI.H:89
const_reverse_iterator & operator++() noexcept
Prefix increment.
Definition: IntRangeI.H:216
IntType rbegin_value() const noexcept
The max value of the range.
Definition: IntRangeI.H:71
const_reverse_iterator & operator+=(const IntType n) noexcept
Arbitrary increment.
Definition: IntRangeI.H:258
const_reverse_iterator crend() const noexcept
A const_reverse_iterator set to 1 before the begin of range.
Definition: IntRangeI.H:388
IntType end_value() const noexcept
The value 1 beyond the end of the range.
Definition: IntRangeI.H:64
void clampSize() noexcept
Enforce non-negative size.
Definition: IntRangeI.H:437
Random-access reverse input iterator with const access.
Definition: IntRange.H:501
constexpr bool operator<(const const_reverse_iterator &iter) const noexcept
Reverse compare less-than.
Definition: IntRangeI.H:314
const_iterator begin() const noexcept
A const_iterator set to the beginning of the range.
Definition: IntRangeI.H:332
const_iterator cend() const noexcept
A const_iterator set to 1 beyond the end of the range.
Definition: IntRangeI.H:356
const_reverse_iterator rbegin() const noexcept
A const_reverse_iterator set to 1 before the end of range.
Definition: IntRangeI.H:364
const direction noexcept
Definition: Scalar.H:258
const_iterator & operator++() noexcept
Prefix increment.
Definition: IntRangeI.H:98
IntType operator++() noexcept
Increase the size by 1.
Definition: IntRangeI.H:466
const_iterator cbegin() const noexcept
A const_iterator set to the beginning of the range.
Definition: IntRangeI.H:340
const_reverse_iterator & operator--() noexcept
Prefix decrement.
Definition: IntRangeI.H:237
const_reverse_iterator rend() const noexcept
A const_reverse_iterator set to 1 before the begin of range.
Definition: IntRangeI.H:380
const_iterator end() const noexcept
A const_iterator set to 1 beyond the end of the range.
Definition: IntRangeI.H:348
constexpr const_iterator operator-(const IntType n) const noexcept
Return iterator with offset.
Definition: IntRangeI.H:169
const_iterator & operator--() noexcept
Prefix decrement.
Definition: IntRangeI.H:119
const_iterator at(const IntType i) const
Return const_iterator to a position within the range, with bounds checking.
Definition: IntRangeI.H:324
constexpr bool operator==(const const_reverse_iterator &iter) const noexcept
Test for equality of values.
Definition: IntRangeI.H:305
IntType begin_value() const noexcept
The value at the beginning of the range - same as min(), start()
Definition: IntRangeI.H:57
constexpr const_iterator operator+(const IntType n) const noexcept
Return iterator with offset.
Definition: IntRangeI.H:160
constexpr bool operator<(const const_iterator &iter) const noexcept
Compare less-than.
Definition: IntRangeI.H:196
const_reverse_iterator & operator-=(const IntType n) noexcept
Arbitrary decrement.
Definition: IntRangeI.H:268
IntType operator-=(const IntType n) noexcept
Decrease the size by n, but never below 0.
Definition: IntRangeI.H:512
label n
const_iterator & operator-=(const IntType n) noexcept
Arbitrary decrement.
Definition: IntRangeI.H:150
constexpr IntType operator[](const IntType n) const noexcept
Offset dereference operator.
Definition: IntRangeI.H:207
IntType rend_value() const noexcept
The value 1 before the begin of range.
Definition: IntRangeI.H:78
IntType operator+=(const IntType n) noexcept
Increase the size by n.
Definition: IntRangeI.H:503
void reset() noexcept
Reset to zero start and zero size.
Definition: IntRangeI.H:397
constexpr IntRange() noexcept
Default construct an empty range (0,0)
Definition: IntRangeI.H:24
void setStart(const IntType i) noexcept
Set the start position, no checks.
Definition: IntRangeI.H:416
constexpr const_reverse_iterator operator+(const IntType n) const noexcept
Return iterator with offset.
Definition: IntRangeI.H:278