MinMax< T > Class Template Reference

A min/max value pair with additional methods. In addition to conveniently storing values, it can be used for logic operations or to modify data. A few global functions and functors are also provided. More...

Inheritance diagram for MinMax< T >:
Collaboration diagram for MinMax< T >:

Public Types

typedef T value_type
 The value type the MinMax represents. More...
 
typedef pTraits< T >::cmptType cmptType
 Component type. More...
 
- Public Types inherited from Tuple2< T, T >
typedef T first_type
 Type of member first, the first template parameter (T1) More...
 
typedef T second_type
 Type of member second, the second template parameter (T2) More...
 

Public Member Functions

 MinMax ()
 Construct inverted range. More...
 
 MinMax (const T &minVal, const T &maxVal)
 Copy construct from components. More...
 
 MinMax (const std::pair< T, T > &range)
 Copy construct from components. More...
 
 MinMax (const Pair< T > &range)
 Copy construct from components. More...
 
 MinMax (const Foam::zero)
 Construct with a single zero value. More...
 
 MinMax (const Foam::zero_one)
 Implicit construct from zero_one as 0-1 range (pTraits zero, one) More...
 
 MinMax (const T &val)
 Construct with a single initial value. More...
 
 MinMax (const UList< T > &vals)
 Construct from list of values. More...
 
const Tmin () const noexcept
 The min value (first) More...
 
Tmin () noexcept
 The min value (first) More...
 
const Tmax () const noexcept
 The max value (second) More...
 
Tmax () noexcept
 The max value (second) More...
 
T centre () const
 The min/max average value. More...
 
T span () const
 The min to max span. Zero if the range is invalid. More...
 
scalar mag () const
 The magnitude of the min to max span. Zero if the range is invalid. More...
 
bool empty () const
 Range is empty if it is inverted. More...
 
bool good () const
 Range is non-inverted. More...
 
bool valid () const
 Range is non-inverted. More...
 
void reset ()
 Reset to an inverted (invalid) range. More...
 
void reset (const T &val)
 Reset min/max to be identical to the specified value. More...
 
void reset (const T &minVal, const T &maxVal)
 Reset min/max to specified values. More...
 
void clear ()
 Same as reset() - reset to an inverted (invalid) range. More...
 
bool intersects (const MinMax< T > &b) const
 Test if the ranges intersect (exclusive check) More...
 
bool overlaps (const MinMax< T > &b) const
 Test if ranges overlap/touch (inclusive check) More...
 
int compare (const T &val) const
 Compares the min/max range with the specified value. More...
 
bool contains (const T &val) const
 True if the value is within the range (inclusive check) More...
 
T clamp (const T &val) const
 Return value clamped component-wise. More...
 
MinMax< T > & add (const MinMax &other)
 Extend the range to include the other min/max range. More...
 
MinMax< T > & add (const T &val)
 Include the value into the range. More...
 
MinMax< T > & add (const UList< T > &vals)
 Include the values into the range. More...
 
bool operator() (const T &val) const
 Identical to contains(), for use as a predicate. More...
 
MinMax< T > & operator &= (const MinMax< T > &b)
 Restrict min/max range to union with other range. More...
 
MinMax< T > & operator+= (const MinMax< T > &b)
 Extend min/max range to include other range. More...
 
MinMax< T > & operator+= (const T &val)
 Extend min/max range to include value. More...
 
MinMax< T > & operator+= (const UList< T > &vals)
 Extend min/max range to include all values. More...
 
MinMax< T > & operator*= (const scalar &s)
 Multiply range by scalar factor. More...
 
MinMax< T > & operator/= (const scalar &s)
 Divide range by scalar factor. More...
 
T clip (const T &val) const
 Old method name. Same as clamp (2023-01) More...
 
void inplaceClip (T &val) const
 Old method (2023-01) More...
 
- Public Member Functions inherited from Tuple2< T, T >
 Tuple2 ()=default
 Default construct. More...
 
 Tuple2 (const T &f, const T &s)
 Copy construct from components. More...
 
 Tuple2 (T &&f, T &&s)
 Move construct from components. More...
 
 Tuple2 (const std::pair< T, T > &vals)
 Copy construct from std::pair. More...
 
 Tuple2 (std::pair< T, T > &&vals)
 Move construct from std::pair. More...
 
 Tuple2 (Istream &is)
 Construct from Istream. More...
 
const Tfirst () const noexcept
 Access the first element. More...
 
Tfirst () noexcept
 Access the first element. More...
 
const Tsecond () const noexcept
 Access the second element. More...
 
Tsecond () noexcept
 Access the second element. More...
 

Static Public Member Functions

static MinMax< Tge (const T &minVal)
 A semi-infinite range from minVal to the type max. More...
 
static MinMax< Tle (const T &maxVal)
 A semi-infinite range from type min to maxVal. More...
 
static MinMax< Tzero_one ()
 A 0-1 range corresponding to the pTraits zero, one. More...
 

Detailed Description

template<class T>
class Foam::MinMax< T >

A min/max value pair with additional methods. In addition to conveniently storing values, it can be used for logic operations or to modify data. A few global functions and functors are also provided.

Examples of use.

Determine min/max limits from a List of values:

List<scalar> values = ...;

// on construction
MinMax<scalar> range(values);

range.reset();

range += val;

// global minMax() function
Info<< minMax(values) << nl;

General comparison operations

scalar val;
if (val < range) ... value is below range min
if (range.contains(val)) ... value within range
if (range.compare(val) > 0) ... value is above range max
if (range(val)) ... value within range - as predicate

Since the range has a predicate form, it can be used as a filter method. For example,

Info<< "values in range: " << subsetList(values, range) << nl;

boolList mask = ListOps::create<bool>(values, range);
Info<< "values values in range " << mask << nl;

One advantage offered by MinMax is to clamp or limit values to a particular range. For example,

scalarMinMax range(lower, upper);

scalar val;
val = range.clamp(val)    .. return clamped values

// vs.
val = min(max(value, lower), upper)

Definition at line 72 of file HashSet.H.

Member Typedef Documentation

◆ value_type

typedef T value_type

The value type the MinMax represents.

Definition at line 116 of file MinMax.H.

◆ cmptType

Component type.

Definition at line 121 of file MinMax.H.

Constructor & Destructor Documentation

◆ MinMax() [1/8]

MinMax ( )
inline

Construct inverted range.

Definition at line 47 of file MinMaxI.H.

◆ MinMax() [2/8]

MinMax ( const T minVal,
const T maxVal 
)
inline

Copy construct from components.

Definition at line 54 of file MinMaxI.H.

◆ MinMax() [3/8]

MinMax ( const std::pair< T, T > &  range)
inline

Copy construct from components.

Definition at line 61 of file MinMaxI.H.

◆ MinMax() [4/8]

MinMax ( const Pair< T > &  range)
inline

Copy construct from components.

Definition at line 68 of file MinMaxI.H.

◆ MinMax() [5/8]

MinMax ( const Foam::zero  )
inlineexplicit

Construct with a single zero value.

Definition at line 75 of file MinMaxI.H.

◆ MinMax() [6/8]

MinMax ( const Foam::zero_one  )
inline

Implicit construct from zero_one as 0-1 range (pTraits zero, one)

Definition at line 82 of file MinMaxI.H.

◆ MinMax() [7/8]

MinMax ( const T val)
inlineexplicit

Construct with a single initial value.

Definition at line 89 of file MinMaxI.H.

◆ MinMax() [8/8]

MinMax ( const UList< T > &  vals)
inlineexplicit

Construct from list of values.

Definition at line 96 of file MinMaxI.H.

Member Function Documentation

◆ ge()

Foam::MinMax< T > ge ( const T minVal)
inlinestatic

A semi-infinite range from minVal to the type max.

Definition at line 24 of file MinMaxI.H.

Referenced by singleDirectionUniformBin::read(), writeFile::read(), externalCoupled::read(), and STDMD::read().

Here is the caller graph for this function:

◆ le()

Foam::MinMax< T > le ( const T maxVal)
inlinestatic

A semi-infinite range from type min to maxVal.

Definition at line 31 of file MinMaxI.H.

◆ zero_one()

Foam::MinMax< T > zero_one ( )
inlinestatic

A 0-1 range corresponding to the pTraits zero, one.

Definition at line 38 of file MinMaxI.H.

◆ min() [1/2]

◆ min() [2/2]

T & min ( )
inlinenoexcept

The min value (first)

Definition at line 114 of file MinMaxI.H.

◆ max() [1/2]

◆ max() [2/2]

T & max ( )
inlinenoexcept

The max value (second)

Definition at line 128 of file MinMaxI.H.

◆ centre()

T centre ( ) const
inline

The min/max average value.

Definition at line 135 of file MinMaxI.H.

◆ span()

T span ( ) const
inline

The min to max span. Zero if the range is invalid.

Definition at line 143 of file MinMaxI.H.

Referenced by singleDirectionUniformBin::initialise(), and equalBinWidth::write().

Here is the caller graph for this function:

◆ mag()

Foam::scalar mag ( ) const
inline

The magnitude of the min to max span. Zero if the range is invalid.

Definition at line 150 of file MinMaxI.H.

◆ empty()

bool empty ( ) const
inline

Range is empty if it is inverted.

Definition at line 157 of file MinMaxI.H.

◆ good()

bool good ( ) const
inline

Range is non-inverted.

Definition at line 165 of file MinMaxI.H.

Referenced by singleDirectionUniformBin::initialise(), MinMax< Foam::vector >::valid(), and equalBinWidth::write().

Here is the caller graph for this function:

◆ valid()

bool valid ( ) const
inline

Range is non-inverted.

Definition at line 237 of file MinMax.H.

◆ reset() [1/3]

void reset ( )
inline

Reset to an inverted (invalid) range.

Definition at line 172 of file MinMaxI.H.

Referenced by MinMax< Foam::vector >::clear(), boundBox::intersects(), and equalBinWidth::read().

Here is the caller graph for this function:

◆ reset() [2/3]

void reset ( const T val)
inline

Reset min/max to be identical to the specified value.

Definition at line 180 of file MinMaxI.H.

◆ reset() [3/3]

void reset ( const T minVal,
const T maxVal 
)
inline

Reset min/max to specified values.

Definition at line 188 of file MinMaxI.H.

◆ clear()

void clear ( )
inline

Same as reset() - reset to an inverted (invalid) range.

Definition at line 257 of file MinMax.H.

◆ intersects()

bool intersects ( const MinMax< T > &  b) const
inline

Test if the ranges intersect (exclusive check)

Definition at line 196 of file MinMaxI.H.

◆ overlaps()

bool overlaps ( const MinMax< T > &  b) const
inline

Test if ranges overlap/touch (inclusive check)

Definition at line 204 of file MinMaxI.H.

Referenced by boundBox::intersects().

Here is the caller graph for this function:

◆ compare()

int compare ( const T val) const
inline

Compares the min/max range with the specified value.

Returns
  • 0: value is within the range, or range is invalid
  • -1: range (max) is less than the value
  • +1: range (min) is greater than value

Definition at line 212 of file MinMaxI.H.

◆ contains()

bool contains ( const T val) const
inline

True if the value is within the range (inclusive check)

Definition at line 231 of file MinMaxI.H.

◆ clamp()

T clamp ( const T val) const
inline

Return value clamped component-wise.

If the range is invalid, just returns the value.

Definition at line 238 of file MinMaxI.H.

Referenced by MinMax< Foam::vector >::clip(), and MinMax< Foam::vector >::inplaceClip().

Here is the caller graph for this function:

◆ add() [1/3]

Foam::MinMax< T > & add ( const MinMax< T > &  other)
inline

Extend the range to include the other min/max range.

Definition at line 250 of file MinMaxI.H.

Referenced by PDRblock::location::edgeLimits(), singleDirectionUniformBin::initialise(), boundBox::intersects(), and Foam::minMaxMag().

Here is the caller graph for this function:

◆ add() [2/3]

Foam::MinMax< T > & add ( const T val)
inline

Include the value into the range.

Definition at line 259 of file MinMaxI.H.

◆ add() [3/3]

Foam::MinMax< T > & add ( const UList< T > &  vals)
inline

Include the values into the range.

Definition at line 268 of file MinMaxI.H.

◆ operator()()

bool operator() ( const T val) const
inline

Identical to contains(), for use as a predicate.

Definition at line 281 of file MinMaxI.H.

◆ operator &=()

MinMax<T>& operator&= ( const MinMax< T > &  b)
inline

Restrict min/max range to union with other range.

◆ operator+=() [1/3]

Foam::MinMax< T > & operator+= ( const MinMax< T > &  b)
inline

Extend min/max range to include other range.

Can be used in a reduction operation.

Definition at line 329 of file MinMaxI.H.

◆ operator+=() [2/3]

Foam::MinMax< T > & operator+= ( const T val)
inline

Extend min/max range to include value.

Definition at line 336 of file MinMaxI.H.

◆ operator+=() [3/3]

Foam::MinMax< T > & operator+= ( const UList< T > &  vals)
inline

Extend min/max range to include all values.

Definition at line 343 of file MinMaxI.H.

◆ operator*=()

Foam::MinMax< T > & operator*= ( const scalar &  s)
inline

Multiply range by scalar factor.

Definition at line 350 of file MinMaxI.H.

◆ operator/=()

Foam::MinMax< T > & operator/= ( const scalar &  s)
inline

Divide range by scalar factor.

Definition at line 359 of file MinMaxI.H.

◆ clip()

T clip ( const T val) const
inline

Old method name. Same as clamp (2023-01)

Definition at line 358 of file MinMax.H.

◆ inplaceClip()

void inplaceClip ( T val) const
inline

Old method (2023-01)

Definition at line 363 of file MinMax.H.


The documentation for this class was generated from the following files: