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-2022 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 Foam_labelRange_H
40 #define Foam_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  // Edit
113 
114  //- Adjust the start to avoid negative indices.
115  // The size is decreased accordingly, but will never become negative.
116  // Eg, adjusting (-10, 15) becomes (0,5).
117  // adjusting (-20, 15) becomes (0,0)
118  void adjust() noexcept;
119 
120  //- Reset start and length, no checks
121  using IntRange<label>::reset;
122 
123  //- Reset start and length, enforces non-negative size.
124  // Optionally adjust the start to avoid any negative indices.
125  inline void reset
126  (
127  const label beg,
128  const label end,
129  const bool adjustStart
130  ) noexcept;
131 
132 
133  // Other
134 
135  //- Return list of labels corresponding to the range.
136  // Same as Foam::identity()
137  List<label> labels() const;
138 
139  //- Return true if the ranges overlap.
140  // Optional test for ranges that also just touch each other
141  bool overlaps(const labelRange& range, bool touches=false) const;
142 
143  //- Return a joined range, squashing any gaps in between
144  // A prior overlaps() check can be used to avoid squashing gaps.
145  labelRange join(const labelRange& range) const;
146 
147  //- Calculate the intersection of the range with another.
148  // If there is no intersection, it returns an empty range with zero
149  // for start/size.
150  labelRange subset(const labelRange& range) const;
151 
152  //- Calculate the intersection with the given start/size range.
153  // If there is no intersection, it returns an empty range with zero
154  // for start/size.
155  labelRange subset(const label start, const label size) const;
156 
157  //- Calculate the intersection with the given 0/size range.
158  // If there is no intersection, it returns an empty range with zero
159  // for start/size.
160  labelRange subset0(const label size) const;
161 
162 
163  // Housekeeping
164 
165  //- Deprecated(2020-09) True if range is non-empty
166  //
167  // \deprecated(2020-09) - use bool operator
168  bool valid() const noexcept { return !empty(); }
169 };
170 
171 
172 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
173 
174 //- Conversion/extraction to labelRange operation (functor).
175 // Specializations shall provide a corresponding \c operator().
176 // For example,
177 // \code
178 // template<>
179 // struct labelRangeOp<polyPatch>
180 // {
181 // labelRange operator()(const polyPatch& pp) const
182 // {
183 // return labelRange(pp.start(), pp.size());
184 // }
185 // };
186 // \endcode
187 template<class> struct labelRangeOp;
188 
189 
190 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
191 
192 } // End namespace Foam
193 
194 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
195 
196 #include "labelRangeI.H"
197 
198 #endif
199 
200 // ************************************************************************* //
void reset(const label beg, const label end, const bool adjustStart) noexcept
Reset start and length, enforces non-negative size.
Definition: labelRangeI.H:71
constexpr labelRange() noexcept
Default construct an empty range (0,0)
Definition: labelRangeI.H:24
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:56
void adjust() noexcept
Adjust the start to avoid negative indices.
Definition: labelRange.C:74
An interval of (signed) integers defined by a start and a size.
Definition: IntRange.H:59
A range or interval of labels defined by a start and a size.
Definition: labelRange.H:51
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
A min/max value pair with additional methods. In addition to conveniently storing values...
Definition: HashSet.H:72
static int debug
Debugging.
Definition: labelRange.H:75
scalar range
List< label > labels() const
Return list of labels corresponding to the range.
Definition: labelRange.C:59
labelRange subset0(const label size) const
Calculate the intersection with the given 0/size range.
Definition: labelRange.C:175
labelRange subset(const labelRange &range) const
Calculate the intersection of the range with another.
Definition: labelRange.C:137
label start() const noexcept
The (inclusive) lower value of the range.
Definition: IntRange.H:199
const direction noexcept
Definition: Scalar.H:258
labelRange join(const labelRange &range) const
Return a joined range, squashing any gaps in between.
Definition: labelRange.C:112
const_iterator end() const noexcept
A const_iterator set to 1 beyond the end of the range.
Definition: IntRangeI.H:348
bool valid() const noexcept
Deprecated(2020-09) True if range is non-empty.
Definition: labelRange.H:212
bool empty() const noexcept
True if range is empty (zero-sized)
Definition: IntRange.H:184
label size() const noexcept
The size of the range.
Definition: IntRange.H:189
bool overlaps(const labelRange &range, bool touches=false) const
Return true if the ranges overlap.
Definition: labelRange.C:89
Namespace for OpenFOAM.