about summary refs log tree commit diff
path: root/src/liballoc
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2019-05-10 04:10:36 +0000
committerbors <bors@rust-lang.org>2019-05-10 04:10:36 +0000
commit407536e38db4c7ebd33288db450703a7588e0a9d (patch)
tree0523b1ffe01a4a2c1ca52c60c8423b4ec0777c2a /src/liballoc
parent03bd2f653f0d0adb69d862fbeec64663157e71e1 (diff)
parentadbaf7a9cdc3f67116e2034d0e5e235779dae286 (diff)
downloadrust-407536e38db4c7ebd33288db450703a7588e0a9d.tar.gz
rust-407536e38db4c7ebd33288db450703a7588e0a9d.zip
Auto merge of #60451 - rasendubi:binaryheap-min-heap, r=scottmcm
BinaryHeap: add min-heap example

Fixes #58174.
Diffstat (limited to 'src/liballoc')
-rw-r--r--src/liballoc/collections/binary_heap.rs24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/liballoc/collections/binary_heap.rs b/src/liballoc/collections/binary_heap.rs
index 8c142a3d317..c355361b53c 100644
--- a/src/liballoc/collections/binary_heap.rs
+++ b/src/liballoc/collections/binary_heap.rs
@@ -207,6 +207,30 @@ use super::SpecExtend;
 /// // The heap should now be empty.
 /// assert!(heap.is_empty())
 /// ```
+///
+/// ## Min-heap
+///
+/// Either `std::cmp::Reverse` or a custom `Ord` implementation can be used to
+/// make `BinaryHeap` a min-heap. This makes `heap.pop()` return the smallest
+/// value instead of the greatest one.
+///
+/// ```
+/// use std::collections::BinaryHeap;
+/// use std::cmp::Reverse;
+///
+/// let mut heap = BinaryHeap::new();
+///
+/// // Wrap values in `Reverse`
+/// heap.push(Reverse(1));
+/// heap.push(Reverse(5));
+/// heap.push(Reverse(2));
+///
+/// // If we pop these scores now, they should come back in the reverse order.
+/// assert_eq!(heap.pop(), Some(Reverse(1)));
+/// assert_eq!(heap.pop(), Some(Reverse(2)));
+/// assert_eq!(heap.pop(), Some(Reverse(5)));
+/// assert_eq!(heap.pop(), None);
+/// ```
 #[stable(feature = "rust1", since = "1.0.0")]
 pub struct BinaryHeap<T> {
     data: Vec<T>,