ndarray
ndarray copied to clipboard
Allow `slice_axis` slicing with the `s!` macro
trafficstars
Hello! I would like to thank you for developing this wonderful repository.
I think it would be useful to be able to index the slice_axis,slice_axis_mut,slice_axis_inplace methods with s! macro. What do you think?
I think there is a way to implement it as follows. If this method is good, I would like to make a PR.
slice.rs
impl<Din, Dout> From<SliceInfo<[SliceInfoElem; 1], Din, Dout>> for Slice
where
Din: Dimension,
Dout: Dimension,
{
fn from(index: SliceInfo<[SliceInfoElem; 1], Din, Dout>) -> Slice {
match index.deref()[0] {
SliceInfoElem::Slice { start, end, step } => Slice::new(start, end, step),
SliceInfoElem::Index(index) => Slice::new(index, Some(index + 1), 1),
SliceInfoElem::NewAxis => {
panic!("can not convert to Slice")
}
}
}
}
impl_method.rs
pub fn slice_axis<I>(&self, axis: Axis, indices: I) -> ArrayView<'_, A, D>
where
S: Data,
I: Into<Slice>,
{
let mut view = self.view();
view.slice_axis_inplace(axis, indices);
view
}
pub fn slice_axis_mut<I>(&mut self, axis: Axis, indices: I) -> ArrayViewMut<'_, A, D>
where
S: DataMut,
I: Into<Slice>,
{
let mut view_mut = self.view_mut();
view_mut.slice_axis_inplace(axis, indices);
view_mut
}
pub fn slice_axis_inplace<I>(&mut self, axis: Axis, indices: I)
where
I: Into<Slice>,
{
let offset = do_slice(
&mut self.dim.slice_mut()[axis.index()],
&mut self.strides.slice_mut()[axis.index()],
indices.into(),
);
unsafe {
self.ptr = self.ptr.offset(offset);
}
debug_assert!(self.pointer_is_inbounds());
}