Move contructors to the top of PriorityQueue.
This commit is contained in:
parent
9e2bb9d67b
commit
87ef2f390b
1 changed files with 19 additions and 19 deletions
|
|
@ -180,6 +180,25 @@ impl<T: Ord> Default for PriorityQueue<T> {
|
|||
}
|
||||
|
||||
impl<T: Ord> PriorityQueue<T> {
|
||||
/// Create an empty PriorityQueue
|
||||
pub fn new() -> PriorityQueue<T> { PriorityQueue{data: vec!(),} }
|
||||
|
||||
/// Create an empty PriorityQueue with capacity `capacity`
|
||||
pub fn with_capacity(capacity: uint) -> PriorityQueue<T> {
|
||||
PriorityQueue { data: Vec::with_capacity(capacity) }
|
||||
}
|
||||
|
||||
/// Create a PriorityQueue from a vector (heapify)
|
||||
pub fn from_vec(xs: Vec<T>) -> PriorityQueue<T> {
|
||||
let mut q = PriorityQueue{data: xs,};
|
||||
let mut n = q.len() / 2;
|
||||
while n > 0 {
|
||||
n -= 1;
|
||||
q.siftdown(n)
|
||||
}
|
||||
q
|
||||
}
|
||||
|
||||
/// An iterator visiting all values in underlying vector, in
|
||||
/// arbitrary order.
|
||||
pub fn iter<'a>(&'a self) -> Items<'a, T> {
|
||||
|
|
@ -278,25 +297,6 @@ impl<T: Ord> PriorityQueue<T> {
|
|||
q.into_vec()
|
||||
}
|
||||
|
||||
/// Create an empty PriorityQueue
|
||||
pub fn new() -> PriorityQueue<T> { PriorityQueue{data: vec!(),} }
|
||||
|
||||
/// Create an empty PriorityQueue with capacity `capacity`
|
||||
pub fn with_capacity(capacity: uint) -> PriorityQueue<T> {
|
||||
PriorityQueue { data: Vec::with_capacity(capacity) }
|
||||
}
|
||||
|
||||
/// Create a PriorityQueue from a vector (heapify)
|
||||
pub fn from_vec(xs: Vec<T>) -> PriorityQueue<T> {
|
||||
let mut q = PriorityQueue{data: xs,};
|
||||
let mut n = q.len() / 2;
|
||||
while n > 0 {
|
||||
n -= 1;
|
||||
q.siftdown(n)
|
||||
}
|
||||
q
|
||||
}
|
||||
|
||||
// The implementations of siftup and siftdown use unsafe blocks in
|
||||
// order to move an element out of the vector (leaving behind a
|
||||
// zeroed element), shift along the others and move it back into the
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue