ENH: add compareOp for three-way comparison

- similar to the \c <=> operator in C++20.
  Primarily for use when defining cascaded sort function objects.
This commit is contained in:
Mark Olesen
2018-10-17 09:29:28 +02:00
parent 8b569b16d1
commit e0255cfff2
3 changed files with 95 additions and 8 deletions

View File

@ -405,6 +405,28 @@ inline Scalar stabilise(const Scalar s, const Scalar tol)
// Specializations
// Default definition in ops.H
template<class T> struct compareOp;
//- Compare scalar values
template<>
struct compareOp<Scalar>
{
const Scalar tolerance;
//- Construct with specified tolerance (non-negative value)
compareOp(Scalar tol = ScalarVSMALL)
:
tolerance(tol)
{}
Scalar operator()(const Scalar& a, const Scalar& b) const
{
return (mag(a - b) <= tolerance) ? 0 : (a - b);
}
};
// Default definition in ops.H
template<class T> struct equalOp;

View File

@ -216,6 +216,21 @@ WeightedOp(multiply, (weight*y))
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
//- Three-way comparison operation of two parameters,
// similar to the \c <=> operator in C++20.
//
// \return a negative value for less, a positive value for greater,
// and zero for equal value.
template<class T>
struct compareOp
{
int operator()(const T& a, const T& b) const WARNRETURN
{
return (a < b) ? -1 : (b < a) ? 1 : 0;
}
};
//- General get operation to extract the 'name' from an object as a word.
// The default implementation uses the 'name()' method commonly used within
// OpenFOAM.