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_TEST(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_TEST(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_TEST(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_TEST(B(elmts) == A[idx0+i][idx1+1][idx2+j*2]); |
91 | } |
92 | } |
93 | |
94 | // Flip the third dimension |
95 | { |
96 | typename ViewTraits::array_view3 B = A[ |
97 | indices[range(idx0+0,idx0+2)] |
98 | [range(idx1+0,idx1+2)] |
99 | [range(idx2+2,idx2+0,-1)] |
100 | ]; |
101 | |
102 | // typename ViewTraits::array_view3 B = |
103 | // A[indices[range(idx0+0,idx0+2)][idx1+1][range(idx2+0,idx2+4,2)]]; |
104 | |
105 | for (index i = 0; i != 2; ++i) |
106 | for (index j = 0; j != 2; ++j) |
107 | for (index k = 0; k != 2; ++k) { |
108 | BOOST_TEST(B[i][j][k] == A[idx0+i][idx1+j][idx2+2-k]); |
109 | boost::array<index,3> elmts; |
110 | elmts[0]=i; elmts[1]=j; elmts[2]=k; |
111 | BOOST_TEST(B(elmts) == A[idx0+i][idx1+j][idx2+2-k]); |
112 | } |
113 | } |
114 | |
115 | ++tests_run; |
116 | } |
117 | |
118 | |
119 | template <typename Array> |
120 | void access(Array& A, const mutable_array_tag&) { |
121 | assign(A); |
122 | |
123 | typedef typename view_traits_generator<Array,mutable_array_tag>::type |
124 | m_view_traits; |
125 | |
126 | typedef typename view_traits_generator<Array,const_array_tag>::type |
127 | c_view_traits; |
128 | |
129 | test_views(A,m_view_traits()); |
130 | test_views(A,c_view_traits()); |
131 | |
132 | const Array& CA = A; |
133 | test_views(CA,c_view_traits()); |
134 | } |
135 | |
136 | template <typename Array> |
137 | void access(Array& A, const const_array_tag&) { |
138 | typedef typename view_traits_generator<Array,const_array_tag>::type |
139 | c_view_traits; |
140 | test_views(A,c_view_traits()); |
141 | } |
142 | |
143 | |
144 | int main() { |
145 | return run_generative_tests(); |
146 | } |
147 | |