From 71e5cbcbc4299ab218da327d2080af0b49bdae23 Mon Sep 17 00:00:00 2001 From: Hauke Heibel Date: Wed, 3 Jun 2009 16:47:38 +0200 Subject: [PATCH] Added specializations for DontAlign when using Dynamic matrices. This allows users to store Matrices in smart pointers without the need for a specialized allocator/de-allocator. --- Eigen/src/Core/MatrixStorage.h | 112 ++++++++++++++++++++++++++++++--- 1 file changed, 103 insertions(+), 9 deletions(-) diff --git a/Eigen/src/Core/MatrixStorage.h b/Eigen/src/Core/MatrixStorage.h index 3c6a6117c..002df076c 100644 --- a/Eigen/src/Core/MatrixStorage.h +++ b/Eigen/src/Core/MatrixStorage.h @@ -66,10 +66,10 @@ template struct ei_matrix_array class ei_matrix_storage; +template class ei_matrix_storage; // purely fixed-size matrix -template class ei_matrix_storage +template class ei_matrix_storage { ei_matrix_array m_data; public: @@ -86,7 +86,7 @@ template class ei_matr }; // null matrix -template class ei_matrix_storage +template class ei_matrix_storage { public: inline explicit ei_matrix_storage() {} @@ -101,7 +101,7 @@ template class ei_matrix_storage }; // dynamic-size matrix with fixed-size storage -template class ei_matrix_storage +template class ei_matrix_storage { ei_matrix_array m_data; int m_rows; @@ -126,7 +126,7 @@ template class ei_matrix_storage class ei_matrix_storage +template class ei_matrix_storage { ei_matrix_array m_data; int m_rows; @@ -148,7 +148,7 @@ template class ei_matrix_storage< }; // dynamic-size matrix with fixed-size storage and fixed height -template class ei_matrix_storage +template class ei_matrix_storage { ei_matrix_array m_data; int m_cols; @@ -170,7 +170,7 @@ template class ei_matrix_storage< }; // purely dynamic matrix. -template class ei_matrix_storage +template class ei_matrix_storage { T *m_data; int m_rows; @@ -203,8 +203,42 @@ template class ei_matrix_storage class ei_matrix_storage +{ + T *m_data; + int m_rows; + int m_cols; +public: + inline explicit ei_matrix_storage() : m_data(0), m_rows(0), m_cols(0) {} + inline ei_matrix_storage(ei_constructor_without_unaligned_array_assert) + : m_data(0), m_rows(0), m_cols(0) {} + inline ei_matrix_storage(int size, int rows, int cols) + : m_data(new T[size]), m_rows(rows), m_cols(cols) {} + inline ~ei_matrix_storage() { delete [] m_data; } + inline void swap(ei_matrix_storage& other) + { std::swap(m_data,other.m_data); std::swap(m_rows,other.m_rows); std::swap(m_cols,other.m_cols); } + inline int rows(void) const {return m_rows;} + inline int cols(void) const {return m_cols;} + void resize(int size, int rows, int cols) + { + if(size != m_rows*m_cols) + { + delete [] m_data; + if (size) + m_data = new T[size]; + else + m_data = 0; + } + m_rows = rows; + m_cols = cols; + } + inline const T *data() const { return m_data; } + inline T *data() { return m_data; } +}; + // matrix with dynamic width and fixed height (so that matrix has dynamic size). -template class ei_matrix_storage +template class ei_matrix_storage { T *m_data; int m_cols; @@ -232,8 +266,38 @@ template class ei_matrix_storage class ei_matrix_storage +{ + T *m_data; + int m_cols; + public: + inline explicit ei_matrix_storage() : m_data(0), m_cols(0) {} + inline ei_matrix_storage(ei_constructor_without_unaligned_array_assert) : m_data(0), m_cols(0) {} + inline ei_matrix_storage(int size, int, int cols) : m_data(new T[size]), m_cols(cols) {} + inline ~ei_matrix_storage() { delete [] m_data; } + inline void swap(ei_matrix_storage& other) { std::swap(m_data,other.m_data); std::swap(m_cols,other.m_cols); } + inline static int rows(void) {return _Rows;} + inline int cols(void) const {return m_cols;} + void resize(int size, int, int cols) + { + if(size != _Rows*m_cols) + { + delete [] m_data; + if (size) + m_data = new T[size]; + else + m_data = 0; + } + m_cols = cols; + } + inline const T *data() const { return m_data; } + inline T *data() { return m_data; } +}; + // matrix with dynamic height and fixed width (so that matrix has dynamic size). -template class ei_matrix_storage +template class ei_matrix_storage { T *m_data; int m_rows; @@ -261,4 +325,34 @@ template class ei_matrix_storage class ei_matrix_storage +{ + T *m_data; + int m_rows; + public: + inline explicit ei_matrix_storage() : m_data(0), m_rows(0) {} + inline ei_matrix_storage(ei_constructor_without_unaligned_array_assert) : m_data(0), m_rows(0) {} + inline ei_matrix_storage(int size, int rows, int) : m_data(new T[size]), m_rows(rows) {} + inline ~ei_matrix_storage() { delete [] m_data; } + inline void swap(ei_matrix_storage& other) { std::swap(m_data,other.m_data); std::swap(m_rows,other.m_rows); } + inline int rows(void) const {return m_rows;} + inline static int cols(void) {return _Cols;} + void resize(int size, int rows, int) + { + if(size != m_rows*_Cols) + { + delete [] m_data; + if (size) + m_data = new T[size]; + else + m_data = 0; + } + m_rows = rows; + } + inline const T *data() const { return m_data; } + inline T *data() { return m_data; } +}; + #endif // EIGEN_MATRIX_H