aboutsummaryrefslogtreecommitdiffstats
path: root/src/bitset.rs
diff options
context:
space:
mode:
authorJonas Maier <jonas@x77.dev>2026-06-05 15:03:44 +0200
committerJonas Maier <jonas@x77.dev>2026-06-05 15:03:44 +0200
commita464bec4d558533776329634504ab0c1bee84bba (patch)
treeae19c073bf41ad52d97f951a1ffe10429d341199 /src/bitset.rs
parent2184c1cab66771594051197451c5e0f4dd2567a2 (diff)
downloadpish-a464bec4d558533776329634504ab0c1bee84bba.tar.gz
some bytecode implementation, does not work yet
Diffstat (limited to 'src/bitset.rs')
-rw-r--r--src/bitset.rs43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/bitset.rs b/src/bitset.rs
new file mode 100644
index 0000000..eae2596
--- /dev/null
+++ b/src/bitset.rs
@@ -0,0 +1,43 @@
+#[derive(Clone, PartialEq, Eq)]
+pub struct BitSet {
+ data: Vec<u32>,
+ size: usize,
+}
+
+const BITS: usize = 32;
+
+impl BitSet {
+ pub fn new(size: usize) -> Self {
+ Self {
+ size,
+ data: vec![0; size.div_ceil(32)],
+ }
+ }
+
+ pub fn set(&mut self, bit: usize, val: bool) {
+ assert!(bit < self.size);
+ let idx = bit / BITS;
+ let bit = bit % BITS;
+
+ if val {
+ self.data[idx] |= 1 << bit;
+ } else {
+ self.data[idx] &= !(1 << bit);
+ }
+ }
+
+ pub fn get(&self, bit: usize) -> bool {
+ assert!(bit < self.size);
+ let idx = bit / BITS;
+ let bit = bit % BITS;
+
+ (self.data[idx] & (1 << bit)) != 0
+ }
+
+ pub fn set_all(&mut self, val: bool) {
+ let val = if val { !0 } else { 0 };
+ for d in self.data.iter_mut() {
+ *d = val;
+ }
+ }
+}