Brian Silverman | 6137817 | 2018-08-04 23:37:59 -0700 | [diff] [blame^] | 1 | // Copyright 2008 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 | // resize_from_other.cpp - an experiment in writing a resize function for |
| 15 | // multi_arrays that will use the extents from another to build itself. |
| 16 | // |
| 17 | |
| 18 | #include <boost/multi_array.hpp> |
| 19 | #include <boost/static_assert.hpp> |
| 20 | #include <boost/array.hpp> |
| 21 | #include <algorithm> |
| 22 | |
| 23 | template <typename T, typename U, std::size_t N> |
| 24 | void |
| 25 | resize_from_MultiArray(boost::multi_array<T,N>& marray, U& other) { |
| 26 | |
| 27 | // U must be a model of MultiArray |
| 28 | boost::function_requires< |
| 29 | boost::multi_array_concepts::ConstMultiArrayConcept<U,U::dimensionality> >(); |
| 30 | // U better have U::dimensionality == N |
| 31 | BOOST_STATIC_ASSERT(U::dimensionality == N); |
| 32 | |
| 33 | boost::array<typename boost::multi_array<T,N>::size_type, N> shape; |
| 34 | |
| 35 | std::copy(other.shape(), other.shape()+N, shape.begin()); |
| 36 | |
| 37 | marray.resize(shape); |
| 38 | |
| 39 | } |
| 40 | |
| 41 | #include <iostream> |
| 42 | |
| 43 | |
| 44 | int main () { |
| 45 | |
| 46 | boost::multi_array<int,2> A(boost::extents[5][4]), B; |
| 47 | boost::multi_array<int,3> C; |
| 48 | |
| 49 | resize_from_MultiArray(B,A); |
| 50 | |
| 51 | #if 0 |
| 52 | resize_from_MultiArray(C,A); // Compile-time error |
| 53 | #endif |
| 54 | |
| 55 | std::cout << B.shape()[0] << ", " << B.shape()[1] << '\n'; |
| 56 | |
| 57 | } |