mirror of
https://github.com/BlackMATov/vmath.hpp.git
synced 2025-12-16 22:19:51 +07:00
basic qua detail functions
This commit is contained in:
@@ -13,6 +13,48 @@
|
|||||||
|
|
||||||
namespace vmath_hpp::detail::impl
|
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>& a,
|
||||||
|
std::index_sequence<Is...>
|
||||||
|
) -> qua<decltype(f(
|
||||||
|
std::declval<A>()))>
|
||||||
|
{
|
||||||
|
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>& a,
|
||||||
|
const qua<B>& b,
|
||||||
|
std::index_sequence<Is...>
|
||||||
|
) -> qua<decltype(f(
|
||||||
|
std::declval<A>(),
|
||||||
|
std::declval<B>()))>
|
||||||
|
{
|
||||||
|
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>& a,
|
||||||
|
const qua<B>& b,
|
||||||
|
const qua<C>& c,
|
||||||
|
std::index_sequence<Is...>
|
||||||
|
) -> qua<decltype(f(
|
||||||
|
std::declval<A>(),
|
||||||
|
std::declval<B>(),
|
||||||
|
std::declval<C>()))>
|
||||||
|
{
|
||||||
|
return { f(a[Is], b[Is], c[Is])... };
|
||||||
|
}
|
||||||
|
|
||||||
template < typename A, typename B, typename F, std::size_t... Is >
|
template < typename A, typename B, typename F, std::size_t... Is >
|
||||||
[[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE
|
[[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE
|
||||||
auto fold_join_impl(
|
auto fold_join_impl(
|
||||||
@@ -35,10 +77,54 @@ namespace vmath_hpp::detail::impl
|
|||||||
) -> A {
|
) -> A {
|
||||||
return ((init = f(std::move(init), b[Is], c[Is])), ...);
|
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>& a,
|
||||||
|
std::index_sequence<I, Is...>
|
||||||
|
) -> A {
|
||||||
|
A init = a[I];
|
||||||
|
return ((init = f(std::move(init), a[Is])), ...);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace vmath_hpp::detail
|
namespace vmath_hpp::detail
|
||||||
{
|
{
|
||||||
|
template < typename A, typename F >
|
||||||
|
[[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE
|
||||||
|
auto map_join(
|
||||||
|
F&& f,
|
||||||
|
const qua<A>& a
|
||||||
|
) {
|
||||||
|
return impl::map_join_impl(
|
||||||
|
std::forward<F>(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>& a,
|
||||||
|
const qua<B>& b
|
||||||
|
) {
|
||||||
|
return impl::map_join_impl(
|
||||||
|
std::forward<F>(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>& a,
|
||||||
|
const qua<B>& b,
|
||||||
|
const qua<C>& c
|
||||||
|
) {
|
||||||
|
return impl::map_join_impl(
|
||||||
|
std::forward<F>(f), a, b, c, std::make_index_sequence<4>{});
|
||||||
|
}
|
||||||
|
|
||||||
template < typename A, typename B, typename F >
|
template < typename A, typename B, typename F >
|
||||||
[[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE
|
[[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE
|
||||||
auto fold_join(
|
auto fold_join(
|
||||||
@@ -61,6 +147,16 @@ namespace vmath_hpp::detail
|
|||||||
return impl::fold_join_impl(
|
return impl::fold_join_impl(
|
||||||
std::forward<F>(f), std::move(init), b, c, std::make_index_sequence<4>{});
|
std::forward<F>(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>& a
|
||||||
|
) {
|
||||||
|
return impl::fold1_join_impl(
|
||||||
|
std::forward<F>(f), a, std::make_index_sequence<4>{});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|||||||
@@ -14,4 +14,29 @@ namespace
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("vmath/qua_fun") {
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user