This is a follow-up question for A ones Function for Boost.MultiArray in C++. Thanks to G. Sliepen's answer, the mentioned method which can set various initial value is better in generic purpose usage. However, the previous function specified the input type which must be boost::detail::multi_array::extent_gen<NumDims>
. The type issue happened in some cases, an example is as below. The object filled_multi_array1
is created first with filled_multi_array(boost::extents[3][4][2], 1.0)
. This works well. I want to create a filled_multi_array2
object which size is as same as filled_multi_array1
and the initial value is 2.0
. This doesn't work in existed version of filled_multi_array
function.
auto filled_multi_array1 = filled_multi_array(boost::extents[3][4][2], 1.0);
auto filled_multi_array2 = filled_multi_array<double, filled_multi_array1.dimensionality>(filled_multi_array1.shape(), 2.0);
for (decltype(filled_multi_array2)::index i = 0; i != 3; ++i)
for (decltype(filled_multi_array2)::index j = 0; j != 4; ++j)
for (decltype(filled_multi_array2)::index k = 0; k != 2; ++k)
std::cout << filled_multi_array2[i][j][k] << std::endl;
Then, I found that the type returned from .shape()
function is this one const boost::detail::multi_array::multi_array_base::size_type*
. It seems that the another function overloading is needed to handle the above filled_multi_array2
case. The experimental code of the filled_multi_array
function with the input type const boost::detail::multi_array::multi_array_base::size_type*
is as follows.
template<class T, std::size_t NumDims>
auto filled_multi_array(const boost::detail::multi_array::multi_array_base::size_type* size, const T& value)
{
boost::multi_array<T, NumDims> output(reinterpret_cast<boost::array<size_t, NumDims> const&>(*size));
std::fill_n(output.data(), output.num_elements(), value);
return output;
}
With this overloaded function, filled_multi_array2
object can be generated successfully and the output is like:
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
The full implementation of filled_multi_array
template function:
template<class T, std::size_t NumDims>
auto filled_multi_array(boost::detail::multi_array::extent_gen<NumDims> size, const T& value)
{
boost::multi_array<T, NumDims> output(size);
std::fill_n(output.data(), output.num_elements(), value);
return output;
}
template<class T, std::size_t NumDims>
auto filled_multi_array(const boost::detail::multi_array::multi_array_base::size_type* size, const T& value)
{
boost::multi_array<T, NumDims> output(reinterpret_cast<boost::array<size_t, NumDims> const&>(*size));
std::fill_n(output.data(), output.num_elements(), value);
return output;
}
All suggestions are welcome.
The summary information:
Which question it is a follow-up to?
What changes has been made in the code since last question?
The another overloaded
filled_multi_array
function has been added here.Why a new review is being asked for?
I am trying to handle both cases, including the one with the specified shape directly and the other one with
.shape()
function (the shape is referenced from another Boost.MultiArray object). If there is any possible improvement, please let me know.