-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add missing documentation examples for BinaryHeap. #32137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bors
merged 3 commits into
rust-lang:master
from
nathankleyn:improve-docs-for-binaryheap
Mar 12, 2016
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -167,6 +167,49 @@ use vec::{self, Vec}; | |
/// item's ordering relative to any other item, as determined by the `Ord` | ||
/// trait, changes while it is in the heap. This is normally only possible | ||
/// through `Cell`, `RefCell`, global state, I/O, or unsafe code. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use std::collections::BinaryHeap; | ||
/// | ||
/// // type inference lets us omit an explicit type signature (which | ||
/// // would be `BinaryHeap<i32>` in this example). | ||
/// let mut heap = BinaryHeap::new(); | ||
/// | ||
/// // We can use peek to look at the next item in the heap. In this case, | ||
/// // there's no items in there yet so we get None. | ||
/// assert_eq!(heap.peek(), None); | ||
/// | ||
/// // Let's add some scores... | ||
/// heap.push(1); | ||
/// heap.push(5); | ||
/// heap.push(2); | ||
/// | ||
/// // Now peek shows the most important item in the heap. | ||
/// assert_eq!(heap.peek(), Some(&5)); | ||
/// | ||
/// // We can check the length of a heap. | ||
/// assert_eq!(heap.len(), 3); | ||
/// | ||
/// // We can iterate over the items in the heap, although they are returned in | ||
/// // a random order. | ||
/// for x in heap.iter() { | ||
/// println!("{}", x); | ||
/// } | ||
/// | ||
/// // If we instead pop these scores, they should come back in order. | ||
/// assert_eq!(heap.pop(), Some(5)); | ||
/// assert_eq!(heap.pop(), Some(2)); | ||
/// assert_eq!(heap.pop(), Some(1)); | ||
/// assert_eq!(heap.pop(), None); | ||
/// | ||
/// // We can clear the heap of any remaining items. | ||
/// heap.clear(); | ||
/// | ||
/// // The heap should now be empty. | ||
/// assert!(heap.is_empty()) | ||
/// ``` | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub struct BinaryHeap<T> { | ||
data: Vec<T>, | ||
|
@@ -331,6 +374,17 @@ impl<T: Ord> BinaryHeap<T> { | |
} | ||
|
||
/// Discards as much additional capacity as possible. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use std::collections::BinaryHeap; | ||
/// let mut heap: BinaryHeap<i32> = BinaryHeap::with_capacity(100); | ||
/// | ||
/// assert!(heap.capacity() >= 100); | ||
/// heap.shrink_to_fit(); | ||
/// assert!(heap.capacity() == 0); | ||
/// ``` | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub fn shrink_to_fit(&mut self) { | ||
self.data.shrink_to_fit(); | ||
|
@@ -571,12 +625,36 @@ impl<T: Ord> BinaryHeap<T> { | |
} | ||
|
||
/// Returns the length of the binary heap. | ||
/// | ||
/// # Examples | ||
/// | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and here, etc There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed! |
||
/// ``` | ||
/// use std::collections::BinaryHeap; | ||
/// let mut heap = BinaryHeap::from(vec![1, 3]); | ||
/// | ||
/// assert_eq!(heap.len(), 2); | ||
/// ``` | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub fn len(&self) -> usize { | ||
self.data.len() | ||
} | ||
|
||
/// Checks if the binary heap is empty. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use std::collections::BinaryHeap; | ||
/// let mut heap = BinaryHeap::new(); | ||
/// | ||
/// assert!(heap.is_empty()); | ||
/// | ||
/// heap.push(3); | ||
/// heap.push(5); | ||
/// heap.push(1); | ||
/// | ||
/// assert!(!heap.is_empty()); | ||
/// ``` | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub fn is_empty(&self) -> bool { | ||
self.len() == 0 | ||
|
@@ -585,13 +663,41 @@ impl<T: Ord> BinaryHeap<T> { | |
/// Clears the binary heap, returning an iterator over the removed elements. | ||
/// | ||
/// The elements are removed in arbitrary order. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use std::collections::BinaryHeap; | ||
/// let mut heap = BinaryHeap::from(vec![1, 3]); | ||
/// | ||
/// assert!(!heap.is_empty()); | ||
/// | ||
/// for x in heap.drain() { | ||
/// println!("{}", x); | ||
/// } | ||
/// | ||
/// assert!(heap.is_empty()); | ||
/// ``` | ||
#[inline] | ||
#[stable(feature = "drain", since = "1.6.0")] | ||
pub fn drain(&mut self) -> Drain<T> { | ||
Drain { iter: self.data.drain(..) } | ||
} | ||
|
||
/// Drops all items from the binary heap. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use std::collections::BinaryHeap; | ||
/// let mut heap = BinaryHeap::from(vec![1, 3]); | ||
/// | ||
/// assert!(!heap.is_empty()); | ||
/// | ||
/// heap.clear(); | ||
/// | ||
/// assert!(heap.is_empty()); | ||
/// ``` | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub fn clear(&mut self) { | ||
self.drain(); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same comment about basic usage from the other PR here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed!