is_sorted.hpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013-2014 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_ALGORITHM_IS_SORTED_HPP
  11. #define BOOST_COMPUTE_ALGORITHM_IS_SORTED_HPP
  12. #include <boost/static_assert.hpp>
  13. #include <boost/compute/command_queue.hpp>
  14. #include <boost/compute/system.hpp>
  15. #include <boost/compute/functional/bind.hpp>
  16. #include <boost/compute/functional/operator.hpp>
  17. #include <boost/compute/algorithm/adjacent_find.hpp>
  18. #include <boost/compute/type_traits/is_device_iterator.hpp>
  19. namespace boost {
  20. namespace compute {
  21. /// Returns \c true if the values in the range [\p first, \p last)
  22. /// are in sorted order.
  23. ///
  24. /// \param first first element in the range to check
  25. /// \param last last element in the range to check
  26. /// \param compare comparison function (by default \c less)
  27. /// \param queue command queue to perform the operation
  28. ///
  29. /// \return \c true if the range [\p first, \p last) is sorted
  30. ///
  31. /// Space complexity: \Omega(1)
  32. ///
  33. /// \see sort()
  34. template<class InputIterator, class Compare>
  35. inline bool is_sorted(InputIterator first,
  36. InputIterator last,
  37. Compare compare,
  38. command_queue &queue = system::default_queue())
  39. {
  40. BOOST_STATIC_ASSERT(is_device_iterator<InputIterator>::value);
  41. using ::boost::compute::placeholders::_1;
  42. using ::boost::compute::placeholders::_2;
  43. return ::boost::compute::adjacent_find(
  44. first, last, ::boost::compute::bind(compare, _2, _1), queue
  45. ) == last;
  46. }
  47. /// \overload
  48. template<class InputIterator>
  49. inline bool is_sorted(InputIterator first,
  50. InputIterator last,
  51. command_queue &queue = system::default_queue())
  52. {
  53. BOOST_STATIC_ASSERT(is_device_iterator<InputIterator>::value);
  54. typedef typename std::iterator_traits<InputIterator>::value_type value_type;
  55. return ::boost::compute::is_sorted(
  56. first, last, ::boost::compute::less<value_type>(), queue
  57. );
  58. }
  59. } // end compute namespace
  60. } // end boost namespace
  61. #endif // BOOST_COMPUTE_ALGORITHM_IS_SORTED_HPP