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