1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
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
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
11 #ifndef BOOST_COMPUTE_ALGORITHM_INPLACE_MERGE_HPP
12 #define BOOST_COMPUTE_ALGORITHM_INPLACE_MERGE_HPP
16 #include <boost/static_assert.hpp>
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>
22 #include <boost/compute/type_traits/is_device_iterator.hpp>
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.
30 /// Space complexity: \Omega(n)
31 template<class Iterator>
32 inline void inplace_merge(Iterator first,
35 command_queue &queue = system::default_queue())
37 BOOST_STATIC_ASSERT(is_device_iterator<Iterator>::value);
38 BOOST_ASSERT(first < middle && middle < last);
40 typedef typename std::iterator_traits<Iterator>::value_type T;
42 const context &context = queue.get_context();
44 ptrdiff_t left_size = std::distance(first, middle);
45 ptrdiff_t right_size = std::distance(middle, last);
47 vector<T> left(left_size, context);
48 vector<T> right(right_size, context);
50 copy(first, middle, left.begin(), queue);
51 copy(middle, last, right.begin(), queue);
53 ::boost::compute::merge(
63 } // end compute namespace
64 } // end boost namespace
66 #endif // BOOST_COMPUTE_ALGORITHM_INPLACE_MERGE_HPP