@@ -358,12 +358,25 @@ impl<T, D> PyArray<T, D> {
358
358
}
359
359
}
360
360
361
- struct InvertedAxises ( Vec < Axis > ) ;
361
+ struct InvertedAxes ( u32 ) ;
362
362
363
- impl InvertedAxises {
364
- fn invert < S : RawData , D : Dimension > ( self , array : & mut ArrayBase < S , D > ) {
365
- for axis in self . 0 {
366
- array. invert_axis ( axis) ;
363
+ impl InvertedAxes {
364
+ fn new ( len : usize ) -> Self {
365
+ assert ! ( len <= 32 , "Only dimensionalities of up to 32 are supported" ) ;
366
+ Self ( 0 )
367
+ }
368
+
369
+ fn push ( & mut self , axis : usize ) {
370
+ debug_assert ! ( axis < 32 ) ;
371
+ self . 0 |= 1 << axis;
372
+ }
373
+
374
+ fn invert < S : RawData , D : Dimension > ( mut self , array : & mut ArrayBase < S , D > ) {
375
+ while self . 0 != 0 {
376
+ let axis = self . 0 . trailing_zeros ( ) as usize ;
377
+ self . 0 &= !( 1 << axis) ;
378
+
379
+ array. invert_axis ( Axis ( axis) ) ;
367
380
}
368
381
}
369
382
}
@@ -372,36 +385,39 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
372
385
/// Same as [shape](#method.shape), but returns `D`
373
386
#[ inline( always) ]
374
387
pub fn dims ( & self ) -> D {
375
- D :: from_dimension ( & Dim ( self . shape ( ) ) ) . expect ( "PyArray::dims different dimension " )
388
+ D :: from_dimension ( & Dim ( self . shape ( ) ) ) . expect ( "mismatching dimensions " )
376
389
}
377
390
378
- fn ndarray_shape_ptr ( & self ) -> ( StrideShape < D > , * mut T , InvertedAxises ) {
379
- let shape_slice = self . shape ( ) ;
380
- let shape: Shape < _ > = Dim ( self . dims ( ) ) . into ( ) ;
381
- let sizeof_t = mem:: size_of :: < T > ( ) ;
391
+ fn ndarray_shape_ptr ( & self ) -> ( StrideShape < D > , * mut T , InvertedAxes ) {
392
+ let shape = self . shape ( ) ;
382
393
let strides = self . strides ( ) ;
394
+
383
395
let mut new_strides = D :: zeros ( strides. len ( ) ) ;
384
396
let mut data_ptr = unsafe { self . data ( ) } ;
385
- let mut inverted_axises = vec ! [ ] ;
397
+ let mut inverted_axes = InvertedAxes :: new ( strides. len ( ) ) ;
398
+
386
399
for i in 0 ..strides. len ( ) {
387
400
// TODO(kngwyu): Replace this hacky negative strides support with
388
401
// a proper constructor, when it's implemented.
389
402
// See https://github.com/rust-ndarray/ndarray/issues/842 for more.
390
403
if strides[ i] < 0 {
391
404
// Move the pointer to the start position
392
- let offset = strides[ i] * ( shape_slice [ i] as isize - 1 ) / sizeof_t as isize ;
405
+ let offset = strides[ i] * ( shape [ i] as isize - 1 ) / mem :: size_of :: < T > ( ) as isize ;
393
406
unsafe {
394
407
data_ptr = data_ptr. offset ( offset) ;
395
408
}
396
- new_strides[ i] = ( -strides[ i] ) as usize / sizeof_t;
397
- inverted_axises. push ( Axis ( i) ) ;
409
+ new_strides[ i] = ( -strides[ i] ) as usize / mem:: size_of :: < T > ( ) ;
410
+
411
+ inverted_axes. push ( i) ;
398
412
} else {
399
- new_strides[ i] = strides[ i] as usize / sizeof_t ;
413
+ new_strides[ i] = strides[ i] as usize / mem :: size_of :: < T > ( ) ;
400
414
}
401
415
}
402
- let st = D :: from_dimension ( & Dim ( new_strides) )
403
- . expect ( "PyArray::ndarray_shape: dimension mismatching" ) ;
404
- ( shape. strides ( st) , data_ptr, InvertedAxises ( inverted_axises) )
416
+
417
+ let shape = Shape :: from ( D :: from_dimension ( & Dim ( shape) ) . expect ( "mismatching dimensions" ) ) ;
418
+ let new_strides = D :: from_dimension ( & Dim ( new_strides) ) . expect ( "mismatching dimensions" ) ;
419
+
420
+ ( shape. strides ( new_strides) , data_ptr, inverted_axes)
405
421
}
406
422
407
423
/// Creates a new uninitialized PyArray in python heap.
@@ -818,9 +834,9 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
818
834
/// If the internal array is not readonly and can be mutated from Python code,
819
835
/// holding the `ArrayView` might cause undefined behavior.
820
836
pub unsafe fn as_array ( & self ) -> ArrayView < ' _ , T , D > {
821
- let ( shape, ptr, inverted_axises ) = self . ndarray_shape_ptr ( ) ;
837
+ let ( shape, ptr, inverted_axes ) = self . ndarray_shape_ptr ( ) ;
822
838
let mut res = ArrayView :: from_shape_ptr ( shape, ptr) ;
823
- inverted_axises . invert ( & mut res) ;
839
+ inverted_axes . invert ( & mut res) ;
824
840
res
825
841
}
826
842
@@ -830,25 +846,25 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
830
846
/// If another reference to the internal data exists(e.g., `&[T]` or `ArrayView`),
831
847
/// it might cause undefined behavior.
832
848
pub unsafe fn as_array_mut ( & self ) -> ArrayViewMut < ' _ , T , D > {
833
- let ( shape, ptr, inverted_axises ) = self . ndarray_shape_ptr ( ) ;
849
+ let ( shape, ptr, inverted_axes ) = self . ndarray_shape_ptr ( ) ;
834
850
let mut res = ArrayViewMut :: from_shape_ptr ( shape, ptr) ;
835
- inverted_axises . invert ( & mut res) ;
851
+ inverted_axes . invert ( & mut res) ;
836
852
res
837
853
}
838
854
839
855
/// Returns the internal array as [`RawArrayView`] enabling element access via raw pointers
840
856
pub fn as_raw_array ( & self ) -> RawArrayView < T , D > {
841
- let ( shape, ptr, inverted_axises ) = self . ndarray_shape_ptr ( ) ;
857
+ let ( shape, ptr, inverted_axes ) = self . ndarray_shape_ptr ( ) ;
842
858
let mut res = unsafe { RawArrayView :: from_shape_ptr ( shape, ptr) } ;
843
- inverted_axises . invert ( & mut res) ;
859
+ inverted_axes . invert ( & mut res) ;
844
860
res
845
861
}
846
862
847
863
/// Returns the internal array as [`RawArrayViewMut`] enabling element access via raw pointers
848
864
pub fn as_raw_array_mut ( & self ) -> RawArrayViewMut < T , D > {
849
- let ( shape, ptr, inverted_axises ) = self . ndarray_shape_ptr ( ) ;
865
+ let ( shape, ptr, inverted_axes ) = self . ndarray_shape_ptr ( ) ;
850
866
let mut res = unsafe { RawArrayViewMut :: from_shape_ptr ( shape, ptr) } ;
851
- inverted_axises . invert ( & mut res) ;
867
+ inverted_axes . invert ( & mut res) ;
852
868
res
853
869
}
854
870
0 commit comments