2021-06-17 15:30:11 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef _LIBCPP___ALGORITHM_LOWER_BOUND_H
|
|
|
|
#define _LIBCPP___ALGORITHM_LOWER_BOUND_H
|
|
|
|
|
|
|
|
#include <__algorithm/comp.h>
|
|
|
|
#include <__algorithm/half_positive.h>
|
2022-06-13 08:43:43 +00:00
|
|
|
#include <__algorithm/iterator_operations.h>
|
2022-01-07 14:45:05 +00:00
|
|
|
#include <__config>
|
2022-06-05 19:15:16 +00:00
|
|
|
#include <__functional/identity.h>
|
2022-06-10 17:53:10 +00:00
|
|
|
#include <__functional/invoke.h>
|
|
|
|
#include <__iterator/advance.h>
|
|
|
|
#include <__iterator/distance.h>
|
|
|
|
#include <__iterator/iterator_traits.h>
|
2022-06-05 19:15:16 +00:00
|
|
|
#include <__type_traits/is_callable.h>
|
2022-07-13 00:52:14 +00:00
|
|
|
#include <__type_traits/remove_reference.h>
|
2021-06-17 15:30:11 +00:00
|
|
|
|
|
|
|
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
|
2022-02-02 01:16:40 +00:00
|
|
|
# pragma GCC system_header
|
2021-06-17 15:30:11 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
_LIBCPP_BEGIN_NAMESPACE_STD
|
|
|
|
|
2022-07-13 00:52:14 +00:00
|
|
|
template <class _AlgPolicy, class _Iter, class _Sent, class _Type, class _Proj, class _Comp>
|
2022-08-19 11:08:01 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Iter
|
2023-06-16 13:52:23 +00:00
|
|
|
__lower_bound(_Iter __first, _Sent __last, const _Type& __value, _Comp& __comp, _Proj& __proj) {
|
2022-07-13 00:52:14 +00:00
|
|
|
auto __len = _IterOps<_AlgPolicy>::distance(__first, __last);
|
2022-06-05 19:15:16 +00:00
|
|
|
|
|
|
|
while (__len != 0) {
|
|
|
|
auto __l2 = std::__half_positive(__len);
|
|
|
|
_Iter __m = __first;
|
2022-07-13 00:52:14 +00:00
|
|
|
_IterOps<_AlgPolicy>::advance(__m, __l2);
|
2022-06-05 19:15:16 +00:00
|
|
|
if (std::__invoke(__comp, std::__invoke(__proj, *__m), __value)) {
|
|
|
|
__first = ++__m;
|
|
|
|
__len -= __l2 + 1;
|
|
|
|
} else {
|
|
|
|
__len = __l2;
|
2021-06-17 15:30:11 +00:00
|
|
|
}
|
2022-06-05 19:15:16 +00:00
|
|
|
}
|
|
|
|
return __first;
|
2021-06-17 15:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class _ForwardIterator, class _Tp, class _Compare>
|
2022-08-19 11:08:01 +00:00
|
|
|
_LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
|
2022-07-08 16:17:26 +00:00
|
|
|
lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, _Compare __comp) {
|
2022-06-05 19:15:16 +00:00
|
|
|
static_assert(__is_callable<_Compare, decltype(*__first), const _Tp&>::value, "The comparator has to be callable");
|
|
|
|
auto __proj = std::__identity();
|
2023-06-16 13:52:23 +00:00
|
|
|
return std::__lower_bound<_ClassicAlgPolicy>(__first, __last, __value, __comp, __proj);
|
2021-06-17 15:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class _ForwardIterator, class _Tp>
|
2022-08-19 11:08:01 +00:00
|
|
|
_LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
|
2022-07-08 16:17:26 +00:00
|
|
|
lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) {
|
2023-06-06 20:57:45 +00:00
|
|
|
return std::lower_bound(__first, __last, __value, __less<>());
|
2021-06-17 15:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_END_NAMESPACE_STD
|
|
|
|
|
|
|
|
#endif // _LIBCPP___ALGORITHM_LOWER_BOUND_H
|