Skip to content

Commit

Permalink
simplify and add tests for is_disjoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Eh2406 authored and konstin committed Mar 12, 2024
1 parent 5003723 commit 260ccb0
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ impl<V: Ord> Range<V> {
assert!(end_before_start_with_gap(&p[0].1, &p[1].0));
}
for (s, e) in self.segments.iter() {
assert!(valid_segment(s, e));
assert!(valid_segment(&s, &e));
}
}
self
Expand Down Expand Up @@ -544,7 +544,7 @@ impl<V: Ord + Clone> Range<V> {
// But, we already know that the segments in our input are valid.
// So we do not need to check if the `start` from the input `end` came from is smaller then `end`.
// If the `other_start` is larger than end, then the intersection will be invalid.
if !valid_segment(other_start, end) {
if !valid_segment(&other_start, &end) {
// Note: We can call `this_iter.next_if(!valid_segment(other_start, this_end))` in a loop.
// But the checks make it slower for the benchmarked inputs.
continue;
Expand All @@ -570,7 +570,7 @@ impl<V: Ord + Clone> Range<V> {
Self { segments: output }.check_invariants()
}

/// Return true is there can be no `V` so that `V` is contained in both `self` and `other`.
/// Return true if there can be no `V` so that `V` is contained in both `self` and `other`.
///
/// Note that we don't know that set of all existing `V`s here, so we only check if the segments
/// are disjoint, not if no version is contained in both.
Expand All @@ -579,10 +579,10 @@ impl<V: Ord + Clone> Range<V> {
let mut left_iter = self.segments.iter().peekable();
let mut right_iter = other.segments.iter().peekable();

while let (Some(left), Some(right)) = (left_iter.peek(), right_iter.peek()) {
if end_before_start(left.end_bound(), right.start_bound()) {
while let Some((left, right)) = left_iter.peek().zip(right_iter.peek()) {
if !valid_segment(&right.start_bound(), &left.end_bound()) {
left_iter.next();
} else if end_before_start(right.end_bound(), left.start_bound()) {
} else if !valid_segment(&left.start_bound(), &right.end_bound()) {
right_iter.next();
} else {
return false;
Expand Down Expand Up @@ -985,6 +985,12 @@ pub mod tests {
assert_eq!(r1.union(&r2).contains(&version), r1.contains(&version) || r2.contains(&version));
}

#[test]
fn is_disjoint_through_intersection(r1 in strategy(), r2 in strategy()) {
let disjoint_def = r1.intersection(&r2) == Range::empty();
assert_eq!(r1.is_disjoint(&r2), disjoint_def);
}

#[test]
fn union_through_intersection(r1 in strategy(), r2 in strategy()) {
let union_def = r1
Expand Down

0 comments on commit 260ccb0

Please sign in to comment.