gpt4 book ai didi

c++ - boost::multi_array reshape() 函数的复杂性

转载 作者:太空宇宙 更新时间:2023-11-04 11:28:07 24 4
gpt4 key购买 nike

boost::multi_array reshape() 函数的复杂度是多少?我希望它是 O(1),但我无法在文档中找到此信息。该库的文档实际上非常稀缺。

我问的原因是我想使用单个循环遍历 multi_array 对象(我不关心数组索引)。该库似乎没有提供使用单个迭代器迭代数组的方法。因此,作为一种解决方法,我想首先沿单个维度 reshape 数组(其他维度设置为 1)。然后我可以使用单个循环遍历数组。但是,我不确定 reshape() 操作的效率如何。

因此我的第二个问题:是否有一种简单的方法可以使用单个循环遍历 multi-array 对象的所有元素?

最佳答案

下面是multi_array_ref.hpp文件中reshape函数的实现。

template <typename SizeList>
void reshape(const SizeList& extents) {
boost::function_requires<
CollectionConcept<SizeList> >();
BOOST_ASSERT(num_elements_ ==
std::accumulate(extents.begin(),extents.end(),
size_type(1),std::multiplies<size_type>()));

std::copy(extents.begin(),extents.end(),extent_list_.begin());
this->compute_strides(stride_list_,extent_list_,storage_);

origin_offset_ =
this->calculate_origin_offset(stride_list_,extent_list_,
storage_,index_base_list_);
}

看起来该函数只是重新索引 extents 对象中与数组大小相关联的元素。该函数与 extends 中的元素数量成线性关系。但我认为它的复杂性在数组中的元素总数中是恒定的。

关于c++ - boost::multi_array reshape() 函数的复杂性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25903561/

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