add <<, >> and <<=, >>= operators

This commit is contained in:
BlackMATov
2021-02-27 12:35:56 +07:00
parent 4f09d7859d
commit 017424641f
5 changed files with 230 additions and 0 deletions

View File

@@ -659,6 +659,44 @@ vec<T, Size>& operator^=(vec<T, Size>& xs, T y);
template < typename T, size_t Size >
vec<T, Size>& operator^=(vec<T, Size>& xs, const vec<T, Size>& ys);
// operator<<
template < typename T, size_t Size >
auto operator<<(const vec<T, Size>& xs, T y);
template < typename T, size_t Size >
auto operator<<(T x, const vec<T, Size>& ys);
template < typename T, size_t Size >
auto operator<<(const vec<T, Size>& xs, const vec<T, Size>& ys);
// operator<<=
template < typename T, size_t Size >
vec<T, Size>& operator<<=(vec<T, Size>& xs, T y);
template < typename T, size_t Size >
vec<T, Size>& operator<<=(vec<T, Size>& xs, const vec<T, Size>& ys);
// operator>>
template < typename T, size_t Size >
auto operator>>(const vec<T, Size>& xs, T y);
template < typename T, size_t Size >
auto operator>>(T x, const vec<T, Size>& ys);
template < typename T, size_t Size >
auto operator>>(const vec<T, Size>& xs, const vec<T, Size>& ys);
// operator>>=
template < typename T, size_t Size >
vec<T, Size>& operator>>=(vec<T, Size>& xs, T y);
template < typename T, size_t Size >
vec<T, Size>& operator>>=(vec<T, Size>& xs, const vec<T, Size>& ys);
// operator&&
template < typename T, size_t Size >
@@ -873,6 +911,44 @@ mat<T, Size>& operator^=(mat<T, Size>& xs, T y);
template < typename T, size_t Size >
mat<T, Size>& operator^=(mat<T, Size>& xs, const mat<T, Size>& ys);
// operator<<
template < typename T, size_t Size >
auto operator<<(const mat<T, Size>& xs, T y);
template < typename T, size_t Size >
auto operator<<(T x, const mat<T, Size>& ys);
template < typename T, size_t Size >
auto operator<<(const mat<T, Size>& xs, const mat<T, Size>& ys);
// operator<<=
template < typename T, size_t Size >
mat<T, Size>& operator<<=(mat<T, Size>& xs, T y);
template < typename T, size_t Size >
mat<T, Size>& operator<<=(mat<T, Size>& xs, const mat<T, Size>& ys);
// operator>>
template < typename T, size_t Size >
auto operator>>(const mat<T, Size>& xs, T y);
template < typename T, size_t Size >
auto operator>>(T x, const mat<T, Size>& ys);
template < typename T, size_t Size >
auto operator>>(const mat<T, Size>& xs, const mat<T, Size>& ys);
// operator>>=
template < typename T, size_t Size >
mat<T, Size>& operator>>=(mat<T, Size>& xs, T y);
template < typename T, size_t Size >
mat<T, Size>& operator>>=(mat<T, Size>& xs, const mat<T, Size>& ys);
// operator&&
template < typename T, size_t Size >

View File

@@ -422,6 +422,64 @@ namespace vmath_hpp
return (xs = (xs ^ ys));
}
// operator<<
template < typename T, std::size_t Size >
[[nodiscard]] constexpr auto 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 auto 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 auto 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 auto 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 auto 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 auto 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 >

View File

@@ -473,6 +473,64 @@ namespace vmath_hpp
return (xs = (xs ^ ys));
}
// operator<<
template < typename T, std::size_t Size >
[[nodiscard]] constexpr auto 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 auto 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 auto 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 auto 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 auto 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 auto 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 >

View File

@@ -43,6 +43,8 @@ TEST_CASE("vmath/mat_fun") {
STATIC_CHECK((imat2(11,12,11,12) & 6) == imat2(2,4,2,4));
STATIC_CHECK((imat2(11,12,11,12) | 6) == imat2(15,14,15,14));
STATIC_CHECK((imat2(11,12,11,12) ^ 6) == imat2(13,10,13,10));
STATIC_CHECK((imat2(1,2,3,4) << 2) == imat2(4,8,12,16));
STATIC_CHECK((imat2(4,8,12,16) >> 2) == imat2(1,2,3,4));
STATIC_CHECK((imat2(1,0,1,0) && 1) == bmat2(1,0,1,0));
STATIC_CHECK((imat2(1,0,1,0) || 1) == bmat2(1,1,1,1));
@@ -53,6 +55,8 @@ TEST_CASE("vmath/mat_fun") {
STATIC_CHECK((6 &imat2(11,12,11,12)) == imat2(2,4,2,4));
STATIC_CHECK((6 |imat2(11,12,11,12)) == imat2(15,14,15,14));
STATIC_CHECK((6 ^ imat2(11,12,11,12)) == imat2(13,10,13,10));
STATIC_CHECK((2 << imat2(1,2,3,4)) == imat2(4,8,16,32));
STATIC_CHECK((16 >> imat2(1,2,3,4)) == imat2(8,4,2,1));
STATIC_CHECK((1 && imat2(1,0,1,0)) == bmat2(1,0,1,0));
STATIC_CHECK((1 || imat2(1,0,1,0)) == bmat2(1,1,1,1));
@@ -149,6 +153,20 @@ TEST_CASE("vmath/mat_fun") {
CHECK(&v2 == &(v2 ^= imat2(11,12,11,12)));
CHECK(v2 == imat2(13,11,13,11));
}
{
imat2 v1{2,3,4,5};
CHECK(&v1 == &(v1 <<= 2));
CHECK(v1 == imat2(8,12,16,20));
CHECK(&v1 == &(v1 <<= imat2(1,2,3,4)));
CHECK(v1 == imat2(16,48,128,320));
}
{
imat2 v1{16,48,128,320};
CHECK(&v1 == &(v1 >>= 2));
CHECK(v1 == imat2(4,12,32,80));
CHECK(&v1 == &(v1 >>= imat2(1,2,3,4)));
CHECK(v1 == imat2(2,3,4,5));
}
}
SUBCASE("Operators2") {

View File

@@ -26,6 +26,8 @@ TEST_CASE("vmath/vec_fun") {
STATIC_CHECK((ivec2(11,12) & 6) == ivec2(2,4));
STATIC_CHECK((ivec2(11,12) | 6) == ivec2(15,14));
STATIC_CHECK((ivec2(11,12) ^ 6) == ivec2(13,10));
STATIC_CHECK((ivec2(11,12) << 2) == ivec2(44,48));
STATIC_CHECK((ivec2(11,12) >> 2) == ivec2(2,3));
STATIC_CHECK((ivec2(1,0) && 1) == bvec2(1,0));
STATIC_CHECK((ivec2(1,0) || 1) == bvec2(1,1));
@@ -36,6 +38,8 @@ TEST_CASE("vmath/vec_fun") {
STATIC_CHECK((6 & ivec2(11,12)) == ivec2(2,4));
STATIC_CHECK((6 | ivec2(11,12)) == ivec2(15,14));
STATIC_CHECK((6 ^ ivec2(11,12)) == ivec2(13,10));
STATIC_CHECK((2 << ivec2(3,4)) == ivec2(16,32));
STATIC_CHECK((48 >> ivec2(3,4)) == ivec2(6,3));
STATIC_CHECK((1 && ivec2(1,0)) == bvec2(1,0));
STATIC_CHECK((1 || ivec2(1,0)) == bvec2(1,1));
@@ -46,6 +50,8 @@ TEST_CASE("vmath/vec_fun") {
STATIC_CHECK((ivec2(6,7) & ivec2(11,12)) == ivec2(2,4));
STATIC_CHECK((ivec2(6,7) | ivec2(11,12)) == ivec2(15,15));
STATIC_CHECK((ivec2(6,7) ^ ivec2(11,12)) == ivec2(13,11));
STATIC_CHECK((ivec2(11,6) << ivec2(2,3)) == ivec2(44,48));
STATIC_CHECK((ivec2(44,48) >> ivec2(2,3)) == ivec2(11,6));
STATIC_CHECK((ivec2(0,1) && ivec2(1,0)) == bvec2(0,0));
STATIC_CHECK((ivec2(0,1) || ivec2(1,0)) == bvec2(1,1));
@@ -115,6 +121,20 @@ TEST_CASE("vmath/vec_fun") {
CHECK(&v2 == &(v2 ^= ivec2(11,12)));
CHECK(v2 == ivec2(13,11));
}
{
ivec2 v1{2,3};
CHECK(&v1 == &(v1 <<= 2));
CHECK(v1 == ivec2(8,12));
CHECK(&v1 == &(v1 <<= ivec2(2,3)));
CHECK(v1 == ivec2(32,96));
}
{
ivec2 v1{32,96};
CHECK(&v1 == &(v1 >>= 2));
CHECK(v1 == ivec2(8,24));
CHECK(&v1 == &(v1 >>= ivec2(2,3)));
CHECK(v1 == ivec2(2,3));
}
}
SUBCASE("Operators2") {