Skip to content

Commit

Permalink
feat: contains_many works with iter of V not just &V
Browse files Browse the repository at this point in the history
  • Loading branch information
Eh2406 committed Jan 12, 2024
1 parent 4a48371 commit 0de319e
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
//! If we do not see practical bugs, or we get a formal proof that the code cannot lead to error states, then we may remove this warning.
use crate::{internal::small_vec::SmallVec, version_set::VersionSet};
use std::borrow::Borrow;
use std::cmp::Ordering;
use std::ops::RangeBounds;
use std::{
Expand Down Expand Up @@ -217,14 +218,14 @@ impl<V: Ord> Range<V> {
/// The `versions` iterator must be sorted.
/// Functionally equivalent to `versions.map(|v| self.contains(v))`.
/// Except it runs in `O(size_of_range + len_of_versions)` not `O(size_of_range * len_of_versions)`
pub fn contains_many<'s, I>(&'s self, versions: I) -> impl Iterator<Item = bool> + 's
pub fn contains_many<'s, I, BV>(&'s self, versions: I) -> impl Iterator<Item = bool> + 's
where
I: Iterator<Item = &'s V> + 's,
V: 's,
I: Iterator<Item = BV> + 's,
BV: Borrow<V>,
{
versions.scan(0, move |i, v| {
while let Some(segment) = self.segments.get(*i) {
match within_bounds(v, segment) {
match within_bounds(v.borrow(), segment) {
Ordering::Less => return Some(false),
Ordering::Equal => return Some(true),
Ordering::Greater => *i += 1,
Expand Down Expand Up @@ -769,4 +770,22 @@ pub mod tests {
assert!(simp.segments.len() <= range.segments.len())
}
}

#[test]
fn contains_many_can_take_owned() {
let range: Range<u8> = Range::singleton(1);
let versions = vec![1, 2, 3];
// Check that iter can be a Cow
assert_eq!(
range.contains_many(versions.iter()).count(),
range
.contains_many(versions.iter().map(std::borrow::Cow::Borrowed))
.count()
);
// Check that iter can be a V
assert_eq!(
range.contains_many(versions.iter()).count(),
range.contains_many(versions.into_iter()).count()
);
}
}

0 comments on commit 0de319e

Please sign in to comment.