diff options
| author | James Miller <james@aatch.net> | 2016-04-03 14:58:34 +1200 |
|---|---|---|
| committer | James Miller <james@aatch.net> | 2016-04-03 14:58:34 +1200 |
| commit | 605bc042646ef0dc0bd6e0420e6bd5a4715c93df (patch) | |
| tree | 46b52d148e0fad110de38a1622a9d643260c343b /src/librustc_data_structures | |
| parent | 63321ca19390535795780ce15991b6238fb67db4 (diff) | |
| download | rust-605bc042646ef0dc0bd6e0420e6bd5a4715c93df.tar.gz rust-605bc042646ef0dc0bd6e0420e6bd5a4715c93df.zip | |
Use a BitVector instead of Vec<bool> for recording cleanup blocks
Also adds a FromIterator impl for BitVector to allow construction of a BitVector from an iterator yeilding bools.
Diffstat (limited to 'src/librustc_data_structures')
| -rw-r--r-- | src/librustc_data_structures/bitvec.rs | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/src/librustc_data_structures/bitvec.rs b/src/librustc_data_structures/bitvec.rs index e45c6cfc6dc..092b406ae9e 100644 --- a/src/librustc_data_structures/bitvec.rs +++ b/src/librustc_data_structures/bitvec.rs @@ -8,6 +8,8 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +use std::iter::FromIterator; + /// A very simple BitVector type. #[derive(Clone)] pub struct BitVector { @@ -51,7 +53,9 @@ impl BitVector { pub fn grow(&mut self, num_bits: usize) { let num_words = u64s(num_bits); let extra_words = self.data.len() - num_words; - self.data.extend((0..extra_words).map(|_| 0)); + if extra_words > 0 { + self.data.extend((0..extra_words).map(|_| 0)); + } } /// Iterates over indexes of set bits in a sorted order @@ -94,6 +98,27 @@ impl<'a> Iterator for BitVectorIter<'a> { } } +impl FromIterator<bool> for BitVector { + fn from_iter<I>(iter: I) -> BitVector where I: IntoIterator<Item=bool> { + let iter = iter.into_iter(); + let (len, _) = iter.size_hint(); + // Make the minimum length for the bitvector 64 bits since that's + // the smallest non-zero size anyway. + let len = if len < 64 { 64 } else { len }; + let mut bv = BitVector::new(len); + for (idx, val) in iter.enumerate() { + if idx > len { + bv.grow(idx); + } + if val { + bv.insert(idx); + } + } + + bv + } +} + /// A "bit matrix" is basically a square matrix of booleans /// represented as one gigantic bitvector. In other words, it is as if /// you have N bitvectors, each of length N. Note that `elements` here is `N`/ |
