resize_from_other.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2008 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. // resize_from_other.cpp - an experiment in writing a resize function for
  12. // multi_arrays that will use the extents from another to build itself.
  13. //
  14. #include <boost/multi_array.hpp>
  15. #include <boost/static_assert.hpp>
  16. #include <boost/array.hpp>
  17. #include <algorithm>
  18. template <typename T, typename U, std::size_t N>
  19. void
  20. resize_from_MultiArray(boost::multi_array<T,N>& marray, U& other) {
  21. // U must be a model of MultiArray
  22. boost::function_requires<
  23. boost::multi_array_concepts::ConstMultiArrayConcept<U,U::dimensionality> >();
  24. // U better have U::dimensionality == N
  25. BOOST_STATIC_ASSERT(U::dimensionality == N);
  26. boost::array<typename boost::multi_array<T,N>::size_type, N> shape;
  27. std::copy(other.shape(), other.shape()+N, shape.begin());
  28. marray.resize(shape);
  29. }
  30. #include <iostream>
  31. int main () {
  32. boost::multi_array<int,2> A(boost::extents[5][4]), B;
  33. boost::multi_array<int,3> C;
  34. resize_from_MultiArray(B,A);
  35. #if 0
  36. resize_from_MultiArray(C,A); // Compile-time error
  37. #endif
  38. std::cout << B.shape()[0] << ", " << B.shape()[1] << '\n';
  39. }