make the std::vector fix work also with dynamic size Eigen objects, e.g.

std::vector<VectorXd>
update unit test
This commit is contained in:
Benoit Jacob 2009-01-10 13:10:23 +00:00
parent 3efe6e4176
commit 0c1ef2f4c6
4 changed files with 42 additions and 37 deletions

View File

@ -29,7 +29,7 @@
#include "Core" #include "Core"
#include <vector> #include <vector>
namespace Eigen{ namespace Eigen {
template<typename aligned_type> class ei_unaligned_type; template<typename aligned_type> class ei_unaligned_type;
template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols> template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
@ -38,9 +38,10 @@ class ei_unaligned_type<Matrix<_Scalar,_Rows,_Cols,_Options,_MaxRows,_MaxCols> >
{ {
public: public:
typedef Matrix<_Scalar,_Rows,_Cols,_Options,_MaxRows,_MaxCols> aligned_base; 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) ei_unaligned_type(const aligned_base& other)
{ {
resize(other.rows(), other.cols());
ei_assign_impl<ei_unaligned_type,aligned_base,NoVectorization>::run(*this, other); ei_assign_impl<ei_unaligned_type,aligned_base,NoVectorization>::run(*this, other);
} }
}; };

View File

@ -25,7 +25,6 @@
#ifndef EIGEN_MATRIX_H #ifndef EIGEN_MATRIX_H
#define EIGEN_MATRIX_H #define EIGEN_MATRIX_H
struct ei_select_matrix_constructor_doing_absolutely_nothing {};
/** \class Matrix /** \class Matrix
* *
@ -133,8 +132,8 @@ class Matrix
protected: protected:
ei_matrix_storage<Scalar, MaxSizeAtCompileTime, RowsAtCompileTime, ColsAtCompileTime, Options> m_storage; ei_matrix_storage<Scalar, MaxSizeAtCompileTime, RowsAtCompileTime, ColsAtCompileTime, Options> m_storage;
Matrix(ei_select_matrix_constructor_doing_absolutely_nothing) // this ctor does not even do an assertion Matrix(ei_constructor_without_unaligned_array_assert)
: m_storage(ei_select_matrix_storage_constructor_doing_absolutely_nothing()) {} : m_storage(ei_constructor_without_unaligned_array_assert()) {}
public: public:
enum { NeedsToAlign = (Options&AutoAlign) == AutoAlign enum { NeedsToAlign = (Options&AutoAlign) == AutoAlign

View File

@ -26,8 +26,7 @@
#ifndef EIGEN_MATRIXSTORAGE_H #ifndef EIGEN_MATRIXSTORAGE_H
#define EIGEN_MATRIXSTORAGE_H #define EIGEN_MATRIXSTORAGE_H
struct ei_select_matrix_storage_constructor_doing_absolutely_nothing {}; struct ei_constructor_without_unaligned_array_assert {};
struct ei_select_matrix_array_constructor_doing_absolutely_nothing {};
/** \internal /** \internal
* Static array automatically aligned if the total byte size is a multiple of 16 and the matrix options require auto alignment * 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 <typename T, int Size, int MatrixOptions,
#endif #endif
} }
ei_matrix_array(ei_select_matrix_array_constructor_doing_absolutely_nothing) {} ei_matrix_array(ei_constructor_without_unaligned_array_assert) {}
}; };
template <typename T, int Size, int MatrixOptions> struct ei_matrix_array<T,Size,MatrixOptions,false> template <typename T, int Size, int MatrixOptions> struct ei_matrix_array<T,Size,MatrixOptions,false>
{ {
T array[Size]; T array[Size];
ei_matrix_array() {} ei_matrix_array() {}
ei_matrix_array(ei_select_matrix_array_constructor_doing_absolutely_nothing) {} ei_matrix_array(ei_constructor_without_unaligned_array_assert) {}
}; };
/** \internal /** \internal
@ -75,8 +74,8 @@ template<typename T, int Size, int _Rows, int _Cols, int _Options> class ei_matr
ei_matrix_array<T,Size,_Options> m_data; ei_matrix_array<T,Size,_Options> m_data;
public: public:
inline explicit ei_matrix_storage() {} inline explicit ei_matrix_storage() {}
inline ei_matrix_storage(ei_select_matrix_storage_constructor_doing_absolutely_nothing) inline ei_matrix_storage(ei_constructor_without_unaligned_array_assert)
: m_data(ei_select_matrix_array_constructor_doing_absolutely_nothing()) {} : m_data(ei_constructor_without_unaligned_array_assert()) {}
inline ei_matrix_storage(int,int,int) {} inline ei_matrix_storage(int,int,int) {}
inline void swap(ei_matrix_storage& other) { std::swap(m_data,other.m_data); } inline void swap(ei_matrix_storage& other) { std::swap(m_data,other.m_data); }
inline static int rows(void) {return _Rows;} inline static int rows(void) {return _Rows;}
@ -94,8 +93,8 @@ template<typename T, int Size, int _Options> class ei_matrix_storage<T, Size, Dy
int m_cols; int m_cols;
public: public:
inline explicit ei_matrix_storage() : m_rows(0), m_cols(0) {} inline explicit ei_matrix_storage() : m_rows(0), m_cols(0) {}
inline ei_matrix_storage(ei_select_matrix_storage_constructor_doing_absolutely_nothing) inline ei_matrix_storage(ei_constructor_without_unaligned_array_assert)
: m_data(ei_select_matrix_array_constructor_doing_absolutely_nothing()) {} : m_data(ei_constructor_without_unaligned_array_assert()), m_rows(0), m_cols(0) {}
inline ei_matrix_storage(int, int rows, int cols) : m_rows(rows), m_cols(cols) {} inline ei_matrix_storage(int, int rows, int cols) : m_rows(rows), m_cols(cols) {}
inline ~ei_matrix_storage() {} inline ~ei_matrix_storage() {}
inline void swap(ei_matrix_storage& other) inline void swap(ei_matrix_storage& other)
@ -118,8 +117,8 @@ template<typename T, int Size, int _Cols, int _Options> class ei_matrix_storage<
int m_rows; int m_rows;
public: public:
inline explicit ei_matrix_storage() : m_rows(0) {} inline explicit ei_matrix_storage() : m_rows(0) {}
inline ei_matrix_storage(ei_select_matrix_storage_constructor_doing_absolutely_nothing) inline ei_matrix_storage(ei_constructor_without_unaligned_array_assert)
: m_data(ei_select_matrix_array_constructor_doing_absolutely_nothing()) {} : 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(int, int rows, int) : m_rows(rows) {}
inline ~ei_matrix_storage() {} 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); } 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<typename T, int Size, int _Rows, int _Options> class ei_matrix_storage<
int m_cols; int m_cols;
public: public:
inline explicit ei_matrix_storage() : m_cols(0) {} inline explicit ei_matrix_storage() : m_cols(0) {}
inline ei_matrix_storage(ei_select_matrix_storage_constructor_doing_absolutely_nothing) inline ei_matrix_storage(ei_constructor_without_unaligned_array_assert)
: m_data(ei_select_matrix_array_constructor_doing_absolutely_nothing()) {} : 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(int, int, int cols) : m_cols(cols) {}
inline ~ei_matrix_storage() {} 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); } 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<typename T, int _Options> class ei_matrix_storage<T, Dynamic, Dynamic,
int m_cols; int m_cols;
public: public:
inline explicit ei_matrix_storage() : m_data(0), m_rows(0), m_cols(0) {} inline explicit ei_matrix_storage() : m_data(0), m_rows(0), m_cols(0) {}
inline ei_matrix_storage(ei_select_matrix_storage_constructor_doing_absolutely_nothing) inline ei_matrix_storage(ei_constructor_without_unaligned_array_assert)
: m_data(ei_select_matrix_array_constructor_doing_absolutely_nothing()) {} : m_data(0), m_rows(0), m_cols(0) {}
inline ei_matrix_storage(int size, int rows, int cols) inline ei_matrix_storage(int size, int rows, int cols)
: m_data(ei_aligned_new<T>(size)), m_rows(rows), m_cols(cols) {} : m_data(ei_aligned_new<T>(size)), m_rows(rows), m_cols(cols) {}
inline ~ei_matrix_storage() { ei_aligned_delete(m_data, m_rows*m_cols); } inline ~ei_matrix_storage() { ei_aligned_delete(m_data, m_rows*m_cols); }
@ -193,8 +192,7 @@ template<typename T, int _Rows, int _Options> class ei_matrix_storage<T, Dynamic
int m_cols; int m_cols;
public: public:
inline explicit ei_matrix_storage() : m_data(0), m_cols(0) {} inline explicit ei_matrix_storage() : m_data(0), m_cols(0) {}
inline ei_matrix_storage(ei_select_matrix_storage_constructor_doing_absolutely_nothing) inline ei_matrix_storage(ei_constructor_without_unaligned_array_assert) : m_data(0), m_cols(0) {}
: m_data(ei_select_matrix_array_constructor_doing_absolutely_nothing()) {}
inline ei_matrix_storage(int size, int, int cols) : m_data(ei_aligned_new<T>(size)), m_cols(cols) {} inline ei_matrix_storage(int size, int, int cols) : m_data(ei_aligned_new<T>(size)), m_cols(cols) {}
inline ~ei_matrix_storage() { ei_aligned_delete(m_data, _Rows*m_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); } 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<typename T, int _Cols, int _Options> class ei_matrix_storage<T, Dynamic
int m_rows; int m_rows;
public: public:
inline explicit ei_matrix_storage() : m_data(0), m_rows(0) {} inline explicit ei_matrix_storage() : m_data(0), m_rows(0) {}
inline ei_matrix_storage(ei_select_matrix_storage_constructor_doing_absolutely_nothing) inline ei_matrix_storage(ei_constructor_without_unaligned_array_assert) : m_data(0), m_rows(0) {}
: m_data(ei_select_matrix_array_constructor_doing_absolutely_nothing()) {}
inline ei_matrix_storage(int size, int rows, int) : m_data(ei_aligned_new<T>(size)), m_rows(rows) {} inline ei_matrix_storage(int size, int rows, int) : m_data(ei_aligned_new<T>(size)), m_rows(rows) {}
inline ~ei_matrix_storage() { ei_aligned_delete(m_data, _Cols*m_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); } inline void swap(ei_matrix_storage& other) { std::swap(m_data,other.m_data); std::swap(m_rows,other.m_rows); }

View File

@ -26,10 +26,12 @@
#include <Eigen/StdVector> #include <Eigen/StdVector>
template<typename MatrixType> template<typename MatrixType>
void check_stdvector_fixedsize() void check_stdvector(const MatrixType& m)
{ {
MatrixType x = MatrixType::Random(), y = MatrixType::Random(); int rows = m.rows();
std::vector<MatrixType> v(10), w(20, y); int cols = m.cols();
MatrixType x = MatrixType::Random(rows,cols), y = MatrixType::Random(rows,cols);
std::vector<MatrixType> v(10, MatrixType(rows,cols)), w(20, y);
v[5] = x; v[5] = x;
w[6] = v[5]; w[6] = v[5];
VERIFY_IS_APPROX(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]); VERIFY_IS_APPROX(w[i], v[i]);
} }
v.resize(21); v.resize(21);
v[20] = x; v[20].set(x);
VERIFY_IS_APPROX(v[20], x); VERIFY_IS_APPROX(v[20], x);
v.resize(22,y); v.resize(22,y);
VERIFY_IS_APPROX(v[21], y); VERIFY_IS_APPROX(v[21], y);
v.push_back(x); v.push_back(x);
VERIFY_IS_APPROX(v[22], x); VERIFY_IS_APPROX(v[22], x);
VERIFY((size_t)&(v[22]) == (size_t)&(v[21]) + sizeof(MatrixType));
} }
void test_stdvector() void test_stdvector()
{ {
// some non vectorizable fixed sizes // some non vectorizable fixed sizes
CALL_SUBTEST(check_stdvector_fixedsize<Vector2f>()); CALL_SUBTEST(check_stdvector(Vector2f()));
CALL_SUBTEST(check_stdvector_fixedsize<Matrix3f>()); CALL_SUBTEST(check_stdvector(Matrix3f()));
CALL_SUBTEST(check_stdvector_fixedsize<Matrix3d>()); CALL_SUBTEST(check_stdvector(Matrix3d()));
// some vectorizable fixed sizes // some vectorizable fixed sizes
CALL_SUBTEST(check_stdvector_fixedsize<Vector2d>()); CALL_SUBTEST(check_stdvector(Matrix2f()));
CALL_SUBTEST(check_stdvector_fixedsize<Vector4f>()); CALL_SUBTEST(check_stdvector(Vector4f()));
CALL_SUBTEST(check_stdvector_fixedsize<Matrix4f>()); CALL_SUBTEST(check_stdvector(Matrix4f()));
CALL_SUBTEST(check_stdvector_fixedsize<Matrix4d>()); 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)));
} }