1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___ALGORITHM_RANGES_PARTITION_POINT_H
10 #define _LIBCPP___ALGORITHM_RANGES_PARTITION_POINT_H
12 #include <__algorithm/half_positive.h>
14 #include <__functional/identity.h>
15 #include <__functional/invoke.h>
16 #include <__iterator/concepts.h>
17 #include <__iterator/distance.h>
18 #include <__iterator/iterator_traits.h>
19 #include <__iterator/next.h>
20 #include <__iterator/projected.h>
21 #include <__ranges/access.h>
22 #include <__ranges/concepts.h>
23 #include <__ranges/dangling.h>
24 #include <__utility/move.h>
26 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
27 # pragma GCC system_header
31 #include <__undef_macros>
33 #if _LIBCPP_STD_VER >= 20
35 _LIBCPP_BEGIN_NAMESPACE_STD
38 struct __partition_point
{
39 // TODO(ranges): delegate to the classic algorithm.
40 template <class _Iter
, class _Sent
, class _Proj
, class _Pred
>
41 _LIBCPP_HIDE_FROM_ABI
constexpr static _Iter
42 __partition_point_fn_impl(_Iter
&& __first
, _Sent
&& __last
, _Pred
& __pred
, _Proj
& __proj
) {
43 auto __len
= ranges::distance(__first
, __last
);
46 auto __half_len
= std::__half_positive(__len
);
47 auto __mid
= ranges::next(__first
, __half_len
);
49 if (std::invoke(__pred
, std::invoke(__proj
, *__mid
))) {
51 __len
-= __half_len
+ 1;
61 template <forward_iterator _Iter
,
62 sentinel_for
<_Iter
> _Sent
,
63 class _Proj
= identity
,
64 indirect_unary_predicate
<projected
<_Iter
, _Proj
>> _Pred
>
65 _LIBCPP_HIDE_FROM_ABI
constexpr _Iter
operator()(_Iter __first
, _Sent __last
, _Pred __pred
, _Proj __proj
= {}) const {
66 return __partition_point_fn_impl(std::move(__first
), std::move(__last
), __pred
, __proj
);
69 template <forward_range _Range
,
70 class _Proj
= identity
,
71 indirect_unary_predicate
<projected
<iterator_t
<_Range
>, _Proj
>> _Pred
>
72 _LIBCPP_HIDE_FROM_ABI
constexpr borrowed_iterator_t
<_Range
>
73 operator()(_Range
&& __range
, _Pred __pred
, _Proj __proj
= {}) const {
74 return __partition_point_fn_impl(ranges::begin(__range
), ranges::end(__range
), __pred
, __proj
);
78 inline namespace __cpo
{
79 inline constexpr auto partition_point
= __partition_point
{};
83 _LIBCPP_END_NAMESPACE_STD
85 #endif // _LIBCPP_STD_VER >= 20
89 #endif // _LIBCPP___ALGORITHM_RANGES_PARTITION_POINT_H