File indexing completed on 2026-05-03 08:13:18
0001
0002
0003
0004
0005
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
0052
0053
0054
0055
0056
0057
0058
0059
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
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
0072
0073 if (__it == __last || !std::__invoke(__comp, std::__invoke(__proj, *__it), __value)) {
0074
0075
0076 if (__dist == 1)
0077 return __it;
0078 return std::__lower_bound_bisecting<_AlgPolicy>(__first, __value, __dist, __comp, __proj);
0079 }
0080
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