MinMaxI.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
26\*---------------------------------------------------------------------------*/
27
28// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
29
30template<class T>
32{
33 return MinMax<T>(minVal, pTraits<T>::max);
34}
35
36
37template<class T>
39{
40 return MinMax<T>(pTraits<T>::min, maxVal);
41}
42
43
44template<class T>
46{
48}
49
50
51// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
52
53template<class T>
55:
57{}
58
59
60template<class T>
61inline Foam::MinMax<T>::MinMax(const T& minVal, const T& maxVal)
62:
63 Tuple2<T,T>(minVal, maxVal)
64{}
65
66
67template<class T>
68inline Foam::MinMax<T>::MinMax(const std::pair<T,T>& range)
69:
70 Tuple2<T,T>(range.first, range.second)
71{}
72
73
74template<class T>
76:
77 Tuple2<T,T>(range.first(), range.second())
78{}
79
80
81template<class T>
83:
85{}
86
87
88template<class T>
89inline Foam::MinMax<T>::MinMax(const T& val)
90:
91 Tuple2<T,T>(val, val)
92{}
93
94
95template<class T>
97:
98 MinMax<T>()
99{
100 add(vals);
101}
102
103
104// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
105
106template<class T>
107inline const T& Foam::MinMax<T>::min() const noexcept
108{
109 return this->first();
110}
111
112
113template<class T>
115{
116 return this->first();
117}
118
119
120template<class T>
121inline const T& Foam::MinMax<T>::max() const noexcept
122{
123 return this->second();
124}
125
126
127template<class T>
129{
130 return this->second();
131}
132
133
134template<class T>
136{
137 // Multiply before adding to avoid possible overflow
138 return (0.5 * min()) + (0.5 * max());
139}
140
141
142template<class T>
144{
145 return (empty() ? Zero : (max() - min()));
147
148
149template<class T>
150inline Foam::scalar Foam::MinMax<T>::mag() const
151{
152 return ::Foam::mag(span());
153}
154
156template<class T>
157inline bool Foam::MinMax<T>::empty() const
159 return (max() < min());
160}
162
163template<class T>
164inline bool Foam::MinMax<T>::valid() const
165{
166 return !(max() < min());
168
169
170template<class T>
172{
175}
176
177
178template<class T>
180{
182 max() = ::Foam::min(max(), b.max());
183
184 return valid();
185}
186
188template<class T>
189inline bool Foam::MinMax<T>::overlaps(const MinMax<T>& b) const
191 const MinMax<T>& a = *this;
192 return (a.min() <= b.max() && b.min() <= a.max());
194
195
196template<class T>
197inline int Foam::MinMax<T>::compare(const T& val) const
198{
199 if (valid())
200 {
201 if (max() < val)
203 return -1;
204 }
205 else if (val < min())
206 {
207 return 1;
209 }
210
211 return 0;
212}
213
214
215template<class T>
216inline bool Foam::MinMax<T>::contains(const T& val) const
217{
218 return (valid() && !compare(val));
219}
220
221
222template<class T>
223inline const T& Foam::MinMax<T>::clip(const T& val) const
224{
225 if (valid())
226 {
227 if (val < min())
229 return min();
230 }
231 else if (max() < val)
232 {
233 return max();
234 }
235 }
236
237 return val; // Pass-through
238}
239
240
241template<class T>
242inline bool Foam::MinMax<T>::inplaceClip(T& val) const
244 if (valid())
245 {
246 if (val < min())
247 {
248 val = min();
249 return true;
250 }
251 else if (max() < val)
252 {
253 val = max();
254 return true;
256 }
257
258 return false; // No change
260
261
262template<class T>
264{
265 min() = Foam::min(min(), other.min());
266 max() = Foam::max(max(), other.max());
267 return *this;
269
270
271template<class T>
273{
274 min() = Foam::min(min(), val);
275 max() = Foam::max(max(), val);
276 return *this;
277}
278
279
280template<class T>
282{
283 for (const T& val : vals)
284 {
285 add(val);
286 }
287 return *this;
288}
289
290
291// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
292
293template<class T>
294inline bool Foam::MinMax<T>::operator()(const T& val) const
295{
296 return contains(val);
297}
298
299
300// Perhaps not entirely useful
301//
302// template<class T>
303// inline Foam::MinMax<T>& Foam::MinMax<T>::operator-=
304// (
305// const MinMax<T>& b
306// )
307// {
308// MinMax<T>& a = *this;
309//
310// // Remove overlapping portion, but cannot create a 'hole' in the middle
311//
312// if (!a.valid() || !b.valid())
313// {
314// // Invalid range(s): no-op
315// }
316// else if (a.min() < b.max() && b.min() <= a.min())
317// {
318// // Overlap on the left
319// min() = ::Foam::max(a.min(), b.max());
320// }
321// else if (b.min() < a.max() && a.max() <= b.max())
322// {
323// // Overlap on the right
324// max() = ::Foam::min(a.max(), b.min());
325// }
326//
327// return *this;
328// }
329
330
331template<class T>
333{
334 return add(b);
335}
336
337
338template<class T>
340{
341 return add(val);
342}
343
344
345template<class T>
347{
348 return add(vals);
349}
350
351
352template<class T>
354{
355 min() *= s;
356 max() *= s;
357 return *this;
358}
359
360
361template<class T>
363{
364 min() /= s;
365 max() /= s;
366 return *this;
367}
368
369
370// ************************************************************************* //
scalar range
Y[inertIndex] max(0.0)
void max(const dimensioned< Type > &dt)
Use the maximum of the field and specified value.
void min(const dimensioned< Type > &dt)
Use the minimum of the field and specified value.
A min/max value pair with additional methods. In addition to conveniently storing values,...
Definition: MinMax.H:128
MinMax< T > & operator*=(const scalar &s)
Multiply range by scalar factor.
Definition: MinMaxI.H:353
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
MinMax< T > & operator/=(const scalar &s)
Divide range by scalar factor.
Definition: MinMaxI.H:362
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
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
MinMax< T > & operator+=(const MinMax< T > &b)
Extend min/max range to include other range.
Definition: MinMaxI.H:332
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
int overlaps
Flag to control which overlap calculations are performed.
Definition: PDRparams.H:97
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
Ostream & operator()() const
Output stream (master only).
Definition: ensightCaseI.H:74
Sums a given list of (at least two or more) fields and outputs the result into a new field,...
Definition: add.H:161
A class representing the concept of 1 (one) that can be used to avoid manipulating objects known to b...
Definition: one.H:62
A traits class, which is primarily used for primitives.
Definition: pTraits.H:59
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))
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
void add(FieldField< Field1, typename typeOfSum< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:33
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
const direction noexcept
Definition: Scalar.H:223
volScalarField & b
Definition: createFields.H:27
A non-counting (dummy) refCount.
Definition: refCount.H:59