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 -------------------------------------------------------------------------------
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 \*---------------------------------------------------------------------------*/
27 
28 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
29 
30 template<class T>
31 inline Foam::MinMax<T> Foam::MinMax<T>::ge(const T& minVal)
32 {
33  return MinMax<T>(minVal, pTraits<T>::max);
34 }
35 
36 
37 template<class T>
38 inline Foam::MinMax<T> Foam::MinMax<T>::le(const T& maxVal)
39 {
40  return MinMax<T>(pTraits<T>::min, maxVal);
41 }
42 
43 
44 template<class T>
46 {
48 }
49 
50 
51 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
52 
53 template<class T>
55 :
56  // An inverted range
57  min_(pTraits<T>::max),
58  max_(pTraits<T>::min)
59 {}
60 
61 
62 template<class T>
63 inline Foam::MinMax<T>::MinMax(const T& minVal, const T& maxVal)
64 :
65  min_(minVal),
66  max_(maxVal)
67 {}
68 
69 
70 template<class T>
71 inline Foam::MinMax<T>::MinMax(const std::pair<T,T>& range)
72 :
73  min_(range.first),
74  max_(range.second)
75 {}
76 
77 
78 template<class T>
80 :
81  min_(range.first()),
82  max_(range.second())
83 {}
84 
85 
86 template<class T>
88 :
89  min_(range.first()),
90  max_(range.second())
91 {}
92 
93 
94 template<class T>
96 :
97  min_(pTraits<T>::zero),
98  max_(pTraits<T>::zero)
99 {}
100 
101 
102 template<class T>
104 :
105  min_(pTraits<T>::zero),
106  max_(pTraits<T>::one)
107 {}
108 
109 
110 template<class T>
111 inline Foam::MinMax<T>::MinMax(const T& val)
112 :
113  min_(val),
114  max_(val)
115 {}
116 
117 
118 template<class T>
119 inline Foam::MinMax<T>::MinMax(const UList<T>& vals)
120 :
121  MinMax<T>()
122 {
123  add(vals);
124 }
125 
126 
127 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
128 
129 template<class T>
130 inline T Foam::MinMax<T>::centre() const
131 {
132  // Multiply before adding to avoid possible overflow
133  return (0.5 * min()) + (0.5 * max());
134 }
135 
136 
137 template<class T>
138 inline T Foam::MinMax<T>::span() const
139 {
140  return (empty() ? Zero : (max() - min()));
141 }
142 
143 
144 template<class T>
145 inline Foam::scalar Foam::MinMax<T>::mag() const
146 {
147  return (empty() ? scalar(0) : ::Foam::mag(max() - min()));
148 }
149 
150 
151 template<class T>
152 inline bool Foam::MinMax<T>::empty() const
153 {
154  // Is empty/invalid if (max < min), ie, !(min <= max)
155  return (max() < min());
156 }
157 
158 
159 template<class T>
160 inline bool Foam::MinMax<T>::good() const
161 {
162  return !(max() < min());
163 }
164 
165 
166 template<class T>
167 inline void Foam::MinMax<T>::reset()
168 {
169  // An inverted range
170  min_ = pTraits<T>::max;
171  max_ = pTraits<T>::min;
172 }
173 
174 
175 template<class T>
176 inline void Foam::MinMax<T>::reset(const T& val)
177 {
178  min_ = val;
179  max_ = val;
180 }
181 
182 
183 template<class T>
184 inline void Foam::MinMax<T>::reset(const T& minVal, const T& maxVal)
185 {
186  min_ = minVal;
187  max_ = maxVal;
188 }
189 
190 
191 template<class T>
192 inline bool Foam::MinMax<T>::intersects(const MinMax<T>& b) const
193 {
194  const MinMax<T>& a = *this;
195  return (a.min() < b.max() && b.min() < a.max());
196 }
197 
198 
199 template<class T>
200 inline bool Foam::MinMax<T>::overlaps(const MinMax<T>& b) const
201 {
202  const MinMax<T>& a = *this;
203  return (a.min() <= b.max() && b.min() <= a.max());
204 }
205 
206 
207 template<class T>
208 inline 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  }
220  }
221 
222  return 0;
223 }
224 
225 
226 template<class T>
227 inline bool Foam::MinMax<T>::contains(const T& val) const
228 {
229  return good() && !(val < min() || max() < val);
230 }
231 
232 
233 template<class T>
234 inline T Foam::MinMax<T>::clamp(const T& val) const
235 {
236  if (good())
237  {
238  // Uses Foam::min/Foam::max for component-wise handling
239  return Foam::min(Foam::max(val, min()), max());
240  }
241 
242  return val; // Pass-through
243 }
244 
245 
246 template<class T>
247 inline Foam::MinMax<T>& Foam::MinMax<T>::add(const MinMax& other)
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 
256 template<class T>
257 inline 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 
266 template<class T>
267 inline Foam::MinMax<T>& Foam::MinMax<T>::add(const UList<T>& vals)
268 {
269  for (const T& val : vals)
270  {
271  add(val);
272  }
273  return *this;
274 }
275 
276 
277 template<class T>
278 template<class... Args>
280 (
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 
295 
296 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
297 
298 template<class T>
299 inline bool Foam::MinMax<T>::operator()(const T& val) const
300 {
301  return contains(val);
302 }
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 
336 template<class T>
337 inline Foam::MinMax<T>& Foam::MinMax<T>::operator&=(const MinMax<T>& b)
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 
347 template<class T>
349 {
350  return add(b);
351 }
352 
353 
354 template<class T>
356 {
357  return add(val);
358 }
359 
360 
361 template<class T>
363 {
364  return add(vals);
365 }
366 
367 
368 template<class T>
370 {
371  min() *= s;
372  max() *= s;
373  return *this;
374 }
375 
376 
377 template<class T>
379 {
380  min() /= s;
381  max() /= s;
382  return *this;
383 }
384 
385 
386 // ************************************************************************* //
bool intersects(const MinMax< T > &b) const
Test if the ranges intersect (exclusive check)
Definition: MinMaxI.H:185
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
A 2-tuple for storing two objects of dissimilar types. The container is similar in purpose to std::pa...
Definition: stringOps.H:56
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:40
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
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
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
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
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
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:26
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
void add(FieldField< Field1, typename typeOfSum< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
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
bool operator()(const T &val) const
Identical to contains(), for use as a predicate.
Definition: MinMaxI.H:292
const volScalarField & T
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
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
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
MinMax()
Default construct: an inverted range.
Definition: MinMaxI.H:47
A class representing the concept of 1 (one) that can be used to avoid manipulating objects known to b...
Definition: one.H:56
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127