diff --git a/headers/vmath.hpp/vmath_qua_fun.hpp b/headers/vmath.hpp/vmath_qua_fun.hpp index 4f2c24d..b8f3280 100644 --- a/headers/vmath.hpp/vmath_qua_fun.hpp +++ b/headers/vmath.hpp/vmath_qua_fun.hpp @@ -13,6 +13,48 @@ namespace vmath_hpp::detail::impl { + template < typename A, typename F, std::size_t... Is > + [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE + auto map_join_impl( + F&& f, + const qua& a, + std::index_sequence + ) -> qua()))> + { + return { f(a[Is])... }; + } + + template < typename A, typename B, typename F, std::size_t... Is > + [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE + auto map_join_impl( + F&& f, + const qua& a, + const qua& b, + std::index_sequence + ) -> qua(), + std::declval()))> + { + return { f(a[Is], b[Is])... }; + } + + template < typename A, typename B, typename C, typename F, std::size_t... Is > + [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE + auto map_join_impl( + F&& f, + const qua& a, + const qua& b, + const qua& c, + std::index_sequence + ) -> qua(), + std::declval(), + std::declval()))> + { + return { f(a[Is], b[Is], c[Is])... }; + } + template < typename A, typename B, typename F, std::size_t... Is > [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE auto fold_join_impl( @@ -35,10 +77,54 @@ namespace vmath_hpp::detail::impl ) -> A { return ((init = f(std::move(init), b[Is], c[Is])), ...); } + + template < typename A, typename F, std::size_t I, std::size_t... Is > + [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE + auto fold1_join_impl( + F&& f, + const qua& a, + std::index_sequence + ) -> A { + A init = a[I]; + return ((init = f(std::move(init), a[Is])), ...); + } } namespace vmath_hpp::detail { + template < typename A, typename F > + [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE + auto map_join( + F&& f, + const qua& a + ) { + return impl::map_join_impl( + std::forward(f), a, std::make_index_sequence<4>{}); + } + + 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 impl::map_join_impl( + std::forward(f), a, b, std::make_index_sequence<4>{}); + } + + 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 impl::map_join_impl( + std::forward(f), a, b, c, std::make_index_sequence<4>{}); + } + template < typename A, typename B, typename F > [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE auto fold_join( @@ -61,6 +147,16 @@ namespace vmath_hpp::detail return impl::fold_join_impl( std::forward(f), std::move(init), b, c, std::make_index_sequence<4>{}); } + + template < typename A, typename F > + [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE + auto fold1_join( + F&& f, + const qua& a + ) { + return impl::fold1_join_impl( + std::forward(f), a, std::make_index_sequence<4>{}); + } } // diff --git a/untests/vmath_qua_fun_tests.cpp b/untests/vmath_qua_fun_tests.cpp index dff5436..4f02c92 100644 --- a/untests/vmath_qua_fun_tests.cpp +++ b/untests/vmath_qua_fun_tests.cpp @@ -14,4 +14,29 @@ namespace } TEST_CASE("vmath/qua_fun") { + SUBCASE("Detail") { + STATIC_REQUIRE(map_join([](const int& x){ + return x * 2; + }, qua(1,2,3,4)) == qua(2,4,6,8)); + + STATIC_REQUIRE(map_join([](const int& x, const int& y){ + return x + y; + }, qua(1,2,3,4), qua(2,3,4,5)) == qua(3,5,7,9)); + + STATIC_REQUIRE(map_join([](const int& x, const int& y, const int& z){ + return x + y + z; + }, qua(1,2,3,4), qua(2,3,4,5), qua(3,4,5,6)) == qua(6,9,12,15)); + + STATIC_REQUIRE(fold_join([](int acc, const int& x){ + return acc + x; + }, 0, qua(1,2,3,4)) == 10); + + STATIC_REQUIRE(fold_join([](int acc, const int& x, const int& y){ + return acc + x + y; + }, 0, qua(1,2,3,4), qua(2,3,4,5)) == 24); + + STATIC_REQUIRE(fold1_join([](const int& acc, const int& x){ + return acc + x; + }, qua{1,2,3,4}) == 10); + } }