Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
FIX: in remove_axis(axis, index) specify we need index < len_of(axis)
Removing the index-past-the end does not make sense (it's an ok split
index, so the previous code passed through without action).
  • Loading branch information
bluss committed Apr 9, 2021
commit 107f51f2cee9de50b086bef521896df624d0d2a8
10 changes: 9 additions & 1 deletion src/impl_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2446,13 +2446,21 @@ where

/// Remove the `index`th elements along `axis` and shift down elements from higher indexes.
///
/// Note that this "removes" the elements by swapping them around to the end of the axis and
/// shortening the length of the axis; the elements are not deinitialized or dropped by this,
/// just moved out of view (this only matters for elements with ownership semantics). It's
/// similar to slicing an owned array in place.
///
/// Decreases the length of `axis` by one.
///
/// ***Panics** if `axis` or `index` is out of bounds.
/// ***Panics*** if `axis` is out of bounds<br>
/// ***Panics*** if not `index < self.len_of(axis)`.
pub fn remove_index(&mut self, axis: Axis, index: usize)
where
S: DataOwned + DataMut,
{
assert!(index < self.len_of(axis), "index {} must be less than length of Axis({})",
index, axis.index());
let (_, mut tail) = self.view_mut().split_at(axis, index);
Comment thread
bluss marked this conversation as resolved.
// shift elements to the front
// use swapping to keep all elements initialized (as required by owned storage)
Expand Down
34 changes: 34 additions & 0 deletions tests/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2439,3 +2439,37 @@ fn test_remove_index() {
[],
[]]);
}

#[should_panic(expected="must be less")]
#[test]
fn test_remove_index_oob1() {
let mut a = arr2(&[[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10,11,12]]);
a.remove_index(Axis(0), 4);
}

#[should_panic(expected="must be less")]
#[test]
fn test_remove_index_oob2() {
let mut a = array![[10], [4], [1]];
a.remove_index(Axis(1), 0);
assert_eq!(a.shape(), &[3, 0]);
assert_eq!(a,
array![[],
[],
[]]);
a.remove_index(Axis(0), 1); // ok
assert_eq!(a,
array![[],
[]]);
a.remove_index(Axis(1), 0); // oob
}

#[should_panic(expected="index out of bounds")]
#[test]
fn test_remove_index_oob3() {
let mut a = array![[10], [4], [1]];
a.remove_index(Axis(2), 0);
}