ENH: additional MinMax span() and zero_one() methods

This commit is contained in:
Mark Olesen
2019-05-08 12:12:00 +02:00
committed by Andrew Heather
parent ac317699d8
commit 0f1fcb97b5
3 changed files with 35 additions and 4 deletions

View File

@ -42,7 +42,7 @@ using namespace Foam;
template<class T> template<class T>
Ostream& printInfo(const MinMax<T>& range) Ostream& printInfo(const MinMax<T>& range)
{ {
Info<< range << " valid=" << range.valid(); Info<< range << " valid=" << range.valid() << " span=" << range.span();
return Info; return Info;
} }
@ -84,6 +84,12 @@ int main(int argc, char *argv[])
Info<<"Construct range : "; Info<<"Construct range : ";
printInfo(MinMax<scalar>(1, 20)) << nl; printInfo(MinMax<scalar>(1, 20)) << nl;
Info<<"A 0-1 scalar range : ";
printInfo(scalarMinMax::zero_one()) << nl;
Info<<"A 0-1 vector range : ";
printInfo(MinMax<vector>::zero_one()) << nl;
{ {
scalarMinMax range1(10, 20); scalarMinMax range1(10, 20);

View File

@ -159,6 +159,12 @@ public:
inline explicit MinMax(const UList<T>& vals); inline explicit MinMax(const UList<T>& vals);
// Static Member Functions
//- A 0-1 range corresponding to the pTraits zero, one
inline static MinMax<T> zero_one();
// Member Functions // Member Functions
// Access // Access
@ -178,6 +184,9 @@ public:
//- The min/max average value //- The min/max average value
inline T centre() const; inline T centre() const;
//- The min to max span. Zero if the range is invalid.
inline T span() const;
//- The magnitude of the min to max span. Zero if the range is invalid. //- The magnitude of the min to max span. Zero if the range is invalid.
inline scalar mag() const; inline scalar mag() const;
@ -195,10 +204,10 @@ public:
//- Intersect (union) with the second range. //- Intersect (union) with the second range.
// \return True if the resulting intersection is non-empty. // \return True if the resulting intersection is non-empty.
bool intersect(const MinMax<T>& b); inline bool intersect(const MinMax<T>& b);
//- Test if the ranges overlap //- Test if the ranges overlap
bool overlaps(const MinMax<T>& b) const; inline bool overlaps(const MinMax<T>& b) const;
//- Compares the min/max range with the specified value. //- Compares the min/max range with the specified value.
// \return // \return

View File

@ -23,6 +23,15 @@ License
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
template<class T>
inline Foam::MinMax<T> Foam::MinMax<T>::zero_one()
{
return MinMax<T>(pTraits<T>::zero, pTraits<T>::one);
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class T> template<class T>
@ -114,10 +123,17 @@ inline T Foam::MinMax<T>::centre() const
} }
template<class T>
inline T Foam::MinMax<T>::span() const
{
return (empty() ? Zero : (max() - min()));
}
template<class T> template<class T>
inline Foam::scalar Foam::MinMax<T>::mag() const inline Foam::scalar Foam::MinMax<T>::mag() const
{ {
return (empty() ? Zero : ::Foam::mag(max() - min())); return ::Foam::mag(span());
} }