diff --git a/include/vectorwrapper/vectorwrapper.hpp b/include/vectorwrapper/vectorwrapper.hpp index 9d32032..2feb29d 100644 --- a/include/vectorwrapper/vectorwrapper.hpp +++ b/include/vectorwrapper/vectorwrapper.hpp @@ -436,6 +436,27 @@ namespace vwr { Vec::type> operator/ ( const Vec& parLeft, const Vec& parRight ); template Vec::type> operator% ( const Vec& parLeft, const Vec& parRight ); + + template + Vec operator+ ( const Vec& parLeft, const typename Vec::scalar_type& parRight ); + template + Vec operator- ( const Vec& parLeft, const typename Vec::scalar_type& parRight ); + template + Vec operator* ( const Vec& parLeft, const typename Vec::scalar_type& parRight ); + template + Vec operator/ ( const Vec& parLeft, const typename Vec::scalar_type& parRight ); + template + Vec operator% ( const Vec& parLeft, const typename Vec::scalar_type& parRight ); + template + Vec operator+ ( const typename Vec::scalar_type& parLeft, const Vec& parRight ); + template + Vec operator- ( const typename Vec::scalar_type& parLeft, const Vec& parRight ); + template + Vec operator* ( const typename Vec::scalar_type& parLeft, const Vec& parRight ); + template + Vec operator/ ( const typename Vec::scalar_type& parLeft, const Vec& parRight ); + template + Vec operator% ( const typename Vec::scalar_type& parLeft, const Vec& parRight ); } //namespace vwr #include "vectorwrapper/vectorwrapper.inl" diff --git a/include/vectorwrapper/vectorwrapper.inl b/include/vectorwrapper/vectorwrapper.inl index 85032aa..ccca6e1 100644 --- a/include/vectorwrapper/vectorwrapper.inl +++ b/include/vectorwrapper/vectorwrapper.inl @@ -299,6 +299,20 @@ namespace vwr { return return_type(parOp(parLeft[I], parRight[I])...); } + template + inline + Vec binary_op_scalar_right (const Vec& parLeft, const typename Vec::scalar_type& parRight, Op parOp, bt::number_seq) { + typedef Vec return_type; + return return_type(parOp(parLeft[I], parRight)...); + } + + template + inline + Vec binary_op_scalar_left (const typename Vec::scalar_type& parLeft, const Vec& parRight, Op parOp, bt::number_seq) { + typedef Vec return_type; + return return_type(parOp(parLeft, parRight[I])...); + } + template inline Vec unary_operator_minus (const Vec& parVec, bt::number_seq) { @@ -479,4 +493,96 @@ namespace vwr { bt::number_range::dimensions>() ); } + + template + inline Vec operator+ (const Vec& parLeft, const typename Vec::scalar_type& parRight) { + return implem::binary_op_scalar_right( + parLeft, + parRight, + std::plus::scalar_type>(), + bt::number_range::dimensions>() + ); + } + template + inline Vec operator- (const Vec& parLeft, const typename Vec::scalar_type& parRight) { + return implem::binary_op_scalar_right( + parLeft, + parRight, + std::minus::scalar_type>(), + bt::number_range::dimensions>() + ); + } + template + inline Vec operator* (const Vec& parLeft, const typename Vec::scalar_type& parRight) { + return implem::binary_op_scalar_right( + parLeft, + parRight, + std::multiplies::scalar_type>(), + bt::number_range::dimensions>() + ); + } + template + inline Vec operator/ (const Vec& parLeft, const typename Vec::scalar_type& parRight) { + return implem::binary_op_scalar_right( + parLeft, + parRight, + std::divides::scalar_type>(), + bt::number_range::dimensions>() + ); + } + template + inline Vec operator% (const Vec& parLeft, const typename Vec::scalar_type& parRight) { + return implem::binary_op_scalar_right( + parLeft, + parRight, + std::modulus::scalar_type>(), + bt::number_range::dimensions>() + ); + } + + template + inline Vec operator+ (const typename Vec::scalar_type& parLeft, const Vec& parRight) { + return implem::binary_op_scalar_left( + parLeft, + parRight, + std::plus::scalar_type>(), + bt::number_range::dimensions>() + ); + } + template + inline Vec operator- (const typename Vec::scalar_type& parLeft, const Vec& parRight) { + return implem::binary_op_scalar_left( + parLeft, + parRight, + std::minus::scalar_type>(), + bt::number_range::dimensions>() + ); + } + template + inline Vec operator* (const typename Vec::scalar_type& parLeft, const Vec& parRight) { + return implem::binary_op_scalar_left( + parLeft, + parRight, + std::multiplies::scalar_type>(), + bt::number_range::dimensions>() + ); + } + template + inline Vec operator/ (const typename Vec::scalar_type& parLeft, const Vec& parRight) { + return implem::binary_op_scalar_left( + parLeft, + parRight, + std::divides::scalar_type>(), + bt::number_range::dimensions>() + ); + } + template + inline Vec operator% (const typename Vec::scalar_type& parLeft, const Vec& parRight) { + return implem::binary_op_scalar_left( + parLeft, + parRight, + std::modulus::scalar_type>(), + bt::number_range::dimensions>() + ); + } } //namespace vwr diff --git a/test/unit/test_operators.cpp b/test/unit/test_operators.cpp index 715c752..4c1cad4 100644 --- a/test/unit/test_operators.cpp +++ b/test/unit/test_operators.cpp @@ -82,3 +82,58 @@ TEST(vwr, bin_operators) { EXPECT_EQ(res, a % b); } } + +TEST(vwr, bin_operators_scalar) { + using namespace vwr; + + { + ivec3 a(50, 100, 150); + ivec3 res(50 + 10, 100 + 10, 150 + 10); + EXPECT_EQ(res, a + 10); + } + { + ivec3 a(50, 100, 150); + ivec3 res(50 - 25, 100 - 25, 150 - 25); + EXPECT_EQ(res, a - 25); + } + { + ivec3 a(50, 100, 150); + ivec3 res(50 * 10, 100 * 10, 150 * 10); + EXPECT_EQ(res, a * 10); + } + { + ivec3 a(50, 100, 150); + ivec3 res(50 / 2, 100 / 2, 150 / 2); + EXPECT_EQ(res, a / 2); + } + { + ivec3 a(50, 100, 150); + ivec3 res(50 % 7, 100 % 7, 150 % 7); + EXPECT_EQ(res, a % 7); + } + { + ivec3 a(50, 100, 150); + ivec3 res(25 + 50, 25 + 100, 25 + 150); + EXPECT_EQ(res, 25 + a); + } + { + ivec3 a(50, 100, 150); + ivec3 res(25 - 50, 25 - 100, 25 - 150); + EXPECT_EQ(res, 25 - a); + } + { + ivec3 a(50, 100, 150); + ivec3 res(4 * 50, 4 * 100, 4 * 150); + EXPECT_EQ(res, 4 * a); + } + { + ivec3 a(50, 100, 150); + ivec3 res(1000 / 50, 1000 / 100, 1000 / 150); + EXPECT_EQ(res, 1000 / a); + } + { + ivec3 a(50, 100, 150); + ivec3 res(1000 % 50, 1000 % 100, 1000 % 150); + EXPECT_EQ(res, 1000 % a); + } +}