1 //===----------------------------------------------------------------------===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef _LIBCPP___CXX03___ALGORITHM_RANGES_PARTITION_POINT_H 10 #define _LIBCPP___CXX03___ALGORITHM_RANGES_PARTITION_POINT_H 11 12 #include <__cxx03/__algorithm/half_positive.h> 13 #include <__cxx03/__config> 14 #include <__cxx03/__functional/identity.h> 15 #include <__cxx03/__functional/invoke.h> 16 #include <__cxx03/__iterator/concepts.h> 17 #include <__cxx03/__iterator/distance.h> 18 #include <__cxx03/__iterator/iterator_traits.h> 19 #include <__cxx03/__iterator/next.h> 20 #include <__cxx03/__iterator/projected.h> 21 #include <__cxx03/__ranges/access.h> 22 #include <__cxx03/__ranges/concepts.h> 23 #include <__cxx03/__ranges/dangling.h> 24 #include <__cxx03/__utility/move.h> 25 26 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 27 # pragma GCC system_header 28 #endif 29 30 _LIBCPP_PUSH_MACROS 31 #include <__cxx03/__undef_macros> 32 33 #if _LIBCPP_STD_VER >= 20 34 35 _LIBCPP_BEGIN_NAMESPACE_STD 36 37 namespace ranges { 38 namespace __partition_point { 39 40 struct __fn { 41 // TODO(ranges): delegate to the classic algorithm. 42 template <class _Iter, class _Sent, class _Proj, class _Pred> 43 _LIBCPP_HIDE_FROM_ABI constexpr static _Iter 44 __partition_point_fn_impl(_Iter&& __first, _Sent&& __last, _Pred& __pred, _Proj& __proj) { 45 auto __len = ranges::distance(__first, __last); 46 47 while (__len != 0) { 48 auto __half_len = std::__half_positive(__len); 49 auto __mid = ranges::next(__first, __half_len); 50 51 if (std::invoke(__pred, std::invoke(__proj, *__mid))) { 52 __first = ++__mid; 53 __len -= __half_len + 1; 54 55 } else { 56 __len = __half_len; 57 } 58 } 59 60 return __first; 61 } 62 63 template <forward_iterator _Iter, 64 sentinel_for<_Iter> _Sent, 65 class _Proj = identity, 66 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred> 67 _LIBCPP_HIDE_FROM_ABI constexpr _Iter operator()(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {}) const { 68 return __partition_point_fn_impl(std::move(__first), std::move(__last), __pred, __proj); 69 } 70 71 template <forward_range _Range, 72 class _Proj = identity, 73 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred> 74 _LIBCPP_HIDE_FROM_ABI constexpr borrowed_iterator_t<_Range> 75 operator()(_Range&& __range, _Pred __pred, _Proj __proj = {}) const { 76 return __partition_point_fn_impl(ranges::begin(__range), ranges::end(__range), __pred, __proj); 77 } 78 }; 79 80 } // namespace __partition_point 81 82 inline namespace __cpo { 83 inline constexpr auto partition_point = __partition_point::__fn{}; 84 } // namespace __cpo 85 } // namespace ranges 86 87 _LIBCPP_END_NAMESPACE_STD 88 89 #endif // _LIBCPP_STD_VER >= 20 90 91 _LIBCPP_POP_MACROS 92 93 #endif // _LIBCPP___CXX03___ALGORITHM_RANGES_PARTITION_POINT_H 94