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
//! Common Set operations for SegmentSet

use crate::{map::Key, RangeBounds, SegmentMap, SegmentSet};

pub mod difference;
pub mod intersection;
pub mod symmetric_difference;
pub mod union;

impl<T: Ord> SegmentSet<T> {
    /// Check whether `self` and `other` are disjoint sets
    ///
    /// That is, the intersection between `self` and `other` is empty
    pub fn is_disjoint(&self, other: &Self) -> bool {
        self.iter_intersection(other).next().is_none()
    }

    /// Check whether `self` is a subset of `other`
    ///
    /// That is, all elements of `self` exist in `other`, or (as implemented)
    /// `self.difference(other)` is empty
    pub fn is_subset(&self, other: &Self) -> bool {
        self.iter_difference(other).next().is_none()
    }

    pub fn is_superset(&self, other: &Self) -> bool {
        other.is_subset(self)
    }

    // TODO: No Clone
    // TODO: subset(&self, range: R) -> Self; slightly faster than ranges(..).filter().collect() because it doesn't need to check insertions
    pub fn subset<R: RangeBounds<T>>(&self, range: R) -> SegmentSet<T>
    where
        T: Clone + Ord,
    {
        SegmentSet {
            map: SegmentMap {
                map: self
                    .map
                    .iter_subset(range)
                    .map(|(r, _)| (Key(r), ()))
                    .collect(),
                store: alloc::vec::Vec::new(),
            },
        }
    }

    // as_complement / into_complement?
    pub fn complement(&self) -> SegmentSet<&T>
    where
        T: Ord,
    {
        self.map.complement()
    }
}

/// Set Complement
impl<'a, T: Ord + Clone> core::ops::Not for &'a SegmentSet<T> {
    type Output = SegmentSet<&'a T>;

    // TODO: docs
    fn not(self) -> Self::Output {
        self.complement()
    }
}

impl<T: Ord + Clone> core::ops::Not for SegmentSet<T> {
    type Output = SegmentSet<T>;

    fn not(self) -> Self::Output {
        self.complement().cloned()
    }
}