@@ -32,7 +32,7 @@ impl<T> BaseIter<(uint, &T)> for TrieMap<T> {
32
32
/// Visit all key-value pairs in order
33
33
#[ inline( always) ]
34
34
pure fn each ( & self , f : fn ( & ( uint, & self /T ) ) -> bool ) {
35
- self . root . each ( f)
35
+ self . root . each ( f) ;
36
36
}
37
37
#[ inline( always) ]
38
38
pure fn size_hint ( & self ) -> Option < uint > { Some ( self . len ( ) ) }
@@ -42,7 +42,7 @@ impl<T> ReverseIter<(uint, &T)> for TrieMap<T> {
42
42
/// Visit all key-value pairs in reverse order
43
43
#[ inline( always) ]
44
44
pure fn each_reverse ( & self , f : fn ( & ( uint, & self /T ) ) -> bool ) {
45
- self . root . each_reverse ( f)
45
+ self . root . each_reverse ( f) ;
46
46
}
47
47
}
48
48
@@ -149,7 +149,7 @@ impl<T> TrieMap<T> {
149
149
150
150
/// Iterate over the map and mutate the contained values
151
151
fn mutate_values ( & mut self , f : fn ( uint , & mut T ) -> bool ) {
152
- self . root . mutate_values ( f)
152
+ self . root . mutate_values ( f) ;
153
153
}
154
154
}
155
155
@@ -217,34 +217,39 @@ impl<T: Copy> TrieNode<T> {
217
217
}
218
218
219
219
impl < T > TrieNode < T > {
220
- pure fn each ( & self , f : fn ( & ( uint, & self /T ) ) -> bool ) {
220
+ pure fn each ( & self , f : fn ( & ( uint, & self /T ) ) -> bool ) -> bool {
221
221
for uint:: range( 0 , self . children. len( ) ) |idx| {
222
222
match self . children [ idx] {
223
- Internal ( ref x) => x. each ( f) ,
224
- External ( k, ref v) => if !f ( & ( k, v) ) { return } ,
223
+ Internal ( ref x) => if ! x. each ( f) { return false } ,
224
+ External ( k, ref v) => if !f ( & ( k, v) ) { return false } ,
225
225
Nothing => ( )
226
226
}
227
227
}
228
+ true
228
229
}
229
230
230
- pure fn each_reverse ( & self , f : fn ( & ( uint, & self /T ) ) -> bool ) {
231
+ pure fn each_reverse ( & self , f : fn ( & ( uint, & self /T ) ) -> bool ) -> bool {
231
232
for uint:: range_rev( self . children. len( ) , 0 ) |idx| {
232
233
match self . children [ idx - 1 ] {
233
- Internal ( ref x) => x . each ( f) ,
234
- External ( k, ref v) => if !f ( & ( k, v) ) { return } ,
234
+ Internal ( ref x) => if !x . each_reverse ( f) { return false } ,
235
+ External ( k, ref v) => if !f ( & ( k, v) ) { return false } ,
235
236
Nothing => ( )
236
237
}
237
238
}
239
+ true
238
240
}
239
241
240
- fn mutate_values ( & mut self , f : fn ( uint , & mut T ) -> bool ) {
242
+ fn mutate_values ( & mut self , f : fn ( uint , & mut T ) -> bool ) -> bool {
241
243
for vec:: each_mut( self . children) |child| {
242
244
match * child {
243
- Internal ( ref mut x) => x. mutate_values ( f) ,
244
- External ( k, ref mut v) => if !f ( k, v) { return } ,
245
+ Internal ( ref mut x) => if !x. mutate_values ( f) {
246
+ return false
247
+ } ,
248
+ External ( k, ref mut v) => if !f ( k, v) { return false } ,
245
249
Nothing => ( )
246
250
}
247
251
}
252
+ true
248
253
}
249
254
}
250
255
@@ -366,4 +371,78 @@ mod tests {
366
371
check_integrity( & trie. root) ;
367
372
}
368
373
}
374
+
375
+ #[ test]
376
+ fn test_each ( ) {
377
+ let mut m = TrieMap : : new( ) ;
378
+
379
+ assert m. insert( 3 , 6 ) ;
380
+ assert m. insert( 0 , 0 ) ;
381
+ assert m. insert( 4 , 8 ) ;
382
+ assert m. insert( 2 , 4 ) ;
383
+ assert m. insert( 1 , 2 ) ;
384
+
385
+ let mut n = 0 ;
386
+ for m. each |& ( k, v) | {
387
+ assert k == n;
388
+ assert * v == n * 2 ;
389
+ n += 1 ;
390
+ }
391
+ }
392
+
393
+ #[ test]
394
+ fn test_each_break( ) {
395
+ let mut m = TrieMap :: new( ) ;
396
+
397
+ for uint:: range_rev( uint:: max_value, uint:: max_value - 10000 ) |x| {
398
+ m. insert( x, x / 2 ) ;
399
+ }
400
+
401
+ let mut n = uint:: max_value - 9999 ;
402
+ for m. each |& ( k, v) | {
403
+ if n == uint:: max_value - 5000 { break }
404
+ assert n < uint:: max_value - 5000 ;
405
+
406
+ assert k == n;
407
+ assert * v == n / 2 ;
408
+ n += 1 ;
409
+ }
410
+ }
411
+
412
+ #[ test]
413
+ fn test_each_reverse ( ) {
414
+ let mut m = TrieMap : : new( ) ;
415
+
416
+ assert m. insert( 3 , 6 ) ;
417
+ assert m. insert( 0 , 0 ) ;
418
+ assert m. insert( 4 , 8 ) ;
419
+ assert m. insert( 2 , 4 ) ;
420
+ assert m. insert( 1 , 2 ) ;
421
+
422
+ let mut n = 4 ;
423
+ for m. each_reverse |& ( k, v) | {
424
+ assert k == n;
425
+ assert * v == n * 2 ;
426
+ n -= 1 ;
427
+ }
428
+ }
429
+
430
+ #[ test]
431
+ fn test_each_reverse_break( ) {
432
+ let mut m = TrieMap :: new( ) ;
433
+
434
+ for uint:: range_rev( uint:: max_value, uint:: max_value - 10000 ) |x| {
435
+ m. insert( x, x / 2 ) ;
436
+ }
437
+
438
+ let mut n = uint:: max_value;
439
+ for m. each_reverse |& ( k, v) | {
440
+ if n == uint:: max_value - 5000 { break }
441
+ assert n > uint:: max_value - 5000 ;
442
+
443
+ assert k == n;
444
+ assert * v == n / 2 ;
445
+ n -= 1 ;
446
+ }
447
+ }
369
448
}
0 commit comments