Loading...
Searching...
No Matches
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-2025 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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
27Class
28 Foam::labelRange
29
30Description
31 A range or interval of labels defined by a start and a size.
32
33SourceFiles
34 labelRange.cxx
35 labelRangeI.H
36
37\*---------------------------------------------------------------------------*/
38
39#ifndef Foam_labelRange_H
40#define Foam_labelRange_H
41
42#include "IntRange.H"
43
44// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45
46namespace Foam
47{
48
49// Forward Declarations
50class labelRange;
51template<class T> class MinMax;
52template<class T> class Pair;
53
54//- Read from Istream as bracketed (start size) tuple
56
57//- Write to Ostream as bracketed (start size) tuple
59
60/*---------------------------------------------------------------------------*\
61 Class labelRange Declaration
62\*---------------------------------------------------------------------------*/
64class labelRange
65:
66 public IntRange<label>
67{
68public:
69
70 // STL type definitions
71
72 //- Type of values the range contains
73 typedef label value_type;
75 //- The type that can represent the size of the range
76 typedef label size_type;
77
78 //- Input iterator with const access
80
81 //- Reverse input iterator with const access
83
84
85 // Static Data Members
86
87 //- Debugging
88 static int debug;
89
90
91 // Generated Methods
92
93 //- Copy construct
94 labelRange(const labelRange&) noexcept = default;
95
96 //- Move construct
98
99 //- Copy assignment
100 labelRange& operator=(const labelRange&) noexcept = default;
101
102 //- Move assignment
103 labelRange& operator=(labelRange&&) noexcept = default;
104
106 // Constructors
107
108 //- Default construct an empty range (0,0)
109 inline constexpr labelRange() noexcept;
111 //- Construct a range with specified length, starting at zero (0,len)
112 inline explicit constexpr labelRange(label len) noexcept;
113
114 //- Construct a range from start/length. Applies clamp on length
115 inline labelRange(label beg, label len) noexcept;
116
117 //- Construct a range from start/size, enforces non-negative size.
118 // Optionally adjust the start to avoid any negative indices.
119 // \see adjust()
120 inline labelRange(label beg, label len, bool adjustStart) noexcept;
121
122 //- Construct from a min/max range (both inclusive),
123 //- enforces non-negative size.
124 // Passing an invalid range results in an empty labelRange
125 explicit labelRange(const MinMax<label>& range) noexcept;
126
127 //- Construct from start (inclusive) and end (exclusive) values,
128 //- enforces non-negative size.
129 // Passing an invalid range results in an empty labelRange
130 explicit labelRange(const Pair<label>& start_end) noexcept;
131
132 //- Implicit construct from an IntRange with the same or lower
133 //- representation (no checks for signed vs unsigned).
134 template
135 <
136 class IntT,
137 class = std::enable_if_t
138 <std::is_integral_v<IntT> && (sizeof(IntT) <= sizeof(value_type))>
139 >
140 labelRange(const IntRange<IntT>& rhs) noexcept
141 :
142 IntRange<label>(rhs.start(), rhs.size())
143 {}
144
145 //- Construct from Istream. Read a bracketed (start size) tuple
146 explicit labelRange(Istream& is);
147
148
149 // Member Functions
150
151 // Edit
152
153 //- Adjust the start to avoid negative indices.
154 // The size is decreased accordingly, but will never become negative.
155 // Eg, adjusting (-10, 15) becomes (0,5).
156 // adjusting (-20, 15) becomes (0,0)
157 void adjust() noexcept;
158
159 //- Reset start and length, no checks
160 using IntRange<label>::reset;
161
162 //- Reset start and length, enforces non-negative size.
163 // Optionally adjust the start to avoid any negative indices.
164 inline void reset(label beg, label len, bool adjustStart) noexcept;
165
166
167 // Other
168
169 //- Return true if the ranges overlap.
170 // Optional test for ranges that also just touch each other
171 bool overlaps(const labelRange& range, bool touches=false) const;
172
173 //- Return a joined range, squashing any gaps in between
174 // A prior overlaps() check can be used to avoid squashing gaps.
175 labelRange join(const labelRange& range) const;
176
177 //- Calculate the intersection of the range with another.
178 // If there is no intersection, it returns an empty range with zero
179 // for start/size.
180 labelRange subset(const labelRange& range) const;
181
182 //- Calculate the intersection with the given start/size range.
183 // If there is no intersection, it returns an empty range with zero
184 // for start/size.
185 labelRange subset(label start, label size) const;
186
187 //- Calculate the intersection with the given 0/size range.
188 // If there is no intersection, it returns an empty range with zero
189 // for start/size.
190 inline labelRange subset0(label len) const;
191
192
193 // Operators
194
195 //- Assign from an IntRange with the same or lower
196 //- representation (no checks for signed vs unsigned).
197 template
198 <
199 class IntT,
200 class = std::enable_if_t
201 <std::is_integral_v<IntT> && (sizeof(IntT) <= sizeof(value_type))>
202 >
203 void operator=(const IntRange<IntT>& rhs) noexcept
204 {
205 IntRange<label>::reset(rhs.start(), rhs.size());
206 }
207
208
209 // Housekeeping
210
211 //- Deprecated(2020-09) True if range has size greater than zero
212 //
213 // \deprecated(2020-09) - use bool operator - or good()
214 bool valid() const noexcept { return good(); }
215};
216
217
218// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
219
220//- Conversion/extraction to labelRange operation (functor).
221// Specializations shall provide a corresponding \c operator().
222// For example,
223// \code
224// template<>
225// struct labelRangeOp<polyPatch>
226// {
227// labelRange operator()(const polyPatch& pp) const
228// {
229// return labelRange(pp.start(), pp.size());
230// }
231// };
232// \endcode
233template<class> struct labelRangeOp;
234
235
236// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
237
238} // End namespace Foam
239
240// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
242#include "labelRangeI.H"
243
244#endif
245
246// ************************************************************************* //
scalar range
void reset() noexcept
Reset to (0,0).
Definition IntRangeI.H:99
IndexIterator< label > const_iterator
Definition IntRange.H:111
label start() const noexcept
Definition IntRange.H:218
bool good() const noexcept
Definition IntRange.H:203
label size() const noexcept
Definition IntRange.H:208
ReverseIndexIterator< label > const_reverse_iterator
Definition IntRange.H:116
constexpr IntRange() noexcept
Definition IntRangeI.H:24
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
A min/max value pair with additional methods. In addition to conveniently storing values,...
Definition MinMax.H:106
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
An ordered pair of two objects of type <T> with first() and second() elements.
Definition Pair.H:66
A range or interval of labels defined by a start and a size.
Definition labelRange.H:66
label size_type
The type that can represent the size of the range.
Definition labelRange.H:79
constexpr labelRange() noexcept
Default construct an empty range (0,0).
Definition labelRangeI.H:24
void adjust() noexcept
Adjust the start to avoid negative indices.
labelRange(Istream &is)
Construct from Istream. Read a bracketed (start size) tuple.
labelRange(const labelRange &) noexcept=default
Copy construct.
bool overlaps(const labelRange &range, bool touches=false) const
Return true if the ranges overlap.
labelRange subset0(label len) const
Calculate the intersection with the given 0/size range.
Definition labelRangeI.H:87
bool valid() const noexcept
Deprecated(2020-09) True if range has size greater than zero.
Definition labelRange.H:278
labelRange(labelRange &&) noexcept=default
Move construct.
label value_type
Type of values the range contains.
Definition labelRange.H:74
void reset(label beg, label len, bool adjustStart) noexcept
Reset start and length, enforces non-negative size.
Definition labelRangeI.H:67
labelRange join(const labelRange &range) const
Return a joined range, squashing any gaps in between.
static int debug
Debugging.
Definition labelRange.H:97
labelRange subset(const labelRange &range) const
Calculate the intersection of the range with another.
Namespace for OpenFOAM.
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces).
Istream & operator>>(Istream &, directionInfo &)
void rhs(fvMatrix< typename Expr::value_type > &m, const Expr &expression)
const direction noexcept
Definition scalarImpl.H:265
Conversion/extraction to labelRange operation (functor).
Definition labelRange.H:300