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