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_SORT_HEAP_H
10 #define _LIBCPP___ALGORITHM_SORT_HEAP_H
11 
12 #include <__algorithm/comp.h>
13 #include <__algorithm/comp_ref_type.h>
14 #include <__algorithm/pop_heap.h>
15 #include <__config>
16 #include <__iterator/iterator_traits.h>
17 #include <__utility/move.h>
18 #include <type_traits> // swap
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 _RandomAccessIterator>
27 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
28 void __sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare& __comp) {
29   using _CompRef = typename __comp_ref_type<_Compare>::type;
30   _CompRef __comp_ref = __comp;
31 
32   using difference_type = typename iterator_traits<_RandomAccessIterator>::difference_type;
33   for (difference_type __n = __last - __first; __n > 1; --__last, (void) --__n)
34     std::__pop_heap<_CompRef>(__first, __last, __comp_ref, __n);
35 }
36 
37 template <class _RandomAccessIterator, class _Compare>
38 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
39 void sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) {
40   std::__sort_heap(std::move(__first), std::move(__last), __comp);
41 }
42 
43 template <class _RandomAccessIterator>
44 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
45 void sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last) {
46   std::sort_heap(std::move(__first), std::move(__last),
47       __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
48 }
49 
50 _LIBCPP_END_NAMESPACE_STD
51 
52 #endif // _LIBCPP___ALGORITHM_SORT_HEAP_H
53