From e69d970e220f5f1402ad2f934fcb71eb9141cf9e Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Fri, 6 Mar 2015 17:49:35 +1100 Subject: [PATCH] coord/ops: add max/min vector and element --- coord/ops.hpp | 46 +++++++++++++++++++++++++++++++++++++++ maths.hpp | 59 ++++++++++++++++++++++++++------------------------- 2 files changed, 76 insertions(+), 29 deletions(-) diff --git a/coord/ops.hpp b/coord/ops.hpp index f54e2039..5179c398 100644 --- a/coord/ops.hpp +++ b/coord/ops.hpp @@ -21,6 +21,7 @@ #define __UTIL_COORDS_OPS #include "../preprocessor.hpp" +#include "../maths.hpp" #include @@ -206,6 +207,51 @@ namespace util { sum += a[i] * b[i]; return sum; } + + //------------------------------------------------------------------------- + template < + size_t S, + typename T, + template class K + > + K + min (K a, K b) + { + K out; + for (size_t i = 0; i < S; ++i) + out[i] = min (a[i], b[i]); + return out; + } + + + //------------------------------------------------------------------------- + template < + size_t S, + typename T, + template class K + > + K + max (K a, K b) + { + K out; + for (size_t i = 0; i < S; ++i) + out[i] = max (a[i], b[i]); + return out; + } + + + //------------------------------------------------------------------------- + template class K> + T + min (K k) + { return *std::min_element (k.begin (), k.end ()); } + + + //------------------------------------------------------------------------- + template class K> + T + max (K k) + { return *std::max_element (k.begin (), k.end ()); } } #endif diff --git a/maths.hpp b/maths.hpp index d5f54b1c..b5bbf50e 100644 --- a/maths.hpp +++ b/maths.hpp @@ -237,44 +237,45 @@ combination [[gnu::pure]] (unsigned n, unsigned k) //----------------------------------------------------------------------------- /// Variadic minimum -template -constexpr T -min [[gnu::pure]] (const T a) - { return a; } +namespace util { + template + constexpr T + min [[gnu::pure]] (const T a) + { return a; } -template -constexpr typename std::enable_if< - std::is_unsigned::type>::value == std::is_unsigned::type>::value && - std::is_integral::type>::value == std::is_integral::type>::value, - typename std::common_type::type ->::type -min [[gnu::pure]] (const T a, const U b, Args ...args) -{ - return min (a < b ? a : b, std::forward (args)...); -} + template + constexpr typename std::enable_if< + std::is_unsigned::type>::value == std::is_unsigned::type>::value && + std::is_integral::type>::value == std::is_integral::type>::value, + typename std::common_type::type + >::type + min [[gnu::pure]] (const T a, const U b, Args ...args) + { + return min (a < b ? a : b, std::forward (args)...); + } //----------------------------------------------------------------------------- -/// Variadic maximum -template -constexpr T -max [[gnu::pure]] (const T a) - { return a; } + /// Variadic maximum + template + constexpr T + max [[gnu::pure]] (const T a) + { return a; } -template -constexpr typename std::enable_if< - std::is_unsigned::type>::value == std::is_unsigned::type>::value && - std::is_integral::type>::value == std::is_integral::type>::value, - typename std::common_type::type ->::type -max [[gnu::pure]] (const T a, const U b, Args ...args) -{ - return max (a > b ? a : b, std::forward (args)...); + template + constexpr typename std::enable_if< + std::is_unsigned::type>::value == std::is_unsigned::type>::value && + std::is_integral::type>::value == std::is_integral::type>::value, + typename std::common_type::type + >::type + max [[gnu::pure]] (const T a, const U b, Args ...args) + { + return max (a > b ? a : b, std::forward (args)...); + } } - //----------------------------------------------------------------------------- // Limiting functions