xref: /freebsd-src/contrib/llvm-project/libcxx/include/__algorithm/lower_bound.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
2fe6060f1SDimitry Andric //
3fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6fe6060f1SDimitry Andric //
7fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
8fe6060f1SDimitry Andric 
9fe6060f1SDimitry Andric #ifndef _LIBCPP___ALGORITHM_LOWER_BOUND_H
10fe6060f1SDimitry Andric #define _LIBCPP___ALGORITHM_LOWER_BOUND_H
11fe6060f1SDimitry Andric 
12fe6060f1SDimitry Andric #include <__algorithm/comp.h>
13fe6060f1SDimitry Andric #include <__algorithm/half_positive.h>
1481ad6265SDimitry Andric #include <__algorithm/iterator_operations.h>
1504eeddc0SDimitry Andric #include <__config>
1681ad6265SDimitry Andric #include <__functional/identity.h>
1781ad6265SDimitry Andric #include <__functional/invoke.h>
1881ad6265SDimitry Andric #include <__iterator/advance.h>
1981ad6265SDimitry Andric #include <__iterator/distance.h>
2081ad6265SDimitry Andric #include <__iterator/iterator_traits.h>
2181ad6265SDimitry Andric #include <__type_traits/is_callable.h>
22753f127fSDimitry Andric #include <__type_traits/remove_reference.h>
23fe6060f1SDimitry Andric 
24fe6060f1SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
25fe6060f1SDimitry Andric #  pragma GCC system_header
26fe6060f1SDimitry Andric #endif
27fe6060f1SDimitry Andric 
28fe6060f1SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
29fe6060f1SDimitry Andric 
30*0fca6ea1SDimitry Andric template <class _AlgPolicy, class _Iter, class _Type, class _Proj, class _Comp>
31*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Iter __lower_bound_bisecting(
32*0fca6ea1SDimitry Andric     _Iter __first,
33*0fca6ea1SDimitry Andric     const _Type& __value,
34*0fca6ea1SDimitry Andric     typename iterator_traits<_Iter>::difference_type __len,
35*0fca6ea1SDimitry Andric     _Comp& __comp,
36*0fca6ea1SDimitry Andric     _Proj& __proj) {
3781ad6265SDimitry Andric   while (__len != 0) {
3881ad6265SDimitry Andric     auto __l2 = std::__half_positive(__len);
3981ad6265SDimitry Andric     _Iter __m = __first;
40753f127fSDimitry Andric     _IterOps<_AlgPolicy>::advance(__m, __l2);
4181ad6265SDimitry Andric     if (std::__invoke(__comp, std::__invoke(__proj, *__m), __value)) {
42fe6060f1SDimitry Andric       __first = ++__m;
43fe6060f1SDimitry Andric       __len -= __l2 + 1;
4481ad6265SDimitry Andric     } else {
45fe6060f1SDimitry Andric       __len = __l2;
46fe6060f1SDimitry Andric     }
4781ad6265SDimitry Andric   }
48fe6060f1SDimitry Andric   return __first;
49fe6060f1SDimitry Andric }
50fe6060f1SDimitry Andric 
51*0fca6ea1SDimitry Andric // One-sided binary search, aka meta binary search, has been in the public domain for decades, and has the general
52*0fca6ea1SDimitry Andric // advantage of being \Omega(1) rather than the classic algorithm's \Omega(log(n)), with the downside of executing at
53*0fca6ea1SDimitry Andric // most 2*log(n) comparisons vs the classic algorithm's exact log(n). There are two scenarios in which it really shines:
54*0fca6ea1SDimitry Andric // the first one is when operating over non-random-access iterators, because the classic algorithm requires knowing the
55*0fca6ea1SDimitry Andric // container's size upfront, which adds \Omega(n) iterator increments to the complexity. The second one is when you're
56*0fca6ea1SDimitry Andric // traversing the container in order, trying to fast-forward to the next value: in that case, the classic algorithm
57*0fca6ea1SDimitry Andric // would yield \Omega(n*log(n)) comparisons and, for non-random-access iterators, \Omega(n^2) iterator increments,
58*0fca6ea1SDimitry Andric // whereas the one-sided version will yield O(n) operations on both counts, with a \Omega(log(n)) bound on the number of
59*0fca6ea1SDimitry Andric // comparisons.
60*0fca6ea1SDimitry Andric template <class _AlgPolicy, class _ForwardIterator, class _Sent, class _Type, class _Proj, class _Comp>
61*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
62*0fca6ea1SDimitry Andric __lower_bound_onesided(_ForwardIterator __first, _Sent __last, const _Type& __value, _Comp& __comp, _Proj& __proj) {
63*0fca6ea1SDimitry Andric   // step = 0, ensuring we can always short-circuit when distance is 1 later on
64*0fca6ea1SDimitry Andric   if (__first == __last || !std::__invoke(__comp, std::__invoke(__proj, *__first), __value))
65*0fca6ea1SDimitry Andric     return __first;
66*0fca6ea1SDimitry Andric 
67*0fca6ea1SDimitry Andric   using _Distance = typename iterator_traits<_ForwardIterator>::difference_type;
68*0fca6ea1SDimitry Andric   for (_Distance __step = 1; __first != __last; __step <<= 1) {
69*0fca6ea1SDimitry Andric     auto __it   = __first;
70*0fca6ea1SDimitry Andric     auto __dist = __step - _IterOps<_AlgPolicy>::__advance_to(__it, __step, __last);
71*0fca6ea1SDimitry Andric     // once we reach the last range where needle can be we must start
72*0fca6ea1SDimitry Andric     // looking inwards, bisecting that range
73*0fca6ea1SDimitry Andric     if (__it == __last || !std::__invoke(__comp, std::__invoke(__proj, *__it), __value)) {
74*0fca6ea1SDimitry Andric       // we've already checked the previous value and it was less, we can save
75*0fca6ea1SDimitry Andric       // one comparison by skipping bisection
76*0fca6ea1SDimitry Andric       if (__dist == 1)
77*0fca6ea1SDimitry Andric         return __it;
78*0fca6ea1SDimitry Andric       return std::__lower_bound_bisecting<_AlgPolicy>(__first, __value, __dist, __comp, __proj);
79*0fca6ea1SDimitry Andric     }
80*0fca6ea1SDimitry Andric     // range not found, move forward!
81*0fca6ea1SDimitry Andric     __first = __it;
82*0fca6ea1SDimitry Andric   }
83*0fca6ea1SDimitry Andric   return __first;
84*0fca6ea1SDimitry Andric }
85*0fca6ea1SDimitry Andric 
86*0fca6ea1SDimitry Andric template <class _AlgPolicy, class _ForwardIterator, class _Sent, class _Type, class _Proj, class _Comp>
87*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
88*0fca6ea1SDimitry Andric __lower_bound(_ForwardIterator __first, _Sent __last, const _Type& __value, _Comp& __comp, _Proj& __proj) {
89*0fca6ea1SDimitry Andric   const auto __dist = _IterOps<_AlgPolicy>::distance(__first, __last);
90*0fca6ea1SDimitry Andric   return std::__lower_bound_bisecting<_AlgPolicy>(__first, __value, __dist, __comp, __proj);
91*0fca6ea1SDimitry Andric }
92*0fca6ea1SDimitry Andric 
93fe6060f1SDimitry Andric template <class _ForwardIterator, class _Tp, class _Compare>
94*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
95cb14a3feSDimitry Andric lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, _Compare __comp) {
96cb14a3feSDimitry Andric   static_assert(__is_callable<_Compare, decltype(*__first), const _Tp&>::value, "The comparator has to be callable");
9781ad6265SDimitry Andric   auto __proj = std::__identity();
9806c3fb27SDimitry Andric   return std::__lower_bound<_ClassicAlgPolicy>(__first, __last, __value, __comp, __proj);
99fe6060f1SDimitry Andric }
100fe6060f1SDimitry Andric 
101fe6060f1SDimitry Andric template <class _ForwardIterator, class _Tp>
102*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
103cb14a3feSDimitry Andric lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) {
10406c3fb27SDimitry Andric   return std::lower_bound(__first, __last, __value, __less<>());
105fe6060f1SDimitry Andric }
106fe6060f1SDimitry Andric 
107fe6060f1SDimitry Andric _LIBCPP_END_NAMESPACE_STD
108fe6060f1SDimitry Andric 
109fe6060f1SDimitry Andric #endif // _LIBCPP___ALGORITHM_LOWER_BOUND_H
110