From 473a1c1869961e976034334c654839a6f129be21 Mon Sep 17 00:00:00 2001 From: Pavel Krajcevski Date: Tue, 8 Oct 2013 15:06:20 -0400 Subject: [PATCH] Move the operators out of the class definition so that we can derive from Vectors. --- Base/include/VectorBase.h | 167 +++++++++++++++++++------------------- 1 file changed, 84 insertions(+), 83 deletions(-) diff --git a/Base/include/VectorBase.h b/Base/include/VectorBase.h index a7d892f..f27664f 100644 --- a/Base/include/VectorBase.h +++ b/Base/include/VectorBase.h @@ -51,6 +51,8 @@ namespace FasTC { } } + static const int Size = N; + // Accessors T &operator()(int idx) { return vec[idx]; } T &operator[](int idx) { return vec[idx]; } @@ -71,89 +73,6 @@ namespace FasTC { return VectorBase<_T, N>(vec); } - // Operators - template - VectorBase operator+(const VectorBase<_T, N> &v) const { - VectorBase a; - for(int i = 0; i < N; i++) - a.vec[i] = v(i) + vec[i]; - return a; - } - - template - VectorBase &operator+=(const VectorBase<_T, N> &v) const { - for(int i = 0; i < N; i++) - vec[i] += v(i); - return *this; - } - - template - VectorBase operator-(const VectorBase<_T, N> &v) const { - VectorBase a; - for(int i = 0; i < N; i++) - a(i) = vec[i] - v[i]; - return a; - } - - template - VectorBase &operator-=(const VectorBase<_T, N> &v) const { - for(int i = 0; i < N; i++) { - vec[i] -= v[i]; - } - return *this; - } - - template - VectorBase &operator=(const VectorBase<_T, N> &v) { - for(int i = 0; i < N; i++) - vec[i] = v[i]; - return *this; - } - - template - VectorBase operator*(const _T s) const { - VectorBase a; - for(int i = 0; i < N; i++) - a[i] = vec[i] * s; - return a; - } - - template - friend VectorBase operator*(const _T s, const VectorBase &v) { - VectorBase a; - for(int i = 0; i < N; i++) - a[i] = v[i] * s; - return a; - } - - template - VectorBase operator/(const _T s) const { - VectorBase a; - for(int i = 0; i < N; i++) - a[i] = vec[i] / s; - return a; - } - - template - friend VectorBase operator/(const _T s, const VectorBase &v) { - VectorBase a; - for(int i = 0; i < N; i++) - a[i] = v[i] / s; - return a; - } - - template - void operator*=(const _T s) { - for(int i = 0; i < N; i++) - vec[i] *= s; - } - - template - void operator/=(const _T s) { - for(int i = 0; i < N; i++) - vec[i] /= s; - } - // Vector operations template T Dot(const VectorBase<_T, N> &v) const { @@ -166,6 +85,88 @@ namespace FasTC { T LengthSq() const { return this->Dot(*this); } T Length() const { return sqrt(LengthSq()); } }; + + // Operators + template + static inline VectorTypeOne operator+(const VectorTypeOne &v1, + const VectorTypeTwo &v2) { + VectorTypeOne a; + for(int i = 0; i < VectorTypeOne::Size; i++) + a(i) = v1(i) + v2(i); + return a; + } + + template + static inline VectorTypeOne &operator+=(VectorTypeOne &v1, + const VectorTypeTwo &v2) { + for(int i = 0; i < VectorTypeOne::Size; i++) + v1(i) += v2(i); + return v1; + } + + template + static inline VectorTypeOne operator-(const VectorTypeOne &v1, + const VectorTypeTwo &v2) { + VectorTypeOne a; + for(int i = 0; i < VectorTypeOne::Size; i++) + a(i) = v1(i) - v2(i); + return a; + } + + template + static inline VectorTypeOne &operator-=(VectorTypeOne &v1, + const VectorTypeTwo &v2) { + for(int i = 0; i < VectorTypeOne::Size; i++) { + v1(i) -= v2(i); + } + return v1; + } + + template + static inline VectorType operator*(const VectorType &v, const ScalarType &s) { + VectorType a; + for(int i = 0; i < VectorType::Size; i++) + a(i) = v(i) * s; + return a; + } + + template + static inline VectorType operator*(const ScalarType &s, const VectorType &v) { + VectorType a; + for(int i = 0; i < VectorType::Size; i++) + a(i) = v(i) * s; + return a; + } + + template + static inline VectorType operator/(const VectorType &v, const ScalarType &s) { + VectorType a; + for(int i = 0; i < VectorType::Size; i++) + a(i) = v(i) / s; + return a; + } + + template + static inline operator/(const ScalarType &s, const VectorType &v) { + VectorType a; + for(int i = 0; i < VectorType::Size; i++) + a(i) = v(i) / s; + return a; + } + + template + static inline VectorType &operator*=(VectorType &v, const ScalarType &s) { + for(int i = 0; i < VectorType::Size; i++) + v(i) *= s; + return v; + } + + template + static inline VectorType &operator/=(VectorType &v, const ScalarType &s) { + for(int i = 0; i < VectorType::Size; i++) + v(i) /= s; + return v; + } }; #endif // BASE_INCLUDE_VECTORBASE_H_