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-2019 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 "MinMax.H"
31 #include "List.H"
32 #include "token.H"
33 #include <numeric>
34 
35 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36 
37 namespace Foam
38 {
39  int labelRange::debug(debug::debugSwitch("labelRange", 0));
40 }
41 
43 
44 
45 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
46 
48 :
49  start_(0),
50  size_(0)
51 {
52  if (range.min() < range.max())
53  {
54  start_ = range.min();
55  size_ = (range.max() - range.min()); // Hope for no overflow?
56  }
57 }
58 
59 
61 :
62  start_(0),
63  size_(0)
64 {
65  is >> *this;
66 }
67 
68 
69 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
70 
72 {
73  if (size_ <= 0)
74  {
75  return List<label>();
76  }
77 
78  List<label> result(size_);
79  std::iota(result.begin(), result.end(), start_);
80 
81  return result;
82 }
83 
84 
85 void Foam::labelRange::adjust() noexcept
86 {
87  if (start_ < 0)
88  {
89  if (size_ > 0) // Second check needed to avoid (negative) overflow
90  {
91  size_ += start_;
92  }
93  start_ = 0;
94  }
95  if (size_ < 0)
96  {
97  size_ = 0; // No negative sizes
98  }
99 }
100 
101 
102 bool Foam::labelRange::overlaps(const labelRange& range, bool touches) const
103 {
104  const label extra = touches ? 1 : 0;
105 
106  return
107  (
108  this->size() && range.size()
109  &&
110  (
111  (
112  range.first() >= this->first()
113  && range.first() <= this->last() + extra
114  )
115  ||
116  (
117  this->first() >= range.first()
118  && this->first() <= range.last() + extra
119  )
120  )
121  );
122 }
123 
124 
126 {
127  // Trivial cases first
128  if (!size_)
129  {
130  return *this;
131  }
132  else if (!range.size())
133  {
134  return range;
135  }
136 
137  const label lower = Foam::min(this->first(), range.first());
138  const label upper = Foam::max(this->last(), range.last());
139  const label total = upper+1 - lower;
140  // last = start+size-1
141  // size = last+1-start
142 
143  return labelRange(lower, total);
144 }
145 
146 
148 {
149  const label lower = Foam::max(this->first(), range.first());
150  const label upper = Foam::min(this->last(), range.last());
151  const label total = upper+1 - lower;
152  // last = start+size-1
153  // size = last+1-start
154 
155  if (total > 0)
156  {
157  return labelRange(lower, total);
158  }
159 
160  return labelRange();
161 }
162 
163 
165 (
166  const label start,
167  const label size
168 ) const
169 {
170  const label lower = Foam::max(this->start(), start);
171  const label upper = Foam::min(this->last(), start+Foam::max(0,size-1));
172  const label total = upper+1 - lower;
173  // last = start+size-1
174  // size = last+1-start
175 
176  if (total > 0)
177  {
178  return labelRange(lower, total);
179  }
180 
181  return labelRange();
182 }
183 
184 
186 {
187  const label lower = Foam::max(this->start(), 0);
188  const label upper = Foam::min(this->last(), Foam::max(0,size-1));
189  const label total = upper+1 - lower;
190  // last = start+size-1
191  // size = last+1-start
192 
193  if (total > 0)
194  {
195  return labelRange(lower, total);
196  }
197 
198  return labelRange();
199 }
200 
201 
202 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
203 
205 {
206  label start, size;
207 
208  is.readBegin("labelRange");
209  is >> start >> size;
210  is.readEnd("labelRange");
211 
212  if (size < 0) size = 0; // No negative sizes
213 
214  range.setStart(start);
215  range.setSize(size);
216 
217  is.check(FUNCTION_NAME);
218  return is;
219 }
220 
221 
223 {
224  os << token::BEGIN_LIST
225  << range.start() << token::SPACE << range.size()
226  << token::END_LIST;
227 
228  os.check(FUNCTION_NAME);
229  return os;
230 }
231 
232 
233 // ************************************************************************* //
token.H
List.H
Foam::labelRange::join
labelRange join(const labelRange &range) const
Return a joined range, squashing any gaps in between.
Definition: labelRange.C:125
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:85
MinMax.H
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:228
Foam::labelRange::null
static const labelRange null
An empty range with start=0, size=0.
Definition: labelRange.H:89
Foam::stringOps::lower
string lower(const std::string &str)
Return string transformed with std::tolower on each character.
Definition: stringOps.C:1199
Foam::Istream::readEnd
bool readEnd(const char *funcName)
End read of data chunk, ends with ')'.
Definition: Istream.C:127
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::Istream::readBegin
bool readBegin(const char *funcName)
Begin read of data chunk, starts with '('.
Definition: Istream.C:109
Foam::labelRange::overlaps
bool overlaps(const labelRange &range, bool touches=false) const
Return true if the ranges overlap.
Definition: labelRange.C:102
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::stringOps::upper
string upper(const std::string &str)
Return string transformed with std::toupper on each character.
Definition: stringOps.C:1219
Foam::labelRange
A range or interval of labels defined by a start and a size.
Definition: labelRange.H:58
Foam::labelRange::subset
labelRange subset(const labelRange &range) const
Calculate the intersection of the range with another.
Definition: labelRange.C:147
Foam::labelRange::labels
List< label > labels() const
Return the range as a list of labels.
Definition: labelRange.C:71
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::IOstream::check
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:51
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::labelRange::debug
static int debug
Debugging.
Definition: labelRange.H:80
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: HashTable.H:102
Foam::labelRange::labelRange
constexpr labelRange() noexcept
An empty range with zero for start/size.
Definition: labelRangeI.H:31
Foam::start
label ListType::const_reference const label start
Definition: ListOps.H:408
Foam::token::SPACE
Space [isspace].
Definition: token.H:112
Foam::labelRange::subset0
labelRange subset0(const label size) const
Calculate the intersection with the given 0/size range.
Definition: labelRange.C:185
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:261
Foam::token::END_LIST
End list [isseparator].
Definition: token.H:118
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::token::BEGIN_LIST
Begin list [isseparator].
Definition: token.H:117
Foam::MinMax
A min/max value pair with additional methods. In addition to conveniently storing values,...
Definition: HashSet.H:76
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &)
Definition: boundaryPatch.C:102