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-2022 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>
89 inline Foam::MinMax<T>::MinMax(const T& val)
90 :
91  Tuple2<T,T>(val, val)
92 {}
93 
94 
95 template<class T>
96 inline Foam::MinMax<T>::MinMax(const UList<T>& vals)
97 :
98  MinMax<T>()
99 {
100  add(vals);
101 }
102 
103 
104 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
105 
106 template<class T>
107 inline const T& Foam::MinMax<T>::min() const noexcept
108 {
109  return this->first();
110 }
111 
112 
113 template<class T>
115 {
116  return this->first();
117 }
118 
119 
120 template<class T>
121 inline const T& Foam::MinMax<T>::max() const noexcept
122 {
123  return this->second();
124 }
125 
126 
127 template<class T>
129 {
130  return this->second();
131 }
132 
133 
134 template<class T>
135 inline T Foam::MinMax<T>::centre() const
136 {
137  // Multiply before adding to avoid possible overflow
138  return (0.5 * min()) + (0.5 * max());
139 }
140 
141 
142 template<class T>
143 inline T Foam::MinMax<T>::span() const
144 {
145  return (empty() ? Zero : (max() - min()));
146 }
147 
148 
149 template<class T>
150 inline Foam::scalar Foam::MinMax<T>::mag() const
151 {
152  return ::Foam::mag(span());
153 }
154 
155 
156 template<class T>
157 inline bool Foam::MinMax<T>::empty() const
158 {
159  // Is empty/invalid if (max < min), ie, !(min <= max)
160  return (max() < min());
161 }
162 
163 
164 template<class T>
165 inline bool Foam::MinMax<T>::good() const
166 {
167  return !empty();
168 }
169 
170 
171 template<class T>
172 inline void Foam::MinMax<T>::reset()
173 {
174  min() = pTraits<T>::max;
175  max() = pTraits<T>::min;
176 }
177 
178 
179 template<class T>
180 inline void Foam::MinMax<T>::reset(const T& val)
181 {
182  min() = val;
183  max() = val;
184 }
185 
186 
187 template<class T>
188 inline void Foam::MinMax<T>::reset(const T& minVal, const T& maxVal)
189 {
190  min() = minVal;
191  max() = maxVal;
192 }
193 
194 
195 template<class T>
196 inline bool Foam::MinMax<T>::intersects(const MinMax<T>& b) const
197 {
198  const MinMax<T>& a = *this;
199  return (a.min() < b.max() && b.min() < a.max());
200 }
201 
202 
203 template<class T>
204 inline bool Foam::MinMax<T>::overlaps(const MinMax<T>& b) const
205 {
206  const MinMax<T>& a = *this;
207  return (a.min() <= b.max() && b.min() <= a.max());
208 }
209 
210 
211 template<class T>
212 inline int Foam::MinMax<T>::compare(const T& val) const
213 {
214  if (good())
215  {
216  if (max() < val)
217  {
218  return -1;
219  }
220  else if (val < min())
221  {
222  return 1;
223  }
224  }
225 
226  return 0;
227 }
228 
229 
230 template<class T>
231 inline bool Foam::MinMax<T>::contains(const T& val) const
232 {
233  return good() && !(val < min() || max() < val);
234 }
235 
236 
237 template<class T>
238 inline const T& Foam::MinMax<T>::clip(const T& val) const
239 {
240  if (good())
241  {
242  if (val < min())
243  {
244  return min();
245  }
246  else if (max() < val)
247  {
248  return max();
249  }
250  }
251 
252  return val; // Pass-through
253 }
254 
255 
256 template<class T>
257 inline bool Foam::MinMax<T>::inplaceClip(T& val) const
258 {
259  if (good())
260  {
261  if (val < min())
262  {
263  val = min();
264  return true;
265  }
266  else if (max() < val)
267  {
268  val = max();
269  return true;
270  }
271  }
272 
273  return false; // No change
274 }
275 
276 
277 template<class T>
278 inline Foam::MinMax<T>& Foam::MinMax<T>::add(const MinMax& other)
279 {
280  min() = Foam::min(min(), other.min());
281  max() = Foam::max(max(), other.max());
282  return *this;
283 }
284 
285 
286 template<class T>
287 inline Foam::MinMax<T>& Foam::MinMax<T>::add(const T& val)
288 {
289  min() = Foam::min(min(), val);
290  max() = Foam::max(max(), val);
291  return *this;
292 }
293 
294 
295 template<class T>
296 inline Foam::MinMax<T>& Foam::MinMax<T>::add(const UList<T>& vals)
297 {
298  for (const T& val : vals)
299  {
300  add(val);
301  }
302  return *this;
303 }
304 
305 
306 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
307 
308 template<class T>
309 inline bool Foam::MinMax<T>::operator()(const T& val) const
310 {
311  return contains(val);
312 }
313 
314 
315 // Perhaps not entirely useful
316 //
317 // template<class T>
318 // inline Foam::MinMax<T>& Foam::MinMax<T>::operator-=
319 // (
320 // const MinMax<T>& b
321 // )
322 // {
323 // MinMax<T>& a = *this;
324 //
325 // // Remove overlapping portion, but cannot create a 'hole' in the middle
326 //
327 // if (a.empty() || b.empty())
328 // {
329 // // Invalid range(s): no-op
330 // }
331 // else if (a.min() < b.max() && b.min() <= a.min())
332 // {
333 // // Overlap on the left
334 // min() = ::Foam::max(a.min(), b.max());
335 // }
336 // else if (b.min() < a.max() && a.max() <= b.max())
337 // {
338 // // Overlap on the right
339 // max() = ::Foam::min(a.max(), b.min());
340 // }
341 //
342 // return *this;
343 // }
344 
345 
346 template<class T>
347 inline Foam::MinMax<T>& Foam::MinMax<T>::operator&=(const MinMax<T>& b)
348 {
349  min() = ::Foam::max(min(), b.min());
350  max() = ::Foam::min(max(), b.max());
351 
352  return *this;
353 }
354 
355 
356 template<class T>
358 {
359  return add(b);
360 }
361 
362 
363 template<class T>
365 {
366  return add(val);
367 }
368 
369 
370 template<class T>
372 {
373  return add(vals);
374 }
375 
376 
377 template<class T>
378 inline Foam::MinMax<T>& Foam::MinMax<T>::operator*=(const scalar& s)
379 {
380  min() *= s;
381  max() *= s;
382  return *this;
383 }
384 
385 
386 template<class T>
387 inline Foam::MinMax<T>& Foam::MinMax<T>::operator/=(const scalar& s)
388 {
389  min() /= s;
390  max() /= s;
391  return *this;
392 }
393 
394 
395 // ************************************************************************* //
bool intersects(const MinMax< T > &b) const
Test if the ranges intersect (exclusive check)
Definition: MinMaxI.H:189
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:55
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.
Definition: pTraits.H:50
const T & clip(const T &val) const
If out of range, return the respective min/max limits, otherwise return the value itself...
Definition: MinMaxI.H:231
scalar range
bool good() const
Range is non-inverted.
Definition: MinMaxI.H:158
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:165
const T & min() const noexcept
The min value (first)
Definition: MinMaxI.H:100
bool empty() const
Range is empty if it is inverted.
Definition: MinMaxI.H:150
An ordered pair of two objects of type <T> with first() and second() elements.
Definition: instant.H:46
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
bool inplaceClip(T &val) const
Inplace clip value by the min/max limits.
Definition: MinMaxI.H:250
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:128
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:99
MinMax< T > & operator/=(const scalar &s)
Divide range by scalar factor.
Definition: MinMaxI.H:380
bool overlaps(const MinMax< T > &b) const
Test if ranges overlap/touch (inclusive check)
Definition: MinMaxI.H:197
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:271
bool operator()(const T &val) const
Identical to contains(), for use as a predicate.
Definition: MinMaxI.H:302
const volScalarField & T
int compare(const T &val) const
Compares the min/max range with the specified value.
Definition: MinMaxI.H:205
MinMax< T > & operator+=(const MinMax< T > &b)
Extend min/max range to include other range.
Definition: MinMaxI.H:350
const T & max() const noexcept
The max value (second)
Definition: MinMaxI.H:114
T span() const
The min to max span. Zero if the range is invalid.
Definition: MinMaxI.H:136
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:58
scalar mag() const
The magnitude of the min to max span. Zero if the range is invalid.
Definition: MinMaxI.H:143
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:224
MinMax()
Construct inverted range.
Definition: MinMaxI.H:47
MinMax< T > & operator*=(const scalar &s)
Multiply range by scalar factor.
Definition: MinMaxI.H:371
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:157