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-2023 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  Tuple2<T,T>(pTraits<T>::max, pTraits<T>::min)
57 {}
58 
59 
60 template<class T>
61 inline Foam::MinMax<T>::MinMax(const T& minVal, const T& maxVal)
62 :
63  Tuple2<T,T>(minVal, maxVal)
64 {}
65 
66 
67 template<class T>
68 inline Foam::MinMax<T>::MinMax(const std::pair<T,T>& range)
69 :
70  Tuple2<T,T>(range.first, range.second)
71 {}
72 
73 
74 template<class T>
76 :
77  Tuple2<T,T>(range.first(), range.second())
78 {}
79 
80 
81 template<class T>
83 :
85 {}
86 
87 
88 template<class T>
90 :
91  Tuple2<T,T>(pTraits<T>::zero, pTraits<T>::one)
92 {}
93 
94 
95 template<class T>
96 inline Foam::MinMax<T>::MinMax(const T& val)
97 :
98  Tuple2<T,T>(val, val)
99 {}
100 
101 
102 template<class T>
103 inline Foam::MinMax<T>::MinMax(const UList<T>& vals)
104 :
105  MinMax<T>()
106 {
107  add(vals);
108 }
109 
110 
111 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
112 
113 template<class T>
114 inline const T& Foam::MinMax<T>::min() const noexcept
115 {
116  return this->first();
117 }
118 
119 
120 template<class T>
122 {
123  return this->first();
124 }
125 
126 
127 template<class T>
128 inline const T& Foam::MinMax<T>::max() const noexcept
129 {
130  return this->second();
131 }
132 
133 
134 template<class T>
136 {
137  return this->second();
138 }
139 
140 
141 template<class T>
142 inline T Foam::MinMax<T>::centre() const
143 {
144  // Multiply before adding to avoid possible overflow
145  return (0.5 * min()) + (0.5 * max());
146 }
147 
148 
149 template<class T>
150 inline T Foam::MinMax<T>::span() const
151 {
152  return (empty() ? Zero : (max() - min()));
153 }
154 
155 
156 template<class T>
157 inline Foam::scalar Foam::MinMax<T>::mag() const
158 {
159  return ::Foam::mag(span());
160 }
161 
162 
163 template<class T>
164 inline bool Foam::MinMax<T>::empty() const
165 {
166  // Is empty/invalid if (max < min), ie, !(min <= max)
167  return (max() < min());
168 }
169 
170 
171 template<class T>
172 inline bool Foam::MinMax<T>::good() const
173 {
174  return !empty();
175 }
176 
177 
178 template<class T>
179 inline void Foam::MinMax<T>::reset()
180 {
181  min() = pTraits<T>::max;
182  max() = pTraits<T>::min;
183 }
184 
185 
186 template<class T>
187 inline void Foam::MinMax<T>::reset(const T& val)
188 {
189  min() = val;
190  max() = val;
191 }
192 
193 
194 template<class T>
195 inline void Foam::MinMax<T>::reset(const T& minVal, const T& maxVal)
196 {
197  min() = minVal;
198  max() = maxVal;
199 }
200 
201 
202 template<class T>
203 inline bool Foam::MinMax<T>::intersects(const MinMax<T>& b) const
204 {
205  const MinMax<T>& a = *this;
206  return (a.min() < b.max() && b.min() < a.max());
207 }
208 
209 
210 template<class T>
211 inline bool Foam::MinMax<T>::overlaps(const MinMax<T>& b) const
212 {
213  const MinMax<T>& a = *this;
214  return (a.min() <= b.max() && b.min() <= a.max());
215 }
216 
217 
218 template<class T>
219 inline int Foam::MinMax<T>::compare(const T& val) const
220 {
221  if (good())
222  {
223  if (max() < val)
224  {
225  return -1;
226  }
227  else if (val < min())
228  {
229  return 1;
230  }
231  }
232 
233  return 0;
234 }
235 
236 
237 template<class T>
238 inline bool Foam::MinMax<T>::contains(const T& val) const
239 {
240  return good() && !(val < min() || max() < val);
241 }
242 
243 
244 template<class T>
245 inline T Foam::MinMax<T>::clamp(const T& val) const
246 {
247  if (good())
248  {
249  return Foam::min(Foam::max(val, min()), max());
250  }
251 
252  return val; // Pass-through
253 }
254 
255 
256 template<class T>
257 inline Foam::MinMax<T>& Foam::MinMax<T>::add(const MinMax& other)
258 {
259  min() = Foam::min(min(), other.min());
260  max() = Foam::max(max(), other.max());
261  return *this;
262 }
263 
264 
265 template<class T>
266 inline Foam::MinMax<T>& Foam::MinMax<T>::add(const T& val)
267 {
268  min() = Foam::min(min(), val);
269  max() = Foam::max(max(), val);
270  return *this;
271 }
272 
273 
274 template<class T>
275 inline Foam::MinMax<T>& Foam::MinMax<T>::add(const UList<T>& vals)
276 {
277  for (const T& val : vals)
278  {
279  add(val);
280  }
281  return *this;
282 }
283 
284 
285 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
286 
287 template<class T>
288 inline bool Foam::MinMax<T>::operator()(const T& val) const
289 {
290  return contains(val);
291 }
292 
293 
294 // Perhaps not entirely useful
295 //
296 // template<class T>
297 // inline Foam::MinMax<T>& Foam::MinMax<T>::operator-=
298 // (
299 // const MinMax<T>& b
300 // )
301 // {
302 // MinMax<T>& a = *this;
303 //
304 // // Remove overlapping portion, but cannot create a 'hole' in the middle
305 //
306 // if (a.empty() || b.empty())
307 // {
308 // // Invalid range(s): no-op
309 // }
310 // else if (a.min() < b.max() && b.min() <= a.min())
311 // {
312 // // Overlap on the left
313 // min() = ::Foam::max(a.min(), b.max());
314 // }
315 // else if (b.min() < a.max() && a.max() <= b.max())
316 // {
317 // // Overlap on the right
318 // max() = ::Foam::min(a.max(), b.min());
319 // }
320 //
321 // return *this;
322 // }
323 
324 
325 template<class T>
326 inline Foam::MinMax<T>& Foam::MinMax<T>::operator&=(const MinMax<T>& b)
327 {
328  min() = ::Foam::max(min(), b.min());
329  max() = ::Foam::min(max(), b.max());
330 
331  return *this;
332 }
333 
334 
335 template<class T>
337 {
338  return add(b);
339 }
340 
341 
342 template<class T>
344 {
345  return add(val);
346 }
347 
348 
349 template<class T>
351 {
352  return add(vals);
353 }
354 
355 
356 template<class T>
357 inline Foam::MinMax<T>& Foam::MinMax<T>::operator*=(const scalar& s)
358 {
359  min() *= s;
360  max() *= s;
361  return *this;
362 }
363 
364 
365 template<class T>
366 inline Foam::MinMax<T>& Foam::MinMax<T>::operator/=(const scalar& s)
367 {
368  min() /= s;
369  max() /= s;
370  return *this;
371 }
372 
373 
374 // ************************************************************************* //
bool intersects(const MinMax< T > &b) const
Test if the ranges intersect (exclusive check)
Definition: MinMaxI.H:196
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:54
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:75
scalar range
bool good() const
Range is non-inverted.
Definition: MinMaxI.H:165
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:172
const T & min() const noexcept
The min value (first)
Definition: MinMaxI.H:107
bool empty() const
Range is empty if it is inverted.
Definition: MinMaxI.H:157
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:238
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:135
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
MinMax< T > & operator/=(const scalar &s)
Divide range by scalar factor.
Definition: MinMaxI.H:359
bool overlaps(const MinMax< T > &b) const
Test if ranges overlap/touch (inclusive check)
Definition: MinMaxI.H:204
const direction noexcept
Definition: Scalar.H:258
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:250
bool operator()(const T &val) const
Identical to contains(), for use as a predicate.
Definition: MinMaxI.H:281
const volScalarField & T
Represents 0/1 range or concept. Used for tagged dispatch or clamping.
Definition: pTraits.H:65
int compare(const T &val) const
Compares the min/max range with the specified value.
Definition: MinMaxI.H:212
MinMax< T > & operator+=(const MinMax< T > &b)
Extend min/max range to include other range.
Definition: MinMaxI.H:329
const T & max() const noexcept
The max value (second)
Definition: MinMaxI.H:121
T span() const
The min to max span. Zero if the range is invalid.
Definition: MinMaxI.H:143
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 if the range is invalid.
Definition: MinMaxI.H:150
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))
bool contains(const T &val) const
True if the value is within the range (inclusive check)
Definition: MinMaxI.H:231
MinMax()
Construct 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
MinMax< T > & operator*=(const scalar &s)
Multiply range by scalar factor.
Definition: MinMaxI.H:350
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127