Loading...
Searching...
No Matches
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-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
26\*---------------------------------------------------------------------------*/
27
28// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
29
30template<class IntType>
33 start_(0),
34 size_(0)
35{}
36
37
38template<class IntType>
39inline constexpr Foam::IntRange<IntType>::IntRange(IntType len) noexcept
41 start_(0),
42 size_(len)
43{}
44
45
46template<class IntType>
48(
49 IntType beg,
50 IntType len
52:
53 start_(beg),
54 size_(len)
55{}
56
57
58// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
59
60template<class IntType>
61inline IntType Foam::IntRange<IntType>::at_value(IntType i) const noexcept
62{
63 return (start_ + ((i < 0 || i > size_) ? size_ : i));
64}
65
66
67template<class IntType>
69{
70 return (start_);
71}
72
73
74template<class IntType>
76{
77 return (start_ + size_);
78}
79
80
81template<class IntType>
83{
84 return (start_ + (size_ - 1));
85}
86
87
88template<class IntType>
90{
91 return (start_ - 1);
92}
93
94
95// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
96
97template<class IntType>
100 start_ = 0;
101 size_ = 0;
102}
103
104
105template<class IntType>
108 start_ = 0;
109 size_ = 0;
110}
111
112
113template<class IntType>
114inline void Foam::IntRange<IntType>::reset(IntType beg, IntType len) noexcept
116 start_ = beg;
117 size_ = len;
118}
119
120
121template<class IntType>
122inline void Foam::IntRange<IntType>::setStart(IntType i) noexcept
123{
124 start_ = i;
125}
126
127
128template<class IntType>
129inline void Foam::IntRange<IntType>::setSize(IntType n) noexcept
130{
131 size_ = n;
132}
133
134
135template<class IntType>
136inline void Foam::IntRange<IntType>::resize(IntType n) noexcept
137{
138 size_ = n;
139}
140
141
142template<class IntType>
144{
145 if (size_ < 0) size_ = 0;
146}
147
148
149template<class IntType>
150inline bool Foam::IntRange<IntType>::contains(IntType value)
151const noexcept
152{
153 // This check is non-overflowing...
154 return (size_ && start_ <= value && (value - start_) < size_);
155}
156
157
158// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
159
160template<class IntType>
161inline constexpr IntType Foam::IntRange<IntType>::
162operator[](IntType i) const noexcept
163{
164 return (start_ + i);
165}
166
167
168template<class IntType>
169inline IntType Foam::IntRange<IntType>::
171{
172 return ++size_;
173}
174
175
176template<class IntType>
177inline IntType Foam::IntRange<IntType>::
178operator++(int) noexcept
179{
180 return size_++;
181}
182
183
184template<class IntType>
185inline IntType Foam::IntRange<IntType>::
188 if (size_ > 0) --size_; // clamp: no negative sizes
189 return size_;
190}
191
192
193template<class IntType>
194inline IntType Foam::IntRange<IntType>::
195operator--(int) noexcept
196{
197 auto old(size_);
198 if (size_ > 0) --size_; // clamp: no negative sizes
199 return old;
200}
201
202
203template<class IntType>
204inline IntType Foam::IntRange<IntType>::
205operator+=(IntType n) noexcept
207 size_ += n;
208 return size_;
209}
210
211
212template<class IntType>
213inline IntType Foam::IntRange<IntType>::
214operator-=(IntType n) noexcept
215{
216 size_ -= n;
217 if (size_ <= 0) size_ = 0; // clamp: no negative sizes
218 return size_;
219}
220
221
222// ************************************************************************* //
label n
void clear() noexcept
Reset to (0,0).
Definition IntRangeI.H:91
IntType operator+=(IntType n) noexcept
Increase the size by n.
Definition IntRangeI.H:198
IntType operator++() noexcept
Increase the size by 1.
Definition IntRangeI.H:163
constexpr IntType operator[](IntType i) const noexcept
Offset dereference, without bounds checking.
Definition IntRangeI.H:155
void reset() noexcept
Reset to (0,0).
Definition IntRangeI.H:99
IntType operator--() noexcept
Decrease the size by 1, but never below 0.
Definition IntRangeI.H:179
IntType rend_value() const noexcept
The value 1 before the begin of range.
Definition IntRangeI.H:82
void setStart(IntType i) noexcept
Set the start position, no checks.
Definition IntRangeI.H:115
IntType rbegin_value() const noexcept
The max value of the range.
Definition IntRangeI.H:75
void resize(IntType n) noexcept
Change the size, no checks. Identical to setSize().
Definition IntRangeI.H:129
void clampSize() noexcept
Enforce non-negative size.
Definition IntRangeI.H:136
bool contains(IntType value) const noexcept
True if the (global) value is within the range.
Definition IntRangeI.H:143
IntType begin_value() const noexcept
The value at the beginning of the range - same as min(), start().
Definition IntRangeI.H:61
constexpr IntRange() noexcept
Default construct an empty range (0,0).
Definition IntRangeI.H:24
IntType end_value() const noexcept
The value 1 beyond the end of the range.
Definition IntRangeI.H:68
IntType operator-=(IntType n) noexcept
Decrease the size by n, but never below 0.
Definition IntRangeI.H:207
void setSize(IntType n) noexcept
Change the size, no checks. Identical to resize().
Definition IntRangeI.H:122
const direction noexcept
Definition scalarImpl.H:265