]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | ||
11 | #ifndef BOOST_COMPUTE_ALGORITHM_STABLE_PARTITION_HPP | |
12 | #define BOOST_COMPUTE_ALGORITHM_STABLE_PARTITION_HPP | |
13 | ||
14 | #include <boost/compute/system.hpp> | |
15 | #include <boost/compute/context.hpp> | |
16 | #include <boost/compute/functional.hpp> | |
17 | #include <boost/compute/command_queue.hpp> | |
18 | #include <boost/compute/algorithm/copy_if.hpp> | |
19 | #include <boost/compute/container/vector.hpp> | |
20 | ||
21 | namespace boost { | |
22 | namespace compute { | |
23 | ||
24 | /// | |
25 | /// \brief Partitioning algorithm | |
26 | /// | |
27 | /// Partitions the elements in the range [\p first, \p last) according to | |
28 | /// \p predicate. The order of the elements is preserved. | |
29 | /// \return Iterator pointing to end of true values | |
30 | /// | |
31 | /// \param first Iterator pointing to start of range | |
32 | /// \param last Iterator pointing to end of range | |
33 | /// \param predicate Unary predicate to be applied on each element | |
34 | /// \param queue Queue on which to execute | |
35 | /// | |
b32b8144 FG |
36 | /// Space complexity: \Omega(3n) |
37 | /// | |
7c673cae FG |
38 | /// \see is_partitioned() and partition() |
39 | /// | |
40 | template<class Iterator, class UnaryPredicate> | |
41 | inline Iterator stable_partition(Iterator first, | |
42 | Iterator last, | |
43 | UnaryPredicate predicate, | |
44 | command_queue &queue = system::default_queue()) | |
45 | { | |
46 | typedef typename std::iterator_traits<Iterator>::value_type value_type; | |
47 | ||
48 | // make temporary copy of the input | |
49 | ::boost::compute::vector<value_type> tmp(first, last, queue); | |
50 | ||
51 | // copy true values | |
52 | Iterator last_true = | |
53 | ::boost::compute::copy_if(tmp.begin(), | |
54 | tmp.end(), | |
55 | first, | |
56 | predicate, | |
57 | queue); | |
58 | ||
59 | // copy false values | |
60 | Iterator last_false = | |
61 | ::boost::compute::copy_if(tmp.begin(), | |
62 | tmp.end(), | |
63 | last_true, | |
64 | not1(predicate), | |
65 | queue); | |
66 | ||
67 | // return iterator pointing to the last true value | |
68 | return last_true; | |
69 | } | |
70 | ||
71 | } // end compute namespace | |
72 | } // end boost namespace | |
73 | ||
74 | #endif // BOOST_COMPUTE_ALGORITHM_STABLE_PARTITION_HPP |