Boost logo

Boost Users :

Subject: Re: [Boost-users] [multi_array] std::swap and subarray
From: alfC (alfredo.correa_at_[hidden])
Date: 2010-02-21 06:12:36


> 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<boost/multi_array.hpp>
namespace std
{
template <typename TValue, boost::detail::multi_array::size_type K>
void swap(
  boost::detail::multi_array::sub_array<TValue, K> lhs,
  boost::detail::multi_array::sub_array<TValue, K> rhs)
{
        assert(lhs.size()==rhs.size());
        for(
          typename boost::detail::multi_array::sub_array<TValue,
K>::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<TValue, K> 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

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net