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