xref: /llvm-project/libcxx/include/__algorithm/sift_down.h (revision 134723edd5bf06ff6ec8aca7b87c56e5bd70ccae)
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_SIFT_DOWN_H
10 #define _LIBCPP___ALGORITHM_SIFT_DOWN_H
11 
12 #include <__config>
13 #include <__iterator/iterator_traits.h>
14 #include <utility>
15 #include <type_traits>
16 
17 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
18 #pragma GCC system_header
19 #endif
20 
21 _LIBCPP_PUSH_MACROS
22 #include <__undef_macros>
23 
24 _LIBCPP_BEGIN_NAMESPACE_STD
25 
26 template <class _Compare, class _RandomAccessIterator>
27 _LIBCPP_CONSTEXPR_AFTER_CXX11 void
28 __sift_down(_RandomAccessIterator __first, _RandomAccessIterator /*__last*/,
29             _Compare __comp,
30             typename iterator_traits<_RandomAccessIterator>::difference_type __len,
31             _RandomAccessIterator __start)
32 {
33     typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
34     typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
35     // left-child of __start is at 2 * __start + 1
36     // right-child of __start is at 2 * __start + 2
37     difference_type __child = __start - __first;
38 
39     if (__len < 2 || (__len - 2) / 2 < __child)
40         return;
41 
42     __child = 2 * __child + 1;
43     _RandomAccessIterator __child_i = __first + __child;
44 
45     if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
46         // right-child exists and is greater than left-child
47         ++__child_i;
48         ++__child;
49     }
50 
51     // check if we are in heap-order
52     if (__comp(*__child_i, *__start))
53         // we are, __start is larger than it's largest child
54         return;
55 
56     value_type __top(_VSTD::move(*__start));
57     do
58     {
59         // we are not in heap-order, swap the parent with its largest child
60         *__start = _VSTD::move(*__child_i);
61         __start = __child_i;
62 
63         if ((__len - 2) / 2 < __child)
64             break;
65 
66         // recompute the child based off of the updated parent
67         __child = 2 * __child + 1;
68         __child_i = __first + __child;
69 
70         if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
71             // right-child exists and is greater than left-child
72             ++__child_i;
73             ++__child;
74         }
75 
76         // check if we are in heap-order
77     } while (!__comp(*__child_i, __top));
78     *__start = _VSTD::move(__top);
79 }
80 
81 _LIBCPP_END_NAMESPACE_STD
82 
83 _LIBCPP_POP_MACROS
84 
85 #endif // _LIBCPP___ALGORITHM_SIFT_DOWN_H
86