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