1134723edSLouis Dionne //===----------------------------------------------------------------------===// 2134723edSLouis Dionne // 3134723edSLouis Dionne // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4134723edSLouis Dionne // See https://llvm.org/LICENSE.txt for license information. 5134723edSLouis Dionne // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6134723edSLouis Dionne // 7134723edSLouis Dionne //===----------------------------------------------------------------------===// 8134723edSLouis Dionne 9134723edSLouis Dionne #ifndef _LIBCPP___ALGORITHM_LOWER_BOUND_H 10134723edSLouis Dionne #define _LIBCPP___ALGORITHM_LOWER_BOUND_H 11134723edSLouis Dionne 12134723edSLouis Dionne #include <__algorithm/comp.h> 13134723edSLouis Dionne #include <__algorithm/half_positive.h> 14988682a3SNikolas Klauser #include <__algorithm/iterator_operations.h> 154d81a46fSArthur O'Dwyer #include <__config> 1681715861SNikolas Klauser #include <__functional/identity.h> 173cd4531bSNikolas Klauser #include <__iterator/advance.h> 183cd4531bSNikolas Klauser #include <__iterator/distance.h> 193cd4531bSNikolas Klauser #include <__iterator/iterator_traits.h> 20*09e3a360SLouis Dionne #include <__type_traits/invoke.h> 2181715861SNikolas Klauser #include <__type_traits/is_callable.h> 22134723edSLouis Dionne 23134723edSLouis Dionne #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 24134723edSLouis Dionne # pragma GCC system_header 25134723edSLouis Dionne #endif 26134723edSLouis Dionne 27134723edSLouis Dionne _LIBCPP_BEGIN_NAMESPACE_STD 28134723edSLouis Dionne 29a0662176SIuri Chaer template <class _AlgPolicy, class _Iter, class _Type, class _Proj, class _Comp> 3017e0686aSNikolas Klauser [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Iter __lower_bound_bisecting( 31a0662176SIuri Chaer _Iter __first, 32a0662176SIuri Chaer const _Type& __value, 33a0662176SIuri Chaer typename iterator_traits<_Iter>::difference_type __len, 34a0662176SIuri Chaer _Comp& __comp, 35a0662176SIuri Chaer _Proj& __proj) { 3681715861SNikolas Klauser while (__len != 0) { 3781715861SNikolas Klauser auto __l2 = std::__half_positive(__len); 3881715861SNikolas Klauser _Iter __m = __first; 39295b951eSKonstantin Varlamov _IterOps<_AlgPolicy>::advance(__m, __l2); 4081715861SNikolas Klauser if (std::__invoke(__comp, std::__invoke(__proj, *__m), __value)) { 41134723edSLouis Dionne __first = ++__m; 42134723edSLouis Dionne __len -= __l2 + 1; 4381715861SNikolas Klauser } else { 44134723edSLouis Dionne __len = __l2; 45134723edSLouis Dionne } 4681715861SNikolas Klauser } 47134723edSLouis Dionne return __first; 48134723edSLouis Dionne } 49134723edSLouis Dionne 50a0662176SIuri Chaer // One-sided binary search, aka meta binary search, has been in the public domain for decades, and has the general 51a0662176SIuri Chaer // advantage of being \Omega(1) rather than the classic algorithm's \Omega(log(n)), with the downside of executing at 52a0662176SIuri Chaer // most 2*log(n) comparisons vs the classic algorithm's exact log(n). There are two scenarios in which it really shines: 53a0662176SIuri Chaer // the first one is when operating over non-random-access iterators, because the classic algorithm requires knowing the 54a0662176SIuri Chaer // container's size upfront, which adds \Omega(n) iterator increments to the complexity. The second one is when you're 55a0662176SIuri Chaer // traversing the container in order, trying to fast-forward to the next value: in that case, the classic algorithm 56a0662176SIuri Chaer // would yield \Omega(n*log(n)) comparisons and, for non-random-access iterators, \Omega(n^2) iterator increments, 57a0662176SIuri Chaer // whereas the one-sided version will yield O(n) operations on both counts, with a \Omega(log(n)) bound on the number of 58a0662176SIuri Chaer // comparisons. 59a0662176SIuri Chaer template <class _AlgPolicy, class _ForwardIterator, class _Sent, class _Type, class _Proj, class _Comp> 6017e0686aSNikolas Klauser [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator 61a0662176SIuri Chaer __lower_bound_onesided(_ForwardIterator __first, _Sent __last, const _Type& __value, _Comp& __comp, _Proj& __proj) { 62a0662176SIuri Chaer // step = 0, ensuring we can always short-circuit when distance is 1 later on 63a0662176SIuri Chaer if (__first == __last || !std::__invoke(__comp, std::__invoke(__proj, *__first), __value)) 64a0662176SIuri Chaer return __first; 65a0662176SIuri Chaer 66a0662176SIuri Chaer using _Distance = typename iterator_traits<_ForwardIterator>::difference_type; 67a0662176SIuri Chaer for (_Distance __step = 1; __first != __last; __step <<= 1) { 68a0662176SIuri Chaer auto __it = __first; 69a0662176SIuri Chaer auto __dist = __step - _IterOps<_AlgPolicy>::__advance_to(__it, __step, __last); 70a0662176SIuri Chaer // once we reach the last range where needle can be we must start 71a0662176SIuri Chaer // looking inwards, bisecting that range 72a0662176SIuri Chaer if (__it == __last || !std::__invoke(__comp, std::__invoke(__proj, *__it), __value)) { 73a0662176SIuri Chaer // we've already checked the previous value and it was less, we can save 74a0662176SIuri Chaer // one comparison by skipping bisection 75a0662176SIuri Chaer if (__dist == 1) 76a0662176SIuri Chaer return __it; 77a0662176SIuri Chaer return std::__lower_bound_bisecting<_AlgPolicy>(__first, __value, __dist, __comp, __proj); 78a0662176SIuri Chaer } 79a0662176SIuri Chaer // range not found, move forward! 80a0662176SIuri Chaer __first = __it; 81a0662176SIuri Chaer } 82a0662176SIuri Chaer return __first; 83a0662176SIuri Chaer } 84a0662176SIuri Chaer 85a0662176SIuri Chaer template <class _AlgPolicy, class _ForwardIterator, class _Sent, class _Type, class _Proj, class _Comp> 8617e0686aSNikolas Klauser [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator 87a0662176SIuri Chaer __lower_bound(_ForwardIterator __first, _Sent __last, const _Type& __value, _Comp& __comp, _Proj& __proj) { 88a0662176SIuri Chaer const auto __dist = _IterOps<_AlgPolicy>::distance(__first, __last); 89a0662176SIuri Chaer return std::__lower_bound_bisecting<_AlgPolicy>(__first, __value, __dist, __comp, __proj); 90a0662176SIuri Chaer } 91a0662176SIuri Chaer 92134723edSLouis Dionne template <class _ForwardIterator, class _Tp, class _Compare> 9317e0686aSNikolas Klauser [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator 949783f28cSLouis Dionne lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, _Compare __comp) { 9525783158SLouis Dionne static_assert(__is_callable<_Compare&, decltype(*__first), const _Tp&>::value, "The comparator has to be callable"); 9681715861SNikolas Klauser auto __proj = std::__identity(); 97d8655fb3SLouis Dionne return std::__lower_bound<_ClassicAlgPolicy>(__first, __last, __value, __comp, __proj); 98134723edSLouis Dionne } 99134723edSLouis Dionne 100134723edSLouis Dionne template <class _ForwardIterator, class _Tp> 10117e0686aSNikolas Klauser [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator 1029783f28cSLouis Dionne lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) { 10388632e48SNikolas Klauser return std::lower_bound(__first, __last, __value, __less<>()); 104134723edSLouis Dionne } 105134723edSLouis Dionne 106134723edSLouis Dionne _LIBCPP_END_NAMESPACE_STD 107134723edSLouis Dionne 108134723edSLouis Dionne #endif // _LIBCPP___ALGORITHM_LOWER_BOUND_H 109