1
Fork 0

add a Mutable container trait with clear

This commit is contained in:
Daniel Micay 2013-01-21 17:25:57 -05:00
parent 66e50892c1
commit ffb9049274
4 changed files with 68 additions and 14 deletions

View file

@ -10,6 +10,7 @@
//! A priority queue implemented with a binary heap
use core::container::Mutable;
use core::cmp::Ord;
use core::prelude::*;
use core::ptr::addr_of;
@ -24,6 +25,11 @@ pub struct PriorityQueue <T: Ord>{
priv data: ~[T],
}
impl <T: Ord> PriorityQueue<T>: Mutable {
/// Drop all items from the queue
fn clear(&mut self) { self.data.truncate(0) }
}
impl <T: Ord> PriorityQueue<T> {
/// Returns the greatest item in the queue - fails if empty
pure fn top(&self) -> &self/T { &self.data[0] }
@ -51,9 +57,6 @@ impl <T: Ord> PriorityQueue<T> {
vec::reserve_at_least(&mut self.data, n)
}
/// Drop all items from the queue
fn clear(&mut self) { self.data.truncate(0) }
/// Pop the greatest item from the queue - fails if empty
fn pop(&mut self) -> T {
let mut item = self.data.pop();