11use alloc:: boxed:: Box ;
22use alloc:: vec:: Vec ;
3+ use core:: iter:: Cloned ;
34use core:: ops:: { Add , AddAssign , Mul , MulAssign , Sub , SubAssign } ;
45use core:: ops:: { Deref , DerefMut , Index , IndexMut } ;
56use core:: slice:: Iter ;
6- use num_traits:: Zero ;
77
88use crate :: layout:: rank:: DynRank ;
99use crate :: layout:: ranked:: Ranked ;
10- use crate :: layout:: shape:: { IntoShape , Shape } ;
10+ use crate :: layout:: shape:: Shape ;
1111use crate :: Axis ;
1212
1313const CAP : usize = 4 ;
@@ -19,19 +19,9 @@ pub enum DynAxesRepr<T>
1919 Alloc ( Box < [ T ] > ) ,
2020}
2121
22+ /// An array shape with a dynamic rank.
2223pub type DShape = DynAxesRepr < usize > ;
2324
24- impl < T > DynAxesRepr < T >
25- {
26- fn ndim ( & self ) -> usize
27- {
28- match self {
29- DynAxesRepr :: Inline ( len, _) => * len,
30- DynAxesRepr :: Alloc ( items) => items. len ( ) ,
31- }
32- }
33- }
34-
3525impl < T > Deref for DynAxesRepr < T >
3626{
3727 type Target = [ T ] ;
@@ -52,7 +42,13 @@ impl<T> DerefMut for DynAxesRepr<T>
5242{
5343 fn deref_mut ( & mut self ) -> & mut Self :: Target
5444 {
55- todo ! ( )
45+ match self {
46+ DynAxesRepr :: Inline ( len, arr) => {
47+ debug_assert ! ( * len <= arr. len( ) ) ;
48+ unsafe { arr. get_unchecked_mut ( ..* len) }
49+ }
50+ DynAxesRepr :: Alloc ( items) => items,
51+ }
5652 }
5753}
5854
@@ -92,33 +88,6 @@ impl<T> IndexMut<Axis> for DynAxesRepr<T>
9288 }
9389}
9490
95- impl < T > Default for DynAxesRepr < T >
96- where T : Zero + Copy
97- {
98- fn default ( ) -> Self
99- {
100- Self :: Inline ( 1 , [ T :: zero ( ) ; 4 ] )
101- }
102- }
103-
104- impl Zero for DShape
105- {
106- fn zero ( ) -> Self
107- {
108- Self :: Inline ( 1 , [ 0usize ; CAP ] )
109- }
110-
111- fn is_zero ( & self ) -> bool
112- {
113- self . iter ( ) . all ( |e| e. is_zero ( ) )
114- }
115-
116- fn set_zero ( & mut self )
117- {
118- self . iter_mut ( ) . for_each ( |e| e. set_zero ( ) ) ;
119- }
120- }
121-
12291impl < Rhs , T > From < Rhs > for DynAxesRepr < T >
12392where
12493 Rhs : AsRef < [ T ] > ,
@@ -156,7 +125,7 @@ macro_rules! impl_op {
156125 /// *Panics* if the two dimensionalities are different
157126 impl <Rhs > $op_trait for DShape
158127 where
159- Rhs : IntoShape < NDim = DynRank >,
128+ Rhs : Into < Self >,
160129 {
161130 type Output = DShape ;
162131
@@ -170,10 +139,10 @@ macro_rules! impl_op {
170139 /// *Panics* if the two dimensionalities are different
171140 impl <Rhs > $op_assign_trait for DShape
172141 where
173- Rhs : IntoShape < NDim = DynRank >,
142+ Rhs : Into < Self >,
174143 {
175144 fn $op_assign_fn( & mut self , rhs: Rhs ) {
176- let other = rhs. into_shape ( ) ;
145+ let other = rhs. into ( ) ;
177146 for i in 0 ..self . ndim( ) . max( other. ndim( ) ) {
178147 self [ i] . $op_assign_fn( other[ i] ) ;
179148 }
@@ -204,11 +173,16 @@ where T: Clone
204173impl Shape for DynAxesRepr < usize >
205174{
206175 type Iter < ' a >
207- = Iter < ' a , usize >
176+ = Cloned < Iter < ' a , usize > >
208177 where Self : ' a ;
209178
179+ fn axis_len ( & self , axis : usize ) -> usize
180+ {
181+ self [ axis]
182+ }
183+
210184 fn iter ( & self ) -> Self :: Iter < ' _ >
211185 {
212- ( * * self ) . iter ( )
186+ ( * * self ) . iter ( ) . cloned ( )
213187 }
214188}
0 commit comments