From c9242467d896791074bc9e93527e7f7ba5f81b76 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Tue, 22 May 2012 14:13:34 +1000 Subject: [PATCH] Add scalar operator overloads for vector --- vector.cpp | 42 +++++++++++++++++++++++++++++++----------- vector.hpp | 11 +++++++++++ 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/vector.cpp b/vector.cpp index c0c7196a..7a2b032c 100644 --- a/vector.cpp +++ b/vector.cpp @@ -21,6 +21,7 @@ #include "debug.hpp" #include "maths.hpp" +#include "random.hpp" #include #include @@ -38,16 +39,6 @@ util::vector::vector () { ; } -//template -//util::vector::vector (const std::initializer_list &_data) { -// CHECK (_data.size () == S); -// -// std::copy (std::begin (_data), -// std::end (_data), -// std::begin (this->data)); -//} - - template util::vector util::vector::operator* (double rhs) const { @@ -132,6 +123,27 @@ util::vector::operator-= (const util::vector &rhs) { } +template +util::vector +util::vector::operator- (double rhs) const { + util::vector out; + + for (size_t i = 0; i < S; ++i) + out.data[i] = this->data[i] - rhs; + return out; +} + + +template +util::vector& +util::vector::operator-= (double rhs) { + for (size_t i = 0; i < S; ++i) + this->data[i] -= rhs; + + return *this; +} + + template util::vector& util::vector::operator+= (const util::vector &rhs) { @@ -184,7 +196,7 @@ double util::vector::dot (const util::vector &rhs) const { double total = 0.0; for (size_t i = 0; i < S; ++i) - total += this->data[i] + rhs.data[i]; + total += this->data[i] * rhs.data[i]; return total; } @@ -314,3 +326,11 @@ template const json::node& util::operator>> (const json::node&, util::vector<3>& template struct util::vector<1>; template struct util::vector<2>; template struct util::vector<3>; + + +namespace util { + template <> vector<1> random (void) { util::vector<1> out; randomise (out.data); return out; } + template <> vector<2> random (void) { util::vector<2> out; randomise (out.data); return out; } + template <> vector<3> random (void) { util::vector<3> out; randomise (out.data); return out; } +} + diff --git a/vector.hpp b/vector.hpp index 07631573..83480a05 100644 --- a/vector.hpp +++ b/vector.hpp @@ -39,6 +39,17 @@ namespace util { util::vector operator* (double) const; util::vector& operator*=(double); + + util::vector operator/ (double) const; + util::vector& operator/=(double); + + util::vector operator+ (double) const; + util::vector& operator+=(double); + + util::vector operator- (double) const; + util::vector& operator-=(double); + + util::vector operator* (const util::vector&) const; util::vector& operator*=(const util::vector&);