labelRange.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 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 Class
28  Foam::labelRange
29 
30 Description
31  A range or interval of labels defined by a start and a size.
32 
33 SourceFiles
34  labelRange.C
35  labelRangeI.H
36 
37 \*---------------------------------------------------------------------------*/
38 #ifndef labelRange_H
39 #define labelRange_H
40 
41 #include "label.H"
42 #include <iterator>
43 
44 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45 
46 namespace Foam
47 {
48 
49 // Forward Declarations
50 class Istream;
51 class Ostream;
52 template<class T> class List;
53 template<class T> class MinMax;
54 
55 /*---------------------------------------------------------------------------*\
56  Class labelRange Declaration
57 \*---------------------------------------------------------------------------*/
58 
59 class labelRange
60 {
61  // Private Data
62 
63  //- The start point for the interval
64  label start_;
65 
66  //- The length of the interval
67  label size_;
68 
69 
70 public:
71 
72  // STL type definitions
73 
74  //- Type of values the range contains
75  typedef label value_type;
76 
77  //- The type that can represent the size of the range
78  typedef label size_type;
79 
80  //- Forward iterator with const access
81  class const_iterator;
82 
83 
84  // Static Data Members
85 
86  //- Debugging
87  static int debug;
88 
89  //- An empty range with start=0, size=0.
90  static const labelRange null;
91 
92 
93  // Constructors
94 
95  //- An empty range with zero for start/size.
96  inline constexpr labelRange() noexcept;
97 
98  //- Construct a range from start/size, enforcing non-negative size.
99  inline labelRange(const label start, const label size) noexcept;
100 
101  //- Construct a range from start/size, enforcing non-negative size.
102  // Optionally adjust the start to avoid any negative indices.
103  inline labelRange
104  (
105  const label start,
106  const label size,
107  const bool adjustStart
108  ) noexcept;
109 
110  //- Construct from a min/max range, enforcing non-negative size.
111  //- Does not adjust the start.
112  // Passing an invalid min/max range results in an empty labelRange
113  explicit labelRange(const MinMax<label>& range) noexcept;
114 
115  //- Construct from Istream.
116  explicit labelRange(Istream& is);
117 
118 
119  // Static Member Functions
120 
121  //- An identity range corresponding to (map[i] == i), or with
122  //- an optional start index, so that (map[i] == i+start)
123  // The parameter order as per the Foam::identity() function.
124  inline static labelRange identity
125  (
126  const label len,
127  const label start=0
128  ) noexcept;
129 
130 
131  // Member Functions
132 
133  //- Change the start position
134  inline void setStart(const label i) noexcept;
135 
136  //- Change the size, enforcing non-negative size.
137  inline void resize(const label n) noexcept;
138 
139  //- Change the size - alias for resize()
140  inline void setSize(const label n) noexcept;
141 
142  //- Reset to zero start and zero size
143  inline void clear() noexcept;
144 
145  //- Is the range empty?
146  inline bool empty() const noexcept;
147 
148  //- Is the range non-empty?
149  inline bool valid() const noexcept;
150 
151  //- The (inclusive) lower value of the range
152  inline label start() const noexcept;
153 
154  //- The effective size of the range
155  inline label size() const noexcept;
156 
157  //- The value before the start of the range
158  inline label before() const noexcept;
159 
160  //- The (inclusive) lower value of the range - same as start()
161  inline label first() const noexcept;
162 
163  //- The (inclusive) lower value of the range - same as start(), first()
164  inline label min() const noexcept;
165 
166  //- The (inclusive) upper value of the range - same as max()
167  inline label last() const noexcept;
168 
169  //- The (inclusive) upper value of the range - same as last()
170  inline label max() const noexcept;
171 
172  //- The value after the last element in the range
173  // This is identical to the value of cend()
174  inline label after() const noexcept;
175 
176  //- Adjust the start to avoid negative indices.
177  // The size is decreased accordingly, but will never become negative.
178  // Eg, adjusting (-10, 15) becomes (0,5).
179  // adjusting (-20, 15) becomes (0,0)
180  void adjust() noexcept;
181 
182  //- Reset start and size, enforcing non-negative size.
183  // \return true if the updated range is valid (non-empty).
184  inline bool reset(const label start, const label size) noexcept;
185 
186  //- Reset start and size, enforcing non-negative size.
187  // Optionally adjust the start to avoid any negative indices.
188  // \return true if the updated range is valid (non-empty).
189  inline bool reset
190  (
191  const label start,
192  const label size,
193  const bool adjustStart
194  ) noexcept;
195 
196  //- Return true if the (global) value is located within the range
197  inline bool found(const label value) const noexcept;
198 
199  //- Return the range as a list of labels
200  List<label> labels() const;
201 
202  //- Return true if the ranges overlap.
203  // Optional test for ranges that also just touch each other
204  bool overlaps(const labelRange& range, bool touches=false) const;
205 
206  //- Return a joined range, squashing any gaps in between
207  // A prior overlaps() check can be used to avoid squashing gaps.
208  labelRange join(const labelRange& range) const;
209 
210  //- Calculate the intersection of the range with another.
211  // If there is no intersection, it returns an empty range with zero
212  // for start/size.
213  labelRange subset(const labelRange& range) const;
214 
215  //- Calculate the intersection with the given start/size range.
216  // If there is no intersection, it returns an empty range with zero
217  // for start/size.
218  labelRange subset(const label start, const label size) const;
219 
220  //- Calculate the intersection with the given 0/size range.
221  // If there is no intersection, it returns an empty range with zero
222  // for start/size.
223  labelRange subset0(const label size) const;
224 
225 
226  // Member Operators
227 
228  //- Return element in the range, without bounds checking
229  inline label operator[](const label i) const noexcept;
230 
231  //- Return true if the global value is located within the range.
232  // Behaviour identical to found() - usable as a predicate
233  inline bool operator()(const label value) const noexcept;
234 
235  //- Increase the size by 1.
236  inline label operator++() noexcept;
237  inline label operator++(int) noexcept;
238 
239  //- Increase the size by n.
240  inline label operator+=(const label n) noexcept;
241 
242  //- Decrease the size by 1, but never below 0.
243  inline label operator--() noexcept;
244  inline label operator--(int) noexcept;
245 
246  //- Decrease the size by n, but never below 0.
247  inline label operator-=(const label n) noexcept;
248 
249 
250  // STL iterator
251 
252  //- Forward iterator with const access
253  class const_iterator
254  :
255  public std::iterator
256  <
257  std::input_iterator_tag,
258  label,
259  label,
260  const label*,
261  const label&
262  >
263  {
264  //- The global value
265  label value_;
266 
267  public:
268 
269  // Constructors
270 
271  //- Construct from range at given local index.
272  // An out-of-range index (eg, negative) creates an 'end' iterator
273  inline const_iterator(const labelRange* range, const label i=0);
274 
275  // Member Operators
276 
277  //- Return the (global) value
278  inline label operator*() const noexcept;
279 
280  //- Prefix increment, no range checking
281  inline const_iterator& operator++() noexcept;
282 
283  //- Postfix increment, no range checking
284  inline const_iterator operator++(int);
285 
286  //- Arbitrary increment, no range checking
287  inline const_iterator& operator+=(const label n) noexcept;
288 
289  //- Arbitrary decrement, no range checking
290  inline const_iterator& operator-=(const label n) noexcept;
291 
292  //- Test for equality of values
293  inline bool operator==(const const_iterator& iter) const noexcept;
294 
295  //- Test for inequality of values
296  inline bool operator!=(const const_iterator& iter) const noexcept;
297  };
298 
299 
300  //- A const_iterator set to the beginning of the range
301  // The value returned is guaranteed to be the same as start()
302  inline const_iterator begin() const;
303 
304  //- A const_iterator set to the beginning of the range
305  // The value returned is guaranteed to be the same as start()
306  inline const_iterator cbegin() const;
307 
308  //- A const_iterator set to 1 beyond the end of the range.
309  // The value returned is the same as after()
310  inline const const_iterator cend() const;
311 
312  //- A const_iterator set to 1 beyond the end of the range.
313  // The value returned is the same as after()
314  inline const const_iterator end() const;
315 
316  //- Return const_iterator to a position within the range,
317  //- with bounds checking.
318  // \return iterator at the requested position, or end() for
319  // out of bounds
320  inline const_iterator at(const label i) const;
321 };
322 
323 
324 // Global Functions
325 
326 //- Conversion/extraction to labelRange operation (functor).
327 // Specializations shall provide a corresponding \c operator().
328 // For example,
329 // \code
330 // template<>
331 // struct labelRangeOp<polyPatch>
332 // {
333 // labelRange operator()(const polyPatch& pp) const
334 // {
335 // return labelRange(pp.start(), pp.size());
336 // }
337 // };
338 // \endcode
339 template<class> struct labelRangeOp;
340 
341 
342 // IOstream Operators
343 
344 //- Read labelRange from Istream as (start size) pair, enforce no negative size
346 
347 //- Write labelRange to Ostream as (start size) pair
349 
350 
351 // Global Operators
352 
353 inline bool operator==(const labelRange& a, const labelRange& b) noexcept
354 {
355  return (a.first() == b.first() && a.size() == b.size());
356 }
357 
358 inline bool operator!=(const labelRange& a, const labelRange& b) noexcept
359 {
360  return !(a == b);
361 }
362 
363 
364 //- Comparison function for sorting, compares the start.
365 // If the start values are equal, also compares the size.
366 inline bool operator<(const labelRange& a, const labelRange& b) noexcept
367 {
368  return
369  (
370  a.first() < b.first()
371  ||
372  (
373  !(b.first() < a.first())
374  && a.size() < b.size()
375  )
376  );
377 }
378 
379 inline bool operator<=(const labelRange& a, const labelRange& b) noexcept
380 {
381  return !(b < a);
382 }
383 
384 
385 inline bool operator>(const labelRange& a, const labelRange& b) noexcept
386 {
387  return (b < a);
388 }
389 
390 inline bool operator>=(const labelRange& a, const labelRange& b) noexcept
391 {
392  return !(a < b);
393 }
394 
395 
396 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
397 
398 } // End namespace Foam
399 
400 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
401 
402 #include "labelRangeI.H"
403 
404 #endif
405 
406 // ************************************************************************* //
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::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::join
labelRange join(const labelRange &range) const
Return a joined range, squashing any gaps in between.
Definition: labelRange.C:125
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::adjust
void adjust() noexcept
Adjust the start to avoid negative indices.
Definition: labelRange.C:85
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:228
Foam::labelRange::size
label size() const noexcept
The effective size of the range.
Definition: labelRangeI.H:226
Foam::labelRange::clear
void clear() noexcept
Reset to zero start and zero size.
Definition: labelRangeI.H:208
Foam::labelRange::overlaps
bool overlaps(const labelRange &range, bool touches=false) const
Return true if the ranges overlap.
Definition: labelRange.C:102
labelRangeI.H
Foam::labelRange::after
label after() const noexcept
The value after the last element in the range.
Definition: labelRangeI.H:268
Foam::labelRange::value_type
label value_type
Type of values the range contains.
Definition: labelRange.H:74
Foam::operator!=
bool operator!=(const eddy &a, const eddy &b)
Definition: eddy.H:235
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::constant::physicoChemical::b
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
Foam::operator==
tmp< faMatrix< Type > > operator==(const faMatrix< Type > &, const faMatrix< Type > &)
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
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::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::subset
labelRange subset(const labelRange &range) const
Calculate the intersection of the range with another.
Definition: labelRange.C:147
Foam::labelRange::labels
List< label > labels() const
Return the range as a list of labels.
Definition: labelRange.C:71
Foam::labelRange::resize
void resize(const label n) noexcept
Change the size, enforcing non-negative size.
Definition: labelRangeI.H:194
Foam::labelRangeOp
Conversion/extraction to labelRange operation (functor).
Definition: labelRange.H:338
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
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::labelRange::debug
static int debug
Debugging.
Definition: labelRange.H:80
range
scalar range
Definition: LISASMDCalcMethod1.H:12
Foam::operator<
bool operator<(const Instant< T1 > &a, const Instant< T2 > &b)
Compare instant values for less-than.
Definition: Instant.C:90
Foam::labelRange::last
label last() const noexcept
The (inclusive) upper value of the range - same as max()
Definition: labelRangeI.H:256
label.H
Foam::labelRange::empty
bool empty() const noexcept
Is the range empty?
Definition: labelRangeI.H:214
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:102
Foam::labelRange::labelRange
constexpr labelRange() noexcept
An empty range with zero for start/size.
Definition: labelRangeI.H:31
Foam::operator*
tmp< faMatrix< Type > > operator*(const areaScalarField &, const faMatrix< Type > &)
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::subset0
labelRange subset0(const label size) const
Calculate the intersection with the given 0/size range.
Definition: labelRange.C:185
Foam::labelRange::identity
static labelRange identity(const label len, const label start=0) noexcept
Definition: labelRangeI.H:179
Foam::operator>
bool operator>(const Instant< T1 > &a, const Instant< T2 > &b)
Compare instant values for greater-than.
Definition: Instant.C:97
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::size_type
label size_type
The type that can represent the size of the range.
Definition: labelRange.H:77
Foam::labelRange::operator-=
label operator-=(const label n) noexcept
Decrease the size by n, but never below 0.
Definition: labelRangeI.H:368
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::labelRange::valid
bool valid() const noexcept
Is the range non-empty?
Definition: labelRangeI.H:220
Foam::operator<=
bool operator<=(const Pair< T > &a, const Pair< T > &b)
Definition: Pair.H:249
Foam::operator>=
bool operator>=(const Pair< T > &a, const Pair< T > &b)
Definition: Pair.H:263
Foam::MinMax
A min/max value pair with additional methods. In addition to conveniently storing values,...
Definition: HashSet.H:76
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &)
Definition: boundaryPatch.C:102