swap_ranges.hpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. // See http://boostorg.github.com/compute for more information.
  9. //---------------------------------------------------------------------------//
  10. #ifndef BOOST_COMPUTE_ALGORITHM_SWAP_RANGES_HPP
  11. #define BOOST_COMPUTE_ALGORITHM_SWAP_RANGES_HPP
  12. #include <boost/static_assert.hpp>
  13. #include <boost/compute/system.hpp>
  14. #include <boost/compute/command_queue.hpp>
  15. #include <boost/compute/algorithm/copy.hpp>
  16. #include <boost/compute/container/vector.hpp>
  17. #include <boost/compute/type_traits/is_device_iterator.hpp>
  18. namespace boost {
  19. namespace compute {
  20. /// Swaps the elements in the range [\p first1, \p last1) with the
  21. /// elements in the range beginning at \p first2.
  22. ///
  23. /// Space complexity: \Omega(distance(\p first1, \p last1))
  24. template<class Iterator1, class Iterator2>
  25. inline Iterator2 swap_ranges(Iterator1 first1,
  26. Iterator1 last1,
  27. Iterator2 first2,
  28. command_queue &queue = system::default_queue())
  29. {
  30. BOOST_STATIC_ASSERT(is_device_iterator<Iterator1>::value);
  31. BOOST_STATIC_ASSERT(is_device_iterator<Iterator2>::value);
  32. typedef typename std::iterator_traits<Iterator1>::value_type value_type;
  33. Iterator2 last2 = first2 + std::distance(first1, last1);
  34. ::boost::compute::vector<value_type> tmp(first1, last1, queue);
  35. ::boost::compute::copy(first2, last2, first1, queue);
  36. ::boost::compute::copy(tmp.begin(), tmp.end(), first2, queue);
  37. return last2;
  38. }
  39. } // end compute namespace
  40. } // end boost namespace
  41. #endif // BOOST_COMPUTE_ALGORITHM_SWAP_RANGES_HPP