[136] | 1 | // This file is part of Eigen, a lightweight C++ template library
|
---|
| 2 | // for linear algebra.
|
---|
| 3 | //
|
---|
| 4 | // Copyright (C) 2009 Benoit Jacob <jacob.benoit.1@gmail.com>
|
---|
| 5 | //
|
---|
| 6 | // This Source Code Form is subject to the terms of the Mozilla
|
---|
| 7 | // Public License v. 2.0. If a copy of the MPL was not distributed
|
---|
| 8 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
---|
| 9 |
|
---|
| 10 | #define EIGEN_NO_STATIC_ASSERT
|
---|
| 11 | #include "main.h"
|
---|
| 12 |
|
---|
| 13 | template<typename T>
|
---|
| 14 | struct other_matrix_type
|
---|
| 15 | {
|
---|
| 16 | typedef int type;
|
---|
| 17 | };
|
---|
| 18 |
|
---|
| 19 | template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
|
---|
| 20 | struct other_matrix_type<Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >
|
---|
| 21 | {
|
---|
| 22 | typedef Matrix<_Scalar, _Rows, _Cols, _Options^RowMajor, _MaxRows, _MaxCols> type;
|
---|
| 23 | };
|
---|
| 24 |
|
---|
| 25 | template<typename MatrixType> void swap(const MatrixType& m)
|
---|
| 26 | {
|
---|
| 27 | typedef typename other_matrix_type<MatrixType>::type OtherMatrixType;
|
---|
| 28 | typedef typename MatrixType::Scalar Scalar;
|
---|
| 29 |
|
---|
| 30 | eigen_assert((!internal::is_same<MatrixType,OtherMatrixType>::value));
|
---|
| 31 | typename MatrixType::Index rows = m.rows();
|
---|
| 32 | typename MatrixType::Index cols = m.cols();
|
---|
| 33 |
|
---|
| 34 | // construct 3 matrix guaranteed to be distinct
|
---|
| 35 | MatrixType m1 = MatrixType::Random(rows,cols);
|
---|
| 36 | MatrixType m2 = MatrixType::Random(rows,cols) + Scalar(100) * MatrixType::Identity(rows,cols);
|
---|
| 37 | OtherMatrixType m3 = OtherMatrixType::Random(rows,cols) + Scalar(200) * OtherMatrixType::Identity(rows,cols);
|
---|
| 38 |
|
---|
| 39 | MatrixType m1_copy = m1;
|
---|
| 40 | MatrixType m2_copy = m2;
|
---|
| 41 | OtherMatrixType m3_copy = m3;
|
---|
| 42 |
|
---|
| 43 | // test swapping 2 matrices of same type
|
---|
| 44 | m1.swap(m2);
|
---|
| 45 | VERIFY_IS_APPROX(m1,m2_copy);
|
---|
| 46 | VERIFY_IS_APPROX(m2,m1_copy);
|
---|
| 47 | m1 = m1_copy;
|
---|
| 48 | m2 = m2_copy;
|
---|
| 49 |
|
---|
| 50 | // test swapping 2 matrices of different types
|
---|
| 51 | m1.swap(m3);
|
---|
| 52 | VERIFY_IS_APPROX(m1,m3_copy);
|
---|
| 53 | VERIFY_IS_APPROX(m3,m1_copy);
|
---|
| 54 | m1 = m1_copy;
|
---|
| 55 | m3 = m3_copy;
|
---|
| 56 |
|
---|
| 57 | // test swapping matrix with expression
|
---|
| 58 | m1.swap(m2.block(0,0,rows,cols));
|
---|
| 59 | VERIFY_IS_APPROX(m1,m2_copy);
|
---|
| 60 | VERIFY_IS_APPROX(m2,m1_copy);
|
---|
| 61 | m1 = m1_copy;
|
---|
| 62 | m2 = m2_copy;
|
---|
| 63 |
|
---|
| 64 | // test swapping two expressions of different types
|
---|
| 65 | m1.transpose().swap(m3.transpose());
|
---|
| 66 | VERIFY_IS_APPROX(m1,m3_copy);
|
---|
| 67 | VERIFY_IS_APPROX(m3,m1_copy);
|
---|
| 68 | m1 = m1_copy;
|
---|
| 69 | m3 = m3_copy;
|
---|
| 70 |
|
---|
| 71 | // test assertion on mismatching size -- matrix case
|
---|
| 72 | VERIFY_RAISES_ASSERT(m1.swap(m1.row(0)));
|
---|
| 73 | // test assertion on mismatching size -- xpr case
|
---|
| 74 | VERIFY_RAISES_ASSERT(m1.row(0).swap(m1));
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | void test_swap()
|
---|
| 78 | {
|
---|
| 79 | CALL_SUBTEST_1( swap(Matrix3f()) ); // fixed size, no vectorization
|
---|
| 80 | CALL_SUBTEST_2( swap(Matrix4d()) ); // fixed size, possible vectorization
|
---|
| 81 | CALL_SUBTEST_3( swap(MatrixXd(3,3)) ); // dyn size, no vectorization
|
---|
| 82 | CALL_SUBTEST_4( swap(MatrixXf(30,30)) ); // dyn size, possible vectorization
|
---|
| 83 | }
|
---|