Skip to content

Commit

Permalink
add ones constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
ekiwi committed Sep 16, 2024
1 parent b63f08a commit 8c6cb00
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "baa"
version = "0.7.2"
version = "0.7.3"
edition = "2021"
authors = ["Kevin Laeufer <[email protected]>"]
description = "BitVector and Array Arithmetic"
Expand Down
9 changes: 9 additions & 0 deletions src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,15 @@ pub trait BitVecMutOps: BitVecOps {
*w = 0;
});
}

/// sets all bits to one
fn assign_ones(&mut self) {
// set everything to one and then mask off the msb
self.words_mut().iter_mut().for_each(|w| {
*w = Word::MAX;
});
self.mask_msb();
}
}

pub const DENSE_ARRAY_MAX_INDEX_WIDTH: WidthInt = 48;
Expand Down
14 changes: 14 additions & 0 deletions src/value/owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ impl BitVecValue {
Self { width, words }
}

pub fn ones(width: WidthInt) -> Self {
let mut out = Self::zero(width);
out.assign_ones();
out
}

pub fn tru() -> Self {
Self::from_u64(1, 1)
}
Expand Down Expand Up @@ -304,4 +310,12 @@ mod tests {
"should mask the top bits"
);
}

#[test]
fn test_ones() {
let a = BitVecValue::ones(3);
assert_eq!(a.words.as_slice(), &[0b111]);
let b = BitVecValue::ones(3 + Word::BITS);
assert_eq!(b.words.as_slice(), &[Word::MAX, 0b111]);
}
}

0 comments on commit 8c6cb00

Please sign in to comment.