From 0c1ef2f4c67430e3c6605adfb1ddc3ac1d1e779f Mon Sep 17 00:00:00 2001 From: Benoit Jacob Date: Sat, 10 Jan 2009 13:10:23 +0000 Subject: [PATCH] make the std::vector fix work also with dynamic size Eigen objects, e.g. std::vector update unit test --- Eigen/StdVector | 5 +++-- Eigen/src/Core/Matrix.h | 5 ++--- Eigen/src/Core/MatrixStorage.h | 33 ++++++++++++++----------------- test/stdvector.cpp | 36 +++++++++++++++++++++------------- 4 files changed, 42 insertions(+), 37 deletions(-) diff --git a/Eigen/StdVector b/Eigen/StdVector index 6d5366468..19be257db 100644 --- a/Eigen/StdVector +++ b/Eigen/StdVector @@ -29,7 +29,7 @@ #include "Core" #include -namespace Eigen{ +namespace Eigen { template class ei_unaligned_type; template @@ -38,9 +38,10 @@ class ei_unaligned_type > { public: typedef Matrix<_Scalar,_Rows,_Cols,_Options,_MaxRows,_MaxCols> aligned_base; - ei_unaligned_type() : aligned_base(ei_select_matrix_constructor_doing_absolutely_nothing()) {} + ei_unaligned_type() : aligned_base(ei_constructor_without_unaligned_array_assert()) {} ei_unaligned_type(const aligned_base& other) { + resize(other.rows(), other.cols()); ei_assign_impl::run(*this, other); } }; diff --git a/Eigen/src/Core/Matrix.h b/Eigen/src/Core/Matrix.h index a95da9995..5349029bf 100644 --- a/Eigen/src/Core/Matrix.h +++ b/Eigen/src/Core/Matrix.h @@ -25,7 +25,6 @@ #ifndef EIGEN_MATRIX_H #define EIGEN_MATRIX_H -struct ei_select_matrix_constructor_doing_absolutely_nothing {}; /** \class Matrix * @@ -133,8 +132,8 @@ class Matrix protected: ei_matrix_storage m_storage; - Matrix(ei_select_matrix_constructor_doing_absolutely_nothing) // this ctor does not even do an assertion - : m_storage(ei_select_matrix_storage_constructor_doing_absolutely_nothing()) {} + Matrix(ei_constructor_without_unaligned_array_assert) + : m_storage(ei_constructor_without_unaligned_array_assert()) {} public: enum { NeedsToAlign = (Options&AutoAlign) == AutoAlign diff --git a/Eigen/src/Core/MatrixStorage.h b/Eigen/src/Core/MatrixStorage.h index a1a10ead5..972ea558a 100644 --- a/Eigen/src/Core/MatrixStorage.h +++ b/Eigen/src/Core/MatrixStorage.h @@ -26,8 +26,7 @@ #ifndef EIGEN_MATRIXSTORAGE_H #define EIGEN_MATRIXSTORAGE_H -struct ei_select_matrix_storage_constructor_doing_absolutely_nothing {}; -struct ei_select_matrix_array_constructor_doing_absolutely_nothing {}; +struct ei_constructor_without_unaligned_array_assert {}; /** \internal * Static array automatically aligned if the total byte size is a multiple of 16 and the matrix options require auto alignment @@ -46,14 +45,14 @@ template struct ei_matrix_array { T array[Size]; ei_matrix_array() {} - ei_matrix_array(ei_select_matrix_array_constructor_doing_absolutely_nothing) {} + ei_matrix_array(ei_constructor_without_unaligned_array_assert) {} }; /** \internal @@ -75,8 +74,8 @@ template class ei_matr ei_matrix_array m_data; public: inline explicit ei_matrix_storage() {} - inline ei_matrix_storage(ei_select_matrix_storage_constructor_doing_absolutely_nothing) - : m_data(ei_select_matrix_array_constructor_doing_absolutely_nothing()) {} + inline ei_matrix_storage(ei_constructor_without_unaligned_array_assert) + : m_data(ei_constructor_without_unaligned_array_assert()) {} inline ei_matrix_storage(int,int,int) {} inline void swap(ei_matrix_storage& other) { std::swap(m_data,other.m_data); } inline static int rows(void) {return _Rows;} @@ -94,8 +93,8 @@ template class ei_matrix_storage class ei_matrix_storage< int m_rows; public: inline explicit ei_matrix_storage() : m_rows(0) {} - inline ei_matrix_storage(ei_select_matrix_storage_constructor_doing_absolutely_nothing) - : m_data(ei_select_matrix_array_constructor_doing_absolutely_nothing()) {} + inline ei_matrix_storage(ei_constructor_without_unaligned_array_assert) + : m_data(ei_constructor_without_unaligned_array_assert()), m_rows(0) {} inline ei_matrix_storage(int, int rows, int) : m_rows(rows) {} inline ~ei_matrix_storage() {} inline void swap(ei_matrix_storage& other) { std::swap(m_data,other.m_data); std::swap(m_rows,other.m_rows); } @@ -140,8 +139,8 @@ template class ei_matrix_storage< int m_cols; public: inline explicit ei_matrix_storage() : m_cols(0) {} - inline ei_matrix_storage(ei_select_matrix_storage_constructor_doing_absolutely_nothing) - : m_data(ei_select_matrix_array_constructor_doing_absolutely_nothing()) {} + inline ei_matrix_storage(ei_constructor_without_unaligned_array_assert) + : m_data(ei_constructor_without_unaligned_array_assert()), m_cols(0) {} inline ei_matrix_storage(int, int, int cols) : m_cols(cols) {} inline ~ei_matrix_storage() {} inline void swap(ei_matrix_storage& other) { std::swap(m_data,other.m_data); std::swap(m_cols,other.m_cols); } @@ -163,8 +162,8 @@ template class ei_matrix_storage(size)), m_rows(rows), m_cols(cols) {} inline ~ei_matrix_storage() { ei_aligned_delete(m_data, m_rows*m_cols); } @@ -193,8 +192,7 @@ template class ei_matrix_storage(size)), m_cols(cols) {} inline ~ei_matrix_storage() { ei_aligned_delete(m_data, _Rows*m_cols); } inline void swap(ei_matrix_storage& other) { std::swap(m_data,other.m_data); std::swap(m_cols,other.m_cols); } @@ -220,8 +218,7 @@ template class ei_matrix_storage(size)), m_rows(rows) {} inline ~ei_matrix_storage() { ei_aligned_delete(m_data, _Cols*m_rows); } inline void swap(ei_matrix_storage& other) { std::swap(m_data,other.m_data); std::swap(m_rows,other.m_rows); } diff --git a/test/stdvector.cpp b/test/stdvector.cpp index 4e6aa9e3a..d14b85f95 100644 --- a/test/stdvector.cpp +++ b/test/stdvector.cpp @@ -26,10 +26,12 @@ #include template -void check_stdvector_fixedsize() +void check_stdvector(const MatrixType& m) { - MatrixType x = MatrixType::Random(), y = MatrixType::Random(); - std::vector v(10), w(20, y); + int rows = m.rows(); + int cols = m.cols(); + MatrixType x = MatrixType::Random(rows,cols), y = MatrixType::Random(rows,cols); + std::vector v(10, MatrixType(rows,cols)), w(20, y); v[5] = x; w[6] = v[5]; VERIFY_IS_APPROX(w[6], v[5]); @@ -38,27 +40,33 @@ void check_stdvector_fixedsize() { VERIFY_IS_APPROX(w[i], v[i]); } + v.resize(21); - v[20] = x; + v[20].set(x); VERIFY_IS_APPROX(v[20], x); v.resize(22,y); - VERIFY_IS_APPROX(v[21], y); + VERIFY_IS_APPROX(v[21], y); v.push_back(x); VERIFY_IS_APPROX(v[22], x); + VERIFY((size_t)&(v[22]) == (size_t)&(v[21]) + sizeof(MatrixType)); } - void test_stdvector() { // some non vectorizable fixed sizes - CALL_SUBTEST(check_stdvector_fixedsize()); - CALL_SUBTEST(check_stdvector_fixedsize()); - CALL_SUBTEST(check_stdvector_fixedsize()); + CALL_SUBTEST(check_stdvector(Vector2f())); + CALL_SUBTEST(check_stdvector(Matrix3f())); + CALL_SUBTEST(check_stdvector(Matrix3d())); // some vectorizable fixed sizes - CALL_SUBTEST(check_stdvector_fixedsize()); - CALL_SUBTEST(check_stdvector_fixedsize()); - CALL_SUBTEST(check_stdvector_fixedsize()); - CALL_SUBTEST(check_stdvector_fixedsize()); - + CALL_SUBTEST(check_stdvector(Matrix2f())); + CALL_SUBTEST(check_stdvector(Vector4f())); + CALL_SUBTEST(check_stdvector(Matrix4f())); + CALL_SUBTEST(check_stdvector(Matrix4d())); + + // some dynamic sizes + CALL_SUBTEST(check_stdvector(MatrixXd(1,1))); + CALL_SUBTEST(check_stdvector(VectorXd(20))); + CALL_SUBTEST(check_stdvector(RowVectorXf(20))); + CALL_SUBTEST(check_stdvector(MatrixXcf(10,10))); }