]>
Commit | Line | Data |
---|---|---|
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 | /// | |
36 | /// \see is_partitioned() and partition() | |
37 | /// | |
38 | template<class Iterator, class UnaryPredicate> | |
39 | inline Iterator stable_partition(Iterator first, | |
40 | Iterator last, | |
41 | UnaryPredicate predicate, | |
42 | command_queue &queue = system::default_queue()) | |
43 | { | |
44 | typedef typename std::iterator_traits<Iterator>::value_type value_type; | |
45 | ||
46 | // make temporary copy of the input | |
47 | ::boost::compute::vector<value_type> tmp(first, last, queue); | |
48 | ||
49 | // copy true values | |
50 | Iterator last_true = | |
51 | ::boost::compute::copy_if(tmp.begin(), | |
52 | tmp.end(), | |
53 | first, | |
54 | predicate, | |
55 | queue); | |
56 | ||
57 | // copy false values | |
58 | Iterator last_false = | |
59 | ::boost::compute::copy_if(tmp.begin(), | |
60 | tmp.end(), | |
61 | last_true, | |
62 | not1(predicate), | |
63 | queue); | |
64 | ||
65 | // return iterator pointing to the last true value | |
66 | return last_true; | |
67 | } | |
68 | ||
69 | } // end compute namespace | |
70 | } // end boost namespace | |
71 | ||
72 | #endif // BOOST_COMPUTE_ALGORITHM_STABLE_PARTITION_HPP |