Skip to content

Commit 678e669

Browse files
committedJan 19, 2025
Auto merge of #134976 - mgsloan:improve-select-nth-unstable-docs, r=ibraheemdev
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.
2 parents c62b732 + 3a6eea0 commit 678e669

File tree

1 file changed

+49
-42
lines changed

1 file changed

+49
-42
lines changed
 

‎library/core/src/slice/mod.rs

+49-42
Original file line numberDiff line numberDiff line change
@@ -3071,19 +3071,21 @@ 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 be `<=` to this value, and all elements after will be `>=` to
3076+
/// it.
30763077
///
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.
3078+
/// This reordering is unstable (i.e. any element that compares equal to the nth element may end
3079+
/// up at that position), in-place (i.e. does not allocate), and runs in *O*(*n*) time. This
3080+
/// function is also known as "kth element" in other libraries.
3081+
///
3082+
/// Returns a triple that partitions the reordered slice:
3083+
///
3084+
/// * The unsorted subslice before `index`, whose elements all satisfy `x <= self[index]`.
30823085
///
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`.
3086+
/// * The element at `index`.
3087+
///
3088+
/// * The unsorted subslice after `index`, whose elements all satisfy `x >= self[index]`.
30873089
///
30883090
/// # Current implementation
30893091
///
@@ -3096,7 +3098,7 @@ impl<T> [T] {
30963098
///
30973099
/// # Panics
30983100
///
3099-
/// Panics when `index >= len()`, meaning it always panics on empty slices.
3101+
/// Panics when `index >= len()`, and so always panics on empty slices.
31003102
///
31013103
/// May panic if the implementation of [`Ord`] for `T` does not implement a [total order].
31023104
///
@@ -3105,8 +3107,7 @@ impl<T> [T] {
31053107
/// ```
31063108
/// let mut v = [-5i32, 4, 2, -3, 1];
31073109
///
3108-
/// // Find the items less than or equal to the median, the median, and greater than or equal to
3109-
/// // the median.
3110+
/// // Find the items `<=` to the median, the median itself, and the items `>=` to it.
31103111
/// let (lesser, median, greater) = v.select_nth_unstable(2);
31113112
///
31123113
/// assert!(lesser == [-3, -5] || lesser == [-5, -3]);
@@ -3132,19 +3133,23 @@ impl<T> [T] {
31323133
sort::select::partition_at_index(self, index, T::lt)
31333134
}
31343135

3135-
/// Reorders the slice with a comparator function such that the element at `index` after the
3136-
/// reordering is at its final sorted position.
3136+
/// Reorders the slice with a comparator function such that the element at `index` is at a
3137+
/// sort-order position. All elements before `index` will be `<=` to this value, and all
3138+
/// elements after will be `>=` to it, according to the comparator function.
31373139
///
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
3140+
/// This reordering is unstable (i.e. any element that compares equal to the nth element may end
3141+
/// up at that position), in-place (i.e. does not allocate), and runs in *O*(*n*) time. This
31423142
/// function is also known as "kth element" in other libraries.
31433143
///
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`.
3144+
/// Returns a triple partitioning the reordered slice:
3145+
///
3146+
/// * The unsorted subslice before `index`, whose elements all satisfy
3147+
/// `compare(x, self[index]).is_le()`.
3148+
///
3149+
/// * The element at `index`.
3150+
///
3151+
/// * The unsorted subslice after `index`, whose elements all satisfy
3152+
/// `compare(x, self[index]).is_ge()`.
31483153
///
31493154
/// # Current implementation
31503155
///
@@ -3157,7 +3162,7 @@ impl<T> [T] {
31573162
///
31583163
/// # Panics
31593164
///
3160-
/// Panics when `index >= len()`, meaning it always panics on empty slices.
3165+
/// Panics when `index >= len()`, and so always panics on empty slices.
31613166
///
31623167
/// May panic if `compare` does not implement a [total order].
31633168
///
@@ -3166,13 +3171,13 @@ impl<T> [T] {
31663171
/// ```
31673172
/// let mut v = [-5i32, 4, 2, -3, 1];
31683173
///
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));
3174+
/// // Find the items `>=` to the median, the median itself, and the items `<=` to it, by using
3175+
/// // a reversed comparator.
3176+
/// let (before, median, after) = v.select_nth_unstable_by(2, |a, b| b.cmp(a));
31723177
///
3173-
/// assert!(lesser == [4, 2] || lesser == [2, 4]);
3178+
/// assert!(before == [4, 2] || before == [2, 4]);
31743179
/// assert_eq!(median, &mut 1);
3175-
/// assert!(greater == [-3, -5] || greater == [-5, -3]);
3180+
/// assert!(after == [-3, -5] || after == [-5, -3]);
31763181
///
31773182
/// // We are only guaranteed the slice will be one of the following, based on the way we sort
31783183
/// // about the specified index.
@@ -3197,19 +3202,21 @@ impl<T> [T] {
31973202
sort::select::partition_at_index(self, index, |a: &T, b: &T| compare(a, b) == Less)
31983203
}
31993204

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.
3205+
/// Reorders the slice with a key extraction function such that the element at `index` is at a
3206+
/// sort-order position. All elements before `index` will have keys `<=` to the key at `index`,
3207+
/// and all elements after will have keys `>=` to it.
32023208
///
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
3209+
/// This reordering is unstable (i.e. any element that compares equal to the nth element may end
3210+
/// up at that position), in-place (i.e. does not allocate), and runs in *O*(*n*) time. This
32073211
/// function is also known as "kth element" in other libraries.
32083212
///
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`.
3213+
/// Returns a triple partitioning the reordered slice:
3214+
///
3215+
/// * The unsorted subslice before `index`, whose elements all satisfy `f(x) <= f(self[index])`.
3216+
///
3217+
/// * The element at `index`.
3218+
///
3219+
/// * The unsorted subslice after `index`, whose elements all satisfy `f(x) >= f(self[index])`.
32133220
///
32143221
/// # Current implementation
32153222
///
@@ -3231,8 +3238,8 @@ impl<T> [T] {
32313238
/// ```
32323239
/// let mut v = [-5i32, 4, 1, -3, 2];
32333240
///
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.
3241+
/// // Find the items `<=` to the absolute median, the absolute median itself, and the items
3242+
/// // `>=` to it.
32363243
/// let (lesser, median, greater) = v.select_nth_unstable_by_key(2, |a| a.abs());
32373244
///
32383245
/// assert!(lesser == [1, 2] || lesser == [2, 1]);

0 commit comments

Comments
 (0)
Please sign in to comment.