@@ -156,6 +156,15 @@ pub trait Dimension : Clone + Eq + Debug + Send + Sync + Default +
156
156
Self :: default ( )
157
157
}
158
158
159
+ #[ doc( hidden) ]
160
+ /// Return an index of same type and with the specified dimensionality.
161
+ ///
162
+ /// This method is useful for generalizing over fixed-size and
163
+ /// variable-size dimension representations.
164
+ ///
165
+ /// **Panics** if `Self` has a fixed size that is not `ndim`.
166
+ fn zero_index_with_ndim ( ndim : usize ) -> Self ;
167
+
159
168
#[ doc( hidden) ]
160
169
#[ inline]
161
170
fn first_index ( & self ) -> Option < Self > {
@@ -371,6 +380,11 @@ impl Dimension for Dim<[Ix; 0]> {
371
380
#[ inline]
372
381
fn into_pattern ( self ) -> Self :: Pattern { }
373
382
#[ inline]
383
+ fn zero_index_with_ndim ( ndim : usize ) -> Self {
384
+ assert_eq ! ( ndim, 0 ) ;
385
+ Self :: default ( )
386
+ }
387
+ #[ inline]
374
388
fn next_for ( & self , _index : Self ) -> Option < Self > {
375
389
None
376
390
}
@@ -402,6 +416,11 @@ impl Dimension for Dim<[Ix; 1]> {
402
416
get ! ( & self , 0 )
403
417
}
404
418
#[ inline]
419
+ fn zero_index_with_ndim ( ndim : usize ) -> Self {
420
+ assert_eq ! ( ndim, 1 ) ;
421
+ Self :: default ( )
422
+ }
423
+ #[ inline]
405
424
fn next_for ( & self , mut index : Self ) -> Option < Self > {
406
425
getm ! ( index, 0 ) += 1 ;
407
426
if get ! ( & index, 0 ) < get ! ( self , 0 ) {
@@ -491,6 +510,11 @@ impl Dimension for Dim<[Ix; 2]> {
491
510
#[ inline]
492
511
fn slice_mut ( & mut self ) -> & mut [ Ix ] { self . ixm ( ) }
493
512
#[ inline]
513
+ fn zero_index_with_ndim ( ndim : usize ) -> Self {
514
+ assert_eq ! ( ndim, 2 ) ;
515
+ Self :: default ( )
516
+ }
517
+ #[ inline]
494
518
fn next_for ( & self , index : Self ) -> Option < Self > {
495
519
let mut i = get ! ( & index, 0 ) ;
496
520
let mut j = get ! ( & index, 1 ) ;
@@ -630,6 +654,12 @@ impl Dimension for Dim<[Ix; 3]> {
630
654
m as usize * n as usize * o as usize
631
655
}
632
656
657
+ #[ inline]
658
+ fn zero_index_with_ndim ( ndim : usize ) -> Self {
659
+ assert_eq ! ( ndim, 3 ) ;
660
+ Self :: default ( )
661
+ }
662
+
633
663
#[ inline]
634
664
fn next_for ( & self , index : Self ) -> Option < Self > {
635
665
let mut i = get ! ( & index, 0 ) ;
@@ -734,6 +764,11 @@ macro_rules! large_dim {
734
764
#[ inline]
735
765
fn slice_mut( & mut self ) -> & mut [ Ix ] { self . ixm( ) }
736
766
#[ inline]
767
+ fn zero_index_with_ndim( ndim: usize ) -> Self {
768
+ assert_eq!( ndim, $n) ;
769
+ Self :: default ( )
770
+ }
771
+ #[ inline]
737
772
$( $insert_axis) *
738
773
#[ inline]
739
774
fn try_remove_axis( & self , axis: Axis ) -> Self :: Smaller {
@@ -786,6 +821,11 @@ impl Dimension for IxDyn
786
821
IxDyn :: zeros ( self . ndim ( ) )
787
822
}
788
823
824
+ #[ inline]
825
+ fn zero_index_with_ndim ( ndim : usize ) -> Self {
826
+ IxDyn :: zeros ( ndim)
827
+ }
828
+
789
829
#[ inline]
790
830
fn insert_axis ( & self , axis : Axis ) -> Self :: Larger {
791
831
debug_assert ! ( axis. index( ) <= self . ndim( ) ) ;
0 commit comments