mirror of
https://github.com/BlackMATov/vmath.hpp.git
synced 2025-12-14 20:31:25 +07:00
add vector operators: ~, &, &=, |, |=, ^, ^=, &&, ||
This commit is contained in:
@@ -172,6 +172,13 @@ namespace vmath_hpp
|
||||
return map_join([](T x){ return -x; }, xs);
|
||||
}
|
||||
|
||||
// ~operator
|
||||
|
||||
template < typename T, std::size_t Size >
|
||||
[[nodiscard]] constexpr vec<T, Size> operator~(const vec<T, Size>& xs) {
|
||||
return map_join([](T x){ return ~x; }, xs);
|
||||
}
|
||||
|
||||
// !operator
|
||||
|
||||
template < typename T, std::size_t Size >
|
||||
@@ -200,12 +207,12 @@ namespace vmath_hpp
|
||||
|
||||
template < typename T, std::size_t Size >
|
||||
constexpr vec<T, Size>& operator+=(vec<T, Size>& xs, T y) {
|
||||
return (xs = xs + y);
|
||||
return (xs = (xs + y));
|
||||
}
|
||||
|
||||
template < typename T, std::size_t Size >
|
||||
constexpr vec<T, Size>& operator+=(vec<T, Size>& xs, const vec<T, Size>& ys) {
|
||||
return (xs = xs + ys);
|
||||
return (xs = (xs + ys));
|
||||
}
|
||||
|
||||
// operator-
|
||||
@@ -229,12 +236,12 @@ namespace vmath_hpp
|
||||
|
||||
template < typename T, std::size_t Size >
|
||||
constexpr vec<T, Size>& operator-=(vec<T, Size>& xs, T y) {
|
||||
return (xs = xs - y);
|
||||
return (xs = (xs - y));
|
||||
}
|
||||
|
||||
template < typename T, std::size_t Size >
|
||||
constexpr vec<T, Size>& operator-=(vec<T, Size>& xs, const vec<T, Size>& ys) {
|
||||
return (xs = xs - ys);
|
||||
return (xs = (xs - ys));
|
||||
}
|
||||
|
||||
// operator*
|
||||
@@ -258,12 +265,12 @@ namespace vmath_hpp
|
||||
|
||||
template < typename T, std::size_t Size >
|
||||
constexpr vec<T, Size>& operator*=(vec<T, Size>& xs, T y) {
|
||||
return (xs = xs * y);
|
||||
return (xs = (xs * y));
|
||||
}
|
||||
|
||||
template < typename T, std::size_t Size >
|
||||
constexpr vec<T, Size>& operator*=(vec<T, Size>& xs, const vec<T, Size>& ys) {
|
||||
return (xs = xs * ys);
|
||||
return (xs = (xs * ys));
|
||||
}
|
||||
|
||||
// operator/
|
||||
@@ -287,12 +294,133 @@ namespace vmath_hpp
|
||||
|
||||
template < typename T, std::size_t Size >
|
||||
constexpr vec<T, Size>& operator/=(vec<T, Size>& xs, T y) {
|
||||
return (xs = xs / y);
|
||||
return (xs = (xs / y));
|
||||
}
|
||||
|
||||
template < typename T, std::size_t Size >
|
||||
constexpr vec<T, Size>& operator/=(vec<T, Size>& xs, const vec<T, Size>& ys) {
|
||||
return (xs = xs / ys);
|
||||
return (xs = (xs / ys));
|
||||
}
|
||||
|
||||
// operator&
|
||||
|
||||
template < typename T, std::size_t Size >
|
||||
[[nodiscard]] constexpr vec<T, Size> operator&(const vec<T, Size>& xs, T y) {
|
||||
return map_join([y](T x){ return x & y; }, xs);
|
||||
}
|
||||
|
||||
template < typename T, std::size_t Size >
|
||||
[[nodiscard]] constexpr vec<T, Size> operator&(T x, const vec<T, Size>& ys) {
|
||||
return map_join([x](T y){ return x & y; }, ys);
|
||||
}
|
||||
|
||||
template < typename T, std::size_t Size >
|
||||
[[nodiscard]] constexpr vec<T, Size> operator&(const vec<T, Size>& xs, const vec<T, Size>& ys) {
|
||||
return map_join([](T x, T y){ return x & y; }, xs, ys);
|
||||
}
|
||||
|
||||
// operator&=
|
||||
|
||||
template < typename T, std::size_t Size >
|
||||
constexpr vec<T, Size>& operator&=(vec<T, Size>& xs, T y) {
|
||||
return (xs = (xs & y));
|
||||
}
|
||||
|
||||
template < typename T, std::size_t Size >
|
||||
constexpr vec<T, Size>& operator&=(vec<T, Size>& xs, const vec<T, Size>& ys) {
|
||||
return (xs = (xs & ys));
|
||||
}
|
||||
|
||||
// operator|
|
||||
|
||||
template < typename T, std::size_t Size >
|
||||
[[nodiscard]] constexpr vec<T, Size> operator|(const vec<T, Size>& xs, T y) {
|
||||
return map_join([y](T x){ return x | y; }, xs);
|
||||
}
|
||||
|
||||
template < typename T, std::size_t Size >
|
||||
[[nodiscard]] constexpr vec<T, Size> operator|(T x, const vec<T, Size>& ys) {
|
||||
return map_join([x](T y){ return x | y; }, ys);
|
||||
}
|
||||
|
||||
template < typename T, std::size_t Size >
|
||||
[[nodiscard]] constexpr vec<T, Size> operator|(const vec<T, Size>& xs, const vec<T, Size>& ys) {
|
||||
return map_join([](T x, T y){ return x | y; }, xs, ys);
|
||||
}
|
||||
|
||||
// operator|=
|
||||
|
||||
template < typename T, std::size_t Size >
|
||||
constexpr vec<T, Size>& operator|=(vec<T, Size>& xs, T y) {
|
||||
return (xs = (xs | y));
|
||||
}
|
||||
|
||||
template < typename T, std::size_t Size >
|
||||
constexpr vec<T, Size>& operator|=(vec<T, Size>& xs, const vec<T, Size>& ys) {
|
||||
return (xs = (xs | ys));
|
||||
}
|
||||
|
||||
// operator^
|
||||
|
||||
template < typename T, std::size_t Size >
|
||||
[[nodiscard]] constexpr vec<T, Size> operator^(const vec<T, Size>& xs, T y) {
|
||||
return map_join([y](T x){ return x ^ y; }, xs);
|
||||
}
|
||||
|
||||
template < typename T, std::size_t Size >
|
||||
[[nodiscard]] constexpr vec<T, Size> operator^(T x, const vec<T, Size>& ys) {
|
||||
return map_join([x](T y){ return x ^ y; }, ys);
|
||||
}
|
||||
|
||||
template < typename T, std::size_t Size >
|
||||
[[nodiscard]] constexpr vec<T, Size> operator^(const vec<T, Size>& xs, const vec<T, Size>& ys) {
|
||||
return map_join([](T x, T y){ return x ^ y; }, xs, ys);
|
||||
}
|
||||
|
||||
// operator^=
|
||||
|
||||
template < typename T, std::size_t Size >
|
||||
constexpr vec<T, Size>& operator^=(vec<T, Size>& xs, T y) {
|
||||
return (xs = (xs ^ y));
|
||||
}
|
||||
|
||||
template < typename T, std::size_t Size >
|
||||
constexpr vec<T, Size>& operator^=(vec<T, Size>& xs, const vec<T, Size>& ys) {
|
||||
return (xs = (xs ^ ys));
|
||||
}
|
||||
|
||||
// operator&&
|
||||
|
||||
template < typename T, std::size_t Size >
|
||||
[[nodiscard]] constexpr vec<bool, Size> operator&&(const vec<T, Size>& xs, T y) {
|
||||
return map_join([y](T x){ return x && y; }, xs);
|
||||
}
|
||||
|
||||
template < typename T, std::size_t Size >
|
||||
[[nodiscard]] constexpr vec<bool, Size> operator&&(T x, const vec<T, Size>& ys) {
|
||||
return map_join([x](T y){ return x && y; }, ys);
|
||||
}
|
||||
|
||||
template < typename T, std::size_t Size >
|
||||
[[nodiscard]] constexpr vec<bool, Size> operator&&(const vec<T, Size>& xs, const vec<T, Size>& ys) {
|
||||
return map_join([](T x, T y){ return x && y; }, xs, ys);
|
||||
}
|
||||
|
||||
// operator||
|
||||
|
||||
template < typename T, std::size_t Size >
|
||||
[[nodiscard]] constexpr vec<bool, Size> operator||(const vec<T, Size>& xs, T y) {
|
||||
return map_join([y](T x){ return x || y; }, xs);
|
||||
}
|
||||
|
||||
template < typename T, std::size_t Size >
|
||||
[[nodiscard]] constexpr vec<bool, Size> operator||(T x, const vec<T, Size>& ys) {
|
||||
return map_join([x](T y){ return x || y; }, ys);
|
||||
}
|
||||
|
||||
template < typename T, std::size_t Size >
|
||||
[[nodiscard]] constexpr vec<bool, Size> operator||(const vec<T, Size>& xs, const vec<T, Size>& ys) {
|
||||
return map_join([](T x, T y){ return x || y; }, xs, ys);
|
||||
}
|
||||
|
||||
// operator==
|
||||
@@ -300,7 +428,7 @@ namespace vmath_hpp
|
||||
template < typename T, std::size_t Size >
|
||||
[[nodiscard]] constexpr bool operator==(const vec<T, Size>& xs, const vec<T, Size>& ys) {
|
||||
return fold_join([](bool acc, T x, T y){
|
||||
return acc && equal_to(x, y);
|
||||
return acc && x == y;
|
||||
}, true, xs, ys);
|
||||
}
|
||||
|
||||
|
||||
@@ -42,22 +42,38 @@ TEST_CASE("vmath/vec_fun") {
|
||||
|
||||
SUBCASE("Operators") {
|
||||
STATIC_REQUIRE(-int2(1,-2) == int2(-1,2));
|
||||
STATIC_REQUIRE(~uint2(0xF0F0F0F0,0x0F0F0F0F) == uint2(0x0F0F0F0F,0xF0F0F0F0));
|
||||
STATIC_REQUIRE(!int3(-1,0,1) == bool3(false, true, false));
|
||||
|
||||
STATIC_REQUIRE(int2(1,2) + 3 == int2(4,5));
|
||||
STATIC_REQUIRE(int2(1,2) - 3 == int2(-2,-1));
|
||||
STATIC_REQUIRE(int2(1,2) * 3 == int2(3,6));
|
||||
STATIC_REQUIRE(int2(2,4) / 2 == int2(1,2));
|
||||
STATIC_REQUIRE((int2(11,12) & 6) == int2(2,4));
|
||||
STATIC_REQUIRE((int2(11,12) | 6) == int2(15,14));
|
||||
STATIC_REQUIRE((int2(11,12) ^ 6) == int2(13,10));
|
||||
STATIC_REQUIRE((int2(1,0) && 1) == bool2(1,0));
|
||||
STATIC_REQUIRE((int2(1,0) || 1) == bool2(1,1));
|
||||
|
||||
STATIC_REQUIRE(3 + int2(1,2) == int2(4,5));
|
||||
STATIC_REQUIRE(3 - int2(1,2) == int2(2,1));
|
||||
STATIC_REQUIRE(3 * int2(1,2) == int2(3,6));
|
||||
STATIC_REQUIRE(4 / int2(2,4) == int2(2,1));
|
||||
STATIC_REQUIRE((6 & int2(11,12)) == int2(2,4));
|
||||
STATIC_REQUIRE((6 | int2(11,12)) == int2(15,14));
|
||||
STATIC_REQUIRE((6 ^ int2(11,12)) == int2(13,10));
|
||||
STATIC_REQUIRE((1 && int2(1,0)) == bool2(1,0));
|
||||
STATIC_REQUIRE((1 || int2(1,0)) == bool2(1,1));
|
||||
|
||||
STATIC_REQUIRE(int2(1,2) + int2(3,4) == int2(4,6));
|
||||
STATIC_REQUIRE(int2(1,2) - int2(3,4) == int2(-2,-2));
|
||||
STATIC_REQUIRE(int2(1,2) * int2(3,4) == int2(3,8));
|
||||
STATIC_REQUIRE(int2(3,4) / int2(1,2) == int2(3,2));
|
||||
STATIC_REQUIRE((int2(6,7) & int2(11,12)) == int2(2,4));
|
||||
STATIC_REQUIRE((int2(6,7) | int2(11,12)) == int2(15,15));
|
||||
STATIC_REQUIRE((int2(6,7) ^ int2(11,12)) == int2(13,11));
|
||||
STATIC_REQUIRE((int2(0,1) && int2(1,0)) == bool2(0,0));
|
||||
STATIC_REQUIRE((int2(0,1) || int2(1,0)) == bool2(1,1));
|
||||
|
||||
{
|
||||
int2 v{1,2};
|
||||
@@ -87,6 +103,30 @@ TEST_CASE("vmath/vec_fun") {
|
||||
REQUIRE(&v == &(v /= int2{3,4}));
|
||||
REQUIRE(v == int2{1,2});
|
||||
}
|
||||
{
|
||||
int2 v1{11,12};
|
||||
REQUIRE(&v1 == &(v1 &= 6));
|
||||
REQUIRE(v1 == int2(2,4));
|
||||
int2 v2{6,7};
|
||||
REQUIRE(&v2 == &(v2 &= int2(11,12)));
|
||||
REQUIRE(v2 == int2(2,4));
|
||||
}
|
||||
{
|
||||
int2 v1{11,12};
|
||||
REQUIRE(&v1 == &(v1 |= 6));
|
||||
REQUIRE(v1 == int2(15,14));
|
||||
int2 v2{6,7};
|
||||
REQUIRE(&v2 == &(v2 |= int2(11,12)));
|
||||
REQUIRE(v2 == int2(15,15));
|
||||
}
|
||||
{
|
||||
int2 v1{11,12};
|
||||
REQUIRE(&v1 == &(v1 ^= 6));
|
||||
REQUIRE(v1 == int2(13,10));
|
||||
int2 v2{6,7};
|
||||
REQUIRE(&v2 == &(v2 ^= int2(11,12)));
|
||||
REQUIRE(v2 == int2(13,11));
|
||||
}
|
||||
}
|
||||
|
||||
SUBCASE("Angle and Trigonometry Functions") {
|
||||
|
||||
Reference in New Issue
Block a user