2525 AxisSliceInfo ,
2626 NdIndex ,
2727 Slice ,
28- SliceInfo ,
2928} ;
3029use iter:: {
3130 AxisChunksIter ,
@@ -42,6 +41,7 @@ use iter::{
4241 ExactChunksMut ,
4342 Windows
4443} ;
44+ use slice:: CanSlice ;
4545use stacking:: stack;
4646
4747/// # Methods For All Array Types
@@ -303,9 +303,9 @@ where
303303 ///
304304 /// **Panics** if an index is out of bounds or step size is zero.<br>
305305 /// (**Panics** if `D` is `IxDyn` and `info` does not match the number of array axes.)
306- pub fn slice < Do > ( & self , info : & SliceInfo < D :: SliceArg , Do > ) -> ArrayView < A , Do >
306+ pub fn slice < I > ( & self , info : & I ) -> ArrayView < A , I :: OutDim >
307307 where
308- Do : Dimension ,
308+ I : CanSlice < D > ,
309309 S : Data ,
310310 {
311311 self . view ( ) . slice_move ( info)
@@ -321,9 +321,9 @@ where
321321 ///
322322 /// **Panics** if an index is out of bounds or step size is zero.<br>
323323 /// (**Panics** if `D` is `IxDyn` and `info` does not match the number of array axes.)
324- pub fn slice_mut < Do > ( & mut self , info : & SliceInfo < D :: SliceArg , Do > ) -> ArrayViewMut < A , Do >
324+ pub fn slice_mut < I > ( & mut self , info : & I ) -> ArrayViewMut < A , I :: OutDim >
325325 where
326- Do : Dimension ,
326+ I : CanSlice < D > ,
327327 S : DataMut ,
328328 {
329329 self . view_mut ( ) . slice_move ( info)
@@ -339,29 +339,37 @@ where
339339 ///
340340 /// **Panics** if an index is out of bounds or step size is zero.<br>
341341 /// (**Panics** if `D` is `IxDyn` and `info` does not match the number of array axes.)
342- pub fn slice_move < Do > ( mut self , info : & SliceInfo < D :: SliceArg , Do > ) -> ArrayBase < S , Do >
342+ pub fn slice_move < I > ( mut self , info : & I ) -> ArrayBase < S , I :: OutDim >
343343 where
344- Do : Dimension ,
344+ I : CanSlice < D > ,
345345 {
346346 // Slice and collapse in-place without changing the number of dimensions.
347- self . slice_collapse ( & * info) ;
347+ self . slice_collapse ( info) ;
348348
349- let indices: & [ AxisSliceInfo ] = ( * * info) . as_ref ( ) ;
350-
351- // Copy the dim and strides that remain after removing the subview axes.
352349 let out_ndim = info. out_ndim ( ) ;
353- let mut new_dim = Do :: zeros ( out_ndim) ;
354- let mut new_strides = Do :: zeros ( out_ndim) ;
355- izip ! ( self . dim. slice( ) , self . strides. slice( ) , indices)
356- . filter_map ( |( d, s, slice_or_index) | match slice_or_index {
357- & AxisSliceInfo :: Slice { ..} => Some ( ( d, s) ) ,
358- & AxisSliceInfo :: Index ( _) => None ,
359- } )
360- . zip ( izip ! ( new_dim. slice_mut( ) , new_strides. slice_mut( ) ) )
361- . for_each ( |( ( d, s) , ( new_d, new_s) ) | {
362- * new_d = * d;
363- * new_s = * s;
350+ let mut new_dim = I :: OutDim :: zeros ( out_ndim) ;
351+ let mut new_strides = I :: OutDim :: zeros ( out_ndim) ;
352+
353+ // Write the dim and strides to the correct new axes.
354+ {
355+ let mut old_axis = 0 ;
356+ let mut new_axis = 0 ;
357+ info. as_ref ( ) . iter ( ) . for_each ( |ax_info| match ax_info {
358+ & AxisSliceInfo :: Slice { .. } => {
359+ // Copy the old dim and stride to corresponding axis.
360+ new_dim[ new_axis] = self . dim [ old_axis] ;
361+ new_strides[ new_axis] = self . strides [ old_axis] ;
362+ old_axis += 1 ;
363+ new_axis += 1 ;
364+ }
365+ & AxisSliceInfo :: Index ( _) => {
366+ // Skip the old axis since it should be removed.
367+ old_axis += 1 ;
368+ }
364369 } ) ;
370+ debug_assert_eq ! ( old_axis, self . ndim( ) ) ;
371+ debug_assert_eq ! ( new_axis, out_ndim) ;
372+ }
365373
366374 ArrayBase {
367375 ptr : self . ptr ,
@@ -373,25 +381,23 @@ where
373381
374382 /// Slice the array in place without changing the number of dimensions.
375383 ///
376- /// Note that [`&SliceInfo`](struct.SliceInfo.html) (produced by the
377- /// [`s![]`](macro.s!.html) macro) will usually coerce into `&D::SliceArg`
378- /// automatically, but in some cases (e.g. if `D` is `IxDyn`), you may need
379- /// to call `.as_ref()`.
380- ///
381384 /// See [*Slicing*](#slicing) for full documentation.
382- /// See also [`D::SliceArg`].
383- ///
384- /// [`D::SliceArg`]: trait.Dimension.html#associatedtype.SliceArg
385385 ///
386386 /// **Panics** if an index is out of bounds or step size is zero.<br>
387- /// (**Panics** if `D` is `IxDyn` and `indices` does not match the number of array axes.)
388- pub fn slice_collapse ( & mut self , indices : & D :: SliceArg ) {
389- let indices: & [ AxisSliceInfo ] = indices. as_ref ( ) ;
390- assert_eq ! ( indices. len( ) , self . ndim( ) ) ;
391- indices
387+ /// (**Panics** if `D` is `IxDyn` and `info` does not match the number of array axes.)
388+ pub fn slice_collapse < I > ( & mut self , info : & I )
389+ where
390+ I : CanSlice < D > ,
391+ {
392+ assert_eq ! (
393+ info. in_ndim( ) ,
394+ self . ndim( ) ,
395+ "The input dimension of `info` must match the array to be sliced." ,
396+ ) ;
397+ info. as_ref ( )
392398 . iter ( )
393399 . enumerate ( )
394- . for_each ( |( axis, slice_or_index ) | match slice_or_index {
400+ . for_each ( |( axis, ax_info ) | match ax_info {
395401 & AxisSliceInfo :: Slice { start, end, step } => {
396402 self . slice_axis_inplace ( Axis ( axis) , Slice { start, end, step } )
397403 }
@@ -407,7 +413,10 @@ where
407413 /// **Panics** if an index is out of bounds or step size is zero.<br>
408414 /// (**Panics** if `D` is `IxDyn` and `indices` does not match the number of array axes.)
409415 #[ deprecated( note="renamed to `slice_collapse`" , since="0.12.1" ) ]
410- pub fn slice_inplace ( & mut self , indices : & D :: SliceArg ) {
416+ pub fn slice_inplace < I > ( & mut self , indices : & I )
417+ where
418+ I : CanSlice < D > ,
419+ {
411420 self . slice_collapse ( indices)
412421 }
413422
0 commit comments