Skip to content

Commit

Permalink
Remove AxisSliceInfo::step_by method
Browse files Browse the repository at this point in the history
This method is awkward because it panics for all cases except the
`Slice` variant. This method is no longer necessary because the `s![]`
macro now calls `Slice::step_by()` instead.
  • Loading branch information
jturner314 committed Mar 16, 2021
1 parent 71b53ab commit c05db1c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 37 deletions.
24 changes: 0 additions & 24 deletions src/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,30 +136,6 @@ impl AxisSliceInfo {
pub fn is_new_axis(&self) -> bool {
matches!(self, AxisSliceInfo::NewAxis)
}

/// Returns a new `AxisSliceInfo` with the given step size (multiplied with
/// the previous step size).
///
/// `step` must be nonzero.
/// (This method checks with a debug assertion that `step` is not zero.)
///
/// **Panics** if `self` is not the `AxisSliceInfo::Slice` variant.
#[inline]
pub fn step_by(self, step: isize) -> Self {
debug_assert_ne!(step, 0, "AxisSliceInfo::step_by: step must be nonzero");
match self {
AxisSliceInfo::Slice {
start,
end,
step: orig_step,
} => AxisSliceInfo::Slice {
start,
end,
step: orig_step * step,
},
_ => panic!("AxisSliceInfo::step_by: `self` must be the `Slice` variant"),
}
}
}

impl fmt::Display for AxisSliceInfo {
Expand Down
16 changes: 8 additions & 8 deletions tests/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ fn test_slice_array_dyn() {
AxisSliceInfo::from(1..),
AxisSliceInfo::from(1),
AxisSliceInfo::from(NewAxis),
AxisSliceInfo::from(..).step_by(2),
AxisSliceInfo::from(Slice::from(..).step_by(2)),
])
.unwrap();
arr.slice(info);
Expand All @@ -232,7 +232,7 @@ fn test_slice_array_dyn() {
let info2 = SliceInfo::<_, Ix3, IxDyn>::try_from([
AxisSliceInfo::from(1..),
AxisSliceInfo::from(1),
AxisSliceInfo::from(..).step_by(2),
AxisSliceInfo::from(Slice::from(..).step_by(2)),
])
.unwrap();
arr.view().slice_collapse(info2);
Expand All @@ -245,7 +245,7 @@ fn test_slice_dyninput_array_dyn() {
AxisSliceInfo::from(1..),
AxisSliceInfo::from(1),
AxisSliceInfo::from(NewAxis),
AxisSliceInfo::from(..).step_by(2),
AxisSliceInfo::from(Slice::from(..).step_by(2)),
])
.unwrap();
arr.slice(info);
Expand All @@ -254,7 +254,7 @@ fn test_slice_dyninput_array_dyn() {
let info2 = SliceInfo::<_, Ix3, IxDyn>::try_from([
AxisSliceInfo::from(1..),
AxisSliceInfo::from(1),
AxisSliceInfo::from(..).step_by(2),
AxisSliceInfo::from(Slice::from(..).step_by(2)),
])
.unwrap();
arr.view().slice_collapse(info2);
Expand All @@ -267,7 +267,7 @@ fn test_slice_dyninput_vec_fixed() {
AxisSliceInfo::from(1..),
AxisSliceInfo::from(1),
AxisSliceInfo::from(NewAxis),
AxisSliceInfo::from(..).step_by(2),
AxisSliceInfo::from(Slice::from(..).step_by(2)),
])
.unwrap();
arr.slice(info);
Expand All @@ -276,7 +276,7 @@ fn test_slice_dyninput_vec_fixed() {
let info2 = SliceInfo::<_, Ix3, Ix2>::try_from(vec![
AxisSliceInfo::from(1..),
AxisSliceInfo::from(1),
AxisSliceInfo::from(..).step_by(2),
AxisSliceInfo::from(Slice::from(..).step_by(2)),
])
.unwrap();
arr.view().slice_collapse(info2);
Expand All @@ -289,7 +289,7 @@ fn test_slice_dyninput_vec_dyn() {
AxisSliceInfo::from(1..),
AxisSliceInfo::from(1),
AxisSliceInfo::from(NewAxis),
AxisSliceInfo::from(..).step_by(2),
AxisSliceInfo::from(Slice::from(..).step_by(2)),
])
.unwrap();
arr.slice(info);
Expand All @@ -298,7 +298,7 @@ fn test_slice_dyninput_vec_dyn() {
let info2 = SliceInfo::<_, Ix3, IxDyn>::try_from(vec![
AxisSliceInfo::from(1..),
AxisSliceInfo::from(1),
AxisSliceInfo::from(..).step_by(2),
AxisSliceInfo::from(Slice::from(..).step_by(2)),
])
.unwrap();
arr.view().slice_collapse(info2);
Expand Down
10 changes: 5 additions & 5 deletions tests/oper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ fn scaled_add_2() {
#[test]
fn scaled_add_3() {
use approx::assert_relative_eq;
use ndarray::{SliceInfo, AxisSliceInfo};
use ndarray::{AxisSliceInfo, Slice, SliceInfo};
use std::convert::TryFrom;

let beta = -2.3;
Expand All @@ -583,12 +583,12 @@ fn scaled_add_3() {
let mut a = range_mat64(m, k);
let mut answer = a.clone();
let cdim = if n == 1 { vec![q] } else { vec![n, q] };
let cslice = if n == 1 {
vec![AxisSliceInfo::from(..).step_by(s2)]
let cslice: Vec<AxisSliceInfo> = if n == 1 {
vec![Slice::from(..).step_by(s2).into()]
} else {
vec![
AxisSliceInfo::from(..).step_by(s1),
AxisSliceInfo::from(..).step_by(s2),
Slice::from(..).step_by(s1).into(),
Slice::from(..).step_by(s2).into(),
]
};

Expand Down

0 comments on commit c05db1c

Please sign in to comment.