Skip to content

Commit 30e8f65

Browse files
committed
Simplify .drain_sorted() and its doc.
1 parent 2dbf268 commit 30e8f65

File tree

1 file changed

+40
-45
lines changed

1 file changed

+40
-45
lines changed

src/liballoc/collections/binary_heap.rs

+40-45
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,42 @@ impl<T: Ord> BinaryHeap<T> {
648648
self.extend(other.drain());
649649
}
650650
}
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+
}
651687
}
652688

653689
impl<T> BinaryHeap<T> {
@@ -920,47 +956,6 @@ impl<T> BinaryHeap<T> {
920956
Drain { iter: self.data.drain(..) }
921957
}
922958

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-
964959
/// Drops all items from the binary heap.
965960
///
966961
/// # Examples
@@ -1263,15 +1258,15 @@ impl<T> FusedIterator for Drain<'_, T> {}
12631258
/// [`BinaryHeap`]: struct.BinaryHeap.html
12641259
#[unstable(feature = "binary_heap_drain_sorted", issue = "59278")]
12651260
#[derive(Debug)]
1266-
pub struct DrainSorted<'a, T> {
1261+
pub struct DrainSorted<'a, T: Ord> {
12671262
inner: &'a mut BinaryHeap<T>,
12681263
}
12691264

12701265
#[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.
12731268
fn drop(&mut self) {
1274-
self.inner.drain();
1269+
while let Some(_) = self.inner.pop() {}
12751270
}
12761271
}
12771272

0 commit comments

Comments
 (0)