binary_search.hpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright Neil Groves 2009. Use, modification and
  2. // distribution is subject to the Boost Software License, Version
  3. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. //
  6. //
  7. // For more information, see http://www.boost.org/libs/range/
  8. //
  9. #ifndef BOOST_RANGE_ALGORITHM_BINARY_SEARCH_HPP_INCLUDED
  10. #define BOOST_RANGE_ALGORITHM_BINARY_SEARCH_HPP_INCLUDED
  11. #include <boost/concept_check.hpp>
  12. #include <boost/range/begin.hpp>
  13. #include <boost/range/end.hpp>
  14. #include <boost/range/concepts.hpp>
  15. #include <algorithm>
  16. namespace boost
  17. {
  18. namespace range
  19. {
  20. /// \brief template function binary_search
  21. ///
  22. /// range-based version of the binary_search std algorithm
  23. ///
  24. /// \pre ForwardRange is a model of the ForwardRangeConcept
  25. /// \pre BinaryPredicate is a model of the BinaryPredicateConcept
  26. template<class ForwardRange, class Value>
  27. inline bool binary_search(const ForwardRange& rng, const Value& val)
  28. {
  29. BOOST_RANGE_CONCEPT_ASSERT(( ForwardRangeConcept<const ForwardRange> ));
  30. return std::binary_search(boost::begin(rng), boost::end(rng), val);
  31. }
  32. /// \overload
  33. template<class ForwardRange, class Value, class BinaryPredicate>
  34. inline bool binary_search(const ForwardRange& rng, const Value& val,
  35. BinaryPredicate pred)
  36. {
  37. BOOST_RANGE_CONCEPT_ASSERT(( ForwardRangeConcept<const ForwardRange> ));
  38. return std::binary_search(boost::begin(rng), boost::end(rng), val, pred);
  39. }
  40. } // namespace range
  41. using range::binary_search;
  42. } // namespace boost
  43. #endif // include guard