Skip to content

Added default impls for container methods #8036

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

Closed
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion src/libextra/bitv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -703,8 +703,8 @@ impl cmp::Eq for BitvSet {
}

impl Container for BitvSet {
#[inline]
fn len(&self) -> uint { self.size }
fn is_empty(&self) -> bool { self.size == 0 }
}

impl Mutable for BitvSet {
Expand Down
3 changes: 0 additions & 3 deletions src/libextra/priority_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ pub struct PriorityQueue<T> {
impl<T:Ord> Container for PriorityQueue<T> {
/// Returns the length of the queue
fn len(&self) -> uint { self.data.len() }

/// Returns true if a queue contains no elements
fn is_empty(&self) -> bool { self.len() == 0 }
}

impl<T:Ord> Mutable for PriorityQueue<T> {
Expand Down
3 changes: 0 additions & 3 deletions src/libextra/ringbuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ pub struct RingBuf<T> {
impl<T> Container for RingBuf<T> {
/// Return the number of elements in the RingBuf
fn len(&self) -> uint { self.nelts }

/// Return true if the RingBufcontains no elements
fn is_empty(&self) -> bool { self.len() == 0 }
}

impl<T> Mutable for RingBuf<T> {
Expand Down
3 changes: 0 additions & 3 deletions src/libextra/smallintmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ impl<V> Container for SmallIntMap<V> {
}
sz
}

/// Return true if the map contains no elements
fn is_empty(&self) -> bool { self.len() == 0 }
}

impl<V> Mutable for SmallIntMap<V> {
Expand Down
13 changes: 0 additions & 13 deletions src/libextra/treemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,6 @@ impl<K: TotalOrd, V> MutableMap<K, V> for TreeMap<K, V> {
find_mut(&mut self.root, key)
}

/// Insert a key-value pair into the map. An existing value for a
/// key is replaced by the new value. Return true if the key did
/// not already exist in the map.
fn insert(&mut self, key: K, value: V) -> bool {
self.swap(key, value).is_none()
}

/// Remove a key-value pair from the map. Return true if the key
/// was present in the map, otherwise false.
fn remove(&mut self, key: &K) -> bool {
self.pop(key).is_some()
}

/// Insert a key-value pair from the map. If the key already had a value
/// present in the map, that value is returned. Otherwise None is returned.
fn swap(&mut self, key: K, value: V) -> Option<V> {
Expand Down
15 changes: 12 additions & 3 deletions src/libstd/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ pub trait Container {
fn len(&self) -> uint;

/// Return true if the container contains no elements
fn is_empty(&self) -> bool;
#[inline]
fn is_empty(&self) -> bool {
self.len() == 0
}
}

/// A trait to represent mutable containers
Expand All @@ -43,11 +46,17 @@ pub trait MutableMap<K, V>: Map<K, V> + Mutable {
/// Insert a key-value pair into the map. An existing value for a
/// key is replaced by the new value. Return true if the key did
/// not already exist in the map.
fn insert(&mut self, key: K, value: V) -> bool;
#[inline]
fn insert(&mut self, key: K, value: V) -> bool {
self.swap(key, value).is_none()
}

/// Remove a key-value pair from the map. Return true if the key
/// was present in the map, otherwise false.
fn remove(&mut self, key: &K) -> bool;
#[inline]
fn remove(&mut self, key: &K) -> bool {
self.pop(key).is_some()
}

/// Insert a key-value pair from the map. If the key already had a value
/// present in the map, that value is returned. Otherwise None is returned.
Expand Down
19 changes: 0 additions & 19 deletions src/libstd/hashmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,6 @@ impl<K:Hash + Eq,V> HashMap<K, V> {
impl<K:Hash + Eq,V> Container for HashMap<K, V> {
/// Return the number of elements in the map
fn len(&self) -> uint { self.size }

/// Return true if the map contains no elements
fn is_empty(&self) -> bool { self.len() == 0 }
}

impl<K:Hash + Eq,V> Mutable for HashMap<K, V> {
Expand Down Expand Up @@ -325,19 +322,6 @@ impl<K:Hash + Eq,V> MutableMap<K, V> for HashMap<K, V> {
Some(self.mut_value_for_bucket(idx))
}

/// Insert a key-value pair into the map. An existing value for a
/// key is replaced by the new value. Return true if the key did
/// not already exist in the map.
fn insert(&mut self, k: K, v: V) -> bool {
self.swap(k, v).is_none()
}

/// Remove a key-value pair from the map. Return true if the key
/// was present in the map, otherwise false.
fn remove(&mut self, k: &K) -> bool {
self.pop(k).is_some()
}

/// Insert a key-value pair from the map. If the key already had a value
/// present in the map, that value is returned. Otherwise None is returned.
fn swap(&mut self, k: K, v: V) -> Option<V> {
Expand Down Expand Up @@ -661,9 +645,6 @@ impl<T:Hash + Eq> Eq for HashSet<T> {
impl<T:Hash + Eq> Container for HashSet<T> {
/// Return the number of elements in the set
fn len(&self) -> uint { self.map.len() }

/// Return true if the set contains no elements
fn is_empty(&self) -> bool { self.map.is_empty() }
}

impl<T:Hash + Eq> Mutable for HashSet<T> {
Expand Down
8 changes: 0 additions & 8 deletions src/libstd/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1097,24 +1097,16 @@ impl<'self> Container for &'self str {
fn len(&self) -> uint {
do self.as_imm_buf |_p, n| { n - 1u }
}
#[inline]
fn is_empty(&self) -> bool {
self.len() == 0
}
}

impl Container for ~str {
#[inline]
fn len(&self) -> uint { self.as_slice().len() }
#[inline]
fn is_empty(&self) -> bool { self.len() == 0 }
}

impl Container for @str {
#[inline]
fn len(&self) -> uint { self.as_slice().len() }
#[inline]
fn is_empty(&self) -> bool { self.len() == 0 }
}

impl Mutable for ~str {
Expand Down
23 changes: 0 additions & 23 deletions src/libstd/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ impl<T> Container for TrieMap<T> {
/// Return the number of elements in the map
#[inline]
fn len(&self) -> uint { self.length }

/// Return true if the map contains no elements
#[inline]
fn is_empty(&self) -> bool { self.len() == 0 }
}

impl<T> Mutable for TrieMap<T> {
Expand Down Expand Up @@ -87,21 +83,6 @@ impl<T> MutableMap<uint, T> for TrieMap<T> {
find_mut(&mut self.root.children[chunk(*key, 0)], *key, 1)
}

/// Insert a key-value pair into the map. An existing value for a
/// key is replaced by the new value. Return true if the key did
/// not already exist in the map.
#[inline]
fn insert(&mut self, key: uint, value: T) -> bool {
self.swap(key, value).is_none()
}

/// Remove a key-value pair from the map. Return true if the key
/// was present in the map, otherwise false.
#[inline]
fn remove(&mut self, key: &uint) -> bool {
self.pop(key).is_some()
}

/// Insert a key-value pair from the map. If the key already had a value
/// present in the map, that value is returned. Otherwise None is returned.
fn swap(&mut self, key: uint, value: T) -> Option<T> {
Expand Down Expand Up @@ -194,10 +175,6 @@ impl Container for TrieSet {
/// Return the number of elements in the set
#[inline]
fn len(&self) -> uint { self.map.len() }

/// Return true if the set contains no elements
#[inline]
fn is_empty(&self) -> bool { self.map.is_empty() }
}

impl Mutable for TrieSet {
Expand Down