This is a follow-up question for A filled_multi_array Template Function for Boost.MultiArray in C++. Based on G. Sliepen's answer, there is another get_extents
template function mentioned for handling retrieving boost::detail::multi_array::extent_gen<NumDims>
type size information from a multi_array
. I am trying to implement this get_extents
template function here.
template<class T, std::size_t NumDims>
auto get_extents(const boost::multi_array<T, NumDims>& input) {
boost::detail::multi_array::extent_gen<NumDims> output;
std::vector<size_t> shape_list;
for (size_t i = 0; i < NumDims; i++)
{
shape_list.push_back(input.shape()[i]);
}
std::copy(shape_list.begin(), shape_list.end(), output.ranges_.begin());
return output;
}
I am trying to copy shape content into the output
object. In general cases, boost::detail::multi_array::extent_gen
object is always constructed by boost::extents[][][]...
. However, I am not sure how to use boost::extents
in this context and I fill the values of shape information into output.ranges_
instead. It works with the following testing code.
auto filled_multi_array1 = filled_multi_array(boost::extents[3][4][2], 1.0);
auto filled_multi_array2 = filled_multi_array(get_extents(filled_multi_array1), 2.0);
// Checking shape
std::cout << "filled_multi_array2.shape()[0]:" << filled_multi_array2.shape()[0] << std::endl;
std::cout << "filled_multi_array2.shape()[1]:" << filled_multi_array2.shape()[1] << std::endl;
std::cout << "filled_multi_array2.shape()[2]:" << filled_multi_array2.shape()[2] << std::endl;
for (decltype(filled_multi_array2)::index i = 0; i != filled_multi_array2.shape()[0]; ++i)
for (decltype(filled_multi_array2)::index j = 0; j != filled_multi_array2.shape()[1]; ++j)
for (decltype(filled_multi_array2)::index k = 0; k != filled_multi_array2.shape()[2]; ++k)
std::cout << filled_multi_array2[i][j][k] << std::endl;
All suggestions are welcome.
The summary information:
Which question it is a follow-up to?
A filled_multi_array Template Function for Boost.MultiArray in C++
What changes has been made in the code since last question?
The
extent_gen
function implementation is the main part in this question.Why a new review is being asked for?
The
shape_list
which type isstd::vector<size_t>
inget_extents
function is used just for copying shape data intooutput.ranges_
. The reason why I create astd::vector
for this operation is that there is neither.begin()
function nor.end()
function ininput.shape()
. Something likestd::copy(input.shape().begin(), input.shape().end(), output.ranges_.begin());
doesn't work. If there is any possible improvement, please let me know.