Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:18

0001 //===----------------------------------------------------------------------===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef _LIBCPP___CXX03___ALGORITHM_LOWER_BOUND_H
0010 #define _LIBCPP___CXX03___ALGORITHM_LOWER_BOUND_H
0011 
0012 #include <__cxx03/__algorithm/comp.h>
0013 #include <__cxx03/__algorithm/half_positive.h>
0014 #include <__cxx03/__algorithm/iterator_operations.h>
0015 #include <__cxx03/__config>
0016 #include <__cxx03/__functional/identity.h>
0017 #include <__cxx03/__functional/invoke.h>
0018 #include <__cxx03/__iterator/advance.h>
0019 #include <__cxx03/__iterator/distance.h>
0020 #include <__cxx03/__iterator/iterator_traits.h>
0021 #include <__cxx03/__type_traits/is_callable.h>
0022 #include <__cxx03/__type_traits/remove_reference.h>
0023 
0024 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0025 #  pragma GCC system_header
0026 #endif
0027 
0028 _LIBCPP_BEGIN_NAMESPACE_STD
0029 
0030 template <class _AlgPolicy, class _Iter, class _Type, class _Proj, class _Comp>
0031 _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Iter __lower_bound_bisecting(
0032     _Iter __first,
0033     const _Type& __value,
0034     typename iterator_traits<_Iter>::difference_type __len,
0035     _Comp& __comp,
0036     _Proj& __proj) {
0037   while (__len != 0) {
0038     auto __l2 = std::__half_positive(__len);
0039     _Iter __m = __first;
0040     _IterOps<_AlgPolicy>::advance(__m, __l2);
0041     if (std::__invoke(__comp, std::__invoke(__proj, *__m), __value)) {
0042       __first = ++__m;
0043       __len -= __l2 + 1;
0044     } else {
0045       __len = __l2;
0046     }
0047   }
0048   return __first;
0049 }
0050 
0051 // One-sided binary search, aka meta binary search, has been in the public domain for decades, and has the general
0052 // advantage of being \Omega(1) rather than the classic algorithm's \Omega(log(n)), with the downside of executing at
0053 // most 2*log(n) comparisons vs the classic algorithm's exact log(n). There are two scenarios in which it really shines:
0054 // the first one is when operating over non-random-access iterators, because the classic algorithm requires knowing the
0055 // container's size upfront, which adds \Omega(n) iterator increments to the complexity. The second one is when you're
0056 // traversing the container in order, trying to fast-forward to the next value: in that case, the classic algorithm
0057 // would yield \Omega(n*log(n)) comparisons and, for non-random-access iterators, \Omega(n^2) iterator increments,
0058 // whereas the one-sided version will yield O(n) operations on both counts, with a \Omega(log(n)) bound on the number of
0059 // comparisons.
0060 template <class _AlgPolicy, class _ForwardIterator, class _Sent, class _Type, class _Proj, class _Comp>
0061 _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
0062 __lower_bound_onesided(_ForwardIterator __first, _Sent __last, const _Type& __value, _Comp& __comp, _Proj& __proj) {
0063   // step = 0, ensuring we can always short-circuit when distance is 1 later on
0064   if (__first == __last || !std::__invoke(__comp, std::__invoke(__proj, *__first), __value))
0065     return __first;
0066 
0067   using _Distance = typename iterator_traits<_ForwardIterator>::difference_type;
0068   for (_Distance __step = 1; __first != __last; __step <<= 1) {
0069     auto __it   = __first;
0070     auto __dist = __step - _IterOps<_AlgPolicy>::__advance_to(__it, __step, __last);
0071     // once we reach the last range where needle can be we must start
0072     // looking inwards, bisecting that range
0073     if (__it == __last || !std::__invoke(__comp, std::__invoke(__proj, *__it), __value)) {
0074       // we've already checked the previous value and it was less, we can save
0075       // one comparison by skipping bisection
0076       if (__dist == 1)
0077         return __it;
0078       return std::__lower_bound_bisecting<_AlgPolicy>(__first, __value, __dist, __comp, __proj);
0079     }
0080     // range not found, move forward!
0081     __first = __it;
0082   }
0083   return __first;
0084 }
0085 
0086 template <class _AlgPolicy, class _ForwardIterator, class _Sent, class _Type, class _Proj, class _Comp>
0087 _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
0088 __lower_bound(_ForwardIterator __first, _Sent __last, const _Type& __value, _Comp& __comp, _Proj& __proj) {
0089   const auto __dist = _IterOps<_AlgPolicy>::distance(__first, __last);
0090   return std::__lower_bound_bisecting<_AlgPolicy>(__first, __value, __dist, __comp, __proj);
0091 }
0092 
0093 template <class _ForwardIterator, class _Tp, class _Compare>
0094 _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
0095 lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, _Compare __comp) {
0096   static_assert(__is_callable<_Compare, decltype(*__first), const _Tp&>::value, "The comparator has to be callable");
0097   auto __proj = std::__identity();
0098   return std::__lower_bound<_ClassicAlgPolicy>(__first, __last, __value, __comp, __proj);
0099 }
0100 
0101 template <class _ForwardIterator, class _Tp>
0102 _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
0103 lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) {
0104   return std::lower_bound(__first, __last, __value, __less<>());
0105 }
0106 
0107 _LIBCPP_END_NAMESPACE_STD
0108 
0109 #endif // _LIBCPP___CXX03___ALGORITHM_LOWER_BOUND_H