diff --git a/src/libcollections/priority_queue.rs b/src/libcollections/priority_queue.rs index 8c7eb1c6033d0..a13785104abd5 100644 --- a/src/libcollections/priority_queue.rs +++ b/src/libcollections/priority_queue.rs @@ -117,6 +117,11 @@ impl PriorityQueue { /// Create an empty PriorityQueue pub fn new() -> PriorityQueue { PriorityQueue{data: ~[],} } + /// Create an empty PriorityQueue with capacity `capacity` + pub fn with_capacity(capacity: uint) -> PriorityQueue { + PriorityQueue { data: slice::with_capacity(capacity) } + } + /// Create a PriorityQueue from a vector (heapify) pub fn from_vec(xs: ~[T]) -> PriorityQueue { let mut q = PriorityQueue{data: xs,}; diff --git a/src/libcollections/smallintmap.rs b/src/libcollections/smallintmap.rs index db7fafe522b72..bd4f85aa81a78 100644 --- a/src/libcollections/smallintmap.rs +++ b/src/libcollections/smallintmap.rs @@ -112,6 +112,11 @@ impl SmallIntMap { /// Create an empty SmallIntMap pub fn new() -> SmallIntMap { SmallIntMap{v: ~[]} } + /// Create an empty SmallIntMap with capacity `capacity` + pub fn with_capacity(capacity: uint) -> SmallIntMap { + SmallIntMap { v: slice::with_capacity(capacity) } + } + pub fn get<'a>(&'a self, key: &uint) -> &'a V { self.find(key).expect("key not present") } diff --git a/src/libcollections/trie.rs b/src/libcollections/trie.rs index 740a36377003e..8b83e65838697 100644 --- a/src/libcollections/trie.rs +++ b/src/libcollections/trie.rs @@ -293,32 +293,46 @@ impl Mutable for TrieSet { fn clear(&mut self) { self.map.clear() } } -impl TrieSet { - /// Create an empty TrieSet +impl Set for TrieSet { #[inline] - pub fn new() -> TrieSet { - TrieSet{map: TrieMap::new()} + fn contains(&self, value: &uint) -> bool { + self.map.contains_key(value) } - /// Return true if the set contains a value #[inline] - pub fn contains(&self, value: &uint) -> bool { - self.map.contains_key(value) + fn is_disjoint(&self, other: &TrieSet) -> bool { + self.iter().all(|v| !other.contains(&v)) } - /// Add a value to the set. Return true if the value was not already - /// present in the set. #[inline] - pub fn insert(&mut self, value: uint) -> bool { + fn is_subset(&self, other: &TrieSet) -> bool { + self.iter().all(|v| other.contains(&v)) + } + + #[inline] + fn is_superset(&self, other: &TrieSet) -> bool { + other.is_subset(self) + } +} + +impl MutableSet for TrieSet { + #[inline] + fn insert(&mut self, value: uint) -> bool { self.map.insert(value, ()) } - /// Remove a value from the set. Return true if the value was - /// present in the set. #[inline] - pub fn remove(&mut self, value: &uint) -> bool { + fn remove(&mut self, value: &uint) -> bool { self.map.remove(value) } +} + +impl TrieSet { + /// Create an empty TrieSet + #[inline] + pub fn new() -> TrieSet { + TrieSet{map: TrieMap::new()} + } /// Visit all values in reverse order #[inline]