diff options
-rw-r--r-- | include/YAGE/Math/matrix.hpp | 39 | ||||
-rw-r--r-- | test/matrix_test.cpp | 6 |
2 files changed, 30 insertions, 15 deletions
diff --git a/include/YAGE/Math/matrix.hpp b/include/YAGE/Math/matrix.hpp index b6807c09..76c90c16 100644 --- a/include/YAGE/Math/matrix.hpp +++ b/include/YAGE/Math/matrix.hpp @@ -1,29 +1,44 @@ #ifndef YAGE_MATH_MATRIX_HPP #define YAGE_MATH_MATRIX_HPP +#include <memory> +#include <vector> + namespace yage { -template<typename Type, int Cols, int Rows> class Matrix +template<int Rows, int Cols, class Type> class Matrix; + +template<int Rows, int Cols, class Type> class Row { + friend class Matrix<Rows, Cols, Type>; private: - Type x[Cols][Rows]; + std::shared_ptr<Matrix<Rows, Cols, Type>> parent_; + int index_; public: - Matrix() + Row<Rows, Cols, Type>(std::shared_ptr<Matrix<Rows, Cols, Type>> parent, int index) : + parent_(parent), index_(index) + {} + + Type &operator[](int col) { - for(int i=0; i<Cols; ++i) - { - for(int j=0; j<Rows; ++j) - { - x[i][j]=5; - } - } + return parent_->data_[index_*Cols+col]; } +}; + +template<int Rows=4, int Cols=4, class Type=double> class Matrix +{ +private: + std::vector<Type> data_; + +public: + Matrix<Rows, Cols, Type>() : data_(Rows*Cols) {} + Matrix<Rows, Cols, Type>(int rows, int cols) : data_(rows*cols) {} - Type get(int i, int j) const + Row<Rows, Cols, Type> operator[](int row) { - return x[i][j]; + return Row<Rows, Cols, Type>(std::make_shared<Matrix<Rows, Cols, Type>>(*this), row); } }; diff --git a/test/matrix_test.cpp b/test/matrix_test.cpp index 8f09ad38..ba1f0d4a 100644 --- a/test/matrix_test.cpp +++ b/test/matrix_test.cpp @@ -4,10 +4,10 @@ int main() { - yage::Matrix<double, 10, 10> matrix; - - int x=matrix.get(5, 2); + yage::Matrix<4, 4, int> matrix; + int x=matrix[2][2]; + std::cout<<"at: "<<x<<'\n'; return 0; } |