From 348b42f3cde32ca416ad76e3ceb8bbe3090fd01e Mon Sep 17 00:00:00 2001 From: Jacob Finkelman Date: Tue, 24 Oct 2023 21:23:58 +0000 Subject: [PATCH 1/6] add binary_search and friends --- src/map.rs | 40 ++++++++++++++++++++++++++++++++++++ src/map/slice.rs | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ src/set.rs | 40 ++++++++++++++++++++++++++++++++++++ src/set/slice.rs | 52 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 185 insertions(+) diff --git a/src/map.rs b/src/map.rs index cb405caf..af724940 100644 --- a/src/map.rs +++ b/src/map.rs @@ -794,6 +794,46 @@ where }); } + /// Search over a sorted map with a comparator function. + /// + /// Returns the position where that value is present, or the position where can be inserted to maintain the sort. + /// see [slice::binary_search_by] for more details. + /// **O(log(n))** + #[inline] + pub fn binary_search_by<'a, F>(&'a self, f: F) -> Result + where + F: FnMut(&'a K, &'a V) -> Ordering, + { + self.as_slice().binary_search_by(f) + } + + /// Search over a sorted map with a key extraction function. + /// + /// Returns the position where that value is present, or the position where can be inserted to maintain the sort. + /// see [slice::binary_search_by_key] for more details. + /// **O(log(n))** + #[inline] + pub fn binary_search_by_key<'a, B, F>(&'a self, b: &B, f: F) -> Result + where + F: FnMut(&'a K, &'a V) -> B, + B: Ord, + { + self.as_slice().binary_search_by_key(b, f) + } + + /// Returns the index of the partition point or a sorted map according to the given predicate + /// (the index of the first element of the second partition). + /// + /// see [slice::partition_point] for more details. + /// **O(log(n))** + #[must_use] + pub fn partition_point

(&self, pred: P) -> usize + where + P: FnMut(&K, &V) -> bool, + { + self.as_slice().partition_point(pred) + } + /// Reverses the order of the map’s key-value pairs in place. /// /// Computes in **O(n)** time and **O(1)** space. diff --git a/src/map/slice.rs b/src/map/slice.rs index 9fb876fd..ec3c914b 100644 --- a/src/map/slice.rs +++ b/src/map/slice.rs @@ -201,6 +201,59 @@ impl Slice { pub fn into_values(self: Box) -> IntoValues { IntoValues::new(self.into_entries()) } + + /// Search over a sorted map for a value. + /// + /// Returns the position where that value is present, or the position where can be inserted to maintain the sort. + /// see [slice::binary_search] for more details. + /// **O(log(n))**, which is notably less scalable than looking the value up in the map set is a slice from. + pub fn binary_search_keys(&self, x: &K) -> Result + where + K: Ord, + { + self.binary_search_by(|p, _| p.cmp(x)) + } + + /// Search over a sorted map with a comparator function. + /// + /// Returns the position where that value is present, or the position where can be inserted to maintain the sort. + /// see [slice::binary_search_by] for more details. + /// **O(log(n))** + #[inline] + pub fn binary_search_by<'a, F>(&'a self, mut f: F) -> Result + where + F: FnMut(&'a K, &'a V) -> Ordering, + { + self.entries.binary_search_by(move |a| f(&a.key, &a.value)) + } + + /// Search over a sorted map with a key extraction function. + /// + /// Returns the position where that value is present, or the position where can be inserted to maintain the sort. + /// see [slice::binary_search_by_key] for more details. + /// **O(log(n))** + #[inline] + pub fn binary_search_by_key<'a, B, F>(&'a self, b: &B, mut f: F) -> Result + where + F: FnMut(&'a K, &'a V) -> B, + B: Ord, + { + self.binary_search_by(|k, v| f(k, v).cmp(b)) + } + + /// Returns the index of the partition point or a sorted map according to the given predicate + /// (the index of the first element of the second partition). + /// + /// see [slice::partition_point] for more details. + /// **O(log(n))** + #[must_use] + pub fn partition_point

(&self, mut pred: P) -> usize + where + P: FnMut(&K, &V) -> bool, + { + self.entries + .partition_point(move |a| pred(&a.key, &a.value)) + } } impl<'a, K, V> IntoIterator for &'a Slice { diff --git a/src/set.rs b/src/set.rs index 811f462e..bfb0e528 100644 --- a/src/set.rs +++ b/src/set.rs @@ -688,6 +688,46 @@ where }); } + /// Search over a sorted set with a comparator function. + /// + /// Returns the position where that value is present, or the position where can be inserted to maintain the sort. + /// see [slice::binary_search_by] for more details. + /// **O(log(n))** + #[inline] + pub fn binary_search_by<'a, F>(&'a self, f: F) -> Result + where + F: FnMut(&'a T) -> Ordering, + { + self.as_slice().binary_search_by(f) + } + + /// Search over a sorted set with a key extraction function. + /// + /// Returns the position where that value is present, or the position where can be inserted to maintain the sort. + /// see [slice::binary_search_by_key] for more details. + /// **O(log(n))** + #[inline] + pub fn binary_search_by_key<'a, B, F>(&'a self, b: &B, f: F) -> Result + where + F: FnMut(&'a T) -> B, + B: Ord, + { + self.as_slice().binary_search_by_key(b, f) + } + + /// Returns the index of the partition point or a sorted set according to the given predicate + /// (the index of the first element of the second partition). + /// + /// see [slice::partition_point] for more details. + /// **O(log(n))** + #[must_use] + pub fn partition_point

(&self, pred: P) -> usize + where + P: FnMut(&T) -> bool, + { + self.as_slice().partition_point(pred) + } + /// Reverses the order of the set’s values in place. /// /// Computes in **O(n)** time and **O(1)** space. diff --git a/src/set/slice.rs b/src/set/slice.rs index 608311d2..9f2a2d53 100644 --- a/src/set/slice.rs +++ b/src/set/slice.rs @@ -109,6 +109,58 @@ impl Slice { pub fn iter(&self) -> Iter<'_, T> { Iter::new(&self.entries) } + + /// Search over a sorted set for a value. + /// + /// Returns the position where that value is present, or the position where can be inserted to maintain the sort. + /// see [slice::binary_search] for more details. + /// **O(log(n))**, which is notably less scalable than looking the value up in the set this is a slice from. + pub fn binary_search(&self, x: &T) -> Result + where + T: Ord, + { + self.binary_search_by(|p| p.cmp(x)) + } + + /// Search over a sorted set with a comparator function. + /// + /// Returns the position where that value is present, or the position where can be inserted to maintain the sort. + /// see [slice::binary_search_by] for more details. + /// **O(log(n))** + #[inline] + pub fn binary_search_by<'a, F>(&'a self, mut f: F) -> Result + where + F: FnMut(&'a T) -> Ordering, + { + self.entries.binary_search_by(move |a| f(&a.key)) + } + + /// Search over a sorted set with a key extraction function. + /// + /// Returns the position where that value is present, or the position where can be inserted to maintain the sort. + /// see [slice::binary_search_by_key] for more details. + /// **O(log(n))** + #[inline] + pub fn binary_search_by_key<'a, B, F>(&'a self, b: &B, mut f: F) -> Result + where + F: FnMut(&'a T) -> B, + B: Ord, + { + self.binary_search_by(|k| f(k).cmp(b)) + } + + /// Returns the index of the partition point or a sorted set according to the given predicate + /// (the index of the first element of the second partition). + /// + /// see [slice::partition_point] for more details. + /// **O(log(n))** + #[must_use] + pub fn partition_point

(&self, mut pred: P) -> usize + where + P: FnMut(&T) -> bool, + { + self.entries.partition_point(move |a| pred(&a.key)) + } } impl<'a, T> IntoIterator for &'a Slice { From d93534fa2937599e2eb6fa59daa55e2ba2705f55 Mon Sep 17 00:00:00 2001 From: Jacob Finkelman Date: Thu, 26 Oct 2023 19:50:29 +0000 Subject: [PATCH 2/6] add tests --- src/map/tests.rs | 221 +++++++++++++++++++++++++++++++++++++++++++++++ src/set/tests.rs | 139 +++++++++++++++++++++++++++++ 2 files changed, 360 insertions(+) diff --git a/src/map/tests.rs b/src/map/tests.rs index f273d716..3f213212 100644 --- a/src/map/tests.rs +++ b/src/map/tests.rs @@ -447,3 +447,224 @@ fn iter_default() { assert_default::>(); assert_default::>(); } + +#[test] +fn test_binary_search_by() { + // addaped from stds test for binary_search + let b: IndexMap<_, i32> = [] + .into_iter() + .enumerate() + .map(|(i, x)| (i + 100, x)) + .collect(); + assert_eq!(b.binary_search_by(|_, x| x.cmp(&5)), Err(0)); + + let b: IndexMap<_, i32> = [4] + .into_iter() + .enumerate() + .map(|(i, x)| (i + 100, x)) + .collect(); + assert_eq!(b.binary_search_by(|_, x| x.cmp(&3)), Err(0)); + assert_eq!(b.binary_search_by(|_, x| x.cmp(&4)), Ok(0)); + assert_eq!(b.binary_search_by(|_, x| x.cmp(&5)), Err(1)); + + let b: IndexMap<_, i32> = [1, 2, 4, 6, 8, 9] + .into_iter() + .enumerate() + .map(|(i, x)| (i + 100, x)) + .collect(); + assert_eq!(b.binary_search_by(|_, x| x.cmp(&5)), Err(3)); + assert_eq!(b.binary_search_by(|_, x| x.cmp(&6)), Ok(3)); + assert_eq!(b.binary_search_by(|_, x| x.cmp(&7)), Err(4)); + assert_eq!(b.binary_search_by(|_, x| x.cmp(&8)), Ok(4)); + + let b: IndexMap<_, i32> = [1, 2, 4, 5, 6, 8] + .into_iter() + .enumerate() + .map(|(i, x)| (i + 100, x)) + .collect(); + assert_eq!(b.binary_search_by(|_, x| x.cmp(&9)), Err(6)); + + let b: IndexMap<_, i32> = [1, 2, 4, 6, 7, 8, 9] + .into_iter() + .enumerate() + .map(|(i, x)| (i + 100, x)) + .collect(); + assert_eq!(b.binary_search_by(|_, x| x.cmp(&6)), Ok(3)); + assert_eq!(b.binary_search_by(|_, x| x.cmp(&5)), Err(3)); + assert_eq!(b.binary_search_by(|_, x| x.cmp(&8)), Ok(5)); + + let b: IndexMap<_, i32> = [1, 2, 4, 5, 6, 8, 9] + .into_iter() + .enumerate() + .map(|(i, x)| (i + 100, x)) + .collect(); + assert_eq!(b.binary_search_by(|_, x| x.cmp(&7)), Err(5)); + assert_eq!(b.binary_search_by(|_, x| x.cmp(&0)), Err(0)); + + let b: IndexMap<_, i32> = [1, 3, 3, 3, 7] + .into_iter() + .enumerate() + .map(|(i, x)| (i + 100, x)) + .collect(); + assert_eq!(b.binary_search_by(|_, x| x.cmp(&0)), Err(0)); + assert_eq!(b.binary_search_by(|_, x| x.cmp(&1)), Ok(0)); + assert_eq!(b.binary_search_by(|_, x| x.cmp(&2)), Err(1)); + assert!(match b.binary_search_by(|_, x| x.cmp(&3)) { + Ok(1..=3) => true, + _ => false, + }); + assert!(match b.binary_search_by(|_, x| x.cmp(&3)) { + Ok(1..=3) => true, + _ => false, + }); + assert_eq!(b.binary_search_by(|_, x| x.cmp(&4)), Err(4)); + assert_eq!(b.binary_search_by(|_, x| x.cmp(&5)), Err(4)); + assert_eq!(b.binary_search_by(|_, x| x.cmp(&6)), Err(4)); + assert_eq!(b.binary_search_by(|_, x| x.cmp(&7)), Ok(4)); + assert_eq!(b.binary_search_by(|_, x| x.cmp(&8)), Err(5)); +} + +#[test] +fn test_binary_search_by_key() { + // addaped from stds test for binary_search + let b: IndexMap<_, i32> = [] + .into_iter() + .enumerate() + .map(|(i, x)| (i + 100, x)) + .collect(); + assert_eq!(b.binary_search_by_key(&5, |_, &x| x), Err(0)); + + let b: IndexMap<_, i32> = [4] + .into_iter() + .enumerate() + .map(|(i, x)| (i + 100, x)) + .collect(); + assert_eq!(b.binary_search_by_key(&3, |_, &x| x), Err(0)); + assert_eq!(b.binary_search_by_key(&4, |_, &x| x), Ok(0)); + assert_eq!(b.binary_search_by_key(&5, |_, &x| x), Err(1)); + + let b: IndexMap<_, i32> = [1, 2, 4, 6, 8, 9] + .into_iter() + .enumerate() + .map(|(i, x)| (i + 100, x)) + .collect(); + assert_eq!(b.binary_search_by_key(&5, |_, &x| x), Err(3)); + assert_eq!(b.binary_search_by_key(&6, |_, &x| x), Ok(3)); + assert_eq!(b.binary_search_by_key(&7, |_, &x| x), Err(4)); + assert_eq!(b.binary_search_by_key(&8, |_, &x| x), Ok(4)); + + let b: IndexMap<_, i32> = [1, 2, 4, 5, 6, 8] + .into_iter() + .enumerate() + .map(|(i, x)| (i + 100, x)) + .collect(); + assert_eq!(b.binary_search_by_key(&9, |_, &x| x), Err(6)); + + let b: IndexMap<_, i32> = [1, 2, 4, 6, 7, 8, 9] + .into_iter() + .enumerate() + .map(|(i, x)| (i + 100, x)) + .collect(); + assert_eq!(b.binary_search_by_key(&6, |_, &x| x), Ok(3)); + assert_eq!(b.binary_search_by_key(&5, |_, &x| x), Err(3)); + assert_eq!(b.binary_search_by_key(&8, |_, &x| x), Ok(5)); + + let b: IndexMap<_, i32> = [1, 2, 4, 5, 6, 8, 9] + .into_iter() + .enumerate() + .map(|(i, x)| (i + 100, x)) + .collect(); + assert_eq!(b.binary_search_by_key(&7, |_, &x| x), Err(5)); + assert_eq!(b.binary_search_by_key(&0, |_, &x| x), Err(0)); + + let b: IndexMap<_, i32> = [1, 3, 3, 3, 7] + .into_iter() + .enumerate() + .map(|(i, x)| (i + 100, x)) + .collect(); + assert_eq!(b.binary_search_by_key(&0, |_, &x| x), Err(0)); + assert_eq!(b.binary_search_by_key(&1, |_, &x| x), Ok(0)); + assert_eq!(b.binary_search_by_key(&2, |_, &x| x), Err(1)); + assert!(match b.binary_search_by_key(&3, |_, &x| x) { + Ok(1..=3) => true, + _ => false, + }); + assert!(match b.binary_search_by_key(&3, |_, &x| x) { + Ok(1..=3) => true, + _ => false, + }); + assert_eq!(b.binary_search_by_key(&4, |_, &x| x), Err(4)); + assert_eq!(b.binary_search_by_key(&5, |_, &x| x), Err(4)); + assert_eq!(b.binary_search_by_key(&6, |_, &x| x), Err(4)); + assert_eq!(b.binary_search_by_key(&7, |_, &x| x), Ok(4)); + assert_eq!(b.binary_search_by_key(&8, |_, &x| x), Err(5)); +} + +#[test] +fn test_partition_point() { + // addaped from stds test for partition_point + let b: IndexMap<_, i32> = [] + .into_iter() + .enumerate() + .map(|(i, x)| (i + 100, x)) + .collect(); + assert_eq!(b.partition_point(|_, &x| x < 5), 0); + + let b: IndexMap<_, i32> = [4] + .into_iter() + .enumerate() + .map(|(i, x)| (i + 100, x)) + .collect(); + assert_eq!(b.partition_point(|_, &x| x < 3), 0); + assert_eq!(b.partition_point(|_, &x| x < 4), 0); + assert_eq!(b.partition_point(|_, &x| x < 5), 1); + + let b: IndexMap<_, i32> = [1, 2, 4, 6, 8, 9] + .into_iter() + .enumerate() + .map(|(i, x)| (i + 100, x)) + .collect(); + assert_eq!(b.partition_point(|_, &x| x < 5), 3); + assert_eq!(b.partition_point(|_, &x| x < 6), 3); + assert_eq!(b.partition_point(|_, &x| x < 7), 4); + assert_eq!(b.partition_point(|_, &x| x < 8), 4); + + let b: IndexMap<_, i32> = [1, 2, 4, 5, 6, 8] + .into_iter() + .enumerate() + .map(|(i, x)| (i + 100, x)) + .collect(); + assert_eq!(b.partition_point(|_, &x| x < 9), 6); + + let b: IndexMap<_, i32> = [1, 2, 4, 6, 7, 8, 9] + .into_iter() + .enumerate() + .map(|(i, x)| (i + 100, x)) + .collect(); + assert_eq!(b.partition_point(|_, &x| x < 6), 3); + assert_eq!(b.partition_point(|_, &x| x < 5), 3); + assert_eq!(b.partition_point(|_, &x| x < 8), 5); + + let b: IndexMap<_, i32> = [1, 2, 4, 5, 6, 8, 9] + .into_iter() + .enumerate() + .map(|(i, x)| (i + 100, x)) + .collect(); + assert_eq!(b.partition_point(|_, &x| x < 7), 5); + assert_eq!(b.partition_point(|_, &x| x < 0), 0); + + let b: IndexMap<_, i32> = [1, 3, 3, 3, 7] + .into_iter() + .enumerate() + .map(|(i, x)| (i + 100, x)) + .collect(); + assert_eq!(b.partition_point(|_, &x| x < 0), 0); + assert_eq!(b.partition_point(|_, &x| x < 1), 0); + assert_eq!(b.partition_point(|_, &x| x < 2), 1); + assert_eq!(b.partition_point(|_, &x| x < 3), 1); + assert_eq!(b.partition_point(|_, &x| x < 4), 4); + assert_eq!(b.partition_point(|_, &x| x < 5), 4); + assert_eq!(b.partition_point(|_, &x| x < 6), 4); + assert_eq!(b.partition_point(|_, &x| x < 7), 4); + assert_eq!(b.partition_point(|_, &x| x < 8), 5); +} diff --git a/src/set/tests.rs b/src/set/tests.rs index 44f8ed84..19509ee3 100644 --- a/src/set/tests.rs +++ b/src/set/tests.rs @@ -543,3 +543,142 @@ fn iter_default() { assert_default::>(); assert_default::>(); } + +#[test] +fn test_binary_search_by() { + // addaped from stds test for binary_search + let b: IndexSet = [].into(); + assert_eq!(b.binary_search_by(|x| x.cmp(&5)), Err(0)); + + let b: IndexSet = [4].into(); + assert_eq!(b.binary_search_by(|x| x.cmp(&3)), Err(0)); + assert_eq!(b.binary_search_by(|x| x.cmp(&4)), Ok(0)); + assert_eq!(b.binary_search_by(|x| x.cmp(&5)), Err(1)); + + let b: IndexSet = [1, 2, 4, 6, 8, 9].into(); + assert_eq!(b.binary_search_by(|x| x.cmp(&5)), Err(3)); + assert_eq!(b.binary_search_by(|x| x.cmp(&6)), Ok(3)); + assert_eq!(b.binary_search_by(|x| x.cmp(&7)), Err(4)); + assert_eq!(b.binary_search_by(|x| x.cmp(&8)), Ok(4)); + + let b: IndexSet = [1, 2, 4, 5, 6, 8].into(); + assert_eq!(b.binary_search_by(|x| x.cmp(&9)), Err(6)); + + let b: IndexSet = [1, 2, 4, 6, 7, 8, 9].into(); + assert_eq!(b.binary_search_by(|x| x.cmp(&6)), Ok(3)); + assert_eq!(b.binary_search_by(|x| x.cmp(&5)), Err(3)); + assert_eq!(b.binary_search_by(|x| x.cmp(&8)), Ok(5)); + + let b: IndexSet = [1, 2, 4, 5, 6, 8, 9].into(); + assert_eq!(b.binary_search_by(|x| x.cmp(&7)), Err(5)); + assert_eq!(b.binary_search_by(|x| x.cmp(&0)), Err(0)); + + let b: IndexSet = [1, 3, 3, 3, 7].into(); + assert_eq!(b.binary_search_by(|x| x.cmp(&0)), Err(0)); + assert_eq!(b.binary_search_by(|x| x.cmp(&1)), Ok(0)); + assert_eq!(b.binary_search_by(|x| x.cmp(&2)), Err(1)); + // diff from std as set the duplicates keys + assert!(match b.binary_search_by(|x| x.cmp(&3)) { + Ok(1..=2) => true, + _ => false, + }); + assert!(match b.binary_search_by(|x| x.cmp(&3)) { + Ok(1..=2) => true, + _ => false, + }); + assert_eq!(b.binary_search_by(|x| x.cmp(&4)), Err(2)); + assert_eq!(b.binary_search_by(|x| x.cmp(&5)), Err(2)); + assert_eq!(b.binary_search_by(|x| x.cmp(&6)), Err(2)); + assert_eq!(b.binary_search_by(|x| x.cmp(&7)), Ok(2)); + assert_eq!(b.binary_search_by(|x| x.cmp(&8)), Err(3)); +} + +#[test] +fn test_binary_search_by_key() { + // addaped from stds test for binary_search + let b: IndexSet = [].into(); + assert_eq!(b.binary_search_by_key(&5, |&x| x), Err(0)); + + let b: IndexSet = [4].into(); + assert_eq!(b.binary_search_by_key(&3, |&x| x), Err(0)); + assert_eq!(b.binary_search_by_key(&4, |&x| x), Ok(0)); + assert_eq!(b.binary_search_by_key(&5, |&x| x), Err(1)); + + let b: IndexSet = [1, 2, 4, 6, 8, 9].into(); + assert_eq!(b.binary_search_by_key(&5, |&x| x), Err(3)); + assert_eq!(b.binary_search_by_key(&6, |&x| x), Ok(3)); + assert_eq!(b.binary_search_by_key(&7, |&x| x), Err(4)); + assert_eq!(b.binary_search_by_key(&8, |&x| x), Ok(4)); + + let b: IndexSet = [1, 2, 4, 5, 6, 8].into(); + assert_eq!(b.binary_search_by_key(&9, |&x| x), Err(6)); + + let b: IndexSet = [1, 2, 4, 6, 7, 8, 9].into(); + assert_eq!(b.binary_search_by_key(&6, |&x| x), Ok(3)); + assert_eq!(b.binary_search_by_key(&5, |&x| x), Err(3)); + assert_eq!(b.binary_search_by_key(&8, |&x| x), Ok(5)); + + let b: IndexSet = [1, 2, 4, 5, 6, 8, 9].into(); + assert_eq!(b.binary_search_by_key(&7, |&x| x), Err(5)); + assert_eq!(b.binary_search_by_key(&0, |&x| x), Err(0)); + + let b: IndexSet = [1, 3, 3, 3, 7].into(); + assert_eq!(b.binary_search_by_key(&0, |&x| x), Err(0)); + assert_eq!(b.binary_search_by_key(&1, |&x| x), Ok(0)); + assert_eq!(b.binary_search_by_key(&2, |&x| x), Err(1)); + // diff from std as set the duplicates keys + assert!(match b.binary_search_by_key(&3, |&x| x) { + Ok(1..=2) => true, + _ => false, + }); + assert!(match b.binary_search_by_key(&3, |&x| x) { + Ok(1..=2) => true, + _ => false, + }); + assert_eq!(b.binary_search_by_key(&4, |&x| x), Err(2)); + assert_eq!(b.binary_search_by_key(&5, |&x| x), Err(2)); + assert_eq!(b.binary_search_by_key(&6, |&x| x), Err(2)); + assert_eq!(b.binary_search_by_key(&7, |&x| x), Ok(2)); + assert_eq!(b.binary_search_by_key(&8, |&x| x), Err(3)); +} + +#[test] +fn test_partition_point() { + // addaped from stds test for partition_point + let b: IndexSet = [].into(); + assert_eq!(b.partition_point(|&x| x < 5), 0); + + let b: IndexSet<_> = [4].into(); + assert_eq!(b.partition_point(|&x| x < 3), 0); + assert_eq!(b.partition_point(|&x| x < 4), 0); + assert_eq!(b.partition_point(|&x| x < 5), 1); + + let b: IndexSet<_> = [1, 2, 4, 6, 8, 9].into(); + assert_eq!(b.partition_point(|&x| x < 5), 3); + assert_eq!(b.partition_point(|&x| x < 6), 3); + assert_eq!(b.partition_point(|&x| x < 7), 4); + assert_eq!(b.partition_point(|&x| x < 8), 4); + + let b: IndexSet<_> = [1, 2, 4, 5, 6, 8].into(); + assert_eq!(b.partition_point(|&x| x < 9), 6); + + let b: IndexSet<_> = [1, 2, 4, 6, 7, 8, 9].into(); + assert_eq!(b.partition_point(|&x| x < 6), 3); + assert_eq!(b.partition_point(|&x| x < 5), 3); + assert_eq!(b.partition_point(|&x| x < 8), 5); + + let b: IndexSet<_> = [1, 2, 4, 5, 6, 8, 9].into(); + assert_eq!(b.partition_point(|&x| x < 7), 5); + assert_eq!(b.partition_point(|&x| x < 0), 0); + + let b: IndexSet<_> = [1, 3, 3, 3, 7].into(); + assert_eq!(b.partition_point(|&x| x < 0), 0); + assert_eq!(b.partition_point(|&x| x < 1), 0); + assert_eq!(b.partition_point(|&x| x < 2), 1); + assert_eq!(b.partition_point(|&x| x < 3), 1); + assert_eq!(b.partition_point(|&x| x < 4), 2); // diff from std as set the duplicates keys + assert_eq!(b.partition_point(|&x| x < 5), 2); + assert_eq!(b.partition_point(|&x| x < 6), 2); + assert_eq!(b.partition_point(|&x| x < 7), 2); + assert_eq!(b.partition_point(|&x| x < 8), 3); +} From 3263c0f03fca5fa42902118adbdfb4854db17633 Mon Sep 17 00:00:00 2001 From: Jacob Finkelman Date: Thu, 26 Oct 2023 19:50:29 +0000 Subject: [PATCH 3/6] fix wording --- src/map.rs | 23 +++++++++++++---------- src/map/slice.rs | 33 +++++++++++++++++++-------------- src/map/tests.rs | 6 +++--- src/set.rs | 23 +++++++++++++---------- src/set/slice.rs | 31 ++++++++++++++++++------------- src/set/tests.rs | 12 ++++++------ 6 files changed, 72 insertions(+), 56 deletions(-) diff --git a/src/map.rs b/src/map.rs index af724940..4f130aea 100644 --- a/src/map.rs +++ b/src/map.rs @@ -796,9 +796,10 @@ where /// Search over a sorted map with a comparator function. /// - /// Returns the position where that value is present, or the position where can be inserted to maintain the sort. - /// see [slice::binary_search_by] for more details. - /// **O(log(n))** + /// Returns the position where that value is present, or the position where it can be inserted to maintain the sort. + /// see [`slice::binary_search_by`] for more details. + /// + /// Computes in **O(log(n))** time. #[inline] pub fn binary_search_by<'a, F>(&'a self, f: F) -> Result where @@ -807,11 +808,12 @@ where self.as_slice().binary_search_by(f) } - /// Search over a sorted map with a key extraction function. + /// Search over a sorted map with an extraction function. + /// + /// Returns the position where that value is present, or the position where it can be inserted to maintain the sort. + /// see [`slice::binary_search_by_key`] for more details. /// - /// Returns the position where that value is present, or the position where can be inserted to maintain the sort. - /// see [slice::binary_search_by_key] for more details. - /// **O(log(n))** + /// Computes in **O(log(n))** time. #[inline] pub fn binary_search_by_key<'a, B, F>(&'a self, b: &B, f: F) -> Result where @@ -821,11 +823,12 @@ where self.as_slice().binary_search_by_key(b, f) } - /// Returns the index of the partition point or a sorted map according to the given predicate + /// Returns the index of the partition point of a sorted map according to the given predicate /// (the index of the first element of the second partition). /// - /// see [slice::partition_point] for more details. - /// **O(log(n))** + /// see [`slice::partition_point`] for more details. + /// + /// Computes in **O(log(n))** time. #[must_use] pub fn partition_point

(&self, pred: P) -> usize where diff --git a/src/map/slice.rs b/src/map/slice.rs index ec3c914b..0ee46389 100644 --- a/src/map/slice.rs +++ b/src/map/slice.rs @@ -202,11 +202,13 @@ impl Slice { IntoValues::new(self.into_entries()) } - /// Search over a sorted map for a value. + /// Search over a sorted map for a key. /// - /// Returns the position where that value is present, or the position where can be inserted to maintain the sort. - /// see [slice::binary_search] for more details. - /// **O(log(n))**, which is notably less scalable than looking the value up in the map set is a slice from. + /// Returns the position where that value is present, or the position where it can be inserted to maintain the sort. + /// see [`slice::binary_search`] for more details. + /// + /// Computes in **O(log(n))** time, + /// which is notably less scalable than looking the value up in the map this is a slice from. pub fn binary_search_keys(&self, x: &K) -> Result where K: Ord, @@ -216,9 +218,10 @@ impl Slice { /// Search over a sorted map with a comparator function. /// - /// Returns the position where that value is present, or the position where can be inserted to maintain the sort. - /// see [slice::binary_search_by] for more details. - /// **O(log(n))** + /// Returns the position where that value is present, or the position where it can be inserted to maintain the sort. + /// see [`slice::binary_search_by`] for more details. + /// + /// Computes in **O(log(n))** time. #[inline] pub fn binary_search_by<'a, F>(&'a self, mut f: F) -> Result where @@ -227,11 +230,12 @@ impl Slice { self.entries.binary_search_by(move |a| f(&a.key, &a.value)) } - /// Search over a sorted map with a key extraction function. + /// Search over a sorted map with an extraction function. /// - /// Returns the position where that value is present, or the position where can be inserted to maintain the sort. - /// see [slice::binary_search_by_key] for more details. - /// **O(log(n))** + /// Returns the position where that value is present, or the position where it can be inserted to maintain the sort. + /// see [`slice::binary_search_by_key`] for more details. + /// + /// Computes in **O(log(n))** time. #[inline] pub fn binary_search_by_key<'a, B, F>(&'a self, b: &B, mut f: F) -> Result where @@ -241,11 +245,12 @@ impl Slice { self.binary_search_by(|k, v| f(k, v).cmp(b)) } - /// Returns the index of the partition point or a sorted map according to the given predicate + /// Returns the index of the partition point of a sorted map according to the given predicate /// (the index of the first element of the second partition). /// - /// see [slice::partition_point] for more details. - /// **O(log(n))** + /// see [`slice::partition_point`] for more details. + /// + /// Computes in **O(log(n))** time. #[must_use] pub fn partition_point

(&self, mut pred: P) -> usize where diff --git a/src/map/tests.rs b/src/map/tests.rs index 3f213212..3d2315d3 100644 --- a/src/map/tests.rs +++ b/src/map/tests.rs @@ -450,7 +450,7 @@ fn iter_default() { #[test] fn test_binary_search_by() { - // addaped from stds test for binary_search + // adapted from std's test for binary_search let b: IndexMap<_, i32> = [] .into_iter() .enumerate() @@ -526,7 +526,7 @@ fn test_binary_search_by() { #[test] fn test_binary_search_by_key() { - // addaped from stds test for binary_search + // adapted from std's test for binary_search let b: IndexMap<_, i32> = [] .into_iter() .enumerate() @@ -602,7 +602,7 @@ fn test_binary_search_by_key() { #[test] fn test_partition_point() { - // addaped from stds test for partition_point + // adapted from std's test for partition_point let b: IndexMap<_, i32> = [] .into_iter() .enumerate() diff --git a/src/set.rs b/src/set.rs index bfb0e528..08dfb043 100644 --- a/src/set.rs +++ b/src/set.rs @@ -690,9 +690,10 @@ where /// Search over a sorted set with a comparator function. /// - /// Returns the position where that value is present, or the position where can be inserted to maintain the sort. - /// see [slice::binary_search_by] for more details. - /// **O(log(n))** + /// Returns the position where that value is present, or the position where it can be inserted to maintain the sort. + /// see [`slice::binary_search_by`] for more details. + /// + /// Computes in **O(log(n))** time. #[inline] pub fn binary_search_by<'a, F>(&'a self, f: F) -> Result where @@ -701,11 +702,12 @@ where self.as_slice().binary_search_by(f) } - /// Search over a sorted set with a key extraction function. + /// Search over a sorted set with an extraction function. + /// + /// Returns the position where that value is present, or the position where it can be inserted to maintain the sort. + /// see [`slice::binary_search_by_key`] for more details. /// - /// Returns the position where that value is present, or the position where can be inserted to maintain the sort. - /// see [slice::binary_search_by_key] for more details. - /// **O(log(n))** + /// Computes in **O(log(n))** time. #[inline] pub fn binary_search_by_key<'a, B, F>(&'a self, b: &B, f: F) -> Result where @@ -715,11 +717,12 @@ where self.as_slice().binary_search_by_key(b, f) } - /// Returns the index of the partition point or a sorted set according to the given predicate + /// Returns the index of the partition point of a sorted set according to the given predicate /// (the index of the first element of the second partition). /// - /// see [slice::partition_point] for more details. - /// **O(log(n))** + /// see [`slice::partition_point`] for more details. + /// + /// Computes in **O(log(n))** time. #[must_use] pub fn partition_point

(&self, pred: P) -> usize where diff --git a/src/set/slice.rs b/src/set/slice.rs index 9f2a2d53..15a320c1 100644 --- a/src/set/slice.rs +++ b/src/set/slice.rs @@ -112,9 +112,11 @@ impl Slice { /// Search over a sorted set for a value. /// - /// Returns the position where that value is present, or the position where can be inserted to maintain the sort. - /// see [slice::binary_search] for more details. - /// **O(log(n))**, which is notably less scalable than looking the value up in the set this is a slice from. + /// Returns the position where that value is present, or the position where it can be inserted to maintain the sort. + /// see [`slice::binary_search`] for more details. + /// + /// Computes in **O(log(n))** time, + /// which is notably less scalable than looking the value up in the set this is a slice from. pub fn binary_search(&self, x: &T) -> Result where T: Ord, @@ -124,9 +126,10 @@ impl Slice { /// Search over a sorted set with a comparator function. /// - /// Returns the position where that value is present, or the position where can be inserted to maintain the sort. - /// see [slice::binary_search_by] for more details. - /// **O(log(n))** + /// Returns the position where that value is present, or the position where it can be inserted to maintain the sort. + /// see [`slice::binary_search_by`] for more details. + /// + /// Computes in **O(log(n))** time. #[inline] pub fn binary_search_by<'a, F>(&'a self, mut f: F) -> Result where @@ -135,11 +138,12 @@ impl Slice { self.entries.binary_search_by(move |a| f(&a.key)) } - /// Search over a sorted set with a key extraction function. + /// Search over a sorted set with an extraction function. /// - /// Returns the position where that value is present, or the position where can be inserted to maintain the sort. - /// see [slice::binary_search_by_key] for more details. - /// **O(log(n))** + /// Returns the position where that value is present, or the position where it can be inserted to maintain the sort. + /// see [`slice::binary_search_by_key`] for more details. + /// + /// Computes in **O(log(n))** time. #[inline] pub fn binary_search_by_key<'a, B, F>(&'a self, b: &B, mut f: F) -> Result where @@ -149,11 +153,12 @@ impl Slice { self.binary_search_by(|k| f(k).cmp(b)) } - /// Returns the index of the partition point or a sorted set according to the given predicate + /// Returns the index of the partition point of a sorted set according to the given predicate /// (the index of the first element of the second partition). /// - /// see [slice::partition_point] for more details. - /// **O(log(n))** + /// see [`slice::partition_point`] for more details. + /// + /// Computes in **O(log(n))** time. #[must_use] pub fn partition_point

(&self, mut pred: P) -> usize where diff --git a/src/set/tests.rs b/src/set/tests.rs index 19509ee3..d7bb9de6 100644 --- a/src/set/tests.rs +++ b/src/set/tests.rs @@ -546,7 +546,7 @@ fn iter_default() { #[test] fn test_binary_search_by() { - // addaped from stds test for binary_search + // adapted from std's test for binary_search let b: IndexSet = [].into(); assert_eq!(b.binary_search_by(|x| x.cmp(&5)), Err(0)); @@ -577,7 +577,7 @@ fn test_binary_search_by() { assert_eq!(b.binary_search_by(|x| x.cmp(&0)), Err(0)); assert_eq!(b.binary_search_by(|x| x.cmp(&1)), Ok(0)); assert_eq!(b.binary_search_by(|x| x.cmp(&2)), Err(1)); - // diff from std as set the duplicates keys + // diff from std as set merges the duplicate keys assert!(match b.binary_search_by(|x| x.cmp(&3)) { Ok(1..=2) => true, _ => false, @@ -595,7 +595,7 @@ fn test_binary_search_by() { #[test] fn test_binary_search_by_key() { - // addaped from stds test for binary_search + // adapted from std's test for binary_search let b: IndexSet = [].into(); assert_eq!(b.binary_search_by_key(&5, |&x| x), Err(0)); @@ -626,7 +626,7 @@ fn test_binary_search_by_key() { assert_eq!(b.binary_search_by_key(&0, |&x| x), Err(0)); assert_eq!(b.binary_search_by_key(&1, |&x| x), Ok(0)); assert_eq!(b.binary_search_by_key(&2, |&x| x), Err(1)); - // diff from std as set the duplicates keys + // diff from std as set merges the duplicate keys assert!(match b.binary_search_by_key(&3, |&x| x) { Ok(1..=2) => true, _ => false, @@ -644,7 +644,7 @@ fn test_binary_search_by_key() { #[test] fn test_partition_point() { - // addaped from stds test for partition_point + // adapted from std's test for partition_point let b: IndexSet = [].into(); assert_eq!(b.partition_point(|&x| x < 5), 0); @@ -676,7 +676,7 @@ fn test_partition_point() { assert_eq!(b.partition_point(|&x| x < 1), 0); assert_eq!(b.partition_point(|&x| x < 2), 1); assert_eq!(b.partition_point(|&x| x < 3), 1); - assert_eq!(b.partition_point(|&x| x < 4), 2); // diff from std as set the duplicates keys + assert_eq!(b.partition_point(|&x| x < 4), 2); // diff from std as set merges the duplicate keys assert_eq!(b.partition_point(|&x| x < 5), 2); assert_eq!(b.partition_point(|&x| x < 6), 2); assert_eq!(b.partition_point(|&x| x < 7), 2); From 1350db6fd48ca4ad6d9843681c6f7d710885f6fb Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Tue, 31 Oct 2023 12:17:45 -0700 Subject: [PATCH 4/6] Capitalize "see" in binary search docs --- src/map.rs | 6 +++--- src/map/slice.rs | 8 ++++---- src/set.rs | 6 +++--- src/set/slice.rs | 8 ++++---- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/map.rs b/src/map.rs index 4f130aea..2a204ebf 100644 --- a/src/map.rs +++ b/src/map.rs @@ -797,7 +797,7 @@ where /// Search over a sorted map with a comparator function. /// /// Returns the position where that value is present, or the position where it can be inserted to maintain the sort. - /// see [`slice::binary_search_by`] for more details. + /// See [`slice::binary_search_by`] for more details. /// /// Computes in **O(log(n))** time. #[inline] @@ -811,7 +811,7 @@ where /// Search over a sorted map with an extraction function. /// /// Returns the position where that value is present, or the position where it can be inserted to maintain the sort. - /// see [`slice::binary_search_by_key`] for more details. + /// See [`slice::binary_search_by_key`] for more details. /// /// Computes in **O(log(n))** time. #[inline] @@ -826,7 +826,7 @@ where /// Returns the index of the partition point of a sorted map according to the given predicate /// (the index of the first element of the second partition). /// - /// see [`slice::partition_point`] for more details. + /// See [`slice::partition_point`] for more details. /// /// Computes in **O(log(n))** time. #[must_use] diff --git a/src/map/slice.rs b/src/map/slice.rs index 0ee46389..3b999bf0 100644 --- a/src/map/slice.rs +++ b/src/map/slice.rs @@ -205,7 +205,7 @@ impl Slice { /// Search over a sorted map for a key. /// /// Returns the position where that value is present, or the position where it can be inserted to maintain the sort. - /// see [`slice::binary_search`] for more details. + /// See [`slice::binary_search`] for more details. /// /// Computes in **O(log(n))** time, /// which is notably less scalable than looking the value up in the map this is a slice from. @@ -219,7 +219,7 @@ impl Slice { /// Search over a sorted map with a comparator function. /// /// Returns the position where that value is present, or the position where it can be inserted to maintain the sort. - /// see [`slice::binary_search_by`] for more details. + /// See [`slice::binary_search_by`] for more details. /// /// Computes in **O(log(n))** time. #[inline] @@ -233,7 +233,7 @@ impl Slice { /// Search over a sorted map with an extraction function. /// /// Returns the position where that value is present, or the position where it can be inserted to maintain the sort. - /// see [`slice::binary_search_by_key`] for more details. + /// See [`slice::binary_search_by_key`] for more details. /// /// Computes in **O(log(n))** time. #[inline] @@ -248,7 +248,7 @@ impl Slice { /// Returns the index of the partition point of a sorted map according to the given predicate /// (the index of the first element of the second partition). /// - /// see [`slice::partition_point`] for more details. + /// See [`slice::partition_point`] for more details. /// /// Computes in **O(log(n))** time. #[must_use] diff --git a/src/set.rs b/src/set.rs index 08dfb043..bbeef6e1 100644 --- a/src/set.rs +++ b/src/set.rs @@ -691,7 +691,7 @@ where /// Search over a sorted set with a comparator function. /// /// Returns the position where that value is present, or the position where it can be inserted to maintain the sort. - /// see [`slice::binary_search_by`] for more details. + /// See [`slice::binary_search_by`] for more details. /// /// Computes in **O(log(n))** time. #[inline] @@ -705,7 +705,7 @@ where /// Search over a sorted set with an extraction function. /// /// Returns the position where that value is present, or the position where it can be inserted to maintain the sort. - /// see [`slice::binary_search_by_key`] for more details. + /// See [`slice::binary_search_by_key`] for more details. /// /// Computes in **O(log(n))** time. #[inline] @@ -720,7 +720,7 @@ where /// Returns the index of the partition point of a sorted set according to the given predicate /// (the index of the first element of the second partition). /// - /// see [`slice::partition_point`] for more details. + /// See [`slice::partition_point`] for more details. /// /// Computes in **O(log(n))** time. #[must_use] diff --git a/src/set/slice.rs b/src/set/slice.rs index 15a320c1..bc39e4af 100644 --- a/src/set/slice.rs +++ b/src/set/slice.rs @@ -113,7 +113,7 @@ impl Slice { /// Search over a sorted set for a value. /// /// Returns the position where that value is present, or the position where it can be inserted to maintain the sort. - /// see [`slice::binary_search`] for more details. + /// See [`slice::binary_search`] for more details. /// /// Computes in **O(log(n))** time, /// which is notably less scalable than looking the value up in the set this is a slice from. @@ -127,7 +127,7 @@ impl Slice { /// Search over a sorted set with a comparator function. /// /// Returns the position where that value is present, or the position where it can be inserted to maintain the sort. - /// see [`slice::binary_search_by`] for more details. + /// See [`slice::binary_search_by`] for more details. /// /// Computes in **O(log(n))** time. #[inline] @@ -141,7 +141,7 @@ impl Slice { /// Search over a sorted set with an extraction function. /// /// Returns the position where that value is present, or the position where it can be inserted to maintain the sort. - /// see [`slice::binary_search_by_key`] for more details. + /// See [`slice::binary_search_by_key`] for more details. /// /// Computes in **O(log(n))** time. #[inline] @@ -156,7 +156,7 @@ impl Slice { /// Returns the index of the partition point of a sorted set according to the given predicate /// (the index of the first element of the second partition). /// - /// see [`slice::partition_point`] for more details. + /// See [`slice::partition_point`] for more details. /// /// Computes in **O(log(n))** time. #[must_use] From eec4c437ea435bfae4e507fe713cde225a63598f Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Tue, 31 Oct 2023 12:44:25 -0700 Subject: [PATCH 5/6] Add plain binary search to maps and sets too While `IndexMap::binary_search_keys` and `IndexSet::binary_search` don't scale as well as their corresponding `get_index_of`, O(log n) vs. O(1), they do still offer the benefit of returning an `Err` insertion point. --- src/map.rs | 15 +++++++++++++++ src/map/slice.rs | 5 +++-- src/set.rs | 15 +++++++++++++++ src/set/slice.rs | 3 ++- 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/map.rs b/src/map.rs index 2a204ebf..bd7cd241 100644 --- a/src/map.rs +++ b/src/map.rs @@ -794,6 +794,21 @@ where }); } + /// Search over a sorted map for a key. + /// + /// Returns the position where that key is present, or the position where it can be inserted to maintain the sort. + /// See [`slice::binary_search`] for more details. + /// + /// Computes in **O(log(n))** time, + /// which is notably less scalable than looking the key up using [`get_index_of`], + /// but this can also position missing keys. + pub fn binary_search_keys(&self, x: &K) -> Result + where + K: Ord, + { + self.as_slice().binary_search_keys(x) + } + /// Search over a sorted map with a comparator function. /// /// Returns the position where that value is present, or the position where it can be inserted to maintain the sort. diff --git a/src/map/slice.rs b/src/map/slice.rs index 3b999bf0..89674469 100644 --- a/src/map/slice.rs +++ b/src/map/slice.rs @@ -204,11 +204,12 @@ impl Slice { /// Search over a sorted map for a key. /// - /// Returns the position where that value is present, or the position where it can be inserted to maintain the sort. + /// Returns the position where that key is present, or the position where it can be inserted to maintain the sort. /// See [`slice::binary_search`] for more details. /// /// Computes in **O(log(n))** time, - /// which is notably less scalable than looking the value up in the map this is a slice from. + /// which is notably less scalable than looking the key up in the map this is a slice from + /// using [`IndexMap::get_index_of`], but this can also position missing keys. pub fn binary_search_keys(&self, x: &K) -> Result where K: Ord, diff --git a/src/set.rs b/src/set.rs index bbeef6e1..a7de3e29 100644 --- a/src/set.rs +++ b/src/set.rs @@ -688,6 +688,21 @@ where }); } + /// Search over a sorted set for a value. + /// + /// Returns the position where that value is present, or the position where it can be inserted to maintain the sort. + /// See [`slice::binary_search`] for more details. + /// + /// Computes in **O(log(n))** time, + /// which is notably less scalable than looking the value up using [`get_index_of`], + /// but this can also position missing values. + pub fn binary_search(&self, x: &T) -> Result + where + T: Ord, + { + self.as_slice().binary_search(x) + } + /// Search over a sorted set with a comparator function. /// /// Returns the position where that value is present, or the position where it can be inserted to maintain the sort. diff --git a/src/set/slice.rs b/src/set/slice.rs index bc39e4af..8da4578c 100644 --- a/src/set/slice.rs +++ b/src/set/slice.rs @@ -116,7 +116,8 @@ impl Slice { /// See [`slice::binary_search`] for more details. /// /// Computes in **O(log(n))** time, - /// which is notably less scalable than looking the value up in the set this is a slice from. + /// which is notably less scalable than looking the value up in the set this is a slice from + /// using [`IndexSet::get_index_of`], but this can also position missing values. pub fn binary_search(&self, x: &T) -> Result where T: Ord, From 7326a6e1ac696b479c3d2595c91a8393c1bf32e2 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Tue, 31 Oct 2023 12:59:49 -0700 Subject: [PATCH 6/6] Re-wrap the binary search docs --- src/map.rs | 17 ++++++++--------- src/map/slice.rs | 18 +++++++++--------- src/set.rs | 17 ++++++++--------- src/set/slice.rs | 18 +++++++++--------- 4 files changed, 34 insertions(+), 36 deletions(-) diff --git a/src/map.rs b/src/map.rs index bd7cd241..4ee24d5f 100644 --- a/src/map.rs +++ b/src/map.rs @@ -796,12 +796,11 @@ where /// Search over a sorted map for a key. /// - /// Returns the position where that key is present, or the position where it can be inserted to maintain the sort. - /// See [`slice::binary_search`] for more details. + /// Returns the position where that key is present, or the position where it can be inserted to + /// maintain the sort. See [`slice::binary_search`] for more details. /// - /// Computes in **O(log(n))** time, - /// which is notably less scalable than looking the key up using [`get_index_of`], - /// but this can also position missing keys. + /// Computes in **O(log(n))** time, which is notably less scalable than looking the key up + /// using [`get_index_of`][IndexMap::get_index_of], but this can also position missing keys. pub fn binary_search_keys(&self, x: &K) -> Result where K: Ord, @@ -811,8 +810,8 @@ where /// Search over a sorted map with a comparator function. /// - /// Returns the position where that value is present, or the position where it can be inserted to maintain the sort. - /// See [`slice::binary_search_by`] for more details. + /// Returns the position where that value is present, or the position where it can be inserted + /// to maintain the sort. See [`slice::binary_search_by`] for more details. /// /// Computes in **O(log(n))** time. #[inline] @@ -825,8 +824,8 @@ where /// Search over a sorted map with an extraction function. /// - /// Returns the position where that value is present, or the position where it can be inserted to maintain the sort. - /// See [`slice::binary_search_by_key`] for more details. + /// Returns the position where that value is present, or the position where it can be inserted + /// to maintain the sort. See [`slice::binary_search_by_key`] for more details. /// /// Computes in **O(log(n))** time. #[inline] diff --git a/src/map/slice.rs b/src/map/slice.rs index 89674469..e2e30961 100644 --- a/src/map/slice.rs +++ b/src/map/slice.rs @@ -204,12 +204,12 @@ impl Slice { /// Search over a sorted map for a key. /// - /// Returns the position where that key is present, or the position where it can be inserted to maintain the sort. - /// See [`slice::binary_search`] for more details. + /// Returns the position where that key is present, or the position where it can be inserted to + /// maintain the sort. See [`slice::binary_search`] for more details. /// - /// Computes in **O(log(n))** time, - /// which is notably less scalable than looking the key up in the map this is a slice from - /// using [`IndexMap::get_index_of`], but this can also position missing keys. + /// Computes in **O(log(n))** time, which is notably less scalable than looking the key up in + /// the map this is a slice from using [`IndexMap::get_index_of`], but this can also position + /// missing keys. pub fn binary_search_keys(&self, x: &K) -> Result where K: Ord, @@ -219,8 +219,8 @@ impl Slice { /// Search over a sorted map with a comparator function. /// - /// Returns the position where that value is present, or the position where it can be inserted to maintain the sort. - /// See [`slice::binary_search_by`] for more details. + /// Returns the position where that value is present, or the position where it can be inserted + /// to maintain the sort. See [`slice::binary_search_by`] for more details. /// /// Computes in **O(log(n))** time. #[inline] @@ -233,8 +233,8 @@ impl Slice { /// Search over a sorted map with an extraction function. /// - /// Returns the position where that value is present, or the position where it can be inserted to maintain the sort. - /// See [`slice::binary_search_by_key`] for more details. + /// Returns the position where that value is present, or the position where it can be inserted + /// to maintain the sort. See [`slice::binary_search_by_key`] for more details. /// /// Computes in **O(log(n))** time. #[inline] diff --git a/src/set.rs b/src/set.rs index a7de3e29..c047b0be 100644 --- a/src/set.rs +++ b/src/set.rs @@ -690,12 +690,11 @@ where /// Search over a sorted set for a value. /// - /// Returns the position where that value is present, or the position where it can be inserted to maintain the sort. - /// See [`slice::binary_search`] for more details. + /// Returns the position where that value is present, or the position where it can be inserted + /// to maintain the sort. See [`slice::binary_search`] for more details. /// - /// Computes in **O(log(n))** time, - /// which is notably less scalable than looking the value up using [`get_index_of`], - /// but this can also position missing values. + /// Computes in **O(log(n))** time, which is notably less scalable than looking the value up + /// using [`get_index_of`][IndexSet::get_index_of], but this can also position missing values. pub fn binary_search(&self, x: &T) -> Result where T: Ord, @@ -705,8 +704,8 @@ where /// Search over a sorted set with a comparator function. /// - /// Returns the position where that value is present, or the position where it can be inserted to maintain the sort. - /// See [`slice::binary_search_by`] for more details. + /// Returns the position where that value is present, or the position where it can be inserted + /// to maintain the sort. See [`slice::binary_search_by`] for more details. /// /// Computes in **O(log(n))** time. #[inline] @@ -719,8 +718,8 @@ where /// Search over a sorted set with an extraction function. /// - /// Returns the position where that value is present, or the position where it can be inserted to maintain the sort. - /// See [`slice::binary_search_by_key`] for more details. + /// Returns the position where that value is present, or the position where it can be inserted + /// to maintain the sort. See [`slice::binary_search_by_key`] for more details. /// /// Computes in **O(log(n))** time. #[inline] diff --git a/src/set/slice.rs b/src/set/slice.rs index 8da4578c..305157a6 100644 --- a/src/set/slice.rs +++ b/src/set/slice.rs @@ -112,12 +112,12 @@ impl Slice { /// Search over a sorted set for a value. /// - /// Returns the position where that value is present, or the position where it can be inserted to maintain the sort. - /// See [`slice::binary_search`] for more details. + /// Returns the position where that value is present, or the position where it can be inserted + /// to maintain the sort. See [`slice::binary_search`] for more details. /// - /// Computes in **O(log(n))** time, - /// which is notably less scalable than looking the value up in the set this is a slice from - /// using [`IndexSet::get_index_of`], but this can also position missing values. + /// Computes in **O(log(n))** time, which is notably less scalable than looking the value up in + /// the set this is a slice from using [`IndexSet::get_index_of`], but this can also position + /// missing values. pub fn binary_search(&self, x: &T) -> Result where T: Ord, @@ -127,8 +127,8 @@ impl Slice { /// Search over a sorted set with a comparator function. /// - /// Returns the position where that value is present, or the position where it can be inserted to maintain the sort. - /// See [`slice::binary_search_by`] for more details. + /// Returns the position where that value is present, or the position where it can be inserted + /// to maintain the sort. See [`slice::binary_search_by`] for more details. /// /// Computes in **O(log(n))** time. #[inline] @@ -141,8 +141,8 @@ impl Slice { /// Search over a sorted set with an extraction function. /// - /// Returns the position where that value is present, or the position where it can be inserted to maintain the sort. - /// See [`slice::binary_search_by_key`] for more details. + /// Returns the position where that value is present, or the position where it can be inserted + /// to maintain the sort. See [`slice::binary_search_by_key`] for more details. /// /// Computes in **O(log(n))** time. #[inline]