Skip to content

Commit 366c66e

Browse files
committed
Implement PartialEq, Eq for TrieMap, TrieSet
1 parent 9e83d29 commit 366c66e

File tree

1 file changed

+46
-18
lines changed

1 file changed

+46
-18
lines changed

src/libcollections/trie.rs

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ pub struct TrieMap<T> {
4141
length: uint
4242
}
4343

44+
impl<T: PartialEq> PartialEq for TrieMap<T> {
45+
fn eq(&self, other: &TrieMap<T>) -> bool {
46+
self.len() == other.len() &&
47+
self.iter().zip(other.iter()).all(|(a, b)| a == b)
48+
}
49+
}
50+
51+
impl<T: Eq> Eq for TrieMap<T> {}
52+
4453
impl<T> Collection for TrieMap<T> {
4554
/// Return the number of elements in the map
4655
#[inline]
@@ -302,7 +311,7 @@ impl<S: Writer, T: Hash<S>> Hash<S> for TrieMap<T> {
302311
}
303312

304313
#[allow(missing_doc)]
305-
#[deriving(Hash)]
314+
#[deriving(Hash, PartialEq, Eq)]
306315
pub struct TrieSet {
307316
map: TrieMap<()>
308317
}
@@ -671,6 +680,7 @@ mod test_map {
671680
use std::prelude::*;
672681
use std::iter::range_step;
673682
use std::uint;
683+
use std::hash;
674684

675685
use {MutableMap, Map};
676686
use super::{TrieMap, TrieNode, Internal, External, Nothing};
@@ -943,6 +953,41 @@ mod test_map {
943953
assert!(m_lower.iter().all(|(_, &x)| x == 0));
944954
assert!(m_upper.iter().all(|(_, &x)| x == 0));
945955
}
956+
957+
#[test]
958+
fn test_eq() {
959+
let mut a = TrieMap::new();
960+
let mut b = TrieMap::new();
961+
962+
assert!(a == b);
963+
assert!(a.insert(0, 5i));
964+
assert!(a != b);
965+
assert!(b.insert(0, 4i));
966+
assert!(a != b);
967+
assert!(a.insert(5, 19));
968+
assert!(a != b);
969+
assert!(!b.insert(0, 5));
970+
assert!(a != b);
971+
assert!(b.insert(5, 19));
972+
assert!(a == b);
973+
}
974+
975+
#[test]
976+
fn test_hash() {
977+
let mut x = TrieMap::new();
978+
let mut y = TrieMap::new();
979+
980+
assert!(hash::hash(&x) == hash::hash(&y));
981+
x.insert(1, 'a');
982+
x.insert(2, 'b');
983+
x.insert(3, 'c');
984+
985+
y.insert(3, 'c');
986+
y.insert(2, 'b');
987+
y.insert(1, 'a');
988+
989+
assert!(hash::hash(&x) == hash::hash(&y));
990+
}
946991
}
947992

948993
#[cfg(test)]
@@ -1059,7 +1104,6 @@ mod bench_map {
10591104
mod test_set {
10601105
use std::prelude::*;
10611106
use std::uint;
1062-
use std::hash;
10631107

10641108
use {MutableSet, Set};
10651109
use super::TrieSet;
@@ -1093,20 +1137,4 @@ mod test_set {
10931137
assert!(set.contains(x));
10941138
}
10951139
}
1096-
1097-
#[test]
1098-
fn test_hash() {
1099-
let mut x = TrieSet::new();
1100-
let mut y = TrieSet::new();
1101-
1102-
x.insert(1);
1103-
x.insert(2);
1104-
x.insert(3);
1105-
1106-
y.insert(3);
1107-
y.insert(2);
1108-
y.insert(1);
1109-
1110-
assert!(hash::hash(&x) == hash::hash(&y));
1111-
}
11121140
}

0 commit comments

Comments
 (0)