|
1 | 1 | #ifndef LIBIM_MATH_H
|
2 | 2 | #define LIBIM_MATH_H
|
| 3 | +#include <algorithm> |
3 | 4 | #include <cmath>
|
4 | 5 | #include <cfloat>
|
5 | 6 | #include <type_traits>
|
| 7 | +#include <utility> |
6 | 8 |
|
7 | 9 | namespace libim {
|
8 | 10 |
|
@@ -31,6 +33,83 @@ namespace libim {
|
31 | 33 | }
|
32 | 34 | }
|
33 | 35 |
|
| 36 | + /** clamp overload for arithmetic types (numbers) for std::clamp because r-value references can cause UB */ |
| 37 | + template<class T, typename = std::enable_if_t<std::is_arithmetic_v<std::remove_reference_t<T>>>> |
| 38 | + [[nodiscard]] constexpr inline T clamp(T v, T lo, T hi) { |
| 39 | + return std::clamp(v, lo, hi); |
| 40 | + } |
| 41 | + |
| 42 | + template<class T, typename = std::enable_if_t<!std::is_arithmetic_v<std::remove_reference_t<T>>>> |
| 43 | + [[nodiscard]] constexpr inline const T& clamp(const T& v, const T& lo, const T& hi) { |
| 44 | + return std::clamp(v, lo, hi); |
| 45 | + } |
| 46 | + |
| 47 | + template<class T, typename = std::enable_if_t<!std::is_arithmetic_v<std::remove_reference_t<T>>>> |
| 48 | + [[nodiscard]] constexpr inline T& clamp(T& v, T& lo, T& hi) { |
| 49 | + return const_cast<T&>(std::clamp(v, lo, hi)); |
| 50 | + } |
| 51 | + |
| 52 | + template<class T, typename = std::enable_if_t<!std::is_arithmetic_v<std::remove_reference_t<T>>>> |
| 53 | + T&& clamp(T&&, T&&, T&&) = delete; |
| 54 | + |
| 55 | + /** min overload for arithmetic types (numbers) for std::min because r-value references can cause UB */ |
| 56 | + template<typename T, typename = std::enable_if_t<std::is_arithmetic_v<std::remove_reference_t<T>>>> |
| 57 | + [[nodiscard]] constexpr inline T min(T a, T b) { |
| 58 | + return std::min({ a, b }); |
| 59 | + } |
| 60 | + |
| 61 | + template<typename T, typename = std::enable_if_t<!std::is_arithmetic_v<std::remove_reference_t<T>>>> |
| 62 | + [[nodiscard]] constexpr inline const T& min(const T& a, const T& b ) { |
| 63 | + return std::min(a, b); |
| 64 | + } |
| 65 | + |
| 66 | + template<typename T, typename = std::enable_if_t<!std::is_arithmetic_v<std::remove_reference_t<T>>>> |
| 67 | + [[nodiscard]] constexpr inline T& min(T& a, T& b ) { |
| 68 | + return const_cast<T&>(std::min(a, b)); |
| 69 | + } |
| 70 | + |
| 71 | + template<typename T, typename = std::enable_if_t<!std::is_arithmetic_v<std::remove_reference_t<T>>>> |
| 72 | + [[nodiscard]] constexpr inline T&& min(T&&, T&&) = delete; |
| 73 | + |
| 74 | + /** max overload for arithmetic types (numbers) for std::max because r-value references can cause UB */ |
| 75 | + template<typename T, typename = std::enable_if_t<std::is_arithmetic_v<std::remove_reference_t<T>>>> |
| 76 | + [[nodiscard]] constexpr inline T max(T a, T b) { |
| 77 | + return std::max({ a, b }); |
| 78 | + } |
| 79 | + |
| 80 | + template<typename T, typename = std::enable_if_t<!std::is_arithmetic_v<std::remove_reference_t<T>>>> |
| 81 | + [[nodiscard]] constexpr inline const T& max(const T& a, const T& b ) { |
| 82 | + return std::max(a, b); |
| 83 | + } |
| 84 | + |
| 85 | + template<typename T, typename = std::enable_if_t<!std::is_arithmetic_v<std::remove_reference_t<T>>>> |
| 86 | + [[nodiscard]] constexpr inline T& max(T& a, T& b ) { |
| 87 | + return const_cast<T&>(std::max(a, b)); |
| 88 | + } |
| 89 | + |
| 90 | + template<typename T, typename = std::enable_if_t<!std::is_arithmetic_v<std::remove_reference_t<T>>>> |
| 91 | + T&& max(T&&, T&&) = delete; |
| 92 | + |
| 93 | + /** minmax overload for arithmetic types (numbers) for std::minmax because r-value references can cause UB */ |
| 94 | + template<typename T, typename = std::enable_if_t<std::is_arithmetic_v<std::remove_reference_t<T>>>> |
| 95 | + [[nodiscard]] constexpr inline std::pair<T, T> minmax(T a, T b) { |
| 96 | + return std::minmax({ a, b }); |
| 97 | + } |
| 98 | + |
| 99 | + template<typename T, typename = std::enable_if_t<!std::is_arithmetic_v<std::remove_reference_t<T>>>> |
| 100 | + [[nodiscard]] constexpr inline std::pair<const T&,const T&> minmax(const T& a, const T& b ) { |
| 101 | + return std::minmax(a, b); |
| 102 | + } |
| 103 | + |
| 104 | + template<typename T, typename = std::enable_if_t<!std::is_arithmetic_v<std::remove_reference_t<T>>>> |
| 105 | + [[nodiscard]] constexpr inline std::pair<T&,T&> minmax(T& a, T& b) { |
| 106 | + auto p = std::minmax(a, b); |
| 107 | + return { const_cast<T&>(p.first), const_cast<T&>(p.second) }; |
| 108 | + } |
| 109 | + |
| 110 | + template<typename T, typename = std::enable_if_t<!std::is_arithmetic_v<std::remove_reference_t<T>>>> |
| 111 | + std::pair<T&&, T&&> minmax(T&&, T&&) = delete; |
| 112 | + |
34 | 113 |
|
35 | 114 | template<typename T, typename = std::enable_if<std::is_floating_point_v<T>>>
|
36 | 115 | static constexpr T epsilon = std::numeric_limits<T>::epsilon();
|
|
0 commit comments