1

What is the complexity of boost::multi_array reshape() function? I expect it to be O(1) but I can't find this info in the documentation. The documentation for this library is actually pretty scarce.

The reason I'm asking is that I would like to iterate through a multi_array object using a single loop (I don't care about array indices). It seems like the library doesn't provide a way of iterating through an array using a single iterator. So, as a workaround, I'd like to reshape the array along a single dimension first (with other dimensions set to 1). Then I can iterate through the array using a single loop. However, I'm not sure how efficient the reshape() operation is.

Hence my second question: Is there an easy way to iterate through all the elements of a multi-array object using a single loop?

Alexey
  • 5,898
  • 9
  • 44
  • 81
  • "Oh, just look into the sources!" It must be *obvious* from there! :) [sorry, could not keep it; good luck] – mlvljr Sep 18 '14 at 03:33
  • 1
    @mlvljr yep, you are right! :) but not sure which source file has the definition – Alexey Sep 18 '14 at 03:43

1 Answers1

1

Below is the implementation of reshape function in multi_array_ref.hpp file.

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_);
}

It looks like the function just re-indexes the elements in extents object associated with array size. The function is linear in the number of elements in extends. But I think it's complexity is constant in the total number of elements in the array.

Alexey
  • 5,898
  • 9
  • 44
  • 81