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-------------------------------------------------------------------------------
11License
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
36namespace Foam
37{
38 int labelRange::debug(debug::debugSwitch("labelRange", 0));
39}
40
41
42// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
43
45:
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:
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
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
96bool 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// ************************************************************************* //
scalar range
void clampSize() noexcept
Enforce non-negative size.
Definition: IntRangeI.H:511
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:77
A min/max value pair with additional methods. In addition to conveniently storing values,...
Definition: MinMax.H:128
int overlaps
Flag to control which overlap calculations are performed.
Definition: PDRparams.H:97
iterator begin() noexcept
Return an iterator to begin traversing the UList.
Definition: UListI.H:329
iterator end() noexcept
Return an iterator to end traversing the UList.
Definition: UListI.H:350
A range or interval of labels defined by a start and a size.
Definition: labelRange.H:58
constexpr labelRange() noexcept
Default construct an empty range (0,0)
Definition: labelRangeI.H:31
void adjust() noexcept
Adjust the start to avoid negative indices.
Definition: labelRange.C:81
List< label > labels() const
Return list of labels corresponding to the range.
Definition: labelRange.C:66
labelRange subset(const labelRange &range) const
Calculate the intersection of the range with another.
Definition: labelRange.C:144
labelRange subset0(const label size) const
Calculate the intersection with the given 0/size range.
Definition: labelRange.C:182
static int debug
Debugging.
Definition: labelRange.H:73
labelRange join(const labelRange &range) const
Return a joined range, squashing any gaps in between.
Definition: labelRange.C:119
int debugSwitch(const char *name, const int deflt=0)
Lookup debug switch or add default value.
Definition: debug.C:225
Namespace for OpenFOAM.
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:33
const direction noexcept
Definition: Scalar.H:223