@@ -39,19 +39,26 @@ pub struct TreeMap<K: Ord, V> {
39
39
priv length : uint
40
40
}
41
41
42
- // FIXME: this is a naive O(n*log(m)) implementation, could be O(n)
43
- impl < K : Ord , V : Eq > TreeMap < K , V > : Eq {
42
+ impl < K : Eq Ord , V : Eq > TreeMap < K , V > : Eq {
44
43
pure fn eq ( & self , other : & TreeMap < K , V > ) -> bool {
45
44
if self . len ( ) != other. len ( ) {
46
45
return false
47
46
}
48
- for self . each |x, y| {
49
- match other. find ( x) {
50
- Some ( z) => if z != y { return false } ,
51
- None => return false
47
+ unsafe { // purity workaround
48
+ let mut x = self . iter ( ) ;
49
+ let mut y = other. iter ( ) ;
50
+ for self . len( ) . times {
51
+ // ICE: x.next() != y.next()
52
+
53
+ let ( x1, x2) = x. next( ) . unwrap( ) ;
54
+ let ( y1, y2) = y. next( ) . unwrap( ) ;
55
+
56
+ if x1 != y1 || x2 ! = y2 {
57
+ return false
58
+ }
52
59
}
60
+ true
53
61
}
54
- true
55
62
}
56
63
pure fn ne( & self , other: & TreeMap < K , V > ) -> bool {
57
64
!self . eq( other)
@@ -137,8 +144,8 @@ impl <K: Ord, V> TreeMap<K, V> {
137
144
ret
138
145
}
139
146
140
- /// Get a lazy iterator over the nodes in the map. Requires that it
141
- /// be frozen (immutable).
147
+ /// Get a lazy iterator over the key-value pairs in the map.
148
+ /// Requires that it be frozen (immutable).
142
149
fn iter ( & self ) -> TreeMapIterator /& self <K , V > {
143
150
TreeMapIterator { stack: ~[ ] , node : & self . root}
144
151
}
@@ -183,7 +190,7 @@ impl <T: Ord> TreeSet<T>: iter::BaseIter<T> {
183
190
pure fn size_hint( & self ) -> Option <uint> { Some ( self . len( ) ) }
184
191
}
185
192
186
- impl < T : Ord > TreeSet < T > : Eq {
193
+ impl <T : Eq Ord > TreeSet <T >: Eq {
187
194
pure fn eq( & self , other: & TreeSet <T >) -> bool { self . map == other. map }
188
195
pure fn ne( & self , other: & TreeSet <T >) -> bool { self . map != other. map }
189
196
}
0 commit comments