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___ALGORITHM_PARTIAL_SORT_COPY_H
10 #define _LIBCPP___ALGORITHM_PARTIAL_SORT_COPY_H
11 
12 #include <__algorithm/comp.h>
13 #include <__algorithm/comp_ref_type.h>
14 #include <__algorithm/make_heap.h>
15 #include <__algorithm/sift_down.h>
16 #include <__algorithm/sort_heap.h>
17 #include <__config>
18 #include <__iterator/iterator_traits.h>
19 
20 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21 #  pragma GCC system_header
22 #endif
23 
24 _LIBCPP_BEGIN_NAMESPACE_STD
25 
26 template <class _Compare, class _InputIterator, class _RandomAccessIterator>
27 _LIBCPP_CONSTEXPR_AFTER_CXX17 _RandomAccessIterator
28 __partial_sort_copy(_InputIterator __first, _InputIterator __last,
29                     _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
30 {
31     _RandomAccessIterator __r = __result_first;
32     if (__r != __result_last)
33     {
34         for (; __first != __last && __r != __result_last; ++__first, (void) ++__r)
35             *__r = *__first;
36         _VSTD::__make_heap<_Compare>(__result_first, __r, __comp);
37         typename iterator_traits<_RandomAccessIterator>::difference_type __len = __r - __result_first;
38         for (; __first != __last; ++__first)
39             if (__comp(*__first, *__result_first))
40             {
41                 *__result_first = *__first;
42                 _VSTD::__sift_down<_Compare>(__result_first, __comp, __len, __result_first);
43             }
44         _VSTD::__sort_heap<_Compare>(__result_first, __r, __comp);
45     }
46     return __r;
47 }
48 
49 template <class _InputIterator, class _RandomAccessIterator, class _Compare>
50 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
51 _RandomAccessIterator
52 partial_sort_copy(_InputIterator __first, _InputIterator __last,
53                   _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
54 {
55     typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
56     return _VSTD::__partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __comp);
57 }
58 
59 template <class _InputIterator, class _RandomAccessIterator>
60 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
61 _RandomAccessIterator
62 partial_sort_copy(_InputIterator __first, _InputIterator __last,
63                   _RandomAccessIterator __result_first, _RandomAccessIterator __result_last)
64 {
65     return _VSTD::partial_sort_copy(__first, __last, __result_first, __result_last,
66                                    __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
67 }
68 
69 _LIBCPP_END_NAMESPACE_STD
70 
71 #endif // _LIBCPP___ALGORITHM_PARTIAL_SORT_COPY_H
72