Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:20

0001 //===----------------------------------------------------------------------===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef _LIBCPP___CXX03___ALGORITHM_RANGES_PARTITION_POINT_H
0010 #define _LIBCPP___CXX03___ALGORITHM_RANGES_PARTITION_POINT_H
0011 
0012 #include <__cxx03/__algorithm/half_positive.h>
0013 #include <__cxx03/__config>
0014 #include <__cxx03/__functional/identity.h>
0015 #include <__cxx03/__functional/invoke.h>
0016 #include <__cxx03/__iterator/concepts.h>
0017 #include <__cxx03/__iterator/distance.h>
0018 #include <__cxx03/__iterator/iterator_traits.h>
0019 #include <__cxx03/__iterator/next.h>
0020 #include <__cxx03/__iterator/projected.h>
0021 #include <__cxx03/__ranges/access.h>
0022 #include <__cxx03/__ranges/concepts.h>
0023 #include <__cxx03/__ranges/dangling.h>
0024 #include <__cxx03/__utility/move.h>
0025 
0026 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0027 #  pragma GCC system_header
0028 #endif
0029 
0030 _LIBCPP_PUSH_MACROS
0031 #include <__cxx03/__undef_macros>
0032 
0033 #if _LIBCPP_STD_VER >= 20
0034 
0035 _LIBCPP_BEGIN_NAMESPACE_STD
0036 
0037 namespace ranges {
0038 namespace __partition_point {
0039 
0040 struct __fn {
0041   // TODO(ranges): delegate to the classic algorithm.
0042   template <class _Iter, class _Sent, class _Proj, class _Pred>
0043   _LIBCPP_HIDE_FROM_ABI constexpr static _Iter
0044   __partition_point_fn_impl(_Iter&& __first, _Sent&& __last, _Pred& __pred, _Proj& __proj) {
0045     auto __len = ranges::distance(__first, __last);
0046 
0047     while (__len != 0) {
0048       auto __half_len = std::__half_positive(__len);
0049       auto __mid      = ranges::next(__first, __half_len);
0050 
0051       if (std::invoke(__pred, std::invoke(__proj, *__mid))) {
0052         __first = ++__mid;
0053         __len -= __half_len + 1;
0054 
0055       } else {
0056         __len = __half_len;
0057       }
0058     }
0059 
0060     return __first;
0061   }
0062 
0063   template <forward_iterator _Iter,
0064             sentinel_for<_Iter> _Sent,
0065             class _Proj = identity,
0066             indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
0067   _LIBCPP_HIDE_FROM_ABI constexpr _Iter operator()(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {}) const {
0068     return __partition_point_fn_impl(std::move(__first), std::move(__last), __pred, __proj);
0069   }
0070 
0071   template <forward_range _Range,
0072             class _Proj = identity,
0073             indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred>
0074   _LIBCPP_HIDE_FROM_ABI constexpr borrowed_iterator_t<_Range>
0075   operator()(_Range&& __range, _Pred __pred, _Proj __proj = {}) const {
0076     return __partition_point_fn_impl(ranges::begin(__range), ranges::end(__range), __pred, __proj);
0077   }
0078 };
0079 
0080 } // namespace __partition_point
0081 
0082 inline namespace __cpo {
0083 inline constexpr auto partition_point = __partition_point::__fn{};
0084 } // namespace __cpo
0085 } // namespace ranges
0086 
0087 _LIBCPP_END_NAMESPACE_STD
0088 
0089 #endif // _LIBCPP_STD_VER >= 20
0090 
0091 _LIBCPP_POP_MACROS
0092 
0093 #endif // _LIBCPP___CXX03___ALGORITHM_RANGES_PARTITION_POINT_H