priority_queue: fix siftup/siftdown naming
This commit is contained in:
parent
7bd0d71554
commit
04df723619
1 changed files with 11 additions and 11 deletions
|
@ -39,7 +39,7 @@ impl <T: Copy Ord> PriorityQueue<T> {
|
||||||
/// Pop the greatest item from the queue - fails if empty
|
/// Pop the greatest item from the queue - fails if empty
|
||||||
fn pop(&mut self) -> T {
|
fn pop(&mut self) -> T {
|
||||||
let mut item = self.data.pop();
|
let mut item = self.data.pop();
|
||||||
if self.is_not_empty() { item <-> self.data[0]; self.siftup(0); }
|
if self.is_not_empty() { item <-> self.data[0]; self.siftdown(0); }
|
||||||
item
|
item
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -51,7 +51,7 @@ impl <T: Copy Ord> PriorityQueue<T> {
|
||||||
/// Push an item onto the queue
|
/// Push an item onto the queue
|
||||||
fn push(&mut self, item: T) {
|
fn push(&mut self, item: T) {
|
||||||
self.data.push(item);
|
self.data.push(item);
|
||||||
self.siftdown(0, self.len() - 1);
|
self.siftup(0, self.len() - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Optimized version of a push followed by a pop
|
/// Optimized version of a push followed by a pop
|
||||||
|
@ -59,7 +59,7 @@ impl <T: Copy Ord> PriorityQueue<T> {
|
||||||
let mut item = item;
|
let mut item = item;
|
||||||
if self.is_not_empty() && self.data[0] > item {
|
if self.is_not_empty() && self.data[0] > item {
|
||||||
item <-> self.data[0];
|
item <-> self.data[0];
|
||||||
self.siftup(0);
|
self.siftdown(0);
|
||||||
}
|
}
|
||||||
item
|
item
|
||||||
}
|
}
|
||||||
|
@ -68,7 +68,7 @@ impl <T: Copy Ord> PriorityQueue<T> {
|
||||||
fn replace(&mut self, item: T) -> T {
|
fn replace(&mut self, item: T) -> T {
|
||||||
let mut item = item;
|
let mut item = item;
|
||||||
item <-> self.data[0];
|
item <-> self.data[0];
|
||||||
self.siftup(0);
|
self.siftdown(0);
|
||||||
item
|
item
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,7 +82,7 @@ impl <T: Copy Ord> PriorityQueue<T> {
|
||||||
while end > 1 {
|
while end > 1 {
|
||||||
end -= 1;
|
end -= 1;
|
||||||
q.data[end] <-> q.data[0];
|
q.data[end] <-> q.data[0];
|
||||||
unsafe { q.siftup_range(0, end) } // purity-checking workaround
|
unsafe { q.siftdown_range(0, end) } // purity-checking workaround
|
||||||
}
|
}
|
||||||
q.to_vec()
|
q.to_vec()
|
||||||
}
|
}
|
||||||
|
@ -92,12 +92,12 @@ impl <T: Copy Ord> PriorityQueue<T> {
|
||||||
let mut n = q.len() / 2;
|
let mut n = q.len() / 2;
|
||||||
while n > 0 {
|
while n > 0 {
|
||||||
n -= 1;
|
n -= 1;
|
||||||
unsafe { q.siftup(n) }; // purity-checking workaround
|
unsafe { q.siftdown(n) }; // purity-checking workaround
|
||||||
}
|
}
|
||||||
q
|
q
|
||||||
}
|
}
|
||||||
|
|
||||||
priv fn siftdown(&mut self, startpos: uint, pos: uint) {
|
priv fn siftup(&mut self, startpos: uint, pos: uint) {
|
||||||
let mut pos = pos;
|
let mut pos = pos;
|
||||||
let newitem = self.data[pos];
|
let newitem = self.data[pos];
|
||||||
|
|
||||||
|
@ -114,7 +114,7 @@ impl <T: Copy Ord> PriorityQueue<T> {
|
||||||
self.data[pos] = newitem;
|
self.data[pos] = newitem;
|
||||||
}
|
}
|
||||||
|
|
||||||
priv fn siftup_range(&mut self, pos: uint, endpos: uint) {
|
priv fn siftdown_range(&mut self, pos: uint, endpos: uint) {
|
||||||
let mut pos = pos;
|
let mut pos = pos;
|
||||||
let startpos = pos;
|
let startpos = pos;
|
||||||
let newitem = self.data[pos];
|
let newitem = self.data[pos];
|
||||||
|
@ -131,11 +131,11 @@ impl <T: Copy Ord> PriorityQueue<T> {
|
||||||
childpos = 2 * pos + 1;
|
childpos = 2 * pos + 1;
|
||||||
}
|
}
|
||||||
self.data[pos] = newitem;
|
self.data[pos] = newitem;
|
||||||
self.siftdown(startpos, pos);
|
self.siftup(startpos, pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
priv fn siftup(&mut self, pos: uint) {
|
priv fn siftdown(&mut self, pos: uint) {
|
||||||
self.siftup_range(pos, self.len());
|
self.siftdown_range(pos, self.len());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue