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-2020 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::testType type,
33  const scalar minVal,
34  const scalar maxVal
35 ) noexcept
36 :
37  min_(minVal),
38  max_(maxVal),
39  type_(type)
40 {}
41 
42 
43 inline constexpr Foam::scalarRange::scalarRange() noexcept
44 :
45  scalarRange(scalarRange::NONE, GREAT, -GREAT)
46 {}
47 
48 
49 inline constexpr Foam::scalarRange::scalarRange(const scalar val) noexcept
50 :
51  scalarRange(scalarRange::EQ, val, val)
52 {}
53 
54 
56 (
57  const scalar minVal,
58  const scalar maxVal
59 ) noexcept
60 :
61  scalarRange(scalarRange::GE_LE, minVal, maxVal)
62 {
63  if (minVal > maxVal)
64  {
65  clear(); // Inverted - explicitly mark as such
66  }
67  else if (equal(minVal, maxVal))
68  {
69  type_ = scalarRange::EQ;
70  }
71 }
72 
73 
74 inline constexpr Foam::scalarRange
75 Foam::scalarRange::ge(const scalar minVal) noexcept
76 {
77  return scalarRange(scalarRange::GE, minVal, GREAT);
78 }
79 
80 
81 inline constexpr Foam::scalarRange
82 Foam::scalarRange::gt(const scalar minVal) noexcept
83 {
84  return scalarRange(scalarRange::GT, minVal, GREAT);
85 }
86 
87 
88 inline constexpr Foam::scalarRange
90 {
91  return scalarRange(scalarRange::GE, 0, GREAT);
92 }
93 
94 
95 inline constexpr Foam::scalarRange
97 {
98  return scalarRange(scalarRange::GT, 0, GREAT);
99 }
100 
101 
102 inline constexpr Foam::scalarRange
103 Foam::scalarRange::le(const scalar maxVal) noexcept
104 {
105  return scalarRange(scalarRange::LE, -GREAT, maxVal);
106 }
107 
108 inline constexpr Foam::scalarRange
109 Foam::scalarRange::lt(const scalar maxVal) noexcept
110 {
111  return scalarRange(scalarRange::LT, -GREAT, maxVal);
112 }
113 
114 
115 inline constexpr Foam::scalarRange
117 {
118  return scalarRange(scalarRange::GE_LE, 0, 1);
119 }
120 
121 
122 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
123 
124 inline void Foam::scalarRange::clear() noexcept
125 {
126  min_ = GREAT;
127  max_ = -GREAT;
128  type_ = scalarRange::NONE;
129 }
130 
131 
132 inline bool Foam::scalarRange::empty() const noexcept
133 {
134  return type_ == scalarRange::NONE;
135 }
136 
137 
138 inline bool Foam::scalarRange::valid() const noexcept
139 {
140  return bool(type_);
141 }
142 
143 
144 inline bool Foam::scalarRange::single() const noexcept
145 {
146  return type_ == scalarRange::EQ;
147 }
148 
149 
150 inline Foam::scalar Foam::scalarRange::min() const noexcept
151 {
152  return min_;
153 }
154 
155 
156 inline Foam::scalar Foam::scalarRange::max() const noexcept
157 {
158  return max_;
159 }
160 
161 
162 inline Foam::scalar Foam::scalarRange::value() const
163 {
164  switch (type_)
165  {
166  case scalarRange::EQ: // For equals, min and max are identical
167  case scalarRange::GE:
168  case scalarRange::GT:
169  return min_;
170 
171  case scalarRange::LE:
172  case scalarRange::LT:
173  return max_;
174 
175  case scalarRange::GE_LE:
176  // Multiply before adding to avoid possible overflow
177  return (0.5 * min_) + (0.5 * max_);
178 
179  default: // NONE or ALWAYS. Zero is reasonable dummy value.
180  return 0;
181  }
182 }
183 
184 
185 inline bool Foam::scalarRange::match(const scalar& val) const
186 {
187  switch (type_)
188  {
189  case scalarRange::EQ: return equal(val, min_);
190  case scalarRange::GE: return (val >= min_);
191  case scalarRange::GT: return (val > min_);
192  case scalarRange::LE: return (val <= max_);
193  case scalarRange::LT: return (val < max_);
194  case scalarRange::GE_LE: return (val >= min_ && val <= max_);
195  case scalarRange::ALWAYS: return true;
196  default: return false;
197  }
198 }
199 
200 
201 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
202 
203 inline bool Foam::scalarRange::operator()(const scalar& val) const
204 {
205  return match(val);
206 }
207 
208 
209 inline constexpr bool
210 Foam::scalarRange::operator==
211 (
212  const scalarRange& rhs
213 ) const noexcept
214 {
215  return (type_ == rhs.type_ && min_ == rhs.min_ && max_ == rhs.max_);
216 }
217 
218 
219 inline constexpr bool
220 Foam::scalarRange::operator!=
221 (
222  const scalarRange& rhs
223 ) const noexcept
224 {
225  return !(*this == rhs);
226 }
227 
228 
229 // ************************************************************************* //
Foam::scalarRange::match
bool match(const scalar &val) const
True if the value matches the condition.
Definition: scalarRangeI.H:185
Foam::scalarRange::le
static constexpr scalarRange le(const scalar maxVal) noexcept
A less-equals bound.
Definition: scalarRangeI.H:103
Foam::scalarRange::gt0
static constexpr scalarRange gt0() noexcept
A greater-than zero bound.
Definition: scalarRangeI.H:96
Foam::scalarRange::scalarRange
constexpr scalarRange() noexcept
Construct an empty (inverse, NONE) range - never matches.
Definition: scalarRangeI.H:43
Foam::scalarRange::max
scalar max() const noexcept
The max value of the range.
Definition: scalarRangeI.H:156
Foam::scalarRange::zero_one
static constexpr scalarRange zero_one() noexcept
A greater-equals 0, less-equals 1 bound.
Definition: scalarRangeI.H:116
Foam::scalarRange::operator()
bool operator()(const scalar &val) const
Identical to match(), for use as a predicate.
Definition: scalarRangeI.H:203
Foam::scalarRange
Scalar bounds to be used as a unary predicate.
Definition: scalarRange.H:68
Foam::scalarRange::ge
static constexpr scalarRange ge(const scalar minVal) noexcept
A greater-equals bound.
Definition: scalarRangeI.H:75
Foam::stringOps::match
bool match(const UList< wordRe > &patterns, const std::string &text)
Return true if text matches one of the regular expressions.
Definition: stringOps.H:75
Foam::scalarRange::min
scalar min() const noexcept
The min value of the range.
Definition: scalarRangeI.H:150
Foam::scalarRange::empty
bool empty() const noexcept
True if range is empty (eg, inverted, NONE)
Definition: scalarRangeI.H:132
clear
patchWriters clear()
Foam::scalarRange::ge0
static constexpr scalarRange ge0() noexcept
A greater-equals zero bound.
Definition: scalarRangeI.H:89
Foam::scalarRange::clear
void clear() noexcept
Reset to an empty (inverse, NONE) range.
Definition: scalarRangeI.H:124
bool
bool
Definition: EEqn.H:20
Foam::scalarRange::lt
static constexpr scalarRange lt(const scalar maxVal) noexcept
A less-than bound.
Definition: scalarRangeI.H:109
Foam::roots::type
type
Types of root.
Definition: Roots.H:54
Foam::scalarRange::single
bool single() const noexcept
True if the range bounds represent a single value.
Definition: scalarRangeI.H:144
Foam::equal
bool equal(const T &s1, const T &s2)
Compare two values for equality.
Definition: doubleFloat.H:46
Foam::scalarRange::valid
bool valid() const noexcept
True if range is non-empty.
Definition: scalarRangeI.H:138
Foam::scalarRange::gt
static constexpr scalarRange gt(const scalar minVal) noexcept
A greater-than bound.
Definition: scalarRangeI.H:82
Foam::scalarRange::value
scalar value() const
A representative (average) value for the range.
Definition: scalarRangeI.H:162