Skip to content

Commit 7e4629a

Browse files
committed
Adds some documentation to partition
1 parent f783c16 commit 7e4629a

File tree

1 file changed

+38
-42
lines changed

1 file changed

+38
-42
lines changed

src/impl_methods.rs

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -576,11 +576,7 @@ where
576576
pub fn slice_move<I>(mut self, info: I) -> ArrayBase<S, I::OutDim>
577577
where I: SliceArg<D>
578578
{
579-
assert_eq!(
580-
info.in_ndim(),
581-
self.ndim(),
582-
"The input dimension of `info` must match the array to be sliced.",
583-
);
579+
assert_eq!(info.in_ndim(), self.ndim(), "The input dimension of `info` must match the array to be sliced.",);
584580
let out_ndim = info.out_ndim();
585581
let mut new_dim = I::OutDim::zeros(out_ndim);
586582
let mut new_strides = I::OutDim::zeros(out_ndim);
@@ -648,11 +644,7 @@ impl<A, D: Dimension> LayoutRef<A, D>
648644
pub fn slice_collapse<I>(&mut self, info: I)
649645
where I: SliceArg<D>
650646
{
651-
assert_eq!(
652-
info.in_ndim(),
653-
self.ndim(),
654-
"The input dimension of `info` must match the array to be sliced.",
655-
);
647+
assert_eq!(info.in_ndim(), self.ndim(), "The input dimension of `info` must match the array to be sliced.",);
656648
let mut axis = 0;
657649
info.as_ref().iter().for_each(|&ax_info| match ax_info {
658650
SliceInfoElem::Slice { start, end, step } => {
@@ -1120,8 +1112,7 @@ impl<A, D: Dimension> ArrayRef<A, D>
11201112
// bounds check the indices first
11211113
if let Some(max_index) = indices.iter().cloned().max() {
11221114
if max_index >= axis_len {
1123-
panic!("ndarray: index {} is out of bounds in array of len {}",
1124-
max_index, self.len_of(axis));
1115+
panic!("ndarray: index {} is out of bounds in array of len {}", max_index, self.len_of(axis));
11251116
}
11261117
} // else: indices empty is ok
11271118
let view = self.view().into_dimensionality::<Ix1>().unwrap();
@@ -1530,10 +1521,7 @@ impl<A, D: Dimension> ArrayRef<A, D>
15301521

15311522
ndassert!(
15321523
axis_index < self.ndim(),
1533-
concat!(
1534-
"Window axis {} does not match array dimension {} ",
1535-
"(with array of shape {:?})"
1536-
),
1524+
concat!("Window axis {} does not match array dimension {} ", "(with array of shape {:?})"),
15371525
axis_index,
15381526
self.ndim(),
15391527
self.shape()
@@ -3119,8 +3107,7 @@ where
31193107
/// ***Panics*** if not `index < self.len_of(axis)`.
31203108
pub fn remove_index(&mut self, axis: Axis, index: usize)
31213109
{
3122-
assert!(index < self.len_of(axis), "index {} must be less than length of Axis({})",
3123-
index, axis.index());
3110+
assert!(index < self.len_of(axis), "index {} must be less than length of Axis({})", index, axis.index());
31243111
let (_, mut tail) = self.view_mut().split_at(axis, index);
31253112
// shift elements to the front
31263113
Zip::from(tail.lanes_mut(axis)).for_each(|mut lane| lane.rotate1_front());
@@ -3193,15 +3180,16 @@ impl<A, D: Dimension> ArrayRef<A, D>
31933180
/// - All elements equal or greater than the k-th element to its right
31943181
/// - The ordering within each partition is undefined
31953182
///
3183+
/// Empty arrays (i.e., those with any zero-length axes) are considered partitioned already,
3184+
/// and will be returned unchanged.
3185+
///
3186+
/// **Panics** if `k` is out of bounds for a non-zero axis length.
3187+
///
31963188
/// # Parameters
31973189
///
31983190
/// * `kth` - Index to partition by. The k-th element will be in its sorted position.
31993191
/// * `axis` - Axis along which to partition.
32003192
///
3201-
/// # Returns
3202-
///
3203-
/// A new array of the same shape and type as the input array, with elements partitioned.
3204-
///
32053193
/// # Examples
32063194
///
32073195
/// ```
@@ -3228,10 +3216,10 @@ impl<A, D: Dimension> ArrayRef<A, D>
32283216
return result;
32293217
}
32303218

3231-
// Bounds checking. goes to panic if kth is out of bounds
3219+
// Bounds checking. Panics if kth is out of bounds
32323220
let axis_len = self.len_of(axis);
32333221
if kth >= axis_len {
3234-
panic!("partition index {} is out of bounds for axis of length {}", kth, axis_len);
3222+
panic!("Partition index {} is out of bounds for axis {} of length {}", kth, axis.0, axis_len);
32353223
}
32363224

32373225
// Check if the first lane is contiguous
@@ -3428,11 +3416,7 @@ mod tests
34283416
fn test_partition_contiguous_or_not()
34293417
{
34303418
// Test contiguous case (C-order)
3431-
let a = array![
3432-
[7, 1, 5],
3433-
[2, 6, 0],
3434-
[3, 4, 8]
3435-
];
3419+
let a = array![[7, 1, 5], [2, 6, 0], [3, 4, 8]];
34363420

34373421
// Partition along axis 0 (contiguous)
34383422
let p_axis0 = a.partition(1, Axis(0));
@@ -3442,20 +3426,24 @@ mod tests
34423426
// - Last row should be >= middle row (kth element)
34433427
for col in 0..3 {
34443428
let kth = p_axis0[[1, col]];
3445-
assert!(p_axis0[[0, col]] <= kth,
3429+
assert!(
3430+
p_axis0[[0, col]] <= kth,
34463431
"Column {}: First row {} should be <= middle row {}",
3447-
col, p_axis0[[0, col]], kth);
3448-
assert!(p_axis0[[2, col]] >= kth,
3432+
col,
3433+
p_axis0[[0, col]],
3434+
kth
3435+
);
3436+
assert!(
3437+
p_axis0[[2, col]] >= kth,
34493438
"Column {}: Last row {} should be >= middle row {}",
3450-
col, p_axis0[[2, col]], kth);
3439+
col,
3440+
p_axis0[[2, col]],
3441+
kth
3442+
);
34513443
}
34523444

34533445
// Test non-contiguous case (F-order)
3454-
let a = array![
3455-
[7, 1, 5],
3456-
[2, 6, 0],
3457-
[3, 4, 8]
3458-
];
3446+
let a = array![[7, 1, 5], [2, 6, 0], [3, 4, 8]];
34593447

34603448
// Make array non-contiguous by transposing
34613449
let a = a.t().to_owned();
@@ -3467,12 +3455,20 @@ mod tests
34673455
// - First column should be <= middle column
34683456
// - Last column should be >= middle column
34693457
for row in 0..3 {
3470-
assert!(p_axis1[[row, 0]] <= p_axis1[[row, 1]],
3458+
assert!(
3459+
p_axis1[[row, 0]] <= p_axis1[[row, 1]],
34713460
"Row {}: First column {} should be <= middle column {}",
3472-
row, p_axis1[[row, 0]], p_axis1[[row, 1]]);
3473-
assert!(p_axis1[[row, 2]] >= p_axis1[[row, 1]],
3461+
row,
3462+
p_axis1[[row, 0]],
3463+
p_axis1[[row, 1]]
3464+
);
3465+
assert!(
3466+
p_axis1[[row, 2]] >= p_axis1[[row, 1]],
34743467
"Row {}: Last column {} should be >= middle column {}",
3475-
row, p_axis1[[row, 2]], p_axis1[[row, 1]]);
3468+
row,
3469+
p_axis1[[row, 2]],
3470+
p_axis1[[row, 1]]
3471+
);
34763472
}
34773473
}
34783474

0 commit comments

Comments
 (0)