@@ -86,14 +86,21 @@ where
8686 F : FnMut ( B , D :: Pattern ) -> B ,
8787 {
8888 let IndicesIter { mut index, dim } = self ;
89- let inner_axis = dim. ndim ( ) - 1 ;
89+ let ndim = dim. ndim ( ) ;
90+ if ndim == 0 {
91+ return match index {
92+ Some ( ix) => f ( init, ix. into_pattern ( ) ) ,
93+ None => init,
94+ } ;
95+ }
96+ let inner_axis = ndim - 1 ;
9097 let inner_len = dim[ inner_axis] ;
9198 let mut acc = init;
9299 while let Some ( mut ix) = index {
93100 // unroll innermost axis
94- while ix[ inner_axis] < inner_len {
101+ for i in ix[ inner_axis] ..inner_len {
102+ ix[ inner_axis] = i;
95103 acc = f ( acc, ix. clone ( ) . into_pattern ( ) ) ;
96- ix[ inner_axis] += 1 ;
97104 }
98105 index = dim. next_for ( ix) ;
99106 }
@@ -304,17 +311,30 @@ mod tests {
304311
305312 #[ test]
306313 fn test_indices_iter_c_fold ( ) {
307- let dim = ( 3 , 4 ) ;
308- let mut it = indices ( dim) . into_iter ( ) ;
309- it. next ( ) ;
310- let clone = it. clone ( ) ;
311- let len = it. len ( ) ;
312- let acc = clone. fold ( 0 , |acc, ix| {
313- assert_eq ! ( ix, it. next( ) . unwrap( ) ) ;
314- acc + 1
315- } ) ;
316- assert_eq ! ( acc, len) ;
317- assert ! ( it. next( ) . is_none( ) ) ;
314+ macro_rules! run_test {
315+ ( $dim: expr) => {
316+ for num_consume in 0 ..3 {
317+ let mut it = indices( $dim) . into_iter( ) ;
318+ for _ in 0 ..num_consume {
319+ it. next( ) ;
320+ }
321+ let clone = it. clone( ) ;
322+ let len = it. len( ) ;
323+ let acc = clone. fold( 0 , |acc, ix| {
324+ assert_eq!( ix, it. next( ) . unwrap( ) ) ;
325+ acc + 1
326+ } ) ;
327+ assert_eq!( acc, len) ;
328+ assert!( it. next( ) . is_none( ) ) ;
329+ }
330+ } ;
331+ }
332+ run_test ! ( ( ) ) ;
333+ run_test ! ( ( 2 , ) ) ;
334+ run_test ! ( ( 2 , 3 ) ) ;
335+ run_test ! ( ( 2 , 0 , 3 ) ) ;
336+ run_test ! ( ( 2 , 3 , 4 ) ) ;
337+ run_test ! ( ( 2 , 3 , 4 , 2 ) ) ;
318338 }
319339
320340 #[ test]
0 commit comments