@@ -15,6 +15,7 @@ use itertools::{enumerate, zip};
1515
1616use { Ix , Ixs , Ix0 , Ix1 , Ix2 , Ix3 , IxDyn , Dim , Si } ;
1717use IntoDimension ;
18+ use RemoveAxis ;
1819use { ArrayView1 , ArrayViewMut1 } ;
1920use { zipsl, zipsl_mut, ZipExt } ;
2021use Axis ;
@@ -64,6 +65,9 @@ pub unsafe trait Dimension : Clone + Eq + Debug + Send + Sync + Default +
6465 /// - and so on..
6566 /// - For `Vec<Ix>`: `Vec<usize>`,
6667 type Pattern : IntoDimension < Dim =Self > ;
68+ // Next smaller dimension (if it exists)
69+ #[ doc( hidden) ]
70+ type TrySmaller : Dimension ;
6771 #[ doc( hidden) ]
6872 fn ndim ( & self ) -> usize ;
6973
@@ -341,6 +345,9 @@ pub unsafe trait Dimension : Clone + Eq + Debug + Send + Sync + Default +
341345 . max_by_key ( |ax| ax. stride ( ) . abs ( ) )
342346 . map_or ( Axis ( 0 ) , |ax| ax. axis ( ) )
343347 }
348+
349+ #[ doc( hidden) ]
350+ fn try_remove_axis ( & self , axis : Axis ) -> Self :: TrySmaller ;
344351}
345352
346353// utility functions
@@ -361,6 +368,7 @@ fn abs_index(len: Ixs, index: Ixs) -> Ix {
361368unsafe impl Dimension for Dim < [ Ix ; 0 ] > {
362369 type SliceArg = [ Si ; 0 ] ;
363370 type Pattern = ( ) ;
371+ type TrySmaller = Self ;
364372 // empty product is 1 -> size is 1
365373 #[ inline]
366374 fn ndim ( & self ) -> usize { 0 }
@@ -376,12 +384,17 @@ unsafe impl Dimension for Dim<[Ix; 0]> {
376384 fn next_for ( & self , _index : Self ) -> Option < Self > {
377385 None
378386 }
387+ #[ inline]
388+ fn try_remove_axis ( & self , _ignore : Axis ) -> Self :: TrySmaller {
389+ * self
390+ }
379391}
380392
381393
382394unsafe impl Dimension for Dim < [ Ix ; 1 ] > {
383395 type SliceArg = [ Si ; 1 ] ;
384396 type Pattern = Ix ;
397+ type TrySmaller = <Self as RemoveAxis >:: Smaller ;
385398 #[ inline]
386399 fn ndim ( & self ) -> usize { 1 }
387400 #[ inline]
@@ -456,11 +469,16 @@ unsafe impl Dimension for Dim<[Ix; 1]> {
456469 None
457470 }
458471 }
472+ #[ inline]
473+ fn try_remove_axis ( & self , axis : Axis ) -> Self :: TrySmaller {
474+ self . remove_axis ( axis)
475+ }
459476}
460477
461478unsafe impl Dimension for Dim < [ Ix ; 2 ] > {
462479 type SliceArg = [ Si ; 2 ] ;
463480 type Pattern = ( Ix , Ix ) ;
481+ type TrySmaller = <Self as RemoveAxis >:: Smaller ;
464482 #[ inline]
465483 fn ndim ( & self ) -> usize { 2 }
466484 #[ inline]
@@ -601,11 +619,16 @@ unsafe impl Dimension for Dim<[Ix; 2]> {
601619 None
602620 }
603621 }
622+ #[ inline]
623+ fn try_remove_axis ( & self , axis : Axis ) -> Self :: TrySmaller {
624+ self . remove_axis ( axis)
625+ }
604626}
605627
606628unsafe impl Dimension for Dim < [ Ix ; 3 ] > {
607629 type SliceArg = [ Si ; 3 ] ;
608630 type Pattern = ( Ix , Ix , Ix ) ;
631+ type TrySmaller = <Self as RemoveAxis >:: Smaller ;
609632 #[ inline]
610633 fn ndim ( & self ) -> usize { 3 }
611634 #[ inline]
@@ -681,13 +704,18 @@ unsafe impl Dimension for Dim<[Ix; 3]> {
681704 }
682705 order
683706 }
707+ #[ inline]
708+ fn try_remove_axis ( & self , axis : Axis ) -> Self :: TrySmaller {
709+ self . remove_axis ( axis)
710+ }
684711}
685712
686713macro_rules! large_dim {
687714 ( $n: expr, $name: ident, $( $ix: ident) ,+) => (
688715 unsafe impl Dimension for Dim <[ Ix ; $n] > {
689716 type SliceArg = [ Si ; $n] ;
690717 type Pattern = ( $( $ix, ) * ) ;
718+ type TrySmaller = <Self as RemoveAxis >:: Smaller ;
691719 #[ inline]
692720 fn ndim( & self ) -> usize { $n }
693721 #[ inline]
@@ -698,6 +726,10 @@ macro_rules! large_dim {
698726 fn slice( & self ) -> & [ Ix ] { self . ix( ) }
699727 #[ inline]
700728 fn slice_mut( & mut self ) -> & mut [ Ix ] { self . ixm( ) }
729+ #[ inline]
730+ fn try_remove_axis( & self , axis: Axis ) -> Self :: TrySmaller {
731+ self . remove_axis( axis)
732+ }
701733 }
702734 )
703735}
@@ -712,13 +744,25 @@ unsafe impl Dimension for IxDyn
712744{
713745 type SliceArg = [ Si ] ;
714746 type Pattern = Self ;
747+ type TrySmaller = <Self as RemoveAxis >:: Smaller ;
748+ #[ inline]
715749 fn ndim ( & self ) -> usize { self . ix ( ) . len ( ) }
750+ #[ inline]
716751 fn slice ( & self ) -> & [ Ix ] { self . ix ( ) }
752+ #[ inline]
717753 fn slice_mut ( & mut self ) -> & mut [ Ix ] { self . ixm ( ) }
718754 #[ inline]
719755 fn into_pattern ( self ) -> Self :: Pattern {
720756 self
721757 }
758+ #[ inline]
759+ fn try_remove_axis ( & self , axis : Axis ) -> Self :: TrySmaller {
760+ if self . ndim ( ) > 0 {
761+ self . remove_axis ( axis)
762+ } else {
763+ self . clone ( )
764+ }
765+ }
722766}
723767
724768impl < J > Index < J > for Dim < Vec < usize > >
0 commit comments