diff --git a/headers/vmath.hpp/vmath_mat_fun.hpp b/headers/vmath.hpp/vmath_mat_fun.hpp index 3764677..b3a4835 100644 --- a/headers/vmath.hpp/vmath_mat_fun.hpp +++ b/headers/vmath.hpp/vmath_mat_fun.hpp @@ -18,55 +18,28 @@ namespace vmath_hpp::detail::impl { template < typename A, std::size_t Size, typename F, std::size_t... Is > [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - mat - >::component_type, Size> - map_join_impl( + auto map_join_impl( F&& f, const mat& a, std::index_sequence) { - return { f(a[Is])... }; + return mat{ f(a[Is])... }; } template < typename A, typename B, std::size_t Size, typename F, std::size_t... Is > [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - mat, - vec - >::component_type, Size> - map_join_impl( + auto map_join_impl( F&& f, const mat& a, const mat& b, std::index_sequence) { - return { f(a[Is], b[Is])... }; - } - - template < typename A, typename B, typename C, std::size_t Size, typename F, std::size_t... Is > - [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - mat, - vec, - vec - >::component_type, Size> - map_join_impl( - F&& f, - const mat& a, - const mat& b, - const mat& c, - std::index_sequence) - { - return { f(a[Is], b[Is], c[Is])... }; + return mat{ f(a[Is], b[Is])... }; } template < typename A, typename B, std::size_t Size, typename F, std::size_t... Is > [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - A fold_join_impl( + auto fold_join_impl( F&& f, A init, const mat& b, @@ -75,39 +48,57 @@ namespace vmath_hpp::detail::impl 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 > + template < typename A, std::size_t Size, typename F, std::size_t... Is > [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - A fold_join_impl( - F&& f, - A init, - const vec& b, - const mat& c, - std::index_sequence) - { - return ((init = f(std::move(init), b[Is], c[Is])), ...); - } - - template < typename A, typename B, typename C, std::size_t Size, typename F, std::size_t... Is > - [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - A fold_join_impl( - F&& f, - A init, - const mat& b, - const mat& c, - std::index_sequence) - { - return ((init = f(std::move(init), b[Is], c[Is])), ...); - } - - template < typename A, std::size_t Size, typename F, std::size_t I, std::size_t... Is > - [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - vec fold1_join_impl( + auto fold1_and_join_impl( F&& f, const mat& a, - std::index_sequence) + std::index_sequence) { - vec init = a[I]; - return ((init = f(std::move(init), a[Is])), ...); + return (... && f(a[Is])); + } + + template < typename A, typename B, std::size_t Size, typename F, std::size_t... Is > + [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE + auto fold1_and_join_impl( + F&& f, + const mat& a, + const mat& b, + std::index_sequence) + { + return (... && f(a[Is], b[Is])); + } + + template < typename A, std::size_t Size, typename F, std::size_t... Is > + [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE + auto fold1_or_join_impl( + F&& f, + const mat& a, + std::index_sequence) + { + return (... || f(a[Is])); + } + + template < typename A, typename B, std::size_t Size, typename F, std::size_t... Is > + [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE + auto fold1_or_join_impl( + F&& f, + const mat& a, + const mat& b, + std::index_sequence) + { + return (... || f(a[Is], b[Is])); + } + + template < typename A, typename B, std::size_t Size, typename F, std::size_t... Is > + [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE + auto fold1_plus_join_impl( + F&& f, + const vec& a, + const mat& b, + std::index_sequence) + { + return (... + f(a[Is], b[Is])); } } @@ -125,34 +116,40 @@ namespace vmath_hpp::detail return impl::map_join_impl(std::forward(f), a, b, std::make_index_sequence{}); } - template < typename A, typename B, typename C, std::size_t Size, typename F > - [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - auto map_join(F&& f, const mat& a, const mat& b, const mat& c) { - return impl::map_join_impl(std::forward(f), a, b, c, std::make_index_sequence{}); - } - template < typename A, typename B, std::size_t Size, typename F > [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE auto fold_join(F&& f, A init, const mat& b) { return impl::fold_join_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 > + template < typename A, std::size_t Size, typename F > [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - auto fold_join(F&& f, A init, const vec& b, const mat& c) { - return impl::fold_join_impl(std::forward(f), std::move(init), b, c, std::make_index_sequence{}); + auto fold1_and_join(F&& f, const mat& a) { + return impl::fold1_and_join_impl(std::forward(f), a, std::make_index_sequence{}); } - template < typename A, typename B, typename C, std::size_t Size, typename F > + template < typename A, typename B, std::size_t Size, typename F > [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - auto fold_join(F&& f, A init, const mat& b, const mat& c) { - return impl::fold_join_impl(std::forward(f), std::move(init), b, c, std::make_index_sequence{}); + auto fold1_and_join(F&& f, const mat& a, const mat& b) { + return impl::fold1_and_join_impl(std::forward(f), a, b, std::make_index_sequence{}); } template < typename A, std::size_t Size, typename F > [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - auto fold1_join(F&& f, const mat& a) { - return impl::fold1_join_impl(std::forward(f), a, std::make_index_sequence{}); + auto fold1_or_join(F&& f, const mat& a) { + return impl::fold1_or_join_impl(std::forward(f), a, std::make_index_sequence{}); + } + + template < typename A, typename B, std::size_t Size, typename F > + [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE + auto fold1_or_join(F&& f, const mat& a, const mat& b) { + return impl::fold1_or_join_impl(std::forward(f), a, b, std::make_index_sequence{}); + } + + template < typename A, typename B, std::size_t Size, typename F > + [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE + auto fold1_plus_join(F&& f, const vec& a, const mat& b) { + return impl::fold1_plus_join_impl(std::forward(f), a, b, std::make_index_sequence{}); } } @@ -262,17 +259,12 @@ namespace vmath_hpp template < typename T, typename U, std::size_t Size > [[nodiscard]] constexpr auto operator*(const vec& xs, const mat& ys) { - using V = decltype(std::declval() * std::declval()); - return fold_join([](const vec& acc, T x, const vec& y){ - return acc + x * y; - }, vec{}, xs, ys); + return fold1_plus_join([](T x, const vec& y){ return x * y; }, xs, ys); } template < typename T, typename U, std::size_t Size > [[nodiscard]] constexpr auto operator*(const mat& xs, const mat& ys) { - return map_join([&ys](const vec& x){ - return x * ys; - }, xs); + return map_join([&ys](const vec& x){ return x * ys; }, xs); } // operator*= @@ -436,18 +428,14 @@ namespace vmath_hpp template < typename T, std::size_t Size > [[nodiscard]] constexpr bool operator==(const mat& xs, const mat& ys) { - return fold_join([](bool acc, const vec& x, const vec& y){ - return acc && (x == y); - }, true, xs, ys); + return fold1_and_join([](const vec& x, const vec& y){ return x == y; }, xs, ys); } // operator!= template < typename T, std::size_t Size > [[nodiscard]] constexpr bool operator!=(const mat& xs, const mat& ys) { - return fold_join([](bool acc, const vec& x, const vec& y){ - return acc || (x != y); - }, false, xs, ys); + return fold1_or_join([](const vec& x, const vec& y){ return x != y; }, xs, ys); } // operator< @@ -475,13 +463,13 @@ namespace vmath_hpp template < typename T, std::size_t Size , typename U = decltype(any(std::declval>())) > [[nodiscard]] constexpr U any(const mat& xs) { - return fold_join([](U acc, const vec& x){ return acc || any(x); }, U{false}, xs); + return fold1_or_join([](const vec& x){ return any(x); }, xs); } template < typename T, std::size_t Size , typename U = decltype(all(std::declval>())) > [[nodiscard]] constexpr U all(const mat& xs) { - return fold_join([](U acc, const vec& x){ return acc && all(x); }, U{true}, xs); + return fold1_and_join([](const vec& x){ return all(x); }, xs); } template < typename T, std::size_t Size diff --git a/headers/vmath.hpp/vmath_qua_fun.hpp b/headers/vmath.hpp/vmath_qua_fun.hpp index 1296647..fc4f0e1 100644 --- a/headers/vmath.hpp/vmath_qua_fun.hpp +++ b/headers/vmath.hpp/vmath_qua_fun.hpp @@ -22,41 +22,11 @@ namespace vmath_hpp::detail return qua(map_join(std::forward(f), vec{a})); } - template < typename A, typename B, typename F > - [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - auto map_join(F&& f, const qua& a, const qua& b) { - return qua(map_join(std::forward(f), vec{a}, vec{b})); - } - - template < typename A, typename B, typename C, typename F > - [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - auto map_join(F&& f, const qua& a, const qua& b, const qua& c) { - return qua(map_join(std::forward(f), vec{a}, vec{b}, vec{c})); - } - - template < typename A, typename B, typename C, typename D, typename F > - [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - auto map_join(F&& f, const qua& a, const qua& b, const qua& c, const qua& d) { - return qua(map_join(std::forward(f), vec{a}, vec{b}, vec{c}, vec{d})); - } - template < typename A, typename B, typename F > [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE auto fold_join(F&& f, A init, const qua& b) { return fold_join(std::forward(f), std::move(init), vec{b}); } - - template < typename A, typename B, typename C, typename F > - [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - auto fold_join(F&& f, A init, const qua& b, const qua& c) { - return fold_join(std::forward(f), std::move(init), vec{b}, vec{c}); - } - - template < typename A, typename F > - [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - auto fold1_join(F&& f, const qua& a) { - return fold1_join(std::forward(f), vec{a}); - } } // diff --git a/headers/vmath.hpp/vmath_vec_fun.hpp b/headers/vmath.hpp/vmath_vec_fun.hpp index c2b057f..115adec 100644 --- a/headers/vmath.hpp/vmath_vec_fun.hpp +++ b/headers/vmath.hpp/vmath_vec_fun.hpp @@ -15,40 +15,40 @@ namespace vmath_hpp::detail::impl { template < typename A, std::size_t Size, typename F, std::size_t... Is > [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - vec, Size> map_join_impl( + auto map_join_impl( F&& f, const vec& a, std::index_sequence) { - return { f(a[Is])... }; + return vec{ f(a[Is])... }; } template < typename A, typename B, std::size_t Size, typename F, std::size_t... Is > [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - vec, Size> map_join_impl( + auto map_join_impl( F&& f, const vec& a, const vec& b, std::index_sequence) { - return { f(a[Is], b[Is])... }; + return vec{ f(a[Is], b[Is])... }; } template < typename A, typename B, typename C, std::size_t Size, typename F, std::size_t... Is > [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - vec, Size> map_join_impl( + auto map_join_impl( F&& f, const vec& a, const vec& b, const vec& c, std::index_sequence) { - return { f(a[Is], b[Is], c[Is])... }; + return vec{ f(a[Is], b[Is], c[Is])... }; } template < typename A, typename B, typename C, typename D, std::size_t Size, typename F, std::size_t... Is > [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - vec, Size> map_join_impl( + auto map_join_impl( F&& f, const vec& a, const vec& b, @@ -56,12 +56,12 @@ namespace vmath_hpp::detail::impl const vec& d, std::index_sequence) { - return { f(a[Is], b[Is], c[Is], d[Is])... }; + return vec{ f(a[Is], b[Is], c[Is], d[Is])... }; } template < typename A, typename B, std::size_t Size, typename F, std::size_t... Is > [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - A fold_join_impl( + auto fold_join_impl( F&& f, A init, const vec& b, @@ -70,21 +70,9 @@ namespace vmath_hpp::detail::impl 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 > - [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - A fold_join_impl( - F&& f, - A init, - const vec& b, - const vec& c, - std::index_sequence) - { - return ((init = f(std::move(init), b[Is], c[Is])), ...); - } - template < typename A, std::size_t Size, typename F, std::size_t I, std::size_t... Is > [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - A fold1_join_impl( + auto fold1_join_impl( F&& f, const vec& a, std::index_sequence) @@ -92,6 +80,59 @@ namespace vmath_hpp::detail::impl A init = a[I]; return ((init = f(std::move(init), a[Is])), ...); } + + template < typename A, std::size_t Size, typename F, std::size_t... Is > + [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE + auto fold1_and_join_impl( + F&& f, + const vec& a, + std::index_sequence) + { + return (... && f(a[Is])); + } + + template < typename A, typename B, std::size_t Size, typename F, std::size_t... Is > + [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE + auto fold1_and_join_impl( + F&& f, + const vec& a, + const vec& b, + std::index_sequence) + { + return (... && f(a[Is], b[Is])); + } + + template < typename A, std::size_t Size, typename F, std::size_t... Is > + [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE + auto fold1_or_join_impl( + F&& f, + const vec& a, + std::index_sequence) + { + return (... || f(a[Is])); + } + + template < typename A, typename B, std::size_t Size, typename F, std::size_t... Is > + [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE + auto fold1_or_join_impl( + F&& f, + const vec& a, + const vec& b, + std::index_sequence) + { + return (... || f(a[Is], b[Is])); + } + + template < typename A, typename B, std::size_t Size, typename F, std::size_t... Is > + [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE + auto fold1_plus_join_impl( + F&& f, + const vec& a, + const vec& b, + std::index_sequence) + { + return (... + f(a[Is], b[Is])); + } } namespace vmath_hpp::detail @@ -126,17 +167,41 @@ namespace vmath_hpp::detail return impl::fold_join_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 > - [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - auto fold_join(F&& f, A init, const vec& b, const vec& c) { - return impl::fold_join_impl(std::forward(f), std::move(init), b, c, std::make_index_sequence{}); - } - template < typename A, std::size_t Size, typename F > [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE auto fold1_join(F&& f, const vec& a) { return impl::fold1_join_impl(std::forward(f), a, std::make_index_sequence{}); } + + template < typename A, std::size_t Size, typename F > + [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE + auto fold1_and_join(F&& f, const vec& a) { + return impl::fold1_and_join_impl(std::forward(f), a, std::make_index_sequence{}); + } + + template < typename A, typename B, std::size_t Size, typename F > + [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE + auto fold1_and_join(F&& f, const vec& a, const vec& b) { + return impl::fold1_and_join_impl(std::forward(f), a, b, std::make_index_sequence{}); + } + + template < typename A, std::size_t Size, typename F > + [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE + auto fold1_or_join(F&& f, const vec& a) { + return impl::fold1_or_join_impl(std::forward(f), a, std::make_index_sequence{}); + } + + template < typename A, typename B, std::size_t Size, typename F > + [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE + auto fold1_or_join(F&& f, const vec& a, const vec& b) { + return impl::fold1_or_join_impl(std::forward(f), a, b, std::make_index_sequence{}); + } + + template < typename A, typename B, std::size_t Size, typename F > + [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE + auto fold1_plus_join(F&& f, const vec& a, const vec& b) { + return impl::fold1_plus_join_impl(std::forward(f), a, b, std::make_index_sequence{}); + } } // @@ -414,18 +479,14 @@ namespace vmath_hpp template < typename T, std::size_t Size > [[nodiscard]] constexpr bool operator==(const vec& xs, const vec& ys) { - return fold_join([](bool acc, T x, T y){ - return acc && x == y; - }, true, xs, ys); + return fold1_and_join([](T x, T y){ return x == y; }, xs, ys); } // operator!= template < typename T, std::size_t Size > [[nodiscard]] constexpr bool operator!=(const vec& xs, const vec& ys) { - return fold_join([](bool acc, T x, T y){ - return acc || x != y; - }, false, xs, ys); + return fold1_or_join([](T x, T y){ return x != y; }, xs, ys); } // operator< @@ -778,9 +839,7 @@ namespace vmath_hpp template < typename T, typename U, std::size_t Size , typename V = decltype(std::declval() * std::declval()) > [[nodiscard]] constexpr V dot(const vec& xs, const vec& ys) { - return fold_join([](V acc, T x, U y){ - return acc + (x * y); - }, V{0}, xs, ys); + return fold1_plus_join([](T x, U y){ return x * y; }, xs, ys); } template < typename T, std::size_t Size > @@ -860,13 +919,13 @@ namespace vmath_hpp template < typename T, std::size_t Size , typename U = decltype(any(std::declval())) > [[nodiscard]] constexpr U any(const vec& xs) { - return fold_join([](U acc, T x){ return acc || any(x); }, U{false}, xs); + return fold1_or_join([](T x){ return any(x); }, xs); } template < typename T, std::size_t Size , typename U = decltype(all(std::declval())) > [[nodiscard]] constexpr U all(const vec& xs) { - return fold_join([](U acc, T x){ return acc && all(x); }, U{true}, xs); + return fold1_and_join([](T x){ return all(x); }, xs); } template < typename T, std::size_t Size diff --git a/untests/vmath_mat_fun_tests.cpp b/untests/vmath_mat_fun_tests.cpp index d068acc..a91252e 100644 --- a/untests/vmath_mat_fun_tests.cpp +++ b/untests/vmath_mat_fun_tests.cpp @@ -30,32 +30,6 @@ namespace } TEST_CASE("vmath/mat_fun") { - SUBCASE("detail") { - STATIC_CHECK(map_join([](const int2& x){ - return x * 2; - }, int2x2{}) == int2x2(2,0,0,2)); - - STATIC_CHECK(map_join([](const int2& x, const int2& y){ - return x + y; - }, int2x2{}, int2x2{}) == int2x2(2,0,0,2)); - - STATIC_CHECK(map_join([](const int2& x, const int2& y, const int2& z){ - return x + y + z; - }, int2x2{}, int2x2{}, int2x2{}) == int2x2(3,0,0,3)); - - STATIC_CHECK(fold_join([](int acc, const int2& x){ - return acc + x.x; - }, 0, int2x2{}) == 1); - - STATIC_CHECK(fold_join([](int acc, const int2& x, const int2& y){ - return acc + x.x + y.x; - }, 0, int2x2{}, int2x2{}) == 2); - - STATIC_CHECK(fold1_join([](const int2& acc, const int2& x){ - return acc + x; - }, int2x2{}) == int2(1,1)); - } - SUBCASE("operators") { STATIC_CHECK(+int2x2(1,-2,3,-4) == int2x2(1,-2,3,-4)); STATIC_CHECK(-int2x2(1,-2,3,-4) == int2x2(-1,2,-3,4)); diff --git a/untests/vmath_vec_fun_tests.cpp b/untests/vmath_vec_fun_tests.cpp index f9fc0b9..b9eb76e 100644 --- a/untests/vmath_vec_fun_tests.cpp +++ b/untests/vmath_vec_fun_tests.cpp @@ -13,36 +13,6 @@ namespace } TEST_CASE("vmath/vec_fun") { - SUBCASE("Detail") { - STATIC_CHECK(map_join([](const int& x){ - return x * 2; - }, vec{1,2,3,4}) == vec{2,4,6,8}); - - STATIC_CHECK(map_join([](const int& x, const int& y){ - return x + y; - }, vec{1,2,3,4}, vec{2,3,4,5}) == vec{3,5,7,9}); - - STATIC_CHECK(map_join([](const int& x, const int& y, const int& z){ - return x + y + z; - }, vec{1,2,3,4}, vec{2,3,4,5}, vec{3,4,5,6}) == vec{6,9,12,15}); - - STATIC_CHECK(map_join([](const int& x, const int& y, const int& z, const int& w){ - return x + y + z + w; - }, vec{1,2,3,4}, vec{2,3,4,5}, vec{3,4,5,6}, vec{4,5,6,7}) == vec{10,14,18,22}); - - STATIC_CHECK(fold_join([](int acc, const int& x){ - return acc - x; - }, 0, vec{1,2,3,4}) == -10); - - STATIC_CHECK(fold_join([](int acc, const int& x, const int& y){ - return acc - x - y; - }, 0, vec{1,2,3,4}, vec{2,3,4,5}) == -24); - - STATIC_CHECK(fold1_join([](const int& acc, const int& x){ - return acc - x; - }, vec{1,2,3,4}) == -8); - } - SUBCASE("Operators") { STATIC_CHECK(+int2(1,-2) == int2(1,-2)); STATIC_CHECK(-int2(1,-2) == int2(-1,2));