在下面的代码中,该ExtractSubArray
函数是完全通用的,而在ExtractSubArrayCornerAndExtent
编写代码时需要了解维数(以构造 RangeType 参数的序列)。有没有办法编写一个通用的ExtractSubArrayCornerAndExtent
(没有 SFINAE 为每个维度使用不同的函数(这会很烦人,并且需要一组固定的可能维度)。
#include <boost/multi_array.hpp>
template <unsigned int Dimension>
boost::multi_array<double, Dimension> ExtractSubArray(const boost::multi_array<double, Dimension>& array, const typename boost::detail::multi_array::index_gen<Dimension, Dimension>& indices)
{
using ArrayType = boost::multi_array<double, Dimension>;
using IndexType = boost::array<double, Dimension>;
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 = view;
return subArray;
}
template <unsigned int Dimension>
boost::multi_array<double, Dimension> ExtractSubArrayCornerAndExtent(const boost::multi_array<double, Dimension>& array, const boost::array<double, Dimension>& corner,
const boost::array<double, Dimension>& subarraySize)
{
using ArrayType = boost::multi_array<double, Dimension>;
using RangeType = typename ArrayType::index_range;
// Here I have assumed Dimension=3 to produce the second argument. How do you construct this second argument when you don't know Dimension ahead of time.
return ExtractSubArray<Dimension>(array, boost::indices[RangeType(corner[0],subarraySize[0])][RangeType(corner[1],subarraySize[1])][RangeType(corner[2],subarraySize[2])]);
}
int main()
{
using ArrayType = boost::multi_array<double, 3>;
using IndexType = boost::array<double, 3>;
ArrayType myArray(IndexType({{3,3,3}}));
std::vector<double> data(9);
for(size_t i = 0; i < data.size(); ++i) {
data[i] = i;
}
boost::detail::multi_array::index_gen<3,3> indices = boost::indices[ArrayType::index_range(0,1)][ArrayType::index_range(0,1)][ArrayType::index_range(0,1)];
ArrayType subArray = ExtractSubArray<3>(myArray, indices);
IndexType corner = {0,0,0};
IndexType subarraySize = {1,1,1};
ArrayType subArray2 = ExtractSubArrayCornerAndExtent<3>(myArray, corner, subarraySize);
return 0;
}