diff --git a/Eigen/src/Core/Matrix.h b/Eigen/src/Core/Matrix.h
index 652f31aa8..af21553a7 100644
--- a/Eigen/src/Core/Matrix.h
+++ b/Eigen/src/Core/Matrix.h
@@ -137,6 +137,9 @@ class Matrix
enum { NeedsToAlign = (Options&AutoAlign) == AutoAlign
&& SizeAtCompileTime!=Dynamic && ((sizeof(Scalar)*SizeAtCompileTime)%16)==0 };
EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)
+
+ Base& base() { return *static_cast(this); }
+ const Base& base() const { return *static_cast(this); }
EIGEN_STRONG_INLINE int rows() const { return m_storage.rows(); }
EIGEN_STRONG_INLINE int cols() const { return m_storage.cols(); }
@@ -395,13 +398,8 @@ class Matrix
/** Override MatrixBase::swap() since for dynamic-sized matrices of same type it is enough to swap the
* data pointers.
*/
- inline void swap(Matrix& other)
- {
- if (Base::SizeAtCompileTime==Dynamic)
- m_storage.swap(other.m_storage);
- else
- this->Base::swap(other);
- }
+ template
+ void swap(const MatrixBase& other);
/** \name Map
* These are convenience functions returning Map objects. The Map() static functions return unaligned Map objects,
@@ -540,8 +538,39 @@ class Matrix
&& (_Options & (AutoAlign|RowMajor)) == _Options),
INVALID_MATRIX_TEMPLATE_PARAMETERS)
}
+
+ template
+ friend struct ei_matrix_swap_impl;
};
+template::ret>
+struct ei_matrix_swap_impl
+{
+ static inline void run(MatrixType& matrix, MatrixBase& other)
+ {
+ matrix.base().swap(other);
+ }
+};
+
+template
+struct ei_matrix_swap_impl
+{
+ static inline void run(MatrixType& matrix, MatrixBase& other)
+ {
+ ei_assert(matrix.rows() == other.rows() && matrix.cols() == other.cols());
+ matrix.m_storage.swap(other.derived().m_storage);
+ }
+};
+
+template
+template
+inline void Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::swap(const MatrixBase& other)
+{
+ ei_matrix_swap_impl::run(*this, *const_cast*>(&other));
+}
+
+
/** \defgroup matrixtypedefs Global matrix typedefs
*
* \ingroup Core_Module
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index ba328f7ae..839f37dcf 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -219,6 +219,7 @@ ei_add_test(sparse_vector)
ei_add_test(sparse_basic)
ei_add_test(sparse_solvers " " "${SPARSE_LIBS}")
ei_add_test(sparse_product)
+ei_add_test(swap)
# print a summary of the different options
message("************************************************************")
diff --git a/test/swap.cpp b/test/swap.cpp
new file mode 100644
index 000000000..8b325992c
--- /dev/null
+++ b/test/swap.cpp
@@ -0,0 +1,98 @@
+// This file is part of Eigen, a lightweight C++ template library
+// for linear algebra.
+//
+// Copyright (C) 2009 Benoit Jacob
+//
+// Eigen is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 3 of the License, or (at your option) any later version.
+//
+// Alternatively, you can redistribute it and/or
+// modify it under the terms of the GNU General Public License as
+// published by the Free Software Foundation; either version 2 of
+// the License, or (at your option) any later version.
+//
+// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
+// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License and a copy of the GNU General Public License along with
+// Eigen. If not, see .
+
+#define EIGEN_NO_STATIC_ASSERT
+#include "main.h"
+
+template
+struct other_matrix_type
+{
+ typedef int type;
+};
+
+template
+struct other_matrix_type >
+{
+ typedef Matrix<_Scalar, _Rows, _Cols, _Options^RowMajor, _MaxRows, _MaxCols> type;
+};
+
+template void swap(const MatrixType& m)
+{
+ typedef typename other_matrix_type::type OtherMatrixType;
+ typedef typename MatrixType::Scalar Scalar;
+
+ ei_assert((!ei_is_same_type::ret));
+ int rows = m.rows();
+ int cols = m.cols();
+
+ // construct 3 matrix guaranteed to be distinct
+ MatrixType m1 = MatrixType::Random(rows,cols);
+ MatrixType m2 = MatrixType::Random(rows,cols) + Scalar(100) * MatrixType::Identity(rows,cols);
+ OtherMatrixType m3 = OtherMatrixType::Random(rows,cols) + Scalar(200) * OtherMatrixType::Identity(rows,cols);
+
+ MatrixType m1_copy = m1;
+ MatrixType m2_copy = m2;
+ OtherMatrixType m3_copy = m3;
+
+ // test swapping 2 matrices of same type
+ m1.swap(m2);
+ VERIFY_IS_APPROX(m1,m2_copy);
+ VERIFY_IS_APPROX(m2,m1_copy);
+ m1 = m1_copy;
+ m2 = m2_copy;
+
+ // test swapping 2 matrices of different types
+ m1.swap(m3);
+ VERIFY_IS_APPROX(m1,m3_copy);
+ VERIFY_IS_APPROX(m3,m1_copy);
+ m1 = m1_copy;
+ m3 = m3_copy;
+
+ // test swapping matrix with expression
+ m1.swap(m2.block(0,0,rows,cols));
+ VERIFY_IS_APPROX(m1,m2_copy);
+ VERIFY_IS_APPROX(m2,m1_copy);
+ m1 = m1_copy;
+ m2 = m2_copy;
+
+ // test swapping two expressions of different types
+ m1.transpose().swap(m3.transpose());
+ VERIFY_IS_APPROX(m1,m3_copy);
+ VERIFY_IS_APPROX(m3,m1_copy);
+ m1 = m1_copy;
+ m3 = m3_copy;
+
+ // test assertion on mismatching size -- matrix case
+ VERIFY_RAISES_ASSERT(m1.swap(m1.row(0)));
+ // test assertion on mismatching size -- xpr case
+ VERIFY_RAISES_ASSERT(m1.row(0).swap(m1));
+}
+
+void test_swap()
+{
+ CALL_SUBTEST( swap(Matrix3f()) ); // fixed size, no vectorization
+ CALL_SUBTEST( swap(Matrix4d()) ); // fixed size, possible vectorization
+ CALL_SUBTEST( swap(MatrixXd(3,3)) ); // dyn size, no vectorization
+ CALL_SUBTEST( swap(MatrixXf(30,30)) ); // dyn size, possible vectorization
+}