Skip to content

Commit

Permalink
Add plain binary search to maps and sets too
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cuviper committed Oct 31, 2023
1 parent 1350db6 commit eec4c43
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
15 changes: 15 additions & 0 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize, usize>
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.
Expand Down
5 changes: 3 additions & 2 deletions src/map/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,12 @@ impl<K, V> Slice<K, V> {

/// 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<usize, usize>
where
K: Ord,
Expand Down
15 changes: 15 additions & 0 deletions src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize, usize>
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.
Expand Down
3 changes: 2 additions & 1 deletion src/set/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ impl<T> Slice<T> {
/// 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<usize, usize>
where
T: Ord,
Expand Down

0 comments on commit eec4c43

Please sign in to comment.