labelRangeI.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) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2017-2019 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
30 
31 inline constexpr Foam::labelRange::labelRange() noexcept
32 :
33  start_(0),
34  size_(0)
35 {}
36 
37 
39 (
40  const label start,
41  const label size
42 ) noexcept
43 :
44  start_(start),
45  size_(size)
46 {
47  if (size_ < 0) size_ = 0; // No negative sizes
48 }
49 
50 
52 (
53  const label start,
54  const label size,
55  const bool adjustStart
56 ) noexcept
57 :
58  start_(start),
59  size_(size)
60 {
61  if (adjustStart)
62  {
63  adjust(); // Eliminate negative start, adjust size accordingly
64  }
65  else if (size_ < 0)
66  {
67  size_ = 0; // No negative sizes
68  }
69 }
70 
71 
72 // * * * * * * * * * * * * * * * * Iterators * * * * * * * * * * * * * * * * //
73 
75 (
76  const labelRange* range,
77  const label i
78 )
79 :
80  value_
81  (
82  range->start()
83  + ((i < 0 || i > range->size()) ? range->size() : i)
84  )
85 {}
86 
87 
89 {
90  return value_;
91 }
92 
93 
96 {
97  ++value_;
98  return *this;
99 }
100 
101 
104 {
105  const_iterator old(*this);
106  ++value_;
107  return old;
108 }
109 
110 
113 {
114  value_ += n;
115  return *this;
116 }
117 
118 
121 {
122  value_ -= n;
123  return *this;
124 }
125 
126 
127 inline bool Foam::labelRange::const_iterator::operator==
128 (
129  const const_iterator& iter
130 ) const noexcept
131 {
132  return (value_ == iter.value_);
133 }
134 
135 
136 inline bool Foam::labelRange::const_iterator::operator!=
137 (
138  const const_iterator& iter
139 ) const noexcept
140 {
141  return (value_ != iter.value_);
142 }
143 
144 
146 {
147  return const_iterator(this, 0);
148 }
149 
150 
152 {
153  return const_iterator(this, 0);
154 }
155 
156 
158 {
159  return const_iterator(this, -1);
160 }
161 
162 
164 {
165  return const_iterator(this, -1);
166 }
167 
168 
171 {
172  return const_iterator(this, i);
173 }
174 
175 
176 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
177 
179 (
180  const label len,
181  const label start
182 ) noexcept
183 {
184  return labelRange(start, len);
185 }
186 
187 
188 inline void Foam::labelRange::setStart(const label i) noexcept
189 {
190  start_ = i;
191 }
192 
193 
194 inline void Foam::labelRange::resize(const label n) noexcept
195 {
196  size_ = n;
197  if (size_ < 0) size_ = 0; // No negative sizes
198 }
199 
200 
201 inline void Foam::labelRange::setSize(const label n) noexcept
202 {
203  size_ = n;
204  if (size_ < 0) size_ = 0; // No negative sizes
205 }
206 
207 
208 inline void Foam::labelRange::clear() noexcept
209 {
210  start_ = size_ = 0;
211 }
212 
213 
214 inline bool Foam::labelRange::empty() const noexcept
215 {
216  return !size_;
217 }
218 
219 
220 inline bool Foam::labelRange::valid() const noexcept
221 {
222  return size_;
223 }
224 
225 
226 inline Foam::label Foam::labelRange::size() const noexcept
227 {
228  return size_;
229 }
230 
231 
232 inline Foam::label Foam::labelRange::start() const noexcept
233 {
234  return start_;
235 }
236 
237 
238 inline Foam::label Foam::labelRange::before() const noexcept
239 {
240  return start_ - 1;
241 }
242 
243 
244 inline Foam::label Foam::labelRange::first() const noexcept
245 {
246  return start_;
247 }
248 
249 
250 inline Foam::label Foam::labelRange::min() const noexcept
251 {
252  return start_;
253 }
254 
255 
256 inline Foam::label Foam::labelRange::last() const noexcept
257 {
258  return start_ + size_ - 1;
259 }
260 
261 
262 inline Foam::label Foam::labelRange::max() const noexcept
263 {
264  return start_ + size_ - 1;
265 }
266 
267 
268 inline Foam::label Foam::labelRange::after() const noexcept
269 {
270  return start_ + size_;
271 }
272 
273 
274 inline bool Foam::labelRange::reset
275 (
276  const label start,
277  const label size
278 ) noexcept
279 {
280  start_ = start;
281  size_ = size;
282 
283  if (size_ < 0) size_ = 0; // No negative sizes
284 
285  return size_;
286 }
287 
288 
289 inline bool Foam::labelRange::reset
290 (
291  const label start,
292  const label size,
293  const bool adjustStart
294 ) noexcept
295 {
296  start_ = start;
297  size_ = size;
298 
299  if (adjustStart)
300  {
301  adjust(); // Eliminate negative start, adjust size accordingly
302  }
303  else if (size_ < 0)
304  {
305  size_ = 0; // No negative sizes
306  }
307 
308  return size_;
309 }
310 
311 
312 inline bool Foam::labelRange::found(const label value) const noexcept
313 {
314  return (size_ && start_ <= value && (value - start_) < size_);
315 }
316 
317 
318 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
319 
320 inline Foam::label Foam::labelRange::operator[](const label i) const noexcept
321 {
322  return start_ + i;
323 }
324 
325 
326 inline bool Foam::labelRange::operator()(const label value) const noexcept
327 {
328  return found(value);
329 }
330 
331 
333 {
334  return ++size_;
335 }
336 
337 
339 {
340  return size_++;
341 }
342 
343 
345 {
346  --size_;
347  if (size_ < 0) size_ = 0; // No negative sizes
348  return size_;
349 }
350 
351 
353 {
354  const label old(size_);
355  --size_;
356  if (size_ < 0) size_ = 0; // No negative sizes
357  return old;
358 }
359 
360 
362 {
363  size_ += n;
364  return size_;
365 }
366 
367 
369 {
370  size_ -= n;
371  if (size_ < 0) size_ = 0; // No negative sizes
372  return size_;
373 }
374 
375 
376 
377 // ************************************************************************* //
Foam::labelRange::setSize
void setSize(const label n) noexcept
Change the size - alias for resize()
Definition: labelRangeI.H:201
Foam::labelRange::reset
bool reset(const label start, const label size) noexcept
Reset start and size, enforcing non-negative size.
Definition: labelRangeI.H:275
Foam::labelRange::const_iterator::operator*
label operator*() const noexcept
Return the (global) value.
Definition: labelRangeI.H:88
Foam::labelRange::cbegin
const_iterator cbegin() const
A const_iterator set to the beginning of the range.
Definition: labelRangeI.H:151
Foam::labelRange::before
label before() const noexcept
The value before the start of the range.
Definition: labelRangeI.H:238
Foam::labelRange::end
const const_iterator end() const
A const_iterator set to 1 beyond the end of the range.
Definition: labelRangeI.H:157
Foam::labelRange::operator++
label operator++() noexcept
Increase the size by 1.
Definition: labelRangeI.H:332
Foam::labelRange::cend
const const_iterator cend() const
A const_iterator set to 1 beyond the end of the range.
Definition: labelRangeI.H:163
Foam::labelRange::size
label size() const noexcept
The effective size of the range.
Definition: labelRangeI.H:226
Foam::labelRange::const_iterator::operator+=
const_iterator & operator+=(const label n) noexcept
Arbitrary increment, no range checking.
Definition: labelRangeI.H:112
Foam::labelRange::clear
void clear() noexcept
Reset to zero start and zero size.
Definition: labelRangeI.H:208
Foam::labelRange::after
label after() const noexcept
The value after the last element in the range.
Definition: labelRangeI.H:268
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
Foam::labelRange::found
bool found(const label value) const noexcept
Return true if the (global) value is located within the range.
Definition: labelRangeI.H:312
Foam::labelRange::const_iterator::operator++
const_iterator & operator++() noexcept
Prefix increment, no range checking.
Definition: labelRangeI.H:95
Foam::labelRange::max
label max() const noexcept
The (inclusive) upper value of the range - same as last()
Definition: labelRangeI.H:262
Foam::labelRange
A range or interval of labels defined by a start and a size.
Definition: labelRange.H:58
Foam::labelRange::at
const_iterator at(const label i) const
Definition: labelRangeI.H:170
Foam::labelRange::resize
void resize(const label n) noexcept
Change the size, enforcing non-negative size.
Definition: labelRangeI.H:194
Foam::labelRange::operator--
label operator--() noexcept
Decrease the size by 1, but never below 0.
Definition: labelRangeI.H:344
Foam::labelRange::first
label first() const noexcept
The (inclusive) lower value of the range - same as start()
Definition: labelRangeI.H:244
Foam::labelRange::const_iterator
Forward iterator with const access.
Definition: labelRange.H:252
found
bool found
Definition: TABSMDCalcMethod2.H:32
Foam::labelRange::const_iterator::const_iterator
const_iterator(const labelRange *range, const label i=0)
Construct from range at given local index.
Definition: labelRangeI.H:75
Foam::labelRange::operator()
bool operator()(const label value) const noexcept
Return true if the global value is located within the range.
Definition: labelRangeI.H:326
range
scalar range
Definition: LISASMDCalcMethod1.H:12
Foam::labelRange::last
label last() const noexcept
The (inclusive) upper value of the range - same as max()
Definition: labelRangeI.H:256
Foam::labelRange::empty
bool empty() const noexcept
Is the range empty?
Definition: labelRangeI.H:214
Foam::labelRange::labelRange
constexpr labelRange() noexcept
An empty range with zero for start/size.
Definition: labelRangeI.H:31
Foam::start
label ListType::const_reference const label start
Definition: ListOps.H:408
Foam::labelRange::operator+=
label operator+=(const label n) noexcept
Increase the size by n.
Definition: labelRangeI.H:361
Foam::labelRange::start
label start() const noexcept
The (inclusive) lower value of the range.
Definition: labelRangeI.H:232
Foam::labelRange::identity
static labelRange identity(const label len, const label start=0) noexcept
Definition: labelRangeI.H:179
Foam::labelRange::setStart
void setStart(const label i) noexcept
Change the start position.
Definition: labelRangeI.H:188
Foam::labelRange::begin
const_iterator begin() const
A const_iterator set to the beginning of the range.
Definition: labelRangeI.H:145
Foam::labelRange::min
label min() const noexcept
The (inclusive) lower value of the range - same as start(), first()
Definition: labelRangeI.H:250
Foam::labelRange::operator-=
label operator-=(const label n) noexcept
Decrease the size by n, but never below 0.
Definition: labelRangeI.H:368
Foam::labelRange::valid
bool valid() const noexcept
Is the range non-empty?
Definition: labelRangeI.H:220
Foam::labelRange::operator[]
label operator[](const label i) const noexcept
Return element in the range, without bounds checking.
Definition: labelRangeI.H:320
Foam::labelRange::const_iterator::operator-=
const_iterator & operator-=(const label n) noexcept
Arbitrary decrement, no range checking.
Definition: labelRangeI.H:120