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 -------------------------------------------------------------------------------
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>
114 inline T& Foam::MinMax<T>::min() noexcept
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>
128 inline T& Foam::MinMax<T>::max() noexcept
129 {
130  return this->second();
131 }
132 
133 
134 template<class T>
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  return (max() < min());
160 }
161 
162 
163 template<class T>
164 inline bool Foam::MinMax<T>::valid() const
165 {
166  return !(max() < min());
167 }
168 
169 
170 template<class T>
172 {
173  min() = pTraits<T>::max;
174  max() = pTraits<T>::min;
175 }
176 
177 
178 template<class T>
180 {
181  min() = ::Foam::max(min(), b.min());
182  max() = ::Foam::min(max(), b.max());
183 
184  return valid();
185 }
186 
187 
188 template<class T>
189 inline bool Foam::MinMax<T>::overlaps(const MinMax<T>& b) const
190 {
191  const MinMax<T>& a = *this;
192  return (a.min() <= b.max() && b.min() <= a.max());
193 }
194 
195 
196 template<class T>
197 inline int Foam::MinMax<T>::compare(const T& val) const
198 {
199  if (valid())
200  {
201  if (max() < val)
202  {
203  return -1;
204  }
205  else if (val < min())
206  {
207  return 1;
208  }
209  }
210 
211  return 0;
212 }
213 
214 
215 template<class T>
216 inline bool Foam::MinMax<T>::contains(const T& val) const
217 {
218  return (valid() && !compare(val));
219 }
220 
221 
222 template<class T>
223 inline const T& Foam::MinMax<T>::clip(const T& val) const
224 {
225  if (valid())
226  {
227  if (val < min())
228  {
229  return min();
230  }
231  else if (max() < val)
232  {
233  return max();
234  }
235  }
236 
237  return val; // Pass-through
238 }
239 
240 
241 template<class T>
242 inline bool Foam::MinMax<T>::inplaceClip(T& val) const
243 {
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;
255  }
256  }
257 
258  return false; // No change
259 }
260 
261 
262 template<class T>
264 {
265  min() = Foam::min(min(), other.min());
266  max() = Foam::max(max(), other.max());
267  return *this;
268 }
269 
270 
271 template<class T>
273 {
274  min() = Foam::min(min(), val);
275  max() = Foam::max(max(), val);
276  return *this;
277 }
278 
279 
280 template<class T>
282 {
283  for (const T& val : vals)
284  {
285  add(val);
286  }
287  return *this;
288 }
289 
290 
291 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
292 
293 template<class T>
294 inline 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 
331 template<class T>
333 {
334  return add(b);
335 }
336 
337 
338 template<class T>
340 {
341  return add(val);
342 }
343 
344 
345 template<class T>
347 {
348  return add(vals);
349 }
350 
351 
352 template<class T>
354 {
355  min() *= s;
356  max() *= s;
357  return *this;
358 }
359 
360 
361 template<class T>
363 {
364  min() /= s;
365  max() /= s;
366  return *this;
367 }
368 
369 
370 // ************************************************************************* //
s
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))
Definition: gmvOutputSpray.H:25
Foam::MinMax::inplaceClip
bool inplaceClip(T &val) const
Inplace clip value by the min/max limits.
Definition: MinMaxI.H:242
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
Foam::MinMax::max
const T & max() const noexcept
The max value (second)
Definition: MinMaxI.H:121
Foam::MinMax::min
const T & min() const noexcept
The min value (first)
Definition: MinMaxI.H:107
Foam::MinMax::mag
scalar mag() const
The magnitude of the min to max span. Zero if the range is invalid.
Definition: MinMaxI.H:150
Foam::MinMax::contains
bool contains(const T &val) const
True if the value is within the range.
Definition: MinMaxI.H:216
Foam::min
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:33
Foam::MinMax::centre
T centre() const
The min/max average value.
Definition: MinMaxI.H:135
Foam::MinMax::operator/=
MinMax< T > & operator/=(const scalar &s)
Divide range by scalar factor.
Definition: MinMaxI.H:362
Foam::constant::physicoChemical::b
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
Foam::MinMax::overlaps
bool overlaps(const MinMax< T > &b) const
Test if the ranges overlap.
Definition: MinMaxI.H:189
Foam::MinMax::span
T span() const
The min to max span. Zero if the range is invalid.
Definition: MinMaxI.H:143
Foam::MinMax::zero_one
static MinMax< T > zero_one()
A 0-1 range corresponding to the pTraits zero, one.
Definition: MinMaxI.H:45
Foam::MinMax::operator()
bool operator()(const T &val) const
Identical to contains(), for use as a predicate.
Definition: MinMaxI.H:294
Foam::MinMax::operator+=
MinMax< T > & operator+=(const MinMax< T > &b)
Extend min/max range to include other range.
Definition: MinMaxI.H:332
Foam::max
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
Foam::MinMax::clip
const T & clip(const T &val) const
Definition: MinMaxI.H:223
Foam::add
void add(FieldField< Field1, typename typeOfSum< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
Definition: FieldFieldFunctions.C:939
Foam::MinMax::le
static MinMax< T > le(const T &maxVal)
A semi-infinite range from type min to maxVal.
Definition: MinMaxI.H:38
T
const volScalarField & T
Definition: createFieldRefs.H:2
Foam::MinMax::MinMax
MinMax()
Construct inverted range.
Definition: MinMaxI.H:54
Foam::MinMax::intersect
bool intersect(const MinMax< T > &b)
Intersect (union) with the second range.
Definition: MinMaxI.H:179
Foam::MinMax::clear
void clear()
Reset to an invalid, inverted range.
Definition: MinMaxI.H:171
range
scalar range
Definition: LISASMDCalcMethod1.H:12
Foam::Pair
An ordered pair of two objects of type <T> with first() and second() elements.
Definition: Pair.H:54
Foam::MinMax::ge
static MinMax< T > ge(const T &minVal)
A semi-infinite range from minVal to the type max.
Definition: MinMaxI.H:31
Foam::pTraits
A traits class, which is primarily used for primitives.
Definition: pTraits.H:56
Foam::MinMax::add
MinMax< T > & add(const MinMax &other)
Extend the range to include the other min/max range.
Definition: MinMaxI.H:263
Foam::MinMax::operator*=
MinMax< T > & operator*=(const scalar &s)
Multiply range by scalar factor.
Definition: MinMaxI.H:353
Foam::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
Foam::UList
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:103
Foam::MinMax::valid
bool valid() const
Range is valid if it is not inverted.
Definition: MinMaxI.H:164
Foam::MinMax::empty
bool empty() const
Range is empty if it is inverted.
Definition: MinMaxI.H:157
Foam::Tuple2
A 2-tuple for storing two objects of dissimilar types. The container is similar in purpose to std::pa...
Definition: stringOps.H:60
Foam::MinMax
A min/max value pair with additional methods. In addition to conveniently storing values,...
Definition: HashSet.H:76
Foam::MinMax::compare
int compare(const T &val) const
Compares the min/max range with the specified value.
Definition: MinMaxI.H:197
Foam::zero
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:62