@@ -32,7 +32,7 @@ impl<T> BaseIter<(uint, &T)> for TrieMap<T> {
3232 /// Visit all key-value pairs in order
3333 #[ inline( always) ]
3434 pure fn each ( & self , f : fn ( & ( uint, & self /T ) ) -> bool ) {
35- self . root . each ( f)
35+ self . root . each ( f) ;
3636 }
3737 #[ inline( always) ]
3838 pure fn size_hint ( & self ) -> Option < uint > { Some ( self . len ( ) ) }
@@ -42,7 +42,7 @@ impl<T> ReverseIter<(uint, &T)> for TrieMap<T> {
4242 /// Visit all key-value pairs in reverse order
4343 #[ inline( always) ]
4444 pure fn each_reverse ( & self , f : fn ( & ( uint, & self /T ) ) -> bool ) {
45- self . root . each_reverse ( f)
45+ self . root . each_reverse ( f) ;
4646 }
4747}
4848
@@ -149,7 +149,7 @@ impl<T> TrieMap<T> {
149149
150150 /// Iterate over the map and mutate the contained values
151151 fn mutate_values ( & mut self , f : fn ( uint , & mut T ) -> bool ) {
152- self . root . mutate_values ( f)
152+ self . root . mutate_values ( f) ;
153153 }
154154}
155155
@@ -217,34 +217,39 @@ impl<T: Copy> TrieNode<T> {
217217}
218218
219219impl < 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 {
221221 for uint:: range( 0 , self . children. len( ) ) |idx| {
222222 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 } ,
225225 Nothing => ( )
226226 }
227227 }
228+ true
228229 }
229230
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 {
231232 for uint:: range_rev( self . children. len( ) , 0 ) |idx| {
232233 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 } ,
235236 Nothing => ( )
236237 }
237238 }
239+ true
238240 }
239241
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 {
241243 for vec:: each_mut( self . children) |child| {
242244 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 } ,
245249 Nothing => ( )
246250 }
247251 }
252+ true
248253 }
249254}
250255
@@ -366,4 +371,78 @@ mod tests {
366371 check_integrity( & trie. root) ;
367372 }
368373 }
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+ }
369448}
0 commit comments