我有一个名为 DataView 的函数,它执行“切片”(创建 array_view),如下所示:
template <class T>
typename Boost<T>::Array2D::template array_view<2>::type MyArray<T>::DataView(int x_start, int x_finish, int y_start, int y_finish)
{
using range = boost::multi_array_types::index_range;
return _Data[boost::indices[range().start(x_start).finish(x_finish).stride(1)][range().start(y_start).finish(y_finish).stride(1)]];
}
...其中 _Data 是 Array2D (boost::multi_array) 类型的私有成员,其中包含 MyArray 的数据部分。切片(视图创建)没有问题,如下所示:
Boost<double>::Array2D::array_view<2>::type array_view = my_array->DataView(x1, x2, y1, y2);
当我想将新获取的视图 (array_view) 分配回原始的 multi_array (my_array) 时,就会出现问题。我试图通过在 MyArray 类上创建这个函数来做到这一点:
template <class T>
void MyArray<T>::Data(const typename Boost<T>::Array2D &inData)
{
// find the dimensions of inData
int xdim = static_cast<int>(inData.shape()[0]);
int ydim = static_cast<int>(inData.shape()[1]);
// resize _Data to match the size of inData
_Data.resize(boost::extents[xdim][ydim]);
// assign _Data to the new set of data
_Data = inData;
}
...并使用这行代码调用函数...
my_array->Data(array_view);
构建成功时,我的应用程序产生以下错误:
base.hpp:178: Reference boost::detail::multi_array::value_accessor_one<T>::access(boost::type<Reference>, boost::detail::multi_array::value_accessor_one<T>::index, TPtr, const size_type*, const index*, const index*) const [with Reference = double&; TPtr = double*; T = double; boost::detail::multi_array::value_accessor_one<T>::index = long int; boost::detail::multi_array::multi_array_base::size_type = long unsigned int]: Assertion `size_type(idx - index_bases[0]) < extents[0]' failed.
我真正需要的是一个简单的示例,说明如何执行以下操作之一:
- 使用我当前的策略,从我的原始 multi_array 创建一个 array_view 并将新视图分配回原始数组,或者
- 对原始的 multi_array 进行切片(就地)。我已经寻找了这个解决方案的一个例子,但似乎找不到它。
谢谢你。