segmap/set/ops/
mod.rs

1//! Common Set operations for SegmentSet
2
3use crate::{map::Key, RangeBounds, SegmentMap, SegmentSet};
4
5pub mod difference;
6pub mod intersection;
7pub mod symmetric_difference;
8pub mod union;
9
10impl<T: Ord> SegmentSet<T> {
11    /// Check whether `self` and `other` are disjoint sets
12    ///
13    /// That is, the intersection between `self` and `other` is empty
14    pub fn is_disjoint(&self, other: &Self) -> bool {
15        self.iter_intersection(other).next().is_none()
16    }
17
18    /// Check whether `self` is a subset of `other`
19    ///
20    /// That is, all elements of `self` exist in `other`, or (as implemented)
21    /// `self.difference(other)` is empty
22    pub fn is_subset(&self, other: &Self) -> bool {
23        self.iter_difference(other).next().is_none()
24    }
25
26    pub fn is_superset(&self, other: &Self) -> bool {
27        other.is_subset(self)
28    }
29
30    // TODO: No Clone
31    // TODO: subset(&self, range: R) -> Self; slightly faster than ranges(..).filter().collect() because it doesn't need to check insertions
32    pub fn subset<R: RangeBounds<T>>(&self, range: R) -> SegmentSet<T>
33    where
34        T: Clone + Ord,
35    {
36        SegmentSet {
37            map: SegmentMap {
38                map: self
39                    .map
40                    .iter_subset(range)
41                    .map(|(r, _)| (Key(r), ()))
42                    .collect(),
43                store: alloc::vec::Vec::new(),
44            },
45        }
46    }
47
48    // as_complement / into_complement?
49    pub fn complement(&self) -> SegmentSet<&T>
50    where
51        T: Ord,
52    {
53        self.map.complement()
54    }
55}
56
57/// Set Complement
58impl<'a, T: Ord + Clone> core::ops::Not for &'a SegmentSet<T> {
59    type Output = SegmentSet<&'a T>;
60
61    // TODO: docs
62    fn not(self) -> Self::Output {
63        self.complement()
64    }
65}
66
67impl<T: Ord + Clone> core::ops::Not for SegmentSet<T> {
68    type Output = SegmentSet<T>;
69
70    fn not(self) -> Self::Output {
71        self.complement().cloned()
72    }
73}