about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDaniel Micay <danielmicay@gmail.com>2013-01-31 16:43:49 -0500
committerDaniel Micay <danielmicay@gmail.com>2013-01-31 23:13:55 -0500
commit274e75cd82ac8b94c655e1cba9194f82c47f73d7 (patch)
treec226fd3a8203b2724fac962a758367dc41b8ffdb
parentaee79294699153ac7da9d2e5d076192c6eee3238 (diff)
downloadrust-274e75cd82ac8b94c655e1cba9194f82c47f73d7.tar.gz
rust-274e75cd82ac8b94c655e1cba9194f82c47f73d7.zip
implement container::Container for SmallIntMap
-rw-r--r--src/libstd/smallintmap.rs36
1 files changed, 30 insertions, 6 deletions
diff --git a/src/libstd/smallintmap.rs b/src/libstd/smallintmap.rs
index c4680056e19..ec35aa85bbe 100644
--- a/src/libstd/smallintmap.rs
+++ b/src/libstd/smallintmap.rs
@@ -14,9 +14,9 @@
  */
 #[forbid(deprecated_mode)];
 
-use map;
 use map::StdMap;
 
+use core::container::{Container, Mutable, Map, Set};
 use core::dvec::DVec;
 use core::ops;
 use core::option::{Some, None};
@@ -80,9 +80,9 @@ pub pure fn contains_key<T: Copy>(self: SmallIntMap<T>, key: uint) -> bool {
     return !find(self, key).is_none();
 }
 
-/// Implements the map::map interface for smallintmap
-impl<V: Copy> SmallIntMap<V>: map::StdMap<uint, V> {
-    pure fn size() -> uint {
+impl<V> SmallIntMap<V>: Container {
+    /// Return the number of elements in the map
+    pure fn len(&self) -> uint {
         let mut sz = 0u;
         for self.v.each |item| {
             match *item {
@@ -92,6 +92,14 @@ impl<V: Copy> SmallIntMap<V>: map::StdMap<uint, V> {
         }
         sz
     }
+
+    /// Return true if the map contains no elements
+    pure fn is_empty(&self) -> bool { self.len() == 0 }
+}
+
+/// Implements the map::map interface for smallintmap
+impl<V: Copy> SmallIntMap<V>: StdMap<uint, V> {
+    pure fn size() -> uint { self.len() }
     #[inline(always)]
     fn insert(key: uint, value: V) -> bool {
         let exists = contains_key(self, key);
@@ -165,8 +173,8 @@ impl<V: Copy> SmallIntMap<V>: ops::Index<uint, V> {
 }
 
 /// Cast the given smallintmap to a map::map
-pub fn as_map<V: Copy>(s: SmallIntMap<V>) -> map::StdMap<uint, V> {
-    s as map::StdMap::<uint, V>
+pub fn as_map<V: Copy>(s: SmallIntMap<V>) -> StdMap<uint, V> {
+    s as StdMap::<uint, V>
 }
 
 #[cfg(test)]
@@ -177,6 +185,22 @@ mod tests {
     use core::option;
 
     #[test]
+    fn test_len() {
+        let mut map = mk();
+        assert map.len() == 0;
+        assert map.is_empty();
+        map.insert(5, 20);
+        assert map.len() == 1;
+        assert !map.is_empty();
+        map.insert(11, 12);
+        assert map.len() == 2;
+        assert !map.is_empty();
+        map.insert(14, 22);
+        assert map.len() == 3;
+        assert !map.is_empty();
+    }
+
+    #[test]
     fn test_insert_with_key() {
         let map: SmallIntMap<uint> = mk();