Skip to main content

bitsetium/
option.rs

1use crate::ops::*;
2
3impl<T> BitEmpty for Option<T> {
4    fn empty() -> Self {
5        None
6    }
7}
8
9impl<T> BitFull for Option<T>
10where
11    T: BitFull,
12{
13    fn full() -> Self {
14        Some(T::full())
15    }
16}
17
18impl<T> BitTest for Option<T>
19where
20    T: BitTest,
21{
22    fn test(&self, idx: usize) -> bool {
23        match self {
24            None => false,
25            Some(bits) => bits.test(idx),
26        }
27    }
28}
29
30impl<T> BitTestNone for Option<T>
31where
32    T: BitTestNone,
33{
34    fn test_none(&self) -> bool {
35        match self {
36            None => true,
37            Some(bits) => bits.test_none(),
38        }
39    }
40}
41
42impl<T> BitTestAll for Option<T>
43where
44    T: BitTestAll,
45{
46    fn test_all(&self) -> bool {
47        match self {
48            None => false,
49            Some(bits) => bits.test_all(),
50        }
51    }
52}
53
54impl<T> BitSetLimit for Option<T>
55where
56    T: BitSetLimit,
57{
58    const MAX_SET_INDEX: usize = T::MAX_SET_INDEX;
59}
60
61impl<T> BitSet for Option<T>
62where
63    T: BitSet + BitEmpty,
64{
65    unsafe fn set_unchecked(&mut self, idx: usize) {
66        self.get_or_insert_with(T::empty).set_unchecked(idx)
67    }
68}
69
70impl<T> BitUnsetLimit for Option<T>
71where
72    T: BitUnsetLimit,
73{
74    const MAX_UNSET_INDEX: usize = T::MAX_UNSET_INDEX;
75}
76
77impl<T> BitUnset for Option<T>
78where
79    T: BitUnset + BitTestNone,
80{
81    unsafe fn unset_unchecked(&mut self, idx: usize) {
82        if let Some(bits) = self {
83            bits.unset_unchecked(idx);
84            if bits.test_none() {
85                *self = None;
86            }
87        }
88    }
89}
90
91impl<T> BitSearch for Option<T>
92where
93    T: BitSearch,
94{
95    fn find_first_set(&self, lower_bound: usize) -> Option<usize> {
96        match self {
97            None => None,
98            Some(bits) => bits.find_first_set(lower_bound),
99        }
100    }
101}
102
103impl<T> BitComplement for Option<T>
104where
105    T: BitComplement,
106    T::Output: BitFull,
107{
108    type Output = Option<T::Output>;
109
110    fn complement(self) -> Self::Output {
111        match self {
112            None => Some(BitFull::full()),
113            Some(bits) => Some(bits.complement()),
114        }
115    }
116}
117
118impl<T, U> BitUnion<U> for Option<T>
119where
120    T: BitUnion<U>,
121    U: Into<T::Output>,
122{
123    type Output = T::Output;
124
125    fn union(self, rhs: U) -> T::Output {
126        match self {
127            None => rhs.into(),
128            Some(lhs) => lhs.union(rhs),
129        }
130    }
131}
132
133impl<T, U> BitIntersection<U> for Option<T>
134where
135    T: BitIntersection<U>,
136{
137    type Output = Option<T::Output>;
138
139    fn intersection(self, rhs: U) -> Option<T::Output> {
140        match self {
141            None => None,
142            Some(lhs) => Some(lhs.intersection(rhs)),
143        }
144    }
145}
146
147impl<T, U> BitDifference<U> for Option<T>
148where
149    T: BitDifference<U>,
150{
151    type Output = Option<T::Output>;
152
153    fn difference(self, rhs: U) -> Option<T::Output> {
154        match self {
155            None => None,
156            Some(lhs) => Some(lhs.difference(rhs)),
157        }
158    }
159}
160
161impl<T, U> BitSubset<U> for Option<T>
162where
163    T: BitSubset<U>,
164{
165    fn is_subset_of(&self, rhs: &U) -> bool {
166        match self {
167            None => true,
168            Some(lhs) => lhs.is_subset_of(rhs),
169        }
170    }
171}
172
173impl<T, U> BitDisjoint<U> for Option<T>
174where
175    T: BitDisjoint<U>,
176{
177    fn is_disjoint(&self, rhs: &U) -> bool {
178        match self {
179            None => true,
180            Some(lhs) => lhs.is_disjoint(rhs),
181        }
182    }
183}