@@ -3,8 +3,9 @@ pub mod info;
33pub use info:: Info ;
44
55use super :: { data_tree:: DataTree , size} ;
6- use itertools :: Itertools ;
6+ use pipe_trait :: Pipe ;
77use rayon:: prelude:: * ;
8+ use std:: { marker:: PhantomData , ops:: Not } ;
89
910/// Collection of functions and starting points in order to build a [`DataTree`] with [`From`] or [`Into`].
1011#[ derive( Debug ) ]
5657
5758 let children = children
5859 . into_iter ( )
59- . chunks ( 64 )
60- . into_iter ( )
61- . map ( Vec :: < NameIter :: Item > :: from_iter)
62- . map ( |names| -> Vec < _ > {
60+ . pipe ( Chunks :: < 64 , _ > :: new)
61+ . map ( |names : Vec < _ > | -> Vec < _ > {
6362 names
6463 . into_par_iter ( )
6564 . map ( |name| TreeBuilder {
8584 }
8685 }
8786}
87+
88+ /// Utility type to iterate over each `Vec` of at most `LEN` items.
89+ #[ derive( Debug , Clone , Copy ) ]
90+ struct Chunks < const LEN : usize , Iter > {
91+ iter : Iter ,
92+ _phantom : PhantomData < [ ( ) ; LEN ] > ,
93+ }
94+
95+ impl < const LEN : usize , Iter > Chunks < LEN , Iter > {
96+ /// Start iterating chunks.
97+ fn new ( iter : Iter ) -> Self {
98+ Chunks {
99+ iter,
100+ _phantom : PhantomData ,
101+ }
102+ }
103+ }
104+
105+ impl < const LEN : usize , Iter > Iterator for Chunks < LEN , Iter >
106+ where
107+ Iter : Iterator ,
108+ {
109+ type Item = Vec < Iter :: Item > ;
110+ fn next ( & mut self ) -> Option < Self :: Item > {
111+ let chunk: Vec < _ > = self . iter . by_ref ( ) . take ( LEN ) . collect ( ) ;
112+ chunk. is_empty ( ) . not ( ) . then_some ( chunk)
113+ }
114+ }
0 commit comments