qua without fold details

This commit is contained in:
BlackMATov
2021-01-26 01:45:56 +07:00
parent 55d5dfd9a3
commit ec5d2d59e3
5 changed files with 77 additions and 277 deletions

View File

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

View File

@@ -11,57 +11,15 @@
#include "vmath_vec.hpp"
#include "vmath_vec_fun.hpp"
namespace vmath_hpp::detail
{
template < typename T >
class qua_base {
public:
T x{0}, y{0}, z{0}, w{1};
public:
constexpr qua_base() = default;
constexpr explicit qua_base(T w)
: x{0}, y{0}, z{0}, w{w} {}
constexpr qua_base(T x, T y, T z, T w)
: x{x}, y{y}, z{z}, w{w} {}
constexpr qua_base(const vec<T, 3>& xyz, T w)
: x{xyz[0]}, y{xyz[1]}, z{xyz[2]}, w{w} {}
constexpr qua_base(const vec<T, 4>& xyzw)
: x{xyzw[0]}, y{xyzw[1]}, z{xyzw[2]}, w{xyzw[3]} {}
[[nodiscard]] constexpr T& operator[](std::size_t index) noexcept {
switch ( index ) {
default:
case 0: return x;
case 1: return y;
case 2: return z;
case 3: return w;
}
}
[[nodiscard]] constexpr const T& operator[](std::size_t index) const noexcept {
switch ( index ) {
default:
case 0: return x;
case 1: return y;
case 2: return z;
case 3: return w;
}
}
};
}
namespace vmath_hpp
{
template < typename T >
class qua final : public detail::qua_base<T> {
class qua final {
public:
vec<T, 3> v{0};
T s{1};
public:
using self_type = qua;
using base_type = detail::qua_base<T>;
public:
using component_type = T;
using pointer = component_type*;
@@ -77,13 +35,26 @@ 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;
@@ -114,6 +85,26 @@ namespace vmath_hpp
return &(*this)[0];
}
[[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;
}
}
[[nodiscard]] constexpr reference at(std::size_t index) {
if ( index >= size ) {
throw std::out_of_range("qua::at");
@@ -132,10 +123,12 @@ namespace vmath_hpp
namespace vmath_hpp
{
// qua
// vec
template < typename T >
qua(T) -> qua<T>;
vec(const qua<T>&) -> vec<T, 4>;
// qua
template < typename T >
qua(T, T, T, T) -> qua<T>;

View File

@@ -11,153 +11,8 @@
#include "vmath_fun.hpp"
#include "vmath_qua.hpp"
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 >
[[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE
auto fold_join_impl(
F&& f,
A init,
const qua<B>& b,
std::index_sequence<Is...>
) -> A {
return ((init = f(std::move(init), b[Is])), ...);
}
template < typename A, typename B, typename C, typename F, std::size_t... Is >
[[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE
auto fold_join_impl(
F&& f,
A init,
const qua<B>& b,
const qua<C>& c,
std::index_sequence<Is...>
) -> 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>& a,
std::index_sequence<I, Is...>
) -> 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>& 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 >
[[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE
auto fold_join(
F&& f,
A init,
const qua<B>& b
) {
return impl::fold_join_impl(
std::forward<F>(f), std::move(init), b, std::make_index_sequence<4>{});
}
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 impl::fold_join_impl(
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>{});
}
}
#include "vmath_vec.hpp"
#include "vmath_vec_fun.hpp"
//
// Operators
@@ -176,14 +31,14 @@ namespace vmath_hpp
template < typename T >
[[nodiscard]] constexpr qua<T> operator-(const qua<T>& xs) {
return map_join([](T x){ return -x; }, xs);
return qua(-vec<T, 4>{xs});
}
// operator+
template < typename T >
[[nodiscard]] constexpr qua<T> operator+(const qua<T>& xs, const qua<T>& ys) {
return map_join([](T x, T y){ return x + y; }, xs, ys);
return qua(vec{xs} + vec{ys});
}
// operator+=
@@ -197,7 +52,7 @@ namespace vmath_hpp
template < typename T >
[[nodiscard]] constexpr qua<T> operator-(const qua<T>& xs, const qua<T>& ys) {
return map_join([](T x, T y){ return x - y; }, xs, ys);
return qua(vec{xs} - vec{ys});
}
// operator-=
@@ -211,32 +66,23 @@ namespace vmath_hpp
template < typename T >
[[nodiscard]] constexpr bool operator==(const qua<T>& xs, const qua<T>& ys) {
return fold_join([](bool acc, T x, T y){
return acc && x == y;
}, true, xs, ys);
return vec{xs} == vec{ys};
}
// operator!=
template < typename T >
[[nodiscard]] constexpr bool operator!=(const qua<T>& xs, const qua<T>& ys) {
return fold_join([](bool acc, T x, T y){
return acc || x != y;
}, false, xs, ys);
return vec{xs} != vec{ys};
}
// operator<
template < typename T >
[[nodiscard]] constexpr bool operator<(const qua<T>& xs, const qua<T>& ys) {
for ( std::size_t i = 0; i < 4; ++i ) {
if ( xs[i] < ys[i] ) {
return true;
}
if ( ys[i] < xs[i] ) {
return false;
}
}
return false;
return vec{xs} < vec{ys};
}
}
}
}