Skip to content

Commit

Permalink
NdIndex<IxDyn>: tidy for Vec<Ix> and impl for &T
Browse files Browse the repository at this point in the history
  • Loading branch information
bderrett committed Jan 23, 2022
1 parent fee71af commit da8ed68
Showing 1 changed file with 8 additions and 18 deletions.
26 changes: 8 additions & 18 deletions src/dimension/ndindex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,6 @@ impl<'a> IntoDimension for &'a [Ix] {
}
}

unsafe impl<'a> NdIndex<IxDyn> for &'a IxDyn {
fn index_checked(&self, dim: &IxDyn, strides: &IxDyn) -> Option<isize> {
(**self).index_checked(dim, strides)
}
fn index_unchecked(&self, strides: &IxDyn) -> isize {
(**self).index_unchecked(strides)
}
}

unsafe impl<'a> NdIndex<IxDyn> for &'a [Ix] {
fn index_checked(&self, dim: &IxDyn, strides: &IxDyn) -> Option<isize> {
stride_offset_checked(dim.ix(), strides.ix(), *self)
Expand All @@ -239,24 +230,23 @@ unsafe impl<'a> NdIndex<IxDyn> for &'a [Ix] {
}
}

unsafe impl<'a> NdIndex<IxDyn> for &'a Vec<Ix> {
unsafe impl<'a, T> NdIndex<IxDyn> for &T
where
T: NdIndex<IxDyn>,
{
fn index_checked(&self, dim: &IxDyn, strides: &IxDyn) -> Option<isize> {
stride_offset_checked(dim.ix(), strides.ix(), *self)
(*self).index_checked(dim, strides)
}
fn index_unchecked(&self, strides: &IxDyn) -> isize {
zip(strides.ix(), *self)
.map(|(&s, &i)| stride_offset(i, s))
.sum()
(*self).index_unchecked(strides)
}
}

unsafe impl<'a> NdIndex<IxDyn> for Vec<Ix> {
fn index_checked(&self, dim: &IxDyn, strides: &IxDyn) -> Option<isize> {
stride_offset_checked(dim.ix(), strides.ix(), self)
(&**self).index_checked(dim, strides)
}
fn index_unchecked(&self, strides: &IxDyn) -> isize {
zip(strides.ix(), self)
.map(|(&s, i)| stride_offset(*i, s))
.sum()
(&**self).index_unchecked(strides)
}
}

0 comments on commit da8ed68

Please sign in to comment.