slice.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright 2002 The Trustees of Indiana University.
  2. // Use, modification and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Boost.MultiArray Library
  6. // Authors: Ronald Garcia
  7. // Jeremy Siek
  8. // Andrew Lumsdaine
  9. // See http://www.boost.org/libs/multi_array for documentation.
  10. //
  11. // slice.cpp - testing out slicing on a matrices
  12. //
  13. #include "generative_tests.hpp"
  14. #include <boost/array.hpp>
  15. #include <boost/mpl/if.hpp>
  16. #include <boost/type_traits/is_same.hpp>
  17. template <typename Array>
  18. struct view_traits_mutable {
  19. public:
  20. #if 0 // RG - MSVC can't handle templates nested in templates. Use traits
  21. typedef typename Array::template array_view<3>::type array_view3;
  22. typedef typename Array::template array_view<2>::type array_view2;
  23. #endif
  24. typedef typename boost::array_view_gen<Array,3>::type array_view3;
  25. typedef typename boost::array_view_gen<Array,2>::type array_view2;
  26. };
  27. template <typename Array>
  28. struct view_traits_const {
  29. #if 0 // RG - MSVC can't handle templates nested in templates. Use traits
  30. typedef typename Array::template const_array_view<3>::type array_view3;
  31. typedef typename Array::template const_array_view<2>::type array_view2;
  32. #endif
  33. typedef typename boost::const_array_view_gen<Array,3>::type array_view3;
  34. typedef typename boost::const_array_view_gen<Array,2>::type array_view2;
  35. };
  36. // Meta-program selects the proper view_traits implementation.
  37. template <typename Array, typename ConstTag>
  38. struct view_traits_generator :
  39. boost::mpl::if_< boost::is_same<ConstTag,const_array_tag>,
  40. view_traits_const<Array>,
  41. view_traits_mutable<Array> >
  42. {};
  43. template <typename Array, typename ViewTraits>
  44. void test_views(Array& A, const ViewTraits&) {
  45. typedef typename Array::index index;
  46. typedef typename Array::index_range range;
  47. typename Array::index_gen indices;
  48. const index idx0 = A.index_bases()[0];
  49. const index idx1 = A.index_bases()[1];
  50. const index idx2 = A.index_bases()[2];
  51. // Standard View
  52. {
  53. typename ViewTraits::array_view3 B = A[
  54. indices[range(idx0+0,idx0+2)]
  55. [range(idx1+1,idx1+3)]
  56. [range(idx2+0,idx2+4,2)]
  57. ];
  58. for (index i = 0; i != 2; ++i)
  59. for (index j = 0; j != 2; ++j)
  60. for (index k = 0; k != 2; ++k) {
  61. BOOST_TEST(B[i][j][k] == A[idx0+i][idx1+j+1][idx2+k*2]);
  62. boost::array<index,3> elmts;
  63. elmts[0]=i; elmts[1]=j; elmts[2]=k;
  64. BOOST_TEST(B(elmts) == A[idx0+i][idx1+j+1][idx2+k*2]);
  65. }
  66. }
  67. // Degenerate dimensions
  68. {
  69. typename ViewTraits::array_view2 B =
  70. A[indices[range(idx0+0,idx0+2)][idx1+1][range(idx2+0,idx2+4,2)]];
  71. for (index i = 0; i != 2; ++i)
  72. for (index j = 0; j != 2; ++j) {
  73. BOOST_TEST(B[i][j] == A[idx0+i][idx1+1][idx2+j*2]);
  74. boost::array<index,2> elmts;
  75. elmts[0]=i; elmts[1]=j;
  76. BOOST_TEST(B(elmts) == A[idx0+i][idx1+1][idx2+j*2]);
  77. }
  78. }
  79. // Flip the third dimension
  80. {
  81. typename ViewTraits::array_view3 B = A[
  82. indices[range(idx0+0,idx0+2)]
  83. [range(idx1+0,idx1+2)]
  84. [range(idx2+2,idx2+0,-1)]
  85. ];
  86. // typename ViewTraits::array_view3 B =
  87. // A[indices[range(idx0+0,idx0+2)][idx1+1][range(idx2+0,idx2+4,2)]];
  88. for (index i = 0; i != 2; ++i)
  89. for (index j = 0; j != 2; ++j)
  90. for (index k = 0; k != 2; ++k) {
  91. BOOST_TEST(B[i][j][k] == A[idx0+i][idx1+j][idx2+2-k]);
  92. boost::array<index,3> elmts;
  93. elmts[0]=i; elmts[1]=j; elmts[2]=k;
  94. BOOST_TEST(B(elmts) == A[idx0+i][idx1+j][idx2+2-k]);
  95. }
  96. }
  97. ++tests_run;
  98. }
  99. template <typename Array>
  100. void access(Array& A, const mutable_array_tag&) {
  101. assign(A);
  102. typedef typename view_traits_generator<Array,mutable_array_tag>::type
  103. m_view_traits;
  104. typedef typename view_traits_generator<Array,const_array_tag>::type
  105. c_view_traits;
  106. test_views(A,m_view_traits());
  107. test_views(A,c_view_traits());
  108. const Array& CA = A;
  109. test_views(CA,c_view_traits());
  110. }
  111. template <typename Array>
  112. void access(Array& A, const const_array_tag&) {
  113. typedef typename view_traits_generator<Array,const_array_tag>::type
  114. c_view_traits;
  115. test_views(A,c_view_traits());
  116. }
  117. int main() {
  118. return run_generative_tests();
  119. }