151
151
#![ allow( missing_docs) ]
152
152
#![ stable( feature = "rust1" , since = "1.0.0" ) ]
153
153
154
- use core:: ops:: { Deref , DerefMut } ;
154
+ use core:: ops:: { Deref , DerefMut , Place , Placer , InPlace } ;
155
155
use core:: iter:: { FromIterator , FusedIterator } ;
156
156
use core:: mem:: { swap, size_of} ;
157
157
use core:: ptr;
@@ -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,6 +685,7 @@ impl<T: Ord> BinaryHeap<T> {
685
685
}
686
686
hole. move_to ( parent) ;
687
687
}
688
+ hole. pos ( )
688
689
}
689
690
}
690
691
@@ -1189,3 +1190,56 @@ impl<'a, T: 'a + Ord + Copy> Extend<&'a T> for BinaryHeap<T> {
1189
1190
self . extend ( iter. into_iter ( ) . cloned ( ) ) ;
1190
1191
}
1191
1192
}
1193
+
1194
+ #[ unstable( feature = "collection_placement" ,
1195
+ reason = "placement protocol is subject to change" ,
1196
+ issue = "30172" ) ]
1197
+ pub struct BinaryHeapPlace < ' a , T : ' a >
1198
+ where T : Clone + Ord {
1199
+ heap : * mut BinaryHeap < T > ,
1200
+ place : vec:: PlaceBack < ' a , T > ,
1201
+ }
1202
+
1203
+ #[ unstable( feature = "collection_placement" ,
1204
+ reason = "placement protocol is subject to change" ,
1205
+ issue = "30172" ) ]
1206
+ impl < ' a , T : ' a > Placer < T > for & ' a mut BinaryHeap < T >
1207
+ where T : Clone + Ord {
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
+ }
1217
+ }
1218
+ }
1219
+
1220
+ #[ unstable( feature = "collection_placement" ,
1221
+ reason = "placement protocol is subject to change" ,
1222
+ issue = "30172" ) ]
1223
+ impl < ' a , T > Place < T > for BinaryHeapPlace < ' a , T >
1224
+ where T : Clone + Ord {
1225
+ fn pointer ( & mut self ) -> * mut T {
1226
+ self . place . pointer ( )
1227
+ }
1228
+ }
1229
+
1230
+ #[ unstable( feature = "collection_placement" ,
1231
+ reason = "placement protocol is subject to change" ,
1232
+ issue = "30172" ) ]
1233
+ impl < ' a , T > InPlace < T > for BinaryHeapPlace < ' a , T >
1234
+ where T : Clone + Ord {
1235
+ type Owner = & ' a T ;
1236
+
1237
+ unsafe fn finalize ( self ) -> & ' a T {
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)
1244
+ }
1245
+ }
0 commit comments