@@ -279,15 +279,45 @@ impl <T: Ord> TreeSet<T> {
279
279
280
280
/// Visit the values (in-order) representing the symmetric difference
281
281
pure fn symmetric_difference ( & self , other : & TreeSet < T > ,
282
- _f : fn ( & T ) -> bool ) {
282
+ f : fn ( & T ) -> bool ) {
283
283
unsafe { // purity workaround
284
284
let mut x = self . map . iter ( ) ;
285
285
let mut y = other. map . iter ( ) ;
286
286
287
287
let mut a = x. next ( ) ;
288
288
let mut b = y. next ( ) ;
289
+
290
+ while a. is_some ( ) {
291
+ if b. is_none ( ) {
292
+ while a. is_some ( ) {
293
+ let ( a1, _) = a. unwrap ( ) ;
294
+ if !f ( a1) { return }
295
+ a = x. next ( ) ;
296
+ }
297
+ return
298
+ }
299
+
300
+ let ( a1, _) = a. unwrap ( ) ;
301
+ let ( b1, _) = b. unwrap ( ) ;
302
+
303
+ if a1 < b1 {
304
+ if !f ( a1) { return }
305
+ a = x. next ( ) ;
306
+ } else {
307
+ if b1 < a1 {
308
+ if !f ( b1) { return }
309
+ } else {
310
+ a = x. next ( ) ;
311
+ }
312
+ b = y. next ( ) ;
313
+ }
314
+ }
315
+ while b. is_some ( ) {
316
+ let ( b1, _) = b. unwrap ( ) ;
317
+ if !f ( b1) { return }
318
+ b = y. next ( ) ;
319
+ }
289
320
}
290
- fail ~"not yet implemented"
291
321
}
292
322
293
323
/// Visit the values (in-order) representing the intersection
@@ -895,6 +925,32 @@ mod test_set {
895
925
assert i == expected. len ( ) ;
896
926
}
897
927
928
+ #[ test]
929
+ fn test_symmetric_difference ( ) {
930
+ let mut a = TreeSet :: new ( ) ;
931
+ let mut b = TreeSet :: new ( ) ;
932
+
933
+ assert a. insert ( 1 ) ;
934
+ assert a. insert ( 3 ) ;
935
+ assert a. insert ( 5 ) ;
936
+ assert a. insert ( 9 ) ;
937
+ assert a. insert ( 11 ) ;
938
+
939
+ assert b. insert ( -2 ) ;
940
+ assert b. insert ( 3 ) ;
941
+ assert b. insert ( 9 ) ;
942
+ assert b. insert ( 14 ) ;
943
+ assert b. insert ( 22 ) ;
944
+
945
+ let mut i = 0 ;
946
+ let expected = [ -2 , 1 , 5 , 11 , 14 , 22 ] ;
947
+ for a. symmetric_difference( & b) |x| {
948
+ assert * x == expected[ i] ;
949
+ i += 1
950
+ }
951
+ assert i == expected. len ( ) ;
952
+ }
953
+
898
954
#[ test]
899
955
fn test_union ( ) {
900
956
let mut a = TreeSet :: new ( ) ;
0 commit comments