From 5afe4a9e09b09b2a74cf5cbce5b69ca40b3873a9 Mon Sep 17 00:00:00 2001 From: Andy Kurnia Date: Sat, 23 Mar 2024 21:38:32 +0800 Subject: [PATCH 1/2] improve example on inserting to a sorted vector to avoid shifting equal elements --- library/alloc/src/collections/vec_deque/mod.rs | 2 +- library/core/src/slice/mod.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs index 41adc2e79dc74..41935a694209b 100644 --- a/library/alloc/src/collections/vec_deque/mod.rs +++ b/library/alloc/src/collections/vec_deque/mod.rs @@ -2464,7 +2464,7 @@ impl 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(|&x| x < num); + /// let idx = deque.partition_point(|&x| x <= 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]); diff --git a/library/core/src/slice/mod.rs b/library/core/src/slice/mod.rs index 4a574bf034745..b1ae07b05f7ee 100644 --- a/library/core/src/slice/mod.rs +++ b/library/core/src/slice/mod.rs @@ -2728,7 +2728,7 @@ impl [T] { /// ``` /// 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(|&x| x < num); + /// let idx = s.partition_point(|&x| x <= 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]); @@ -4179,7 +4179,7 @@ impl [T] { /// ``` /// 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(|&x| x < num); + /// let idx = s.partition_point(|&x| x <= num); /// s.insert(idx, num); /// assert_eq!(s, [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 42, 55]); /// ``` From 643029693b2987b4b7c8b5073e8a25536cca7069 Mon Sep 17 00:00:00 2001 From: Andy Kurnia Date: Sun, 24 Mar 2024 08:15:00 +0800 Subject: [PATCH 2/2] clarify equivalency of binary_search and partition_point --- library/alloc/src/collections/vec_deque/mod.rs | 4 +++- library/core/src/slice/mod.rs | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs index 41935a694209b..b1752a3465fe0 100644 --- a/library/alloc/src/collections/vec_deque/mod.rs +++ b/library/alloc/src/collections/vec_deque/mod.rs @@ -2465,7 +2465,9 @@ impl 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(|&x| x <= num); - /// // The above is equivalent to `let idx = deque.binary_search(&num).unwrap_or_else(|x| x);` + /// // If `num` is unique, `s.partition_point(|&x| x < num)` (with `<`) is equivalent to + /// // `s.binary_search(&num).unwrap_or_else(|x| x)`, but using `<=` may allow `insert` + /// // to shift less elements. /// deque.insert(idx, num); /// assert_eq!(deque, &[0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 42, 55]); /// ``` diff --git a/library/core/src/slice/mod.rs b/library/core/src/slice/mod.rs index b1ae07b05f7ee..d30ede7d1f942 100644 --- a/library/core/src/slice/mod.rs +++ b/library/core/src/slice/mod.rs @@ -2729,7 +2729,9 @@ impl [T] { /// 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(|&x| x <= num); - /// // The above is equivalent to `let idx = s.binary_search(&num).unwrap_or_else(|x| x);` + /// // If `num` is unique, `s.partition_point(|&x| x < num)` (with `<`) is equivalent to + /// // `s.binary_search(&num).unwrap_or_else(|x| x)`, but using `<=` will allow `insert` + /// // to shift less elements. /// s.insert(idx, num); /// assert_eq!(s, [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 42, 55]); /// ```