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 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>
31 inline constexpr Foam::IntRange<IntType>::IntRange() noexcept
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 // * * * * * * * * * * * * * * Forward Iterators * * * * * * * * * * * * * * //
62 
63 template<class IntType>
65 const_iterator(const IntType val) noexcept
66 :
67  value_(val)
68 {}
69 
70 
71 template<class IntType>
72 inline constexpr IntType
74 operator*() const noexcept
75 {
76  return value_;
77 }
78 
79 
80 template<class IntType>
81 inline constexpr IntType
83 operator[](const IntType n) const noexcept
84 {
85  return (value_ + n);
86 }
87 
88 
89 template<class IntType>
92 operator++() noexcept
93 {
94  ++value_;
95  return *this;
96 }
97 
98 
99 template<class IntType>
102 operator++(int) noexcept
103 {
104  const_iterator old(*this);
105  ++value_;
106  return old;
107 }
108 
109 
110 template<class IntType>
113 operator--() noexcept
114 {
115  --value_;
116  return *this;
117 }
118 
119 
120 template<class IntType>
123 operator--(int) noexcept
124 {
125  const_iterator old(*this);
126  --value_;
127  return old;
128 }
129 
130 
131 template<class IntType>
134 operator+=(const IntType n) noexcept
135 {
136  value_ += n;
137  return *this;
138 }
139 
140 
141 template<class IntType>
144 operator-=(const IntType n) noexcept
145 {
146  value_ -= n;
147  return *this;
148 }
149 
150 
151 template<class IntType>
152 inline constexpr typename Foam::IntRange<IntType>::const_iterator
154 operator+(const IntType n) const noexcept
155 {
156  return const_iterator(value_ + n);
157 }
158 
159 
160 template<class IntType>
161 inline constexpr typename Foam::IntRange<IntType>::const_iterator
163 operator-(const IntType n) const noexcept
164 {
165  return const_iterator(value_ - n);
166 }
167 
168 
169 template<class IntType>
170 inline constexpr IntType
172 operator-(const const_iterator& iter) const noexcept
173 {
174  return (value_ - iter.value_);
175 }
176 
177 
178 template<class IntType>
179 inline constexpr bool
181 operator==(const const_iterator& iter) const noexcept
182 {
183  return (value_ == iter.value_);
184 }
185 
186 
187 template<class IntType>
188 inline constexpr bool
190 operator<(const const_iterator& iter) const noexcept
191 {
192  return (value_ < iter.value_);
193 }
194 
195 
196 // * * * * * * * * * * * * * * Reverse Iterators * * * * * * * * * * * * * * //
197 
198 template<class IntType>
200 const_reverse_iterator(const IntType val) noexcept
201 :
202  value_(val)
203 {}
204 
205 
206 template<class IntType>
207 inline constexpr IntType
209 operator*() const noexcept
210 {
211  return value_;
212 }
213 
214 
215 template<class IntType>
216 inline constexpr IntType
218 operator[](const IntType n) const noexcept
219 {
220  return (value_ - n);
221 }
222 
223 
224 template<class IntType>
227 operator++() noexcept
228 {
229  --value_;
230  return *this;
231 }
232 
233 
234 template<class IntType>
237 operator++(int) noexcept
238 {
239  const_reverse_iterator old(*this);
240  --value_;
241  return old;
242 }
243 
244 
245 template<class IntType>
248 operator--() noexcept
249 {
250  ++value_;
251  return *this;
252 }
253 
254 
255 template<class IntType>
258 operator--(int) noexcept
259 {
260  const_reverse_iterator old(*this);
261  ++value_;
262  return old;
263 }
264 
265 
266 template<class IntType>
269 operator+=(const IntType n) noexcept
270 {
271  value_ -= n;
272  return *this;
273 }
274 
275 
276 template<class IntType>
279 operator-=(const IntType n) noexcept
280 {
281  value_ += n;
282  return *this;
283 }
284 
285 
286 template<class IntType>
287 inline constexpr typename Foam::IntRange<IntType>::const_reverse_iterator
289 operator+(const IntType n) const noexcept
290 {
291  return const_reverse_iterator(value_ - n);
292 }
293 
294 
295 template<class IntType>
296 inline constexpr typename Foam::IntRange<IntType>::const_reverse_iterator
298 operator-(const IntType n) const noexcept
299 {
300  return const_reverse_iterator(value_ + n);
301 }
302 
303 
304 template<class IntType>
305 inline constexpr IntType
307 operator-(const const_reverse_iterator& iter) const noexcept
308 {
309  return (iter.value_ - value_);
310 }
311 
312 
313 template<class IntType>
314 inline constexpr bool
316 operator==(const const_reverse_iterator& iter) const noexcept
317 {
318  return (value_ == iter.value_);
319 }
320 
321 
322 template<class IntType>
323 inline constexpr bool
325 operator<(const const_reverse_iterator& iter) const noexcept
326 {
327  return (value_ > iter.value_);
328 }
329 
330 
331 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
332 
333 template<class IntType>
335 Foam::IntRange<IntType>::at(const IntType i) const
336 {
337  return const_iterator(start_ + ((i < 0 || i > size_) ? size_ : i));
338 }
339 
340 
341 template<class IntType>
344 {
345  return const_iterator(start_);
346 }
347 
348 
349 template<class IntType>
352 {
353  return const_iterator(start_);
354 }
355 
356 
357 template<class IntType>
360 {
361  return const_iterator(start_ + size_);
362 }
363 
364 
365 template<class IntType>
368 {
369  return const_iterator(start_ + size_);
370 }
371 
372 
373 template<class IntType>
376 {
377  return const_reverse_iterator(start_ + (size_-1));
378 }
379 
380 
381 template<class IntType>
384 {
385  return const_reverse_iterator(start_ + (size_-1));
386 }
387 
388 
389 template<class IntType>
392 {
393  return const_reverse_iterator(start_ - 1);
394 }
395 
396 
397 template<class IntType>
400 {
401  return const_reverse_iterator(start_ - 1);
402 }
403 
404 
405 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
406 
407 template<class IntType>
408 inline IntType Foam::IntRange<IntType>::start() const noexcept
409 {
410  return start_;
411 }
412 
413 
414 template<class IntType>
415 inline IntType Foam::IntRange<IntType>::size() const noexcept
416 {
417  return size_;
418 }
419 
420 
421 template<class IntType>
422 inline IntType& Foam::IntRange<IntType>::start() noexcept
423 {
424  return start_;
425 }
426 
427 
428 template<class IntType>
429 inline IntType& Foam::IntRange<IntType>::size() noexcept
430 {
431  return size_;
432 }
433 
434 
435 template<class IntType>
436 inline bool Foam::IntRange<IntType>::empty() const noexcept
437 {
438  return !size_;
439 }
440 
441 
442 template<class IntType>
443 inline IntType Foam::IntRange<IntType>::first() const noexcept
444 {
445  return start_;
446 }
447 
448 
449 template<class IntType>
450 inline IntType Foam::IntRange<IntType>::last() const noexcept
451 {
452  return (start_ + (size_-1));
453 }
454 
455 
456 template<class IntType>
457 inline IntType Foam::IntRange<IntType>::rend_value() const noexcept
458 {
459  return (start_ - 1);
460 }
461 
462 
463 template<class IntType>
464 inline IntType Foam::IntRange<IntType>::end_value() const noexcept
465 {
466  return (start_ + size_);
467 }
468 
469 
470 template<class IntType>
471 inline void Foam::IntRange<IntType>::clear() noexcept
472 {
473  start_ = size_ = 0;
474 }
475 
476 
477 template<class IntType>
479 (
480  const IntType beg,
481  const IntType len
482 ) noexcept
483 {
484  start_ = beg;
485  size_ = len;
486 }
487 
488 
489 template<class IntType>
490 inline void Foam::IntRange<IntType>::setStart(const IntType i) noexcept
491 {
492  start_ = i;
493 }
494 
495 
496 template<class IntType>
497 inline void Foam::IntRange<IntType>::setSize(const IntType n) noexcept
498 {
499  size_ = n;
500 }
501 
502 
503 template<class IntType>
504 inline void Foam::IntRange<IntType>::resize(const IntType n) noexcept
505 {
506  size_ = n;
507 }
508 
509 
510 template<class IntType>
512 {
513  if (size_ < 0) size_ = 0;
514 }
515 
516 
517 template<class IntType>
518 inline bool Foam::IntRange<IntType>::found(const IntType value) const noexcept
519 {
520  // Avoid overflow risk:
521  // (value < (start_ + size_)) --> ((value - start_) < size_)
522 
523  return (size_ && start_ <= value && (value - start_) < size_);
524 }
525 
526 
527 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
528 
529 template<class IntType>
530 inline constexpr IntType Foam::IntRange<IntType>::
531 operator[](const IntType i) const noexcept
532 {
533  return start_ + i;
534 }
535 
536 
537 template<class IntType>
538 inline bool Foam::IntRange<IntType>::
539 operator()(const IntType value) const noexcept
540 {
541  return found(value);
542 }
543 
544 
545 template<class IntType>
546 inline IntType Foam::IntRange<IntType>::
547 operator++() noexcept
548 {
549  return ++size_;
550 }
551 
552 
553 template<class IntType>
554 inline IntType Foam::IntRange<IntType>::
555 operator++(int) noexcept
556 {
557  return size_++;
558 }
559 
560 
561 template<class IntType>
562 inline IntType Foam::IntRange<IntType>::
563 operator--() noexcept
564 {
565  --size_;
566  clampSize();
567  return size_;
568 }
569 
570 
571 template<class IntType>
572 inline IntType Foam::IntRange<IntType>::
573 operator--(int) noexcept
574 {
575  const IntType old(size_);
576  --size_;
577  clampSize();
578  return old;
579 }
580 
581 
582 template<class IntType>
583 inline IntType Foam::IntRange<IntType>::
584 operator+=(const IntType n) noexcept
585 {
586  size_ += n;
587  return size_;
588 }
589 
590 
591 template<class IntType>
592 inline IntType Foam::IntRange<IntType>::
593 operator-=(const IntType n) noexcept
594 {
595  size_ -= n;
596  clampSize();
597  return size_;
598 }
599 
600 
601 // ************************************************************************* //
Foam::IntRange::const_reverse_iterator::operator+
constexpr const_reverse_iterator operator+(const IntType n) const noexcept
Return iterator with offset.
Definition: IntRangeI.H:289
Foam::IntRange::crbegin
const_reverse_iterator crbegin() const noexcept
A const_reverse_iterator set to 1 before the end of range.
Definition: IntRangeI.H:383
Foam::IntRange::const_iterator::operator-=
const_iterator & operator-=(const IntType n) noexcept
Arbitrary decrement.
Definition: IntRangeI.H:144
Foam::IntRange::rend_value
IntType rend_value() const noexcept
The value 1 before the start of the range.
Definition: IntRangeI.H:457
Foam::IntRange::at
const_iterator at(const IntType i) const
Definition: IntRangeI.H:335
Foam::IntRange::const_reverse_iterator::operator*
constexpr IntType operator*() const noexcept
Return the value.
Definition: IntRangeI.H:209
Foam::IntRange::end
const_iterator end() const noexcept
A const_iterator set to 1 beyond the end of the range.
Definition: IntRangeI.H:359
Foam::IntRange::const_reverse_iterator::const_reverse_iterator
constexpr const_reverse_iterator(const IntType val=0) noexcept
Construct with specified value, or default construct.
Definition: IntRangeI.H:200
Foam::IntRange::rbegin
const_reverse_iterator rbegin() const noexcept
A const_reverse_iterator set to 1 before the end of range.
Definition: IntRangeI.H:375
Foam::IntRange::setStart
void setStart(const IntType i) noexcept
Set the start position, no checks.
Definition: IntRangeI.H:490
Foam::IntRange::clear
void clear() noexcept
Reset to zero start and zero size.
Definition: IntRangeI.H:471
Foam::IntRange::found
bool found(const IntType value) const noexcept
True if the (global) value is located within the range.
Definition: IntRangeI.H:518
Foam::IntRange::rend
const_reverse_iterator rend() const noexcept
A const_reverse_iterator set to 1 before the begin of range.
Definition: IntRangeI.H:391
Foam::IntRange::const_iterator::operator==
constexpr bool operator==(const const_iterator &iter) const noexcept
Test for equality of values.
Definition: IntRangeI.H:181
Foam::IntRange::const_reverse_iterator
Random-access reverse input iterator with const access.
Definition: IntRange.H:357
Foam::IntRange::operator[]
constexpr IntType operator[](const IntType i) const noexcept
Offset dereference, without bounds checking.
Definition: IntRangeI.H:531
Foam::IntRange::const_iterator::operator*
constexpr IntType operator*() const noexcept
Return the value.
Definition: IntRangeI.H:74
Foam::IntRange::cend
const_iterator cend() const noexcept
A const_iterator set to 1 beyond the end of the range.
Definition: IntRangeI.H:367
Foam::IntRange::resize
void resize(const IntType n) noexcept
Change the size, no checks. Identical to setSize()
Definition: IntRangeI.H:504
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::IntRange::size
IntType size() const noexcept
The size of the range.
Definition: IntRangeI.H:415
Foam::IntRange::operator()
bool operator()(const IntType value) const noexcept
True if the global value is located within the range.
Definition: IntRangeI.H:539
Foam::IntRange::const_iterator::operator++
const_iterator & operator++() noexcept
Prefix increment.
Definition: IntRangeI.H:92
Foam::IntRange::operator-=
IntType operator-=(const IntType n) noexcept
Decrease the size by n, but never below 0.
Definition: IntRangeI.H:593
Foam::IntRange::end_value
IntType end_value() const noexcept
The value 1 beyond the end of the range.
Definition: IntRangeI.H:464
Foam::IntRange::const_iterator::operator--
const_iterator & operator--() noexcept
Prefix decrement.
Definition: IntRangeI.H:113
Foam::IntRange::crend
const_reverse_iterator crend() const noexcept
A const_reverse_iterator set to 1 before the begin of range.
Definition: IntRangeI.H:399
Foam::IntRange::empty
bool empty() const noexcept
True if range is empty (zero-sized)
Definition: IntRangeI.H:436
Foam::IntRange::const_reverse_iterator::operator+=
const_reverse_iterator & operator+=(const IntType n) noexcept
Arbitrary increment.
Definition: IntRangeI.H:269
Foam::IntRange::const_reverse_iterator::operator[]
constexpr IntType operator[](const IntType n) const noexcept
Offset dereference operator.
Definition: IntRangeI.H:218
Foam::IntRange::const_iterator::operator<
constexpr bool operator<(const const_iterator &iter) const noexcept
Compare less-than.
Definition: IntRangeI.H:190
Foam::IntRange::begin
const_iterator begin() const noexcept
A const_iterator set to the beginning of the range.
Definition: IntRangeI.H:343
Foam::IntRange::const_iterator::const_iterator
constexpr const_iterator(const IntType val=0) noexcept
Construct with specified value, or default construct.
Definition: IntRangeI.H:65
Foam::IntRange::const_reverse_iterator::operator-
constexpr const_reverse_iterator operator-(const IntType n) const noexcept
Return iterator with offset.
Definition: IntRangeI.H:298
Foam::IntRange::const_reverse_iterator::operator--
const_reverse_iterator & operator--() noexcept
Prefix decrement.
Definition: IntRangeI.H:248
Foam::IntRange::IntRange
constexpr IntRange() noexcept
Default construct an empty range (0,0)
Definition: IntRangeI.H:31
Foam::IntRange::operator+=
IntType operator+=(const IntType n) noexcept
Increase the size by n.
Definition: IntRangeI.H:584
Foam::IntRange::first
IntType first() const noexcept
The (inclusive) lower value of the range. Same as start()
Definition: IntRangeI.H:443
Foam::IntRange::operator++
IntType operator++() noexcept
Increase the size by 1.
Definition: IntRangeI.H:547
Foam::IntRange::cbegin
const_iterator cbegin() const noexcept
A const_iterator set to the beginning of the range.
Definition: IntRangeI.H:351
Foam::IntRange::reset
void reset(const IntType beg, const IntType len) noexcept
Reset start and length, no checks.
Definition: IntRangeI.H:479
Foam::IntRange::const_iterator
Random-access input iterator with const access.
Definition: IntRange.H:246
Foam::IntRange::const_iterator::operator+=
const_iterator & operator+=(const IntType n) noexcept
Arbitrary increment.
Definition: IntRangeI.H:134
Foam::IntRange::const_reverse_iterator::operator-=
const_reverse_iterator & operator-=(const IntType n) noexcept
Arbitrary decrement.
Definition: IntRangeI.H:279
Foam::IntRange::const_iterator::operator-
constexpr const_iterator operator-(const IntType n) const noexcept
Return iterator with offset.
Definition: IntRangeI.H:163
Foam::IntRange::const_reverse_iterator::operator==
constexpr bool operator==(const const_reverse_iterator &iter) const noexcept
Test for equality of values.
Definition: IntRangeI.H:316
Foam::IntRange::clampSize
void clampSize() noexcept
Enforce non-negative size.
Definition: IntRangeI.H:511
Foam::IntRange::operator--
IntType operator--() noexcept
Decrease the size by 1, but never below 0.
Definition: IntRangeI.H:563
Foam::IntRange::setSize
void setSize(const IntType n) noexcept
Change the size, no checks. Identical to resize()
Definition: IntRangeI.H:497
Foam::IntRange::start
IntType start() const noexcept
The (inclusive) lower value of the range.
Definition: IntRangeI.H:408
Foam::IntRange::const_iterator::operator+
constexpr const_iterator operator+(const IntType n) const noexcept
Return iterator with offset.
Definition: IntRangeI.H:154
Foam::IntRange::const_reverse_iterator::operator<
constexpr bool operator<(const const_reverse_iterator &iter) const noexcept
Reverse compare less-than.
Definition: IntRangeI.H:325
Foam::IntRange::last
IntType last() const noexcept
The (inclusive) upper value of the range.
Definition: IntRangeI.H:450
Foam::IntRange::const_iterator::operator[]
constexpr IntType operator[](const IntType n) const noexcept
Offset dereference operator.
Definition: IntRangeI.H:83
Foam::IntRange::const_reverse_iterator::operator++
const_reverse_iterator & operator++() noexcept
Prefix increment.
Definition: IntRangeI.H:227