1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___ALGORITHM_PUSH_HEAP_H
10 #define _LIBCPP___ALGORITHM_PUSH_HEAP_H
13 #include <__algorithm/comp.h>
14 #include <__algorithm/comp_ref_type.h>
15 #include <__iterator/iterator_traits.h>
16 #include <__utility/move.h>
18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19 #pragma GCC system_header
23 #include <__undef_macros>
25 _LIBCPP_BEGIN_NAMESPACE_STD
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)
32 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
35 __len = (__len - 2) / 2;
36 _RandomAccessIterator __ptr = __first + __len;
37 if (__comp(*__ptr, *--__last))
39 value_type __t(_VSTD::move(*__last));
42 *__last = _VSTD::move(*__ptr);
46 __len = (__len - 1) / 2;
47 __ptr = __first + __len;
48 } while (__comp(*__ptr, __t));
49 *__last = _VSTD::move(__t);
54 template <class _RandomAccessIterator, class _Compare>
55 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
57 push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
59 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
60 _VSTD::__sift_up<_Comp_ref>(__first, __last, __comp, __last - __first);
63 template <class _RandomAccessIterator>
64 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
66 push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
68 _VSTD::push_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
71 _LIBCPP_END_NAMESPACE_STD
75 #endif // _LIBCPP___ALGORITHM_PUSH_HEAP_H