I am trying to extract a sub-array from a multi_array. For this demo, let's assume that there are no collapsed dimensions (i.e. the dimensionality of the sub-array is the same as the original array). I think I am constructing a view with the requested extents correctly (although awkwardly...), but now how do I copy the data from the requested indices (aka all indices of the view) into the sub-array? Here is an outline:
#include <boost/multi_array.hpp>
const unsigned int Dimension = 3;
using ArrayType = boost::multi_array<double, Dimension>;
using IndexType = boost::array<ArrayType::index, Dimension>;
ArrayType ExtractSubGrid(const ArrayType& array, const typename boost::detail::multi_array::index_gen<Dimension, Dimension>& indices)
{
typename ArrayType::template const_array_view<Dimension>::type view = array[indices];
IndexType subArraySize;
for(size_t dim = 0 ; dim < Dimension; ++dim) {
subArraySize[dim] = indices.ranges_[dim].finish_ - indices.ranges_[dim].start_;
}
ArrayType subArray;
subArray.resize(subArraySize);
// How to do this?
//subArray.data() = view.data();
return subArray;
}
int main()
{
ArrayType myArray(IndexType({{3,4,2}}));
boost::detail::multi_array::index_gen<3,3> indices = boost::indices[ArrayType::index_range(0,2)][ArrayType::index_range(1,3)][ArrayType::index_range(0,4)];
ArrayType subGrid = ExtractSubGrid(myArray, indices);
return 0;
}