@@ -41,6 +41,15 @@ pub struct TrieMap<T> {
41
41
length : uint
42
42
}
43
43
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
+
44
53
impl < T > Collection for TrieMap < T > {
45
54
/// Return the number of elements in the map
46
55
#[ inline]
@@ -302,7 +311,7 @@ impl<S: Writer, T: Hash<S>> Hash<S> for TrieMap<T> {
302
311
}
303
312
304
313
#[ allow( missing_doc) ]
305
- #[ deriving( Hash ) ]
314
+ #[ deriving( Hash , PartialEq , Eq ) ]
306
315
pub struct TrieSet {
307
316
map : TrieMap < ( ) >
308
317
}
@@ -671,6 +680,7 @@ mod test_map {
671
680
use std:: prelude:: * ;
672
681
use std:: iter:: range_step;
673
682
use std:: uint;
683
+ use std:: hash;
674
684
675
685
use { MutableMap , Map } ;
676
686
use super :: { TrieMap , TrieNode , Internal , External , Nothing } ;
@@ -943,6 +953,41 @@ mod test_map {
943
953
assert ! ( m_lower. iter( ) . all( |( _, & x) | x == 0 ) ) ;
944
954
assert ! ( m_upper. iter( ) . all( |( _, & x) | x == 0 ) ) ;
945
955
}
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 , 5 i) ) ;
964
+ assert ! ( a != b) ;
965
+ assert ! ( b. insert( 0 , 4 i) ) ;
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
+ }
946
991
}
947
992
948
993
#[ cfg( test) ]
@@ -1059,7 +1104,6 @@ mod bench_map {
1059
1104
mod test_set {
1060
1105
use std:: prelude:: * ;
1061
1106
use std:: uint;
1062
- use std:: hash;
1063
1107
1064
1108
use { MutableSet , Set } ;
1065
1109
use super :: TrieSet ;
@@ -1093,20 +1137,4 @@ mod test_set {
1093
1137
assert ! ( set. contains( x) ) ;
1094
1138
}
1095
1139
}
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
- }
1112
1140
}
0 commit comments