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-2023 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 template<class T> class Pair;
52 
53 /*---------------------------------------------------------------------------*\
54  Class labelRange Declaration
55 \*---------------------------------------------------------------------------*/
56 
57 class labelRange
58 :
59  public IntRange<label>
60 {
61 public:
62 
63  // STL type definitions
64 
65  //- Input iterator with const access
67 
68  //- Reverse input iterator with const access
70 
71 
72  // Static Data Members
73 
74  //- Debugging
75  static int debug;
76 
77 
78  // Generated Methods: copy/move construct, copy/move assignment
79 
80 
81  // Constructors
82 
83  //- Default construct an empty range (0,0)
84  inline constexpr labelRange() noexcept;
85 
86  //- Construct a range with specified length, starting at zero (0,len)
87  inline explicit constexpr labelRange(const label len) noexcept;
88 
89  //- Construct a range from start/length, no checks
90  inline labelRange(const label beg, const label len) noexcept;
91 
92  //- Construct a range from start/size, enforces non-negative size.
93  // Optionally adjust the start to avoid any negative indices.
94  // \see adjust()
95  inline labelRange
96  (
97  const label beg,
98  const label len,
99  const bool adjustStart
100  ) noexcept;
101 
102  //- Construct from a min/max range (both inclusive),
103  //- enforces non-negative size.
104  // Passing an invalid range results in an empty labelRange
105  explicit labelRange(const MinMax<label>& range) noexcept;
106 
107  //- Construct from start (inclusive) and end (exclusive) values,
108  //- enforces non-negative size.
109  // Passing an invalid range results in an empty labelRange
110  explicit labelRange(const Pair<label>& start_end) noexcept;
111 
112  //- Construct from Istream.
113  explicit labelRange(Istream& is);
114 
115 
116  // Member Functions
117 
118  // Edit
119 
120  //- Adjust the start to avoid negative indices.
121  // The size is decreased accordingly, but will never become negative.
122  // Eg, adjusting (-10, 15) becomes (0,5).
123  // adjusting (-20, 15) becomes (0,0)
124  void adjust() noexcept;
125 
126  //- Reset start and length, no checks
127  using IntRange<label>::reset;
128 
129  //- Reset start and length, enforces non-negative size.
130  // Optionally adjust the start to avoid any negative indices.
131  inline void reset
132  (
133  const label beg,
134  const label end,
135  const bool adjustStart
136  ) noexcept;
137 
138 
139  // Other
140 
141  //- Return list of labels corresponding to the range.
142  // Same as Foam::identity()
143  List<label> labels() const;
144 
145  //- Return true if the ranges overlap.
146  // Optional test for ranges that also just touch each other
147  bool overlaps(const labelRange& range, bool touches=false) const;
148 
149  //- Return a joined range, squashing any gaps in between
150  // A prior overlaps() check can be used to avoid squashing gaps.
151  labelRange join(const labelRange& range) const;
152 
153  //- Calculate the intersection of the range with another.
154  // If there is no intersection, it returns an empty range with zero
155  // for start/size.
156  labelRange subset(const labelRange& range) const;
157 
158  //- Calculate the intersection with the given start/size range.
159  // If there is no intersection, it returns an empty range with zero
160  // for start/size.
161  labelRange subset(const label start, const label size) const;
162 
163  //- Calculate the intersection with the given 0/size range.
164  // If there is no intersection, it returns an empty range with zero
165  // for start/size.
166  labelRange subset0(const label size) const;
167 
168 
169  // Housekeeping
170 
171  //- Deprecated(2020-09) True if range has size greater than zero
172  //
173  // \deprecated(2020-09) - use bool operator - or good()
174  bool valid() const noexcept { return good(); }
175 };
176 
177 
178 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
179 
180 //- Conversion/extraction to labelRange operation (functor).
181 // Specializations shall provide a corresponding \c operator().
182 // For example,
183 // \code
184 // template<>
185 // struct labelRangeOp<polyPatch>
186 // {
187 // labelRange operator()(const polyPatch& pp) const
188 // {
189 // return labelRange(pp.start(), pp.size());
190 // }
191 // };
192 // \endcode
193 template<class> struct labelRangeOp;
194 
195 
196 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
197 
198 } // End namespace Foam
199 
200 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
201 
202 #include "labelRangeI.H"
203 
204 #endif
205 
206 // ************************************************************************* //
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:88
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:52
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:76
scalar range
List< label > labels() const
Return list of labels corresponding to the range.
Definition: labelRange.C:74
labelRange subset0(const label size) const
Calculate the intersection with the given 0/size range.
Definition: labelRange.C:189
labelRange subset(const labelRange &range) const
Calculate the intersection of the range with another.
Definition: labelRange.C:151
bool good() const noexcept
True if range has size greater than zero.
Definition: IntRange.H:189
An ordered pair of two objects of type <T> with first() and second() elements.
Definition: instant.H:46
label start() const noexcept
The (inclusive) lower value of the range.
Definition: IntRange.H:204
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:126
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 has size greater than zero.
Definition: labelRange.H:221
label size() const noexcept
The size of the range.
Definition: IntRange.H:194
bool overlaps(const labelRange &range, bool touches=false) const
Return true if the ranges overlap.
Definition: labelRange.C:103
void reset() noexcept
Reset to zero start and zero size.
Definition: IntRangeI.H:397
Namespace for OpenFOAM.