|
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;
|
@@ -688,6 +688,22 @@ impl<T: Ord> BinaryHeap<T> {
|
688 | 688 | }
|
689 | 689 | }
|
690 | 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 | + hole.pos() |
| 704 | + } |
| 705 | + } |
| 706 | + |
691 | 707 | /// Take an element at `pos` and move it down the heap,
|
692 | 708 | /// while its children are larger.
|
693 | 709 | fn sift_down_range(&mut self, pos: usize, end: usize) {
|
@@ -889,6 +905,19 @@ impl<T: Ord> BinaryHeap<T> {
|
889 | 905 | }
|
890 | 906 | }
|
891 | 907 |
|
| 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 | + |
892 | 921 | /// Hole represents a hole in a slice i.e. an index without valid value
|
893 | 922 | /// (because it was moved from or duplicated).
|
894 | 923 | /// In drop, `Hole` will restore the slice by filling the hole
|
@@ -1189,3 +1218,49 @@ impl<'a, T: 'a + Ord + Copy> Extend<&'a T> for BinaryHeap<T> {
|
1189 | 1218 | self.extend(iter.into_iter().cloned());
|
1190 | 1219 | }
|
1191 | 1220 | }
|
| 1221 | + |
| 1222 | +#[unstable(feature = "collection_placement", |
| 1223 | + reason = "placement protocol is subject to change", |
| 1224 | + issue = "30172")] |
| 1225 | +pub struct PlaceIn<'a, T: 'a> |
| 1226 | +where T: Clone + Ord { |
| 1227 | + heap: &'a mut BinaryHeap<T>, |
| 1228 | +} |
| 1229 | + |
| 1230 | +#[unstable(feature = "collection_placement", |
| 1231 | + reason = "placement protocol is subject to change", |
| 1232 | + issue = "30172")] |
| 1233 | +impl<'a, T> Place<T> for PlaceIn<'a, T> |
| 1234 | +where T: Clone + Ord { |
| 1235 | + fn pointer(&mut self) -> *mut T { |
| 1236 | + self.heap.data.place_back().pointer() |
| 1237 | + } |
| 1238 | +} |
| 1239 | + |
| 1240 | +#[unstable(feature = "collection_placement", |
| 1241 | + reason = "placement protocol is subject to change", |
| 1242 | + issue = "30172")] |
| 1243 | +impl<'a, T> Placer<T> for PlaceIn<'a, T> |
| 1244 | +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 |
| 1250 | + } |
| 1251 | +} |
| 1252 | + |
| 1253 | +#[unstable(feature = "collection_placement", |
| 1254 | + reason = "placement protocol is subject to change", |
| 1255 | + issue = "30172")] |
| 1256 | +impl<'a, T> InPlace<T> for PlaceIn<'a, T> |
| 1257 | +where T: Clone + Ord { |
| 1258 | + type Owner = &'a T; |
| 1259 | + |
| 1260 | + 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] |
| 1265 | + } |
| 1266 | +} |
0 commit comments