nth_element.hpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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_NTH_ELEMENT_HPP
  11. #define BOOST_COMPUTE_ALGORITHM_NTH_ELEMENT_HPP
  12. #include <boost/static_assert.hpp>
  13. #include <boost/compute/command_queue.hpp>
  14. #include <boost/compute/algorithm/fill_n.hpp>
  15. #include <boost/compute/algorithm/find.hpp>
  16. #include <boost/compute/algorithm/partition.hpp>
  17. #include <boost/compute/algorithm/sort.hpp>
  18. #include <boost/compute/functional/bind.hpp>
  19. #include <boost/compute/type_traits/is_device_iterator.hpp>
  20. namespace boost {
  21. namespace compute {
  22. /// Rearranges the elements in the range [\p first, \p last) such that
  23. /// the \p nth element would be in that position in a sorted sequence.
  24. ///
  25. /// Space complexity: \Omega(3n)
  26. template<class Iterator, class Compare>
  27. inline void nth_element(Iterator first,
  28. Iterator nth,
  29. Iterator last,
  30. Compare compare,
  31. command_queue &queue = system::default_queue())
  32. {
  33. BOOST_STATIC_ASSERT(is_device_iterator<Iterator>::value);
  34. if(nth == last) return;
  35. typedef typename std::iterator_traits<Iterator>::value_type value_type;
  36. while(1)
  37. {
  38. value_type value = nth.read(queue);
  39. using boost::compute::placeholders::_1;
  40. Iterator new_nth = partition(
  41. first, last, ::boost::compute::bind(compare, _1, value), queue
  42. );
  43. Iterator old_nth = find(new_nth, last, value, queue);
  44. value_type new_value = new_nth.read(queue);
  45. fill_n(new_nth, 1, value, queue);
  46. fill_n(old_nth, 1, new_value, queue);
  47. new_value = nth.read(queue);
  48. if(value == new_value) break;
  49. if(std::distance(first, nth) < std::distance(first, new_nth))
  50. {
  51. last = new_nth;
  52. }
  53. else
  54. {
  55. first = new_nth;
  56. }
  57. }
  58. }
  59. /// \overload
  60. template<class Iterator>
  61. inline void nth_element(Iterator first,
  62. Iterator nth,
  63. Iterator last,
  64. command_queue &queue = system::default_queue())
  65. {
  66. BOOST_STATIC_ASSERT(is_device_iterator<Iterator>::value);
  67. if(nth == last) return;
  68. typedef typename std::iterator_traits<Iterator>::value_type value_type;
  69. less<value_type> less_than;
  70. return nth_element(first, nth, last, less_than, queue);
  71. }
  72. } // end compute namespace
  73. } // end boost namespace
  74. #endif // BOOST_COMPUTE_ALGORITHM_NTH_ELEMENT_HPP