Skip to content

Commit dc9e804

Browse files
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 aea4e43 commit dc9e804

File tree

1 file changed

+41
-42
lines changed

1 file changed

+41
-42
lines changed

library/core/src/slice/mod.rs

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

3072-
/// Reorders the slice such that the element at `index` after the reordering is at its final
3073-
/// sorted position.
3072+
/// Reorders the slice such that the element at `index` is at a sort-order position. All
3073+
/// elements before `index` will then be `<=` this value, and all elements after will be `>=`.
30743074
///
3075-
/// This reordering has the additional property that any value at position `i < index` will be
3076-
/// less than or equal to any value at a position `j > index`. Additionally, this reordering is
3077-
/// unstable (i.e. any number of equal elements may end up at position `index`), in-place (i.e.
3078-
/// does not allocate), and runs in *O*(*n*) time. This function is also known as "kth element"
3079-
/// in other libraries.
3075+
/// This reordering is unstable (i.e. any element that compares equal to the nth element may end
3076+
/// up at that position), in-place (i.e. does not allocate), and runs in *O*(*n*) time. This
3077+
/// function is also known as "kth element" in other libraries.
3078+
///
3079+
/// Returns a triple partitioning the reordered slice:
30803080
///
3081-
/// It returns a triplet of the following from the reordered slice: the subslice prior to
3082-
/// `index`, the element at `index`, and the subslice after `index`; accordingly, the values in
3083-
/// those two subslices will respectively all be less-than-or-equal-to and
3084-
/// greater-than-or-equal-to the value of the element at `index`.
3081+
/// * The unsorted subslice before `index` (elements all pass `x <= self[index]`)
3082+
/// * The element at `index`
3083+
/// * The unsorted subslice after `index` (elements all pass `x >= self[index]`)
30853084
///
30863085
/// # Current implementation
30873086
///
@@ -3094,7 +3093,7 @@ impl<T> [T] {
30943093
///
30953094
/// # Panics
30963095
///
3097-
/// Panics when `index >= len()`, meaning it always panics on empty slices.
3096+
/// Panics when `index >= len()`, and so always panics on empty slices.
30983097
///
30993098
/// May panic if the implementation of [`Ord`] for `T` does not implement a [total order].
31003099
///
@@ -3103,8 +3102,7 @@ impl<T> [T] {
31033102
/// ```
31043103
/// let mut v = [-5i32, 4, 2, -3, 1];
31053104
///
3106-
/// // Find the items less than or equal to the median, the median, and greater than or equal to
3107-
/// // the median.
3105+
/// // Find the items `<=` the median, the median, and `>=` the median.
31083106
/// let (lesser, median, greater) = v.select_nth_unstable(2);
31093107
///
31103108
/// assert!(lesser == [-3, -5] || lesser == [-5, -3]);
@@ -3130,19 +3128,19 @@ impl<T> [T] {
31303128
sort::select::partition_at_index(self, index, T::lt)
31313129
}
31323130

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

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

0 commit comments

Comments
 (0)