Unnamed repository; edit this file 'description' to name the repository.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//! Provides [Range] type expanding on [RangeBounds].

use std::ops::{self, RangeBounds};

/// A range of `char`s within the text.
#[derive(Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq)]
pub struct Range<T = usize> {
    pub start: T,
    pub end: T,
}

impl<T: PartialOrd> Range<T> {
    pub fn contains(&self, other: Self) -> bool {
        self.start <= other.start && other.end <= self.end
    }
    pub fn is_empty(&self) -> bool {
        self.end <= self.start
    }
}

impl<T> RangeBounds<T> for Range<T> {
    fn start_bound(&self) -> ops::Bound<&T> {
        ops::Bound::Included(&self.start)
    }

    fn end_bound(&self) -> ops::Bound<&T> {
        ops::Bound::Excluded(&self.end)
    }
}

/// Returns true if all ranges yielded by `sub_set` are contained by
/// `super_set`. This is essentially an optimized implementation of
/// `sub_set.all(|rb| super_set.any(|ra| ra.contains(rb)))` that runs in O(m+n)
/// instead of O(mn) (and in many cases faster).
///
/// Both iterators must uphold a the following invariants:
/// * ranges must not overlap (but they can be adjacent)
/// * ranges must be sorted
pub fn is_subset<const ALLOW_EMPTY: bool>(
    mut super_set: impl Iterator<Item = Range>,
    mut sub_set: impl Iterator<Item = Range>,
) -> bool {
    let (mut super_range, mut sub_range) = (super_set.next(), sub_set.next());
    loop {
        match (super_range, sub_range) {
            // skip over irrelevant ranges
            (Some(ra), Some(rb))
                if ra.end <= rb.start && (ra.start != rb.start || !ALLOW_EMPTY) =>
            {
                super_range = super_set.next();
            }
            (Some(ra), Some(rb)) => {
                if ra.contains(rb) {
                    sub_range = sub_set.next();
                } else {
                    return false;
                }
            }
            (None, Some(_)) => {
                // exhausted `super_set`, we can't match the reminder of `sub_set`
                return false;
            }
            (_, None) => {
                // no elements from `sub_sut` left to match, `super_set` contains `sub_set`
                return true;
            }
        }
    }
}

/// Similar to is_subset but requires each element of `super_set` to be matched
pub fn is_exact_subset(
    mut super_set: impl Iterator<Item = Range>,
    mut sub_set: impl Iterator<Item = Range>,
) -> bool {
    let (mut super_range, mut sub_range) = (super_set.next(), sub_set.next());
    let mut super_range_matched = true;
    loop {
        match (super_range, sub_range) {
            // skip over irrelevant ranges
            (Some(ra), Some(rb)) if ra.end <= rb.start && ra.start < rb.start => {
                if !super_range_matched {
                    return false;
                }
                super_range_matched = false;
                super_range = super_set.next();
            }
            (Some(ra), Some(rb)) => {
                if ra.contains(rb) {
                    super_range_matched = true;
                    sub_range = sub_set.next();
                } else {
                    return false;
                }
            }
            (None, Some(_)) => {
                // exhausted `super_set`, we can't match the reminder of `sub_set`
                return false;
            }
            (_, None) => {
                // no elements from `sub_sut` left to match, `super_set` contains `sub_set`
                return super_set.next().is_none();
            }
        }
    }
}