Skip to content

Commit

Permalink
Update binary_search example to instead redirect to partition_point
Browse files Browse the repository at this point in the history
  • Loading branch information
yaahc committed Apr 6, 2022
1 parent c2afaba commit c957b80
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
18 changes: 16 additions & 2 deletions library/alloc/src/collections/vec_deque/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
/// ```
Expand Down Expand Up @@ -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
Expand Down
16 changes: 14 additions & 2 deletions library/core/src/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
/// ```
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c957b80

Please sign in to comment.