1
Fork 0

Reorder bitvec.rs.

So that the `BitArray` code is all together and before the `BitVector`
code, instead of being awkwardly interleaved.
This commit is contained in:
Nicholas Nethercote 2018-09-13 13:27:56 +10:00
parent d6720cc810
commit 755fcae75e

View file

@ -23,46 +23,6 @@ pub struct BitArray<C: Idx> {
marker: PhantomData<C>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct BitVector<C: Idx> {
data: BitArray<C>,
}
impl<C: Idx> BitVector<C> {
pub fn grow(&mut self, num_bits: C) {
self.data.grow(num_bits)
}
pub fn new() -> BitVector<C> {
BitVector {
data: BitArray::new(0),
}
}
pub fn with_capacity(bits: usize) -> BitVector<C> {
BitVector {
data: BitArray::new(bits),
}
}
/// Returns true if the bit has changed.
#[inline]
pub fn insert(&mut self, bit: C) -> bool {
self.grow(bit);
self.data.insert(bit)
}
#[inline]
pub fn contains(&self, bit: C) -> bool {
let (word, mask) = word_mask(bit);
if let Some(word) = self.data.data.get(word) {
(word & mask) != 0
} else {
false
}
}
}
impl<C: Idx> BitArray<C> {
// Do not make this method public, instead switch your use case to BitVector.
#[inline]
@ -206,6 +166,43 @@ impl<'a, C: Idx> Iterator for BitIter<'a, C> {
}
}
/// A resizable BitVector type.
#[derive(Clone, Debug, PartialEq)]
pub struct BitVector<C: Idx> {
data: BitArray<C>,
}
impl<C: Idx> BitVector<C> {
pub fn grow(&mut self, num_bits: C) {
self.data.grow(num_bits)
}
pub fn new() -> BitVector<C> {
BitVector { data: BitArray::new(0) }
}
pub fn with_capacity(bits: usize) -> BitVector<C> {
BitVector { data: BitArray::new(bits) }
}
/// Returns true if the bit has changed.
#[inline]
pub fn insert(&mut self, bit: C) -> bool {
self.grow(bit);
self.data.insert(bit)
}
#[inline]
pub fn contains(&self, bit: C) -> bool {
let (word, mask) = word_mask(bit);
if let Some(word) = self.data.data.get(word) {
(word & mask) != 0
} else {
false
}
}
}
/// A "bit matrix" is basically a matrix of booleans represented as
/// one gigantic bitvector. In other words, it is as if you have
/// `rows` bitvectors, each of length `columns`.