@@ -15,6 +15,7 @@ use crate::iter::AxisIter;
1515use crate :: iter:: AxisIterMut ;
1616use crate :: Dimension ;
1717use crate :: { ArrayView , ArrayViewMut } ;
18+ use crate :: split_at:: SplitPreference ;
1819
1920/// Parallel iterator wrapper.
2021#[ derive( Copy , Clone , Debug ) ]
@@ -170,7 +171,14 @@ macro_rules! par_iter_view_wrapper {
170171 fn fold_with<F >( self , folder: F ) -> F
171172 where F : Folder <Self :: Item >,
172173 {
173- self . into_iter( ) . fold( folder, move |f, elt| f. consume( elt) )
174+ Zip :: from( self . 0 ) . fold_while( folder, |mut folder, elt| {
175+ folder = folder. consume( elt) ;
176+ if folder. full( ) {
177+ FoldWhile :: Done ( folder)
178+ } else {
179+ FoldWhile :: Continue ( folder)
180+ }
181+ } ) . into_inner( )
174182 }
175183 }
176184
@@ -243,7 +251,7 @@ macro_rules! zip_impl {
243251 type Item = ( $( $p:: Item , ) * ) ;
244252
245253 fn split( self ) -> ( Self , Option <Self >) {
246- if self . 0 . size ( ) <= 1 {
254+ if ! self . 0 . can_split ( ) {
247255 return ( self , None )
248256 }
249257 let ( a, b) = self . 0 . split( ) ;
@@ -275,3 +283,53 @@ zip_impl! {
275283 [ P1 P2 P3 P4 P5 ] ,
276284 [ P1 P2 P3 P4 P5 P6 ] ,
277285}
286+
287+ /// A parallel iterator (unindexed) that produces the splits of the array
288+ /// or producer `P`.
289+ pub ( crate ) struct ParallelSplits < P > {
290+ pub ( crate ) iter : P ,
291+ pub ( crate ) max_splits : usize ,
292+ }
293+
294+ impl < P > ParallelIterator for ParallelSplits < P >
295+ where P : SplitPreference + Send ,
296+ {
297+ type Item = P ;
298+
299+ fn drive_unindexed < C > ( self , consumer : C ) -> C :: Result
300+ where C : UnindexedConsumer < Self :: Item >
301+ {
302+ bridge_unindexed ( self , consumer)
303+ }
304+
305+ fn opt_len ( & self ) -> Option < usize > {
306+ None
307+ }
308+ }
309+
310+ impl < P > UnindexedProducer for ParallelSplits < P >
311+ where P : SplitPreference + Send ,
312+ {
313+ type Item = P ;
314+
315+ fn split ( self ) -> ( Self , Option < Self > ) {
316+ if self . max_splits == 0 || !self . iter . can_split ( ) {
317+ return ( self , None )
318+ }
319+ let ( a, b) = self . iter . split ( ) ;
320+ ( ParallelSplits {
321+ iter : a,
322+ max_splits : self . max_splits - 1 ,
323+ } ,
324+ Some ( ParallelSplits {
325+ iter : b,
326+ max_splits : self . max_splits - 1 ,
327+ } ) )
328+ }
329+
330+ fn fold_with < Fold > ( self , folder : Fold ) -> Fold
331+ where Fold : Folder < Self :: Item > ,
332+ {
333+ folder. consume ( self . iter )
334+ }
335+ }
0 commit comments