xref: /llvm-project/libcxx/include/__algorithm/partial_sort.h (revision 5dd19ada571b9190811f3c4d8cd1c2bb5f56227c)
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_H
10 #define _LIBCPP___ALGORITHM_PARTIAL_SORT_H
11 
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/sift_down.h>
17 #include <__algorithm/sort_heap.h>
18 #include <__config>
19 #include <__debug>
20 #include <__debug_utils/randomize_range.h>
21 #include <__iterator/iterator_traits.h>
22 #include <__utility/move.h>
23 #include <type_traits>
24 
25 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
26 #  pragma GCC system_header
27 #endif
28 
29 _LIBCPP_BEGIN_NAMESPACE_STD
30 
31 template <class _AlgPolicy, class _Compare, class _RandomAccessIterator, class _Sentinel>
32 _LIBCPP_CONSTEXPR_AFTER_CXX17
33 _RandomAccessIterator __partial_sort_impl(
34     _RandomAccessIterator __first, _RandomAccessIterator __middle, _Sentinel __last, _Compare __comp) {
35   if (__first == __middle) {
36     return _IterOps<_AlgPolicy>::next(__middle, __last);
37   }
38 
39   std::__make_heap<_AlgPolicy, _Compare>(__first, __middle, __comp);
40 
41   typename iterator_traits<_RandomAccessIterator>::difference_type __len = __middle - __first;
42   _RandomAccessIterator __i = __middle;
43   for (; __i != __last; ++__i)
44   {
45       if (__comp(*__i, *__first))
46       {
47           _IterOps<_AlgPolicy>::iter_swap(__i, __first);
48           std::__sift_down<_AlgPolicy, _Compare>(__first, __comp, __len, __first);
49       }
50 
51   }
52   std::__sort_heap<_AlgPolicy, _Compare>(std::move(__first), std::move(__middle), __comp);
53 
54   return __i;
55 }
56 
57 // TODO(ranges): once `ranges::shuffle` is implemented, remove this helper and make `__debug_randomize_range` support
58 // sentinels.
59 template <class _AlgPolicy, class _RandomAccessIterator, class _Sentinel>
60 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
61 void __maybe_randomize(_RandomAccessIterator __first, _Sentinel __last) {
62   std::__debug_randomize_range<_AlgPolicy>(__first, _IterOps<_AlgPolicy>::next(__first, __last));
63 }
64 
65 template <class _AlgPolicy, class _Compare, class _RandomAccessIterator, class _Sentinel>
66 _LIBCPP_CONSTEXPR_AFTER_CXX17
67 _RandomAccessIterator __partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _Sentinel __last,
68                                      _Compare& __comp) {
69   if (__first == __middle)
70       return _IterOps<_AlgPolicy>::next(__middle, __last);
71 
72   std::__maybe_randomize<_AlgPolicy>(__first, __last);
73 
74   using _Comp_ref = typename __comp_ref_type<_Compare>::type;
75   auto __last_iter = std::__partial_sort_impl<_AlgPolicy, _Comp_ref>(__first, __middle, __last, __comp);
76 
77   std::__maybe_randomize<_AlgPolicy>(__middle, __last);
78 
79   return __last_iter;
80 }
81 
82 template <class _RandomAccessIterator, class _Compare>
83 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
84 void
85 partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
86              _Compare __comp)
87 {
88   static_assert(std::is_copy_constructible<_RandomAccessIterator>::value, "Iterators must be copy constructible.");
89   static_assert(std::is_copy_assignable<_RandomAccessIterator>::value, "Iterators must be copy assignable.");
90 
91   (void)std::__partial_sort<_ClassicAlgPolicy>(std::move(__first), std::move(__middle), std::move(__last), __comp);
92 }
93 
94 template <class _RandomAccessIterator>
95 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
96 void
97 partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
98 {
99     _VSTD::partial_sort(__first, __middle, __last,
100                         __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
101 }
102 
103 _LIBCPP_END_NAMESPACE_STD
104 
105 #endif // _LIBCPP___ALGORITHM_PARTIAL_SORT_H
106