1 //===----------------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef _LIBCPP___ALGORITHM_BINARY_SEARCH_H 10 #define _LIBCPP___ALGORITHM_BINARY_SEARCH_H 11 12 #include <__config> 13 #include <__algorithm/lower_bound.h> 14 #include <__algorithm/comp_ref_type.h> 15 #include <__iterator/iterator_traits.h> 16 17 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 18 #pragma GCC system_header 19 #endif 20 21 _LIBCPP_PUSH_MACROS 22 #include <__undef_macros> 23 24 _LIBCPP_BEGIN_NAMESPACE_STD 25 26 template <class _Compare, class _ForwardIterator, class _Tp> 27 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 28 bool 29 __binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp) 30 { 31 __first = _VSTD::__lower_bound<_Compare>(__first, __last, __value_, __comp); 32 return __first != __last && !__comp(__value_, *__first); 33 } 34 35 template <class _ForwardIterator, class _Tp, class _Compare> 36 _LIBCPP_NODISCARD_EXT inline 37 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 38 bool 39 binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp) 40 { 41 typedef typename __comp_ref_type<_Compare>::type _Comp_ref; 42 return _VSTD::__binary_search<_Comp_ref>(__first, __last, __value_, __comp); 43 } 44 45 template <class _ForwardIterator, class _Tp> 46 _LIBCPP_NODISCARD_EXT inline 47 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 48 bool 49 binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_) 50 { 51 return _VSTD::binary_search(__first, __last, __value_, 52 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>()); 53 } 54 55 56 _LIBCPP_END_NAMESPACE_STD 57 58 _LIBCPP_POP_MACROS 59 60 #endif // _LIBCPP___ALGORITHM_BINARY_SEARCH_H 61