extra: Add with_capacity to PriorityQueue and SmallIntMap

This commit is contained in:
Erick Tryzelaar 2014-04-03 20:47:53 -07:00 committed by Alex Crichton
parent 8057faa2d3
commit 58ac1c3563
2 changed files with 10 additions and 0 deletions

View File

@ -117,6 +117,11 @@ impl<T:Ord> PriorityQueue<T> {
/// Create an empty PriorityQueue
pub fn new() -> PriorityQueue<T> { PriorityQueue{data: ~[],} }
/// Create an empty PriorityQueue with capacity `capacity`
pub fn with_capacity(capacity: uint) -> PriorityQueue<T> {
PriorityQueue { data: slice::with_capacity(capacity) }
}
/// Create a PriorityQueue from a vector (heapify)
pub fn from_vec(xs: ~[T]) -> PriorityQueue<T> {
let mut q = PriorityQueue{data: xs,};

View File

@ -112,6 +112,11 @@ impl<V> SmallIntMap<V> {
/// Create an empty SmallIntMap
pub fn new() -> SmallIntMap<V> { SmallIntMap{v: ~[]} }
/// Create an empty SmallIntMap with capacity `capacity`
pub fn with_capacity(capacity: uint) -> SmallIntMap<V> {
SmallIntMap { v: slice::with_capacity(capacity) }
}
pub fn get<'a>(&'a self, key: &uint) -> &'a V {
self.find(key).expect("key not present")
}