@@ -938,7 +938,14 @@ impl<T, C: Compare<T>> BinaryHeap<T, C> {
938
938
let mut end = self . len ( ) ;
939
939
while end > 1 {
940
940
end -= 1 ;
941
- self . data . swap ( 0 , end) ;
941
+ // SAFETY: `end` goes from `self.len() - 1` to 1 (both included),
942
+ // so it's always a valid index to access.
943
+ // It is safe to access index 0 (i.e. `ptr`), because
944
+ // 1 <= end < self.len(), which means self.len() >= 2.
945
+ unsafe {
946
+ let ptr = self . data . as_mut_ptr ( ) ;
947
+ ptr:: swap ( ptr, ptr. add ( end) ) ;
948
+ }
942
949
self . sift_down_range ( 0 , end) ;
943
950
}
944
951
self . into_vec ( )
@@ -975,23 +982,24 @@ impl<T, C: Compare<T>> BinaryHeap<T, C> {
975
982
unsafe {
976
983
let mut hole = Hole :: new ( & mut self . data , pos) ;
977
984
let mut child = 2 * pos + 1 ;
978
- while child < end {
979
- let right = child + 1 ;
985
+ while child < end - 1 {
980
986
// compare with the greater of the two children
981
- // if right < end && !(hole.get(child) > hole.get(right)) {
982
- if right < end
983
- && self . cmp . compare ( hole. get ( child) , hole. get ( right) ) != Ordering :: Greater
984
- {
985
- child = right;
986
- }
987
+ // if !(hole.get(child) > hole.get(child + 1)) { child += 1 }
988
+ child += ( self . cmp . compare ( hole. get ( child) , hole. get ( child + 1 ) )
989
+ != Ordering :: Greater ) as usize ;
987
990
// if we are already in order, stop.
988
991
// if hole.element() >= hole.get(child) {
989
992
if self . cmp . compare ( hole. element ( ) , hole. get ( child) ) != Ordering :: Less {
990
- break ;
993
+ return ;
991
994
}
992
995
hole. move_to ( child) ;
993
996
child = 2 * hole. pos ( ) + 1 ;
994
997
}
998
+ if child == end - 1
999
+ && self . cmp . compare ( hole. element ( ) , hole. get ( child) ) == Ordering :: Less
1000
+ {
1001
+ hole. move_to ( child) ;
1002
+ }
995
1003
}
996
1004
}
997
1005
@@ -1011,18 +1019,18 @@ impl<T, C: Compare<T>> BinaryHeap<T, C> {
1011
1019
unsafe {
1012
1020
let mut hole = Hole :: new ( & mut self . data , pos) ;
1013
1021
let mut child = 2 * pos + 1 ;
1014
- while child < end {
1022
+ while child < end - 1 {
1015
1023
let right = child + 1 ;
1016
1024
// compare with the greater of the two children
1017
- // if right < end && !(hole.get(child) > hole.get(right)) {
1018
- if right < end
1019
- && self . cmp . compare ( hole. get ( child) , hole. get ( right) ) != Ordering :: Greater
1020
- {
1021
- child = right;
1022
- }
1025
+ // if !(hole.get(child) > hole.get(right)) { child += 1 }
1026
+ child += ( self . cmp . compare ( hole. get ( child) , hole. get ( right) ) != Ordering :: Greater )
1027
+ as usize ;
1023
1028
hole. move_to ( child) ;
1024
1029
child = 2 * hole. pos ( ) + 1 ;
1025
1030
}
1031
+ if child == end - 1 {
1032
+ hole. move_to ( child) ;
1033
+ }
1026
1034
pos = hole. pos ;
1027
1035
}
1028
1036
self . sift_up ( start, pos) ;
0 commit comments