Skip to main content

faultscope_core/
mask.rs

1#[derive(Clone, Debug, PartialEq, Eq)]
2pub struct Mask {
3    pub words: Vec<u64>,
4}
5
6impl Mask {
7    pub fn zero(words: usize) -> Self {
8        Self {
9            words: vec![0; words],
10        }
11    }
12
13    pub fn all(shots: usize) -> Self {
14        let words = word_count(shots);
15        let mut mask = Self {
16            words: vec![u64::MAX; words],
17        };
18        mask.clear_unused(shots);
19        mask
20    }
21
22    pub fn xor_assign(&mut self, other: &Mask) {
23        self.assert_same_width(other);
24        for (left, right) in self.words.iter_mut().zip(&other.words) {
25            *left ^= *right;
26        }
27    }
28
29    pub fn or_assign(&mut self, other: &Mask) {
30        self.assert_same_width(other);
31        for (left, right) in self.words.iter_mut().zip(&other.words) {
32            *left |= *right;
33        }
34    }
35
36    pub fn and_assign(&mut self, other: &Mask) {
37        self.assert_same_width(other);
38        for (left, right) in self.words.iter_mut().zip(&other.words) {
39            *left &= *right;
40        }
41    }
42
43    pub fn bit_count(&self) -> usize {
44        self.words
45            .iter()
46            .map(|word| word.count_ones() as usize)
47            .sum()
48    }
49
50    pub fn and_count(&self, other: &Mask) -> usize {
51        self.assert_same_width(other);
52        self.words
53            .iter()
54            .zip(&other.words)
55            .map(|(left, right)| (left & right).count_ones() as usize)
56            .sum()
57    }
58
59    pub fn is_zero(&self) -> bool {
60        self.words.iter().all(|word| *word == 0)
61    }
62
63    pub fn clear_unused(&mut self, shots: usize) {
64        let extra = shots % 64;
65        if extra != 0 {
66            let keep = (1u64 << extra) - 1;
67            if let Some(last) = self.words.last_mut() {
68                *last &= keep;
69            }
70        }
71    }
72
73    fn assert_same_width(&self, other: &Mask) {
74        assert_eq!(
75            self.words.len(),
76            other.words.len(),
77            "mask word counts must match"
78        );
79    }
80}
81
82pub fn word_count(shots: usize) -> usize {
83    shots.div_ceil(64)
84}
85
86#[cfg(test)]
87mod tests {
88    use super::*;
89
90    #[test]
91    fn all_clears_unused_bits() {
92        let mask = Mask::all(65);
93
94        assert_eq!(mask.words, vec![u64::MAX, 1]);
95    }
96
97    #[test]
98    fn counts_and_intersections() {
99        let left = Mask {
100            words: vec![0b1011],
101        };
102        let right = Mask {
103            words: vec![0b1100],
104        };
105
106        assert_eq!(left.bit_count(), 3);
107        assert_eq!(left.and_count(&right), 1);
108    }
109
110    #[test]
111    #[should_panic(expected = "mask word counts must match")]
112    fn xor_assign_rejects_mismatched_widths() {
113        Mask::zero(1).xor_assign(&Mask::zero(2));
114    }
115
116    #[test]
117    #[should_panic(expected = "mask word counts must match")]
118    fn or_assign_rejects_mismatched_widths() {
119        Mask::zero(1).or_assign(&Mask::zero(2));
120    }
121
122    #[test]
123    #[should_panic(expected = "mask word counts must match")]
124    fn and_assign_rejects_mismatched_widths() {
125        Mask::zero(1).and_assign(&Mask::zero(2));
126    }
127
128    #[test]
129    #[should_panic(expected = "mask word counts must match")]
130    fn and_count_rejects_mismatched_widths() {
131        let _ = Mask::zero(1).and_count(&Mask::zero(2));
132    }
133}