mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +00:00
Update binary_search example to instead redirect to partition_point
This commit is contained in:
parent
c2afaba465
commit
c957b809e9
@ -2593,14 +2593,15 @@ impl<T, A: Allocator> VecDeque<T, A> {
|
||||
/// ```
|
||||
///
|
||||
/// If you want to insert an item to a sorted deque, while maintaining
|
||||
/// sort order:
|
||||
/// sort order, consider using [`partition_point`]:
|
||||
///
|
||||
/// ```
|
||||
/// use std::collections::VecDeque;
|
||||
///
|
||||
/// let mut deque: VecDeque<_> = [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55].into();
|
||||
/// let num = 42;
|
||||
/// let idx = deque.binary_search(&num).unwrap_or_else(|x| x);
|
||||
/// let idx = deque.partition_point(&num);
|
||||
/// // The above is equivalent to `let idx = deque.binary_search(&num).unwrap_or_else(|x| x);`
|
||||
/// deque.insert(idx, num);
|
||||
/// assert_eq!(deque, &[0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 42, 55]);
|
||||
/// ```
|
||||
@ -2744,6 +2745,19 @@ impl<T, A: Allocator> VecDeque<T, A> {
|
||||
/// assert!(deque.iter().take(i).all(|&x| x < 5));
|
||||
/// assert!(deque.iter().skip(i).all(|&x| !(x < 5)));
|
||||
/// ```
|
||||
///
|
||||
/// If you want to insert an item to a sorted deque, while maintaining
|
||||
/// sort order:
|
||||
///
|
||||
/// ```
|
||||
/// use std::collections::VecDeque;
|
||||
///
|
||||
/// let mut deque: VecDeque<_> = [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55].into();
|
||||
/// let num = 42;
|
||||
/// let idx = deque.partition_point(&num);
|
||||
/// deque.insert(idx, num);
|
||||
/// assert_eq!(deque, &[0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 42, 55]);
|
||||
/// ```
|
||||
#[stable(feature = "vecdeque_binary_search", since = "1.54.0")]
|
||||
pub fn partition_point<P>(&self, mut pred: P) -> usize
|
||||
where
|
||||
|
@ -2332,12 +2332,13 @@ impl<T> [T] {
|
||||
/// ```
|
||||
///
|
||||
/// If you want to insert an item to a sorted vector, while maintaining
|
||||
/// sort order:
|
||||
/// sort order, consider using [`partition_point`]:
|
||||
///
|
||||
/// ```
|
||||
/// let mut s = vec![0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55];
|
||||
/// let num = 42;
|
||||
/// let idx = s.binary_search(&num).unwrap_or_else(|x| x);
|
||||
/// let idx = s.partition_point(&num);
|
||||
/// // The above is equivalent to `let idx = s.binary_search(&num).unwrap_or_else(|x| x);`
|
||||
/// s.insert(idx, num);
|
||||
/// assert_eq!(s, [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 42, 55]);
|
||||
/// ```
|
||||
@ -3744,6 +3745,17 @@ impl<T> [T] {
|
||||
/// assert!(v[..i].iter().all(|&x| x < 5));
|
||||
/// assert!(v[i..].iter().all(|&x| !(x < 5)));
|
||||
/// ```
|
||||
///
|
||||
/// If you want to insert an item to a sorted vector, while maintaining
|
||||
/// sort order:
|
||||
///
|
||||
/// ```
|
||||
/// let mut s = vec![0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55];
|
||||
/// let num = 42;
|
||||
/// let idx = s.partition_point(&num);
|
||||
/// s.insert(idx, num);
|
||||
/// assert_eq!(s, [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 42, 55]);
|
||||
/// ```
|
||||
#[stable(feature = "partition_point", since = "1.52.0")]
|
||||
#[must_use]
|
||||
pub fn partition_point<P>(&self, mut pred: P) -> usize
|
||||
|
Loading…
Reference in New Issue
Block a user