MinMax.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) 2019-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 Class
27  Foam::MinMax
28 
29 Description
30  A min/max value pair with additional methods.
31  In addition to conveniently storing values, it can be used for logic
32  operations or to modify data. A few global functions and functors are
33  also provided.
34 
35  Examples of use.
36 
37  Determine min/max limits from a List of values:
38  \verbatim
39  List<scalar> values = ...;
40 
41  // on construction
42  MinMax<scalar> range(values);
43 
44  range.reset();
45 
46  range += val;
47 
48  // global minMax() function
49  Info<< minMax(values) << nl;
50  \endverbatim
51 
52  General comparison operations
53  \verbatim
54  scalar val;
55  if (val < range) ... value is below range min
56  if (range.contains(val)) ... value within range
57  if (range.compare(val) > 0) ... value is above range max
58  if (range(val)) ... value within range - as predicate
59  \endverbatim
60 
61  Since the range has a predicate form, it can be used as a filter method.
62  For example,
63  \verbatim
64  Info<< "values in range: " << subsetList(values, range) << nl;
65 
66  boolList mask = ListOps::create<bool>(values, range);
67  Info<< "values values in range " << mask << nl;
68  \endverbatim
69 
70  One advantage offered by MinMax is to clamp or limit values
71  to a particular range. For example,
72  \verbatim
73  scalarMinMax range(lower, upper);
74 
75  scalar val;
76  val = range.clamp(val) .. return clamped values
77 
78  // vs.
79  val = min(max(value, lower), upper)
80  \endverbatim
81 
82 \*---------------------------------------------------------------------------*/
83 
84 #ifndef Foam_MinMax_H
85 #define Foam_MinMax_H
86 
87 #include "scalar.H"
88 #include "zero.H"
89 #include "Pair.H"
90 #include "Tuple2.H"
91 #include "VectorSpace.H"
92 #include <type_traits>
93 
94 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
95 
96 namespace Foam
97 {
98 
99 // Forward Declarations
100 template<class T> class MinMax;
101 
102 // Common min/max types
103 typedef MinMax<label> labelMinMax;
105 
106 
107 /*---------------------------------------------------------------------------*\
108  Class MinMax Declaration
109 \*---------------------------------------------------------------------------*/
110 
111 template<class T>
112 class MinMax
113 :
114  public Tuple2<T,T>
115 {
116 public:
117 
118  // Typedefs
119 
120  //- The value type the MinMax represents
121  typedef T value_type;
122 
123  //- Component type
124  typedef typename pTraits<T>::cmptType cmptType;
125 
126 
127  // Constructors
128 
129  //- Construct inverted range
130  inline MinMax();
131 
132  //- Copy construct from components
133  inline MinMax(const T& minVal, const T& maxVal);
134 
135  //- Copy construct from components
136  inline MinMax(const std::pair<T,T>& range);
137 
138  //- Copy construct from components
139  inline MinMax(const Pair<T>& range);
140 
141  //- Construct with a single zero value
142  inline explicit MinMax(const Foam::zero);
143 
144  //- Implicit construct from zero_one as 0-1 range (pTraits zero, one)
145  inline MinMax(const Foam::zero_one);
146 
147  //- Construct with a single initial value
148  inline explicit MinMax(const T& val);
149 
150  //- Construct from list of values
151  inline explicit MinMax(const UList<T>& vals);
152 
153 
154  // Static Member Functions
155 
156  //- A semi-infinite range from minVal to the type max
157  inline static MinMax<T> ge(const T& minVal);
158 
159  //- A semi-infinite range from type min to maxVal
160  inline static MinMax<T> le(const T& maxVal);
161 
162  //- A 0-1 range corresponding to the pTraits zero, one
163  inline static MinMax<T> zero_one();
164 
165 
166  // Member Functions
167 
168  // Access
169 
170  //- The min value (first)
171  inline const T& min() const noexcept;
172 
173  //- The min value (first)
174  inline T& min() noexcept;
175 
176  //- The max value (second)
177  inline const T& max() const noexcept;
178 
179  //- The max value (second)
180  inline T& max() noexcept;
181 
182  //- The min/max average value
183  inline T centre() const;
184 
185  //- The min to max span. Zero if the range is invalid.
186  inline T span() const;
187 
188  //- The magnitude of the min to max span. Zero if the range is invalid.
189  inline scalar mag() const;
190 
191  //- Range is empty if it is inverted
192  inline bool empty() const;
193 
194  //- Range is non-inverted
195  inline bool good() const;
196 
197  //- Range is non-inverted
198  bool valid() const { return good(); }
199 
200  //- Reset to an inverted (invalid) range
201  inline void reset();
202 
203  //- Reset min/max to be identical to the specified value
204  inline void reset(const T& val);
205 
206  //- Reset min/max to specified values
207  inline void reset(const T& minVal, const T& maxVal);
208 
209  //- Same as reset() - reset to an inverted (invalid) range
210  void clear() { reset(); }
211 
212 
213  // Testing / Query
214 
215  //- Test if the ranges intersect (exclusive check)
216  inline bool intersects(const MinMax<T>& b) const;
217 
218  //- Test if ranges overlap/touch (inclusive check)
219  inline bool overlaps(const MinMax<T>& b) const;
220 
221  //- Compares the min/max range with the specified value.
222  // \return
223  // - 0: value is within the range, or range is invalid
224  // - -1: range (max) is less than the value
225  // - +1: range (min) is greater than value
226  inline int compare(const T& val) const;
227 
228  //- True if the value is within the range (inclusive check)
229  inline bool contains(const T& val) const;
230 
231  //- Return value clamped component-wise.
232  // If the range is invalid, just returns the value.
233  inline T clamp(const T& val) const;
234 
235 
236  // Manipulate
238  //- Extend the range to include the other min/max range
239  inline MinMax<T>& add(const MinMax& other);
240 
241  //- Include the value into the range
242  inline MinMax<T>& add(const T& val);
243 
244  //- Include the values into the range
245  inline MinMax<T>& add(const UList<T>& vals);
246 
247 
248  // Member Operators
249 
250  //- Identical to contains(), for use as a predicate.
251  inline bool operator()(const T& val) const;
252 
253  //- Restrict min/max range to union with other range
254  inline MinMax<T>& operator&=(const MinMax<T>& b);
255 
256  //- Extend min/max range to include other range
257  // Can be used in a reduction operation.
258  inline MinMax<T>& operator+=(const MinMax<T>& b);
259 
260  //- Extend min/max range to include value
261  inline MinMax<T>& operator+=(const T& val);
262 
263  //- Extend min/max range to include all values
264  inline MinMax<T>& operator+=(const UList<T>& vals);
265 
266  //- Multiply range by scalar factor
267  inline MinMax<T>& operator*=(const scalar& s);
268 
269  //- Divide range by scalar factor
270  inline MinMax<T>& operator/=(const scalar& s);
271 
272 
273  // Housekeeping
274 
275  //- Old method name. Same as clamp (2023-01)
276  T clip(const T& val) const { return this->clamp(val); }
277 
278  //- Old method (2023-01)
279  void inplaceClip(T& val) const { val = this->clamp(val); }
280 };
281 
282 
283 // Global Functions
284 
285 //- Min/max range as a string
286 template<class T>
287 word name(const MinMax<T>& range)
288 {
289  return '(' + Foam::name(range.min()) + ',' + Foam::name(range.max()) + ')';
290 }
291 
292 
293 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
294 
295 } // End namespace Foam
296 
297 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
298 
299 #include "MinMaxI.H"
300 
301 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
302 
303 #endif
304 
305 // Global Functions and Operators
306 #include "MinMaxOps.H"
307 
308 
309 // ************************************************************************* //
bool intersects(const MinMax< T > &b) const
Test if the ranges intersect (exclusive check)
Definition: MinMaxI.H:196
MinMax< scalar > scalarMinMax
A scalar min/max range.
Definition: MinMax.H:97
A 2-tuple for storing two objects of dissimilar types. The container is similar in purpose to std::pa...
Definition: stringOps.H:54
A min/max value pair with additional methods. In addition to conveniently storing values...
Definition: HashSet.H:72
A traits class, which is primarily used for primitives and vector-space.
Definition: pTraits.H:75
scalar range
bool good() const
Range is non-inverted.
Definition: MinMaxI.H:165
T clip(const T &val) const
Old method name. Same as clamp (2023-01)
Definition: MinMax.H:358
static MinMax< T > zero_one()
A 0-1 range corresponding to the pTraits zero, one.
Definition: MinMaxI.H:38
void reset()
Reset to an inverted (invalid) range.
Definition: MinMaxI.H:172
const T & min() const noexcept
The min value (first)
Definition: MinMaxI.H:107
bool empty() const
Range is empty if it is inverted.
Definition: MinMaxI.H:157
An ordered pair of two objects of type <T> with first() and second() elements.
Definition: instant.H:46
T clamp(const T &val) const
Return value clamped component-wise.
Definition: MinMaxI.H:238
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
static MinMax< T > ge(const T &minVal)
A semi-infinite range from minVal to the type max.
Definition: MinMaxI.H:24
T value_type
The value type the MinMax represents.
Definition: MinMax.H:116
bool valid() const
Range is non-inverted.
Definition: MinMax.H:237
T centre() const
The min/max average value.
Definition: MinMaxI.H:135
MinMax< label > labelMinMax
A label min/max range.
Definition: MinMax.H:93
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:105
MinMax< T > & operator/=(const scalar &s)
Divide range by scalar factor.
Definition: MinMaxI.H:359
bool overlaps(const MinMax< T > &b) const
Test if ranges overlap/touch (inclusive check)
Definition: MinMaxI.H:204
const direction noexcept
Definition: Scalar.H:258
MinMax< T > & operator &=(const MinMax< T > &b)
Restrict min/max range to union with other range.
MinMax< T > & add(const MinMax &other)
Extend the range to include the other min/max range.
Definition: MinMaxI.H:250
bool operator()(const T &val) const
Identical to contains(), for use as a predicate.
Definition: MinMaxI.H:281
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
void inplaceClip(T &val) const
Old method (2023-01)
Definition: MinMax.H:363
Represents 0/1 range or concept. Used for tagged dispatch or clamping.
Definition: pTraits.H:65
int compare(const T &val) const
Compares the min/max range with the specified value.
Definition: MinMaxI.H:212
MinMax< T > & operator+=(const MinMax< T > &b)
Extend min/max range to include other range.
Definition: MinMaxI.H:329
Global functions and operators related to the MinMax class. Included by MinMax.H. ...
const T & max() const noexcept
The max value (second)
Definition: MinMaxI.H:121
T span() const
The min to max span. Zero if the range is invalid.
Definition: MinMaxI.H:143
static MinMax< T > le(const T &maxVal)
A semi-infinite range from type min to maxVal.
Definition: MinMaxI.H:31
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:57
scalar mag() const
The magnitude of the min to max span. Zero if the range is invalid.
Definition: MinMaxI.H:150
pTraits< T >::cmptType cmptType
Component type.
Definition: MinMax.H:121
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
bool contains(const T &val) const
True if the value is within the range (inclusive check)
Definition: MinMaxI.H:231
void clear()
Same as reset() - reset to an inverted (invalid) range.
Definition: MinMax.H:257
Namespace for OpenFOAM.
MinMax()
Construct inverted range.
Definition: MinMaxI.H:47
MinMax< T > & operator*=(const scalar &s)
Multiply range by scalar factor.
Definition: MinMaxI.H:350