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
#include "../preprocessor.hpp"
#include "../maths.hpp"
#include <cstdlib>
@ -206,6 +207,51 @@ namespace util {
sum += a[i] * b[i];
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

View File

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