xref: /llvm-project/libcxx/include/__algorithm/push_heap.h (revision 5aaefa510ef055e8f044ca89e352d4313f3aba49)
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 <__algorithm/comp.h>
13 #include <__algorithm/comp_ref_type.h>
14 #include <__config>
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 #  pragma clang include_instead(<algorithm>)
21 #endif
22 
23 _LIBCPP_BEGIN_NAMESPACE_STD
24 
25 template <class _Compare, class _RandomAccessIterator>
26 _LIBCPP_CONSTEXPR_AFTER_CXX11 void
27 __sift_up(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
28           typename iterator_traits<_RandomAccessIterator>::difference_type __len)
29 {
30     typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
31     if (__len > 1)
32     {
33         __len = (__len - 2) / 2;
34         _RandomAccessIterator __ptr = __first + __len;
35         if (__comp(*__ptr, *--__last))
36         {
37             value_type __t(_VSTD::move(*__last));
38             do
39             {
40                 *__last = _VSTD::move(*__ptr);
41                 __last = __ptr;
42                 if (__len == 0)
43                     break;
44                 __len = (__len - 1) / 2;
45                 __ptr = __first + __len;
46             } while (__comp(*__ptr, __t));
47             *__last = _VSTD::move(__t);
48         }
49     }
50 }
51 
52 template <class _RandomAccessIterator, class _Compare>
53 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
54 void
55 push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
56 {
57     typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
58     _VSTD::__sift_up<_Comp_ref>(__first, __last, __comp, __last - __first);
59 }
60 
61 template <class _RandomAccessIterator>
62 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
63 void
64 push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
65 {
66     _VSTD::push_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
67 }
68 
69 _LIBCPP_END_NAMESPACE_STD
70 
71 #endif // _LIBCPP___ALGORITHM_PUSH_HEAP_H
72