@@ -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_reverse ( 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
@@ -385,6 +390,25 @@ mod tests {
385390 }
386391 }
387392
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+
388412 #[ test]
389413 fn test_each_reverse ( ) {
390414 let mut m = TrieMap : : new( ) ;
@@ -402,4 +426,23 @@ mod tests {
402426 n -= 1 ;
403427 }
404428 }
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+ }
405448}
0 commit comments