add matrix operators: ~, &, &=, |, |=, ^, ^=, &&, ||

This commit is contained in:
BlackMATov
2020-12-07 15:37:31 +07:00
parent cc69a590dc
commit 439a10d95e
2 changed files with 178 additions and 9 deletions

View File

@@ -199,6 +199,13 @@ namespace vmath_hpp
return map_join([](const vec<T, Size>& x){ return -x; }, xs);
}
// ~operator
template < typename T, std::size_t Size >
[[nodiscard]] constexpr mat<T, Size> operator~(const mat<T, Size>& xs) {
return map_join([](const vec<T, Size>& x){ return ~x; }, xs);
}
// !operator
template < typename T, std::size_t Size >
@@ -227,12 +234,12 @@ namespace vmath_hpp
template < typename T, std::size_t Size >
constexpr mat<T, Size>& operator+=(mat<T, Size>& xs, T y) {
return (xs = xs + y);
return (xs = (xs + y));
}
template < typename T, std::size_t Size >
constexpr mat<T, Size>& operator+=(mat<T, Size>& xs, const mat<T, Size>& ys) {
return (xs = xs + ys);
return (xs = (xs + ys));
}
// operator-
@@ -256,12 +263,12 @@ namespace vmath_hpp
template < typename T, std::size_t Size >
constexpr mat<T, Size>& operator-=(mat<T, Size>& xs, T y) {
return (xs = xs - y);
return (xs = (xs - y));
}
template < typename T, std::size_t Size >
constexpr mat<T, Size>& operator-=(mat<T, Size>& xs, const mat<T, Size>& ys) {
return (xs = xs - ys);
return (xs = (xs - ys));
}
// operator*
@@ -294,17 +301,17 @@ namespace vmath_hpp
template < typename T, std::size_t Size >
constexpr mat<T, Size>& operator*=(mat<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 mat<T, Size>& ys) {
return (xs = xs * ys);
return (xs = (xs * ys));
}
template < typename T, std::size_t Size >
constexpr mat<T, Size>& operator*=(mat<T, Size>& xs, const mat<T, Size>& ys) {
return (xs = xs * ys);
return (xs = (xs * ys));
}
// operator/
@@ -328,12 +335,133 @@ namespace vmath_hpp
template < typename T, std::size_t Size >
constexpr mat<T, Size>& operator/=(mat<T, Size>& xs, T y) {
return (xs = xs / y);
return (xs = (xs / y));
}
template < typename T, std::size_t Size >
constexpr mat<T, Size>& operator/=(mat<T, Size>& xs, const mat<T, Size>& ys) {
return (xs = xs / ys);
return (xs = (xs / ys));
}
// operator&
template < typename T, std::size_t Size >
[[nodiscard]] constexpr mat<T, Size> operator&(const mat<T, Size>& xs, T y) {
return map_join([y](const vec<T, Size>& x){ return x & y; }, xs);
}
template < typename T, std::size_t Size >
[[nodiscard]] constexpr mat<T, Size> operator&(T x, const mat<T, Size>& ys) {
return map_join([x](const vec<T, Size>& y){ return x & y; }, ys);
}
template < typename T, std::size_t Size >
[[nodiscard]] constexpr mat<T, Size> operator&(const mat<T, Size>& xs, const mat<T, Size>& ys) {
return map_join([](const vec<T, Size>& x, const vec<T, Size>& y){ return x & y; }, xs, ys);
}
// operator&=
template < typename T, std::size_t Size >
constexpr mat<T, Size>& operator&=(mat<T, Size>& xs, T y) {
return (xs = (xs & y));
}
template < typename T, std::size_t Size >
constexpr mat<T, Size>& operator&=(mat<T, Size>& xs, const mat<T, Size>& ys) {
return (xs = (xs & ys));
}
// operator|
template < typename T, std::size_t Size >
[[nodiscard]] constexpr mat<T, Size> operator|(const mat<T, Size>& xs, T y) {
return map_join([y](const vec<T, Size>& x){ return x | y; }, xs);
}
template < typename T, std::size_t Size >
[[nodiscard]] constexpr mat<T, Size> operator|(T x, const mat<T, Size>& ys) {
return map_join([x](const vec<T, Size>& y){ return x | y; }, ys);
}
template < typename T, std::size_t Size >
[[nodiscard]] constexpr mat<T, Size> operator|(const mat<T, Size>& xs, const mat<T, Size>& ys) {
return map_join([](const vec<T, Size>& x, const vec<T, Size>& y){ return x | y; }, xs, ys);
}
// operator|=
template < typename T, std::size_t Size >
constexpr mat<T, Size>& operator|=(mat<T, Size>& xs, T y) {
return (xs = (xs | y));
}
template < typename T, std::size_t Size >
constexpr mat<T, Size>& operator|=(mat<T, Size>& xs, const mat<T, Size>& ys) {
return (xs = (xs | ys));
}
// operator^
template < typename T, std::size_t Size >
[[nodiscard]] constexpr mat<T, Size> operator^(const mat<T, Size>& xs, T y) {
return map_join([y](const vec<T, Size>& x){ return x ^ y; }, xs);
}
template < typename T, std::size_t Size >
[[nodiscard]] constexpr mat<T, Size> operator^(T x, const mat<T, Size>& ys) {
return map_join([x](const vec<T, Size>& y){ return x ^ y; }, ys);
}
template < typename T, std::size_t Size >
[[nodiscard]] constexpr mat<T, Size> operator^(const mat<T, Size>& xs, const mat<T, Size>& ys) {
return map_join([](const vec<T, Size>& x, const vec<T, Size>& y){ return x ^ y; }, xs, ys);
}
// operator^=
template < typename T, std::size_t Size >
constexpr mat<T, Size>& operator^=(mat<T, Size>& xs, T y) {
return (xs = (xs ^ y));
}
template < typename T, std::size_t Size >
constexpr mat<T, Size>& operator^=(mat<T, Size>& xs, const mat<T, Size>& ys) {
return (xs = (xs ^ ys));
}
// operator&&
template < typename T, std::size_t Size >
[[nodiscard]] constexpr mat<bool, Size> operator&&(const mat<T, Size>& xs, T y) {
return map_join([y](const vec<T, Size>& x){ return x && y; }, xs);
}
template < typename T, std::size_t Size >
[[nodiscard]] constexpr mat<bool, Size> operator&&(T x, const mat<T, Size>& ys) {
return map_join([x](const vec<T, Size>& y){ return x && y; }, ys);
}
template < typename T, std::size_t Size >
[[nodiscard]] constexpr mat<bool, Size> operator&&(const mat<T, Size>& xs, const mat<T, Size>& ys) {
return map_join([](const vec<T, Size>& x, const vec<T, Size>& y){ return x && y; }, xs, ys);
}
// operator||
template < typename T, std::size_t Size >
[[nodiscard]] constexpr mat<bool, Size> operator||(const mat<T, Size>& xs, T y) {
return map_join([y](const vec<T, Size>& x){ return x || y; }, xs);
}
template < typename T, std::size_t Size >
[[nodiscard]] constexpr mat<bool, Size> operator||(T x, const mat<T, Size>& ys) {
return map_join([x](const vec<T, Size>& y){ return x || y; }, ys);
}
template < typename T, std::size_t Size >
[[nodiscard]] constexpr mat<bool, Size> operator||(const mat<T, Size>& xs, const mat<T, Size>& ys) {
return map_join([](const vec<T, Size>& x, const vec<T, Size>& y){ return x || y; }, xs, ys);
}
// operator==

View File

@@ -59,17 +59,28 @@ TEST_CASE("vmath/mat_fun") {
SUBCASE("operators") {
STATIC_REQUIRE(-int2x2(1,2,3,4) == int2x2(-1,-2,-3,-4));
STATIC_REQUIRE(~uint2x2(0xF0F0F0F0,0x0F0F0F0F,0xF0F0F0F0,0x0F0F0F0F) == uint2x2(0x0F0F0F0F,0xF0F0F0F0,0x0F0F0F0F,0xF0F0F0F0));
STATIC_REQUIRE(!int2x2(-1,0,1,2) == bool2x2(false,true,false,false));
STATIC_REQUIRE(int2x2(1,2,3,4) + 2 == int2x2(3,4,5,6));
STATIC_REQUIRE(int2x2(1,2,3,4) - 2 == int2x2(-1,0,1,2));
STATIC_REQUIRE(int2x2(1,2,3,4) * 2 == int2x2(2,4,6,8));
STATIC_REQUIRE(int2x2(1,2,3,4) / 2 == int2x2(0,1,1,2));
STATIC_REQUIRE((int2x2(11,12,11,12) & 6) == int2x2(2,4,2,4));
STATIC_REQUIRE((int2x2(11,12,11,12) | 6) == int2x2(15,14,15,14));
STATIC_REQUIRE((int2x2(11,12,11,12) ^ 6) == int2x2(13,10,13,10));
STATIC_REQUIRE((int2x2(1,0,1,0) && 1) == bool2x2(1,0,1,0));
STATIC_REQUIRE((int2x2(1,0,1,0) || 1) == bool2x2(1,1,1,1));
STATIC_REQUIRE(4 + int2x2(1,2,3,4) == int2x2(5,6,7,8));
STATIC_REQUIRE(4 - int2x2(1,2,3,4) == int2x2(3,2,1,0));
STATIC_REQUIRE(4 * int2x2(1,2,3,4) == int2x2(4,8,12,16));
STATIC_REQUIRE(4 / int2x2(1,2,3,4) == int2x2(4,2,1,1));
STATIC_REQUIRE((6 &int2x2(11,12,11,12)) == int2x2(2,4,2,4));
STATIC_REQUIRE((6 |int2x2(11,12,11,12)) == int2x2(15,14,15,14));
STATIC_REQUIRE((6 ^ int2x2(11,12,11,12)) == int2x2(13,10,13,10));
STATIC_REQUIRE((1 && int2x2(1,0,1,0)) == bool2x2(1,0,1,0));
STATIC_REQUIRE((1 || int2x2(1,0,1,0)) == bool2x2(1,1,1,1));
STATIC_REQUIRE(int2x2(1,2,3,4) + int2x2(5,6,7,8) == int2x2(6,8,10,12));
STATIC_REQUIRE(int2x2(1,2,3,4) - int2x2(5,6,7,8) == int2x2(-4,-4,-4,-4));
@@ -82,6 +93,12 @@ TEST_CASE("vmath/mat_fun") {
STATIC_REQUIRE(int3(1,2,3) * int3x3() == int3(1,2,3));
STATIC_REQUIRE(int4(1,2,3,4) * int4x4() == int4(1,2,3,4));
STATIC_REQUIRE((int2x2(6,7,6,7) & int2x2(11,12,11,12)) == int2x2(2,4,2,4));
STATIC_REQUIRE((int2x2(6,7,6,7) | int2x2(11,12,11,12)) == int2x2(15,15,15,15));
STATIC_REQUIRE((int2x2(6,7,6,7) ^ int2x2(11,12,11,12)) == int2x2(13,11,13,11));
STATIC_REQUIRE((int2x2(0,1,0,1) && int2x2(1,0,1,0)) == bool2x2(0,0,0,0));
STATIC_REQUIRE((int2x2(0,1,0,1) || int2x2(1,0,1,0)) == bool2x2(1,1,1,1));
{
int2x2 v{1,2,3,4};
REQUIRE(&v == &(v += 3));
@@ -129,6 +146,30 @@ TEST_CASE("vmath/mat_fun") {
REQUIRE(&v == &(v *= int3x3(scale(int3{2,3,4}))));
REQUIRE(v == int3x3(scale(int3{2,6,12})));
}
{
int2x2 v1{11,12,11,12};
REQUIRE(&v1 == &(v1 &= 6));
REQUIRE(v1 == int2x2(2,4,2,4));
int2x2 v2{6,7,6,7};
REQUIRE(&v2 == &(v2 &= int2x2(11,12,11,12)));
REQUIRE(v2 == int2x2(2,4,2,4));
}
{
int2x2 v1{11,12,11,12};
REQUIRE(&v1 == &(v1 |= 6));
REQUIRE(v1 == int2x2(15,14,15,14));
int2x2 v2{6,7,6,7};
REQUIRE(&v2 == &(v2 |= int2x2(11,12,11,12)));
REQUIRE(v2 == int2x2(15,15,15,15));
}
{
int2x2 v1{11,12,11,12};
REQUIRE(&v1 == &(v1 ^= 6));
REQUIRE(v1 == int2x2(13,10,13,10));
int2x2 v2{6,7,6,7};
REQUIRE(&v2 == &(v2 ^= int2x2(11,12,11,12)));
REQUIRE(v2 == int2x2(13,11,13,11));
}
}
SUBCASE("transpose") {