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