//! A priority queue implemented with a binary heap.
//!
+//! Insertions have `O(log n)` time complexity and checking or popping the largest element is
+//! `O(1)`. Converting a vector to a priority queue can be done in-place, and has `O(n)`
+//! complexity. A priority queue can also be converted to a sorted vector in-place, allowing it to
+//! be used for an `O(n log n)` in-place heapsort.
+//!
//! # Example
//!
//! This is a larger example which implements [Dijkstra's algorithm][dijkstra]
}
impl<T: Ord> Collection for PriorityQueue<T> {
- /// Returns the length of the queue
+ /// Returns the length of the queue.
fn len(&self) -> uint { self.data.len() }
}
impl<T: Ord> Mutable for PriorityQueue<T> {
- /// Drop all items from the queue
+ /// Drops all items from the queue.
fn clear(&mut self) { self.data.truncate(0) }
}
}
impl<T: Ord> PriorityQueue<T> {
- /// Create an empty PriorityQueue as a max-heap.
+ /// Creates an empty `PriorityQueue` as a max-heap.
///
/// # Example
///
/// ```
pub fn new() -> PriorityQueue<T> { PriorityQueue{data: vec!(),} }
- /// Create an empty PriorityQueue with a specific capacity.
+ /// Creates an empty `PriorityQueue` with a specific capacity.
/// This preallocates enough memory for `capacity` elements,
- /// so that the PriorityQueue does not have to be reallocated
+ /// so that the `PriorityQueue` does not have to be reallocated
/// until it contains at least that many values.
///
/// # Example
PriorityQueue { data: Vec::with_capacity(capacity) }
}
- /// Create a PriorityQueue from a vector. This is sometimes called
+ /// Creates a `PriorityQueue` from a vector. This is sometimes called
/// `heapifying` the vector.
///
/// # Example
Items { iter: self.data.iter() }
}
- /// Returns the greatest item in a queue or `None` if it is empty.
+ /// Returns the greatest item in a queue, or `None` if it is empty.
///
/// # Example
///
/// ```
pub fn capacity(&self) -> uint { self.data.capacity() }
- /// Reserve capacity for exactly `n` elements in the PriorityQueue.
+ /// Reserves capacity for exactly `n` elements in the `PriorityQueue`.
/// Do nothing if the capacity is already sufficient.
///
/// # Example
/// ```
pub fn reserve_exact(&mut self, n: uint) { self.data.reserve_exact(n) }
- /// Reserve capacity for at least `n` elements in the PriorityQueue.
+ /// Reserves capacity for at least `n` elements in the `PriorityQueue`.
/// Do nothing if the capacity is already sufficient.
///
/// # Example
self.data.reserve(n)
}
- /// Remove the greatest item from a queue and return it, or `None` if it is
- /// empty.
+ /// Removes the greatest item from a queue and returns it, or `None` if it
+ /// is empty.
///
/// # Example
///
#[deprecated="renamed to `pop`"]
pub fn maybe_pop(&mut self) -> Option<T> { self.pop() }
- /// Push an item onto the queue.
+ /// Pushes an item onto the queue.
///
/// # Example
///
self.siftup(0, new_len);
}
- /// Optimized version of a push followed by a pop.
+ /// Pushes an item onto a queue then pops the greatest item off the queue in
+ /// an optimized fashion.
///
/// # Example
///
item
}
- /// Optimized version of a pop followed by a push. The push is done
- /// regardless of whether the queue is empty.
+ /// Pops the greatest item off a queue then pushes an item onto the queue in
+ /// an optimized fashion. The push is done regardless of whether the queue
+ /// was empty.
///
/// # Example
///
#[deprecated="renamed to `into_sorted_vec`"]
fn to_sorted_vec(self) -> Vec<T> { self.into_sorted_vec() }
- /// Consume the PriorityQueue and return the underlying vector
+ /// Consumes the `PriorityQueue` and returns the underlying vector
/// in arbitrary order.
///
/// # Example
/// ```
pub fn into_vec(self) -> Vec<T> { let PriorityQueue{data: v} = self; v }
- /// Consume the PriorityQueue and return a vector in sorted
+ /// Consumes the `PriorityQueue` and returns a vector in sorted
/// (ascending) order.
///
/// # Example
}
}
-/// PriorityQueue iterator.
-pub struct Items <'a, T> {
+/// `PriorityQueue` iterator.
+pub struct Items <'a, T:'a> {
iter: slice::Items<'a, T>,
}