@@ -1585,6 +1585,153 @@ impl<T> [T] {
15851585 sort:: quicksort ( self , |a, b| f ( a) . lt ( & f ( b) ) ) ;
15861586 }
15871587
1588+ /// Reorder the slice such that the element at `index` is at its final sorted position.
1589+ ///
1590+ /// This reordering has the additional property that any value at position `i < index` will be
1591+ /// less than or equal to any value at a position `j > index`. Additionally, this reordering is
1592+ /// unstable (i.e. any number of equal elements may end up at position `index`), in-place
1593+ /// (i.e. does not allocate), and `O(n)` worst-case. This function is also/ known as "kth
1594+ /// element" in other libraries. It returns a triplet of the following values: all elements less
1595+ /// than the one at the given index, the value at the given index, and all elements greater than
1596+ /// the one at the given index.
1597+ ///
1598+ /// # Current implementation
1599+ ///
1600+ /// The current algorithm is based on the quickselect portion of the same quicksort algorithm
1601+ /// used for [`sort_unstable`].
1602+ ///
1603+ /// [`sort_unstable`]: #method.sort_unstable
1604+ ///
1605+ /// # Panics
1606+ ///
1607+ /// Panics when `index >= len()`, meaning it always panics on empty slices.
1608+ ///
1609+ /// # Examples
1610+ ///
1611+ /// ```
1612+ /// #![feature(slice_partition_at_index)]
1613+ ///
1614+ /// let mut v = [-5i32, 4, 1, -3, 2];
1615+ ///
1616+ /// // Find the median
1617+ /// v.partition_at_index(2);
1618+ ///
1619+ /// // We are only guaranteed the slice will be one of the following, based on the way we sort
1620+ /// // about the specified index.
1621+ /// assert!(v == [-3, -5, 1, 2, 4] ||
1622+ /// v == [-5, -3, 1, 2, 4] ||
1623+ /// v == [-3, -5, 1, 4, 2] ||
1624+ /// v == [-5, -3, 1, 4, 2]);
1625+ /// ```
1626+ #[ unstable( feature = "slice_partition_at_index" , issue = "55300" ) ]
1627+ #[ inline]
1628+ pub fn partition_at_index ( & mut self , index : usize ) -> ( & mut [ T ] , & mut T , & mut [ T ] )
1629+ where T : Ord
1630+ {
1631+ let mut f = |a : & T , b : & T | a. lt ( b) ;
1632+ sort:: partition_at_index ( self , index, & mut f)
1633+ }
1634+
1635+ /// Reorder the slice with a comparator function such that the element at `index` is at its
1636+ /// final sorted position.
1637+ ///
1638+ /// This reordering has the additional property that any value at position `i < index` will be
1639+ /// less than or equal to any value at a position `j > index` using the comparator function.
1640+ /// Additionally, this reordering is unstable (i.e. any number of equal elements may end up at
1641+ /// position `index`), in-place (i.e. does not allocate), and `O(n)` worst-case. This function
1642+ /// is also known as "kth element" in other libraries. It returns a triplet of the following
1643+ /// values: all elements less than the one at the given index, the value at the given index,
1644+ /// and all elements greater than the one at the given index, using the provided comparator
1645+ /// function.
1646+ ///
1647+ /// # Current implementation
1648+ ///
1649+ /// The current algorithm is based on the quickselect portion of the same quicksort algorithm
1650+ /// used for [`sort_unstable`].
1651+ ///
1652+ /// [`sort_unstable`]: #method.sort_unstable
1653+ ///
1654+ /// # Panics
1655+ ///
1656+ /// Panics when `index >= len()`, meaning it always panics on empty slices.
1657+ ///
1658+ /// # Examples
1659+ ///
1660+ /// ```
1661+ /// #![feature(slice_partition_at_index)]
1662+ ///
1663+ /// let mut v = [-5i32, 4, 1, -3, 2];
1664+ ///
1665+ /// // Find the median as if the slice were sorted in descending order.
1666+ /// v.partition_at_index_by(2, |a, b| b.cmp(a));
1667+ ///
1668+ /// // We are only guaranteed the slice will be one of the following, based on the way we sort
1669+ /// // about the specified index.
1670+ /// assert!(v == [2, 4, 1, -5, -3] ||
1671+ /// v == [2, 4, 1, -3, -5] ||
1672+ /// v == [4, 2, 1, -5, -3] ||
1673+ /// v == [4, 2, 1, -3, -5]);
1674+ /// ```
1675+ #[ unstable( feature = "slice_partition_at_index" , issue = "55300" ) ]
1676+ #[ inline]
1677+ pub fn partition_at_index_by < F > ( & mut self , index : usize , mut compare : F )
1678+ -> ( & mut [ T ] , & mut T , & mut [ T ] )
1679+ where F : FnMut ( & T , & T ) -> Ordering
1680+ {
1681+ let mut f = |a : & T , b : & T | compare ( a, b) == Less ;
1682+ sort:: partition_at_index ( self , index, & mut f)
1683+ }
1684+
1685+ /// Reorder the slice with a key extraction function such that the element at `index` is at its
1686+ /// final sorted position.
1687+ ///
1688+ /// This reordering has the additional property that any value at position `i < index` will be
1689+ /// less than or equal to any value at a position `j > index` using the key extraction function.
1690+ /// Additionally, this reordering is unstable (i.e. any number of equal elements may end up at
1691+ /// position `index`), in-place (i.e. does not allocate), and `O(n)` worst-case. This function
1692+ /// is also known as "kth element" in other libraries. It returns a triplet of the following
1693+ /// values: all elements less than the one at the given index, the value at the given index, and
1694+ /// all elements greater than the one at the given index, using the provided key extraction
1695+ /// function.
1696+ ///
1697+ /// # Current implementation
1698+ ///
1699+ /// The current algorithm is based on the quickselect portion of the same quicksort algorithm
1700+ /// used for [`sort_unstable`].
1701+ ///
1702+ /// [`sort_unstable`]: #method.sort_unstable
1703+ ///
1704+ /// # Panics
1705+ ///
1706+ /// Panics when `index >= len()`, meaning it always panics on empty slices.
1707+ ///
1708+ /// # Examples
1709+ ///
1710+ /// ```
1711+ /// #![feature(slice_partition_at_index)]
1712+ ///
1713+ /// let mut v = [-5i32, 4, 1, -3, 2];
1714+ ///
1715+ /// // Return the median as if the array were sorted according to absolute value.
1716+ /// v.partition_at_index_by_key(2, |a| a.abs());
1717+ ///
1718+ /// // We are only guaranteed the slice will be one of the following, based on the way we sort
1719+ /// // about the specified index.
1720+ /// assert!(v == [1, 2, -3, 4, -5] ||
1721+ /// v == [1, 2, -3, -5, 4] ||
1722+ /// v == [2, 1, -3, 4, -5] ||
1723+ /// v == [2, 1, -3, -5, 4]);
1724+ /// ```
1725+ #[ unstable( feature = "slice_partition_at_index" , issue = "55300" ) ]
1726+ #[ inline]
1727+ pub fn partition_at_index_by_key < K , F > ( & mut self , index : usize , mut f : F )
1728+ -> ( & mut [ T ] , & mut T , & mut [ T ] )
1729+ where F : FnMut ( & T ) -> K , K : Ord
1730+ {
1731+ let mut g = |a : & T , b : & T | f ( a) . lt ( & f ( b) ) ;
1732+ sort:: partition_at_index ( self , index, & mut g)
1733+ }
1734+
15881735 /// Moves all consecutive repeated elements to the end of the slice according to the
15891736 /// [`PartialEq`] trait implementation.
15901737 ///
0 commit comments