Skip to main content

zu_vector/
bitmap.rs

1//! Validity and predicate bitmap (perf/02 section 1.3).
2//!
3//! One bit per row, u64 words, arena-backed. Null propagation for
4//! arithmetic is a word-wise AND, never a per-row branch. The invariant
5//! throughout: bits at and above `len` are zero, so popcounts and the
6//! bitmap-to-selection kernel never mask the tail.
7
8use crate::arena::{MorselArena, RawBuf};
9
10pub struct Bitmap {
11    words: RawBuf,
12    len: u32,
13}
14
15impl Bitmap {
16    /// Allocate a bitmap of `len` bits, all set or all clear.
17    pub fn new_in(arena: &mut MorselArena, len: usize, set: bool) -> Self {
18        let words = len.div_ceil(64);
19        let mut buf = arena.alloc_of::<u64>(words);
20        if set {
21            let w = buf.as_mut_slice::<u64>();
22            w.fill(!0u64);
23            let tail = len % 64;
24            if tail != 0 {
25                w[words - 1] = (1u64 << tail) - 1;
26            }
27        } else {
28            // Arena blocks are recycled dirty, so clear is explicit.
29            buf.as_mut_slice::<u64>().fill(0);
30        }
31        Self {
32            words: buf,
33            len: len as u32,
34        }
35    }
36
37    pub fn len(&self) -> usize {
38        self.len as usize
39    }
40
41    pub fn is_empty(&self) -> bool {
42        self.len == 0
43    }
44
45    #[inline]
46    pub fn get(&self, i: usize) -> bool {
47        debug_assert!(i < self.len as usize);
48        self.words.as_slice::<u64>()[i / 64] >> (i % 64) & 1 == 1
49    }
50
51    #[inline]
52    pub fn set(&mut self, i: usize) {
53        debug_assert!(i < self.len as usize);
54        self.words.as_mut_slice::<u64>()[i / 64] |= 1 << (i % 64);
55    }
56
57    #[inline]
58    pub fn clear(&mut self, i: usize) {
59        debug_assert!(i < self.len as usize);
60        self.words.as_mut_slice::<u64>()[i / 64] &= !(1 << (i % 64));
61    }
62
63    pub fn words(&self) -> &[u64] {
64        self.words.as_slice()
65    }
66
67    pub fn words_mut(&mut self) -> &mut [u64] {
68        self.words.as_mut_slice()
69    }
70
71    /// Zero the tail bits above `len`. Kernels that write whole words
72    /// call this once at the end instead of masking per word.
73    pub fn mask_tail(&mut self) {
74        let len = self.len as usize;
75        let tail = len % 64;
76        if tail != 0 {
77            self.words.as_mut_slice::<u64>()[len / 64] &= (1u64 << tail) - 1;
78        }
79    }
80
81    pub fn and_with(&mut self, other: &Bitmap) {
82        debug_assert_eq!(self.len, other.len);
83        for (w, o) in self
84            .words
85            .as_mut_slice::<u64>()
86            .iter_mut()
87            .zip(other.words())
88        {
89            *w &= o;
90        }
91    }
92
93    pub fn or_with(&mut self, other: &Bitmap) {
94        debug_assert_eq!(self.len, other.len);
95        for (w, o) in self
96            .words
97            .as_mut_slice::<u64>()
98            .iter_mut()
99            .zip(other.words())
100        {
101            *w |= o;
102        }
103    }
104
105    pub fn count_ones(&self) -> usize {
106        self.words
107            .as_slice::<u64>()
108            .iter()
109            .map(|w| w.count_ones() as usize)
110            .sum()
111    }
112}
113
114#[cfg(test)]
115mod tests {
116    use super::*;
117
118    #[test]
119    fn tail_stays_masked() {
120        let mut arena = MorselArena::new();
121        let all = Bitmap::new_in(&mut arena, 70, true);
122        assert_eq!(all.count_ones(), 70);
123        let mut none = Bitmap::new_in(&mut arena, 70, false);
124        none.set(69);
125        assert_eq!(none.count_ones(), 1);
126        assert!(none.get(69));
127        none.clear(69);
128        assert_eq!(none.count_ones(), 0);
129    }
130
131    #[test]
132    fn word_ops() {
133        let mut arena = MorselArena::new();
134        let mut a = Bitmap::new_in(&mut arena, 130, false);
135        let mut b = Bitmap::new_in(&mut arena, 130, false);
136        a.set(0);
137        a.set(129);
138        b.set(129);
139        a.and_with(&b);
140        assert_eq!(a.count_ones(), 1);
141        assert!(a.get(129));
142        a.or_with(&b);
143        assert_eq!(a.count_ones(), 1);
144    }
145}