blob: c7f415ae460754581aa827da521031433ac2c58e (
plain)
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
|
use core::mem::transmute;
use contracts::requires;
/// A fixed-length vector of bits.
pub struct BitVec([usize]);
impl BitVec {
fn from_mut(words: &mut [usize]) -> &mut BitVec {
// SAFETY: The types have a newtype relationship.
unsafe { transmute(words) }
}
fn from_ref(words: &[usize]) -> &BitVec {
// SAFETY: The types have a newtype relationship.
unsafe { transmute(words) }
}
/// Retrieves the value of a bit from the BitVec.
#[requires(i < self.len())]
pub fn get(&self, i: usize) -> bool {
let word_index = i / usize::BITS as usize;
let subword_index = i % usize::BITS as usize;
let one_hot = 1 << subword_index;
(self.0[word_index] & one_hot) != 0
}
/// Returns whether the BitVec is empty.
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
/// Returns the number of bits in the BitVec.
pub fn len(&self) -> usize {
self.0.len() * usize::BITS as usize
}
/// Sets the value of a bit in the BitVec.
#[requires(i < self.len())]
pub fn set(&mut self, i: usize, value: bool) {
let word_index = i / usize::BITS as usize;
let subword_index = i % usize::BITS as usize;
let one_hot = 1 << subword_index;
if value {
self.0[word_index] |= one_hot;
} else {
self.0[word_index] &= !one_hot;
}
}
}
|