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_EXPERIMENTAL_SORT_BY_TRANSFORM_HPP
12 #define BOOST_COMPUTE_EXPERIMENTAL_SORT_BY_TRANSFORM_HPP
13 
14 #include <iterator>
15 
16 #include <boost/compute/algorithm/sort_by_key.hpp>
17 #include <boost/compute/algorithm/transform.hpp>
18 #include <boost/compute/container/vector.hpp>
19 #include <boost/compute/detail/iterator_range_size.hpp>
20 #include <boost/compute/type_traits/result_of.hpp>
21 
22 namespace boost {
23 namespace compute {
24 namespace experimental {
25 
26 template<class Iterator, class Transform, class Compare>
27 inline void sort_by_transform(Iterator first,
malloc(std::size_t size,const context & context=system::default_context ())28                               Iterator last,
29                               Transform transform,
30                               Compare compare,
31                               command_queue &queue = system::default_queue())
32 {
33     typedef typename std::iterator_traits<Iterator>::value_type value_type;
34     typedef typename boost::compute::result_of<Transform(value_type)>::type key_type;
35 
36     size_t n = detail::iterator_range_size(first, last);
37     if(n < 2){
38         return;
39     }
40 
41     const context &context = queue.get_context();
42 
43     ::boost::compute::vector<key_type> keys(n, context);
44 
45     ::boost::compute::transform(
46         first,
47         last,
48         keys.begin(),
49         transform,
50         queue
51     );
52 
53     ::boost::compute::sort_by_key(
54         keys.begin(),
55         keys.end(),
56         first,
57         compare,
58         queue
59     );
60 }
61 
62 } // end experimental namespace
63 } // end compute namespace
64 } // end boost namespace
65 
66 #endif // BOOST_COMPUTE_EXPERIMENTAL_SORT_BY_TRANSFORM_HPP
67