In any case I would be more worried about defining the overload of
std::swap with the right implementation by swapping element by
element, instead of creating a huge temporary array. If you figure out
that code, please paste it here.
I couldn't resist, here is the code that properly swaps subarrays, it
should work for subarrays of arbitrary dimension and creates no big
temporaries (just element temporaries).
--
#include
namespace std
{
template
void swap(
boost::detail::multi_array::sub_array lhs,
boost::detail::multi_array::sub_array rhs)
{
assert(lhs.size()==rhs.size());
for(
typename boost::detail::multi_array::sub_array::iterator it_lhs=lhs.begin(), it_rhs=rhs.begin();
it_lhs!=lhs.end();
++it_lhs, ++it_rhs
){
std::iter_swap(it_lhs, it_rhs);
//same as std::swap(*it_lhs, *it_rhs),
//if *it_lhs is not an "element" (i.e. another subarray then
this very same std::swap
// function is called. If it is an element (e.g. double
then it actually performs the element
// by element swap)
}
// this function is supposed to do the same as the following ,
but more efficiently
//boost::multi_array tmp = lhs;
//lhs = rhs;
//rhs = tmp;
}
} // namespace std
--
I now wonder if something like this shouldn't be part of the library.
Although it is not clear to me if the library is still under active
development.
Alfredo