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-2022 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.clear();
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 particular advantage offered by MinMax is to clip or limit values
71  to a particular range. For example,
72  \verbatim
73  scalarMinMax range(lower, upper);
74 
75  scalar val;
76  val = range.clip(val) .. return clip values
77 
78  // vs.
79  val = min(max(value, lower, upper))
80  \endverbatim
81 
82  Or when working on lists, the values can be limited in a single pass
83  of the data without intermediate memory allocation.
84  \verbatim
85  scalarField values = ...;
86 
87  for (scalar& val : values)
88  {
89  range.inplaceClip(val);
90  }
91 
92  // vs.
93  values = min(max(values, lower, upper))
94  \endverbatim
95 
96 \*---------------------------------------------------------------------------*/
97 
98 #ifndef Foam_MinMax_H
99 #define Foam_MinMax_H
100 
101 #include "scalar.H"
102 #include "Pair.H"
103 #include "Tuple2.H"
104 #include "VectorSpace.H"
105 #include <type_traits>
106 
107 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
108 
109 namespace Foam
110 {
112 // Forward Declarations
113 template<class T> class MinMax;
114 class zero;
115 
116 // Common min/max types
117 typedef MinMax<label> labelMinMax;
119 
120 
121 /*---------------------------------------------------------------------------*\
122  Class MinMax Declaration
123 \*---------------------------------------------------------------------------*/
124 
125 template<class T>
126 class MinMax
127 :
128  public Tuple2<T,T>
129 {
130 public:
131 
132  // Typedefs
133 
134  //- The value type the MinMax represents
135  typedef T value_type;
136 
137  //- Component type
138  typedef typename pTraits<T>::cmptType cmptType;
139 
140 
141  // Constructors
142 
143  //- Construct inverted range
144  inline MinMax();
145 
146  //- Copy construct from components
147  inline MinMax(const T& minVal, const T& maxVal);
148 
149  //- Copy construct from components
150  inline MinMax(const std::pair<T,T>& range);
151 
152  //- Copy construct from components
153  inline MinMax(const Pair<T>& range);
154 
155  //- Construct with a single zero value
156  inline explicit MinMax(const Foam::zero);
157 
158  //- Construct with a single initial value
159  inline explicit MinMax(const T& val);
160 
161  //- Construct from list of values
162  inline explicit MinMax(const UList<T>& vals);
163 
164 
165  // Static Member Functions
166 
167  //- A semi-infinite range from minVal to the type max
168  inline static MinMax<T> ge(const T& minVal);
169 
170  //- A semi-infinite range from type min to maxVal
171  inline static MinMax<T> le(const T& maxVal);
172 
173  //- A 0-1 range corresponding to the pTraits zero, one
174  inline static MinMax<T> zero_one();
175 
176 
177  // Member Functions
178 
179  // Access
180 
181  //- The min value (first)
182  inline const T& min() const noexcept;
183 
184  //- The min value (first)
185  inline T& min() noexcept;
186 
187  //- The max value (second)
188  inline const T& max() const noexcept;
189 
190  //- The max value (second)
191  inline T& max() noexcept;
192 
193  //- The min/max average value
194  inline T centre() const;
195 
196  //- The min to max span. Zero if the range is invalid.
197  inline T span() const;
198 
199  //- The magnitude of the min to max span. Zero if the range is invalid.
200  inline scalar mag() const;
201 
202  //- Range is empty if it is inverted
203  inline bool empty() const;
204 
205  //- Range is non-inverted
206  inline bool good() const;
207 
208  //- Range is non-inverted
209  bool valid() const { return good(); }
210 
211  //- Reset to an inverted (invalid) range
212  inline void reset();
213 
214  //- Reset min/max to be identical to the specified value
215  inline void reset(const T& val);
216 
217  //- Reset min/max to specified values
218  inline void reset(const T& minVal, const T& maxVal);
219 
220  //- Same as reset() - reset to an inverted (invalid) range
221  void clear() { reset(); }
222 
223 
224  // Testing / Query
225 
226  //- Test if the ranges intersect (exclusive check)
227  inline bool intersects(const MinMax<T>& b) const;
228 
229  //- Test if ranges overlap/touch (inclusive check)
230  inline bool overlaps(const MinMax<T>& b) const;
231 
232  //- Compares the min/max range with the specified value.
233  // \return
234  // - 0: value is within the range, or range is invalid
235  // - -1: range (max) is less than the value
236  // - +1: range (min) is greater than value
237  inline int compare(const T& val) const;
238 
239  //- True if the value is within the range (inclusive check)
240  inline bool contains(const T& val) const;
241 
242  //- If out of range, return the respective min/max limits, otherwise
243  //- return the value itself.
244  // If the range is invalid, always return the value.
245  inline const T& clip(const T& val) const;
247  //- Inplace clip value by the min/max limits
248  // \return True if clipping was applied.
249  inline bool inplaceClip(T& val) const;
250 
251 
252  // Manipulate
253 
254  //- Extend the range to include the other min/max range
255  inline MinMax<T>& add(const MinMax& other);
256 
257  //- Include the value into the range
258  inline MinMax<T>& add(const T& val);
259 
260  //- Include the values into the range
261  inline MinMax<T>& add(const UList<T>& vals);
262 
263 
264  // Member Operators
265 
266  //- Identical to contains(), for use as a predicate.
267  inline bool operator()(const T& val) const;
268 
269  //- Restrict min/max range to union with other range
270  inline MinMax<T>& operator&=(const MinMax<T>& b);
271 
272  //- Extend min/max range to include other range
273  // Can be used in a reduction operation.
274  inline MinMax<T>& operator+=(const MinMax<T>& b);
275 
276  //- Extend min/max range to include value
277  inline MinMax<T>& operator+=(const T& val);
278 
279  //- Extend min/max range to include all values
280  inline MinMax<T>& operator+=(const UList<T>& vals);
281 
282  //- Multiply range by scalar factor
283  inline MinMax<T>& operator*=(const scalar& s);
284 
285  //- Divide range by scalar factor
286  inline MinMax<T>& operator/=(const scalar& s);
287 };
288 
289 
290 // Global Functions
291 
292 //- Min/max range as a string
293 template<class T>
294 word name(const MinMax<T>& range)
295 {
296  return '(' + Foam::name(range.min()) + ',' + Foam::name(range.max()) + ')';
297 }
298 
299 
300 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
301 
302 } // End namespace Foam
303 
304 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
305 
306 #include "MinMaxI.H"
307 
308 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
309 
310 #endif
311 
312 // Global Functions and Operators
313 #include "MinMaxOps.H"
314 
315 
316 // ************************************************************************* //
bool intersects(const MinMax< T > &b) const
Test if the ranges intersect (exclusive check)
Definition: MinMaxI.H:189
MinMax< scalar > scalarMinMax
A scalar min/max range.
Definition: MinMax.H:111
A 2-tuple for storing two objects of dissimilar types. The container is similar in purpose to std::pa...
Definition: stringOps.H:55
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.
Definition: pTraits.H:50
const T & clip(const T &val) const
If out of range, return the respective min/max limits, otherwise return the value itself...
Definition: MinMaxI.H:231
scalar range
bool good() const
Range is non-inverted.
Definition: MinMaxI.H:158
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:165
const T & min() const noexcept
The min value (first)
Definition: MinMaxI.H:100
bool empty() const
Range is empty if it is inverted.
Definition: MinMaxI.H:150
An ordered pair of two objects of type <T> with first() and second() elements.
Definition: instant.H:46
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:52
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
bool inplaceClip(T &val) const
Inplace clip value by the min/max limits.
Definition: MinMaxI.H:250
A class for handling words, derived from Foam::string.
Definition: word.H:63
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:130
bool valid() const
Range is non-inverted.
Definition: MinMax.H:246
T centre() const
The min/max average value.
Definition: MinMaxI.H:128
MinMax< label > labelMinMax
A label min/max range.
Definition: MinMax.H:107
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:99
MinMax< T > & operator/=(const scalar &s)
Divide range by scalar factor.
Definition: MinMaxI.H:380
bool overlaps(const MinMax< T > &b) const
Test if ranges overlap/touch (inclusive check)
Definition: MinMaxI.H:197
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:271
bool operator()(const T &val) const
Identical to contains(), for use as a predicate.
Definition: MinMaxI.H:302
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
int compare(const T &val) const
Compares the min/max range with the specified value.
Definition: MinMaxI.H:205
MinMax< T > & operator+=(const MinMax< T > &b)
Extend min/max range to include other range.
Definition: MinMaxI.H:350
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:114
T span() const
The min to max span. Zero if the range is invalid.
Definition: MinMaxI.H:136
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:58
scalar mag() const
The magnitude of the min to max span. Zero if the range is invalid.
Definition: MinMaxI.H:143
pTraits< T >::cmptType cmptType
Component type.
Definition: MinMax.H:135
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:224
void clear()
Same as reset() - reset to an inverted (invalid) range.
Definition: MinMax.H:266
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:371