Stabilize slice_select_nth_unstable
This stabilizes the functionality in slice_partition_at_index, but under the names `select_nth_unstable*`. The functions `partition_at_index*` are left as deprecated, to be removed in a later release. Closes #55300
This commit is contained in:
parent
576e2277ac
commit
01ac5a97c9
2 changed files with 69 additions and 31 deletions
|
@ -2034,6 +2034,50 @@ impl<T> [T] {
|
||||||
sort::quicksort(self, |a, b| f(a).lt(&f(b)));
|
sort::quicksort(self, |a, b| f(a).lt(&f(b)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Reorder the slice such that the element at `index` is at its final sorted position.
|
||||||
|
#[unstable(feature = "slice_partition_at_index", issue = "55300")]
|
||||||
|
#[rustc_deprecated(since = "1.49.0", reason = "use the select_nth_unstable() instead")]
|
||||||
|
#[inline]
|
||||||
|
pub fn partition_at_index(&mut self, index: usize) -> (&mut [T], &mut T, &mut [T])
|
||||||
|
where
|
||||||
|
T: Ord,
|
||||||
|
{
|
||||||
|
self.select_nth_unstable(index)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Reorder the slice with a comparator function such that the element at `index` is at its
|
||||||
|
/// final sorted position.
|
||||||
|
#[unstable(feature = "slice_partition_at_index", issue = "55300")]
|
||||||
|
#[rustc_deprecated(since = "1.49.0", reason = "use select_nth_unstable_by() instead")]
|
||||||
|
#[inline]
|
||||||
|
pub fn partition_at_index_by<F>(
|
||||||
|
&mut self,
|
||||||
|
index: usize,
|
||||||
|
compare: F,
|
||||||
|
) -> (&mut [T], &mut T, &mut [T])
|
||||||
|
where
|
||||||
|
F: FnMut(&T, &T) -> Ordering,
|
||||||
|
{
|
||||||
|
self.select_nth_unstable_by(index, compare)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Reorder the slice with a key extraction function such that the element at `index` is at its
|
||||||
|
/// final sorted position.
|
||||||
|
#[unstable(feature = "slice_partition_at_index", issue = "55300")]
|
||||||
|
#[rustc_deprecated(since = "1.49.0", reason = "use the select_nth_unstable_by_key() instead")]
|
||||||
|
#[inline]
|
||||||
|
pub fn partition_at_index_by_key<K, F>(
|
||||||
|
&mut self,
|
||||||
|
index: usize,
|
||||||
|
f: F,
|
||||||
|
) -> (&mut [T], &mut T, &mut [T])
|
||||||
|
where
|
||||||
|
F: FnMut(&T) -> K,
|
||||||
|
K: Ord,
|
||||||
|
{
|
||||||
|
self.select_nth_unstable_by_key(index, f)
|
||||||
|
}
|
||||||
|
|
||||||
/// Reorder the slice such that the element at `index` is at its final sorted position.
|
/// Reorder the slice such that the element at `index` is at its final sorted position.
|
||||||
///
|
///
|
||||||
/// This reordering has the additional property that any value at position `i < index` will be
|
/// This reordering has the additional property that any value at position `i < index` will be
|
||||||
|
@ -2058,12 +2102,10 @@ impl<T> [T] {
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// #![feature(slice_partition_at_index)]
|
|
||||||
///
|
|
||||||
/// let mut v = [-5i32, 4, 1, -3, 2];
|
/// let mut v = [-5i32, 4, 1, -3, 2];
|
||||||
///
|
///
|
||||||
/// // Find the median
|
/// // Find the median
|
||||||
/// v.partition_at_index(2);
|
/// v.select_nth_unstable(2);
|
||||||
///
|
///
|
||||||
/// // We are only guaranteed the slice will be one of the following, based on the way we sort
|
/// // We are only guaranteed the slice will be one of the following, based on the way we sort
|
||||||
/// // about the specified index.
|
/// // about the specified index.
|
||||||
|
@ -2072,9 +2114,9 @@ impl<T> [T] {
|
||||||
/// v == [-3, -5, 1, 4, 2] ||
|
/// v == [-3, -5, 1, 4, 2] ||
|
||||||
/// v == [-5, -3, 1, 4, 2]);
|
/// v == [-5, -3, 1, 4, 2]);
|
||||||
/// ```
|
/// ```
|
||||||
#[unstable(feature = "slice_partition_at_index", issue = "55300")]
|
#[stable(feature = "slice_select_nth_unstable", since = "1.49.0")]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn partition_at_index(&mut self, index: usize) -> (&mut [T], &mut T, &mut [T])
|
pub fn select_nth_unstable(&mut self, index: usize) -> (&mut [T], &mut T, &mut [T])
|
||||||
where
|
where
|
||||||
T: Ord,
|
T: Ord,
|
||||||
{
|
{
|
||||||
|
@ -2108,12 +2150,10 @@ impl<T> [T] {
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// #![feature(slice_partition_at_index)]
|
|
||||||
///
|
|
||||||
/// let mut v = [-5i32, 4, 1, -3, 2];
|
/// let mut v = [-5i32, 4, 1, -3, 2];
|
||||||
///
|
///
|
||||||
/// // Find the median as if the slice were sorted in descending order.
|
/// // Find the median as if the slice were sorted in descending order.
|
||||||
/// v.partition_at_index_by(2, |a, b| b.cmp(a));
|
/// v.select_nth_unstable_by(2, |a, b| b.cmp(a));
|
||||||
///
|
///
|
||||||
/// // We are only guaranteed the slice will be one of the following, based on the way we sort
|
/// // We are only guaranteed the slice will be one of the following, based on the way we sort
|
||||||
/// // about the specified index.
|
/// // about the specified index.
|
||||||
|
@ -2122,9 +2162,9 @@ impl<T> [T] {
|
||||||
/// v == [4, 2, 1, -5, -3] ||
|
/// v == [4, 2, 1, -5, -3] ||
|
||||||
/// v == [4, 2, 1, -3, -5]);
|
/// v == [4, 2, 1, -3, -5]);
|
||||||
/// ```
|
/// ```
|
||||||
#[unstable(feature = "slice_partition_at_index", issue = "55300")]
|
#[stable(feature = "slice_select_nth_unstable", since = "1.49.0")]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn partition_at_index_by<F>(
|
pub fn select_nth_unstable_by<F>(
|
||||||
&mut self,
|
&mut self,
|
||||||
index: usize,
|
index: usize,
|
||||||
mut compare: F,
|
mut compare: F,
|
||||||
|
@ -2162,12 +2202,10 @@ impl<T> [T] {
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// #![feature(slice_partition_at_index)]
|
|
||||||
///
|
|
||||||
/// let mut v = [-5i32, 4, 1, -3, 2];
|
/// let mut v = [-5i32, 4, 1, -3, 2];
|
||||||
///
|
///
|
||||||
/// // Return the median as if the array were sorted according to absolute value.
|
/// // Return the median as if the array were sorted according to absolute value.
|
||||||
/// v.partition_at_index_by_key(2, |a| a.abs());
|
/// v.select_nth_unstable_by_key(2, |a| a.abs());
|
||||||
///
|
///
|
||||||
/// // We are only guaranteed the slice will be one of the following, based on the way we sort
|
/// // We are only guaranteed the slice will be one of the following, based on the way we sort
|
||||||
/// // about the specified index.
|
/// // about the specified index.
|
||||||
|
@ -2176,9 +2214,9 @@ impl<T> [T] {
|
||||||
/// v == [2, 1, -3, 4, -5] ||
|
/// v == [2, 1, -3, 4, -5] ||
|
||||||
/// v == [2, 1, -3, -5, 4]);
|
/// v == [2, 1, -3, -5, 4]);
|
||||||
/// ```
|
/// ```
|
||||||
#[unstable(feature = "slice_partition_at_index", issue = "55300")]
|
#[stable(feature = "slice_select_nth_unstable", since = "1.49.0")]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn partition_at_index_by_key<K, F>(
|
pub fn select_nth_unstable_by_key<K, F>(
|
||||||
&mut self,
|
&mut self,
|
||||||
index: usize,
|
index: usize,
|
||||||
mut f: F,
|
mut f: F,
|
||||||
|
|
|
@ -1571,7 +1571,7 @@ fn sort_unstable() {
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
#[cfg_attr(miri, ignore)] // Miri is too slow
|
#[cfg_attr(miri, ignore)] // Miri is too slow
|
||||||
fn partition_at_index() {
|
fn select_nth_unstable() {
|
||||||
use core::cmp::Ordering::{Equal, Greater, Less};
|
use core::cmp::Ordering::{Equal, Greater, Less};
|
||||||
use rand::rngs::StdRng;
|
use rand::rngs::StdRng;
|
||||||
use rand::seq::SliceRandom;
|
use rand::seq::SliceRandom;
|
||||||
|
@ -1597,7 +1597,7 @@ fn partition_at_index() {
|
||||||
// Sort in default order.
|
// Sort in default order.
|
||||||
for pivot in 0..len {
|
for pivot in 0..len {
|
||||||
let mut v = orig.clone();
|
let mut v = orig.clone();
|
||||||
v.partition_at_index(pivot);
|
v.select_nth_unstable(pivot);
|
||||||
|
|
||||||
assert_eq!(v_sorted[pivot], v[pivot]);
|
assert_eq!(v_sorted[pivot], v[pivot]);
|
||||||
for i in 0..pivot {
|
for i in 0..pivot {
|
||||||
|
@ -1610,7 +1610,7 @@ fn partition_at_index() {
|
||||||
// Sort in ascending order.
|
// Sort in ascending order.
|
||||||
for pivot in 0..len {
|
for pivot in 0..len {
|
||||||
let mut v = orig.clone();
|
let mut v = orig.clone();
|
||||||
let (left, pivot, right) = v.partition_at_index_by(pivot, |a, b| a.cmp(b));
|
let (left, pivot, right) = v.select_nth_unstable_by(pivot, |a, b| a.cmp(b));
|
||||||
|
|
||||||
assert_eq!(left.len() + right.len(), len - 1);
|
assert_eq!(left.len() + right.len(), len - 1);
|
||||||
|
|
||||||
|
@ -1633,7 +1633,7 @@ fn partition_at_index() {
|
||||||
|
|
||||||
for pivot in 0..len {
|
for pivot in 0..len {
|
||||||
let mut v = orig.clone();
|
let mut v = orig.clone();
|
||||||
v.partition_at_index_by(pivot, sort_descending_comparator);
|
v.select_nth_unstable_by(pivot, sort_descending_comparator);
|
||||||
|
|
||||||
assert_eq!(v_sorted_descending[pivot], v[pivot]);
|
assert_eq!(v_sorted_descending[pivot], v[pivot]);
|
||||||
for i in 0..pivot {
|
for i in 0..pivot {
|
||||||
|
@ -1654,7 +1654,7 @@ fn partition_at_index() {
|
||||||
}
|
}
|
||||||
|
|
||||||
for pivot in 0..v.len() {
|
for pivot in 0..v.len() {
|
||||||
v.partition_at_index_by(pivot, |_, _| *[Less, Equal, Greater].choose(&mut rng).unwrap());
|
v.select_nth_unstable_by(pivot, |_, _| *[Less, Equal, Greater].choose(&mut rng).unwrap());
|
||||||
v.sort();
|
v.sort();
|
||||||
for i in 0..v.len() {
|
for i in 0..v.len() {
|
||||||
assert_eq!(v[i], i as i32);
|
assert_eq!(v[i], i as i32);
|
||||||
|
@ -1662,28 +1662,28 @@ fn partition_at_index() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Should not panic.
|
// Should not panic.
|
||||||
[(); 10].partition_at_index(0);
|
[(); 10].select_nth_unstable(0);
|
||||||
[(); 10].partition_at_index(5);
|
[(); 10].select_nth_unstable(5);
|
||||||
[(); 10].partition_at_index(9);
|
[(); 10].select_nth_unstable(9);
|
||||||
[(); 100].partition_at_index(0);
|
[(); 100].select_nth_unstable(0);
|
||||||
[(); 100].partition_at_index(50);
|
[(); 100].select_nth_unstable(50);
|
||||||
[(); 100].partition_at_index(99);
|
[(); 100].select_nth_unstable(99);
|
||||||
|
|
||||||
let mut v = [0xDEADBEEFu64];
|
let mut v = [0xDEADBEEFu64];
|
||||||
v.partition_at_index(0);
|
v.select_nth_unstable(0);
|
||||||
assert!(v == [0xDEADBEEF]);
|
assert!(v == [0xDEADBEEF]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic(expected = "index 0 greater than length of slice")]
|
#[should_panic(expected = "index 0 greater than length of slice")]
|
||||||
fn partition_at_index_zero_length() {
|
fn select_nth_unstable_zero_length() {
|
||||||
[0i32; 0].partition_at_index(0);
|
[0i32; 0].select_nth_unstable(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic(expected = "index 20 greater than length of slice")]
|
#[should_panic(expected = "index 20 greater than length of slice")]
|
||||||
fn partition_at_index_past_length() {
|
fn select_nth_unstable_past_length() {
|
||||||
[0i32; 10].partition_at_index(20);
|
[0i32; 10].select_nth_unstable(20);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod memchr {
|
pub mod memchr {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue