File indexing completed on 2026-05-03 08:13:08
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef _LIBCPP___ALGORITHM_LOWER_BOUND_H
0010 #define _LIBCPP___ALGORITHM_LOWER_BOUND_H
0011
0012 #include <__algorithm/comp.h>
0013 #include <__algorithm/half_positive.h>
0014 #include <__algorithm/iterator_operations.h>
0015 #include <__config>
0016 #include <__functional/identity.h>
0017 #include <__iterator/advance.h>
0018 #include <__iterator/distance.h>
0019 #include <__iterator/iterator_traits.h>
0020 #include <__type_traits/invoke.h>
0021 #include <__type_traits/is_callable.h>
0022
0023 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0024 # pragma GCC system_header
0025 #endif
0026
0027 _LIBCPP_BEGIN_NAMESPACE_STD
0028
0029 template <class _AlgPolicy, class _Iter, class _Type, class _Proj, class _Comp>
0030 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Iter __lower_bound_bisecting(
0031 _Iter __first,
0032 const _Type& __value,
0033 typename iterator_traits<_Iter>::difference_type __len,
0034 _Comp& __comp,
0035 _Proj& __proj) {
0036 while (__len != 0) {
0037 auto __l2 = std::__half_positive(__len);
0038 _Iter __m = __first;
0039 _IterOps<_AlgPolicy>::advance(__m, __l2);
0040 if (std::__invoke(__comp, std::__invoke(__proj, *__m), __value)) {
0041 __first = ++__m;
0042 __len -= __l2 + 1;
0043 } else {
0044 __len = __l2;
0045 }
0046 }
0047 return __first;
0048 }
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059 template <class _AlgPolicy, class _ForwardIterator, class _Sent, class _Type, class _Proj, class _Comp>
0060 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
0061 __lower_bound_onesided(_ForwardIterator __first, _Sent __last, const _Type& __value, _Comp& __comp, _Proj& __proj) {
0062
0063 if (__first == __last || !std::__invoke(__comp, std::__invoke(__proj, *__first), __value))
0064 return __first;
0065
0066 using _Distance = typename iterator_traits<_ForwardIterator>::difference_type;
0067 for (_Distance __step = 1; __first != __last; __step <<= 1) {
0068 auto __it = __first;
0069 auto __dist = __step - _IterOps<_AlgPolicy>::__advance_to(__it, __step, __last);
0070
0071
0072 if (__it == __last || !std::__invoke(__comp, std::__invoke(__proj, *__it), __value)) {
0073
0074
0075 if (__dist == 1)
0076 return __it;
0077 return std::__lower_bound_bisecting<_AlgPolicy>(__first, __value, __dist, __comp, __proj);
0078 }
0079
0080 __first = __it;
0081 }
0082 return __first;
0083 }
0084
0085 template <class _AlgPolicy, class _ForwardIterator, class _Sent, class _Type, class _Proj, class _Comp>
0086 [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
0087 __lower_bound(_ForwardIterator __first, _Sent __last, const _Type& __value, _Comp& __comp, _Proj& __proj) {
0088 const auto __dist = _IterOps<_AlgPolicy>::distance(__first, __last);
0089 return std::__lower_bound_bisecting<_AlgPolicy>(__first, __value, __dist, __comp, __proj);
0090 }
0091
0092 template <class _ForwardIterator, class _Tp, class _Compare>
0093 [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
0094 lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, _Compare __comp) {
0095 static_assert(__is_callable<_Compare&, decltype(*__first), const _Tp&>::value, "The comparator has to be callable");
0096 auto __proj = std::__identity();
0097 return std::__lower_bound<_ClassicAlgPolicy>(__first, __last, __value, __comp, __proj);
0098 }
0099
0100 template <class _ForwardIterator, class _Tp>
0101 [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
0102 lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) {
0103 return std::lower_bound(__first, __last, __value, __less<>());
0104 }
0105
0106 _LIBCPP_END_NAMESPACE_STD
0107
0108 #endif