labelRange.C
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 \*---------------------------------------------------------------------------*/
28 
29 #include "labelRange.H"
30 #include "List.H"
31 #include "MinMax.H"
32 #include "Pair.H"
33 #include <numeric>
34 
35 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36 
37 namespace Foam
38 {
39  int labelRange::debug(debug::debugSwitch("labelRange", 0));
40 }
41 
42 
43 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
44 
46 :
47  labelRange()
48 {
49  if (range.min() <= range.max())
50  {
51  // max is inclusive, so size with +1. Hope for no overflow
52  start() = range.min();
53  size() = 1 + (range.max() - range.min());
54  }
55 }
56 
57 
58 Foam::labelRange::labelRange(const Pair<label>& start_end) noexcept
59 :
60  labelRange()
61 {
62  if (start_end.first() <= start_end.second())
63  {
64  // second is exclusive, so size directly. Hope for no overflow
65  start() = start_end.first();
66  size() = (start_end.second() - start_end.first());
67  }
68 }
69 
70 
72 :
73  labelRange()
74 {
75  is >> *this;
76 }
77 
78 
79 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
80 
82 {
83  List<label> result;
84 
85  if (this->size() > 0)
86  {
87  result.resize(this->size());
88  std::iota(result.begin(), result.end(), this->start());
89  }
90 
91  return result;
92 }
93 
94 
96 {
97  if (this->start() < 0)
98  {
99  if (this->size() > 0)
100  {
101  // Decrease size accordingly
102  this->size() += this->start();
103  }
104  this->start() = 0;
105  }
106  clampSize();
107 }
108 
109 
110 bool Foam::labelRange::overlaps(const labelRange& range, bool touches) const
111 {
112  const label extra = touches ? 1 : 0;
113 
114  return
115  (
116  this->size() && range.size()
117  &&
118  (
119  (
120  range.min() >= this->min()
121  && range.min() <= this->max() + extra
122  )
123  ||
124  (
125  this->min() >= range.min()
126  && this->min() <= range.max() + extra
127  )
128  )
129  );
130 }
131 
132 
134 {
135  // Trivial cases first
136  if (!this->size())
137  {
138  return *this;
139  }
140  else if (!range.size())
141  {
142  return range;
143  }
144 
145  const label lower = Foam::min(this->min(), range.min());
146  const label upper = Foam::max(this->max(), range.max());
147  const label total = upper+1 - lower;
148  // last = start+size-1
149  // size = last+1-start
150 
151  labelRange newRange(lower, total);
152  newRange.clampSize();
153 
154  return newRange;
155 }
156 
157 
159 {
160  const label lower = Foam::max(this->min(), range.min());
161  const label upper = Foam::min(this->max(), range.max());
162  const label total = upper+1 - lower;
163  // last = start+size-1
164  // size = last+1-start
165 
166  if (total > 0)
167  {
168  return labelRange(lower, total);
169  }
170 
171  return labelRange();
172 }
173 
174 
176 (
177  const label start,
178  const label size
179 ) const
180 {
181  const label lower = Foam::max(this->min(), start);
182  const label upper = Foam::min(this->max(), start+Foam::max(0,size-1));
183  const label total = upper+1 - lower;
184  // last = start+size-1
185  // size = last+1-start
186 
187  if (total > 0)
188  {
189  return labelRange(lower, total);
190  }
191 
192  return labelRange();
193 }
194 
195 
196 Foam::labelRange Foam::labelRange::subset0(const label size) const
197 {
198  const label lower = Foam::max(this->min(), 0);
199  const label upper = Foam::min(this->max(), Foam::max(0,size-1));
200  const label total = upper+1 - lower;
201  // last = start+size-1
202  // size = last+1-start
203 
204  if (total > 0)
205  {
206  return labelRange(lower, total);
207  }
208 
209  return labelRange();
210 }
211 
212 
213 // ************************************************************************* //
constexpr labelRange() noexcept
Default construct an empty range (0,0)
Definition: labelRangeI.H:24
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:160
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
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:40
A range or interval of labels defined by a start and a size.
Definition: labelRange.H:52
A min/max value pair with additional methods. In addition to conveniently storing values...
Definition: HashSet.H:72
int debugSwitch(const char *name, const int deflt=0)
Lookup debug switch or add default value.
Definition: debug.C:222
string upper(const std::string &s)
Return string copy transformed with std::toupper on each character.
Definition: stringOps.C:1187
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
IntType max() const noexcept
The (inclusive) upper value of the range, same as rbegin_value(). Ill-defined for an empty range...
Definition: IntRange.H:221
labelRange subset(const labelRange &range) const
Calculate the intersection of the range with another.
Definition: labelRange.C:151
label start() const noexcept
The (inclusive) lower value of the range.
Definition: IntRange.H:204
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:26
iterator begin() noexcept
Return an iterator to begin traversing the UList.
Definition: UListI.H:391
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
string lower(const std::string &s)
Return string copy transformed with std::tolower on each character.
Definition: stringOps.C:1171
IntType min() const noexcept
The (inclusive) lower value of the range, same as start(), begin_value()
Definition: IntRange.H:215
iterator end() noexcept
Return an iterator to end traversing the UList.
Definition: UListI.H:435
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
Namespace for OpenFOAM.