Loading...
Searching...
No Matches
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-2025 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:
56 // An inverted range
57 min_(pTraits<T>::max),
58 max_(pTraits<T>::min)
59{}
60
61
62template<class T>
63inline Foam::MinMax<T>::MinMax(const T& minVal, const T& maxVal)
65 min_(minVal),
66 max_(maxVal)
67{}
68
69
70template<class T>
71inline Foam::MinMax<T>::MinMax(const std::pair<T,T>& range)
73 min_(range.first),
74 max_(range.second)
75{}
76
77
78template<class T>
81 min_(range.first()),
82 max_(range.second())
83{}
84
85
86template<class T>
89 min_(range.first()),
90 max_(range.second())
91{}
92
93
94template<class T>
97 min_(pTraits<T>::zero),
98 max_(pTraits<T>::zero)
99{}
100
101
102template<class T>
105 min_(pTraits<T>::zero),
106 max_(pTraits<T>::one)
107{}
108
109
110template<class T>
111inline Foam::MinMax<T>::MinMax(const T& val)
113 min_(val),
114 max_(val)
115{}
116
117
118template<class T>
119inline Foam::MinMax<T>::MinMax(const UList<T>& vals)
120:
121 MinMax<T>()
122{
123 add(vals);
124}
125
126
127// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
128
129template<class T>
130inline T Foam::MinMax<T>::centre() const
132 // Multiply before adding to avoid possible overflow
133 return (0.5 * min()) + (0.5 * max());
134}
135
136
137template<class T>
140 return (empty() ? Zero : (max() - min()));
141}
142
143
144template<class T>
145inline Foam::scalar Foam::MinMax<T>::mag() const
146{
147 return (empty() ? scalar(0) : ::Foam::mag(max() - min()));
148}
149
150
151template<class T>
152inline bool Foam::MinMax<T>::empty() const
154 // Is empty/invalid if (max < min), ie, !(min <= max)
155 return (max() < min());
156}
157
158
159template<class T>
160inline bool Foam::MinMax<T>::good() const
161{
162 return !(max() < min());
163}
164
165
166template<class T>
167inline void Foam::MinMax<T>::reset()
168{
169 // An inverted range
170 min_ = pTraits<T>::max;
171 max_ = pTraits<T>::min;
172}
173
174
175template<class T>
176inline void Foam::MinMax<T>::reset(const T& val)
178 min_ = val;
179 max_ = val;
180}
181
182
183template<class T>
184inline void Foam::MinMax<T>::reset(const T& minVal, const T& maxVal)
186 min_ = minVal;
187 max_ = maxVal;
188}
189
190
191template<class T>
192inline bool Foam::MinMax<T>::intersects(const MinMax<T>& b) const
194 const MinMax<T>& a = *this;
195 return (a.min() < b.max() && b.min() < a.max());
196}
198
199template<class T>
200inline bool Foam::MinMax<T>::overlaps(const MinMax<T>& b) const
202 const MinMax<T>& a = *this;
203 return (a.min() <= b.max() && b.min() <= a.max());
204}
205
206
207template<class T>
208inline int Foam::MinMax<T>::compare(const T& val) const
209{
210 if (good())
211 {
212 if (max() < val)
213 {
214 return -1;
215 }
216 else if (val < min())
217 {
218 return 1;
219 }
221
222 return 0;
223}
224
225
226template<class T>
227inline bool Foam::MinMax<T>::contains(const T& val) const
228{
229 return good() && !(val < min() || max() < val);
230}
231
233template<class T>
234inline T Foam::MinMax<T>::clamp(const T& val) const
235{
236 if (good())
238 // Uses Foam::min/Foam::max for component-wise handling
239 return Foam::min(Foam::max(val, min()), max());
241
242 return val; // Pass-through
243}
244
245
246template<class T>
248{
249 // Uses Foam::min/Foam::max for component-wise handling
250 min() = Foam::min(min(), other.min());
251 max() = Foam::max(max(), other.max());
252 return *this;
253}
254
255
256template<class T>
257inline Foam::MinMax<T>& Foam::MinMax<T>::add(const T& val)
258{
259 // Uses Foam::min/Foam::max for component-wise handling
260 min() = Foam::min(min(), val);
261 max() = Foam::max(max(), val);
262 return *this;
263}
264
265
266template<class T>
268{
269 for (const T& val : vals)
270 {
271 add(val);
273 return *this;
274}
276
277template<class T>
278template<class... Args>
281 const T& val0,
282 const T& val1,
283 Args&&... values
284)
285{
286 add(val0);
287 add(val1);
288
289 // Add multiple values via fold expression
290 ((*this += std::forward<Args>(values)), ...);
291
292 return *this;
293}
294
296// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
297
298template<class T>
299inline bool Foam::MinMax<T>::operator()(const T& val) const
300{
301 return contains(val);
303
304
305// Perhaps not entirely useful
306//
307// template<class T>
308// inline Foam::MinMax<T>& Foam::MinMax<T>::operator-=
309// (
310// const MinMax<T>& b
311// )
312// {
313// MinMax<T>& a = *this;
314//
315// // Remove overlapping portion, but cannot create a 'hole' in the middle
316//
317// if (a.empty() || b.empty())
318// {
319// // Invalid range(s): no-op
320// }
321// else if (a.min() < b.max() && b.min() <= a.min())
322// {
323// // Overlap on the left
324// min() = ::Foam::max(a.min(), b.max());
325// }
326// else if (b.min() < a.max() && a.max() <= b.max())
327// {
328// // Overlap on the right
329// max() = ::Foam::min(a.max(), b.min());
330// }
331//
332// return *this;
333// }
334
335
336template<class T>
338{
339 // Uses Foam::min/Foam::max for component-wise handling
340 min() = ::Foam::max(min(), b.min());
341 max() = ::Foam::min(max(), b.max());
342
343 return *this;
344}
345
346
347template<class T>
349{
350 return add(b);
351}
352
353
354template<class T>
356{
357 return add(val);
358}
359
360
361template<class T>
364 return add(vals);
365}
366
367
368template<class T>
370{
371 min() *= s;
372 max() *= s;
373 return *this;
374}
375
376
377template<class T>
379{
380 min() /= s;
381 max() /= s;
382 return *this;
383}
384
385
386// ************************************************************************* //
scalar range
A min/max value pair with additional methods. In addition to conveniently storing values,...
Definition MinMax.H:106
bool operator()(const T &val) const
Identical to contains(), for use as a predicate.
Definition MinMaxI.H:292
MinMax< T > & operator&=(const MinMax< T > &b)
Restrict min/max range to union with other range.
Definition MinMaxI.H:330
bool contains(const T &val) const
True if the value is within the range (inclusive check).
Definition MinMaxI.H:220
bool empty() const
Range is empty if it is inverted.
Definition MinMaxI.H:145
T clamp(const T &val) const
Return value clamped component-wise.
Definition MinMaxI.H:227
MinMax< T > & operator/=(scalar s)
Divide range by scalar factor.
Definition MinMaxI.H:371
const T & max() const noexcept
The max value.
Definition MinMax.H:217
MinMax< T > & operator*=(scalar s)
Multiply range by scalar factor.
Definition MinMaxI.H:362
scalar mag() const
The magnitude of the min to max span. Zero for invalid range.
Definition MinMaxI.H:138
static MinMax< T > le(const T &maxVal)
A semi-infinite range from type min to maxVal.
Definition MinMaxI.H:31
const T & min() const noexcept
The min value.
Definition MinMax.H:207
bool overlaps(const MinMax< T > &b) const
Test if ranges overlap/touch (inclusive check).
Definition MinMaxI.H:193
static MinMax< T > ge(const T &minVal)
A semi-infinite range from minVal to the type max.
Definition MinMaxI.H:24
T centre() const
The min/max average value.
Definition MinMaxI.H:123
bool good() const
Range is non-inverted.
Definition MinMaxI.H:153
void reset()
Reset to an inverted (invalid) range.
Definition MinMaxI.H:160
MinMax< T > & operator+=(const MinMax< T > &b)
Extend min/max range to include other range.
Definition MinMaxI.H:341
T span() const
The min to max span. Zero for invalid range.
Definition MinMaxI.H:131
int compare(const T &val) const
Compares the min/max range with the specified value.
Definition MinMaxI.H:201
MinMax()
Default construct: an inverted range.
Definition MinMaxI.H:47
bool intersects(const MinMax< T > &b) const
Test if the ranges intersect (exclusive check).
Definition MinMaxI.H:185
MinMax< T > & add(const MinMax &other)
Extend the range to include the other min/max range.
Definition MinMaxI.H:240
static MinMax< T > zero_one()
A 0-1 range corresponding to the pTraits zero, one.
Definition MinMaxI.H:38
An ordered pair of two objects of type <T> with first() and second() elements.
Definition Pair.H:66
A 2-tuple for storing two objects of dissimilar types. The container is similar in purpose to std::pa...
Definition Tuple2.H:51
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:89
A class representing the concept of 1 (one) that can be used to avoid manipulating objects known to b...
Definition one.H:57
A traits class, which is primarily used for primitives and vector-space.
Definition pTraits.H:64
Represents 0/1 range or concept. Used for tagged dispatch or clamping.
Definition pTraits.H:51
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition zero.H:58
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:40
void add(DimensionedField< scalar, GeoMesh > &result, const dimensioned< scalar > &dt1, const DimensionedField< scalar, GeoMesh > &f2)
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition hashSets.C:26
static constexpr const zero Zero
Global zero (0).
Definition zero.H:127
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
volScalarField & b