scalarRangeI.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) 2018-2023 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
29 
30 inline constexpr Foam::scalarRange::scalarRange
31 (
32  const scalarRange::rangeTypes type,
33  const scalar minVal,
34  const scalar maxVal
35 ) noexcept
36 :
37  min_(minVal),
38  max_(maxVal),
39  type_(type)
40 {}
41 
42 
44 :
45  scalarRange(rangeTypes::NONE, GREAT, -GREAT)
46 {}
47 
48 
49 inline constexpr Foam::scalarRange::scalarRange(const scalar val) noexcept
50 :
51  scalarRange(rangeTypes::EQ, val, val)
52 {}
53 
54 
56 (
57  const scalar minVal,
58  const scalar maxVal
59 ) noexcept
60 :
61  scalarRange(rangeTypes::GE_LE, minVal, maxVal)
62 {
63  if (maxVal < minVal)
64  {
65  reset(); // Inverted - explicitly mark as such
66  }
67  else if (equal(minVal, maxVal))
68  {
69  type_ = rangeTypes::EQ;
70  }
71 }
72 
73 
74 inline constexpr Foam::scalarRange
75 Foam::scalarRange::ge(const scalar minVal) noexcept
76 {
77  return scalarRange(rangeTypes::GE, minVal, GREAT);
78 }
79 
80 
81 inline constexpr Foam::scalarRange
82 Foam::scalarRange::gt(const scalar minVal) noexcept
83 {
84  return scalarRange(rangeTypes::GT, minVal, GREAT);
85 }
86 
87 
88 inline constexpr Foam::scalarRange
90 {
91  return scalarRange(rangeTypes::GE, 0, GREAT);
92 }
93 
94 
95 inline constexpr Foam::scalarRange
97 {
98  return scalarRange(rangeTypes::GT, 0, GREAT);
99 }
100 
101 
102 inline constexpr Foam::scalarRange
103 Foam::scalarRange::le(const scalar maxVal) noexcept
104 {
105  return scalarRange(rangeTypes::LE, -GREAT, maxVal);
106 }
107 
108 inline constexpr Foam::scalarRange
109 Foam::scalarRange::lt(const scalar maxVal) noexcept
110 {
111  return scalarRange(rangeTypes::LT, -GREAT, maxVal);
112 }
113 
114 
115 inline constexpr Foam::scalarRange
117 {
118  return scalarRange(rangeTypes::GE_LE, 0, 1);
119 }
120 
121 
122 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
123 
125 {
126  min_ = GREAT;
127  max_ = -GREAT;
128  type_ = rangeTypes::NONE;
129 }
130 
132 inline bool Foam::scalarRange::empty() const noexcept
133 {
134  return (type_ == rangeTypes::NONE);
135 }
136 
138 inline bool Foam::scalarRange::good() const noexcept
139 {
140  return (type_ != rangeTypes::NONE);
141 }
142 
144 inline bool Foam::scalarRange::single() const noexcept
145 {
146  return (type_ == rangeTypes::EQ);
147 }
148 
149 
150 inline Foam::scalar Foam::scalarRange::value() const
151 {
152  switch (type_)
153  {
154  case rangeTypes::EQ: // For equals, min and max are identical
155  case rangeTypes::GE:
156  case rangeTypes::GT:
157  return min_;
158 
159  case rangeTypes::LE:
160  case rangeTypes::LT:
161  return max_;
162 
163  case rangeTypes::GE_LE:
164  // Multiply before adding to avoid possible overflow
165  return (0.5 * min_) + (0.5 * max_);
167  default: // NONE or ALWAYS. Zero is reasonable dummy value.
168  return 0;
169  }
170 }
171 
172 
173 inline bool Foam::scalarRange::contains(const scalar val) const
174 {
175  switch (type_)
176  {
177  case rangeTypes::EQ: return equal(val, min_);
178  case rangeTypes::GE: return (val >= min_);
179  case rangeTypes::GT: return (val > min_);
180  case rangeTypes::LE: return (val <= max_);
181  case rangeTypes::LT: return (val < max_);
182  case rangeTypes::GE_LE: return (val >= min_ && val <= max_);
183  case rangeTypes::ALWAYS: return true;
184  default: return false;
185  }
186 }
187 
188 
189 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
190 
191 inline constexpr bool
193 {
194  return (type_ == rhs.type_ && min_ == rhs.min_ && max_ == rhs.max_);
195 }
196 
197 
198 inline constexpr bool
200 {
201  return !(*this == rhs);
202 }
203 
204 
205 // ************************************************************************* //
type
Types of root.
Definition: Roots.H:52
constexpr scalarRange() noexcept
Construct an empty (inverse, NONE) range - never matches.
Definition: scalarRangeI.H:36
bool equal(const T &a, const T &b)
Compare two values for equality.
Definition: label.H:164
static constexpr scalarRange gt0() noexcept
A greater-than zero bound.
Definition: scalarRangeI.H:89
bool contains(const scalar val) const
True if the value is matched by the condition.
Definition: scalarRangeI.H:166
void reset() noexcept
Reset to an empty (inverse, NONE) range.
Definition: scalarRangeI.H:117
bool good() const noexcept
True if range is non-empty.
Definition: scalarRangeI.H:131
static constexpr scalarRange zero_one() noexcept
A greater-equals 0, less-equals 1 bound.
Definition: scalarRangeI.H:109
static constexpr scalarRange ge(const scalar minVal) noexcept
A greater-equals bound.
Definition: scalarRangeI.H:68
const direction noexcept
Definition: Scalar.H:258
static constexpr scalarRange ge0() noexcept
A greater-equals zero bound.
Definition: scalarRangeI.H:82
static constexpr scalarRange gt(const scalar minVal) noexcept
A greater-than bound.
Definition: scalarRangeI.H:75
constexpr bool operator!=(const scalarRange &rhs) const noexcept
Definition: scalarRangeI.H:192
bool empty() const noexcept
True if range is empty (eg, inverted, NONE)
Definition: scalarRangeI.H:125
static constexpr scalarRange le(const scalar maxVal) noexcept
A less-equals bound.
Definition: scalarRangeI.H:96
scalar value() const
A representative (average) value for the range.
Definition: scalarRangeI.H:143
constexpr bool operator==(const scalarRange &rhs) const noexcept
Definition: scalarRangeI.H:185
static constexpr scalarRange lt(const scalar maxVal) noexcept
A less-than bound.
Definition: scalarRangeI.H:102
bool single() const noexcept
True if the range bounds represent a single value.
Definition: scalarRangeI.H:137
Scalar bounds to be used as a unary predicate.
Definition: scalarRange.H:63