Skip to content

Commit f4287e5

Browse files
mgsloangitbot
authored and
gitbot
committed
Improve select_nth_unstable documentation clarity
* Instead uses `before` and `after` variable names in the example where `greater` and `lesser` are flipped. * Uses `<=` and `>=` instead of "less than or equal to" and "greater than or equal to" to make the docs more concise. * General attempt to remove unnecessary words and be more precise. For example it seems slightly wrong to say "its final sorted position", since this implies there is only one sorted position for this element.
1 parent b591a53 commit f4287e5

File tree

1 file changed

+40
-42
lines changed

1 file changed

+40
-42
lines changed

core/src/slice/mod.rs

+40-42
Original file line numberDiff line numberDiff line change
@@ -3071,19 +3071,18 @@ impl<T> [T] {
30713071
sort::unstable::sort(self, &mut |a, b| f(a).lt(&f(b)));
30723072
}
30733073

3074-
/// Reorders the slice such that the element at `index` after the reordering is at its final
3075-
/// sorted position.
3074+
/// Reorders the slice such that the element at `index` is at a sort-order position. All
3075+
/// elements before `index` will then be `<=` this value, and all elements after will be `>=`.
30763076
///
3077-
/// This reordering has the additional property that any value at position `i < index` will be
3078-
/// less than or equal to any value at a position `j > index`. Additionally, this reordering is
3079-
/// unstable (i.e. any number of equal elements may end up at position `index`), in-place (i.e.
3080-
/// does not allocate), and runs in *O*(*n*) time. This function is also known as "kth element"
3081-
/// in other libraries.
3077+
/// This reordering is unstable (i.e. any element that compares equal to the nth element may end
3078+
/// up at that position), in-place (i.e. does not allocate), and runs in *O*(*n*) time. This
3079+
/// function is also known as "kth element" in other libraries.
3080+
///
3081+
/// Returns a triple partitioning the reordered slice:
30823082
///
3083-
/// It returns a triplet of the following from the reordered slice: the subslice prior to
3084-
/// `index`, the element at `index`, and the subslice after `index`; accordingly, the values in
3085-
/// those two subslices will respectively all be less-than-or-equal-to and
3086-
/// greater-than-or-equal-to the value of the element at `index`.
3083+
/// * The unsorted subslice before `index` (elements all pass `x <= self[index]`)
3084+
/// * The element at `index`
3085+
/// * The unsorted subslice after `index` (elements all pass `x >= self[index]`)
30873086
///
30883087
/// # Current implementation
30893088
///
@@ -3096,7 +3095,7 @@ impl<T> [T] {
30963095
///
30973096
/// # Panics
30983097
///
3099-
/// Panics when `index >= len()`, meaning it always panics on empty slices.
3098+
/// Panics when `index >= len()`, and so always panics on empty slices.
31003099
///
31013100
/// May panic if the implementation of [`Ord`] for `T` does not implement a [total order].
31023101
///
@@ -3105,8 +3104,7 @@ impl<T> [T] {
31053104
/// ```
31063105
/// let mut v = [-5i32, 4, 2, -3, 1];
31073106
///
3108-
/// // Find the items less than or equal to the median, the median, and greater than or equal to
3109-
/// // the median.
3107+
/// // Find the items `<=` the median, the median, and `>=` the median.
31103108
/// let (lesser, median, greater) = v.select_nth_unstable(2);
31113109
///
31123110
/// assert!(lesser == [-3, -5] || lesser == [-5, -3]);
@@ -3132,19 +3130,19 @@ impl<T> [T] {
31323130
sort::select::partition_at_index(self, index, T::lt)
31333131
}
31343132

3135-
/// Reorders the slice with a comparator function such that the element at `index` after the
3136-
/// reordering is at its final sorted position.
3133+
/// Reorders the slice with a comparator function such that the element at `index` is at a
3134+
/// sort-order position. All elements before `index` will then be `<=` this value, and all
3135+
/// elements after will be `>=` according to the comparator function.
31373136
///
3138-
/// This reordering has the additional property that any value at position `i < index` will be
3139-
/// less than or equal to any value at a position `j > index` using the comparator function.
3140-
/// Additionally, this reordering is unstable (i.e. any number of equal elements may end up at
3141-
/// position `index`), in-place (i.e. does not allocate), and runs in *O*(*n*) time. This
3137+
/// This reordering is unstable (i.e. any element that compares equal to the nth element may end
3138+
/// up at that position), in-place (i.e. does not allocate), and runs in *O*(*n*) time. This
31423139
/// function is also known as "kth element" in other libraries.
31433140
///
3144-
/// It returns a triplet of the following from the slice reordered according to the provided
3145-
/// comparator function: the subslice prior to `index`, the element at `index`, and the subslice
3146-
/// after `index`; accordingly, the values in those two subslices will respectively all be
3147-
/// less-than-or-equal-to and greater-than-or-equal-to the value of the element at `index`.
3141+
/// Returns a triple partitioning the reordered slice:
3142+
///
3143+
/// * The unsorted subslice before `index` (elements all pass `compare(x, self[index]).is_le()`)
3144+
/// * The element at `index`
3145+
/// * The unsorted subslice after `index` (elements all pass `compare(x, self[index]).is_ge()`)
31483146
///
31493147
/// # Current implementation
31503148
///
@@ -3157,7 +3155,7 @@ impl<T> [T] {
31573155
///
31583156
/// # Panics
31593157
///
3160-
/// Panics when `index >= len()`, meaning it always panics on empty slices.
3158+
/// Panics when `index >= len()`, and so always panics on empty slices.
31613159
///
31623160
/// May panic if `compare` does not implement a [total order].
31633161
///
@@ -3166,13 +3164,13 @@ impl<T> [T] {
31663164
/// ```
31673165
/// let mut v = [-5i32, 4, 2, -3, 1];
31683166
///
3169-
/// // Find the items less than or equal to the median, the median, and greater than or equal to
3170-
/// // the median as if the slice were sorted in descending order.
3171-
/// let (lesser, median, greater) = v.select_nth_unstable_by(2, |a, b| b.cmp(a));
3167+
/// // Find the items `>=` the median, the median, and `<=` the median, by using a reversed
3168+
/// // comparator.
3169+
/// let (before, median, after) = v.select_nth_unstable_by(2, |a, b| b.cmp(a));
31723170
///
3173-
/// assert!(lesser == [4, 2] || lesser == [2, 4]);
3171+
/// assert!(before == [4, 2] || before == [2, 4]);
31743172
/// assert_eq!(median, &mut 1);
3175-
/// assert!(greater == [-3, -5] || greater == [-5, -3]);
3173+
/// assert!(after == [-3, -5] || after == [-5, -3]);
31763174
///
31773175
/// // We are only guaranteed the slice will be one of the following, based on the way we sort
31783176
/// // about the specified index.
@@ -3197,19 +3195,19 @@ impl<T> [T] {
31973195
sort::select::partition_at_index(self, index, |a: &T, b: &T| compare(a, b) == Less)
31983196
}
31993197

3200-
/// Reorders the slice with a key extraction function such that the element at `index` after the
3201-
/// reordering is at its final sorted position.
3198+
/// Reorders the slice with a key extraction function such that the element at `index` is at a
3199+
/// sort-order position. All elements before `index` will have keys `<=` the key at `index`, and
3200+
/// all elements after will have keys `>=`.
32023201
///
3203-
/// This reordering has the additional property that any value at position `i < index` will be
3204-
/// less than or equal to any value at a position `j > index` using the key extraction function.
3205-
/// Additionally, this reordering is unstable (i.e. any number of equal elements may end up at
3206-
/// position `index`), in-place (i.e. does not allocate), and runs in *O*(*n*) time. This
3202+
/// This reordering is unstable (i.e. any element that compares equal to the nth element may end
3203+
/// up at that position), in-place (i.e. does not allocate), and runs in *O*(*n*) time. This
32073204
/// function is also known as "kth element" in other libraries.
32083205
///
3209-
/// It returns a triplet of the following from the slice reordered according to the provided key
3210-
/// extraction function: the subslice prior to `index`, the element at `index`, and the subslice
3211-
/// after `index`; accordingly, the values in those two subslices will respectively all be
3212-
/// less-than-or-equal-to and greater-than-or-equal-to the value of the element at `index`.
3206+
/// Returns a triple partitioning the reordered slice:
3207+
///
3208+
/// * The unsorted subslice before `index` (elements all pass `f(x) <= f(self[index])`)
3209+
/// * The element at `index`
3210+
/// * The unsorted subslice after `index` (elements all pass `f(x) >= f(self[index])`)
32133211
///
32143212
/// # Current implementation
32153213
///
@@ -3231,8 +3229,8 @@ impl<T> [T] {
32313229
/// ```
32323230
/// let mut v = [-5i32, 4, 1, -3, 2];
32333231
///
3234-
/// // Find the items less than or equal to the median, the median, and greater than or equal to
3235-
/// // the median as if the slice were sorted according to absolute value.
3232+
/// // Find the items <= the median absolute value, the median absolute value, and >= the median
3233+
/// // absolute value.
32363234
/// let (lesser, median, greater) = v.select_nth_unstable_by_key(2, |a| a.abs());
32373235
///
32383236
/// assert!(lesser == [1, 2] || lesser == [2, 1]);

0 commit comments

Comments
 (0)