Added some operators.
authorsaiam
Tue, 18 Nov 2008 19:42:01 +0000
changeset 57 a89e02118931
parent 56 38f269310f77
child 58 a53f5ad69500
Added some operators.
src/proto2/Vector.hh
--- a/src/proto2/Vector.hh	Tue Nov 18 19:33:31 2008 +0000
+++ b/src/proto2/Vector.hh	Tue Nov 18 19:42:01 2008 +0000
@@ -3,6 +3,9 @@
 
 #include <iostream>
 
+/**
+ * 2D Vector class. Implements standard vector operations.
+ */
 template <typename T>
 class _Vector {
 public:
@@ -10,39 +13,58 @@
     T y;
     
     _Vector() : x(0), y(0){}
+    /**
+     * @param x Initial x-coordinate.
+     * @param y Initial y-coordinate.
+     */
     _Vector(T x, T y) : x(x), y(y) {}
-    _Vector(const _Vector& v) : x(v.x), y(v.y) {}
+    /**
+     * @param v Other vector to be copied.
+     */
+    _Vector(const _Vector &v) : x(v.x), y(v.y) {}
 
-    void operator=(const _Vector& v) {
+    void operator=(const _Vector &v) {
         this->x = v.x;
         this->y = v.y;
     }
-    _Vector operator+(const _Vector& v) const {
+    _Vector operator+(const _Vector &v) const {
         return _Vector(this->x+v.x, this->y+v.y);
     }
-    _Vector operator-(const _Vector& v) const {
+    _Vector operator-(const _Vector &v) const {
         return _Vector(this->x-v.x, this->y-v.y);
     }
-    _Vector operator*(const T d) const {
+    _Vector operator*(const T &d) const {
         return _Vector(this->x*d, this->y*d);
     }
-    _Vector operator/(const T d) const {
+    _Vector operator/(const T &d) const {
         return _Vector(this->x/d, this->y/d);
     } 
-    void operator+=(const _Vector& v) {
+    void operator+=(const _Vector &v) {
         this->x += v.x;
         this->y += v.y;
     }
-    void operator*=(const T f) {
+    void operator-=(const _Vector &v) {
+        this->x -= v.x;
+        this->y -= v.y;
+    }
+    void operator*=(const T &f) {
         this->x *= f;
         this->y *= f;
     }
+    void operator/=(const T &d) {
+        this->x /= d;
+        this->y /= d;
+    }
 };
 
 template<typename T>
-bool operator==(const _Vector<T>& v1, const _Vector<T>& v2) {
+bool operator==(const _Vector<T> &v1, const _Vector<T> &v2) {
     return ((v1.x == v2.x) && (v1.y == v2.y));
 }
+template<typename T>
+bool operator!=(const _Vector<T> &v1, const _Vector<T> &v2) {
+    return !(v1 == v2);
+}
 
 template<typename T>
 std::ostream& operator<<(std::ostream &s, _Vector<T> &v) {