extent: make area and diameter constexpr

This commit is contained in:
Danny Robson 2016-09-28 17:21:02 +10:00
parent 657a407d0e
commit 66f0bbc025
3 changed files with 34 additions and 30 deletions

View File

@ -42,34 +42,6 @@ extent<S,T>::extent (vector<S,T> _v)
} }
///////////////////////////////////////////////////////////////////////////////
template <size_t S, typename T>
T
extent<S,T>::diameter (void) const
{
return static_cast<T> (
std::sqrt (
std::accumulate (std::begin (this->data),
std::end (this->data),
T {0},
[] (auto a, auto b) { return a + b * b; })
)
);
}
//-----------------------------------------------------------------------------
template <size_t S, typename T>
T
extent<S,T>::area (void) const
{
return std::accumulate (std::begin (this->data),
std::end (this->data),
T {1},
std::multiplies<T> ());
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
template <size_t S, typename T> template <size_t S, typename T>
extent<S,T> extent<S,T>

View File

@ -34,10 +34,11 @@ namespace util {
extent () = default; extent () = default;
explicit extent (vector<S,T>); explicit extent (vector<S,T>);
T area (void) const; constexpr T area (void) const;
T diameter (void) const; constexpr T diameter (void) const;
template <typename U = float> template <typename U = float>
constexpr
U aspect (void) const; U aspect (void) const;
template <typename U> template <typename U>

View File

@ -24,9 +24,40 @@
#include <algorithm> #include <algorithm>
///////////////////////////////////////////////////////////////////////////////
template <size_t S, typename T>
constexpr
T
util::extent<S,T>::diameter (void) const
{
return static_cast<T> (
std::sqrt (
std::accumulate (std::begin (this->data),
std::end (this->data),
T {0},
[] (auto a, auto b) { return a + b * b; })
)
);
}
///////////////////////////////////////////////////////////////////////////////
template <size_t S, typename T>
constexpr
T
util::extent<S,T>::area (void) const
{
return std::accumulate (std::begin (this->data),
std::end (this->data),
T {1},
std::multiplies<T> ());
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
template <size_t S, typename T> template <size_t S, typename T>
template <typename U> template <typename U>
constexpr
U U
util::extent<S,T>::aspect (void) const util::extent<S,T>::aspect (void) const
{ {