Add min(), max() for any number of arguments

This commit is contained in:
Martchus 2018-05-05 23:06:51 +02:00
parent c26e84ff8c
commit 034b8a75d1
2 changed files with 38 additions and 0 deletions

View File

@ -12,6 +12,31 @@ CPP_UTILITIES_EXPORT int factorial(int number);
CPP_UTILITIES_EXPORT uint64 powerModulo(uint64 base, uint64 expontent, uint64 module); CPP_UTILITIES_EXPORT uint64 powerModulo(uint64 base, uint64 expontent, uint64 module);
CPP_UTILITIES_EXPORT int64 inverseModulo(int64 number, int64 module); CPP_UTILITIES_EXPORT int64 inverseModulo(int64 number, int64 module);
CPP_UTILITIES_EXPORT uint64 orderModulo(uint64 number, uint64 module); CPP_UTILITIES_EXPORT uint64 orderModulo(uint64 number, uint64 module);
/// \brief Returns the smallest of the given items.
template <typename T> constexpr T min(T first, T second)
{
return first < second ? first : second;
}
/// \brief Returns the smallest of the given items.
template <typename T1, typename... T2> constexpr T1 min(T1 first, T1 second, T2... remaining)
{
return first < second ? min(first, remaining...) : min(second, remaining...);
}
/// \brief Returns the greatest of the given items.
template <typename T> constexpr T max(T first, T second)
{
return first > second ? first : second;
}
/// \brief Returns the greatest of the given items.
template <typename T1, typename... T2> constexpr T1 max(T1 first, T1 second, T2... remaining)
{
return first > second ? max(first, remaining...) : max(second, remaining...);
}
} // namespace MathUtilities } // namespace MathUtilities
#endif // MATHUTILITIES_H #endif // MATHUTILITIES_H

View File

@ -10,6 +10,19 @@ using namespace TestUtilities::Literals;
using namespace CPPUNIT_NS; using namespace CPPUNIT_NS;
namespace MathUtilities {
static_assert(min(1, 2, 3) == 1, "min");
static_assert(min(3, 2, 1) == 1, "min");
static_assert(min(3, 4, 2, 1) == 1, "min");
static_assert(min(3, 4, -2, 2, 1) == -2, "min");
static_assert(max(1, 2, 3) == 3, "max");
static_assert(max(3, 2, 1) == 3, "max");
static_assert(max(3, 4, 2, 1) == 4, "max");
static_assert(max(3, -2, 4, 2, 1) == 4, "max");
} // namespace MathUtilities
/*! /*!
* \brief The MathTests class tests functions of the MathUtilities namespace. * \brief The MathTests class tests functions of the MathUtilities namespace.
*/ */