test_move_semantics.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /** test move semantics - run with and without BOOST_UBLAS_MOVE_SEMANTICS defined */
  2. // Copyright Nasos Iliopoulos, Gunter Winkler 2009.
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #define BOOST_UBLAS_MOVE_SEMANTICS
  7. #include <boost/numeric/ublas/vector.hpp>
  8. #include <boost/numeric/ublas/matrix.hpp>
  9. #include <boost/numeric/ublas/io.hpp>
  10. namespace ublas= boost::numeric::ublas;
  11. std::vector<double> a;
  12. ublas::vector<double> doubleit(ublas::vector<double> m)
  13. {
  14. ublas::vector<double> r;
  15. r=2.0*m;
  16. std::cout << "Temporary pointer: " << &r[0] << std::endl;
  17. return r;
  18. }
  19. template <class T,size_t N>
  20. ublas::bounded_vector<T,N > doubleit(ublas::bounded_vector<T, N> m)
  21. {
  22. ublas::bounded_vector<T,N> r;
  23. r=2.0*m;
  24. std::cout << "Temporary pointer: " << &r[0] << std::endl;
  25. return r;
  26. }
  27. template <class T,size_t N>
  28. ublas::c_vector<T,N > doubleit(ublas::c_vector<T, N> m)
  29. {
  30. ublas::c_vector<T,N> r;
  31. r=2.0*m;
  32. std::cout << "Temporary pointer: " << &r[0] << std::endl;
  33. return r;
  34. }
  35. ublas::matrix<double> doubleit(ublas::matrix<double> m)
  36. {
  37. ublas::matrix<double> r;
  38. r=2.0*m;
  39. std::cout << "Temporary pointer r: " << &r(0,0) << std::endl;
  40. return r;
  41. }
  42. template <class T,size_t N, size_t M>
  43. ublas::bounded_matrix<T,N, M > doubleit(ublas::bounded_matrix<T, N, M> m)
  44. {
  45. ublas::bounded_matrix<T,N, M> r;
  46. r=2.0*m;
  47. std::cout << "Temporary pointer: " << &(r(0,0)) << std::endl;
  48. return r;
  49. }
  50. template <class T,size_t N, size_t M>
  51. ublas::c_matrix<T,N, M > doubleit(ublas::c_matrix<T, N, M> m)
  52. {
  53. ublas::c_matrix<T,N, M> r;
  54. r=2.0*m;
  55. std::cout << "Temporary pointer: " << &(r(0,0)) << std::endl;
  56. return r;
  57. }
  58. void test1()
  59. {
  60. std::cout << "vector<double> --------------------------------------------------------------------" << std::endl;
  61. ublas::vector<double> a(ublas::scalar_vector<double>(2,2.0));
  62. a = doubleit(a);
  63. std::cout << "Pointer (must be equal to temp. pointer if move semantics are enabled) : " << &a[0] << std::endl;
  64. std::cout << a << std::endl;
  65. std::cout << "bounded_vector<double,2> --------------------------------------------------------------------" << std::endl;
  66. ublas::bounded_vector<double,2> b(ublas::scalar_vector<double>(2,2.0));
  67. ublas::bounded_vector<double,2> c;
  68. noalias(c)=doubleit(b);
  69. std::cout << "Pointer (bounded_vector swaps by copy this should be different than temp. pointer) : " << &c[0] << std::endl;
  70. c(1)=0.0;
  71. std::cout << b << std::endl;
  72. std::cout << c << std::endl;
  73. std::cout << "c_vector<double,2> --------------------------------------------------------------------" << std::endl;
  74. ublas::c_vector<double,2> e=ublas::scalar_vector<double>(2,2.0);
  75. ublas::c_vector<double,2> f;
  76. f=doubleit(e);
  77. std::cout << "Pointer (c_vector swaps by copy this should be different than temp. pointer) : " << &f[0] << std::endl;
  78. f(1)=0;
  79. std::cout << e << std::endl;
  80. std::cout << f << std::endl;
  81. }
  82. void test2() {
  83. std::cout << "matrix<double> --------------------------------------------------------------------" << std::endl;
  84. ublas::matrix<double> a(ublas::scalar_matrix<double>(2, 3, 2.0));
  85. a = doubleit(a);
  86. std::cout << "Pointer (must be equal to temp. pointer if move semantics are enabled) : " << &(a(0,0)) << std::endl;
  87. std::cout << a << std::endl;
  88. std::cout << "bounded_matrix<double,2, 3> --------------------------------------------------------------------" << std::endl;
  89. ublas::bounded_matrix<double,2, 3> b(ublas::scalar_matrix<double>(2,3, 2.0));
  90. ublas::bounded_matrix<double,2, 3> c;
  91. noalias(c)=doubleit(b);
  92. std::cout << "Pointer (bounded_matrix swaps by copy this should be different than temp. pointer) : " << &(c(0,0)) << std::endl;
  93. c(1,1)=0.0;
  94. std::cout << b << std::endl;
  95. std::cout << c << std::endl;
  96. std::cout << "c_matrix<double,2 ,3> --------------------------------------------------------------------" << std::endl;
  97. ublas::c_matrix<double,2, 3> e=ublas::scalar_matrix<double>(2,3, 2.0);
  98. ublas::c_matrix<double,2, 3> f;
  99. f=doubleit(e);
  100. std::cout << "Pointer (c_matrix swaps by copy this should be different than temp. pointer) : " << &(f(0,0)) << std::endl;
  101. f(1,1)=0;
  102. std::cout << e << std::endl;
  103. std::cout << f << std::endl;
  104. }
  105. int main(){
  106. test1();
  107. test2();
  108. return 0;
  109. }