mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-04 19:52:28 +00:00
Release Cosmopolitan v3.6.0
This release is an atomic upgrade to GCC 14.1.0 with C23 and C++23
This commit is contained in:
parent
62ace3623a
commit
5660ec4741
1585 changed files with 117353 additions and 271644 deletions
70
third_party/libcxx/__algorithm/lower_bound.h
vendored
70
third_party/libcxx/__algorithm/lower_bound.h
vendored
|
@ -27,11 +27,13 @@
|
|||
|
||||
_LIBCPP_BEGIN_NAMESPACE_STD
|
||||
|
||||
template <class _AlgPolicy, class _Iter, class _Sent, class _Type, class _Proj, class _Comp>
|
||||
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
|
||||
_Iter __lower_bound_impl(_Iter __first, _Sent __last, const _Type& __value, _Comp& __comp, _Proj& __proj) {
|
||||
auto __len = _IterOps<_AlgPolicy>::distance(__first, __last);
|
||||
|
||||
template <class _AlgPolicy, class _Iter, class _Type, class _Proj, class _Comp>
|
||||
_LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Iter __lower_bound_bisecting(
|
||||
_Iter __first,
|
||||
const _Type& __value,
|
||||
typename iterator_traits<_Iter>::difference_type __len,
|
||||
_Comp& __comp,
|
||||
_Proj& __proj) {
|
||||
while (__len != 0) {
|
||||
auto __l2 = std::__half_positive(__len);
|
||||
_Iter __m = __first;
|
||||
|
@ -46,20 +48,60 @@ _Iter __lower_bound_impl(_Iter __first, _Sent __last, const _Type& __value, _Com
|
|||
return __first;
|
||||
}
|
||||
|
||||
// One-sided binary search, aka meta binary search, has been in the public domain for decades, and has the general
|
||||
// advantage of being \Omega(1) rather than the classic algorithm's \Omega(log(n)), with the downside of executing at
|
||||
// most 2*log(n) comparisons vs the classic algorithm's exact log(n). There are two scenarios in which it really shines:
|
||||
// the first one is when operating over non-random-access iterators, because the classic algorithm requires knowing the
|
||||
// container's size upfront, which adds \Omega(n) iterator increments to the complexity. The second one is when you're
|
||||
// traversing the container in order, trying to fast-forward to the next value: in that case, the classic algorithm
|
||||
// would yield \Omega(n*log(n)) comparisons and, for non-random-access iterators, \Omega(n^2) iterator increments,
|
||||
// whereas the one-sided version will yield O(n) operations on both counts, with a \Omega(log(n)) bound on the number of
|
||||
// comparisons.
|
||||
template <class _AlgPolicy, class _ForwardIterator, class _Sent, class _Type, class _Proj, class _Comp>
|
||||
_LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
|
||||
__lower_bound_onesided(_ForwardIterator __first, _Sent __last, const _Type& __value, _Comp& __comp, _Proj& __proj) {
|
||||
// step = 0, ensuring we can always short-circuit when distance is 1 later on
|
||||
if (__first == __last || !std::__invoke(__comp, std::__invoke(__proj, *__first), __value))
|
||||
return __first;
|
||||
|
||||
using _Distance = typename iterator_traits<_ForwardIterator>::difference_type;
|
||||
for (_Distance __step = 1; __first != __last; __step <<= 1) {
|
||||
auto __it = __first;
|
||||
auto __dist = __step - _IterOps<_AlgPolicy>::__advance_to(__it, __step, __last);
|
||||
// once we reach the last range where needle can be we must start
|
||||
// looking inwards, bisecting that range
|
||||
if (__it == __last || !std::__invoke(__comp, std::__invoke(__proj, *__it), __value)) {
|
||||
// we've already checked the previous value and it was less, we can save
|
||||
// one comparison by skipping bisection
|
||||
if (__dist == 1)
|
||||
return __it;
|
||||
return std::__lower_bound_bisecting<_AlgPolicy>(__first, __value, __dist, __comp, __proj);
|
||||
}
|
||||
// range not found, move forward!
|
||||
__first = __it;
|
||||
}
|
||||
return __first;
|
||||
}
|
||||
|
||||
template <class _AlgPolicy, class _ForwardIterator, class _Sent, class _Type, class _Proj, class _Comp>
|
||||
_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
|
||||
__lower_bound(_ForwardIterator __first, _Sent __last, const _Type& __value, _Comp& __comp, _Proj& __proj) {
|
||||
const auto __dist = _IterOps<_AlgPolicy>::distance(__first, __last);
|
||||
return std::__lower_bound_bisecting<_AlgPolicy>(__first, __value, __dist, __comp, __proj);
|
||||
}
|
||||
|
||||
template <class _ForwardIterator, class _Tp, class _Compare>
|
||||
_LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
|
||||
_ForwardIterator lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, _Compare __comp) {
|
||||
static_assert(__is_callable<_Compare, decltype(*__first), const _Tp&>::value,
|
||||
"The comparator has to be callable");
|
||||
_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
|
||||
lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, _Compare __comp) {
|
||||
static_assert(__is_callable<_Compare, decltype(*__first), const _Tp&>::value, "The comparator has to be callable");
|
||||
auto __proj = std::__identity();
|
||||
return std::__lower_bound_impl<_ClassicAlgPolicy>(__first, __last, __value, __comp, __proj);
|
||||
return std::__lower_bound<_ClassicAlgPolicy>(__first, __last, __value, __comp, __proj);
|
||||
}
|
||||
|
||||
template <class _ForwardIterator, class _Tp>
|
||||
_LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
|
||||
_ForwardIterator lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) {
|
||||
return std::lower_bound(__first, __last, __value,
|
||||
__less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
|
||||
_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
|
||||
lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) {
|
||||
return std::lower_bound(__first, __last, __value, __less<>());
|
||||
}
|
||||
|
||||
_LIBCPP_END_NAMESPACE_STD
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue