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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
use {
    crate::{ops::*, union::Union},
    core::fmt::{self, Display},
};

/// Bit-set wrapper that acts like set complement.
///
/// Effectively inverses all bits in the underlying bit-set.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Intersection<T, U>(pub T, pub U);

impl<T, U> Intersection<T, U> {
    /// Swap sets of the intersection.
    pub fn swap_sets(self) -> Intersection<U, T> {
        Intersection(self.1, self.0)
    }
}

impl<T, U> Display for Intersection<T, U>
where
    T: Display,
    U: Display,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Intersection({}, {})", self.0, self.1)
    }
}

impl<T, U> BitEmpty for Intersection<T, U>
where
    T: BitEmpty,
    U: Default,
{
    fn empty() -> Self {
        Intersection(T::empty(), U::default())
    }
}

impl<T, U> BitFull for Intersection<T, U>
where
    T: BitFull,
    U: BitFull,
{
    fn full() -> Self {
        Intersection(T::full(), U::full())
    }
}

impl<T, U> BitTest for Intersection<T, U>
where
    T: BitTest,
    U: BitTest,
{
    fn test(&self, idx: usize) -> bool {
        self.0.test(idx) && self.1.test(idx)
    }
}

impl<T, U> BitTestNone for Intersection<T, U>
where
    T: BitDisjoint<U>,
{
    fn test_none(&self) -> bool {
        self.0.is_disjoint(&self.1)
    }
}

impl<T, U> BitTestAll for Intersection<T, U>
where
    T: BitTestAll,
    U: BitTestAll,
{
    fn test_all(&self) -> bool {
        self.0.test_all() && self.1.test_all()
    }
}

impl<T, U> BitSetLimit for Intersection<T, U>
where
    T: BitSetLimit,
    U: BitSetLimit,
{
    const MAX_SET_INDEX: usize = crate::min(T::MAX_SET_INDEX, U::MAX_SET_INDEX);
}

impl<T, U> BitSet for Intersection<T, U>
where
    T: BitSet,
    U: BitSet,
{
    unsafe fn set_unchecked(&mut self, idx: usize) {
        self.0.set_unchecked(idx);
        self.1.set_unchecked(idx);
    }
}

impl<T, U> BitUnsetLimit for Intersection<T, U>
where
    T: BitUnsetLimit,
    U: BitUnsetLimit,
{
    const MAX_UNSET_INDEX: usize = crate::max(T::MAX_UNSET_INDEX, U::MAX_UNSET_INDEX);
}

impl<T, U> BitUnset for Intersection<T, U>
where
    T: BitUnset,
    U: BitUnset,
{
    unsafe fn unset_unchecked(&mut self, idx: usize) {
        if idx <= T::MAX_UNSET_INDEX {
            self.0.unset_unchecked(idx);
        } else {
            self.1.unset_unchecked(idx);
        }
    }
}

impl<T, U> BitSearch for Intersection<T, U>
where
    T: BitSearch,
    U: BitSearch,
{
    fn find_first_set(&self, lower_bound: usize) -> Option<usize> {
        let mut t = self.0.find_first_set(lower_bound)?;
        let mut u = self.1.find_first_set(lower_bound)?;

        loop {
            if t == u {
                return Some(t);
            } else if t < u {
                t = self.0.find_first_set(t + 1)?;
            } else {
                u = self.1.find_first_set(u + 1)?;
            }
        }
    }
}

impl<T, U> BitComplement for Intersection<T, U>
where
    T: BitComplement,
    U: BitComplement,
{
    type Output = Union<T::Output, U::Output>;

    fn complement(self) -> Self::Output {
        Union(self.0.complement(), self.1.complement())
    }
}

impl<T, U, Y> BitUnion<Y> for Intersection<T, U>
where
    T: BitUnion<Y>,
    U: BitUnion<Y>,
    Y: Copy,
{
    type Output = Intersection<T::Output, U::Output>;

    fn union(self, rhs: Y) -> Self::Output {
        Intersection(self.0.union(rhs), self.1.union(rhs))
    }
}

impl<T, U, Y> BitIntersection<Y> for Intersection<T, U>
where
    T: BitIntersection<Y>,
{
    type Output = Intersection<T::Output, U>;

    fn intersection(self, rhs: Y) -> Self::Output {
        Intersection(self.0.intersection(rhs), self.1)
    }
}

impl<T, U, Y> BitDifference<Y> for Intersection<T, U>
where
    T: BitDifference<Y>,
{
    type Output = Intersection<T::Output, U>;

    fn difference(self, rhs: Y) -> Self::Output {
        Intersection(self.0.difference(rhs), self.1)
    }
}