我正在尝试从 multi_array 中提取子数组。对于这个演示,我们假设没有折叠维度(即子数组的维度与原始数组相同)。我想我正在正确地构建一个具有请求范围的视图(虽然很尴尬......),但是现在如何将数据从请求的索引(也就是视图的所有索引)复制到子数组中?这是一个大纲:
#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;
}