coord/ops: add max/min vector and element

This commit is contained in:
Danny Robson 2015-03-06 17:49:35 +11:00
parent cbf385b570
commit e69d970e22
2 changed files with 76 additions and 29 deletions

View File

@ -21,6 +21,7 @@
#define __UTIL_COORDS_OPS #define __UTIL_COORDS_OPS
#include "../preprocessor.hpp" #include "../preprocessor.hpp"
#include "../maths.hpp"
#include <cstdlib> #include <cstdlib>
@ -206,6 +207,51 @@ namespace util {
sum += a[i] * b[i]; sum += a[i] * b[i];
return sum; return sum;
} }
//-------------------------------------------------------------------------
template <
size_t S,
typename T,
template <size_t,typename> class K
>
K<S,T>
min (K<S,T> a, K<S,T> b)
{
K<S,T> 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 <size_t,typename> class K
>
K<S,T>
max (K<S,T> a, K<S,T> b)
{
K<S,T> out;
for (size_t i = 0; i < S; ++i)
out[i] = max (a[i], b[i]);
return out;
}
//-------------------------------------------------------------------------
template <size_t S, typename T, template<size_t,typename> class K>
T
min (K<S,T> k)
{ return *std::min_element (k.begin (), k.end ()); }
//-------------------------------------------------------------------------
template <size_t S, typename T, template<size_t,typename> class K>
T
max (K<S,T> k)
{ return *std::max_element (k.begin (), k.end ()); }
} }
#endif #endif

View File

@ -237,44 +237,45 @@ combination [[gnu::pure]] (unsigned n, unsigned k)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/// Variadic minimum /// Variadic minimum
template <typename T> namespace util {
constexpr T template <typename T>
min [[gnu::pure]] (const T a) constexpr T
{ return a; } min [[gnu::pure]] (const T a)
{ return a; }
template <typename T, typename U, typename ...Args> template <typename T, typename U, typename ...Args>
constexpr typename std::enable_if< constexpr typename std::enable_if<
std::is_unsigned<typename std::decay<T>::type>::value == std::is_unsigned<typename std::decay<U>::type>::value && std::is_unsigned<typename std::decay<T>::type>::value == std::is_unsigned<typename std::decay<U>::type>::value &&
std::is_integral<typename std::decay<T>::type>::value == std::is_integral<typename std::decay<U>::type>::value, std::is_integral<typename std::decay<T>::type>::value == std::is_integral<typename std::decay<U>::type>::value,
typename std::common_type<T,U>::type typename std::common_type<T,U>::type
>::type >::type
min [[gnu::pure]] (const T a, const U b, Args ...args) min [[gnu::pure]] (const T a, const U b, Args ...args)
{ {
return min (a < b ? a : b, std::forward<Args> (args)...); return min (a < b ? a : b, std::forward<Args> (args)...);
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/// Variadic maximum /// Variadic maximum
template <typename T> template <typename T>
constexpr T constexpr T
max [[gnu::pure]] (const T a) max [[gnu::pure]] (const T a)
{ return a; } { return a; }
template <typename T, typename U, typename ...Args> template <typename T, typename U, typename ...Args>
constexpr typename std::enable_if< constexpr typename std::enable_if<
std::is_unsigned<typename std::decay<T>::type>::value == std::is_unsigned<typename std::decay<U>::type>::value && std::is_unsigned<typename std::decay<T>::type>::value == std::is_unsigned<typename std::decay<U>::type>::value &&
std::is_integral<typename std::decay<T>::type>::value == std::is_integral<typename std::decay<U>::type>::value, std::is_integral<typename std::decay<T>::type>::value == std::is_integral<typename std::decay<U>::type>::value,
typename std::common_type<T,U>::type typename std::common_type<T,U>::type
>::type >::type
max [[gnu::pure]] (const T a, const U b, Args ...args) max [[gnu::pure]] (const T a, const U b, Args ...args)
{ {
return max (a > b ? a : b, std::forward<Args> (args)...); return max (a > b ? a : b, std::forward<Args> (args)...);
}
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Limiting functions // Limiting functions