File indexing completed on 2026-05-03 08:13:09
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef _LIBCPP___ALGORITHM_PARTIAL_SORT_COPY_H
0010 #define _LIBCPP___ALGORITHM_PARTIAL_SORT_COPY_H
0011
0012 #include <__algorithm/comp.h>
0013 #include <__algorithm/comp_ref_type.h>
0014 #include <__algorithm/iterator_operations.h>
0015 #include <__algorithm/make_heap.h>
0016 #include <__algorithm/make_projected.h>
0017 #include <__algorithm/sift_down.h>
0018 #include <__algorithm/sort_heap.h>
0019 #include <__config>
0020 #include <__functional/identity.h>
0021 #include <__iterator/iterator_traits.h>
0022 #include <__type_traits/invoke.h>
0023 #include <__type_traits/is_callable.h>
0024 #include <__utility/move.h>
0025 #include <__utility/pair.h>
0026
0027 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0028 # pragma GCC system_header
0029 #endif
0030
0031 _LIBCPP_PUSH_MACROS
0032 #include <__undef_macros>
0033
0034 _LIBCPP_BEGIN_NAMESPACE_STD
0035
0036 template <class _AlgPolicy,
0037 class _Compare,
0038 class _InputIterator,
0039 class _Sentinel1,
0040 class _RandomAccessIterator,
0041 class _Sentinel2,
0042 class _Proj1,
0043 class _Proj2>
0044 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_InputIterator, _RandomAccessIterator> __partial_sort_copy(
0045 _InputIterator __first,
0046 _Sentinel1 __last,
0047 _RandomAccessIterator __result_first,
0048 _Sentinel2 __result_last,
0049 _Compare&& __comp,
0050 _Proj1&& __proj1,
0051 _Proj2&& __proj2) {
0052 _RandomAccessIterator __r = __result_first;
0053 auto&& __projected_comp = std::__make_projected(__comp, __proj2);
0054
0055 if (__r != __result_last) {
0056 for (; __first != __last && __r != __result_last; ++__first, (void)++__r)
0057 *__r = *__first;
0058 std::__make_heap<_AlgPolicy>(__result_first, __r, __projected_comp);
0059 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __r - __result_first;
0060 for (; __first != __last; ++__first)
0061 if (std::__invoke(__comp, std::__invoke(__proj1, *__first), std::__invoke(__proj2, *__result_first))) {
0062 *__result_first = *__first;
0063 std::__sift_down<_AlgPolicy>(__result_first, __projected_comp, __len, __result_first);
0064 }
0065 std::__sort_heap<_AlgPolicy>(__result_first, __r, __projected_comp);
0066 }
0067
0068 return pair<_InputIterator, _RandomAccessIterator>(
0069 _IterOps<_AlgPolicy>::next(std::move(__first), std::move(__last)), std::move(__r));
0070 }
0071
0072 template <class _InputIterator, class _RandomAccessIterator, class _Compare>
0073 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _RandomAccessIterator partial_sort_copy(
0074 _InputIterator __first,
0075 _InputIterator __last,
0076 _RandomAccessIterator __result_first,
0077 _RandomAccessIterator __result_last,
0078 _Compare __comp) {
0079 static_assert(__is_callable<_Compare&, decltype(*__first), decltype(*__result_first)>::value,
0080 "The comparator has to be callable");
0081
0082 auto __result = std::__partial_sort_copy<_ClassicAlgPolicy>(
0083 __first,
0084 __last,
0085 __result_first,
0086 __result_last,
0087 static_cast<__comp_ref_type<_Compare> >(__comp),
0088 __identity(),
0089 __identity());
0090 return __result.second;
0091 }
0092
0093 template <class _InputIterator, class _RandomAccessIterator>
0094 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _RandomAccessIterator partial_sort_copy(
0095 _InputIterator __first,
0096 _InputIterator __last,
0097 _RandomAccessIterator __result_first,
0098 _RandomAccessIterator __result_last) {
0099 return std::partial_sort_copy(__first, __last, __result_first, __result_last, __less<>());
0100 }
0101
0102 _LIBCPP_END_NAMESPACE_STD
0103
0104 _LIBCPP_POP_MACROS
0105
0106 #endif