sort_by_transform.hpp 1.9 KB

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