From feb18fe8daa3952718c29039ec792d5e69d1276f Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Thu, 25 Jul 2013 04:02:08 -0400 Subject: [PATCH] Added default impls for container methods A couple of implementations of Container::is_empty weren't exactly self.len() == 0 so I left them alone (e.g. Treemap). --- src/libextra/bitv.rs | 2 +- src/libextra/priority_queue.rs | 3 --- src/libextra/ringbuf.rs | 3 --- src/libextra/smallintmap.rs | 3 --- src/libextra/treemap.rs | 13 ------------- src/libstd/container.rs | 15 ++++++++++++--- src/libstd/hashmap.rs | 19 ------------------- src/libstd/str.rs | 8 -------- src/libstd/trie.rs | 23 ----------------------- 9 files changed, 13 insertions(+), 76 deletions(-) diff --git a/src/libextra/bitv.rs b/src/libextra/bitv.rs index 168d6a399167..6e52802578c2 100644 --- a/src/libextra/bitv.rs +++ b/src/libextra/bitv.rs @@ -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 { diff --git a/src/libextra/priority_queue.rs b/src/libextra/priority_queue.rs index d07b645a541d..dd24a2a9eb96 100644 --- a/src/libextra/priority_queue.rs +++ b/src/libextra/priority_queue.rs @@ -27,9 +27,6 @@ pub struct PriorityQueue { impl Container for PriorityQueue { /// 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 Mutable for PriorityQueue { diff --git a/src/libextra/ringbuf.rs b/src/libextra/ringbuf.rs index f46af664b189..aa670ec5ff8d 100644 --- a/src/libextra/ringbuf.rs +++ b/src/libextra/ringbuf.rs @@ -34,9 +34,6 @@ pub struct RingBuf { impl Container for RingBuf { /// 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 Mutable for RingBuf { diff --git a/src/libextra/smallintmap.rs b/src/libextra/smallintmap.rs index bd78f41ddf51..bf6f80534b4f 100644 --- a/src/libextra/smallintmap.rs +++ b/src/libextra/smallintmap.rs @@ -37,9 +37,6 @@ impl Container for SmallIntMap { } sz } - - /// Return true if the map contains no elements - fn is_empty(&self) -> bool { self.len() == 0 } } impl Mutable for SmallIntMap { diff --git a/src/libextra/treemap.rs b/src/libextra/treemap.rs index 303ae6a6d1de..8c7ace564124 100644 --- a/src/libextra/treemap.rs +++ b/src/libextra/treemap.rs @@ -135,19 +135,6 @@ impl MutableMap for TreeMap { 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 { diff --git a/src/libstd/container.rs b/src/libstd/container.rs index 4bad28ca3383..d855beea50b2 100644 --- a/src/libstd/container.rs +++ b/src/libstd/container.rs @@ -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 @@ -43,11 +46,17 @@ pub trait MutableMap: Map + 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. diff --git a/src/libstd/hashmap.rs b/src/libstd/hashmap.rs index deeda5019422..6bef110bfe59 100644 --- a/src/libstd/hashmap.rs +++ b/src/libstd/hashmap.rs @@ -282,9 +282,6 @@ impl HashMap { impl Container for HashMap { /// 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 Mutable for HashMap { @@ -325,19 +322,6 @@ impl MutableMap for HashMap { 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 { @@ -661,9 +645,6 @@ impl Eq for HashSet { impl Container for HashSet { /// 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 Mutable for HashSet { diff --git a/src/libstd/str.rs b/src/libstd/str.rs index 636bbc48f8eb..b97730e443c8 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -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 { diff --git a/src/libstd/trie.rs b/src/libstd/trie.rs index 822b005de37a..4665f3613401 100644 --- a/src/libstd/trie.rs +++ b/src/libstd/trie.rs @@ -36,10 +36,6 @@ impl Container for TrieMap { /// 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 Mutable for TrieMap { @@ -87,21 +83,6 @@ impl MutableMap for TrieMap { 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 { @@ -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 {