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