rotate.hpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2014 Roshan <thisisroshansmail@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_ROTATE_HPP
  11. #define BOOST_COMPUTE_ALGORITHM_ROTATE_HPP
  12. #include <boost/compute/system.hpp>
  13. #include <boost/compute/algorithm/copy.hpp>
  14. #include <boost/compute/container/vector.hpp>
  15. namespace boost {
  16. namespace compute {
  17. /// Performs left rotation such that element at \p n_first comes to the
  18. /// beginning.
  19. ///
  20. /// Space complexity: \Omega(distance(\p first, \p last))
  21. ///
  22. /// \see rotate_copy()
  23. template<class InputIterator>
  24. inline void rotate(InputIterator first,
  25. InputIterator n_first,
  26. InputIterator last,
  27. command_queue &queue = system::default_queue())
  28. {
  29. //Handle trivial cases
  30. if (n_first==first || n_first==last)
  31. {
  32. return;
  33. }
  34. //Handle others
  35. typedef typename std::iterator_traits<InputIterator>::value_type T;
  36. size_t count = detail::iterator_range_size(first, n_first);
  37. size_t count2 = detail::iterator_range_size(first, last);
  38. const context &context = queue.get_context();
  39. vector<T> temp(count2, context);
  40. ::boost::compute::copy(first, last, temp.begin(), queue);
  41. ::boost::compute::copy(temp.begin()+count, temp.end(), first, queue);
  42. ::boost::compute::copy(temp.begin(), temp.begin()+count, last-count, queue);
  43. }
  44. } //end compute namespace
  45. } //end boost namespace
  46. #endif // BOOST_COMPUTE_ALGORITHM_ROTATE_HPP