@@ -77,21 +77,13 @@ pure fn lt<K: Ord + TotalOrd, V>(a: &TreeMap<K, V>,
77
77
78
78
impl <K : Ord + TotalOrd , V > Ord for TreeMap < K , V > {
79
79
#[ inline( always) ]
80
- pure fn lt ( & self , other : & TreeMap < K , V > ) -> bool {
81
- lt ( self , other)
82
- }
80
+ pure fn lt ( & self , other : & TreeMap < K , V > ) -> bool { lt ( self , other) }
83
81
#[ inline( always) ]
84
- pure fn le ( & self , other : & TreeMap < K , V > ) -> bool {
85
- !lt ( other, self )
86
- }
82
+ pure fn le ( & self , other : & TreeMap < K , V > ) -> bool { !lt ( other, self ) }
87
83
#[ inline( always) ]
88
- pure fn ge ( & self , other : & TreeMap < K , V > ) -> bool {
89
- !lt ( self , other)
90
- }
84
+ pure fn ge ( & self , other : & TreeMap < K , V > ) -> bool { !lt ( self , other) }
91
85
#[ inline( always) ]
92
- pure fn gt ( & self , other : & TreeMap < K , V > ) -> bool {
93
- lt ( other, self )
94
- }
86
+ pure fn gt ( & self , other : & TreeMap < K , V > ) -> bool { lt ( other, self ) }
95
87
}
96
88
97
89
impl < ' self , K : TotalOrd , V > BaseIter < ( & ' self K , & ' self V ) > for TreeMap < K , V > {
@@ -149,9 +141,9 @@ impl<K: TotalOrd, V> Map<K, V> for TreeMap<K, V> {
149
141
match * current {
150
142
Some ( ref r) => {
151
143
match key. cmp ( & r. key ) {
152
- Less => current = & r. left ,
153
- Greater => current = & r. right ,
154
- Equal => return Some ( & r. value )
144
+ Less => current = & r. left ,
145
+ Greater => current = & r. right ,
146
+ Equal => return Some ( & r. value )
155
147
}
156
148
}
157
149
None => return None
@@ -244,19 +236,24 @@ pub struct TreeSet<T> {
244
236
245
237
impl < T : TotalOrd > BaseIter < T > for TreeSet < T > {
246
238
/// Visit all values in order
239
+ #[ inline( always) ]
247
240
pure fn each ( & self , f : & fn ( & T ) -> bool ) { self . map . each_key ( f) }
241
+ #[ inline( always) ]
248
242
pure fn size_hint ( & self ) -> Option < uint > { Some ( self . len ( ) ) }
249
243
}
250
244
251
245
impl < T : TotalOrd > ReverseIter < T > for TreeSet < T > {
252
246
/// Visit all values in reverse order
247
+ #[ inline( always) ]
253
248
pure fn each_reverse ( & self , f : & fn ( & T ) -> bool ) {
254
249
self . map . each_key_reverse ( f)
255
250
}
256
251
}
257
252
258
253
impl < T : Eq + TotalOrd > Eq for TreeSet < T > {
254
+ #[ inline( always) ]
259
255
pure fn eq ( & self , other : & TreeSet < T > ) -> bool { self . map == other. map }
256
+ #[ inline( always) ]
260
257
pure fn ne ( & self , other : & TreeSet < T > ) -> bool { self . map != other. map }
261
258
}
262
259
@@ -273,29 +270,35 @@ impl<T: Ord + TotalOrd> Ord for TreeSet<T> {
273
270
274
271
impl < T : TotalOrd > Container for TreeSet < T > {
275
272
/// Return the number of elements in the set
273
+ #[ inline( always) ]
276
274
pure fn len ( & self ) -> uint { self . map . len ( ) }
277
275
278
276
/// Return true if the set contains no elements
277
+ #[ inline( always) ]
279
278
pure fn is_empty ( & self ) -> bool { self . map . is_empty ( ) }
280
279
}
281
280
282
281
impl < T : TotalOrd > Mutable for TreeSet < T > {
283
282
/// Clear the set, removing all values.
283
+ #[ inline( always) ]
284
284
fn clear ( & mut self ) { self . map . clear ( ) }
285
285
}
286
286
287
287
impl < T : TotalOrd > Set < T > for TreeSet < T > {
288
288
/// Return true if the set contains a value
289
+ #[ inline( always) ]
289
290
pure fn contains ( & self , value : & T ) -> bool {
290
291
self . map . contains_key ( value)
291
292
}
292
293
293
294
/// Add a value to the set. Return true if the value was not already
294
295
/// present in the set.
296
+ #[ inline( always) ]
295
297
fn insert ( & mut self , value : T ) -> bool { self . map . insert ( value, ( ) ) }
296
298
297
299
/// Remove a value from the set. Return true if the value was
298
300
/// present in the set.
301
+ #[ inline( always) ]
299
302
fn remove ( & mut self , value : & T ) -> bool { self . map . remove ( value) }
300
303
301
304
/// Return true if the set has no elements in common with `other`.
@@ -320,6 +323,7 @@ impl<T: TotalOrd> Set<T> for TreeSet<T> {
320
323
}
321
324
322
325
/// Return true if the set is a subset of another
326
+ #[ inline( always) ]
323
327
pure fn is_subset ( & self , other : & TreeSet < T > ) -> bool {
324
328
other. is_superset ( self )
325
329
}
@@ -482,16 +486,21 @@ impl<T: TotalOrd> Set<T> for TreeSet<T> {
482
486
a = set_next ( & mut x) ;
483
487
}
484
488
}
489
+ do b. while_some |b1| {
490
+ if f ( b1) { set_next ( & mut y) } else { None }
491
+ }
485
492
}
486
493
}
487
494
}
488
495
489
496
pub impl < T : TotalOrd > TreeSet < T > {
490
497
/// Create an empty TreeSet
498
+ #[ inline( always) ]
491
499
static pure fn new( ) -> TreeSet <T > { TreeSet { map: TreeMap :: new( ) } }
492
500
493
501
/// Get a lazy iterator over the values in the set.
494
502
/// Requires that it be frozen (immutable).
503
+ #[ inline( always) ]
495
504
pure fn iter ( & self ) -> TreeSetIterator /& self <T > {
496
505
TreeSetIterator { iter: self . map . iter ( ) }
497
506
}
@@ -504,13 +513,15 @@ pub struct TreeSetIterator<T> {
504
513
505
514
/// Advance the iterator to the next node (in order). If this iterator is
506
515
/// finished, does nothing.
516
+ #[ inline( always) ]
507
517
pub fn set_next<T >( iter: & mut TreeSetIterator /& r<T >) -> Option <& r/T > {
508
518
do map_next( & mut iter. iter) . map |& ( value, _) | { value }
509
519
}
510
520
511
521
/// Advance the iterator through the set
512
- fn set_advance<T >( iter: & mut TreeSetIterator /& r<T >,
513
- f: & fn ( & r/T ) -> bool ) {
522
+ #[ inline( always) ]
523
+ pub fn set_advance<T >( iter: & mut TreeSetIterator /& r<T >,
524
+ f: & fn ( & r/T ) -> bool ) {
514
525
do map_advance( & mut iter. iter) |( k, _) | { f( k) }
515
526
}
516
527
@@ -532,15 +543,15 @@ pub impl<K: TotalOrd, V> TreeNode<K, V> {
532
543
}
533
544
534
545
pure fn each<K : TotalOrd , V >( node: & r/Option <~TreeNode <K , V >>,
535
- f: & fn ( & ( & r/K , & r/V ) ) -> bool ) {
546
+ f: & fn ( & ( & r/K , & r/V ) ) -> bool ) {
536
547
for node. each |x| {
537
548
each( & x. left, f) ;
538
549
if f( & ( & x. key, & x. value) ) { each( & x. right, f) }
539
550
}
540
551
}
541
552
542
553
pure fn each_reverse<K : TotalOrd , V >( node: & r/Option <~TreeNode <K , V >>,
543
- f: & fn ( & ( & r/K , & r/V ) ) -> bool ) {
554
+ f: & fn ( & ( & r/K , & r/V ) ) -> bool ) {
544
555
for node. each |x| {
545
556
each_reverse( & x. right, f) ;
546
557
if f( & ( & x. key, & x. value) ) { each_reverse( & x. left, f) }
@@ -665,20 +676,20 @@ fn remove<K: TotalOrd, V>(node: &mut Option<~TreeNode<K, V>>,
665
676
skew( save) ;
666
677
667
678
match save. right {
668
- Some ( ref mut right) => {
669
- skew( right) ;
670
- match right. right {
671
- Some ( ref mut x) => { skew( x) } ,
672
- None => ( )
673
- }
679
+ Some ( ref mut right) => {
680
+ skew( right) ;
681
+ match right. right {
682
+ Some ( ref mut x) => { skew( x) } ,
683
+ None => ( )
674
684
}
675
- None => ( )
685
+ }
686
+ None => ( )
676
687
}
677
688
678
689
split( save) ;
679
690
match save. right {
680
- Some ( ref mut x) => { split( x) } ,
681
- None => ( )
691
+ Some ( ref mut x) => { split( x) } ,
692
+ None => ( )
682
693
}
683
694
}
684
695
@@ -1101,112 +1112,82 @@ mod test_set {
1101
1112
}
1102
1113
}
1103
1114
1104
- # [ test ]
1105
- fn test_intersection ( ) {
1106
- let mut a = TreeSet :: new( ) ;
1107
- let mut b = TreeSet :: new( ) ;
1115
+ fn check ( a : & [ int ] , b : & [ int ] , expected : & [ int ] ,
1116
+ f : & fn ( & TreeSet <int> , & TreeSet <int> , f : & fn ( & int ) -> bool ) ) {
1117
+ let mut set_a = TreeSet :: new( ) ;
1118
+ let mut set_b = TreeSet :: new( ) ;
1108
1119
1109
- fail_unless!( a. insert( 11 ) ) ;
1110
- fail_unless!( a. insert( 1 ) ) ;
1111
- fail_unless!( a. insert( 3 ) ) ;
1112
- fail_unless!( a. insert( 77 ) ) ;
1113
- fail_unless!( a. insert( 103 ) ) ;
1114
- fail_unless!( a. insert( 5 ) ) ;
1115
- fail_unless!( a. insert( -5 ) ) ;
1116
-
1117
- fail_unless!( b. insert( 2 ) ) ;
1118
- fail_unless!( b. insert( 11 ) ) ;
1119
- fail_unless!( b. insert( 77 ) ) ;
1120
- fail_unless!( b. insert( -9 ) ) ;
1121
- fail_unless!( b. insert( -42 ) ) ;
1122
- fail_unless!( b. insert( 5 ) ) ;
1123
- fail_unless!( b. insert( 3 ) ) ;
1120
+ for a. each |x| { fail_unless!( set_a. insert( * x) ) }
1121
+ for b. each |y| { fail_unless!( set_b. insert( * y) ) }
1124
1122
1125
1123
let mut i = 0 ;
1126
- let expected = [ 3 , 5 , 11 , 77 ] ;
1127
- for a. intersection( & b) |x| {
1124
+ for f( & set_a, & set_b) |x| {
1128
1125
fail_unless!( * x == expected[ i] ) ;
1129
- i += 1
1126
+ i += 1 ;
1130
1127
}
1131
1128
fail_unless!( i == expected. len( ) ) ;
1132
1129
}
1133
1130
1134
1131
#[ test]
1135
- fn test_difference( ) {
1136
- let mut a = TreeSet :: new( ) ;
1137
- let mut b = TreeSet :: new( ) ;
1138
-
1139
- fail_unless!( a. insert( 1 ) ) ;
1140
- fail_unless!( a. insert( 3 ) ) ;
1141
- fail_unless!( a. insert( 5 ) ) ;
1142
- fail_unless!( a. insert( 9 ) ) ;
1143
- fail_unless!( a. insert( 11 ) ) ;
1132
+ fn test_intersection( ) {
1133
+ fn check_intersection( a: & [ int] , b: & [ int] , expected: & [ int] ) {
1134
+ check( a, b, expected, |x, y, z| x. intersection( y, z) )
1135
+ }
1144
1136
1145
- fail_unless!( b. insert( 3 ) ) ;
1146
- fail_unless!( b. insert( 9 ) ) ;
1137
+ check_intersection( [ ] , [ ] , [ ] ) ;
1138
+ check_intersection( [ 1 , 2 , 3 ] , [ ] , [ ] ) ;
1139
+ check_intersection( [ ] , [ 1 , 2 , 3 ] , [ ] ) ;
1140
+ check_intersection( [ 2 ] , [ 1 , 2 , 3 ] , [ 2 ] ) ;
1141
+ check_intersection( [ 1 , 2 , 3 ] , [ 2 ] , [ 2 ] ) ;
1142
+ check_intersection( [ 11 , 1 , 3 , 77 , 103 , 5 , -5 ] ,
1143
+ [ 2 , 11 , 77 , -9 , -42 , 5 , 3 ] ,
1144
+ [ 3 , 5 , 11 , 77 ] ) ;
1145
+ }
1147
1146
1148
- let mut i = 0 ;
1149
- let expected = [ 1 , 5 , 11 ] ;
1150
- for a. difference( & b) |x| {
1151
- fail_unless!( * x == expected[ i] ) ;
1152
- i += 1
1147
+ #[ test]
1148
+ fn test_difference( ) {
1149
+ fn check_difference( a: & [ int] , b: & [ int] , expected: & [ int] ) {
1150
+ check( a, b, expected, |x, y, z| x. difference( y, z) )
1153
1151
}
1154
- fail_unless!( i == expected. len( ) ) ;
1152
+
1153
+ check_difference( [ ] , [ ] , [ ] ) ;
1154
+ check_difference( [ 1 , 12 ] , [ ] , [ 1 , 12 ] ) ;
1155
+ check_difference( [ ] , [ 1 , 2 , 3 , 9 ] , [ ] ) ;
1156
+ check_difference( [ 1 , 3 , 5 , 9 , 11 ] ,
1157
+ [ 3 , 9 ] ,
1158
+ [ 1 , 5 , 11 ] ) ;
1159
+ check_difference( [ -5 , 11 , 22 , 33 , 40 , 42 ] ,
1160
+ [ -12 , -5 , 14 , 23 , 34 , 38 , 39 , 50 ] ,
1161
+ [ 11 , 22 , 33 , 40 , 42 ] ) ;
1155
1162
}
1156
1163
1157
1164
#[ test]
1158
1165
fn test_symmetric_difference( ) {
1159
- let mut a = TreeSet :: new( ) ;
1160
- let mut b = TreeSet :: new( ) ;
1161
-
1162
- fail_unless!( a. insert( 1 ) ) ;
1163
- fail_unless!( a. insert( 3 ) ) ;
1164
- fail_unless!( a. insert( 5 ) ) ;
1165
- fail_unless!( a. insert( 9 ) ) ;
1166
- fail_unless!( a. insert( 11 ) ) ;
1167
-
1168
- fail_unless!( b. insert( -2 ) ) ;
1169
- fail_unless!( b. insert( 3 ) ) ;
1170
- fail_unless!( b. insert( 9 ) ) ;
1171
- fail_unless!( b. insert( 14 ) ) ;
1172
- fail_unless!( b. insert( 22 ) ) ;
1173
-
1174
- let mut i = 0 ;
1175
- let expected = [ -2 , 1 , 5 , 11 , 14 , 22 ] ;
1176
- for a. symmetric_difference( & b) |x| {
1177
- fail_unless!( * x == expected[ i] ) ;
1178
- i += 1
1166
+ fn check_symmetric_difference( a: & [ int] , b: & [ int] ,
1167
+ expected: & [ int] ) {
1168
+ check( a, b, expected, |x, y, z| x. symmetric_difference( y, z) )
1179
1169
}
1180
- fail_unless!( i == expected. len( ) ) ;
1170
+
1171
+ check_symmetric_difference( [ ] , [ ] , [ ] ) ;
1172
+ check_symmetric_difference( [ 1 , 2 , 3 ] , [ 2 ] , [ 1 , 3 ] ) ;
1173
+ check_symmetric_difference( [ 2 ] , [ 1 , 2 , 3 ] , [ 1 , 3 ] ) ;
1174
+ check_symmetric_difference( [ 1 , 3 , 5 , 9 , 11 ] ,
1175
+ [ -2 , 3 , 9 , 14 , 22 ] ,
1176
+ [ -2 , 1 , 5 , 11 , 14 , 22 ] ) ;
1181
1177
}
1182
1178
1183
1179
#[ test]
1184
1180
fn test_union( ) {
1185
- let mut a = TreeSet :: new( ) ;
1186
- let mut b = TreeSet :: new( ) ;
1187
-
1188
- fail_unless!( a. insert( 1 ) ) ;
1189
- fail_unless!( a. insert( 3 ) ) ;
1190
- fail_unless!( a. insert( 5 ) ) ;
1191
- fail_unless!( a. insert( 9 ) ) ;
1192
- fail_unless!( a. insert( 11 ) ) ;
1193
- fail_unless!( a. insert( 16 ) ) ;
1194
- fail_unless!( a. insert( 19 ) ) ;
1195
- fail_unless!( a. insert( 24 ) ) ;
1196
-
1197
- fail_unless!( b. insert( -2 ) ) ;
1198
- fail_unless!( b. insert( 1 ) ) ;
1199
- fail_unless!( b. insert( 5 ) ) ;
1200
- fail_unless!( b. insert( 9 ) ) ;
1201
- fail_unless!( b. insert( 13 ) ) ;
1202
- fail_unless!( b. insert( 19 ) ) ;
1203
-
1204
- let mut i = 0 ;
1205
- let expected = [ -2 , 1 , 3 , 5 , 9 , 11 , 13 , 16 , 19 , 24 ] ;
1206
- for a. union ( & b) |x| {
1207
- fail_unless ! ( * x == expected[ i] ) ;
1208
- i += 1
1181
+ fn check_union( a: & [ int] , b: & [ int] ,
1182
+ expected: & [ int] ) {
1183
+ check( a, b, expected, |x, y, z| x. union ( y, z) )
1209
1184
}
1210
- fail_unless ! ( i == expected. len( ) ) ;
1185
+
1186
+ check_union( [ ] , [ ] , [ ] ) ;
1187
+ check_union( [ 1 , 2 , 3 ] , [ 2 ] , [ 1 , 2 , 3 ] ) ;
1188
+ check_union( [ 2 ] , [ 1 , 2 , 3 ] , [ 1 , 2 , 3 ] ) ;
1189
+ check_union( [ 1 , 3 , 5 , 9 , 11 , 16 , 19 , 24 ] ,
1190
+ [ -2 , 1 , 5 , 9 , 13 , 19 ] ,
1191
+ [ -2 , 1 , 3 , 5 , 9 , 11 , 13 , 16 , 19 , 24 ] ) ;
1211
1192
}
1212
1193
}
0 commit comments