25
25
AxisSliceInfo ,
26
26
NdIndex ,
27
27
Slice ,
28
- SliceInfo ,
29
28
} ;
30
29
use iter:: {
31
30
AxisChunksIter ,
@@ -42,6 +41,7 @@ use iter::{
42
41
ExactChunksMut ,
43
42
Windows
44
43
} ;
44
+ use slice:: CanSlice ;
45
45
use stacking:: stack;
46
46
47
47
/// # Methods For All Array Types
@@ -303,9 +303,9 @@ where
303
303
///
304
304
/// **Panics** if an index is out of bounds or step size is zero.<br>
305
305
/// (**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 >
307
307
where
308
- Do : Dimension ,
308
+ I : CanSlice < D > ,
309
309
S : Data ,
310
310
{
311
311
self . view ( ) . slice_move ( info)
@@ -321,9 +321,9 @@ where
321
321
///
322
322
/// **Panics** if an index is out of bounds or step size is zero.<br>
323
323
/// (**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 >
325
325
where
326
- Do : Dimension ,
326
+ I : CanSlice < D > ,
327
327
S : DataMut ,
328
328
{
329
329
self . view_mut ( ) . slice_move ( info)
@@ -339,29 +339,37 @@ where
339
339
///
340
340
/// **Panics** if an index is out of bounds or step size is zero.<br>
341
341
/// (**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 >
343
343
where
344
- Do : Dimension ,
344
+ I : CanSlice < D > ,
345
345
{
346
346
// Slice and collapse in-place without changing the number of dimensions.
347
- self . slice_collapse ( & * info) ;
347
+ self . slice_collapse ( info) ;
348
348
349
- let indices: & [ AxisSliceInfo ] = ( * * info) . as_ref ( ) ;
350
-
351
- // Copy the dim and strides that remain after removing the subview axes.
352
349
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
+ }
364
369
} ) ;
370
+ debug_assert_eq ! ( old_axis, self . ndim( ) ) ;
371
+ debug_assert_eq ! ( new_axis, out_ndim) ;
372
+ }
365
373
366
374
ArrayBase {
367
375
ptr : self . ptr ,
@@ -373,25 +381,23 @@ where
373
381
374
382
/// Slice the array in place without changing the number of dimensions.
375
383
///
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
- ///
381
384
/// See [*Slicing*](#slicing) for full documentation.
382
- /// See also [`D::SliceArg`].
383
- ///
384
- /// [`D::SliceArg`]: trait.Dimension.html#associatedtype.SliceArg
385
385
///
386
386
/// **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 ( )
392
398
. iter ( )
393
399
. enumerate ( )
394
- . for_each ( |( axis, slice_or_index ) | match slice_or_index {
400
+ . for_each ( |( axis, ax_info ) | match ax_info {
395
401
& AxisSliceInfo :: Slice { start, end, step } => {
396
402
self . slice_axis_inplace ( Axis ( axis) , Slice { start, end, step } )
397
403
}
@@ -407,7 +413,10 @@ where
407
413
/// **Panics** if an index is out of bounds or step size is zero.<br>
408
414
/// (**Panics** if `D` is `IxDyn` and `indices` does not match the number of array axes.)
409
415
#[ 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
+ {
411
420
self . slice_collapse ( indices)
412
421
}
413
422
0 commit comments