]>
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_INPLACE_MERGE_HPP | |
12 | #define BOOST_COMPUTE_ALGORITHM_INPLACE_MERGE_HPP | |
13 | ||
14 | #include <iterator> | |
15 | ||
92f5a8d4 TL |
16 | #include <boost/static_assert.hpp> |
17 | ||
7c673cae FG |
18 | #include <boost/compute/system.hpp> |
19 | #include <boost/compute/command_queue.hpp> | |
20 | #include <boost/compute/algorithm/merge.hpp> | |
21 | #include <boost/compute/container/vector.hpp> | |
92f5a8d4 | 22 | #include <boost/compute/type_traits/is_device_iterator.hpp> |
7c673cae FG |
23 | |
24 | namespace boost { | |
25 | namespace compute { | |
26 | ||
27 | /// Merges the sorted values in the range [\p first, \p middle) with | |
28 | /// the sorted values in the range [\p middle, \p last) in-place. | |
b32b8144 FG |
29 | /// |
30 | /// Space complexity: \Omega(n) | |
7c673cae FG |
31 | template<class Iterator> |
32 | inline void inplace_merge(Iterator first, | |
33 | Iterator middle, | |
34 | Iterator last, | |
35 | command_queue &queue = system::default_queue()) | |
36 | { | |
92f5a8d4 | 37 | BOOST_STATIC_ASSERT(is_device_iterator<Iterator>::value); |
7c673cae FG |
38 | BOOST_ASSERT(first < middle && middle < last); |
39 | ||
40 | typedef typename std::iterator_traits<Iterator>::value_type T; | |
41 | ||
42 | const context &context = queue.get_context(); | |
43 | ||
44 | ptrdiff_t left_size = std::distance(first, middle); | |
45 | ptrdiff_t right_size = std::distance(middle, last); | |
46 | ||
47 | vector<T> left(left_size, context); | |
48 | vector<T> right(right_size, context); | |
49 | ||
50 | copy(first, middle, left.begin(), queue); | |
51 | copy(middle, last, right.begin(), queue); | |
52 | ||
53 | ::boost::compute::merge( | |
54 | left.begin(), | |
55 | left.end(), | |
56 | right.begin(), | |
57 | right.end(), | |
58 | first, | |
59 | queue | |
60 | ); | |
61 | } | |
62 | ||
63 | } // end compute namespace | |
64 | } // end boost namespace | |
65 | ||
66 | #endif // BOOST_COMPUTE_ALGORITHM_INPLACE_MERGE_HPP |