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-2020 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 
39 #ifndef labelRange_H
40 #define labelRange_H
41 
42 #include "IntRange.H"
43 
44 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45 
46 namespace Foam
47 {
48 
49 // Forward Declarations
50 template<class T> class MinMax;
51 
52 /*---------------------------------------------------------------------------*\
53  Class labelRange Declaration
54 \*---------------------------------------------------------------------------*/
55 
56 class labelRange
57 :
58  public IntRange<label>
59 {
60 public:
61 
62  // STL type definitions
63 
64  //- Input iterator with const access
66 
67  //- Reverse input iterator with const access
69 
70 
71  // Static Data Members
72 
73  //- Debugging
74  static int debug;
75 
76 
77  // Generated Methods: copy/move construct, copy/move assignment
78 
79 
80  // Constructors
81 
82  //- Default construct an empty range (0,0)
83  inline constexpr labelRange() noexcept;
84 
85  //- Construct a range with specified length, starting at zero (0,len)
86  inline explicit constexpr labelRange(const label len) noexcept;
87 
88  //- Construct a range from start/length, no checks
89  inline labelRange(const label beg, const label len) noexcept;
90 
91  //- Construct a range from start/size, enforces non-negative size.
92  // Optionally adjust the start to avoid any negative indices.
93  // \see adjust()
94  inline labelRange
95  (
96  const label beg,
97  const label len,
98  const bool adjustStart
99  ) noexcept;
100 
101  //- Construct from a min/max range, enforces non-negative size.
102  //- Does not adjust the start.
103  // Passing an invalid min/max range results in an empty labelRange
104  explicit labelRange(const MinMax<label>& range) noexcept;
105 
106  //- Construct from Istream.
107  explicit labelRange(Istream& is);
108 
109 
110  // Member Functions
111 
112  // Access
113 
114  //- The (inclusive) lower value of the range - same as first(), start()
115  inline label min() const noexcept;
116 
117  //- The (inclusive) upper value of the range - same as last()
118  inline label max() const noexcept;
119 
120  //- The value before the start of the range
121  inline label before() const noexcept;
122 
123  //- The value after the last element in the range
124  // This is identical to the value of cend()
125  inline label after() const noexcept;
126 
127  //- Adjust the start to avoid negative indices.
128  // The size is decreased accordingly, but will never become negative.
129  // Eg, adjusting (-10, 15) becomes (0,5).
130  // adjusting (-20, 15) becomes (0,0)
131  void adjust() noexcept;
132 
133 
134  // Edit
135 
136  //- Reset start and length, no checks
137  using IntRange<label>::reset;
138 
139  //- Reset start and length, enforces non-negative size.
140  // Optionally adjust the start to avoid any negative indices.
141  inline void reset
142  (
143  const label beg,
144  const label end,
145  const bool adjustStart
146  ) noexcept;
147 
148 
149  // Other
150 
151  //- Return list of labels corresponding to the range.
152  // Same as Foam::identity()
153  List<label> labels() const;
154 
155  //- Return true if the ranges overlap.
156  // Optional test for ranges that also just touch each other
157  bool overlaps(const labelRange& range, bool touches=false) const;
158 
159  //- Return a joined range, squashing any gaps in between
160  // A prior overlaps() check can be used to avoid squashing gaps.
161  labelRange join(const labelRange& range) const;
162 
163  //- Calculate the intersection of the range with another.
164  // If there is no intersection, it returns an empty range with zero
165  // for start/size.
166  labelRange subset(const labelRange& range) const;
167 
168  //- Calculate the intersection with the given start/size range.
169  // If there is no intersection, it returns an empty range with zero
170  // for start/size.
171  labelRange subset(const label start, const label size) const;
172 
173  //- Calculate the intersection with the given 0/size range.
174  // If there is no intersection, it returns an empty range with zero
175  // for start/size.
176  labelRange subset0(const label size) const;
177 
178 
179  // Housekeeping
180 
181  //- Deprecated(2020-09) True if range is non-empty
182  //
183  // \deprecated(2020-09) - use bool operator
184  bool valid() const noexcept { return bool(size()); }
185 };
186 
187 
188 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
189 
190 //- Conversion/extraction to labelRange operation (functor).
191 // Specializations shall provide a corresponding \c operator().
192 // For example,
193 // \code
194 // template<>
195 // struct labelRangeOp<polyPatch>
196 // {
197 // labelRange operator()(const polyPatch& pp) const
198 // {
199 // return labelRange(pp.start(), pp.size());
200 // }
201 // };
202 // \endcode
203 template<class> struct labelRangeOp;
204 
205 
206 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
207 
208 } // End namespace Foam
209 
210 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
211 
212 #include "labelRangeI.H"
213 
214 #endif
215 
216 // ************************************************************************* //
Foam::labelRange::before
label before() const noexcept
The value before the start of the range.
Definition: labelRangeI.H:89
Foam::labelRange::join
labelRange join(const labelRange &range) const
Return a joined range, squashing any gaps in between.
Definition: labelRange.C:119
Foam::IntRange
An interval of (signed) integers defined by a start and a size.
Definition: IntRange.H:63
Foam::IntRange< label >::end
const_iterator end() const noexcept
A const_iterator set to 1 beyond the end of the range.
Definition: IntRangeI.H:359
Foam::labelRange::adjust
void adjust() noexcept
Adjust the start to avoid negative indices.
Definition: labelRange.C:81
Foam::labelRange::reset
void reset(const label beg, const label end, const bool adjustStart) noexcept
Reset start and length, enforces non-negative size.
Definition: labelRangeI.H:102
IntRange.H
Foam::labelRange::overlaps
bool overlaps(const labelRange &range, bool touches=false) const
Return true if the ranges overlap.
Definition: labelRange.C:96
labelRangeI.H
Foam::labelRange::after
label after() const noexcept
The value after the last element in the range.
Definition: labelRangeI.H:95
Foam::IntRange< label >::size
label size() const noexcept
The size of the range.
Definition: IntRangeI.H:415
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::labelRange::max
label max() const noexcept
The (inclusive) upper value of the range - same as last()
Definition: labelRangeI.H:83
Foam::labelRange
A range or interval of labels defined by a start and a size.
Definition: labelRange.H:55
Foam::labelRange::subset
labelRange subset(const labelRange &range) const
Calculate the intersection of the range with another.
Definition: labelRange.C:144
Foam::labelRange::labels
List< label > labels() const
Return list of labels corresponding to the range.
Definition: labelRange.C:66
Foam::labelRangeOp
Conversion/extraction to labelRange operation (functor).
Definition: labelRange.H:202
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::labelRange::debug
static int debug
Debugging.
Definition: labelRange.H:73
range
scalar range
Definition: LISASMDCalcMethod1.H:12
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:63
Foam::labelRange::labelRange
constexpr labelRange() noexcept
Default construct an empty range (0,0)
Definition: labelRangeI.H:31
Foam::labelRange::subset0
labelRange subset0(const label size) const
Calculate the intersection with the given 0/size range.
Definition: labelRange.C:182
bool
bool
Definition: EEqn.H:20
Foam::labelRange::min
label min() const noexcept
The (inclusive) lower value of the range - same as first(), start()
Definition: labelRangeI.H:77
Foam::labelRange::valid
bool valid() const noexcept
Deprecated(2020-09) True if range is non-empty.
Definition: labelRange.H:183
Foam::IntRange< label >::start
label start() const noexcept
The (inclusive) lower value of the range.
Definition: IntRangeI.H:408
Foam::MinMax
A min/max value pair with additional methods. In addition to conveniently storing values,...
Definition: HashSet.H:76