@@ -20,6 +20,7 @@ use crate::NdIndex;
2020
2121use crate :: indexes:: { indices, Indices } ;
2222use crate :: layout:: { CORDER , FORDER } ;
23+ use crate :: split_at:: { SplitPreference , SplitAt } ;
2324
2425use partial_array:: PartialArray ;
2526
9293 private_impl ! { }
9394}
9495
95- pub trait Splittable : Sized {
96- fn split_at ( self , axis : Axis , index : Ix ) -> ( Self , Self ) ;
97- }
98-
99- impl < D > Splittable for D
100- where
101- D : Dimension ,
102- {
103- fn split_at ( self , axis : Axis , index : Ix ) -> ( Self , Self ) {
104- let mut d1 = self ;
105- let mut d2 = d1. clone ( ) ;
106- let i = axis. index ( ) ;
107- let len = d1[ i] ;
108- d1[ i] = index;
109- d2[ i] = len - index;
110- ( d1, d2)
111- }
112- }
113-
11496/// Argument conversion into a producer.
11597///
11698/// Slices and vectors can be used (equivalent to 1-dimensional array views).
@@ -1121,9 +1103,31 @@ macro_rules! map_impl {
11211103 pub fn split( self ) -> ( Self , Self ) {
11221104 debug_assert_ne!( self . size( ) , 0 , "Attempt to split empty zip" ) ;
11231105 debug_assert_ne!( self . size( ) , 1 , "Attempt to split zip with 1 elem" ) ;
1106+ SplitPreference :: split( self )
1107+ }
1108+ }
1109+
1110+ impl <D , $( $p) ,* > SplitPreference for Zip <( $( $p, ) * ) , D >
1111+ where D : Dimension ,
1112+ $( $p: NdProducer <Dim =D > , ) *
1113+ {
1114+ fn can_split( & self ) -> bool { self . size( ) > 1 }
1115+
1116+ fn size( & self ) -> usize { self . size( ) }
1117+
1118+ fn split_preference( & self ) -> ( Axis , usize ) {
11241119 // Always split in a way that preserves layout (if any)
11251120 let axis = self . max_stride_axis( ) ;
11261121 let index = self . len_of( axis) / 2 ;
1122+ ( axis, index)
1123+ }
1124+ }
1125+
1126+ impl <D , $( $p) ,* > SplitAt for Zip <( $( $p, ) * ) , D >
1127+ where D : Dimension ,
1128+ $( $p: NdProducer <Dim =D > , ) *
1129+ {
1130+ fn split_at( self , axis: Axis , index: usize ) -> ( Self , Self ) {
11271131 let ( p1, p2) = self . parts. split_at( axis, index) ;
11281132 let ( d1, d2) = self . dimension. split_at( axis, index) ;
11291133 ( Zip {
@@ -1139,7 +1143,9 @@ macro_rules! map_impl {
11391143 layout_tendency: self . layout_tendency,
11401144 } )
11411145 }
1146+
11421147 }
1148+
11431149 ) +
11441150 }
11451151}
0 commit comments