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
use crate::ops::*;

impl<T> BitEmpty for Option<T> {
    fn empty() -> Self {
        None
    }
}

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

impl<T> BitTest for Option<T>
where
    T: BitTest,
{
    fn test(&self, idx: usize) -> bool {
        match self {
            None => false,
            Some(bits) => bits.test(idx),
        }
    }
}

impl<T> BitTestNone for Option<T>
where
    T: BitTestNone,
{
    fn test_none(&self) -> bool {
        match self {
            None => true,
            Some(bits) => bits.test_none(),
        }
    }
}

impl<T> BitTestAll for Option<T>
where
    T: BitTestAll,
{
    fn test_all(&self) -> bool {
        match self {
            None => false,
            Some(bits) => bits.test_all(),
        }
    }
}

impl<T> BitSetLimit for Option<T>
where
    T: BitSetLimit,
{
    const MAX_SET_INDEX: usize = T::MAX_SET_INDEX;
}

impl<T> BitSet for Option<T>
where
    T: BitSet + BitEmpty,
{
    unsafe fn set_unchecked(&mut self, idx: usize) {
        self.get_or_insert_with(T::empty).set_unchecked(idx)
    }
}

impl<T> BitUnsetLimit for Option<T>
where
    T: BitUnsetLimit,
{
    const MAX_UNSET_INDEX: usize = T::MAX_UNSET_INDEX;
}

impl<T> BitUnset for Option<T>
where
    T: BitUnset + BitTestNone,
{
    unsafe fn unset_unchecked(&mut self, idx: usize) {
        if let Some(bits) = self {
            bits.unset_unchecked(idx);
            if bits.test_none() {
                *self = None;
            }
        }
    }
}

impl<T> BitSearch for Option<T>
where
    T: BitSearch,
{
    fn find_first_set(&self, lower_bound: usize) -> Option<usize> {
        match self {
            None => None,
            Some(bits) => bits.find_first_set(lower_bound),
        }
    }
}

impl<T> BitComplement for Option<T>
where
    T: BitComplement,
    T::Output: BitFull,
{
    type Output = Option<T::Output>;

    fn complement(self) -> Self::Output {
        match self {
            None => Some(BitFull::full()),
            Some(bits) => Some(bits.complement()),
        }
    }
}

impl<T, U> BitUnion<U> for Option<T>
where
    T: BitUnion<U>,
    U: Into<T::Output>,
{
    type Output = T::Output;

    fn union(self, rhs: U) -> T::Output {
        match self {
            None => rhs.into(),
            Some(lhs) => lhs.union(rhs),
        }
    }
}

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

    fn intersection(self, rhs: U) -> Option<T::Output> {
        match self {
            None => None,
            Some(lhs) => Some(lhs.intersection(rhs)),
        }
    }
}

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

    fn difference(self, rhs: U) -> Option<T::Output> {
        match self {
            None => None,
            Some(lhs) => Some(lhs.difference(rhs)),
        }
    }
}

impl<T, U> BitSubset<U> for Option<T>
where
    T: BitSubset<U>,
{
    fn is_subset_of(&self, rhs: &U) -> bool {
        match self {
            None => true,
            Some(lhs) => lhs.is_subset_of(rhs),
        }
    }
}

impl<T, U> BitDisjoint<U> for Option<T>
where
    T: BitDisjoint<U>,
{
    fn is_disjoint(&self, rhs: &U) -> bool {
        match self {
            None => true,
            Some(lhs) => lhs.is_disjoint(rhs),
        }
    }
}