You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/// An efficient heapsort requires that the heap ordering is the inverse of the desired sort order
96
-
/// So the basic case of retrieving minimum elements requires a max heap
97
-
///
98
-
/// This type does not attempt to reproduce all the functionality of [std::collections::BinaryHeap] and instead only implements what is needed for iter operations,
99
-
/// e.g. we do not need to insert single elements.
100
-
/// Additionally, some minor optimizations used in the std BinaryHeap are not used here, e.g. elements are actually swapped rather than managing a "hole"
101
-
structMaxHeap<'a,T,C>{
102
-
// It may be not be possible to shrink the storage for smaller sequencess
103
-
// so manually manage the initialization
104
-
// This is **assumed not to be empty**
105
-
storage:&'amut[T],
106
-
comparator:C,
107
-
// this is always less or equal to the count of actual elements
108
-
// allowing it to be less means the heap property can cover only a subset of the vec
109
-
// while reusing the storage
110
-
len:usize,
111
-
}
90
+
/// Sift the element currently at `origin` **away** from the root until it is properly ordered
91
+
fnsift_down<T>(
92
+
heap:&mut[T],
93
+
comparator:&mutimplFnMut(&T,&T) -> Ordering,
94
+
mutorigin:usize,
95
+
){
96
+
fnchildren_of(n:usize) -> (usize,usize){
97
+
(2* n + 1,2* n + 2)
98
+
}
112
99
113
-
impl<'a,T,C>MaxHeap<'a,T,C>
114
-
where
115
-
C:FnMut(&T,&T) -> Ordering,
116
-
{
117
-
/// Sift the element currently at `origin` **away** from the root until it is properly ordered
118
-
fnsift_down(&mutself,mutorigin:usize){
119
-
fnchildren_of(n:usize) -> (usize,usize){
120
-
(2* n + 1,2* n + 2)
100
+
while origin < heap.len(){
101
+
let(left_idx, right_idx) = children_of(origin);
102
+
if left_idx >= heap.len(){
103
+
return;
121
104
}
122
105
123
-
while origin < self.len{
124
-
let(left_idx, right_idx) = children_of(origin);
125
-
126
-
if left_idx >= self.len{
127
-
// the left is the earlier child, so if it doesn't exist there's nothing to swap with
0 commit comments