xref: /llvm-project/libcxx/include/__algorithm/sort_heap.h (revision 134723edd5bf06ff6ec8aca7b87c56e5bd70ccae)
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 <__config>
13 #include <__algorithm/comp.h>
14 #include <__algorithm/comp_ref_type.h>
15 #include <__algorithm/pop_heap.h>
16 #include <__iterator/iterator_traits.h>
17 #include <type_traits> // swap
18 
19 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20 #pragma GCC system_header
21 #endif
22 
23 _LIBCPP_PUSH_MACROS
24 #include <__undef_macros>
25 
26 _LIBCPP_BEGIN_NAMESPACE_STD
27 
28 template <class _Compare, class _RandomAccessIterator>
29 _LIBCPP_CONSTEXPR_AFTER_CXX17 void
30 __sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
31 {
32     typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
33     for (difference_type __n = __last - __first; __n > 1; --__last, (void) --__n)
34         _VSTD::__pop_heap<_Compare>(__first, __last, __comp, __n);
35 }
36 
37 template <class _RandomAccessIterator, class _Compare>
38 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
39 void
40 sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
41 {
42     typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
43     _VSTD::__sort_heap<_Comp_ref>(__first, __last, __comp);
44 }
45 
46 template <class _RandomAccessIterator>
47 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
48 void
49 sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
50 {
51     _VSTD::sort_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
52 }
53 
54 _LIBCPP_END_NAMESPACE_STD
55 
56 _LIBCPP_POP_MACROS
57 
58 #endif // _LIBCPP___ALGORITHM_SORT_HEAP_H
59