diff options
author | Gael Guennebaud <g.gael@free.fr> | 2013-10-16 12:07:33 +0200 |
---|---|---|
committer | Gael Guennebaud <g.gael@free.fr> | 2013-10-16 12:07:33 +0200 |
commit | b433fb28576af729251594dbccd659607be44280 (patch) | |
tree | 603aab2d17faf733c3a885c6e2acb013ac627ac0 | |
parent | 2c0303c89eb4c1b979c6dcd2379c9d3f98ba4c99 (diff) |
Allow .conservativeResize(rows,cols) on vectors
-rw-r--r-- | Eigen/src/Core/PlainObjectBase.h | 16 | ||||
-rw-r--r-- | test/conservative_resize.cpp | 31 |
2 files changed, 36 insertions, 11 deletions
diff --git a/Eigen/src/Core/PlainObjectBase.h b/Eigen/src/Core/PlainObjectBase.h index e9aa3a88c..1a4c19233 100644 --- a/Eigen/src/Core/PlainObjectBase.h +++ b/Eigen/src/Core/PlainObjectBase.h @@ -47,7 +47,10 @@ template<> struct check_rows_cols_for_overflow<Dynamic> { } }; -template <typename Derived, typename OtherDerived = Derived, bool IsVector = bool(Derived::IsVectorAtCompileTime)> struct conservative_resize_like_impl; +template <typename Derived, + typename OtherDerived = Derived, + bool IsVector = bool(Derived::IsVectorAtCompileTime) && bool(OtherDerived::IsVectorAtCompileTime)> +struct conservative_resize_like_impl; template<typename MatrixTypeA, typename MatrixTypeB, bool SwapPointers> struct matrix_swap_impl; @@ -682,8 +685,10 @@ private: enum { ThisConstantIsPrivateInPlainObjectBase }; }; +namespace internal { + template <typename Derived, typename OtherDerived, bool IsVector> -struct internal::conservative_resize_like_impl +struct conservative_resize_like_impl { typedef typename Derived::Index Index; static void run(DenseBase<Derived>& _this, Index rows, Index cols) @@ -743,11 +748,14 @@ struct internal::conservative_resize_like_impl } }; -namespace internal { - +// Here, the specialization for vectors inherits from the general matrix case +// to allow calling .conservativeResize(rows,cols) on vectors. template <typename Derived, typename OtherDerived> struct conservative_resize_like_impl<Derived,OtherDerived,true> + : conservative_resize_like_impl<Derived,OtherDerived,false> { + using conservative_resize_like_impl<Derived,OtherDerived,false>::run; + typedef typename Derived::Index Index; static void run(DenseBase<Derived>& _this, Index size) { diff --git a/test/conservative_resize.cpp b/test/conservative_resize.cpp index 2d1ab3f03..498421b4c 100644 --- a/test/conservative_resize.cpp +++ b/test/conservative_resize.cpp @@ -60,34 +60,51 @@ void run_matrix_tests() template <typename Scalar> void run_vector_tests() { - typedef Matrix<Scalar, 1, Eigen::Dynamic> MatrixType; + typedef Matrix<Scalar, 1, Eigen::Dynamic> VectorType; - MatrixType m, n; + VectorType m, n; // boundary cases ... - m = n = MatrixType::Random(50); + m = n = VectorType::Random(50); m.conservativeResize(1); VERIFY_IS_APPROX(m, n.segment(0,1)); - m = n = MatrixType::Random(50); + m = n = VectorType::Random(50); m.conservativeResize(50); VERIFY_IS_APPROX(m, n.segment(0,50)); + + m = n = VectorType::Random(50); + m.conservativeResize(m.rows(),1); + VERIFY_IS_APPROX(m, n.segment(0,1)); + + m = n = VectorType::Random(50); + m.conservativeResize(m.rows(),50); + VERIFY_IS_APPROX(m, n.segment(0,50)); // random shrinking ... for (int i=0; i<50; ++i) { const int size = internal::random<int>(1,50); - m = n = MatrixType::Random(50); + m = n = VectorType::Random(50); m.conservativeResize(size); VERIFY_IS_APPROX(m, n.segment(0,size)); + + m = n = VectorType::Random(50); + m.conservativeResize(m.rows(), size); + VERIFY_IS_APPROX(m, n.segment(0,size)); } // random growing with zeroing ... for (int i=0; i<50; ++i) { const int size = internal::random<int>(50,100); - m = n = MatrixType::Random(50); - m.conservativeResizeLike(MatrixType::Zero(size)); + m = n = VectorType::Random(50); + m.conservativeResizeLike(VectorType::Zero(size)); + VERIFY_IS_APPROX(m.segment(0,50), n); + VERIFY( size<=50 || m.segment(50,size-50).sum() == Scalar(0) ); + + m = n = VectorType::Random(50); + m.conservativeResizeLike(Matrix<Scalar,Dynamic,Dynamic>::Zero(1,size)); VERIFY_IS_APPROX(m.segment(0,50), n); VERIFY( size<=50 || m.segment(50,size-50).sum() == Scalar(0) ); } |