@@ -144,27 +144,20 @@ where
144
144
fn to_pyarray < ' py > ( & self , py : Python < ' py > ) -> & ' py PyArray < Self :: Item , Self :: Dim > {
145
145
let len = self . len ( ) ;
146
146
match self . order ( ) {
147
- Some ( order ) if A :: IS_COPY => {
147
+ Some ( flag ) if A :: IS_COPY => {
148
148
// if the array is contiguous, copy it by `copy_nonoverlapping`.
149
149
let strides = self . npy_strides ( ) ;
150
150
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) ;
153
152
ptr:: copy_nonoverlapping ( self . as_ptr ( ) , array. data ( ) , len) ;
154
153
array
155
154
}
156
155
}
157
156
_ => {
158
157
// if the array is not contiguous, copy all elements by `ArrayBase::iter`.
159
158
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
- ) ;
166
159
unsafe {
167
- let array = PyArray :: < A , _ > :: new_ ( py, dim, strides . as_ptr ( ) , 0 ) ;
160
+ let array = PyArray :: < A , _ > :: new ( py, dim, false ) ;
168
161
let mut data_ptr = array. data ( ) ;
169
162
for item in self . iter ( ) {
170
163
data_ptr. write ( item. clone ( ) ) ;
@@ -177,69 +170,45 @@ where
177
170
}
178
171
}
179
172
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
-
194
173
pub ( 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 > ;
197
176
}
198
177
199
178
impl < A , S , D > ArrayExt for ArrayBase < S , D >
200
179
where
201
180
S : Data < Elem = A > ,
202
181
D : Dimension ,
203
182
{
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
206
199
}
207
200
208
- fn order ( & self ) -> Option < Order > {
201
+ fn order ( & self ) -> Option < c_int > {
209
202
if self . is_standard_layout ( ) {
210
- Some ( Order :: Standard )
203
+ Some ( npyffi :: NPY_ORDER :: NPY_CORDER as _ )
211
204
} 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 _ )
213
206
} else {
214
207
None
215
208
}
216
209
}
217
210
}
218
211
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
-
243
212
/// Utility trait to specify the dimensions of an array.
244
213
pub trait ToNpyDims : Dimension + Sealed {
245
214
#[ doc( hidden) ]
0 commit comments