File tree Expand file tree Collapse file tree 2 files changed +43
-1
lines changed Expand file tree Collapse file tree 2 files changed +43
-1
lines changed Original file line number Diff line number Diff line change @@ -2446,13 +2446,21 @@ where
2446
2446
2447
2447
/// Remove the `index`th elements along `axis` and shift down elements from higher indexes.
2448
2448
///
2449
+ /// Note that this "removes" the elements by swapping them around to the end of the axis and
2450
+ /// shortening the length of the axis; the elements are not deinitialized or dropped by this,
2451
+ /// just moved out of view (this only matters for elements with ownership semantics). It's
2452
+ /// similar to slicing an owned array in place.
2453
+ ///
2449
2454
/// Decreases the length of `axis` by one.
2450
2455
///
2451
- /// ***Panics** if `axis` or `index` is out of bounds.
2456
+ /// ***Panics*** if `axis` is out of bounds<br>
2457
+ /// ***Panics*** if not `index < self.len_of(axis)`.
2452
2458
pub fn remove_index ( & mut self , axis : Axis , index : usize )
2453
2459
where
2454
2460
S : DataOwned + DataMut ,
2455
2461
{
2462
+ assert ! ( index < self . len_of( axis) , "index {} must be less than length of Axis({})" ,
2463
+ index, axis. index( ) ) ;
2456
2464
let ( _, mut tail) = self . view_mut ( ) . split_at ( axis, index) ;
2457
2465
// shift elements to the front
2458
2466
// use swapping to keep all elements initialized (as required by owned storage)
Original file line number Diff line number Diff line change @@ -2439,3 +2439,37 @@ fn test_remove_index() {
2439
2439
[ ] ,
2440
2440
[ ] ] ) ;
2441
2441
}
2442
+
2443
+ #[ should_panic( expected="must be less" ) ]
2444
+ #[ test]
2445
+ fn test_remove_index_oob1 ( ) {
2446
+ let mut a = arr2 ( & [ [ 1 , 2 , 3 ] ,
2447
+ [ 4 , 5 , 6 ] ,
2448
+ [ 7 , 8 , 9 ] ,
2449
+ [ 10 , 11 , 12 ] ] ) ;
2450
+ a. remove_index ( Axis ( 0 ) , 4 ) ;
2451
+ }
2452
+
2453
+ #[ should_panic( expected="must be less" ) ]
2454
+ #[ test]
2455
+ fn test_remove_index_oob2 ( ) {
2456
+ let mut a = array ! [ [ 10 ] , [ 4 ] , [ 1 ] ] ;
2457
+ a. remove_index ( Axis ( 1 ) , 0 ) ;
2458
+ assert_eq ! ( a. shape( ) , & [ 3 , 0 ] ) ;
2459
+ assert_eq ! ( a,
2460
+ array![ [ ] ,
2461
+ [ ] ,
2462
+ [ ] ] ) ;
2463
+ a. remove_index ( Axis ( 0 ) , 1 ) ; // ok
2464
+ assert_eq ! ( a,
2465
+ array![ [ ] ,
2466
+ [ ] ] ) ;
2467
+ a. remove_index ( Axis ( 1 ) , 0 ) ; // oob
2468
+ }
2469
+
2470
+ #[ should_panic( expected="index out of bounds" ) ]
2471
+ #[ test]
2472
+ fn test_remove_index_oob3 ( ) {
2473
+ let mut a = array ! [ [ 10 ] , [ 4 ] , [ 1 ] ] ;
2474
+ a. remove_index ( Axis ( 2 ) , 0 ) ;
2475
+ }
You can’t perform that action at this time.
0 commit comments