subview.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. #include "boost/multi_array.hpp"
  11. #include "boost/cstdlib.hpp"
  12. int
  13. main()
  14. {
  15. const int ndims=3;
  16. typedef boost::multi_array<int,ndims> array;
  17. int data[] = {
  18. 0,1,2,3,
  19. 4,5,6,7,
  20. 8,9,10,11,
  21. 12,13,14,15,
  22. 16,17,18,19,
  23. 20,21,22,23
  24. };
  25. const int data_size=24;
  26. array myarray(boost::extents[2][3][4]);
  27. myarray.assign(data,data+data_size);
  28. //
  29. // array_view dims:
  30. // [base,stride,bound)
  31. // [0,1,2), [1,1,3), [0,2,4)
  32. //
  33. typedef array::index_range range;
  34. array::array_view<ndims>::type myview =
  35. myarray[boost::indices[range(0,2)][range(1,3)][range(0,4,2)]];
  36. for (array::index i = 0; i != 2; ++i)
  37. for (array::index j = 0; j != 2; ++j)
  38. for (array::index k = 0; k != 2; ++k)
  39. assert(myview[i][j][k] == myarray[i][j+1][k*2]);
  40. return boost::exit_success;
  41. }