File indexing completed on 2026-05-03 08:13:12
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef _LIBCPP___ALGORITHM_UPPER_BOUND_H
0010 #define _LIBCPP___ALGORITHM_UPPER_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 <__functional/invoke.h>
0018 #include <__iterator/advance.h>
0019 #include <__iterator/distance.h>
0020 #include <__iterator/iterator_traits.h>
0021 #include <__type_traits/invoke.h>
0022 #include <__type_traits/is_callable.h>
0023 #include <__type_traits/is_constructible.h>
0024 #include <__utility/move.h>
0025
0026 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0027 # pragma GCC system_header
0028 #endif
0029
0030 _LIBCPP_PUSH_MACROS
0031 #include <__undef_macros>
0032
0033 _LIBCPP_BEGIN_NAMESPACE_STD
0034
0035 template <class _AlgPolicy, class _Compare, class _Iter, class _Sent, class _Tp, class _Proj>
0036 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Iter
0037 __upper_bound(_Iter __first, _Sent __last, const _Tp& __value, _Compare&& __comp, _Proj&& __proj) {
0038 auto __len = _IterOps<_AlgPolicy>::distance(__first, __last);
0039 while (__len != 0) {
0040 auto __half_len = std::__half_positive(__len);
0041 auto __mid = _IterOps<_AlgPolicy>::next(__first, __half_len);
0042 if (std::__invoke(__comp, __value, std::__invoke(__proj, *__mid)))
0043 __len = __half_len;
0044 else {
0045 __first = ++__mid;
0046 __len -= __half_len + 1;
0047 }
0048 }
0049 return __first;
0050 }
0051
0052 template <class _ForwardIterator, class _Tp, class _Compare>
0053 [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
0054 upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, _Compare __comp) {
0055 static_assert(__is_callable<_Compare&, const _Tp&, decltype(*__first)>::value, "The comparator has to be callable");
0056 static_assert(is_copy_constructible<_ForwardIterator>::value, "Iterator has to be copy constructible");
0057 return std::__upper_bound<_ClassicAlgPolicy>(
0058 std::move(__first), std::move(__last), __value, std::move(__comp), std::__identity());
0059 }
0060
0061 template <class _ForwardIterator, class _Tp>
0062 [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
0063 upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) {
0064 return std::upper_bound(std::move(__first), std::move(__last), __value, __less<>());
0065 }
0066
0067 _LIBCPP_END_NAMESPACE_STD
0068
0069 _LIBCPP_POP_MACROS
0070
0071 #endif