xref: /openbsd-src/gnu/llvm/libcxx/include/__algorithm/ranges_binary_search.h (revision 4bdff4bed0e3d54e55670334c7d0077db4170f86)
1*4bdff4beSrobert //===----------------------------------------------------------------------===//
2*4bdff4beSrobert //
3*4bdff4beSrobert // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*4bdff4beSrobert // See https://llvm.org/LICENSE.txt for license information.
5*4bdff4beSrobert // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*4bdff4beSrobert //
7*4bdff4beSrobert //===----------------------------------------------------------------------===//
8*4bdff4beSrobert 
9*4bdff4beSrobert #ifndef _LIBCPP___ALGORITHM_RANGES_BINARY_SEARCH_H
10*4bdff4beSrobert #define _LIBCPP___ALGORITHM_RANGES_BINARY_SEARCH_H
11*4bdff4beSrobert 
12*4bdff4beSrobert #include <__algorithm/iterator_operations.h>
13*4bdff4beSrobert #include <__algorithm/lower_bound.h>
14*4bdff4beSrobert #include <__config>
15*4bdff4beSrobert #include <__functional/identity.h>
16*4bdff4beSrobert #include <__functional/invoke.h>
17*4bdff4beSrobert #include <__functional/ranges_operations.h>
18*4bdff4beSrobert #include <__iterator/concepts.h>
19*4bdff4beSrobert #include <__iterator/projected.h>
20*4bdff4beSrobert #include <__ranges/access.h>
21*4bdff4beSrobert #include <__ranges/concepts.h>
22*4bdff4beSrobert 
23*4bdff4beSrobert #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
24*4bdff4beSrobert #  pragma GCC system_header
25*4bdff4beSrobert #endif
26*4bdff4beSrobert 
27*4bdff4beSrobert #if _LIBCPP_STD_VER > 17
28*4bdff4beSrobert 
29*4bdff4beSrobert _LIBCPP_BEGIN_NAMESPACE_STD
30*4bdff4beSrobert 
31*4bdff4beSrobert namespace ranges {
32*4bdff4beSrobert namespace __binary_search {
33*4bdff4beSrobert struct __fn {
34*4bdff4beSrobert   template <forward_iterator _Iter, sentinel_for<_Iter> _Sent, class _Type, class _Proj = identity,
35*4bdff4beSrobert             indirect_strict_weak_order<const _Type*, projected<_Iter, _Proj>> _Comp = ranges::less>
36*4bdff4beSrobert   _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr
operator__fn37*4bdff4beSrobert   bool operator()(_Iter __first, _Sent __last, const _Type& __value, _Comp __comp = {}, _Proj __proj = {}) const {
38*4bdff4beSrobert     auto __ret = std::__lower_bound_impl<_RangeAlgPolicy>(__first, __last, __value, __comp, __proj);
39*4bdff4beSrobert     return __ret != __last && !std::invoke(__comp, __value, std::invoke(__proj, *__ret));
40*4bdff4beSrobert   }
41*4bdff4beSrobert 
42*4bdff4beSrobert   template <forward_range _Range, class _Type, class _Proj = identity,
43*4bdff4beSrobert             indirect_strict_weak_order<const _Type*, projected<iterator_t<_Range>, _Proj>> _Comp = ranges::less>
44*4bdff4beSrobert   _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr
operator__fn45*4bdff4beSrobert   bool operator()(_Range&& __r, const _Type& __value, _Comp __comp = {}, _Proj __proj = {}) const {
46*4bdff4beSrobert     auto __first = ranges::begin(__r);
47*4bdff4beSrobert     auto __last = ranges::end(__r);
48*4bdff4beSrobert     auto __ret = std::__lower_bound_impl<_RangeAlgPolicy>(__first, __last, __value, __comp, __proj);
49*4bdff4beSrobert     return __ret != __last && !std::invoke(__comp, __value, std::invoke(__proj, *__ret));
50*4bdff4beSrobert   }
51*4bdff4beSrobert };
52*4bdff4beSrobert } // namespace __binary_search
53*4bdff4beSrobert 
54*4bdff4beSrobert inline namespace __cpo {
55*4bdff4beSrobert   inline constexpr auto binary_search = __binary_search::__fn{};
56*4bdff4beSrobert } // namespace __cpo
57*4bdff4beSrobert } // namespace ranges
58*4bdff4beSrobert 
59*4bdff4beSrobert _LIBCPP_END_NAMESPACE_STD
60*4bdff4beSrobert 
61*4bdff4beSrobert #endif // _LIBCPP_STD_VER > 17
62*4bdff4beSrobert 
63*4bdff4beSrobert #endif // _LIBCPP___ALGORITHM_RANGES_BINARY_SEARCH_H
64