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