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-2019 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 
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 
44 :
45  scalarRange(scalarRange::NONE, GREAT, -GREAT)
46 {}
47 
48 
49 inline Foam::scalarRange::scalarRange(const scalar value) noexcept
50 :
51  scalarRange(scalarRange::EQ, value, value)
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_ = EQ;
70  }
71 }
72 
73 
74 inline Foam::scalarRange Foam::scalarRange::ge(const scalar minVal) noexcept
75 {
76  return scalarRange(scalarRange::GE, minVal, GREAT);
77 }
78 
79 
80 inline Foam::scalarRange Foam::scalarRange::gt(const scalar minVal) noexcept
81 {
82  return scalarRange(scalarRange::GT, minVal, GREAT);
83 }
84 
85 
87 {
88  return scalarRange(scalarRange::GE, 0, GREAT);
89 }
90 
91 
93 {
94  return scalarRange(scalarRange::GT, 0, GREAT);
95 }
96 
97 
98 inline Foam::scalarRange Foam::scalarRange::le(const scalar maxVal) noexcept
99 {
100  return scalarRange(scalarRange::LE, -GREAT, maxVal);
101 }
102 
103 inline Foam::scalarRange Foam::scalarRange::lt(const scalar maxVal) noexcept
104 {
105  return scalarRange(scalarRange::LT, -GREAT, maxVal);
106 }
107 
108 
110 {
111  return scalarRange(scalarRange::GE_LE, 0, 1);
112 }
113 
114 
115 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
116 
117 inline void Foam::scalarRange::clear() noexcept
118 {
119  min_ = GREAT;
120  max_ = -GREAT;
121  type_ = scalarRange::NONE;
122 }
123 
124 
125 inline bool Foam::scalarRange::empty() const noexcept
126 {
127  return type_ == NONE;
128 }
129 
130 
131 inline bool Foam::scalarRange::valid() const noexcept
132 {
133  return type_;
134 }
135 
136 
137 inline bool Foam::scalarRange::single() const noexcept
138 {
139  return type_ == scalarRange::EQ;
140 }
141 
142 
143 inline Foam::scalar Foam::scalarRange::min() const noexcept
144 {
145  return min_;
146 }
147 
148 
149 inline Foam::scalar Foam::scalarRange::max() const noexcept
150 {
151  return max_;
152 }
153 
154 
155 inline Foam::scalar Foam::scalarRange::value() const
156 {
157  switch (type_)
158  {
159  case scalarRange::EQ: // For equals, min and max are identical
160  case scalarRange::GE:
161  case scalarRange::GT:
162  return min_;
163 
164  case scalarRange::LE:
165  case scalarRange::LT:
166  return max_;
167 
168  case scalarRange::GE_LE:
169  // Multiply before adding to avoid possible overflow
170  return (0.5 * min_) + (0.5 * max_);
171 
172  default: // NONE or ALWAYS. Zero is reasonable dummy value.
173  return 0;
174  }
175 }
176 
177 
178 inline bool Foam::scalarRange::match(const scalar& val) const
179 {
180  switch (type_)
181  {
182  case EQ: return equal(val, min_);
183  case GE: return (val >= min_);
184  case GT: return (val > min_);
185  case LE: return (val <= max_);
186  case LT: return (val < max_);
187  case GE_LE: return (val >= min_ && val <= max_);
188  case ALWAYS: return true;
189  default: return false;
190  }
191 }
192 
193 
194 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
195 
196 inline bool Foam::scalarRange::operator()(const scalar& val) const
197 {
198  return match(val);
199 }
200 
201 
202 inline bool Foam::scalarRange::operator==
203 (
204  const scalarRange& rhs
205 ) const noexcept
206 {
207  return (type_ == rhs.type_ && min_ == rhs.min_ && max_ == rhs.max_);
208 }
209 
210 
211 inline bool Foam::scalarRange::operator!=
212 (
213  const scalarRange& rhs
214 ) const noexcept
215 {
216  return !(operator==(rhs));
217 }
218 
219 
220 // ************************************************************************* //
Foam::scalarRange::lt
static scalarRange lt(const scalar maxVal) noexcept
Construct a less-than bound.
Definition: scalarRangeI.H:103
Foam::scalarRange::gt0
static scalarRange gt0() noexcept
Construct a greater-than zero bound.
Definition: scalarRangeI.H:92
Foam::val
label ListType::const_reference val
Definition: ListOps.H:407
Foam::scalarRange::match
bool match(const scalar &val) const
True if the value matches the condition.
Definition: scalarRangeI.H:178
Foam::scalarRange::ge0
static scalarRange ge0() noexcept
Construct a greater-equals zero bound.
Definition: scalarRangeI.H:86
Foam::scalarRange::max
scalar max() const noexcept
The max value of the range.
Definition: scalarRangeI.H:149
Foam::operator==
tmp< faMatrix< Type > > operator==(const faMatrix< Type > &, const faMatrix< Type > &)
Foam::scalarRange::operator()
bool operator()(const scalar &val) const
Identical to match(), for use as a predicate.
Definition: scalarRangeI.H:196
Foam::scalarRange
Scalar bounds to be used as a unary predicate.
Definition: scalarRange.H:67
Foam::scalarRange::gt
static scalarRange gt(const scalar minVal) noexcept
Construct a greater-than bound.
Definition: scalarRangeI.H:80
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:143
Foam::scalarRange::empty
bool empty() const noexcept
True if range is empty (eg, inverted)
Definition: scalarRangeI.H:125
clear
patchWriters clear()
Foam::scalarRange::scalarRange
scalarRange() noexcept
Construct an empty (inverse) range.
Definition: scalarRangeI.H:43
Foam::scalarRange::le
static scalarRange le(const scalar maxVal) noexcept
Construct a less-equals bound.
Definition: scalarRangeI.H:98
Foam::scalarRange::clear
void clear() noexcept
Reset to an empty (inverse) range.
Definition: scalarRangeI.H:117
Foam::roots::type
type
Types of root.
Definition: Roots.H:54
Foam::scalarRange::ge
static scalarRange ge(const scalar minVal) noexcept
Construct a greater-equals bound.
Definition: scalarRangeI.H:74
Foam::scalarRange::single
bool single() const noexcept
True if the range bounds represent a single value.
Definition: scalarRangeI.H:137
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:131
Foam::scalarRange::zero_one
static scalarRange zero_one() noexcept
Construct a greater-equals 0, less-equals 1 bound.
Definition: scalarRangeI.H:109
Foam::scalarRange::value
scalar value() const
A representative (average) value for the range.
Definition: scalarRangeI.H:155