some style fixes

This commit is contained in:
BlackMATov
2021-02-23 10:00:16 +07:00
parent 5c800b1ce6
commit 2bded8a680
10 changed files with 267 additions and 275 deletions

View File

@@ -91,8 +91,6 @@ public:
constexpr vec_base() = default;
constexpr explicit vec_base(T v);
constexpr vec_base(T x, T y);
constexpr explicit vec_base(const vec_base<T, 3>& xy);
constexpr explicit vec_base(const vec_base<T, 4>& xy);
};
template < typename T >
@@ -107,7 +105,8 @@ public:
constexpr vec_base(T x, T y, T z);
constexpr vec_base(const vec_base<T, 2>& xy, T z);
constexpr vec_base(T x, const vec_base<T, 2>& yz);
constexpr explicit vec_base(const vec_base<T, 4>& xyz);
constexpr explicit operator vec<T, 2>() const;
};
template < typename T >
@@ -127,12 +126,16 @@ public:
constexpr vec_base(const vec_base<T, 2>& xy, const vec_base<T, 2>& zw);
constexpr vec_base(const vec_base<T, 3>& xyz, T w);
constexpr vec_base(T x, const vec_base<T, 3>& yzw);
constexpr explicit operator vec<T, 2>() const;
constexpr explicit operator vec<T, 3>() const;
};
template < typename T, size_t Size >
class vec final : public vec_base<T, Size> {
public:
using self_type = vec;
using base_type = vec_base<T, Size>;
using component_type = T;
using pointer = component_type*;
@@ -234,9 +237,6 @@ public:
constexpr mat_base(
const row_type& row0,
const row_type& row1);
constexpr explicit mat_base(const mat_base<T, 3>& other);
constexpr explicit mat_base(const mat_base<T, 4>& other);
};
template < typename T >
@@ -268,7 +268,8 @@ public:
const vec_base<T, 2>& v);
constexpr explicit mat_base(const mat_base<T, 2>& other);
constexpr explicit mat_base(const mat_base<T, 4>& other);
constexpr explicit operator mat<T, 2>() const;
};
template < typename T >
@@ -304,12 +305,18 @@ public:
constexpr explicit mat_base(const mat_base<T, 2>& other);
constexpr explicit mat_base(const mat_base<T, 3>& other);
constexpr explicit operator mat<T, 2>() const;
constexpr explicit operator mat<T, 3>() const;
};
template < typename T, size_t Size >
class mat final : public mat_base<T, Size> {
public:
using self_type = mat;
using base_type = mat_base<T, Size>;
using component_type = T;
using row_type = vec<T, Size>;
using pointer = row_type*;
@@ -388,18 +395,30 @@ using ptrdiff4x4_t = mat<ptrdiff_t, 4>;
### Quaternion Types
```cpp
template < typename T >
class qua_base {
public:
vec<T, 3> v = vec<T, 3>{T{0}};
T s = T{1};
constexpr qua_base();
constexpr qua_base(T vx, T vy, T vz, T s);
constexpr qua_base(const vec<T, 3>& v, T s);
constexpr explicit qua_base(const vec<T, 4>& vs);
constexpr explicit operator vec<T, 4>() const;
};
template < typename T >
class qua final {
public:
using self_type = qua;
using base_type = qua_base<T>;
using component_type = T;
using imag_type = vec<T, 3>;
using real_type = T;
imag_type v = imag_type{T{0}};
real_type s = real_type{T{1}};
public:
using self_type = qua;
using component_type = T;
using pointer = component_type*;
using const_pointer = const component_type*;
@@ -417,12 +436,6 @@ public:
constexpr qua(const qua&) = default;
constexpr qua& operator=(const qua&) = default;
constexpr qua(T vx, T vy, T vz, T s);
constexpr qua(const vec<T, 3>& v, T s);
constexpr explicit qua(const vec<T, 4>& vs);
constexpr explicit operator vec<T, 4>() const;
void swap(qua& other) noexcept(is_nothrow_swappable_v<T>);
iterator begin() noexcept;

View File

@@ -112,7 +112,7 @@ namespace vmath_hpp::detail
template < typename T >
[[nodiscard]] std::size_t hash(const qua<T>& q) noexcept {
return hash(vec{q});
return fold_join(hash_combiner{}, std::size_t{}, q);
}
}
@@ -156,17 +156,17 @@ namespace vmath_hpp
template < typename To, typename From, std::size_t Size >
[[nodiscard]] constexpr vec<To, Size> cast_to(const vec<From, Size>& v) {
return detail::map_join([](From x){ return cast_to<To>(x); }, v);
return map_join([](From x){ return cast_to<To>(x); }, v);
}
template < typename To, typename From, std::size_t Size >
[[nodiscard]] constexpr mat<To, Size> cast_to(const mat<From, Size>& m) {
return detail::map_join([](const vec<From, Size>& v){ return cast_to<To>(v); }, m);
return map_join([](const vec<From, Size>& v){ return cast_to<To>(v); }, m);
}
template < typename To, typename From >
[[nodiscard]] constexpr qua<To> cast_to(const qua<From>& q) {
return qua(cast_to<To>(vec{q}));
return map_join([](From x){ return cast_to<To>(x); }, q);
}
}

View File

@@ -15,16 +15,14 @@
namespace vmath_hpp
{
template < typename T >
[[nodiscard]] std::enable_if_t<std::is_signed_v<T>, T>
[[nodiscard]] std::enable_if_t<std::is_arithmetic_v<T>, T>
constexpr abs(T x) noexcept {
if constexpr ( std::is_signed_v<T> ) {
return x < T{0} ? -x : x;
}
template < typename T >
[[nodiscard]] std::enable_if_t<std::is_unsigned_v<T>, T>
constexpr abs(T x) noexcept {
} else {
return x;
}
}
template < typename T >
[[nodiscard]] std::enable_if_t<std::is_arithmetic_v<T>, T>

View File

@@ -48,18 +48,6 @@ namespace vmath_hpp::detail
const row_type& row0,
const row_type& row1)
: rows{row0, row1} {}
constexpr explicit mat_base(
const mat_base<T, 3>& other)
: rows{
row_type{other.rows[0]},
row_type{other.rows[1]}} {}
constexpr explicit mat_base(
const mat_base<T, 4>& other)
: rows{
row_type{other.rows[0]},
row_type{other.rows[1]}} {}
};
template < typename T >
@@ -116,12 +104,11 @@ namespace vmath_hpp::detail
row_type{other.rows[1], T{0}},
row_type{T{0}, T{0}, T{1}}} {}
constexpr explicit mat_base(
const mat_base<T, 4>& other)
: rows{
row_type{other.rows[0]},
row_type{other.rows[1]},
row_type{other.rows[2]}} {}
constexpr explicit operator mat<T, 2>() const {
return {
vec<T, 2>{rows[0]},
vec<T, 2>{rows[1]}};
}
};
template < typename T >
@@ -193,6 +180,19 @@ namespace vmath_hpp::detail
row_type{other.rows[1], T{0}},
row_type{other.rows[2], T{0}},
row_type{T{0}, T{0}, T{0}, T{1}}} {}
constexpr explicit operator mat<T, 2>() const {
return {
vec<T, 2>{rows[0]},
vec<T, 2>{rows[1]}};
}
constexpr explicit operator mat<T, 3>() const {
return {
vec<T, 3>{rows[0]},
vec<T, 3>{rows[1]},
vec<T, 3>{rows[2]}};
}
};
}
@@ -203,7 +203,8 @@ namespace vmath_hpp
public:
using self_type = mat;
using base_type = detail::mat_base<T, Size>;
public:
using component_type = T;
using row_type = vec<T, Size>;
using pointer = row_type*;

View File

@@ -18,88 +18,94 @@ namespace vmath_hpp::detail::impl
{
template < typename A, std::size_t Size, typename F, std::size_t... Is >
[[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE
auto map_join_impl(
mat<typename std::invoke_result_t<
F,
vec<A, Size>
>::component_type, Size>
map_join_impl(
F&& f,
const mat<A, Size>& a,
std::index_sequence<Is...>
) -> mat<typename decltype(f(
std::declval<vec<A, Size>>()))::component_type, Size>
std::index_sequence<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 map_join_impl(
mat<typename std::invoke_result_t<
F,
vec<A, Size>,
vec<B, Size>
>::component_type, Size>
map_join_impl(
F&& f,
const mat<A, Size>& a,
const mat<B, Size>& b,
std::index_sequence<Is...>
) -> mat<typename decltype(f(
std::declval<vec<A, Size>>(),
std::declval<vec<B, Size>>()))::component_type, Size>
std::index_sequence<Is...>)
{
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
auto map_join_impl(
mat<typename std::invoke_result_t<
F,
vec<A, Size>,
vec<B, Size>,
vec<C, Size>
>::component_type, Size>
map_join_impl(
F&& f,
const mat<A, Size>& a,
const mat<B, Size>& b,
const mat<C, Size>& c,
std::index_sequence<Is...>
) -> mat<typename decltype(f(
std::declval<vec<A, Size>>(),
std::declval<vec<B, Size>>(),
std::declval<vec<C, Size>>()))::component_type, Size>
std::index_sequence<Is...>)
{
return { f(a[Is], b[Is], c[Is])... };
}
template < typename A, typename B, std::size_t Size, typename F, std::size_t... Is >
[[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE
auto fold_join_impl(
A fold_join_impl(
F&& f,
A init,
const mat<B, Size>& b,
std::index_sequence<Is...>
) -> A {
std::index_sequence<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 >
[[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE
auto fold_join_impl(
A fold_join_impl(
F&& f,
A init,
const vec<B, Size>& b,
const mat<C, Size>& c,
std::index_sequence<Is...>
) -> A {
std::index_sequence<Is...>)
{
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
auto fold_join_impl(
A fold_join_impl(
F&& f,
A init,
const mat<B, Size>& b,
const mat<C, Size>& c,
std::index_sequence<Is...>
) -> A {
std::index_sequence<Is...>)
{
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
auto fold1_join_impl(
vec<A, Size> fold1_join_impl(
F&& f,
const mat<A, Size>& a,
std::index_sequence<I, Is...>
) -> vec<A, Size> {
std::index_sequence<I, Is...>)
{
vec<A, Size> init = a[I];
return ((init = f(std::move(init), a[Is])), ...);
}
@@ -109,80 +115,44 @@ namespace vmath_hpp::detail
{
template < typename A, std::size_t Size, typename F >
[[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE
auto map_join(
F&& f,
const mat<A, Size>& a
) {
return impl::map_join_impl(
std::forward<F>(f), a, std::make_index_sequence<Size>{});
auto map_join(F&& f, const mat<A, Size>& a) {
return impl::map_join_impl(std::forward<F>(f), a, std::make_index_sequence<Size>{});
}
template < typename A, typename B, std::size_t Size, typename F >
[[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE
auto map_join(
F&& f,
const mat<A, Size>& a,
const mat<B, Size>& b
) {
return impl::map_join_impl(
std::forward<F>(f), a, b, std::make_index_sequence<Size>{});
auto map_join(F&& f, const mat<A, Size>& a, const mat<B, Size>& b) {
return impl::map_join_impl(std::forward<F>(f), a, b, std::make_index_sequence<Size>{});
}
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, Size>& a,
const mat<B, Size>& b,
const mat<C, Size>& c
) {
return impl::map_join_impl(
std::forward<F>(f), a, b, c, std::make_index_sequence<Size>{});
auto map_join(F&& f, const mat<A, Size>& a, const mat<B, Size>& b, const mat<C, Size>& c) {
return impl::map_join_impl(std::forward<F>(f), a, b, c, std::make_index_sequence<Size>{});
}
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, Size>& b
) {
return impl::fold_join_impl(
std::forward<F>(f), std::move(init), b, std::make_index_sequence<Size>{});
auto fold_join(F&& f, A init, const mat<B, Size>& b) {
return impl::fold_join_impl(std::forward<F>(f), std::move(init), b, std::make_index_sequence<Size>{});
}
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, Size>& b,
const mat<C, Size>& c
) {
return impl::fold_join_impl(
std::forward<F>(f), std::move(init), b, c, std::make_index_sequence<Size>{});
auto fold_join(F&& f, A init, const vec<B, Size>& b, const mat<C, Size>& c) {
return impl::fold_join_impl(std::forward<F>(f), std::move(init), b, c, std::make_index_sequence<Size>{});
}
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 mat<B, Size>& b,
const mat<C, Size>& c
) {
return impl::fold_join_impl(
std::forward<F>(f), std::move(init), b, c, std::make_index_sequence<Size>{});
auto fold_join(F&& f, A init, const mat<B, Size>& b, const mat<C, Size>& c) {
return impl::fold_join_impl(std::forward<F>(f), std::move(init), b, c, std::make_index_sequence<Size>{});
}
template < typename A, std::size_t Size, typename F >
[[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE
auto fold1_join(
F&& f,
const mat<A, Size>& a
) {
return impl::fold1_join_impl(
std::forward<F>(f), a, std::make_index_sequence<Size>{});
auto fold1_join(F&& f, const mat<A, Size>& a) {
return impl::fold1_join_impl(std::forward<F>(f), a, std::make_index_sequence<Size>{});
}
}
@@ -196,7 +166,7 @@ namespace vmath_hpp
template < typename T, std::size_t Size >
[[nodiscard]] constexpr auto operator+(const mat<T, Size>& xs) {
return xs;
return map_join([](const vec<T, Size>& x){ return +x; }, xs);
}
// -operator

View File

@@ -11,20 +11,63 @@
#include "vmath_vec.hpp"
#include "vmath_vec_fun.hpp"
namespace vmath_hpp::detail
{
template < typename T >
class qua_base {
public:
vec<T, 3> v = vec<T, 3>{T{0}};
T s = T{1};
public:
constexpr qua_base() = default;
constexpr qua_base(T vx, T vy, T vz, T s)
: v{vx, vy, vz}, s{s} {}
constexpr qua_base(const vec<T, 3>& v, T s)
: v{v}, s{s} {}
constexpr explicit qua_base(const vec<T, 4>& vs)
: v{vs[0], vs[1], vs[2]}, s{vs[3]} {}
constexpr explicit operator vec<T, 4>() const {
return {v, s};
}
[[nodiscard]] constexpr T& operator[](std::size_t index) noexcept {
switch ( index ) {
default:
case 0: return v.x;
case 1: return v.y;
case 2: return v.z;
case 3: return s;
}
}
[[nodiscard]] constexpr const T& operator[](std::size_t index) const noexcept {
switch ( index ) {
default:
case 0: return v.x;
case 1: return v.y;
case 2: return v.z;
case 3: return s;
}
}
};
}
namespace vmath_hpp
{
template < typename T >
class qua final {
public:
using imag_type = vec<T, 3>;
using real_type = T;
imag_type v = imag_type{T{0}};
real_type s = real_type{T{1}};
class qua final : public detail::qua_base<T> {
public:
using self_type = qua;
using base_type = detail::qua_base<T>;
using component_type = T;
using imag_type = vec<T, 3>;
using real_type = T;
using pointer = component_type*;
using const_pointer = const component_type*;
@@ -38,26 +81,13 @@ namespace vmath_hpp
static constexpr std::size_t size = 4;
public:
using base_type::qua_base;
using base_type::operator[];
constexpr qua() = default;
constexpr qua(const qua&) = default;
constexpr qua& operator=(const qua&) = default;
constexpr qua(T vx, T vy, T vz, T s)
: v{vx, vy, vz}
, s{s} {}
constexpr qua(const vec<T, 3>& v, T s)
: v{v}
, s{s} {}
constexpr explicit qua(const vec<T, 4>& vs)
: v{vs[0], vs[1], vs[2]}
, s{vs[3]} {}
constexpr explicit operator vec<T, 4>() const {
return {(*this).v, (*this).s};
}
void swap(qua& other) noexcept(std::is_nothrow_swappable_v<T>) {
for ( std::size_t i = 0; i < size; ++i ) {
using std::swap;
@@ -97,26 +127,6 @@ namespace vmath_hpp
VMATH_HPP_THROW_IF(index >= size, std::out_of_range("qua::at"));
return (*this)[index];
}
[[nodiscard]] constexpr reference operator[](std::size_t index) noexcept {
switch ( index ) {
default:
case 0: return v.x;
case 1: return v.y;
case 2: return v.z;
case 3: return s;
}
}
[[nodiscard]] constexpr const_reference operator[](std::size_t index) const noexcept {
switch ( index ) {
default:
case 0: return v.x;
case 1: return v.y;
case 2: return v.z;
case 3: return s;
}
}
};
}

View File

@@ -14,6 +14,51 @@
#include "vmath_vec.hpp"
#include "vmath_vec_fun.hpp"
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 qua(map_join(std::forward<F>(f), vec{a}));
}
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 qua(map_join(std::forward<F>(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>& a, const qua<B>& b, const qua<C>& c) {
return qua(map_join(std::forward<F>(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>& a, const qua<B>& b, const qua<C>& c, const qua<D>& d) {
return qua(map_join(std::forward<F>(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>& b) {
return fold_join(std::forward<F>(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>& b, const qua<C>& c) {
return fold_join(std::forward<F>(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>& a) {
return fold1_join(std::forward<F>(f), vec{a});
}
}
//
// Operators
//
@@ -24,7 +69,7 @@ namespace vmath_hpp
template < typename T >
[[nodiscard]] constexpr auto operator+(const qua<T>& xs) {
return xs;
return qua(+vec<T, 4>{xs});
}
// -operator
@@ -163,8 +208,8 @@ namespace vmath_hpp
}
template < typename T >
[[nodiscard]] qua<T> lerp(const qua<T>& xs, const qua<T>& ys, T xs_a, T ys_a) {
return qua(lerp(vec{xs}, vec{ys}, xs_a, ys_a));
[[nodiscard]] qua<T> lerp(const qua<T>& xs, const qua<T>& ys, T x_a, T y_a) {
return qua(lerp(vec{xs}, vec{ys}, x_a, y_a));
}
template < typename T >

View File

@@ -27,12 +27,6 @@ namespace vmath_hpp::detail
constexpr vec_base(T x, T y)
: x{x}, y{y} {}
constexpr explicit vec_base(const vec_base<T, 3>& xy)
: x{xy[0]}, y{xy[1]} {}
constexpr explicit vec_base(const vec_base<T, 4>& xy)
: x{xy[0]}, y{xy[1]} {}
[[nodiscard]] constexpr T& operator[](std::size_t index) noexcept {
switch ( index ) {
default:
@@ -71,8 +65,9 @@ namespace vmath_hpp::detail
constexpr vec_base(T x, const vec_base<T, 2>& yz)
: x{x}, y{yz[0]}, z{yz[1]} {}
constexpr explicit vec_base(const vec_base<T, 4>& xyz)
: x{xyz[0]}, y{xyz[1]}, z{xyz[2]} {}
constexpr explicit operator vec<T, 2>() const {
return {x, y};
}
[[nodiscard]] constexpr T& operator[](std::size_t index) noexcept {
switch ( index ) {
@@ -127,6 +122,14 @@ namespace vmath_hpp::detail
constexpr vec_base(T x, const vec_base<T, 3>& yzw)
: x{x}, y{yzw[0]}, z{yzw[1]}, w{yzw[2]} {}
constexpr explicit operator vec<T, 2>() const {
return {x, y};
}
constexpr explicit operator vec<T, 3>() const {
return {x, y, z};
}
[[nodiscard]] constexpr T& operator[](std::size_t index) noexcept {
switch ( index ) {
default:
@@ -156,7 +159,6 @@ namespace vmath_hpp
public:
using self_type = vec;
using base_type = detail::vec_base<T, Size>;
public:
using component_type = T;
using pointer = component_type*;

View File

@@ -15,94 +15,80 @@ namespace vmath_hpp::detail::impl
{
template < typename A, std::size_t Size, typename F, std::size_t... Is >
[[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE
auto map_join_impl(
vec<std::invoke_result_t<F, A>, Size> map_join_impl(
F&& f,
const vec<A, Size>& a,
std::index_sequence<Is...>
) -> vec<decltype(f(
std::declval<A>())), Size>
std::index_sequence<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 map_join_impl(
vec<std::invoke_result_t<F, A, B>, Size> map_join_impl(
F&& f,
const vec<A, Size>& a,
const vec<B, Size>& b,
std::index_sequence<Is...>
) -> vec<decltype(f(
std::declval<A>(),
std::declval<B>())), Size>
std::index_sequence<Is...>)
{
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
auto map_join_impl(
vec<std::invoke_result_t<F, A, B, C>, Size> map_join_impl(
F&& f,
const vec<A, Size>& a,
const vec<B, Size>& b,
const vec<C, Size>& c,
std::index_sequence<Is...>
) -> vec<decltype(f(
std::declval<A>(),
std::declval<B>(),
std::declval<C>())), Size>
std::index_sequence<Is...>)
{
return { 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
auto map_join_impl(
vec<std::invoke_result_t<F, A, B, C, D>, Size> map_join_impl(
F&& f,
const vec<A, Size>& a,
const vec<B, Size>& b,
const vec<C, Size>& c,
const vec<D, Size>& d,
std::index_sequence<Is...>
) -> vec<decltype(f(
std::declval<A>(),
std::declval<B>(),
std::declval<C>(),
std::declval<D>())), Size>
std::index_sequence<Is...>)
{
return { 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
auto fold_join_impl(
A fold_join_impl(
F&& f,
A init,
const vec<B, Size>& b,
std::index_sequence<Is...>
) -> A {
std::index_sequence<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 >
[[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE
auto fold_join_impl(
A fold_join_impl(
F&& f,
A init,
const vec<B, Size>& b,
const vec<C, Size>& c,
std::index_sequence<Is...>
) -> A {
std::index_sequence<Is...>)
{
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
auto fold1_join_impl(
A fold1_join_impl(
F&& f,
const vec<A, Size>& a,
std::index_sequence<I, Is...>
) -> A {
std::index_sequence<I, Is...>)
{
A init = a[I];
return ((init = f(std::move(init), a[Is])), ...);
}
@@ -112,81 +98,44 @@ namespace vmath_hpp::detail
{
template < typename A, std::size_t Size, typename F >
[[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE
auto map_join(
F&& f,
const vec<A, Size>& a
) {
return impl::map_join_impl(
std::forward<F>(f), a, std::make_index_sequence<Size>{});
auto map_join(F&& f, const vec<A, Size>& a) {
return impl::map_join_impl(std::forward<F>(f), a, std::make_index_sequence<Size>{});
}
template < typename A, typename B, std::size_t Size, typename F >
[[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE
auto map_join(
F&& f,
const vec<A, Size>& a,
const vec<B, Size>& b
) {
return impl::map_join_impl(
std::forward<F>(f), a, b, std::make_index_sequence<Size>{});
auto map_join(F&& f, const vec<A, Size>& a, const vec<B, Size>& b) {
return impl::map_join_impl(std::forward<F>(f), a, b, std::make_index_sequence<Size>{});
}
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 vec<A, Size>& a,
const vec<B, Size>& b,
const vec<C, Size>& c
) {
return impl::map_join_impl(
std::forward<F>(f), a, b, c, std::make_index_sequence<Size>{});
auto map_join(F&& f, const vec<A, Size>& a, const vec<B, Size>& b, const vec<C, Size>& c) {
return impl::map_join_impl(std::forward<F>(f), a, b, c, std::make_index_sequence<Size>{});
}
template < typename A, typename B, typename C, typename D, std::size_t Size, typename F >
[[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE
auto map_join(
F&& f,
const vec<A, Size>& a,
const vec<B, Size>& b,
const vec<C, Size>& c,
const vec<D, Size>& d
) {
return impl::map_join_impl(
std::forward<F>(f), a, b, c, d, std::make_index_sequence<Size>{});
auto map_join(F&& f, const vec<A, Size>& a, const vec<B, Size>& b, const vec<C, Size>& c, const vec<D, Size>& d) {
return impl::map_join_impl(std::forward<F>(f), a, b, c, d, std::make_index_sequence<Size>{});
}
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 vec<B, Size>& b
) {
return impl::fold_join_impl(
std::forward<F>(f), std::move(init), b, std::make_index_sequence<Size>{});
auto fold_join(F&& f, A init, const vec<B, Size>& b) {
return impl::fold_join_impl(std::forward<F>(f), std::move(init), b, std::make_index_sequence<Size>{});
}
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, Size>& b,
const vec<C, Size>& c
) {
return impl::fold_join_impl(
std::forward<F>(f), std::move(init), b, c, std::make_index_sequence<Size>{});
auto fold_join(F&& f, A init, const vec<B, Size>& b, const vec<C, Size>& c) {
return impl::fold_join_impl(std::forward<F>(f), std::move(init), b, c, std::make_index_sequence<Size>{});
}
template < typename A, std::size_t Size, typename F >
[[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE
auto fold1_join(
F&& f,
const vec<A, Size>& a
) {
return impl::fold1_join_impl(
std::forward<F>(f), a, std::make_index_sequence<Size>{});
auto fold1_join(F&& f, const vec<A, Size>& a) {
return impl::fold1_join_impl(std::forward<F>(f), a, std::make_index_sequence<Size>{});
}
}
@@ -200,7 +149,7 @@ namespace vmath_hpp
template < typename T, std::size_t Size >
[[nodiscard]] constexpr auto operator+(const vec<T, Size>& xs) {
return xs;
return map_join([](T x){ return +x; }, xs);
}
// -operator

View File

@@ -16,27 +16,31 @@ TEST_CASE("vmath/vec_fun") {
SUBCASE("Detail") {
STATIC_CHECK(map_join([](const int& x){
return x * 2;
}, int2{1}) == int2{2});
}, vec{1,2,3,4}) == vec{2,4,6,8});
STATIC_CHECK(map_join([](const int& x, const int& y){
return x + y;
}, int2{1}, int2{1}) == int2{2});
}, 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;
}, int2{1}, int2{1}, int2{1}) == int2(3));
}, 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, int2{1}) == 2);
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, int2{1}, int2{1}) == 4);
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;
}, int2{1}) == 2);
return acc - x;
}, vec{1,2,3,4}) == -8);
}
SUBCASE("Operators") {