From cc69a590dcaaf6016eb262d68a227d3cf559adf5 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Mon, 7 Dec 2020 15:20:17 +0700 Subject: [PATCH] add vector operators: ~, &, &=, |, |=, ^, ^=, &&, || --- headers/vmath.hpp/vmath_vec_fun.hpp | 146 ++++++++++++++++++++++++++-- untests/vmath_vec_fun_tests.cpp | 40 ++++++++ 2 files changed, 177 insertions(+), 9 deletions(-) diff --git a/headers/vmath.hpp/vmath_vec_fun.hpp b/headers/vmath.hpp/vmath_vec_fun.hpp index 7e6bb00..3958c74 100644 --- a/headers/vmath.hpp/vmath_vec_fun.hpp +++ b/headers/vmath.hpp/vmath_vec_fun.hpp @@ -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 operator~(const vec& 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& operator+=(vec& xs, T y) { - return (xs = xs + y); + return (xs = (xs + y)); } template < typename T, std::size_t Size > constexpr vec& operator+=(vec& xs, const vec& 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& operator-=(vec& xs, T y) { - return (xs = xs - y); + return (xs = (xs - y)); } template < typename T, std::size_t Size > constexpr vec& operator-=(vec& xs, const vec& 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& operator*=(vec& xs, T y) { - return (xs = xs * y); + return (xs = (xs * y)); } template < typename T, std::size_t Size > constexpr vec& operator*=(vec& xs, const vec& 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& operator/=(vec& xs, T y) { - return (xs = xs / y); + return (xs = (xs / y)); } template < typename T, std::size_t Size > constexpr vec& operator/=(vec& xs, const vec& ys) { - return (xs = xs / ys); + return (xs = (xs / ys)); + } + + // operator& + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr vec operator&(const vec& xs, T y) { + return map_join([y](T x){ return x & y; }, xs); + } + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr vec operator&(T x, const vec& ys) { + return map_join([x](T y){ return x & y; }, ys); + } + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr vec operator&(const vec& xs, const vec& ys) { + return map_join([](T x, T y){ return x & y; }, xs, ys); + } + + // operator&= + + template < typename T, std::size_t Size > + constexpr vec& operator&=(vec& xs, T y) { + return (xs = (xs & y)); + } + + template < typename T, std::size_t Size > + constexpr vec& operator&=(vec& xs, const vec& ys) { + return (xs = (xs & ys)); + } + + // operator| + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr vec operator|(const vec& xs, T y) { + return map_join([y](T x){ return x | y; }, xs); + } + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr vec operator|(T x, const vec& ys) { + return map_join([x](T y){ return x | y; }, ys); + } + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr vec operator|(const vec& xs, const vec& ys) { + return map_join([](T x, T y){ return x | y; }, xs, ys); + } + + // operator|= + + template < typename T, std::size_t Size > + constexpr vec& operator|=(vec& xs, T y) { + return (xs = (xs | y)); + } + + template < typename T, std::size_t Size > + constexpr vec& operator|=(vec& xs, const vec& ys) { + return (xs = (xs | ys)); + } + + // operator^ + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr vec operator^(const vec& xs, T y) { + return map_join([y](T x){ return x ^ y; }, xs); + } + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr vec operator^(T x, const vec& ys) { + return map_join([x](T y){ return x ^ y; }, ys); + } + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr vec operator^(const vec& xs, const vec& ys) { + return map_join([](T x, T y){ return x ^ y; }, xs, ys); + } + + // operator^= + + template < typename T, std::size_t Size > + constexpr vec& operator^=(vec& xs, T y) { + return (xs = (xs ^ y)); + } + + template < typename T, std::size_t Size > + constexpr vec& operator^=(vec& xs, const vec& ys) { + return (xs = (xs ^ ys)); + } + + // operator&& + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr vec operator&&(const vec& xs, T y) { + return map_join([y](T x){ return x && y; }, xs); + } + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr vec operator&&(T x, const vec& ys) { + return map_join([x](T y){ return x && y; }, ys); + } + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr vec operator&&(const vec& xs, const vec& ys) { + return map_join([](T x, T y){ return x && y; }, xs, ys); + } + + // operator|| + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr vec operator||(const vec& xs, T y) { + return map_join([y](T x){ return x || y; }, xs); + } + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr vec operator||(T x, const vec& ys) { + return map_join([x](T y){ return x || y; }, ys); + } + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr vec operator||(const vec& xs, const vec& 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& xs, const vec& ys) { return fold_join([](bool acc, T x, T y){ - return acc && equal_to(x, y); + return acc && x == y; }, true, xs, ys); } diff --git a/untests/vmath_vec_fun_tests.cpp b/untests/vmath_vec_fun_tests.cpp index ba1a79d..9de7d7e 100644 --- a/untests/vmath_vec_fun_tests.cpp +++ b/untests/vmath_vec_fun_tests.cpp @@ -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") {