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