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-2025 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  // Private Data
115 
116  //- Min value
117  T min_;
118 
119  //- Max value
120  T max_;
121 
122 public:
123 
124  // Typedefs
125 
126  //- The value type the MinMax represents
127  typedef T value_type;
128 
129  //- Component type
130  typedef typename pTraits<T>::cmptType cmptType;
132 
133  // Constructors
134 
135  //- Default construct: an inverted range
136  inline MinMax();
137 
138  //- Construct from min/max limits
139  inline MinMax(const T& minVal, const T& maxVal);
140 
141  //- Implicit construct from min/max limits
142  inline MinMax(const std::pair<T,T>& range);
143 
144  //- Implicit construct from min/max limits
145  inline MinMax(const Pair<T>& range);
146 
147  //- Implicit construct from min/max limits
148  inline MinMax(const Tuple2<T,T>& range);
149 
150  //- Construct with a single zero value
151  inline explicit MinMax(Foam::zero);
152 
153  //- Implicit construct from zero_one as 0-1 range (pTraits zero, one)
154  inline MinMax(Foam::zero_one);
155 
156  //- Construct with a single initial value
157  inline explicit MinMax(const T& val);
158 
159  //- Construct from list of values
160  inline explicit MinMax(const UList<T>& vals);
161 
162 
163  // Static Member Functions
164 
165  //- A semi-infinite range from minVal to the type max
166  inline static MinMax<T> ge(const T& minVal);
167 
168  //- A semi-infinite range from type min to maxVal
169  inline static MinMax<T> le(const T& maxVal);
170 
171  //- A 0-1 range corresponding to the pTraits zero, one
172  inline static MinMax<T> zero_one();
173 
174 
175  // Member Functions
176 
177  // Access
178 
179  //- The min value
180  const T& min() const noexcept { return min_; }
181 
182  //- The min value
183  T& min() noexcept { return min_; }
184 
185  //- The max value
186  const T& max() const noexcept { return max_; }
187 
188  //- The max value
189  T& max() noexcept { return max_; }
190 
191  //- The min/max average value
192  inline T centre() const;
193 
194  //- The min to max span. Zero for invalid range.
195  inline T span() const;
196 
197  //- The magnitude of the min to max span. Zero for invalid range.
198  inline scalar mag() const;
199 
200  //- Range is empty if it is inverted
201  inline bool empty() const;
202 
203  //- Range is non-inverted
204  inline bool good() const;
205 
206  //- Reset to an inverted (invalid) range
207  inline void reset();
208 
209  //- Reset min/max to be identical to the specified value
210  inline void reset(const T& val);
211 
212  //- Reset min/max to specified values
213  inline void reset(const T& minVal, const T& maxVal);
214 
215  //- Same as reset() - reset to an inverted (invalid) range
216  void clear() { reset(); }
218 
219  // Testing / Query
220 
221  //- Test if the ranges intersect (exclusive check)
222  inline bool intersects(const MinMax<T>& b) const;
223 
224  //- Test if ranges overlap/touch (inclusive check)
225  inline bool overlaps(const MinMax<T>& b) const;
226 
227  //- Compares the min/max range with the specified value.
228  // \return
229  // - 0: value is within the range, or range is invalid
230  // - -1: range (max) is less than the value
231  // - +1: range (min) is greater than value
232  inline int compare(const T& val) const;
233 
234  //- True if the value is within the range (inclusive check)
235  inline bool contains(const T& val) const;
236 
237  //- Return value clamped component-wise.
238  // If the range is invalid, just returns the value.
239  inline T clamp(const T& val) const;
240 
241 
242  // Manipulate
243 
244  //- Extend the range to include the other min/max range
245  inline MinMax<T>& add(const MinMax& other);
246 
247  //- Include the value into the range
248  inline MinMax<T>& add(const T& val);
249 
250  //- Include the values into the range
251  inline MinMax<T>& add(const UList<T>& vals);
252 
253  //- Include two or more values into the range.
254  // All values must be similar types
255  template<class... Args>
256  inline MinMax<T>& add(const T& val0, const T& val1, Args&&... values);
257 
258 
259  // Member Operators
260 
261  //- Identical to contains(), for use as a predicate.
262  inline bool operator()(const T& val) const;
263 
264  //- Restrict min/max range to union with other range
265  inline MinMax<T>& operator&=(const MinMax<T>& b);
266 
267  //- Extend min/max range to include other range
268  // Can be used in a reduction operation.
269  inline MinMax<T>& operator+=(const MinMax<T>& b);
270 
271  //- Extend min/max range to include value
272  inline MinMax<T>& operator+=(const T& val);
273 
274  //- Extend min/max range to include all values
275  inline MinMax<T>& operator+=(const UList<T>& vals);
276 
277  //- Multiply range by scalar factor
278  inline MinMax<T>& operator*=(scalar s);
279 
280  //- Divide range by scalar factor
281  inline MinMax<T>& operator/=(scalar s);
282 
283 
284  // Housekeeping
285 
286  //- Range is non-inverted. Same as good (2022-10)
287  bool valid() const { return good(); }
288 
289  //- Old method name. Same as clamp (2023-01)
290  T clip(const T& val) const { return this->clamp(val); }
291 };
292 
293 
294 // * * * * * * * * * * * * * * * * * Traits * * * * * * * * * * * * * * * * //
295 
296 //- Declare MinMax as non-contiguous (similar to Tuple2).
297 // Output remains separate (even in binary) and, since the defined
298 // \c operator+ is somewhat non-standard, also avoid false matching with
299 // any MPI intrinsic operation.
300 template<class T>
301 struct is_contiguous<MinMax<T>> : std::false_type {};
302 
303 
304 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
305 
306 //- Min/max range as a string
307 template<class T>
308 word name(const MinMax<T>& range)
309 {
310  return '(' + Foam::name(range.min()) + ',' + Foam::name(range.max()) + ')';
311 }
312 
313 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
314 
315 //- Read min/max range as (min max) tuple from Istream
316 template<class T>
317 inline Istream& operator>>(Istream& is, MinMax<T>& range)
318 {
319  is.readBegin("min.max");
320  is >> range.min() >> range.max();
321  is.readEnd("min.max");
322 
323  is.check(FUNCTION_NAME);
324  return is;
325 }
326 
327 //- Write min/max range as (min max) tuple to Ostream
328 template<class T>
329 inline Ostream& operator<<(Ostream& os, const MinMax<T>& range)
330 {
332  << range.min() << token::SPACE << range.max()
333  << token::END_LIST;
334  return os;
335 }
336 
337 
338 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
339 
340 } // End namespace Foam
341 
342 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
343 
344 #include "MinMaxI.H"
345 
346 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
347 
348 #endif
349 
350 // Global Functions and Operators
351 #include "MinMaxOps.H"
352 
353 
354 // ************************************************************************* //
bool intersects(const MinMax< T > &b) const
Test if the ranges intersect (exclusive check)
Definition: MinMaxI.H:185
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:56
bool readBegin(const char *funcName)
Begin read of data chunk, starts with &#39;(&#39;.
Definition: Istream.C:134
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:61
Begin list [isseparator].
Definition: token.H:161
const T & min() const noexcept
The min value.
Definition: MinMax.H:207
scalar range
bool good() const
Range is non-inverted.
Definition: MinMaxI.H:153
T clip(const T &val) const
Old method name. Same as clamp (2023-01)
Definition: MinMax.H:381
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:160
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:164
bool empty() const
Range is empty if it is inverted.
Definition: MinMaxI.H:145
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:227
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
Istream & operator>>(Istream &, directionInfo &)
Space [isspace].
Definition: token.H:131
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:126
bool valid() const
Range is non-inverted. Same as good (2022-10)
Definition: MinMax.H:376
End list [isseparator].
Definition: token.H:162
T centre() const
The min/max average value.
Definition: MinMaxI.H:123
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
bool overlaps(const MinMax< T > &b) const
Test if ranges overlap/touch (inclusive check)
Definition: MinMaxI.H:193
const direction noexcept
Definition: scalarImpl.H:255
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:240
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
bool operator()(const T &val) const
Identical to contains(), for use as a predicate.
Definition: MinMaxI.H:292
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Represents 0/1 range or concept. Used for tagged dispatch or clamping.
Definition: pTraits.H:51
int compare(const T &val) const
Compares the min/max range with the specified value.
Definition: MinMaxI.H:201
MinMax< T > & operator+=(const MinMax< T > &b)
Extend min/max range to include other range.
Definition: MinMaxI.H:341
Global functions and operators related to the MinMax class. Included by MinMax.H. ...
MinMax< T > & operator*=(scalar s)
Multiply range by scalar factor.
Definition: MinMaxI.H:362
const T & max() const noexcept
The max value.
Definition: MinMax.H:217
T span() const
The min to max span. Zero for invalid range.
Definition: MinMaxI.H:131
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 for invalid range.
Definition: MinMaxI.H:138
pTraits< T >::cmptType cmptType
Component type.
Definition: MinMax.H:131
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))
MinMax< T > & operator/=(scalar s)
Divide range by scalar factor.
Definition: MinMaxI.H:371
bool contains(const T &val) const
True if the value is within the range (inclusive check)
Definition: MinMaxI.H:220
void clear()
Same as reset() - reset to an inverted (invalid) range.
Definition: MinMax.H:267
Namespace for OpenFOAM.
MinMax()
Default construct: an inverted range.
Definition: MinMaxI.H:47