rotate.hpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. /// \see rotate_copy()
  21. template<class InputIterator>
  22. inline void rotate(InputIterator first,
  23. InputIterator n_first,
  24. InputIterator last,
  25. command_queue &queue = system::default_queue())
  26. {
  27. //Handle trivial cases
  28. if (n_first==first || n_first==last)
  29. {
  30. return;
  31. }
  32. //Handle others
  33. typedef typename std::iterator_traits<InputIterator>::value_type T;
  34. size_t count = detail::iterator_range_size(first, n_first);
  35. size_t count2 = detail::iterator_range_size(first, last);
  36. const context &context = queue.get_context();
  37. vector<T> temp(count2, context);
  38. ::boost::compute::copy(first, last, temp.begin(), queue);
  39. ::boost::compute::copy(temp.begin()+count, temp.end(), first, queue);
  40. ::boost::compute::copy(temp.begin(), temp.begin()+count, last-count, queue);
  41. }
  42. } //end compute namespace
  43. } //end boost namespace
  44. #endif // BOOST_COMPUTE_ALGORITHM_ROTATE_HPP