Back to home page

EIC code displayed by LXR

 
 

    


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

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___ALGORITHM_PARTIAL_SORT_H
0010 #define _LIBCPP___ALGORITHM_PARTIAL_SORT_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/sift_down.h>
0017 #include <__algorithm/sort_heap.h>
0018 #include <__config>
0019 #include <__debug_utils/randomize_range.h>
0020 #include <__iterator/iterator_traits.h>
0021 #include <__type_traits/is_assignable.h>
0022 #include <__type_traits/is_constructible.h>
0023 #include <__utility/move.h>
0024 
0025 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0026 #  pragma GCC system_header
0027 #endif
0028 
0029 _LIBCPP_PUSH_MACROS
0030 #include <__undef_macros>
0031 
0032 _LIBCPP_BEGIN_NAMESPACE_STD
0033 
0034 template <class _AlgPolicy, class _Compare, class _RandomAccessIterator, class _Sentinel>
0035 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _RandomAccessIterator __partial_sort_impl(
0036     _RandomAccessIterator __first, _RandomAccessIterator __middle, _Sentinel __last, _Compare&& __comp) {
0037   if (__first == __middle) {
0038     return _IterOps<_AlgPolicy>::next(__middle, __last);
0039   }
0040 
0041   std::__make_heap<_AlgPolicy>(__first, __middle, __comp);
0042 
0043   typename iterator_traits<_RandomAccessIterator>::difference_type __len = __middle - __first;
0044   _RandomAccessIterator __i                                              = __middle;
0045   for (; __i != __last; ++__i) {
0046     if (__comp(*__i, *__first)) {
0047       _IterOps<_AlgPolicy>::iter_swap(__i, __first);
0048       std::__sift_down<_AlgPolicy>(__first, __comp, __len, __first);
0049     }
0050   }
0051   std::__sort_heap<_AlgPolicy>(std::move(__first), std::move(__middle), __comp);
0052 
0053   return __i;
0054 }
0055 
0056 template <class _AlgPolicy, class _Compare, class _RandomAccessIterator, class _Sentinel>
0057 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _RandomAccessIterator
0058 __partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _Sentinel __last, _Compare& __comp) {
0059   if (__first == __middle)
0060     return _IterOps<_AlgPolicy>::next(__middle, __last);
0061 
0062   std::__debug_randomize_range<_AlgPolicy>(__first, __last);
0063 
0064   auto __last_iter =
0065       std::__partial_sort_impl<_AlgPolicy>(__first, __middle, __last, static_cast<__comp_ref_type<_Compare> >(__comp));
0066 
0067   std::__debug_randomize_range<_AlgPolicy>(__middle, __last);
0068 
0069   return __last_iter;
0070 }
0071 
0072 template <class _RandomAccessIterator, class _Compare>
0073 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void partial_sort(
0074     _RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last, _Compare __comp) {
0075   static_assert(std::is_copy_constructible<_RandomAccessIterator>::value, "Iterators must be copy constructible.");
0076   static_assert(std::is_copy_assignable<_RandomAccessIterator>::value, "Iterators must be copy assignable.");
0077 
0078   (void)std::__partial_sort<_ClassicAlgPolicy>(std::move(__first), std::move(__middle), std::move(__last), __comp);
0079 }
0080 
0081 template <class _RandomAccessIterator>
0082 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
0083 partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last) {
0084   std::partial_sort(__first, __middle, __last, __less<>());
0085 }
0086 
0087 _LIBCPP_END_NAMESPACE_STD
0088 
0089 _LIBCPP_POP_MACROS
0090 
0091 #endif // _LIBCPP___ALGORITHM_PARTIAL_SORT_H