storage_order.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright 2002 The Trustees of Indiana University.
  2. // Use, modification and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Boost.MultiArray Library
  6. // Authors: Ronald Garcia
  7. // Jeremy Siek
  8. // Andrew Lumsdaine
  9. // See http://www.boost.org/libs/multi_array for documentation.
  10. //
  11. // storage_order.cpp - testing storage_order-isms.
  12. //
  13. #include <boost/multi_array.hpp>
  14. #include <boost/core/lightweight_test.hpp>
  15. #include <boost/array.hpp>
  16. int
  17. main()
  18. {
  19. const int ndims=3;
  20. int data_row[] = {
  21. 0,1,2,3,
  22. 4,5,6,7,
  23. 8,9,10,11,
  24. 12,13,14,15,
  25. 16,17,18,19,
  26. 20,21,22,23
  27. };
  28. int data_col[] = {
  29. 0,12,
  30. 4,16,
  31. 8,20,
  32. 1,13,
  33. 5,17,
  34. 9,21,
  35. 2,14,
  36. 6,18,
  37. 10,22,
  38. 3,15,
  39. 7,19,
  40. 11,23
  41. };
  42. const int num_elements = 24;
  43. // fortran storage order
  44. {
  45. typedef boost::multi_array<int,ndims> array;
  46. array::extent_gen extents;
  47. array A(extents[2][3][4],boost::fortran_storage_order());
  48. A.assign(data_col,data_col+num_elements);
  49. int* num = data_row;
  50. for (array::index i = 0; i != 2; ++i)
  51. for (array::index j = 0; j != 3; ++j)
  52. for (array::index k = 0; k != 4; ++k)
  53. BOOST_TEST(A[i][j][k] == *num++);
  54. }
  55. // Mimic fortran_storage_order using
  56. // general_storage_order data placement
  57. {
  58. typedef boost::general_storage_order<ndims> storage;
  59. typedef boost::multi_array<int,ndims> array;
  60. array::size_type ordering[] = {0,1,2};
  61. bool ascending[] = {true,true,true};
  62. array::extent_gen extents;
  63. array A(extents[2][3][4], storage(ordering,ascending));
  64. A.assign(data_col,data_col+num_elements);
  65. int* num = data_row;
  66. for (array::index i = 0; i != 2; ++i)
  67. for (array::index j = 0; j != 3; ++j)
  68. for (array::index k = 0; k != 4; ++k)
  69. BOOST_TEST(A[i][j][k] == *num++);
  70. }
  71. // general_storage_order with arbitrary storage order
  72. {
  73. typedef boost::general_storage_order<ndims> storage;
  74. typedef boost::multi_array<int,ndims> array;
  75. array::size_type ordering[] = {2,0,1};
  76. bool ascending[] = {true,true,true};
  77. array::extent_gen extents;
  78. array A(extents[2][3][4], storage(ordering,ascending));
  79. int data_arb[] = {
  80. 0,1,2,3,
  81. 12,13,14,15,
  82. 4,5,6,7,
  83. 16,17,18,19,
  84. 8,9,10,11,
  85. 20,21,22,23
  86. };
  87. A.assign(data_arb,data_arb+num_elements);
  88. int* num = data_row;
  89. for (array::index i = 0; i != 2; ++i)
  90. for (array::index j = 0; j != 3; ++j)
  91. for (array::index k = 0; k != 4; ++k)
  92. BOOST_TEST(A[i][j][k] == *num++);
  93. }
  94. // general_storage_order with descending dimensions.
  95. {
  96. const int ndims=3;
  97. typedef boost::general_storage_order<ndims> storage;
  98. typedef boost::multi_array<int,ndims> array;
  99. array::size_type ordering[] = {2,0,1};
  100. bool ascending[] = {false,true,true};
  101. array::extent_gen extents;
  102. array A(extents[2][3][4], storage(ordering,ascending));
  103. int data_arb[] = {
  104. 12,13,14,15,
  105. 0,1,2,3,
  106. 16,17,18,19,
  107. 4,5,6,7,
  108. 20,21,22,23,
  109. 8,9,10,11
  110. };
  111. A.assign(data_arb,data_arb+num_elements);
  112. int* num = data_row;
  113. for (array::index i = 0; i != 2; ++i)
  114. for (array::index j = 0; j != 3; ++j)
  115. for (array::index k = 0; k != 4; ++k)
  116. BOOST_TEST(A[i][j][k] == *num++);
  117. }
  118. return boost::report_errors();
  119. }