From 0f1fcb97b55d8550d14b0d097eb09b87c083391e Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Wed, 8 May 2019 12:12:00 +0200 Subject: [PATCH] ENH: additional MinMax span() and zero_one() methods --- applications/test/minMax1/Test-minMax1.C | 8 +++++++- src/OpenFOAM/primitives/ranges/MinMax/MinMax.H | 13 +++++++++++-- .../primitives/ranges/MinMax/MinMaxI.H | 18 +++++++++++++++++- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/applications/test/minMax1/Test-minMax1.C b/applications/test/minMax1/Test-minMax1.C index a33b3a3134..26e43aecf0 100644 --- a/applications/test/minMax1/Test-minMax1.C +++ b/applications/test/minMax1/Test-minMax1.C @@ -42,7 +42,7 @@ using namespace Foam; template Ostream& printInfo(const MinMax& range) { - Info<< range << " valid=" << range.valid(); + Info<< range << " valid=" << range.valid() << " span=" << range.span(); return Info; } @@ -84,6 +84,12 @@ int main(int argc, char *argv[]) Info<<"Construct range : "; printInfo(MinMax(1, 20)) << nl; + Info<<"A 0-1 scalar range : "; + printInfo(scalarMinMax::zero_one()) << nl; + + Info<<"A 0-1 vector range : "; + printInfo(MinMax::zero_one()) << nl; + { scalarMinMax range1(10, 20); diff --git a/src/OpenFOAM/primitives/ranges/MinMax/MinMax.H b/src/OpenFOAM/primitives/ranges/MinMax/MinMax.H index 1cd0243075..59b1f16201 100644 --- a/src/OpenFOAM/primitives/ranges/MinMax/MinMax.H +++ b/src/OpenFOAM/primitives/ranges/MinMax/MinMax.H @@ -159,6 +159,12 @@ public: inline explicit MinMax(const UList& vals); + // Static Member Functions + + //- A 0-1 range corresponding to the pTraits zero, one + inline static MinMax zero_one(); + + // Member Functions // Access @@ -178,6 +184,9 @@ public: //- The min/max average value 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. inline scalar mag() const; @@ -195,10 +204,10 @@ public: //- Intersect (union) with the second range. // \return True if the resulting intersection is non-empty. - bool intersect(const MinMax& b); + inline bool intersect(const MinMax& b); //- Test if the ranges overlap - bool overlaps(const MinMax& b) const; + inline bool overlaps(const MinMax& b) const; //- Compares the min/max range with the specified value. // \return diff --git a/src/OpenFOAM/primitives/ranges/MinMax/MinMaxI.H b/src/OpenFOAM/primitives/ranges/MinMax/MinMaxI.H index 83033baf14..afdf34916d 100644 --- a/src/OpenFOAM/primitives/ranges/MinMax/MinMaxI.H +++ b/src/OpenFOAM/primitives/ranges/MinMax/MinMaxI.H @@ -23,6 +23,15 @@ License \*---------------------------------------------------------------------------*/ +// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // + +template +inline Foam::MinMax Foam::MinMax::zero_one() +{ + return MinMax(pTraits::zero, pTraits::one); +} + + // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // template @@ -114,10 +123,17 @@ inline T Foam::MinMax::centre() const } +template +inline T Foam::MinMax::span() const +{ + return (empty() ? Zero : (max() - min())); +} + + template inline Foam::scalar Foam::MinMax::mag() const { - return (empty() ? Zero : ::Foam::mag(max() - min())); + return ::Foam::mag(span()); }