swap.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. #define EIGEN_NO_STATIC_ASSERT
  10. #include "main.h"
  11. template<typename T>
  12. struct other_matrix_type
  13. {
  14. typedef int type;
  15. };
  16. template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
  17. struct other_matrix_type<Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >
  18. {
  19. typedef Matrix<_Scalar, _Rows, _Cols, _Options^RowMajor, _MaxRows, _MaxCols> type;
  20. };
  21. template<typename MatrixType> void swap(const MatrixType& m)
  22. {
  23. typedef typename other_matrix_type<MatrixType>::type OtherMatrixType;
  24. typedef typename MatrixType::Scalar Scalar;
  25. eigen_assert((!internal::is_same<MatrixType,OtherMatrixType>::value));
  26. typename MatrixType::Index rows = m.rows();
  27. typename MatrixType::Index cols = m.cols();
  28. // construct 3 matrix guaranteed to be distinct
  29. MatrixType m1 = MatrixType::Random(rows,cols);
  30. MatrixType m2 = MatrixType::Random(rows,cols) + Scalar(100) * MatrixType::Identity(rows,cols);
  31. OtherMatrixType m3 = OtherMatrixType::Random(rows,cols) + Scalar(200) * OtherMatrixType::Identity(rows,cols);
  32. MatrixType m1_copy = m1;
  33. MatrixType m2_copy = m2;
  34. OtherMatrixType m3_copy = m3;
  35. // test swapping 2 matrices of same type
  36. Scalar *d1=m1.data(), *d2=m2.data();
  37. m1.swap(m2);
  38. VERIFY_IS_APPROX(m1,m2_copy);
  39. VERIFY_IS_APPROX(m2,m1_copy);
  40. if(MatrixType::SizeAtCompileTime==Dynamic)
  41. {
  42. VERIFY(m1.data()==d2);
  43. VERIFY(m2.data()==d1);
  44. }
  45. m1 = m1_copy;
  46. m2 = m2_copy;
  47. // test swapping 2 matrices of different types
  48. m1.swap(m3);
  49. VERIFY_IS_APPROX(m1,m3_copy);
  50. VERIFY_IS_APPROX(m3,m1_copy);
  51. m1 = m1_copy;
  52. m3 = m3_copy;
  53. // test swapping matrix with expression
  54. m1.swap(m2.block(0,0,rows,cols));
  55. VERIFY_IS_APPROX(m1,m2_copy);
  56. VERIFY_IS_APPROX(m2,m1_copy);
  57. m1 = m1_copy;
  58. m2 = m2_copy;
  59. // test swapping two expressions of different types
  60. m1.transpose().swap(m3.transpose());
  61. VERIFY_IS_APPROX(m1,m3_copy);
  62. VERIFY_IS_APPROX(m3,m1_copy);
  63. m1 = m1_copy;
  64. m3 = m3_copy;
  65. if(m1.rows()>1)
  66. {
  67. // test assertion on mismatching size -- matrix case
  68. VERIFY_RAISES_ASSERT(m1.swap(m1.row(0)));
  69. // test assertion on mismatching size -- xpr case
  70. VERIFY_RAISES_ASSERT(m1.row(0).swap(m1));
  71. }
  72. }
  73. void test_swap()
  74. {
  75. int s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE);
  76. CALL_SUBTEST_1( swap(Matrix3f()) ); // fixed size, no vectorization
  77. CALL_SUBTEST_2( swap(Matrix4d()) ); // fixed size, possible vectorization
  78. CALL_SUBTEST_3( swap(MatrixXd(s,s)) ); // dyn size, no vectorization
  79. CALL_SUBTEST_4( swap(MatrixXf(s,s)) ); // dyn size, possible vectorization
  80. TEST_SET_BUT_UNUSED_VARIABLE(s)
  81. }