| 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 | // index_bases - test of the index_base modifying facilities. |
| 15 | // |
| 16 | |
| 17 | #include <boost/multi_array.hpp> |
| 18 | |
| 19 | #include <boost/core/lightweight_test.hpp> |
| 20 | |
| 21 | #include <boost/array.hpp> |
| 22 | #include <vector> |
| 23 | #include <iostream> |
| 24 | int |
| 25 | main() |
| 26 | { |
| 27 | typedef boost::multi_array<double, 3> array; |
| 28 | typedef boost::multi_array_ref<double, 3> array_ref; |
| 29 | typedef boost::const_multi_array_ref<double, 3> const_array_ref; |
| 30 | typedef array::array_view<3>::type array_view; |
| 31 | |
| 32 | typedef array::size_type size_type; |
| 33 | typedef array::extent_range range; |
| 34 | typedef array::index_range irange; |
| 35 | |
| 36 | array::extent_gen extents; |
| 37 | array::index_gen indices; |
| 38 | |
| 39 | // Construct with nonzero bases |
| 40 | { |
| 41 | |
| 42 | array A(extents[range(1,4)][range(2,5)][range(3,6)]); |
| 43 | array B(extents[3][3][3]); |
| 44 | |
| 45 | double ptr[27]; |
| 46 | array_ref |
| 47 | C(ptr,extents[range(1,4)][range(2,5)][range(3,6)]); |
| 48 | |
| 49 | const_array_ref |
| 50 | D(ptr,extents[range(1,4)][range(2,5)][range(3,6)]); |
| 51 | |
| 52 | array_view E = A[indices[irange()][irange()][irange()]]; |
| 53 | |
| 54 | std::vector<double> vals; |
| 55 | for (int i = 0; i < 27; ++i) |
| 56 | vals.push_back(x: i); |
| 57 | |
| 58 | A.assign(begin: vals.begin(),end: vals.end()); |
| 59 | B.assign(begin: vals.begin(),end: vals.end()); |
| 60 | C.assign(begin: vals.begin(),end: vals.end()); |
| 61 | |
| 62 | boost::array<int,3> bases = { .elems: { 1, 2, 3 } }; |
| 63 | for (size_type a = 0; a < A.shape()[0]; ++a) |
| 64 | for (size_type b = 0; b < A.shape()[1]; ++b) |
| 65 | for (size_type c = 0; c < A.shape()[2]; ++c) { |
| 66 | BOOST_TEST(A[a+bases[0]][b+bases[1]][c+bases[2]] == B[a][b][c]); |
| 67 | BOOST_TEST(C[a+bases[0]][b+bases[1]][c+bases[2]] == B[a][b][c]); |
| 68 | BOOST_TEST(D[a+bases[0]][b+bases[1]][c+bases[2]] == B[a][b][c]); |
| 69 | // Test that E does not inherit A's index_base |
| 70 | BOOST_TEST(E[a][b][c] == B[a][b][c]); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // Reindex |
| 75 | { |
| 76 | typedef array::size_type size_type; |
| 77 | array A(extents[3][3][3]), B(extents[3][3][3]); |
| 78 | |
| 79 | double ptr[27]; |
| 80 | array_ref C(ptr,extents[3][3][3]); |
| 81 | const_array_ref D(ptr,extents[3][3][3]); |
| 82 | |
| 83 | array_view E = B[indices[irange()][irange()][irange()]]; |
| 84 | |
| 85 | std::vector<double> vals; |
| 86 | for (int i = 0; i < 27; ++i) |
| 87 | vals.push_back(x: i); |
| 88 | |
| 89 | A.assign(begin: vals.begin(),end: vals.end()); |
| 90 | B.assign(begin: vals.begin(),end: vals.end()); |
| 91 | C.assign(begin: vals.begin(),end: vals.end()); |
| 92 | |
| 93 | boost::array<int,3> bases = { .elems: { 1, 2, 3 } }; |
| 94 | |
| 95 | A.reindex(values: bases); |
| 96 | C.reindex(values: bases); |
| 97 | D.reindex(values: bases); |
| 98 | E.reindex(values: bases); |
| 99 | |
| 100 | for (size_type a = 0; a < A.shape()[0]; ++a) |
| 101 | for (size_type b = 0; b < A.shape()[1]; ++b) |
| 102 | for (size_type c = 0; c < A.shape()[2]; ++c) { |
| 103 | BOOST_TEST(A[a+bases[0]][b+bases[1]][c+bases[2]] == B[a][b][c]); |
| 104 | BOOST_TEST(C[a+bases[0]][b+bases[1]][c+bases[2]] == B[a][b][c]); |
| 105 | BOOST_TEST(D[a+bases[0]][b+bases[1]][c+bases[2]] == B[a][b][c]); |
| 106 | BOOST_TEST(E[a+bases[0]][b+bases[1]][c+bases[2]] == B[a][b][c]); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // Set Index Base |
| 111 | { |
| 112 | typedef array::size_type size_type; |
| 113 | array A(extents[3][3][3]), B(extents[3][3][3]); |
| 114 | |
| 115 | double ptr[27]; |
| 116 | array_ref C(ptr,extents[3][3][3]); |
| 117 | const_array_ref D(ptr,extents[3][3][3]); |
| 118 | |
| 119 | array_view E = B[indices[irange()][irange()][irange()]]; |
| 120 | |
| 121 | std::vector<double> vals; |
| 122 | for (int i = 0; i < 27; ++i) |
| 123 | vals.push_back(x: i); |
| 124 | |
| 125 | A.assign(begin: vals.begin(),end: vals.end()); |
| 126 | B.assign(begin: vals.begin(),end: vals.end()); |
| 127 | C.assign(begin: vals.begin(),end: vals.end()); |
| 128 | |
| 129 | #ifdef BOOST_NO_SFINAE |
| 130 | typedef boost::multi_array_types::index index; |
| 131 | A.reindex(index(1)); |
| 132 | C.reindex(index(1)); |
| 133 | D.reindex(index(1)); |
| 134 | E.reindex(index(1)); |
| 135 | #else |
| 136 | A.reindex(value: 1); |
| 137 | C.reindex(value: 1); |
| 138 | D.reindex(value: 1); |
| 139 | E.reindex(value: 1); |
| 140 | #endif |
| 141 | |
| 142 | for (size_type a = 0; a < A.shape()[0]; ++a) |
| 143 | for (size_type b = 0; b < A.shape()[1]; ++b) |
| 144 | for (size_type c = 0; c < A.shape()[2]; ++c) { |
| 145 | BOOST_TEST(A[a+1][b+1][c+1] == B[a][b][c]); |
| 146 | BOOST_TEST(C[a+1][b+1][c+1] == B[a][b][c]); |
| 147 | BOOST_TEST(D[a+1][b+1][c+1] == B[a][b][c]); |
| 148 | BOOST_TEST(E[a+1][b+1][c+1] == B[a][b][c]); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | return boost::report_errors(); |
| 153 | } |
| 154 | |