@@ -673,7 +673,7 @@ impl<T: Ord> BinaryHeap<T> {
673
673
// the hole is filled back at the end of its scope, even on panic.
674
674
// Using a hole reduces the constant factor compared to using swaps,
675
675
// which involves twice as many moves.
676
- fn sift_up ( & mut self , start : usize , pos : usize ) {
676
+ fn sift_up ( & mut self , start : usize , pos : usize ) -> usize {
677
677
unsafe {
678
678
// Take out the value at `pos` and create a hole.
679
679
let mut hole = Hole :: new ( & mut self . data , pos) ;
@@ -685,21 +685,6 @@ impl<T: Ord> BinaryHeap<T> {
685
685
}
686
686
hole. move_to ( parent) ;
687
687
}
688
- }
689
- }
690
-
691
- fn sift_up_ind ( & mut self , start : usize , pos : usize ) -> usize {
692
- unsafe {
693
- // Take out the value at `pos` and create a hole.
694
- let mut hole = Hole :: new ( & mut self . data , pos) ;
695
-
696
- while hole. pos ( ) > start {
697
- let parent = ( hole. pos ( ) - 1 ) / 2 ;
698
- if hole. element ( ) <= hole. get ( parent) {
699
- return hole. pos ( ) ;
700
- }
701
- hole. move_to ( parent) ;
702
- }
703
688
hole. pos ( )
704
689
}
705
690
}
@@ -905,19 +890,6 @@ impl<T: Ord> BinaryHeap<T> {
905
890
}
906
891
}
907
892
908
- impl < T > BinaryHeap < T >
909
- where T : Clone + Ord {
910
- /// kek
911
- #[ unstable( feature = "collection_placement" ,
912
- reason = "placement protocol is subject to change" ,
913
- issue = "30172" ) ]
914
- pub fn place ( & mut self ) -> PlaceIn < T > {
915
- PlaceIn {
916
- heap : self ,
917
- }
918
- }
919
- }
920
-
921
893
/// Hole represents a hole in a slice i.e. an index without valid value
922
894
/// (because it was moved from or duplicated).
923
895
/// In drop, `Hole` will restore the slice by filling the hole
@@ -1222,45 +1194,52 @@ impl<'a, T: 'a + Ord + Copy> Extend<&'a T> for BinaryHeap<T> {
1222
1194
#[ unstable( feature = "collection_placement" ,
1223
1195
reason = "placement protocol is subject to change" ,
1224
1196
issue = "30172" ) ]
1225
- pub struct PlaceIn < ' a , T : ' a >
1197
+ pub struct BinaryHeapPlace < ' a , T : ' a >
1226
1198
where T : Clone + Ord {
1227
- heap : & ' a mut BinaryHeap < T > ,
1199
+ heap : * mut BinaryHeap < T > ,
1200
+ place : vec:: PlaceBack < ' a , T > ,
1228
1201
}
1229
1202
1230
1203
#[ unstable( feature = "collection_placement" ,
1231
1204
reason = "placement protocol is subject to change" ,
1232
1205
issue = "30172" ) ]
1233
- impl < ' a , T > Place < T > for PlaceIn < ' a , T >
1206
+ impl < ' a , T : ' a > Placer < T > for & ' a mut BinaryHeap < T >
1234
1207
where T : Clone + Ord {
1235
- fn pointer ( & mut self ) -> * mut T {
1236
- self . heap . data . place_back ( ) . pointer ( )
1208
+ type Place = BinaryHeapPlace < ' a , T > ;
1209
+
1210
+ fn make_place ( self ) -> Self :: Place {
1211
+ let ptr = self as * mut BinaryHeap < T > ;
1212
+ let place = Placer :: make_place ( self . data . place_back ( ) ) ;
1213
+ BinaryHeapPlace {
1214
+ heap : ptr,
1215
+ place : place,
1216
+ }
1237
1217
}
1238
1218
}
1239
1219
1240
1220
#[ unstable( feature = "collection_placement" ,
1241
1221
reason = "placement protocol is subject to change" ,
1242
1222
issue = "30172" ) ]
1243
- impl < ' a , T > Placer < T > for PlaceIn < ' a , T >
1223
+ impl < ' a , T > Place < T > for BinaryHeapPlace < ' a , T >
1244
1224
where T : Clone + Ord {
1245
- type Place = PlaceIn < ' a , T > ;
1246
-
1247
- fn make_place ( self ) -> Self {
1248
- let _ = self . heap . data . place_back ( ) . make_place ( ) ;
1249
- self
1225
+ fn pointer ( & mut self ) -> * mut T {
1226
+ self . place . pointer ( )
1250
1227
}
1251
1228
}
1252
1229
1253
1230
#[ unstable( feature = "collection_placement" ,
1254
1231
reason = "placement protocol is subject to change" ,
1255
1232
issue = "30172" ) ]
1256
- impl < ' a , T > InPlace < T > for PlaceIn < ' a , T >
1233
+ impl < ' a , T > InPlace < T > for BinaryHeapPlace < ' a , T >
1257
1234
where T : Clone + Ord {
1258
1235
type Owner = & ' a T ;
1259
1236
1260
1237
unsafe fn finalize ( self ) -> & ' a T {
1261
- let len = self . heap . len ( ) ;
1262
- let _ = self . heap . data . place_back ( ) . finalize ( ) ;
1263
- let i = self . heap . sift_up_ind ( 0 , len) ;
1264
- & mut self . heap . data [ i]
1238
+ self . place . finalize ( ) ;
1239
+
1240
+ let heap: & mut BinaryHeap < T > = & mut * self . heap ;
1241
+ let len = heap. len ( ) ;
1242
+ let i = heap. sift_up ( 0 , len - 1 ) ;
1243
+ heap. data . get_unchecked ( i)
1265
1244
}
1266
1245
}
0 commit comments