@@ -144,27 +144,20 @@ where
144144 fn to_pyarray < ' py > ( & self , py : Python < ' py > ) -> & ' py PyArray < Self :: Item , Self :: Dim > {
145145 let len = self . len ( ) ;
146146 match self . order ( ) {
147- Some ( order ) if A :: IS_COPY => {
147+ Some ( flag ) if A :: IS_COPY => {
148148 // if the array is contiguous, copy it by `copy_nonoverlapping`.
149149 let strides = self . npy_strides ( ) ;
150150 unsafe {
151- let array =
152- PyArray :: new_ ( py, self . raw_dim ( ) , strides. as_ptr ( ) , order. to_flag ( ) ) ;
151+ let array = PyArray :: new_ ( py, self . raw_dim ( ) , strides. as_ptr ( ) , flag) ;
153152 ptr:: copy_nonoverlapping ( self . as_ptr ( ) , array. data ( ) , len) ;
154153 array
155154 }
156155 }
157156 _ => {
158157 // if the array is not contiguous, copy all elements by `ArrayBase::iter`.
159158 let dim = self . raw_dim ( ) ;
160- let strides = NpyStrides :: new :: < _ , A > (
161- dim. default_strides ( )
162- . slice ( )
163- . iter ( )
164- . map ( |& x| x as npyffi:: npy_intp ) ,
165- ) ;
166159 unsafe {
167- let array = PyArray :: < A , _ > :: new_ ( py, dim, strides . as_ptr ( ) , 0 ) ;
160+ let array = PyArray :: < A , _ > :: new ( py, dim, false ) ;
168161 let mut data_ptr = array. data ( ) ;
169162 for item in self . iter ( ) {
170163 data_ptr. write ( item. clone ( ) ) ;
@@ -177,69 +170,45 @@ where
177170 }
178171}
179172
180- pub ( crate ) enum Order {
181- Standard ,
182- Fortran ,
183- }
184-
185- impl Order {
186- fn to_flag ( & self ) -> c_int {
187- match self {
188- Order :: Standard => 0 ,
189- Order :: Fortran => 1 ,
190- }
191- }
192- }
193-
194173pub ( crate ) trait ArrayExt {
195- fn npy_strides ( & self ) -> NpyStrides ;
196- fn order ( & self ) -> Option < Order > ;
174+ fn npy_strides ( & self ) -> [ npyffi :: npy_intp ; 32 ] ;
175+ fn order ( & self ) -> Option < c_int > ;
197176}
198177
199178impl < A , S , D > ArrayExt for ArrayBase < S , D >
200179where
201180 S : Data < Elem = A > ,
202181 D : Dimension ,
203182{
204- fn npy_strides ( & self ) -> NpyStrides {
205- NpyStrides :: new :: < _ , A > ( self . strides ( ) . iter ( ) . map ( |& x| x as npyffi:: npy_intp ) )
183+ fn npy_strides ( & self ) -> [ npyffi:: npy_intp ; 32 ] {
184+ let strides = self . strides ( ) ;
185+ let itemsize = mem:: size_of :: < A > ( ) as isize ;
186+
187+ assert ! (
188+ strides. len( ) <= 32 ,
189+ "Only dimensionalities of up to 32 are supported"
190+ ) ;
191+
192+ let mut new_strides = [ 0 ; 32 ] ;
193+
194+ for i in 0 ..strides. len ( ) {
195+ new_strides[ i] = ( strides[ i] * itemsize) as npyffi:: npy_intp ;
196+ }
197+
198+ new_strides
206199 }
207200
208- fn order ( & self ) -> Option < Order > {
201+ fn order ( & self ) -> Option < c_int > {
209202 if self . is_standard_layout ( ) {
210- Some ( Order :: Standard )
203+ Some ( npyffi :: NPY_ORDER :: NPY_CORDER as _ )
211204 } else if self . ndim ( ) > 1 && self . raw_view ( ) . reversed_axes ( ) . is_standard_layout ( ) {
212- Some ( Order :: Fortran )
205+ Some ( npyffi :: NPY_ORDER :: NPY_FORTRANORDER as _ )
213206 } else {
214207 None
215208 }
216209 }
217210}
218211
219- /// An array of strides sufficiently large for [any NumPy array][NPY_MAXDIMS]
220- ///
221- /// [NPY_MAXDIMS]: https://github.com/numpy/numpy/blob/4c60b3263ac50e5e72f6a909e156314fc3c9cba0/numpy/core/include/numpy/ndarraytypes.h#L40
222- pub ( crate ) struct NpyStrides ( [ npyffi:: npy_intp ; 32 ] ) ;
223-
224- impl NpyStrides {
225- pub ( crate ) fn as_ptr ( & self ) -> * const npy_intp {
226- self . 0 . as_ptr ( )
227- }
228-
229- fn new < S , A > ( strides : S ) -> Self
230- where
231- S : Iterator < Item = npyffi:: npy_intp > ,
232- {
233- let type_size = mem:: size_of :: < A > ( ) as npyffi:: npy_intp ;
234- let mut res = [ 0 ; 32 ] ;
235- for ( i, s) in strides. enumerate ( ) {
236- * res. get_mut ( i)
237- . expect ( "Only dimensionalities of up to 32 are supported" ) = s * type_size;
238- }
239- Self ( res)
240- }
241- }
242-
243212/// Utility trait to specify the dimensions of an array.
244213pub trait ToNpyDims : Dimension + Sealed {
245214 #[ doc( hidden) ]
0 commit comments