mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
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:
@ -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;
|
||||
|
||||
|
||||
@ -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.
|
||||
|
||||
Reference in New Issue
Block a user