File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change 80
80
} ;
81
81
( l, Some ( l) )
82
82
}
83
+
84
+ fn fold < B , F > ( self , init : B , mut f : F ) -> B
85
+ where
86
+ F : FnMut ( B , D :: Pattern ) -> B ,
87
+ {
88
+ let IndicesIter { mut index, dim } = self ;
89
+ let inner_axis = dim. ndim ( ) - 1 ;
90
+ let inner_len = dim[ inner_axis] ;
91
+ let mut acc = init;
92
+ while let Some ( mut ix) = index {
93
+ // unroll innermost axis
94
+ while ix[ inner_axis] < inner_len {
95
+ acc = f ( acc, ix. clone ( ) . into_pattern ( ) ) ;
96
+ ix[ inner_axis] += 1 ;
97
+ }
98
+ index = dim. next_for ( ix) ;
99
+ }
100
+ acc
101
+ }
83
102
}
84
103
85
104
impl < D > ExactSizeIterator for IndicesIter < D > where D : Dimension { }
@@ -283,6 +302,21 @@ mod tests {
283
302
assert_eq ! ( len, 0 ) ;
284
303
}
285
304
305
+ #[ test]
306
+ 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( ) ) ;
318
+ }
319
+
286
320
#[ test]
287
321
fn test_indices_iter_f_size_hint ( ) {
288
322
let dim = ( 3 , 4 ) ;
You can’t perform that action at this time.
0 commit comments