From 9e83d29f30046292f43e982893517f9aee47fa48 Mon Sep 17 00:00:00 2001 From: nham Date: Tue, 22 Jul 2014 16:36:09 -0400 Subject: [PATCH 1/2] Derive Hash for TrieMap and TrieSet --- src/libcollections/trie.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/libcollections/trie.rs b/src/libcollections/trie.rs index 29ec85590b320..21fafdec01988 100644 --- a/src/libcollections/trie.rs +++ b/src/libcollections/trie.rs @@ -17,6 +17,7 @@ use core::default::Default; use core::mem::zeroed; use core::mem; use core::uint; +use std::hash::{Writer, Hash}; use {Collection, Mutable, Map, MutableMap, Set, MutableSet}; use slice::{Items, MutItems}; @@ -292,7 +293,16 @@ impl Extendable<(uint, T)> for TrieMap { } } +impl> Hash for TrieMap { + fn hash(&self, state: &mut S) { + for elt in self.iter() { + elt.hash(state); + } + } +} + #[allow(missing_doc)] +#[deriving(Hash)] pub struct TrieSet { map: TrieMap<()> } @@ -1049,6 +1059,7 @@ mod bench_map { mod test_set { use std::prelude::*; use std::uint; + use std::hash; use {MutableSet, Set}; use super::TrieSet; @@ -1082,4 +1093,20 @@ mod test_set { assert!(set.contains(x)); } } + + #[test] + fn test_hash() { + let mut x = TrieSet::new(); + let mut y = TrieSet::new(); + + x.insert(1); + x.insert(2); + x.insert(3); + + y.insert(3); + y.insert(2); + y.insert(1); + + assert!(hash::hash(&x) == hash::hash(&y)); + } } From 366c66e171b94ef78ce7b8daf2530dbdc30eadb9 Mon Sep 17 00:00:00 2001 From: nham Date: Tue, 22 Jul 2014 17:04:16 -0400 Subject: [PATCH 2/2] Implement PartialEq, Eq for TrieMap, TrieSet --- src/libcollections/trie.rs | 64 +++++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 18 deletions(-) diff --git a/src/libcollections/trie.rs b/src/libcollections/trie.rs index 21fafdec01988..424cda92c12d4 100644 --- a/src/libcollections/trie.rs +++ b/src/libcollections/trie.rs @@ -41,6 +41,15 @@ pub struct TrieMap { length: uint } +impl PartialEq for TrieMap { + fn eq(&self, other: &TrieMap) -> bool { + self.len() == other.len() && + self.iter().zip(other.iter()).all(|(a, b)| a == b) + } +} + +impl Eq for TrieMap {} + impl Collection for TrieMap { /// Return the number of elements in the map #[inline] @@ -302,7 +311,7 @@ impl> Hash for TrieMap { } #[allow(missing_doc)] -#[deriving(Hash)] +#[deriving(Hash, PartialEq, Eq)] pub struct TrieSet { map: TrieMap<()> } @@ -671,6 +680,7 @@ mod test_map { use std::prelude::*; use std::iter::range_step; use std::uint; + use std::hash; use {MutableMap, Map}; use super::{TrieMap, TrieNode, Internal, External, Nothing}; @@ -943,6 +953,41 @@ mod test_map { assert!(m_lower.iter().all(|(_, &x)| x == 0)); assert!(m_upper.iter().all(|(_, &x)| x == 0)); } + + #[test] + fn test_eq() { + let mut a = TrieMap::new(); + let mut b = TrieMap::new(); + + assert!(a == b); + assert!(a.insert(0, 5i)); + assert!(a != b); + assert!(b.insert(0, 4i)); + assert!(a != b); + assert!(a.insert(5, 19)); + assert!(a != b); + assert!(!b.insert(0, 5)); + assert!(a != b); + assert!(b.insert(5, 19)); + assert!(a == b); + } + + #[test] + fn test_hash() { + let mut x = TrieMap::new(); + let mut y = TrieMap::new(); + + assert!(hash::hash(&x) == hash::hash(&y)); + x.insert(1, 'a'); + x.insert(2, 'b'); + x.insert(3, 'c'); + + y.insert(3, 'c'); + y.insert(2, 'b'); + y.insert(1, 'a'); + + assert!(hash::hash(&x) == hash::hash(&y)); + } } #[cfg(test)] @@ -1059,7 +1104,6 @@ mod bench_map { mod test_set { use std::prelude::*; use std::uint; - use std::hash; use {MutableSet, Set}; use super::TrieSet; @@ -1093,20 +1137,4 @@ mod test_set { assert!(set.contains(x)); } } - - #[test] - fn test_hash() { - let mut x = TrieSet::new(); - let mut y = TrieSet::new(); - - x.insert(1); - x.insert(2); - x.insert(3); - - y.insert(3); - y.insert(2); - y.insert(1); - - assert!(hash::hash(&x) == hash::hash(&y)); - } }