Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / include / __algorithm / push_heap.h
blob82b571e44bd4dd9bf060ab5fcc48419df05d67e8
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 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___ALGORITHM_PUSH_HEAP_H
10 #define _LIBCPP___ALGORITHM_PUSH_HEAP_H
12 #include <__algorithm/comp.h>
13 #include <__algorithm/comp_ref_type.h>
14 #include <__algorithm/iterator_operations.h>
15 #include <__config>
16 #include <__iterator/iterator_traits.h>
17 #include <__type_traits/is_copy_assignable.h>
18 #include <__type_traits/is_copy_constructible.h>
19 #include <__utility/move.h>
21 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
22 # pragma GCC system_header
23 #endif
25 _LIBCPP_PUSH_MACROS
26 #include <__undef_macros>
28 _LIBCPP_BEGIN_NAMESPACE_STD
30 template <class _AlgPolicy, class _Compare, class _RandomAccessIterator>
31 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
32 void __sift_up(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare&& __comp,
33 typename iterator_traits<_RandomAccessIterator>::difference_type __len) {
34 using value_type = typename iterator_traits<_RandomAccessIterator>::value_type;
36 if (__len > 1) {
37 __len = (__len - 2) / 2;
38 _RandomAccessIterator __ptr = __first + __len;
40 if (__comp(*__ptr, *--__last)) {
41 value_type __t(_IterOps<_AlgPolicy>::__iter_move(__last));
42 do {
43 *__last = _IterOps<_AlgPolicy>::__iter_move(__ptr);
44 __last = __ptr;
45 if (__len == 0)
46 break;
47 __len = (__len - 1) / 2;
48 __ptr = __first + __len;
49 } while (__comp(*__ptr, __t));
51 *__last = std::move(__t);
56 template <class _AlgPolicy, class _RandomAccessIterator, class _Compare>
57 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
58 void __push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare& __comp) {
59 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __last - __first;
60 std::__sift_up<_AlgPolicy, __comp_ref_type<_Compare> >(std::move(__first), std::move(__last), __comp, __len);
63 template <class _RandomAccessIterator, class _Compare>
64 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
65 void push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) {
66 static_assert(std::is_copy_constructible<_RandomAccessIterator>::value, "Iterators must be copy constructible.");
67 static_assert(std::is_copy_assignable<_RandomAccessIterator>::value, "Iterators must be copy assignable.");
69 std::__push_heap<_ClassicAlgPolicy>(std::move(__first), std::move(__last), __comp);
72 template <class _RandomAccessIterator>
73 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
74 void push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last) {
75 std::push_heap(std::move(__first), std::move(__last), __less<>());
78 _LIBCPP_END_NAMESPACE_STD
80 _LIBCPP_POP_MACROS
82 #endif // _LIBCPP___ALGORITHM_PUSH_HEAP_H