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_PARTIAL_SORT_COPY_H
10 #define _LIBCPP___ALGORITHM_PARTIAL_SORT_COPY_H
12 #include <__algorithm/comp.h>
13 #include <__algorithm/comp_ref_type.h>
14 #include <__algorithm/iterator_operations.h>
15 #include <__algorithm/make_heap.h>
16 #include <__algorithm/make_projected.h>
17 #include <__algorithm/sift_down.h>
18 #include <__algorithm/sort_heap.h>
20 #include <__functional/identity.h>
21 #include <__functional/invoke.h>
22 #include <__iterator/iterator_traits.h>
23 #include <__type_traits/is_callable.h>
24 #include <__utility/move.h>
25 #include <__utility/pair.h>
27 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
28 # pragma GCC system_header
31 _LIBCPP_BEGIN_NAMESPACE_STD
33 template <class _AlgPolicy
, class _Compare
,
34 class _InputIterator
, class _Sentinel1
, class _RandomAccessIterator
, class _Sentinel2
,
35 class _Proj1
, class _Proj2
>
36 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair
<_InputIterator
, _RandomAccessIterator
>
37 __partial_sort_copy(_InputIterator __first
, _Sentinel1 __last
,
38 _RandomAccessIterator __result_first
, _Sentinel2 __result_last
,
39 _Compare
&& __comp
, _Proj1
&& __proj1
, _Proj2
&& __proj2
)
41 _RandomAccessIterator __r
= __result_first
;
42 auto&& __projected_comp
= std::__make_projected(__comp
, __proj2
);
44 if (__r
!= __result_last
)
46 for (; __first
!= __last
&& __r
!= __result_last
; ++__first
, (void) ++__r
)
48 std::__make_heap
<_AlgPolicy
>(__result_first
, __r
, __projected_comp
);
49 typename iterator_traits
<_RandomAccessIterator
>::difference_type __len
= __r
- __result_first
;
50 for (; __first
!= __last
; ++__first
)
51 if (std::__invoke(__comp
, std::__invoke(__proj1
, *__first
), std::__invoke(__proj2
, *__result_first
))) {
52 *__result_first
= *__first
;
53 std::__sift_down
<_AlgPolicy
>(__result_first
, __projected_comp
, __len
, __result_first
);
55 std::__sort_heap
<_AlgPolicy
>(__result_first
, __r
, __projected_comp
);
58 return pair
<_InputIterator
, _RandomAccessIterator
>(
59 _IterOps
<_AlgPolicy
>::next(std::move(__first
), std::move(__last
)), std::move(__r
));
62 template <class _InputIterator
, class _RandomAccessIterator
, class _Compare
>
63 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
65 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
66 _RandomAccessIterator __result_first
, _RandomAccessIterator __result_last
, _Compare __comp
)
68 static_assert(__is_callable
<_Compare
, decltype(*__first
), decltype(*__result_first
)>::value
,
69 "Comparator has to be callable");
71 auto __result
= std::__partial_sort_copy
<_ClassicAlgPolicy
>(__first
, __last
, __result_first
, __result_last
,
72 static_cast<__comp_ref_type
<_Compare
> >(__comp
), __identity(), __identity());
73 return __result
.second
;
76 template <class _InputIterator
, class _RandomAccessIterator
>
77 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
79 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
80 _RandomAccessIterator __result_first
, _RandomAccessIterator __result_last
)
82 return _VSTD::partial_sort_copy(__first
, __last
, __result_first
, __result_last
, __less
<>());
85 _LIBCPP_END_NAMESPACE_STD
87 #endif // _LIBCPP___ALGORITHM_PARTIAL_SORT_COPY_H