From b2095603d1929c5460c556a4a19f69a28bb6befa Mon Sep 17 00:00:00 2001 From: Michele Santullo Date: Fri, 14 Aug 2015 12:32:52 +0200 Subject: [PATCH] Add operators for scalar types and fix wrong operator<= --- .../include/vectorwrapper/vectorwrapper.hpp | 13 ++++ .../include/vectorwrapper/vectorwrapper.inl | 60 ++++++++++++++++++- test/unit/vector.cpp | 3 +- 3 files changed, 71 insertions(+), 5 deletions(-) diff --git a/lib/vectorwrapper/include/vectorwrapper/vectorwrapper.hpp b/lib/vectorwrapper/include/vectorwrapper/vectorwrapper.hpp index 3bfc092..b41ebb7 100644 --- a/lib/vectorwrapper/include/vectorwrapper/vectorwrapper.hpp +++ b/lib/vectorwrapper/include/vectorwrapper/vectorwrapper.hpp @@ -165,6 +165,10 @@ namespace vwr { template VecBase& operator-= ( const VecBase& parOther ); template VecBase& operator*= ( const VecBase& parOther ); template VecBase& operator/= ( const VecBase& parOther ); + template VecBase& operator+= ( const T& parOther ); + template VecBase& operator-= ( const T& parOther ); + template VecBase& operator*= ( const T& parOther ); + template VecBase& operator/= ( const T& parOther ); private: vector_type m_wrapped; @@ -409,6 +413,15 @@ namespace vwr { Vec::type> operator* ( const Vec& parLeft, const Vec& parRight ); template Vec::type> operator/ ( const Vec& parLeft, const Vec& parRight ); + + template + Vec operator+ ( Vec parLeft, const T2& parRight ); + template + Vec operator- ( Vec parLeft, const T2& parRight ); + template + Vec operator* ( Vec parLeft, const T2& parRight ); + template + Vec operator/ ( Vec parLeft, const T2& parRight ); } //namespace vwr #include "vectorwrapper/vectorwrapper.inl" diff --git a/lib/vectorwrapper/include/vectorwrapper/vectorwrapper.inl b/lib/vectorwrapper/include/vectorwrapper/vectorwrapper.inl index a2ca96d..181d0da 100644 --- a/lib/vectorwrapper/include/vectorwrapper/vectorwrapper.inl +++ b/lib/vectorwrapper/include/vectorwrapper/vectorwrapper.inl @@ -42,7 +42,7 @@ namespace vwr { VecGetter::get_at(m_wrapped, 1) = parY; const scalar_type args[sizeof...(Args)] = {parArgs...}; - for (int z = 0; z < sizeof...(Args); ++z) { + for (std::size_t z = 0; z < sizeof...(Args); ++z) { VecGetter::get_at(m_wrapped, z + 2) = args[z]; } } @@ -132,6 +132,39 @@ namespace vwr { return *this; } + template + template + VecBase& VecBase::operator+= (const T& parOther) { + for (int z = 0; z < VectorWrapperInfo::dimensions; ++z) { + (*this)[z] += parOther; + } + return *this; + } + template + template + VecBase& VecBase::operator-= (const T& parOther) { + for (int z = 0; z < VectorWrapperInfo::dimensions; ++z) { + (*this)[z] -= parOther; + } + return *this; + } + template + template + VecBase& VecBase::operator*= (const T& parOther) { + for (int z = 0; z < VectorWrapperInfo::dimensions; ++z) { + (*this)[z] *= parOther; + } + return *this; + } + template + template + VecBase& VecBase::operator/= (const T& parOther) { + for (int z = 0; z < VectorWrapperInfo::dimensions; ++z) { + (*this)[z] /= parOther; + } + return *this; + } + template typename VectorWrapperInfo::scalar_type& VecGetter::get_at (T& parVec, std::size_t parIndex) { assert(parIndex < VectorWrapperInfo::dimensions); @@ -335,11 +368,11 @@ namespace vwr { template inline bool operator> (const Vec& parLeft, const T& parRight) { - return not (parLeft < parRight) and not (parLeft == parRight); + return (parRight < parLeft); } template inline bool operator<= (const Vec& parLeft, const T& parRight) { - return (parLeft < parRight) or (parLeft == parRight); + return not (parRight < parLeft); } template inline bool operator>= (const Vec& parLeft, const T& parRight) { @@ -386,4 +419,25 @@ namespace vwr { } return retval; } + + template + inline Vec operator+ (Vec parLeft, const T2& parRight) { + parLeft += parRight; + return parLeft; + } + template + inline Vec operator- (Vec parLeft, const T2& parRight) { + parLeft -= parRight; + return parLeft; + } + template + inline Vec operator* (Vec parLeft, const T2& parRight) { + parLeft *= parRight; + return parLeft; + } + template + inline Vec operator/ (Vec parLeft, const T2& parRight) { + parLeft /= parRight; + return parLeft; + } } //namespace vwr diff --git a/test/unit/vector.cpp b/test/unit/vector.cpp index 4287754..0534f7b 100644 --- a/test/unit/vector.cpp +++ b/test/unit/vector.cpp @@ -1,7 +1,6 @@ #include #include "doorkeeper/primitivetypes.hpp" - TEST(misc, vector) { typedef dk::Vector<2> coords2; typedef dk::Vector<8> coords8; @@ -34,7 +33,7 @@ TEST(misc, vector) { { const coords8 small(1, 2, 3, 4, 5, 6, 7, 8); const coords8 big(small * 100); - for (int z = 0; z < coords8::Dimension; ++z) { + for (int z = 0; z < coords8::dimensions; ++z) { EXPECT_EQ(big[z], (z + 1) * 100); }