diff --git a/library/core/src/slice/mod.rs b/library/core/src/slice/mod.rs index ff926c517bc7c..84ad126b07b7b 100644 --- a/library/core/src/slice/mod.rs +++ b/library/core/src/slice/mod.rs @@ -2055,12 +2055,12 @@ impl<T> [T] { /// # Examples /// /// ``` - /// #![feature(slice_partition_at_index)] + /// #![feature(slice_select_nth_unstable)] /// /// let mut v = [-5i32, 4, 1, -3, 2]; /// /// // 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 /// // about the specified index. @@ -2069,14 +2069,14 @@ impl<T> [T] { /// v == [-3, -5, 1, 4, 2] || /// v == [-5, -3, 1, 4, 2]); /// ``` - #[unstable(feature = "slice_partition_at_index", issue = "55300")] + #[unstable(feature = "slice_select_nth_unstable", issue = "55300")] #[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 T: Ord, { let mut f = |a: &T, b: &T| a.lt(b); - sort::partition_at_index(self, index, &mut f) + sort::select_nth_unstable(self, index, &mut f) } /// Reorder the slice with a comparator function such that the element at `index` is at its @@ -2105,12 +2105,12 @@ impl<T> [T] { /// # Examples /// /// ``` - /// #![feature(slice_partition_at_index)] + /// #![feature(slice_select_nth_unstable)] /// /// let mut v = [-5i32, 4, 1, -3, 2]; /// /// // 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 /// // about the specified index. @@ -2119,9 +2119,9 @@ impl<T> [T] { /// v == [4, 2, 1, -5, -3] || /// v == [4, 2, 1, -3, -5]); /// ``` - #[unstable(feature = "slice_partition_at_index", issue = "55300")] + #[unstable(feature = "slice_select_nth_unstable", issue = "55300")] #[inline] - pub fn partition_at_index_by<F>( + pub fn select_nth_unstable_by<F>( &mut self, index: usize, mut compare: F, @@ -2130,7 +2130,7 @@ impl<T> [T] { F: FnMut(&T, &T) -> Ordering, { let mut f = |a: &T, b: &T| compare(a, b) == Less; - sort::partition_at_index(self, index, &mut f) + sort::select_nth_unstable(self, index, &mut f) } /// Reorder the slice with a key extraction function such that the element at `index` is at its @@ -2159,12 +2159,12 @@ impl<T> [T] { /// # Examples /// /// ``` - /// #![feature(slice_partition_at_index)] + /// #![feature(slice_select_nth_unstable)] /// /// let mut v = [-5i32, 4, 1, -3, 2]; /// /// // 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 /// // about the specified index. @@ -2173,9 +2173,9 @@ impl<T> [T] { /// v == [2, 1, -3, 4, -5] || /// v == [2, 1, -3, -5, 4]); /// ``` - #[unstable(feature = "slice_partition_at_index", issue = "55300")] + #[unstable(feature = "slice_select_nth_unstable", issue = "55300")] #[inline] - pub fn partition_at_index_by_key<K, F>( + pub fn select_nth_unstable_by_key<K, F>( &mut self, index: usize, mut f: F, @@ -2185,7 +2185,7 @@ impl<T> [T] { K: Ord, { let mut g = |a: &T, b: &T| f(a).lt(&f(b)); - sort::partition_at_index(self, index, &mut g) + sort::select_nth_unstable(self, index, &mut g) } /// Moves all consecutive repeated elements to the end of the slice according to the diff --git a/library/core/src/slice/sort.rs b/library/core/src/slice/sort.rs index 71d2c2c9b2f4c..9ce3628733cfe 100644 --- a/library/core/src/slice/sort.rs +++ b/library/core/src/slice/sort.rs @@ -768,7 +768,7 @@ where recurse(v, &mut is_less, None, limit); } -fn partition_at_index_loop<'a, T, F>( +fn select_nth_unstable_loop<'a, T, F>( mut v: &'a mut [T], mut index: usize, is_less: &mut F, @@ -828,7 +828,7 @@ fn partition_at_index_loop<'a, T, F>( } } -pub fn partition_at_index<T, F>( +pub fn select_nth_unstable<T, F>( v: &mut [T], index: usize, mut is_less: F, @@ -840,7 +840,7 @@ where use cmp::Ordering::Less; if index >= v.len() { - panic!("partition_at_index index {} greater than length of slice {}", index, v.len()); + panic!("select_nth_unstable index {} greater than length of slice {}", index, v.len()); } if mem::size_of::<T>() == 0 { @@ -864,7 +864,7 @@ where .unwrap(); v.swap(min_index, index); } else { - partition_at_index_loop(v, index, &mut is_less, None); + select_nth_unstable_loop(v, index, &mut is_less, None); } let (left, right) = v.split_at_mut(index); diff --git a/library/core/tests/lib.rs b/library/core/tests/lib.rs index 4db391f3e567e..31b20f4523183 100644 --- a/library/core/tests/lib.rs +++ b/library/core/tests/lib.rs @@ -26,7 +26,7 @@ #![feature(pattern)] #![feature(raw)] #![feature(sort_internals)] -#![feature(slice_partition_at_index)] +#![feature(slice_select_nth_unstable)] #![feature(min_specialization)] #![feature(step_trait)] #![feature(step_trait_ext)] diff --git a/library/core/tests/slice.rs b/library/core/tests/slice.rs index 9556d43f9d78b..9e01a2c885c8b 100644 --- a/library/core/tests/slice.rs +++ b/library/core/tests/slice.rs @@ -1570,7 +1570,7 @@ fn sort_unstable() { #[test] #[cfg(not(target_arch = "wasm32"))] #[cfg_attr(miri, ignore)] // Miri is too slow -fn partition_at_index() { +fn select_nth_unstable() { use core::cmp::Ordering::{Equal, Greater, Less}; use rand::rngs::StdRng; use rand::seq::SliceRandom; @@ -1596,7 +1596,7 @@ fn partition_at_index() { // Sort in default order. for pivot in 0..len { let mut v = orig.clone(); - v.partition_at_index(pivot); + v.select_nth_unstable(pivot); assert_eq!(v_sorted[pivot], v[pivot]); for i in 0..pivot { @@ -1609,7 +1609,7 @@ fn partition_at_index() { // Sort in ascending order. for pivot in 0..len { 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); @@ -1632,7 +1632,7 @@ fn partition_at_index() { for pivot in 0..len { 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]); for i in 0..pivot { @@ -1653,7 +1653,7 @@ fn partition_at_index() { } 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(); for i in 0..v.len() { assert_eq!(v[i], i as i32); @@ -1661,28 +1661,28 @@ fn partition_at_index() { } // Should not panic. - [(); 10].partition_at_index(0); - [(); 10].partition_at_index(5); - [(); 10].partition_at_index(9); - [(); 100].partition_at_index(0); - [(); 100].partition_at_index(50); - [(); 100].partition_at_index(99); + [(); 10].select_nth_unstable(0); + [(); 10].select_nth_unstable(5); + [(); 10].select_nth_unstable(9); + [(); 100].select_nth_unstable(0); + [(); 100].select_nth_unstable(50); + [(); 100].select_nth_unstable(99); let mut v = [0xDEADBEEFu64]; - v.partition_at_index(0); + v.select_nth_unstable(0); assert!(v == [0xDEADBEEF]); } #[test] #[should_panic(expected = "index 0 greater than length of slice")] -fn partition_at_index_zero_length() { - [0i32; 0].partition_at_index(0); +fn select_nth_unstable_zero_length() { + [0i32; 0].select_nth_unstable(0); } #[test] #[should_panic(expected = "index 20 greater than length of slice")] -fn partition_at_index_past_length() { - [0i32; 10].partition_at_index(20); +fn select_nth_unstable_past_length() { + [0i32; 10].select_nth_unstable(20); } pub mod memchr {