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_PUSH_HEAP_H
10 #define _LIBCPP___ALGORITHM_PUSH_HEAP_H
11 
12 #include <__config>
13 #include <__algorithm/comp.h>
14 #include <__algorithm/comp_ref_type.h>
15 #include <__iterator/iterator_traits.h>
16 #include <__utility/move.h>
17 
18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19 #pragma GCC system_header
20 #endif
21 
22 _LIBCPP_PUSH_MACROS
23 #include <__undef_macros>
24 
25 _LIBCPP_BEGIN_NAMESPACE_STD
26 
27 template <class _Compare, class _RandomAccessIterator>
28 _LIBCPP_CONSTEXPR_AFTER_CXX11 void
29 __sift_up(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
30           typename iterator_traits<_RandomAccessIterator>::difference_type __len)
31 {
32     typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
33     if (__len > 1)
34     {
35         __len = (__len - 2) / 2;
36         _RandomAccessIterator __ptr = __first + __len;
37         if (__comp(*__ptr, *--__last))
38         {
39             value_type __t(_VSTD::move(*__last));
40             do
41             {
42                 *__last = _VSTD::move(*__ptr);
43                 __last = __ptr;
44                 if (__len == 0)
45                     break;
46                 __len = (__len - 1) / 2;
47                 __ptr = __first + __len;
48             } while (__comp(*__ptr, __t));
49             *__last = _VSTD::move(__t);
50         }
51     }
52 }
53 
54 template <class _RandomAccessIterator, class _Compare>
55 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
56 void
57 push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
58 {
59     typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
60     _VSTD::__sift_up<_Comp_ref>(__first, __last, __comp, __last - __first);
61 }
62 
63 template <class _RandomAccessIterator>
64 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
65 void
66 push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
67 {
68     _VSTD::push_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
69 }
70 
71 _LIBCPP_END_NAMESPACE_STD
72 
73 _LIBCPP_POP_MACROS
74 
75 #endif // _LIBCPP___ALGORITHM_PUSH_HEAP_H
76