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-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 \*---------------------------------------------------------------------------*/
28 
29 #include "labelRange.H"
30 #include "List.H"
31 #include "MinMax.H"
32 #include <numeric>
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
36 namespace Foam
37 {
38  int labelRange::debug(debug::debugSwitch("labelRange", 0));
39 }
40 
41 
42 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
43 
45 :
46  labelRange()
47 {
48  if (range.min() < range.max())
49  {
50  start() = range.min();
51  size() = (range.max() - range.min()); // Hope for no overflow?
52  }
53 }
54 
55 
57 :
58  labelRange()
59 {
60  is >> *this;
61 }
62 
63 
64 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
65 
67 {
68  if (size() < 0)
69  {
70  // Skip this check?
71  return List<label>();
72  }
73 
74  List<label> result(this->size());
75  std::iota(result.begin(), result.end(), this->start());
76 
77  return result;
78 }
79 
80 
81 void Foam::labelRange::adjust() noexcept
82 {
83  if (this->start() < 0)
84  {
85  if (this->size() > 0)
86  {
87  // Decrease size accordingly
88  this->size() += this->start();
89  }
90  this->start() = 0;
91  }
92  clampSize();
93 }
94 
95 
96 bool Foam::labelRange::overlaps(const labelRange& range, bool touches) const
97 {
98  const label extra = touches ? 1 : 0;
99 
100  return
101  (
102  this->size() && range.size()
103  &&
104  (
105  (
106  range.first() >= this->first()
107  && range.first() <= this->last() + extra
108  )
109  ||
110  (
111  this->first() >= range.first()
112  && this->first() <= range.last() + extra
113  )
114  )
115  );
116 }
117 
118 
120 {
121  // Trivial cases first
122  if (!this->size())
123  {
124  return *this;
125  }
126  else if (!range.size())
127  {
128  return range;
129  }
130 
131  const label lower = Foam::min(this->first(), range.first());
132  const label upper = Foam::max(this->last(), range.last());
133  const label total = upper+1 - lower;
134  // last = start+size-1
135  // size = last+1-start
136 
137  labelRange newRange(lower, total);
138  newRange.clampSize();
139 
140  return newRange;
141 }
142 
143 
145 {
146  const label lower = Foam::max(this->first(), range.first());
147  const label upper = Foam::min(this->last(), range.last());
148  const label total = upper+1 - lower;
149  // last = start+size-1
150  // size = last+1-start
151 
152  if (total > 0)
153  {
154  return labelRange(lower, total);
155  }
156 
157  return labelRange();
158 }
159 
160 
162 (
163  const label start,
164  const label size
165 ) const
166 {
167  const label lower = Foam::max(this->start(), start);
168  const label upper = Foam::min(this->last(), start+Foam::max(0,size-1));
169  const label total = upper+1 - lower;
170  // last = start+size-1
171  // size = last+1-start
172 
173  if (total > 0)
174  {
175  return labelRange(lower, total);
176  }
177 
178  return labelRange();
179 }
180 
181 
183 {
184  const label lower = Foam::max(this->start(), 0);
185  const label upper = Foam::min(this->last(), Foam::max(0,size-1));
186  const label total = upper+1 - lower;
187  // last = start+size-1
188  // size = last+1-start
189 
190  if (total > 0)
191  {
192  return labelRange(lower, total);
193  }
194 
195  return labelRange();
196 }
197 
198 
199 // ************************************************************************* //
List.H
Foam::labelRange::join
labelRange join(const labelRange &range) const
Return a joined range, squashing any gaps in between.
Definition: labelRange.C:119
Foam::debug::debugSwitch
int debugSwitch(const char *name, const int deflt=0)
Lookup debug switch or add default value.
Definition: debug.C:225
Foam::labelRange::adjust
void adjust() noexcept
Adjust the start to avoid negative indices.
Definition: labelRange.C:81
MinMax.H
Foam::min
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:33
Foam::labelRange::overlaps
bool overlaps(const labelRange &range, bool touches=false) const
Return true if the ranges overlap.
Definition: labelRange.C:96
Foam::stringOps::lower
string lower(const std::string &s)
Return string copy transformed with std::tolower on each character.
Definition: stringOps.C:1184
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
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::max
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
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
labelRange.H
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
Foam::stringOps::upper
string upper(const std::string &s)
Return string copy transformed with std::toupper on each character.
Definition: stringOps.C:1200
Foam::IntRange::clampSize
void clampSize() noexcept
Enforce non-negative size.
Definition: IntRangeI.H:511
Foam::MinMax
A min/max value pair with additional methods. In addition to conveniently storing values,...
Definition: HashSet.H:76