Skip to content

Commit 698075e

Browse files
return early when the array has zero lenth dims
1 parent e543145 commit 698075e

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

src/impl_methods.rs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3221,9 +3221,11 @@ impl<A, D: Dimension> ArrayRef<A, D>
32213221
A: Clone + Ord + num_traits::Zero,
32223222
D: Dimension,
32233223
{
3224-
// Must check for zero-length dimensions
3224+
let mut result = self.to_owned();
3225+
3226+
// Return early if the array has zero-length dimensions
32253227
if self.shape().iter().any(|s| *s == 0) {
3226-
panic!("cannot partition an empty array");
3228+
return result;
32273229
}
32283230

32293231
// Bounds checking. goes to panic if kth is out of bounds
@@ -3232,8 +3234,6 @@ impl<A, D: Dimension> ArrayRef<A, D>
32323234
panic!("partition index {} is out of bounds for axis of length {}", kth, axis_len);
32333235
}
32343236

3235-
let mut result = self.to_owned();
3236-
32373237
// Check if the first lane is contiguous
32383238
let is_contiguous = result
32393239
.lanes_mut(axis)
@@ -3477,14 +3477,17 @@ mod tests
34773477
}
34783478

34793479
#[test]
3480-
#[should_panic = "cannot partition an empty array"]
34813480
fn test_partition_empty()
34823481
{
34833482
// Test 1D empty array
34843483
let empty1d = Array1::<i32>::zeros(0);
34853484
let result1d = empty1d.partition(0, Axis(0));
34863485
assert_eq!(result1d.len(), 0);
34873486

3487+
// Test 1D empty array with kth out of bounds
3488+
let result1d_out_of_bounds = empty1d.partition(1, Axis(0));
3489+
assert_eq!(result1d_out_of_bounds.len(), 0);
3490+
34883491
// Test 2D empty array
34893492
let empty2d = Array2::<i32>::zeros((0, 3));
34903493
let result2d = empty2d.partition(0, Axis(0));
@@ -3494,5 +3497,31 @@ mod tests
34943497
let empty2d_cols = Array2::<i32>::zeros((2, 0));
34953498
let result2d_cols = empty2d_cols.partition(0, Axis(1));
34963499
assert_eq!(result2d_cols.shape(), &[2, 0]);
3500+
3501+
// Test 3D empty array
3502+
let empty3d = Array3::<i32>::zeros((0, 2, 3));
3503+
let result3d = empty3d.partition(0, Axis(0));
3504+
assert_eq!(result3d.shape(), &[0, 2, 3]);
3505+
3506+
// Test 3D empty array with zero in middle dimension
3507+
let empty3d_mid = Array3::<i32>::zeros((2, 0, 3));
3508+
let result3d_mid = empty3d_mid.partition(0, Axis(1));
3509+
assert_eq!(result3d_mid.shape(), &[2, 0, 3]);
3510+
3511+
// Test 4D empty array
3512+
let empty4d = Array4::<i32>::zeros((0, 2, 3, 4));
3513+
let result4d = empty4d.partition(0, Axis(0));
3514+
assert_eq!(result4d.shape(), &[0, 2, 3, 4]);
3515+
3516+
// Test empty array with non-zero dimensions in other axes
3517+
let empty_mixed = Array2::<i32>::zeros((0, 5));
3518+
let result_mixed = empty_mixed.partition(0, Axis(0));
3519+
assert_eq!(result_mixed.shape(), &[0, 5]);
3520+
3521+
// Test empty array with negative strides
3522+
let mut arr = Array2::<i32>::zeros((3, 3));
3523+
let empty_slice = arr.slice(s![0..0, ..]);
3524+
let result_slice = empty_slice.partition(0, Axis(0));
3525+
assert_eq!(result_slice.shape(), &[0, 3]);
34973526
}
34983527
}

0 commit comments

Comments
 (0)