Skip to content

Add .nth() method to Slice type #386

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,48 @@ impl Slice {
pub fn step_by(self, step: Ixs) -> Self {
Slice { step, ..self }
}

/// Returns the `n`th index in this slice.
///
/// The `n` argument starts from zero, so `nth(0)` is the first index. If
/// `n` is greater than or equal to the length of the slice, then the
/// return value is `None`.
///
/// # Examples
///
/// ```
/// use ndarray::Slice;
///
/// // indices 0, 1, 2, 3, 4
/// assert_eq!(Slice::new(0, Some(5), 1).nth(2), Some(2));
///
/// // indices 1, 3
/// assert_eq!(Slice::new(1, Some(5), 2).nth(1), Some(3));
///
/// // indices 1, -1, -3
/// assert_eq!(Slice::new(1, Some(-4), -2).nth(2), Some(-3));
///
/// // indices 1, 3, 5, ...
/// assert_eq!(Slice::new(1, None, 2).nth(2), Some(5));
///
/// // indices 3, 3, 3, ...
/// assert_eq!(Slice::new(3, Some(5), 0).nth(1000), Some(3));
Copy link
Member

Choose a reason for hiding this comment

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

What meaning do we give to a step of 0? It used to be an error when using it for slicing, I'll have to check again.

Copy link
Member Author

Choose a reason for hiding this comment

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

If you actually call slice() with a step of 0, then you'll get a panic. (See dimension::do_slice.) You'll also get a panic when calling slice() if the slice begin/end is out-of-range.

In contrast, nth() doesn't panic in these cases. It's possible to change it to panic if step == 0, without changing the function signature. To make it panic for out-of-range indices, it would need an argument for the length of the axis you're going to slice.

If you'd prefer it panic in those cases, I'd probably choose a different name (e.g. index()) instead of nth().

Copy link
Member

Choose a reason for hiding this comment

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

Great, thanks for clarifying!

That sounds you have no particular behaviour in mind for 0, so we can choose one freely. I'm not too fond of mixing panics in explicitly checked or fallible methods either, so I'm not sure. I do lean towards that it should be a panic -- maybe a debug assertion. Maybe indeed a debug assertion already in Slice::new ? Or is that redundant?

Copy link
Member

Choose a reason for hiding this comment

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

Ah with public fields a debug assertion in new doesn't do much good anyway.

Copy link
Member Author

@jturner314 jturner314 Nov 24, 2017

Choose a reason for hiding this comment

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

It's certainly possible to make the fields non-public (or maybe just step) and add an assertion to new() and step_by(). In some ways, I think that's a better design anyway; what do you think? It's still not possible to handle out-of-range indices this way, but it would catch step == 0 early.

If we make some of the fields in Slice non-public, I would also change enum SliceOrIndex { Slice { start, end, step }, ... } to enum SliceOrIndex { Slice(Slice), ... } to hide the fields there too.

Edit: If it matters, Python's slice type does allow step == 0.

///
/// // indices 1, 3
/// assert_eq!(Slice::new(1, Some(5), 2).nth(2), None);
/// ```
pub fn nth(&self, n: usize) -> Option<isize> {
let nth = self.start + self.step * n as isize;
if let Some(end) = self.end {
if self.step == 0 || (self.step > 0 && nth < end) || (self.step < 0 && nth > end) {
Some(nth)
} else {
None
}
} else {
Some(nth)
}
}
}

impl From<Range<Ixs>> for Slice {
Expand Down