From 3657d5606d7ea5607a0670341a57c3ef20202ea0 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Mon, 10 Dec 2012 09:00:52 -0800 Subject: [PATCH] core: add Eq impl to LinearMap. --- src/libcore/send_map.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) 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 LinearMap: cmp::Eq { + pure fn eq(&self, other: &LinearMap) -> 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) -> 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; + } }