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-2020 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
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
26Class
27 Foam::MinMax
28
29Description
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 MinMax_H
99#define 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
109namespace Foam
110{
111
112// Forward declarations
113template<class T> class MinMax;
114class zero;
115
116// Common min/max types
119
120
121/*---------------------------------------------------------------------------*\
122 Class MinMax Declaration
123\*---------------------------------------------------------------------------*/
124
125template<class T>
126class MinMax
127:
128 public Tuple2<T,T>
129{
130public:
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 valid if it is not inverted
206 inline bool valid() const;
207
208 //- Reset to an invalid, inverted range
209 inline void clear();
210
211
212 // Testing / Query
213
214 //- Intersect (union) with the second range.
215 // \return True if the resulting intersection is non-empty.
216 inline bool intersect(const MinMax<T>& b);
217
218 //- Test if the ranges overlap
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
229 inline bool contains(const T& val) const;
230
231 //- If out of range, return the respective min/max limits, otherwise
232 //- return the value itself.
233 // If the range is invalid, always return the value.
234 inline const T& clip(const T& val) const;
235
236 //- Inplace clip value by the min/max limits
237 // \return True if clipping was applied.
238 inline bool inplaceClip(T& val) const;
239
240
241 // Manipulate
242
243 //- Extend the range to include the other min/max range
244 inline MinMax<T>& add(const MinMax& other);
245
246 //- Include the value into the range
247 inline MinMax<T>& add(const T& val);
248
249 //- Include the values into the range
250 inline MinMax<T>& add(const UList<T>& vals);
251
252
253 // Member Operators
254
255 //- Identical to contains(), for use as a predicate.
256 inline bool operator()(const T& val) const;
257
258 //- Extend min/max range to include other range
259 // Can be used in a reduction operation.
260 inline MinMax<T>& operator+=(const MinMax<T>& b);
261
262 //- Extend min/max range to include value
263 inline MinMax<T>& operator+=(const T& val);
264
265 //- Extend min/max range to include all values
266 inline MinMax<T>& operator+=(const UList<T>& vals);
267
268 //- Multiply range by scalar factor
269 inline MinMax<T>& operator*=(const scalar& s);
270
271 //- Divide range by scalar factor
272 inline MinMax<T>& operator/=(const scalar& s);
273};
274
275
276// Global Functions
277
278//- Min/max range as a string
279template<class T>
280word name(const MinMax<T>& range)
281{
282 return '(' + Foam::name(range.min()) + ',' + Foam::name(range.max()) + ')';
283}
284
285
286// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
287
288} // End namespace Foam
289
290// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
291
292#include "MinMaxI.H"
293
294// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
295
296#endif
297
298// Global Functions and Operators
299#include "MinMaxOps.H"
300
301
302// ************************************************************************* //
scalar range
Global functions and operators related to the MinMax class. Included by MinMax.H.
A min/max value pair with additional methods. In addition to conveniently storing values,...
Definition: MinMax.H:128
T value_type
The value type the MinMax represents.
Definition: MinMax.H:134
bool valid() const
Range is valid if it is not inverted.
Definition: MinMaxI.H:164
bool contains(const T &val) const
True if the value is within the range.
Definition: MinMaxI.H:216
bool empty() const
Range is empty if it is inverted.
Definition: MinMaxI.H:157
const T & clip(const T &val) const
Definition: MinMaxI.H:223
bool inplaceClip(T &val) const
Inplace clip value by the min/max limits.
Definition: MinMaxI.H:242
const T & max() const noexcept
The max value (second)
Definition: MinMaxI.H:121
scalar mag() const
The magnitude of the min to max span. Zero if the range is invalid.
Definition: MinMaxI.H:150
static MinMax< T > le(const T &maxVal)
A semi-infinite range from type min to maxVal.
Definition: MinMaxI.H:38
const T & min() const noexcept
The min value (first)
Definition: MinMaxI.H:107
bool overlaps(const MinMax< T > &b) const
Test if the ranges overlap.
Definition: MinMaxI.H:189
static MinMax< T > ge(const T &minVal)
A semi-infinite range from minVal to the type max.
Definition: MinMaxI.H:31
T centre() const
The min/max average value.
Definition: MinMaxI.H:135
void clear()
Reset to an invalid, inverted range.
Definition: MinMaxI.H:171
pTraits< T >::cmptType cmptType
Component type.
Definition: MinMax.H:137
T span() const
The min to max span. Zero if the range is invalid.
Definition: MinMaxI.H:143
bool intersect(const MinMax< T > &b)
Intersect (union) with the second range.
Definition: MinMaxI.H:179
int compare(const T &val) const
Compares the min/max range with the specified value.
Definition: MinMaxI.H:197
MinMax()
Construct inverted range.
Definition: MinMaxI.H:54
MinMax< T > & add(const MinMax &other)
Extend the range to include the other min/max range.
Definition: MinMaxI.H:263
static MinMax< T > zero_one()
A 0-1 range corresponding to the pTraits zero, one.
Definition: MinMaxI.H:45
An ordered pair of two objects of type <T> with first() and second() elements.
Definition: Pair.H:69
A 2-tuple for storing two objects of dissimilar types. The container is similar in purpose to std::pa...
Definition: Tuple2.H:58
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:94
An edge is a list of two point labels. The functionality it provides supports the discretisation on a...
Definition: edge.H:66
A class for handling words, derived from Foam::string.
Definition: word.H:68
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:63
const volScalarField & T
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))
Namespace for OpenFOAM.
MinMax< label > labelMinMax
A label min/max range.
Definition: MinMax.H:116
MinMax< scalar > scalarMinMax
A scalar min/max range.
Definition: MinMax.H:117
const direction noexcept
Definition: Scalar.H:223
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
volScalarField & b
Definition: createFields.H:27