瀏覽代碼

Add support for addition and subtraction with value_type.

Sam Jaffe 7 年之前
父節點
當前提交
959a6c080f
共有 2 個文件被更改,包括 41 次插入9 次删除
  1. 32 0
      vector.hpp
  2. 9 9
      vector_test.cpp

+ 32 - 0
vector.hpp

@@ -139,19 +139,43 @@ VECTOR_ENABLE_IF_LT_N(i, value_type &) name() { return _data[i]; }
       return *this;
     }
     
+    vector& operator+=(T const & other) {
+      return operator+=(vector(other, fill));
+    }
+    
     vector operator+(vector const & other) const {
       return vector{*this} += other;
     }
     
+    vector operator+(T const & other) const {
+      return operator+(vector(other, fill));
+    }
+    
+    friend vector operator+(T const & lhs, vector const & rhs) {
+      return rhs + lhs;
+    }
+    
     vector& operator-=(vector const & other) {
       VECTOR_FOR_EACH(i) { _data[i] -= other[i]; }
       return *this;
     }
     
+    vector& operator-=(T const & other) {
+      return operator-=(vector(other, fill));
+    }
+    
     vector operator-(vector const & other) const {
       return vector{*this} -= other;
     }
     
+    vector operator-(T const & other) const {
+      return operator-(vector(other, fill));
+    }
+    
+    friend vector operator-(T const & lhs, vector const & rhs) {
+      return vector(lhs, fill) - rhs;
+    }
+    
     vector operator-() const {
       return vector{} -= *this;
     }
@@ -272,6 +296,14 @@ VECTOR_ENABLE_IF_LT_N(i, value_type &) name() { return _data[i]; }
   bool operator> (vector<T, N> const & lhs, vector<T, N> const & rhs) { return compare(lhs, rhs) >  0; }
   template <typename T, std::size_t N>
   bool operator>=(vector<T, N> const & lhs, vector<T, N> const & rhs) { return compare(lhs, rhs) >= 0; }
+  
+#undef VECTOR_FOR_EACH
+#undef VECTOR_FOR_EACH_RANGE
+#undef VECTOR_ACCESS_FN
+#undef VECTOR_DISABLE_IF_VECTOR
+#undef VECTOR_ENABLE_IF_EQ_T
+#undef VECTOR_ENABLE_IF_EQ_N
+#undef VECTOR_ENABLE_IF_LT_N
 } }
 
 template <typename... Ts>

+ 9 - 9
vector_test.cpp

@@ -81,15 +81,15 @@ TEST(Vector, DivisionByZeroThrowsException) {
   EXPECT_THROW((vec3{{1.0, 1.0, 1.0}}/vec3({1.0, 0.5, 0.0})), std::domain_error);
 }
 
-//  TEST(Vector, AdditionWithValueType) {
-//    EXPECT_THAT((vec2i{1,0} + 1), (vec2i{2,1}))
-//    EXPECT_THAT(1+iota2i(), (vec2i{2,3}));
-//  }
-//
-//  TEST(Vector, SubtractionWithValueType) {
-//    EXPECT_THAT(4-iota2i(), (vec2i{3,2}));
-//    EXPECT_THAT((vec2i{1,0} - 1), (vec2i{0,-1}))
-//  }
+TEST(Vector, AdditionWithValueType) {
+  EXPECT_THAT(vec2i({1,0}) + 1, vec2i({2,1}));
+  EXPECT_THAT(1+iota2i(), vec2i({2,3}));
+}
+
+TEST(Vector, SubtractionWithValueType) {
+  EXPECT_THAT(4-iota2i(), vec2i({3,2}));
+  EXPECT_THAT(vec2i({1,0}) - 1, vec2i({0,-1}));
+}
 
 TEST(Vector, MultiplicationWithValueType) {
   EXPECT_THAT((vec2i{{1,0}} * 3), vec2i({3,0}));