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