Skip to content

Commit

Permalink
Reorder bitvec.rs.
Browse files Browse the repository at this point in the history
So that the `BitArray` code is all together and before the `BitVector`
code, instead of being awkwardly interleaved.
  • Loading branch information
nnethercote committed Sep 13, 2018
1 parent d6720cc commit 755fcae
Showing 1 changed file with 37 additions and 40 deletions.
77 changes: 37 additions & 40 deletions src/librustc_data_structures/bitvec.rs
Expand Up @@ -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]
Expand Down Expand Up @@ -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`.
Expand Down

0 comments on commit 755fcae

Please sign in to comment.