Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TrieSet should impl Set/MutableSet; add with_capacity to PriorityQueue/SmallIntMap #13061

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/libcollections/priority_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,};
Expand Down
5 changes: 5 additions & 0 deletions src/libcollections/smallintmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
40 changes: 27 additions & 13 deletions src/libcollections/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,32 +293,46 @@ impl Mutable for TrieSet {
fn clear(&mut self) { self.map.clear() }
}

impl TrieSet {
/// Create an empty TrieSet
impl Set<uint> 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<uint> 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]
Expand Down