xref: /llvm-project/libcxx/include/__algorithm/make_heap.h (revision 7ed7d4ccb8991e2b5b95334b508f8cec2faee737)
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_MAKE_HEAP_H
10 #define _LIBCPP___ALGORITHM_MAKE_HEAP_H
11 
12 #include <__config>
13 #include <__algorithm/comp_ref_type.h>
14 #include <__algorithm/sift_down.h>
15 #include <__iterator/iterator_traits.h>
16 
17 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
18 #pragma GCC system_header
19 #endif
20 
21 _LIBCPP_PUSH_MACROS
22 #include <__undef_macros>
23 
24 _LIBCPP_BEGIN_NAMESPACE_STD
25 
26 template <class _Compare, class _RandomAccessIterator>
27 _LIBCPP_CONSTEXPR_AFTER_CXX11 void
28 __make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
29 {
30     typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
31     difference_type __n = __last - __first;
32     if (__n > 1)
33     {
34         // start from the first parent, there is no need to consider children
35         for (difference_type __start = (__n - 2) / 2; __start >= 0; --__start)
36         {
37             _VSTD::__sift_down<_Compare>(__first, __last, __comp, __n, __first + __start);
38         }
39     }
40 }
41 
42 template <class _RandomAccessIterator, class _Compare>
43 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
44 void
45 make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
46 {
47     typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
48     _VSTD::__make_heap<_Comp_ref>(__first, __last, __comp);
49 }
50 
51 template <class _RandomAccessIterator>
52 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
53 void
54 make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
55 {
56     _VSTD::make_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
57 }
58 
59 _LIBCPP_END_NAMESPACE_STD
60 
61 _LIBCPP_POP_MACROS
62 
63 #endif // _LIBCPP___ALGORITHM_MAKE_HEAP_H
64