Skip to main content

bitsetium/
intersection.rs

1use {
2    crate::{ops::*, union::Union},
3    core::fmt::{self, Display},
4};
5
6/// Bit-set wrapper that acts like set complement.
7///
8/// Effectively inverses all bits in the underlying bit-set.
9#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
10pub struct Intersection<T, U>(pub T, pub U);
11
12impl<T, U> Intersection<T, U> {
13    /// Swap sets of the intersection.
14    pub fn swap_sets(self) -> Intersection<U, T> {
15        Intersection(self.1, self.0)
16    }
17}
18
19impl<T, U> Display for Intersection<T, U>
20where
21    T: Display,
22    U: Display,
23{
24    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25        write!(f, "Intersection({}, {})", self.0, self.1)
26    }
27}
28
29impl<T, U> BitEmpty for Intersection<T, U>
30where
31    T: BitEmpty,
32    U: Default,
33{
34    fn empty() -> Self {
35        Intersection(T::empty(), U::default())
36    }
37}
38
39impl<T, U> BitFull for Intersection<T, U>
40where
41    T: BitFull,
42    U: BitFull,
43{
44    fn full() -> Self {
45        Intersection(T::full(), U::full())
46    }
47}
48
49impl<T, U> BitTest for Intersection<T, U>
50where
51    T: BitTest,
52    U: BitTest,
53{
54    fn test(&self, idx: usize) -> bool {
55        self.0.test(idx) && self.1.test(idx)
56    }
57}
58
59impl<T, U> BitTestNone for Intersection<T, U>
60where
61    T: BitDisjoint<U>,
62{
63    fn test_none(&self) -> bool {
64        self.0.is_disjoint(&self.1)
65    }
66}
67
68impl<T, U> BitTestAll for Intersection<T, U>
69where
70    T: BitTestAll,
71    U: BitTestAll,
72{
73    fn test_all(&self) -> bool {
74        self.0.test_all() && self.1.test_all()
75    }
76}
77
78impl<T, U> BitSetLimit for Intersection<T, U>
79where
80    T: BitSetLimit,
81    U: BitSetLimit,
82{
83    const MAX_SET_INDEX: usize = crate::min(T::MAX_SET_INDEX, U::MAX_SET_INDEX);
84}
85
86impl<T, U> BitSet for Intersection<T, U>
87where
88    T: BitSet,
89    U: BitSet,
90{
91    unsafe fn set_unchecked(&mut self, idx: usize) {
92        self.0.set_unchecked(idx);
93        self.1.set_unchecked(idx);
94    }
95}
96
97impl<T, U> BitUnsetLimit for Intersection<T, U>
98where
99    T: BitUnsetLimit,
100    U: BitUnsetLimit,
101{
102    const MAX_UNSET_INDEX: usize = crate::max(T::MAX_UNSET_INDEX, U::MAX_UNSET_INDEX);
103}
104
105impl<T, U> BitUnset for Intersection<T, U>
106where
107    T: BitUnset,
108    U: BitUnset,
109{
110    unsafe fn unset_unchecked(&mut self, idx: usize) {
111        if idx <= T::MAX_UNSET_INDEX {
112            self.0.unset_unchecked(idx);
113        } else {
114            self.1.unset_unchecked(idx);
115        }
116    }
117}
118
119impl<T, U> BitSearch for Intersection<T, U>
120where
121    T: BitSearch,
122    U: BitSearch,
123{
124    fn find_first_set(&self, lower_bound: usize) -> Option<usize> {
125        let mut t = self.0.find_first_set(lower_bound)?;
126        let mut u = self.1.find_first_set(lower_bound)?;
127
128        loop {
129            if t == u {
130                return Some(t);
131            } else if t < u {
132                t = self.0.find_first_set(t + 1)?;
133            } else {
134                u = self.1.find_first_set(u + 1)?;
135            }
136        }
137    }
138}
139
140impl<T, U> BitComplement for Intersection<T, U>
141where
142    T: BitComplement,
143    U: BitComplement,
144{
145    type Output = Union<T::Output, U::Output>;
146
147    fn complement(self) -> Self::Output {
148        Union(self.0.complement(), self.1.complement())
149    }
150}
151
152impl<T, U, Y> BitUnion<Y> for Intersection<T, U>
153where
154    T: BitUnion<Y>,
155    U: BitUnion<Y>,
156    Y: Copy,
157{
158    type Output = Intersection<T::Output, U::Output>;
159
160    fn union(self, rhs: Y) -> Self::Output {
161        Intersection(self.0.union(rhs), self.1.union(rhs))
162    }
163}
164
165impl<T, U, Y> BitIntersection<Y> for Intersection<T, U>
166where
167    T: BitIntersection<Y>,
168{
169    type Output = Intersection<T::Output, U>;
170
171    fn intersection(self, rhs: Y) -> Self::Output {
172        Intersection(self.0.intersection(rhs), self.1)
173    }
174}
175
176impl<T, U, Y> BitDifference<Y> for Intersection<T, U>
177where
178    T: BitDifference<Y>,
179{
180    type Output = Intersection<T::Output, U>;
181
182    fn difference(self, rhs: Y) -> Self::Output {
183        Intersection(self.0.difference(rhs), self.1)
184    }
185}