Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support zero-length axis in .map_axis/_mut() #612

Merged
merged 6 commits into from
Apr 9, 2019
Merged

Support zero-length axis in .map_axis/_mut() #612

merged 6 commits into from
Apr 9, 2019

Conversation

andrei-papou
Copy link
Contributor

PR for #579

} else {
let smaller_zero_dim = <D::Smaller as Dimension>::zeros(self.dim.ndim() - 1);
let ptr = std_ptr::NonNull::<A>::dangling().as_ptr();
unsafe {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like a comment here to explain why this is a safe thing to do (same in the other spot).

Copy link
Contributor Author

@andrei-papou andrei-papou Mar 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@LukeMathWalker is it enough to point to this section in the ArrayBase docstring?

The only exceptions are if the array is empty or the element
type is zero-sized. In these cases, ptr may be dangling, but it must
still be safe to [.offset()] the pointer along the axes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest to extract it in a function, so that we can reuse code across the two calling sites and keep a better eye on it. It's probably better to copy the relevant section over, to have it close to the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ready

Copy link
Member

@jturner314 jturner314 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this @andrei-papou! Unfortunately, the implementation isn't quite right. (See my comment on the code.) Here's one possible solution:

    pub fn map_axis<'a, B, F>(&'a self, axis: Axis, mut mapping: F)
        -> Array<B, D::Smaller>
        where D: RemoveAxis,
              F: FnMut(ArrayView1<'a, A>) -> B,
              A: 'a,
              S: Data,
    {
        let view_len = self.len_of(axis);
        let view_stride = self.strides.axis(axis);
        if view_len == 0 {
            let new_dim = self.dim.remove_axis(axis);
            Array::from_shape_fn(new_dim, move |_| mapping(ArrayView::from(&[])))
        } else {
            // use the 0th subview as a map to each 1d array view extended from
            // the 0th element.
            self.index_axis(axis, 0).map(|first_elt| {
                unsafe {
                    mapping(ArrayView::new_(first_elt, Ix1(view_len), Ix1(view_stride)))
                }
            })
        }
    }

I'd like to see some tests for the zero-length-axis case as part of the PR too.

src/impl_methods.rs Outdated Show resolved Hide resolved
tests/array.rs Outdated
counter
});
assert_eq!(result.shape(), &[3, 4]);
assert_eq!(result, arr2(&[
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, this test is good, but we don't guarantee the iteration order of map_axes, so the elements of result are not guaranteed to be arranged in this way. Something like

itertools::assert_equal(result.iter().cloned().sorted(), 1..=3*4);

would work to verify that the elements are correct while ignoring their ordering.

(For access to .sorted(), make sure to use itertools::Itertools and add features = ["use_std"] to the itertools dependency in Cargo.toml.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I've updated the test accordingly.

@andrei-papou
Copy link
Contributor Author

@jturner314 any more comments on the changes? the issues above are fixed

@jturner314 jturner314 merged commit 2924f2e into rust-ndarray:master Apr 9, 2019
@jturner314
Copy link
Member

Looks good to me. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants