Skip to content

Commit

Permalink
feat: update contains_many and simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
Eh2406 committed Mar 18, 2024
1 parent a1080b9 commit 498d5bb
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{
borrow::Borrow,
cmp::{max, min},
fmt::Display,
ops::Bound,
Expand Down Expand Up @@ -118,18 +119,19 @@ impl SemverPubgrub {
/// 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 Version> + Clone + 's,
I: Iterator<Item = BV> + Clone + 's,
BV: Borrow<Version> + 's,
{
let mut n_iter = self
.normal
.contains_many(versions.clone().filter(|v| v.pre.is_empty()));
.contains_many(versions.clone().filter(|v| v.borrow().pre.is_empty()));
let mut p_iter = self
.pre
.contains_many(versions.clone().filter(|v| !v.pre.is_empty()));
.contains_many(versions.clone().filter(|v| !v.borrow().pre.is_empty()));
versions.filter_map(move |v| {
if v.pre.is_empty() {
if v.borrow().pre.is_empty() {
n_iter.next()
} else {
p_iter.next()
Expand All @@ -147,15 +149,18 @@ impl SemverPubgrub {
/// - If none of the versions are contained in the original than the range will be simplified to `empty`.
///
/// If versions are not sorted the correctness of this function is not guaranteed.
pub fn simplify<'v, I>(&self, versions: I) -> Self
pub fn simplify<'v, I, BV>(&self, versions: I) -> Self
where
I: Iterator<Item = &'v Version> + Clone + 'v,
I: Iterator<Item = BV> + Clone + 'v,
BV: Borrow<Version> + 'v,
{
Self {
normal: self
.normal
.simplify(versions.clone().filter(|v| v.pre.is_empty())),
pre: self.pre.simplify(versions.filter(|v| !v.pre.is_empty())),
.simplify(versions.clone().filter(|v| v.borrow().pre.is_empty())),
pre: self
.pre
.simplify(versions.filter(|v| !v.borrow().pre.is_empty())),
}
}
}
Expand Down

0 comments on commit 498d5bb

Please sign in to comment.