about summary refs log tree commit diff
path: root/src/libcore/send_map.rs
diff options
context:
space:
mode:
authorErick Tryzelaar <erick.tryzelaar@gmail.com>2012-12-10 09:00:52 -0800
committerGraydon Hoare <graydon@mozilla.com>2012-12-11 13:57:57 -0800
commit3657d5606d7ea5607a0670341a57c3ef20202ea0 (patch)
tree940a30de5e97a6aa619792d14b6483fe400123bc /src/libcore/send_map.rs
parent645bd98b602500ef910685eeeb899df298221e35 (diff)
core: add Eq impl to LinearMap.
Diffstat (limited to 'src/libcore/send_map.rs')
-rw-r--r--src/libcore/send_map.rs37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/libcore/send_map.rs b/src/libcore/send_map.rs
index 27b670fbf7f..4a8fe459b37 100644
--- a/src/libcore/send_map.rs
+++ b/src/libcore/send_map.rs
@@ -428,6 +428,25 @@ pub mod linear {
             option::unwrap(move value)
         }
     }
+
+    impl<K:Hash IterBytes Eq, V: Eq> LinearMap<K, V>: cmp::Eq {
+        pure fn eq(&self, other: &LinearMap<K, V>) -> bool {
+            if self.len() != other.len() { return false; }
+
+            for self.each |key, value| {
+                match other.find_ref(key) {
+                    None => return false,
+                    Some(v) => if value != v { return false },
+                }
+            }
+
+            return true;
+        }
+
+        pure fn ne(&self, other: &LinearMap<K, V>) -> bool {
+            !self.eq(other)
+        }
+    }
 }
 
 #[test]
@@ -538,4 +557,22 @@ pub mod test {
             Some(v) => assert *v == 2
         }
     }
+
+    #[test]
+    pub fn test_eq() {
+        let mut m1 = ~LinearMap();
+        m1.insert(1, 2);
+        m1.insert(2, 3);
+        m1.insert(3, 4);
+
+        let mut m2 = ~LinearMap();
+        m2.insert(1, 2);
+        m2.insert(2, 3);
+
+        assert m1 != m2;
+
+        m2.insert(3, 4);
+
+        assert m1 == m2;
+    }
 }