1 // Copyright 2002 The Trustees of Indiana University.
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)
7 // Boost.MultiArray Library
8 // Authors: Ronald Garcia
11 // See http://www.boost.org/libs/multi_array for documentation.
14 // slice.cpp - testing out slicing on a matrices
17 #include "generative_tests.hpp"
18 #include <boost/array.hpp>
19 #include <boost/mpl/if.hpp>
20 #include <boost/type_traits/is_same.hpp>
22 template <typename Array
>
23 struct view_traits_mutable
{
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
;
29 typedef typename
boost::array_view_gen
<Array
,3>::type array_view3
;
30 typedef typename
boost::array_view_gen
<Array
,2>::type array_view2
;
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
;
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
;
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
> >
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
;
59 const index idx0
= A
.index_bases()[0];
60 const index idx1
= A
.index_bases()[1];
61 const index idx2
= A
.index_bases()[2];
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)]
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]);
80 // Degenerate dimensions
82 typename
ViewTraits::array_view2 B
=
83 A
[indices
[range(idx0
+0,idx0
+2)][idx1
+1][range(idx2
+0,idx2
+4,2)]];
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]);
94 // Flip the third dimension
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)]
102 // typename ViewTraits::array_view3 B =
103 // A[indices[range(idx0+0,idx0+2)][idx1+1][range(idx2+0,idx2+4,2)]];
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
]);
119 template <typename Array
>
120 void access(Array
& A
, const mutable_array_tag
&) {
123 typedef typename view_traits_generator
<Array
,mutable_array_tag
>::type
126 typedef typename view_traits_generator
<Array
,const_array_tag
>::type
129 test_views(A
,m_view_traits());
130 test_views(A
,c_view_traits());
133 test_views(CA
,c_view_traits());
136 template <typename Array
>
137 void access(Array
& A
, const const_array_tag
&) {
138 typedef typename view_traits_generator
<Array
,const_array_tag
>::type
140 test_views(A
,c_view_traits());
145 return run_generative_tests();