binary_search.hpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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_ALGORITHM_BINARY_SEARCH_HPP
  11. #define BOOST_COMPUTE_ALGORITHM_BINARY_SEARCH_HPP
  12. #include <boost/static_assert.hpp>
  13. #include <boost/compute/system.hpp>
  14. #include <boost/compute/command_queue.hpp>
  15. #include <boost/compute/algorithm/lower_bound.hpp>
  16. #include <boost/compute/type_traits/is_device_iterator.hpp>
  17. namespace boost {
  18. namespace compute {
  19. /// Returns \c true if \p value is in the sorted range [\p first,
  20. /// \p last).
  21. ///
  22. /// Space complexity: \Omega(1)
  23. template<class InputIterator, class T>
  24. inline bool binary_search(InputIterator first,
  25. InputIterator last,
  26. const T &value,
  27. command_queue &queue = system::default_queue())
  28. {
  29. BOOST_STATIC_ASSERT(is_device_iterator<InputIterator>::value);
  30. InputIterator position = lower_bound(first, last, value, queue);
  31. return position != last && position.read(queue) == value;
  32. }
  33. } // end compute namespace
  34. } // end boost namespace
  35. #endif // BOOST_COMPUTE_ALGORITHM_BINARY_SEARCH_HPP