layered_bitset/
option.rs

1use crate::{BitSet, BitSetMut};
2
3impl<T> BitSet for Option<T>
4where
5    T: BitSet,
6{
7    const UPPER_BOUND: u32 = T::UPPER_BOUND;
8
9    fn get(&self, index: u32) -> bool {
10        match self {
11            None => false,
12            Some(bits) => bits.get(index),
13        }
14    }
15
16    fn find_set(&self, lower_bound: u32) -> Option<u32> {
17        match self {
18            None => None,
19            Some(bits) => bits.find_set(lower_bound),
20        }
21    }
22}
23
24impl<T> BitSetMut for Option<T>
25where
26    T: BitSetMut + Default,
27{
28    fn set(&mut self, index: u32, bit: bool) {
29        match (&mut *self, bit) {
30            (None, true) => {
31                let mut bits = T::default();
32                bits.set(index, true);
33                *self = Some(bits);
34            }
35            (None, false) => {}
36            (Some(bits), false) => {
37                bits.set(index, false);
38                if bits.is_empty() {
39                    *self = None;
40                }
41            }
42            (Some(bits), true) => bits.set(index, true),
43        }
44    }
45}