@@ -648,6 +648,42 @@ impl<T: Ord> BinaryHeap<T> {
648
648
self . extend ( other. drain ( ) ) ;
649
649
}
650
650
}
651
+
652
+ /// Returns an iterator which retrieves elements in heap order.
653
+ /// The retrieved elements will be removed from the original heap.
654
+ /// The remaining elements are removed on drop in heap order.
655
+ ///
656
+ /// Note:
657
+ /// * `.drain_sorted()` is O(n lg n); much slower than `.drain()`.
658
+ ///
659
+ /// # Examples
660
+ ///
661
+ /// Basic usage:
662
+ ///
663
+ /// ```
664
+ /// #![feature(binary_heap_drain_sorted)]
665
+ /// use std::collections::BinaryHeap;
666
+ ///
667
+ /// let mut heap = BinaryHeap::from(vec![1, 2, 3, 4, 5]);
668
+ /// assert_eq!(heap.len(), 5);
669
+ ///
670
+ /// let removed = heap.drain_sorted()
671
+ /// .take(3).collect::<Vec<_>>(); // removes 3 elements in heap order
672
+ ///
673
+ /// assert_eq!(removed, vec![5, 4, 3]);
674
+ /// assert_eq!(heap.len(), 2);
675
+ ///
676
+ /// drop(drain_sorted); // removes remaining elements in heap order
677
+ ///
678
+ /// assert_eq!(heap.len(), 0);
679
+ /// ```
680
+ #[ inline]
681
+ #[ unstable( feature = "binary_heap_drain_sorted" , issue = "59278" ) ]
682
+ pub fn drain_sorted ( & mut self ) -> DrainSorted < ' _ , T > {
683
+ DrainSorted {
684
+ inner : self ,
685
+ }
686
+ }
651
687
}
652
688
653
689
impl < T > BinaryHeap < T > {
@@ -920,47 +956,6 @@ impl<T> BinaryHeap<T> {
920
956
Drain { iter : self . data . drain ( ..) }
921
957
}
922
958
923
- /// Returns an iterator which retrieves elements in heap order.
924
- /// The retrieved elements will be removed from the original heap.
925
- ///
926
- /// Note:
927
- /// * Unlike other `.drain()` methods, this method removes elements *lazily*.
928
- /// In order to remove elements in heap order, you need to retrieve elements explicitly.
929
- /// * The remaining elements are removed on drop in arbitrary order.
930
- ///
931
- /// # Examples
932
- ///
933
- /// Basic usage:
934
- ///
935
- /// ```
936
- /// #![feature(binary_heap_drain_sorted)]
937
- /// use std::collections::BinaryHeap;
938
- ///
939
- /// let mut heap = BinaryHeap::from(vec![1, 2, 3, 4, 5]);
940
- /// assert_eq!(heap.len(), 5);
941
- ///
942
- /// let len = heap.len();
943
- /// let removed = heap.drain_sorted()
944
- /// .take(len).collect::<Vec<_>>(); // removes all elements in *heap* order
945
- /// assert_eq!(removed, vec![5, 4, 3, 2, 1]);
946
- /// assert_eq!(heap.len(), 0);
947
- ///
948
- ///
949
- /// let mut heap = BinaryHeap::from(vec![1, 2, 3, 4, 5]);
950
- /// assert_eq!(heap.len(), 5);
951
- ///
952
- /// let drain_sorted = heap.drain_sorted();
953
- /// drop(drain_sorted); // removes all elements in *arbitrary* order
954
- /// assert_eq!(heap.len(), 0);
955
- /// ```
956
- #[ inline]
957
- #[ unstable( feature = "binary_heap_drain_sorted" , issue = "59278" ) ]
958
- pub fn drain_sorted ( & mut self ) -> DrainSorted < ' _ , T > {
959
- DrainSorted {
960
- inner : self ,
961
- }
962
- }
963
-
964
959
/// Drops all items from the binary heap.
965
960
///
966
961
/// # Examples
@@ -1263,15 +1258,15 @@ impl<T> FusedIterator for Drain<'_, T> {}
1263
1258
/// [`BinaryHeap`]: struct.BinaryHeap.html
1264
1259
#[ unstable( feature = "binary_heap_drain_sorted" , issue = "59278" ) ]
1265
1260
#[ derive( Debug ) ]
1266
- pub struct DrainSorted < ' a , T > {
1261
+ pub struct DrainSorted < ' a , T : Ord > {
1267
1262
inner : & ' a mut BinaryHeap < T > ,
1268
1263
}
1269
1264
1270
1265
#[ unstable( feature = "binary_heap_drain_sorted" , issue = "59278" ) ]
1271
- impl < ' a , T > Drop for DrainSorted < ' a , T > {
1272
- /// Removes heap elements in arbitrary order for efficiency .
1266
+ impl < ' a , T : Ord > Drop for DrainSorted < ' a , T > {
1267
+ /// Removes heap elements in heap order.
1273
1268
fn drop ( & mut self ) {
1274
- self . inner . drain ( ) ;
1269
+ while let Some ( _ ) = self . inner . pop ( ) { }
1275
1270
}
1276
1271
}
1277
1272
0 commit comments