From 768757a0f2ad19180d516cd78910cddcdcf10c0e Mon Sep 17 00:00:00 2001 From: Tobias Bucher Date: Sat, 7 Jun 2014 23:17:40 +0200 Subject: [PATCH 1/2] Fix many collections implementing PartialEq They now properly check each element with the `eq` and `ne` methods in their own `eq` and `ne` method, respectively. --- src/libcollections/bitv.rs | 2 -- src/libcollections/ringbuf.rs | 5 +++-- src/libcollections/treemap.rs | 6 ++++++ src/libcore/cell.rs | 7 ++++--- src/libcore/ptr.rs | 12 ++++-------- src/libcore/slice.rs | 2 +- src/libstd/collections/hashmap.rs | 20 +++++++++++++++++--- src/libstd/collections/lru_cache.rs | 7 ++++--- 8 files changed, 39 insertions(+), 22 deletions(-) diff --git a/src/libcollections/bitv.rs b/src/libcollections/bitv.rs index 58f081b25e3e5..cb08765208c62 100644 --- a/src/libcollections/bitv.rs +++ b/src/libcollections/bitv.rs @@ -815,8 +815,6 @@ impl cmp::PartialEq for BitvSet { } return true; } - - fn ne(&self, other: &BitvSet) -> bool { !self.eq(other) } } impl fmt::Show for BitvSet { diff --git a/src/libcollections/ringbuf.rs b/src/libcollections/ringbuf.rs index ce4195789fab6..9f6e88bb8f9ec 100644 --- a/src/libcollections/ringbuf.rs +++ b/src/libcollections/ringbuf.rs @@ -371,10 +371,11 @@ fn raw_index(lo: uint, len: uint, index: uint) -> uint { impl PartialEq for RingBuf { fn eq(&self, other: &RingBuf) -> bool { self.nelts == other.nelts && - self.iter().zip(other.iter()).all(|(a, b)| a.eq(b)) + self.iter().zip(other.iter()).all(|(a, b)| a == b) } fn ne(&self, other: &RingBuf) -> bool { - !self.eq(other) + self.nelts != other.nelts && + self.iter().zip(other.iter()).all(|(a, b)| a != b) } } diff --git a/src/libcollections/treemap.rs b/src/libcollections/treemap.rs index def1c353bc132..87d9326057348 100644 --- a/src/libcollections/treemap.rs +++ b/src/libcollections/treemap.rs @@ -52,6 +52,10 @@ impl PartialEq for TreeMap { self.len() == other.len() && self.iter().zip(other.iter()).all(|(a, b)| a == b) } + fn ne(&self, other: &TreeMap) -> bool { + self.len() != other.len() && + self.iter().zip(other.iter()).all(|(a, b)| a != b) + } } // Lexicographical comparison @@ -559,6 +563,8 @@ pub struct TreeSet { impl PartialEq for TreeSet { #[inline] fn eq(&self, other: &TreeSet) -> bool { self.map == other.map } + #[inline] + fn ne(&self, other: &TreeSet) -> bool { self.map != other.map } } impl PartialOrd for TreeSet { diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index eef133181e129..31cd1ee98a09f 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -199,9 +199,10 @@ impl Clone for Cell { } impl PartialEq for Cell { - fn eq(&self, other: &Cell) -> bool { - self.get() == other.get() - } + #[inline] + fn eq(&self, other: &Cell) -> bool { self.get() == other.get() } + #[inline] + fn ne(&self, other: &Cell) -> bool { self.get() != other.get() } } /// A mutable memory location with dynamically checked borrow rules diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index b2776b78b1ce3..a80d59a37ffb8 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -404,11 +404,9 @@ impl RawPtr for *mut T { #[cfg(not(test))] impl PartialEq for *T { #[inline] - fn eq(&self, other: &*T) -> bool { - *self == *other - } + fn eq(&self, other: &*T) -> bool { *self == *other } #[inline] - fn ne(&self, other: &*T) -> bool { !self.eq(other) } + fn ne(&self, other: &*T) -> bool { *self != *other } } #[cfg(not(test))] @@ -417,11 +415,9 @@ impl Eq for *T {} #[cfg(not(test))] impl PartialEq for *mut T { #[inline] - fn eq(&self, other: &*mut T) -> bool { - *self == *other - } + fn eq(&self, other: &*mut T) -> bool { *self == *other } #[inline] - fn ne(&self, other: &*mut T) -> bool { !self.eq(other) } + fn ne(&self, other: &*mut T) -> bool { *self != *other } } #[cfg(not(test))] diff --git a/src/libcore/slice.rs b/src/libcore/slice.rs index 4dea1fd75a4bf..e613c6d4c2acf 100644 --- a/src/libcore/slice.rs +++ b/src/libcore/slice.rs @@ -270,7 +270,7 @@ pub mod traits { #[inline] fn eq(&self, other: &~[T]) -> bool { self.as_slice() == *other } #[inline] - fn ne(&self, other: &~[T]) -> bool { !self.eq(other) } + fn ne(&self, other: &~[T]) -> bool { self.as_slice() != *other } } impl<'a,T:Eq> Eq for &'a [T] {} diff --git a/src/libstd/collections/hashmap.rs b/src/libstd/collections/hashmap.rs index 571c579470441..4c80050c7d1a1 100644 --- a/src/libstd/collections/hashmap.rs +++ b/src/libstd/collections/hashmap.rs @@ -1420,6 +1420,17 @@ impl, V: PartialEq, S, H: Hasher> PartialEq for HashMap) -> bool { + if self.len() != other.len() { return true; } + + self.iter() + .all(|(key, value)| { + match other.find(key) { + None => true, + Some(v) => *value != *v + } + }) + } } impl, V: Eq, S, H: Hasher> Eq for HashMap {} @@ -1495,10 +1506,13 @@ pub struct HashSet { } impl, S, H: Hasher> PartialEq for HashSet { + #[inline] fn eq(&self, other: &HashSet) -> bool { - if self.len() != other.len() { return false; } - - self.iter().all(|key| other.contains(key)) + self.map == other.map + } + #[inline] + fn ne(&self, other: &HashSet) -> bool { + self.map != other.map } } diff --git a/src/libstd/collections/lru_cache.rs b/src/libstd/collections/lru_cache.rs index a12b00f34dc4a..a5f2fdf30091e 100644 --- a/src/libstd/collections/lru_cache.rs +++ b/src/libstd/collections/lru_cache.rs @@ -73,9 +73,10 @@ impl> Hash for KeyRef { } impl PartialEq for KeyRef { - fn eq(&self, other: &KeyRef) -> bool { - unsafe{ (*self.k).eq(&*other.k) } - } + #[inline] + fn eq(&self, other: &KeyRef) -> bool { unsafe { (&*self.k) == (&*other.k) } } + #[inline] + fn ne(&self, other: &KeyRef) -> bool { unsafe { (&*self.k) != (&*other.k) } } } impl Eq for KeyRef {} From 4e43b2b192af27b8702e6392765acef4fb091b2a Mon Sep 17 00:00:00 2001 From: Tobias Bucher Date: Sun, 8 Jun 2014 02:28:29 +0200 Subject: [PATCH 2/2] Fix implementation of `!=` for `HashMap`, `RingBuf` and `TreeMap` --- src/libcollections/ringbuf.rs | 4 ++-- src/libcollections/treemap.rs | 4 ++-- src/libstd/collections/hashmap.rs | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/libcollections/ringbuf.rs b/src/libcollections/ringbuf.rs index 9f6e88bb8f9ec..168d2b179e30e 100644 --- a/src/libcollections/ringbuf.rs +++ b/src/libcollections/ringbuf.rs @@ -374,8 +374,8 @@ impl PartialEq for RingBuf { self.iter().zip(other.iter()).all(|(a, b)| a == b) } fn ne(&self, other: &RingBuf) -> bool { - self.nelts != other.nelts && - self.iter().zip(other.iter()).all(|(a, b)| a != b) + self.nelts != other.nelts || + self.iter().zip(other.iter()).any(|(a, b)| a != b) } } diff --git a/src/libcollections/treemap.rs b/src/libcollections/treemap.rs index 87d9326057348..2107210b9a44f 100644 --- a/src/libcollections/treemap.rs +++ b/src/libcollections/treemap.rs @@ -53,8 +53,8 @@ impl PartialEq for TreeMap { self.iter().zip(other.iter()).all(|(a, b)| a == b) } fn ne(&self, other: &TreeMap) -> bool { - self.len() != other.len() && - self.iter().zip(other.iter()).all(|(a, b)| a != b) + self.len() != other.len() || + self.iter().zip(other.iter()).any(|(a, b)| a != b) } } diff --git a/src/libstd/collections/hashmap.rs b/src/libstd/collections/hashmap.rs index 4c80050c7d1a1..31de1b6aa6a0a 100644 --- a/src/libstd/collections/hashmap.rs +++ b/src/libstd/collections/hashmap.rs @@ -1424,7 +1424,7 @@ impl, V: PartialEq, S, H: Hasher> PartialEq for HashMap true, Some(v) => *value != *v