Module biodivine_lib_param_bn::biodivine_std::bitvector
source · [−]Expand description
Bitvectors are simply sequences of boolean values. However, since they are used quite often and in some very specific applications, we provide a unified optimized interface for them.
Note that there is a ton of rust libraries which implement bitvectors (and indeed, we are deferring to some of them for larger bit vectors). However, we wanted to avoid relying on a specific library for this in order to provide a more unified API.
AFAIK, BitVector
is not used in any truly performance critical code, but there
are some concerns for overall memory consumption and efficiency.
use biodivine_lib_param_bn::biodivine_std::bitvector::{BitVector58, BitVector};
// Create a BitVector of length 4 initialized to false.
let mut bv = BitVector58::empty(4);
assert_eq!(4, bv.len());
bv.flip(1); // Invert value at given index.
bv.set(2, true); // Set value at index to a constant.
assert!(bv.get(1));
assert!(!bv.get(3));
// Should print BV(4)[1 2] using default `Display` implementation.
println!("{}", bv);
BitVector
conversions
Mostly for utility purposes (as this is not very efficient), every BitVector
provides
conversion to and from Vec<bool>
(exact representation of the values in the BitVector
)
and Vec<usize>
(indices of true
item in the BitVector
).
// Convert bool vector to BitVector.
use biodivine_lib_param_bn::biodivine_std::bitvector::{BitVector58, BitVector};
let bv = BitVector58::from(vec![false, true, true, false]);
// And back to bool vector.
assert_eq!(vec![false, true, true, false], bv.values());
// Convert index vector to BitVector.
assert_eq!(bv, BitVector58::from_ones(4, vec![1,2]));
// And also back to an index vector.
assert_eq!(vec![1,2], bv.ones());
// Inverted index vector is also available.
assert_eq!(vec![0,3], bv.zeros());
We recommend that users of bit vectors wrap these conversion utilities in more appropriate
methods aimed at the specific use case (e.g. substituting usize
for domain specific
values).
Modules
Structs
A BitVector
implementation that uses the explicit implementation from the bitvector
crate.
A BitVector
implementation that uses u64
as the underlying representation and
can therefore hold only up-to 58 values (remaining 6 bits store the vector length).
Should be pretty fast though.
Traits
BitVector
is a collection of boolean values of a fixed length.