• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // reshape.cpp - testing reshaping functionality
15 //
16 
17 #include <boost/multi_array.hpp>
18 
19 #include <boost/core/lightweight_test.hpp>
20 
21 #include <boost/array.hpp>
22 #include <boost/type.hpp>
23 
24 int
main()25 main()
26 {
27   const int ndims=3;
28   typedef boost::multi_array<int,ndims> array;
29   typedef boost::multi_array_ref<int,ndims> array_ref;
30   typedef boost::const_multi_array_ref<int,ndims> const_array_ref;
31 
32   boost::array<array::size_type,ndims> dims = {{2,3,4}};
33   boost::array<array::size_type,ndims> new_dims = {{4,3,2}};
34 
35   int data[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,
36                  14,15,16,17,18,19,20,21,22,23};
37   const int data_size=24;
38 
39   // Basic reshape test
40   {
41     array A(dims);
42     A.assign(data,data+data_size);
43 
44     array_ref B(data,dims);
45     const_array_ref C(data,dims);
46 
47     A.reshape(new_dims);
48     B.reshape(new_dims);
49     C.reshape(new_dims);
50 
51     int* ptr = data;
52     for (array::index i = 0; i != 4; ++i)
53       for (array::index j = 0; j != 3; ++j)
54         for (array::index k = 0; k != 2; ++k) {
55           BOOST_TEST(A[i][j][k] == *ptr);
56           BOOST_TEST(B[i][j][k] == *ptr);
57           BOOST_TEST(C[i][j][k] == *ptr++);
58         }
59   }
60 
61   // Ensure that index bases are preserved over reshape
62   {
63     boost::array<array::index,ndims> bases = {{0, 1, -1}};
64 
65     array A(dims);
66     A.assign(data,data+data_size);
67 
68     array_ref B(data,dims);
69     const_array_ref C(data,dims);
70 
71     A.reindex(bases);
72     B.reindex(bases);
73     C.reindex(bases);
74 
75     A.reshape(new_dims);
76     B.reshape(new_dims);
77     C.reshape(new_dims);
78 
79     int* ptr = data;
80     for (array::index i = 0; i != 4; ++i)
81       for (array::index j = 1; j != 4; ++j)
82         for (array::index k = -1; k != 1; ++k) {
83           BOOST_TEST(A[i][j][k] == *ptr);
84           BOOST_TEST(B[i][j][k] == *ptr);
85           BOOST_TEST(C[i][j][k] == *ptr++);
86         }
87   }
88 
89   return boost::report_errors();
90 }
91 
92 
93 
94 
95 
96 
97 
98