Ver código fonte

Split matrix into several helper files

Sam Jaffe 5 anos atrás
pai
commit
8cc69dc9e1

+ 23 - 0
include/math/matrix/forward.h

@@ -0,0 +1,23 @@
+#pragma once
+
+#include <cstdlib>
+
+namespace math { namespace matrix {
+  template <typename T, std::size_t R, std::size_t C> class matrix;
+}}
+
+namespace math { namespace vector {
+  template <typename T, std::size_t N> class vector;
+}}
+
+namespace math { namespace matrix { namespace concat_strategy {
+  struct {
+  } horizonal;
+  using horizontal_concat_t = decltype(horizonal);
+  struct {
+  } vertical;
+  using vertical_concat_t = decltype(vertical);
+  struct {
+  } diagonal;
+  using diagonal_concat_t = decltype(diagonal);
+}}}

+ 4 - 54
include/math/matrix/matrix.hpp

@@ -10,31 +10,9 @@
 #include "expect/expect.hpp"
 #include "math/vector/vector.hpp"
 
-namespace math { namespace matrix {
-
-  template <typename T> struct is_matrix {
-    static const constexpr bool value = false;
-  };
-
-  template <typename T, std::size_t R, std::size_t C> class matrix;
-  template <typename T, size_t R, size_t C> struct is_matrix<matrix<T, R, C>> {
-    static const constexpr bool value = true;
-  };
-  template <typename T, size_t N> struct is_matrix<vector::vector<T, N>> {
-    static const constexpr bool value = true;
-  };
-
-  namespace concat_strategy {
-    struct {
-    } horizonal;
-    using horizontal_concat_t = decltype(horizonal);
-    struct {
-    } vertical;
-    using vertical_concat_t = decltype(vertical);
-    struct {
-    } diagonal;
-    using diagonal_concat_t = decltype(diagonal);
-  };
+#include "forward.h"
+#include "row_reference.hpp"
+#include "traits.hpp"
 
 #define MATRIX_DISABLE_IF_MATRIX(_type, t, r, c)                               \
   typename std::enable_if<!is_matrix<_type>::value, matrix<t, r, c>>::type
@@ -44,35 +22,7 @@ namespace math { namespace matrix {
     for (size_t j = 0; j < j_max; ++j)
 #define MATRIX_FOR_EACH(i, j) MATRIX_FOR_EACH_RANGE(i, R, j, C)
 
-  template <typename T, std::size_t C> class row_reference {
-  private:
-    row_reference(T (&h)[C]) : _handle(h) {}
-
-  public:
-    row_reference(row_reference const &) = delete;
-    row_reference(row_reference &&) = default;
-
-    template <typename S>
-    row_reference & operator=(row_reference<S, C> const & other) {
-      VECTOR_FOR_EACH_RANGE(i, C) { _handle[i] = other[i]; }
-      return *this;
-    }
-
-    T const & operator[](std::size_t col) const { return _handle[col]; }
-    T & operator[](std::size_t col) { return _handle[col]; }
-    T const & at(std::size_t col) const {
-      expects(col < C, std::out_of_range, "column index out of range");
-      return operator[](col);
-    }
-    T & at(std::size_t col) {
-      expects(col < C, std::out_of_range, "column index out of range");
-      return operator[](col);
-    }
-
-  private:
-    template <typename _T, std::size_t R, std::size_t _C> friend class matrix;
-    T (&_handle)[C];
-  };
+namespace math { namespace matrix {
 
   template <typename T, std::size_t R, std::size_t C> class matrix {
   public:

+ 39 - 0
include/math/matrix/row_reference.hpp

@@ -0,0 +1,39 @@
+#pragma once
+
+#include <cstdlib>
+#include <stdexcept>
+
+#include "expect/expect.hpp"
+#include "math/vector/vector.hpp"
+
+namespace math { namespace matrix {
+  template <typename T, size_t C> class row_reference {
+  private:
+    row_reference(T (&h)[C]) : _handle(h) {}
+
+  public:
+    row_reference(row_reference const &) = delete;
+    row_reference(row_reference &&) = default;
+
+    template <typename S>
+    row_reference & operator=(row_reference<S, C> const & other) {
+      VECTOR_FOR_EACH_RANGE(i, C) { _handle[i] = other[i]; }
+      return *this;
+    }
+
+    T const & operator[](size_t col) const { return _handle[col]; }
+    T & operator[](size_t col) { return _handle[col]; }
+    T const & at(size_t col) const {
+      expects(col < C, std::out_of_range, "column index out of range");
+      return operator[](col);
+    }
+    T & at(size_t col) {
+      expects(col < C, std::out_of_range, "column index out of range");
+      return operator[](col);
+    }
+
+  private:
+    template <typename _T, size_t R, size_t _C> friend class matrix;
+    T (&_handle)[C];
+  };
+}}

+ 19 - 0
include/math/matrix/traits.hpp

@@ -0,0 +1,19 @@
+#pragma once
+
+#include "forward.h"
+
+namespace math { namespace matrix {
+
+  template <typename T> struct is_matrix {
+    static const constexpr bool value = false;
+  };
+
+  template <typename T, size_t R, size_t C> struct is_matrix<matrix<T, R, C>> {
+    static const constexpr bool value = true;
+  };
+
+  template <typename T, size_t N> struct is_matrix<vector::vector<T, N>> {
+    static const constexpr bool value = true;
+  };
+
+}}