From d0b72f75365b172848cfd0f037d5c5876fe2ed08 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Tue, 24 Nov 2020 04:07:39 +0700 Subject: [PATCH] vector zip_fold operator== --- headers/vmath.hpp/vmath_fun.hpp | 4 +- headers/vmath.hpp/vmath_vec_fun.hpp | 110 ++++++++++++++++------------ 2 files changed, 65 insertions(+), 49 deletions(-) diff --git a/headers/vmath.hpp/vmath_fun.hpp b/headers/vmath.hpp/vmath_fun.hpp index 7992d8b..3ea4c4a 100644 --- a/headers/vmath.hpp/vmath_fun.hpp +++ b/headers/vmath.hpp/vmath_fun.hpp @@ -136,8 +136,8 @@ namespace vmath_hpp } template < typename T > - T distance(T p0, T p1) noexcept { - return length(p0 - p1); + T distance(T x, T y) noexcept { + return length(x - y); } template < typename T > diff --git a/headers/vmath.hpp/vmath_vec_fun.hpp b/headers/vmath.hpp/vmath_vec_fun.hpp index 9cad825..bc77b64 100644 --- a/headers/vmath.hpp/vmath_vec_fun.hpp +++ b/headers/vmath.hpp/vmath_vec_fun.hpp @@ -16,10 +16,10 @@ namespace vmath_hpp::detail namespace impl { template < typename A, std::size_t Size, typename F, std::size_t... Is > - constexpr auto map_impl(F&& f, const vec& v, std::index_sequence) + constexpr auto map_impl(F&& f, const vec& a, std::index_sequence) -> vec, Size> { - return { f(v[Is])... }; + return { f(a[Is])... }; } template < typename A, typename B, std::size_t Size, typename F, std::size_t... Is > @@ -37,16 +37,23 @@ namespace vmath_hpp::detail } template < typename A, typename B, std::size_t Size, typename F, std::size_t... Is > - constexpr auto fold_impl(F&& f, A init, const vec& v, std::index_sequence) + constexpr auto fold_impl(F&& f, A init, const vec& b, std::index_sequence) -> A { - return ((init = f(std::move(init), v[Is])), ...); + return ((init = f(std::move(init), b[Is])), ...); + } + + template < typename A, typename B, typename C, std::size_t Size, typename F, std::size_t... Is > + constexpr auto fold_impl(F&& f, A init, const vec& b, const vec& c, std::index_sequence) + -> A + { + return ((init = f(std::move(init), b[Is], c[Is])), ...); } } - template < typename T, std::size_t Size, typename F > - constexpr auto map(F&& f, const vec& v) { - return impl::map_impl(std::forward(f), v, std::make_index_sequence{}); + template < typename A, std::size_t Size, typename F > + constexpr auto map(F&& f, const vec& a) { + return impl::map_impl(std::forward(f), a, std::make_index_sequence{}); } template < typename A, typename B, std::size_t Size, typename F > @@ -60,8 +67,13 @@ namespace vmath_hpp::detail } template < typename A, typename B, std::size_t Size, typename F > - constexpr auto fold(F&& f, A init, const vec& v) { - return impl::fold_impl(std::forward(f), std::move(init), v, std::make_index_sequence{}); + constexpr auto fold(F&& f, A init, const vec& b) { + return impl::fold_impl(std::forward(f), std::move(init), b, std::make_index_sequence{}); + } + + template < typename A, typename B, typename C, std::size_t Size, typename F > + constexpr auto fold(F&& f, A init, const vec& b, const vec& c) { + return impl::fold_impl(std::forward(f), std::move(init), b, c, std::make_index_sequence{}); } } @@ -150,23 +162,27 @@ namespace vmath_hpp template < typename T, std::size_t Size > constexpr bool operator==(const vec& xs, const vec& ys) { - return all(zip(std::equal_to<>(), xs, ys)); + return fold([](bool acc, T x, T y){ + return acc && (x == y); + }, true, xs, ys); } template < typename T, std::size_t Size > constexpr bool operator!=(const vec& xs, const vec& ys) { - return any(zip(std::not_equal_to<>(), xs, ys)); + return fold([](bool acc, T x, T y){ + return acc || (x != y); + }, false, xs, ys); } // operator< template < typename T, std::size_t Size > - constexpr bool operator<(const vec& l, const vec& r) { + constexpr bool operator<(const vec& xs, const vec& ys) { for ( std::size_t i = 0; i < Size; ++i ) { - if ( l[i] < r[i] ) { + if ( xs[i] < ys[i] ) { return true; } - if ( r[i] < l[i] ) { + if ( ys[i] < xs[i] ) { return false; } } @@ -473,45 +489,45 @@ namespace vmath_hpp namespace vmath_hpp { template < typename T, std::size_t Size > - T length(const vec& x) noexcept { - return sqrt(dot(x, x)); + T length(const vec& xs) { + return sqrt(dot(xs, xs)); } template < typename T, std::size_t Size > - T distance(const vec& p0, const vec& p1) noexcept { - return length(p0 - p1); + T distance(const vec& xs, const vec& ys) { + return length(xs - ys); } template < typename T, std::size_t Size > - constexpr T dot(const vec& x, const vec& y) noexcept { - return fold(std::plus<>(), T(0), zip(std::multiplies<>(), x, y)); + constexpr T dot(const vec& xs, const vec& ys) { + return fold(std::plus<>(), T(0), zip(std::multiplies<>(), xs, ys)); } template < typename T > - constexpr vec cross(const vec& x, const vec& y) noexcept { + constexpr vec cross(const vec& xs, const vec& ys) { return { - x.y * y.z - x.z * y.y, - x.z * y.x - x.x * y.z, - x.x * y.y - x.y * y.x}; + xs.y * ys.z - xs.z * ys.y, + xs.z * ys.x - xs.x * ys.z, + xs.x * ys.y - xs.y * ys.x}; } template < typename T, std::size_t Size > - vec normalize(const vec& x) noexcept { - return x * invsqrt(dot(x, x)); + vec normalize(const vec& xs) { + return xs * invsqrt(dot(xs, xs)); } template < typename T, std::size_t Size > - vec faceforward(const vec& n, const vec& i, const vec& nref) noexcept { + vec faceforward(const vec& n, const vec& i, const vec& nref) { return dot(nref, i) < T(0) ? n : -n; } template < typename T, std::size_t Size > - vec reflect(const vec& i, const vec& n) noexcept { + vec reflect(const vec& i, const vec& n) { return i - n * dot(n, i) * T(2); } template < typename T, std::size_t Size > - vec refract(const vec& i, const vec& n, T eta) noexcept { + vec refract(const vec& i, const vec& n, T eta) { const T d = dot(n, i); const T k = T(1) - eta * eta * (T(1) - d * d); return T(k >= T(0)) * (eta * i - (eta * d + sqrt(k)) * n); @@ -525,47 +541,47 @@ namespace vmath_hpp namespace vmath_hpp { template < typename T, std::size_t Size > - constexpr vec less(const vec& x, const vec& y) { - return zip(std::less<>(), x, y); + constexpr vec less(const vec& xs, const vec& ys) { + return zip(std::less<>(), xs, ys); } template < typename T, std::size_t Size > - constexpr vec less_equal(const vec& x, const vec& y) { - return zip(std::less_equal<>(), x, y); + constexpr vec less_equal(const vec& xs, const vec& ys) { + return zip(std::less_equal<>(), xs, ys); } template < typename T, std::size_t Size > - constexpr vec greater(const vec& x, const vec& y) { - return zip(std::greater<>(), x, y); + constexpr vec greater(const vec& xs, const vec& ys) { + return zip(std::greater<>(), xs, ys); } template < typename T, std::size_t Size > - constexpr vec greater_equal(const vec& x, const vec& y) { - return zip(std::greater_equal<>(), x, y); + constexpr vec greater_equal(const vec& xs, const vec& ys) { + return zip(std::greater_equal<>(), xs, ys); } template < typename T, std::size_t Size > - constexpr vec equal_to(const vec& x, const vec& y) { - return zip(std::equal_to<>(), x, y); + constexpr vec equal_to(const vec& xs, const vec& ys) { + return zip(std::equal_to<>(), xs, ys); } template < typename T, std::size_t Size > - constexpr vec not_equal_to(const vec& x, const vec& y) { - return zip(std::not_equal_to<>(), x, y); + constexpr vec not_equal_to(const vec& xs, const vec& ys) { + return zip(std::not_equal_to<>(), xs, ys); } template < std::size_t Size > - constexpr bool any(const vec& x) { - return fold(std::logical_or<>(), false, x); + constexpr bool any(const vec& xs) { + return fold(std::logical_or<>(), false, xs); } template < std::size_t Size > - constexpr bool all(const vec& x) { - return fold(std::logical_and<>(), true, x); + constexpr bool all(const vec& xs) { + return fold(std::logical_and<>(), true, xs); } template < std::size_t Size > - constexpr vec not_(const vec& x) { - return map(std::logical_not<>(), x); + constexpr vec not_(const vec& xs) { + return map(std::logical_not<>(), xs); } }