xref: /llvm-project/libcxx/include/__algorithm/sift_down.h (revision 2b0ec7ca44ea227665f25b7dc3a3c097f16673cb)
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 <__assert>
13 #include <__config>
14 #include <__iterator/iterator_traits.h>
15 #include <__utility/move.h>
16 
17 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
18 #  pragma GCC system_header
19 #endif
20 
21 _LIBCPP_BEGIN_NAMESPACE_STD
22 
23 template <class _Compare, class _RandomAccessIterator>
24 _LIBCPP_CONSTEXPR_AFTER_CXX11 void
25 __sift_down(_RandomAccessIterator __first, _Compare __comp,
26             typename iterator_traits<_RandomAccessIterator>::difference_type __len,
27             _RandomAccessIterator __start)
28 {
29     typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
30     typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
31     // left-child of __start is at 2 * __start + 1
32     // right-child of __start is at 2 * __start + 2
33     difference_type __child = __start - __first;
34 
35     if (__len < 2 || (__len - 2) / 2 < __child)
36         return;
37 
38     __child = 2 * __child + 1;
39     _RandomAccessIterator __child_i = __first + __child;
40 
41     if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + difference_type(1)))) {
42         // right-child exists and is greater than left-child
43         ++__child_i;
44         ++__child;
45     }
46 
47     // check if we are in heap-order
48     if (__comp(*__child_i, *__start))
49         // we are, __start is larger than its largest child
50         return;
51 
52     value_type __top(_VSTD::move(*__start));
53     do
54     {
55         // we are not in heap-order, swap the parent with its largest child
56         *__start = _VSTD::move(*__child_i);
57         __start = __child_i;
58 
59         if ((__len - 2) / 2 < __child)
60             break;
61 
62         // recompute the child based off of the updated parent
63         __child = 2 * __child + 1;
64         __child_i = __first + __child;
65 
66         if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + difference_type(1)))) {
67             // right-child exists and is greater than left-child
68             ++__child_i;
69             ++__child;
70         }
71 
72         // check if we are in heap-order
73     } while (!__comp(*__child_i, __top));
74     *__start = _VSTD::move(__top);
75 }
76 
77 template <class _Compare, class _RandomAccessIterator>
78 _LIBCPP_CONSTEXPR_AFTER_CXX11 _RandomAccessIterator
79 __floyd_sift_down(_RandomAccessIterator __first, _Compare __comp,
80                   typename iterator_traits<_RandomAccessIterator>::difference_type __len)
81 {
82     using difference_type = typename iterator_traits<_RandomAccessIterator>::difference_type;
83     _LIBCPP_ASSERT(__len >= 2, "shouldn't be called unless __len >= 2");
84 
85     _RandomAccessIterator __hole = __first;
86     _RandomAccessIterator __child_i = __first;
87     difference_type __child = 0;
88 
89     while (true) {
90         __child_i += difference_type(__child + 1);
91         __child = 2 * __child + 1;
92 
93         if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + difference_type(1)))) {
94             // right-child exists and is greater than left-child
95             ++__child_i;
96             ++__child;
97         }
98 
99         // swap __hole with its largest child
100         *__hole = std::move(*__child_i);
101         __hole = __child_i;
102 
103         // if __hole is now a leaf, we're done
104         if (__child > (__len - 2) / 2)
105             return __hole;
106     }
107 }
108 
109 _LIBCPP_END_NAMESPACE_STD
110 
111 #endif // _LIBCPP___ALGORITHM_SIFT_DOWN_H
112