gpt4 book ai didi

c++ - 如何在运行时构建 multi_array::index_gen

转载 作者:搜寻专家 更新时间:2023-10-31 02:16:50 24 4
gpt4 key购买 nike

在下面的代码中,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;
}

最佳答案

编译时迭代通常必须用递归来实现。您可以使用递归模板构建索引对象。它看起来像这样:

#include <boost/multi_array.hpp>

template <typename T, size_t Dimension>
boost::multi_array<T, Dimension> ExtractSubArray(
const boost::multi_array<T, Dimension>& array,
const typename boost::detail::multi_array::index_gen<Dimension, Dimension>& indices)
{
using ArrayType = boost::multi_array<T, Dimension>;
using IndexType = boost::array<size_t, 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;
}

// Helper functor to build indices.
template<typename RangeArrayType, size_t Dimension>
struct IndicesBuilder {
// Recursively invoke the functor for the next lowest dimension and
// add the next range.
static auto build(const RangeArrayType& range)
-> decltype(IndicesBuilder<RangeArrayType, Dimension - 1>::build(range)[range[Dimension - 1]]) {
return IndicesBuilder<RangeArrayType, Dimension - 1>::build(range)[range[Dimension - 1]];
}
};

// Helper functor specialization to terminate recursion.
template<typename RangeArrayType>
struct IndicesBuilder<RangeArrayType, 1> {
static auto build(const RangeArrayType& range)
-> decltype(boost::indices[range[0]]) {
return boost::indices[range[0]];
}
};

template <typename T, size_t Dimension>
boost::multi_array<T, Dimension> ExtractSubArrayCornerAndExtent(
const boost::multi_array<T, Dimension>& array,
const boost::array<size_t, Dimension>& corner,
const boost::array<size_t, Dimension>& subarraySize)
{
using ArrayType = boost::multi_array<T, Dimension>;
using RangeType = typename ArrayType::index_range;

// Build a random-access container with the ranges.
std::vector<RangeType> range;
for (size_t i = 0; i < Dimension; ++i)
range.push_back(RangeType(corner[i], corner[i] + subarraySize[i]));

// Use the helper functor to build the index object.
const auto index = IndicesBuilder<decltype(range), Dimension>::build(range);

return ExtractSubArray<T, Dimension>(array, index);
}

int main() {
using ArrayType = boost::multi_array<double, 3>;
using IndexType = boost::array<size_t, 3>;

ArrayType myArray(IndexType({{3,3,3}}));

IndexType corner = {0,0,0};
IndexType subarraySize = {1,1,1};
ArrayType subArray2 = ExtractSubArrayCornerAndExtent(myArray, corner, subarraySize);

return 0;
};

添加到您的代码中的是 IndicesBuilder 模板结构,它具有用于构建索引对象的一维的静态函数。该静态函数调用自身(使用不同的模板参数)来构建所有维度。递归由返回第一个维度的 IndicesBuilder 的特化终止。尾随返回类型万岁!

我编译并运行了这段代码,没有出现 fatal error ,但我没有做任何进一步的测试,因此可能存在一些小错误。不过,一般方法应该可行。

关于c++ - 如何在运行时构建 multi_array::index_gen,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36501811/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com