Skip to content

Commit 1961c5f

Browse files
committed
Factor out parts of view creation code that do not depend on S, T and F to reduce code bloat.
1 parent 3baef36 commit 1961c5f

File tree

1 file changed

+38
-28
lines changed

1 file changed

+38
-28
lines changed

src/array.rs

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -832,38 +832,48 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
832832
where
833833
F: FnOnce(StrideShape<D>, *mut T) -> ArrayBase<S, D>,
834834
{
835-
let shape = D::from_dimension(&Dim(self.shape())).expect("mismatching dimensions");
836-
837-
let strides = self.strides();
838-
let itemsize = mem::size_of::<T>();
839-
840-
assert!(
841-
strides.len() <= 32,
842-
"Only dimensionalities of up to 32 are supported"
843-
);
844-
845-
let mut new_strides = D::zeros(strides.len());
846-
let mut data_ptr = self.data();
847-
let mut inverted_axes = 0_u32;
848-
849-
for i in 0..strides.len() {
850-
// FIXME(kngwyu): Replace this hacky negative strides support with
851-
// a proper constructor, when it's implemented.
852-
// See https://github.com/rust-ndarray/ndarray/issues/842 for more.
853-
if strides[i] >= 0 {
854-
new_strides[i] = strides[i] as usize / itemsize;
855-
} else {
856-
// Move the pointer to the start position.
857-
let offset = strides[i] * (shape[i] as isize - 1) / itemsize as isize;
858-
data_ptr = unsafe { data_ptr.offset(offset) };
859-
860-
new_strides[i] = (-strides[i]) as usize / itemsize;
835+
fn inner<D: Dimension>(
836+
shape: &[usize],
837+
strides: &[isize],
838+
itemsize: usize,
839+
mut data_ptr: *mut u8,
840+
) -> (StrideShape<D>, u32, *mut u8) {
841+
let shape = D::from_dimension(&Dim(shape)).expect("mismatching dimensions");
842+
843+
assert!(
844+
strides.len() <= 32,
845+
"Only dimensionalities of up to 32 are supported"
846+
);
861847

862-
inverted_axes |= 1 << i;
848+
let mut new_strides = D::zeros(strides.len());
849+
let mut inverted_axes = 0_u32;
850+
851+
for i in 0..strides.len() {
852+
// FIXME(kngwyu): Replace this hacky negative strides support with
853+
// a proper constructor, when it's implemented.
854+
// See https://github.com/rust-ndarray/ndarray/issues/842 for more.
855+
if strides[i] >= 0 {
856+
new_strides[i] = strides[i] as usize / itemsize;
857+
} else {
858+
// Move the pointer to the start position.
859+
data_ptr = unsafe { data_ptr.offset(strides[i] * (shape[i] as isize - 1)) };
860+
861+
new_strides[i] = (-strides[i]) as usize / itemsize;
862+
inverted_axes |= 1 << i;
863+
}
863864
}
865+
866+
(shape.strides(new_strides), inverted_axes, data_ptr)
864867
}
865868

866-
let mut array = from_shape_ptr(shape.strides(new_strides), data_ptr);
869+
let (shape, mut inverted_axes, data_ptr) = inner(
870+
self.shape(),
871+
self.strides(),
872+
mem::size_of::<T>(),
873+
self.data() as _,
874+
);
875+
876+
let mut array = from_shape_ptr(shape, data_ptr as _);
867877

868878
while inverted_axes != 0 {
869879
let axis = inverted_axes.trailing_zeros() as usize;

0 commit comments

Comments
 (0)