Skip to content
Snippets Groups Projects
Commit 3f8c99c8 authored by Mark OLESEN's avatar Mark OLESEN
Browse files

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

parent 9d8899ca
No related branches found
No related tags found
No related merge requests found
...@@ -42,7 +42,7 @@ using namespace Foam; ...@@ -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[]) ...@@ -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);
......
...@@ -159,6 +159,12 @@ public: ...@@ -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: ...@@ -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: ...@@ -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
......
...@@ -23,6 +23,15 @@ License ...@@ -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 ...@@ -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());
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment