1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use indexed_vec::{Idx, IndexVec};
14 use std::marker::PhantomData;
18 pub const WORD_BYTES: usize = ::std::mem::size_of::<Word>();
19 pub const WORD_BITS: usize = WORD_BYTES * 8;
21 /// A very simple BitArray type.
23 /// It does not support resizing after creation; use `BitVector` for that.
24 #[derive(Clone, Debug, Eq, PartialEq)]
25 pub struct BitArray<C: Idx> {
27 marker: PhantomData<C>,
30 impl<C: Idx> BitArray<C> {
31 // Do not make this method public, instead switch your use case to BitVector.
33 fn grow(&mut self, num_bits: C) {
34 let num_words = num_words(num_bits);
35 if self.data.len() <= num_words {
36 self.data.resize(num_words + 1, 0)
41 pub fn new(num_bits: usize) -> BitArray<C> {
42 BitArray::new_empty(num_bits)
46 pub fn new_empty(num_bits: usize) -> BitArray<C> {
47 let num_words = num_words(num_bits);
49 data: vec![0; num_words],
55 pub fn new_filled(num_bits: usize) -> BitArray<C> {
56 let num_words = num_words(num_bits);
57 let mut result = BitArray {
58 data: vec![!0; num_words],
61 result.clear_above(num_bits);
66 pub fn clear(&mut self) {
67 for p in &mut self.data {
72 /// Sets all elements up to `num_bits`.
73 pub fn set_up_to(&mut self, num_bits: usize) {
74 for p in &mut self.data {
77 self.clear_above(num_bits);
80 /// Clear all elements above `num_bits`.
81 fn clear_above(&mut self, num_bits: usize) {
82 let first_clear_block = num_bits / WORD_BITS;
84 if first_clear_block < self.data.len() {
85 // Within `first_clear_block`, the `num_bits % WORD_BITS` LSBs
87 let mask = (1 << (num_bits % WORD_BITS)) - 1;
88 self.data[first_clear_block] &= mask;
90 // All the blocks above `first_clear_block` are fully cleared.
91 for b in &mut self.data[first_clear_block + 1..] {
97 pub fn count(&self) -> usize {
98 self.data.iter().map(|e| e.count_ones() as usize).sum()
101 /// True if `self` contains the bit `bit`.
103 pub fn contains(&self, bit: C) -> bool {
104 let (word, mask) = word_mask(bit);
105 (self.data[word] & mask) != 0
108 /// True if `self` contains all the bits in `other`.
110 /// The two vectors must have the same length.
112 pub fn contains_all(&self, other: &BitArray<C>) -> bool {
113 assert_eq!(self.data.len(), other.data.len());
114 self.data.iter().zip(&other.data).all(|(a, b)| (a & b) == *b)
118 pub fn is_empty(&self) -> bool {
119 self.data.iter().all(|a| *a == 0)
122 /// Returns true if the bit has changed.
124 pub fn insert(&mut self, bit: C) -> bool {
125 let (word, mask) = word_mask(bit);
126 let data = &mut self.data[word];
128 let new_value = value | mask;
133 /// Sets all bits to true.
134 pub fn insert_all(&mut self) {
135 for data in &mut self.data {
140 /// Returns true if the bit has changed.
142 pub fn remove(&mut self, bit: C) -> bool {
143 let (word, mask) = word_mask(bit);
144 let data = &mut self.data[word];
146 let new_value = value & !mask;
152 pub fn merge(&mut self, all: &BitArray<C>) -> bool {
153 assert!(self.data.len() == all.data.len());
154 let mut changed = false;
155 for (i, j) in self.data.iter_mut().zip(&all.data) {
165 pub fn words(&self) -> &[Word] {
169 pub fn words_mut(&mut self) -> &mut [Word] {
173 /// Iterates over indexes of set bits in a sorted order
175 pub fn iter<'a>(&'a self) -> BitIter<'a, C> {
178 iter: self.data.iter().enumerate(),
184 impl<T: Idx> rustc_serialize::Encodable for BitArray<T> {
185 fn encode<E: rustc_serialize::Encoder>(&self, encoder: &mut E) -> Result<(), E::Error> {
186 self.data.encode(encoder)
190 impl<T: Idx> rustc_serialize::Decodable for BitArray<T> {
191 fn decode<D: rustc_serialize::Decoder>(d: &mut D) -> Result<BitArray<T>, D::Error> {
192 let words: Vec<Word> = rustc_serialize::Decodable::decode(d)?;
200 pub struct BitIter<'a, C: Idx> {
201 cur: Option<(Word, usize)>,
202 iter: iter::Enumerate<slice::Iter<'a, Word>>,
203 marker: PhantomData<C>
206 impl<'a, C: Idx> Iterator for BitIter<'a, C> {
208 fn next(&mut self) -> Option<C> {
210 if let Some((ref mut word, offset)) = self.cur {
211 let bit_pos = word.trailing_zeros() as usize;
212 if bit_pos != WORD_BITS {
213 let bit = 1 << bit_pos;
215 return Some(C::new(bit_pos + offset))
219 let (i, word) = self.iter.next()?;
220 self.cur = Some((*word, WORD_BITS * i));
225 pub trait BitwiseOperator {
226 /// Applies some bit-operation pointwise to each of the bits in the two inputs.
227 fn join(&self, pred1: Word, pred2: Word) -> Word;
231 pub fn bitwise<Op: BitwiseOperator>(out_vec: &mut [Word], in_vec: &[Word], op: &Op) -> bool
233 assert_eq!(out_vec.len(), in_vec.len());
234 let mut changed = false;
235 for (out_elem, in_elem) in out_vec.iter_mut().zip(in_vec.iter()) {
236 let old_val = *out_elem;
237 let new_val = op.join(old_val, *in_elem);
239 changed |= old_val != new_val;
244 pub struct Intersect;
245 impl BitwiseOperator for Intersect {
247 fn join(&self, a: Word, b: Word) -> Word { a & b }
251 impl BitwiseOperator for Union {
253 fn join(&self, a: Word, b: Word) -> Word { a | b }
257 impl BitwiseOperator for Subtract {
259 fn join(&self, a: Word, b: Word) -> Word { a & !b }
262 pub fn bits_to_string(words: &[Word], bits: usize) -> String {
263 let mut result = String::new();
266 // Note: this is a little endian printout of bytes.
268 // i tracks how many bits we have printed so far.
270 for &word in words.iter() {
272 for _ in 0..WORD_BYTES { // for each byte in `v`:
273 let remain = bits - i;
274 // If less than a byte remains, then mask just that many bits.
275 let mask = if remain <= 8 { (1 << remain) - 1 } else { 0xFF };
276 assert!(mask <= 0xFF);
279 result.push_str(&format!("{}{:02x}", sep, byte));
281 if remain <= 8 { break; }
293 /// A resizable BitVector type.
294 #[derive(Clone, Debug, PartialEq)]
295 pub struct BitVector<C: Idx> {
299 impl<C: Idx> BitVector<C> {
300 pub fn grow(&mut self, num_bits: C) {
301 self.data.grow(num_bits)
304 pub fn new() -> BitVector<C> {
305 BitVector { data: BitArray::new(0) }
308 pub fn with_capacity(bits: usize) -> BitVector<C> {
309 BitVector { data: BitArray::new(bits) }
312 /// Returns true if the bit has changed.
314 pub fn insert(&mut self, bit: C) -> bool {
316 self.data.insert(bit)
320 pub fn contains(&self, bit: C) -> bool {
321 let (word, mask) = word_mask(bit);
322 if let Some(word) = self.data.data.get(word) {
330 /// A "bit matrix" is basically a matrix of booleans represented as
331 /// one gigantic bitvector. In other words, it is as if you have
332 /// `rows` bitvectors, each of length `columns`.
333 #[derive(Clone, Debug)]
334 pub struct BitMatrix<R: Idx, C: Idx> {
337 phantom: PhantomData<(R, C)>,
340 impl<R: Idx, C: Idx> BitMatrix<R, C> {
341 /// Create a new `rows x columns` matrix, initially empty.
342 pub fn new(rows: usize, columns: usize) -> BitMatrix<R, C> {
343 // For every element, we need one bit for every other
344 // element. Round up to an even number of words.
345 let words_per_row = num_words(columns);
348 vector: vec![0; rows * words_per_row],
349 phantom: PhantomData,
353 /// The range of bits for a given row.
354 fn range(&self, row: R) -> (usize, usize) {
355 let row = row.index();
356 let words_per_row = num_words(self.columns);
357 let start = row * words_per_row;
358 (start, start + words_per_row)
361 /// Sets the cell at `(row, column)` to true. Put another way, add
362 /// `column` to the bitset for `row`.
364 /// Returns true if this changed the matrix, and false otherwise.
365 pub fn add(&mut self, row: R, column: R) -> bool {
366 let (start, _) = self.range(row);
367 let (word, mask) = word_mask(column);
368 let vector = &mut self.vector[..];
369 let v1 = vector[start + word];
371 vector[start + word] = v2;
375 /// Do the bits from `row` contain `column`? Put another way, is
376 /// the matrix cell at `(row, column)` true? Put yet another way,
377 /// if the matrix represents (transitive) reachability, can
378 /// `row` reach `column`?
379 pub fn contains(&self, row: R, column: R) -> bool {
380 let (start, _) = self.range(row);
381 let (word, mask) = word_mask(column);
382 (self.vector[start + word] & mask) != 0
385 /// Returns those indices that are true in rows `a` and `b`. This
386 /// is an O(n) operation where `n` is the number of elements
387 /// (somewhat independent from the actual size of the
388 /// intersection, in particular).
389 pub fn intersection(&self, a: R, b: R) -> Vec<C> {
390 let (a_start, a_end) = self.range(a);
391 let (b_start, b_end) = self.range(b);
392 let mut result = Vec::with_capacity(self.columns);
393 for (base, (i, j)) in (a_start..a_end).zip(b_start..b_end).enumerate() {
394 let mut v = self.vector[i] & self.vector[j];
395 for bit in 0..WORD_BITS {
400 result.push(C::new(base * WORD_BITS + bit));
408 /// Add the bits from row `read` to the bits from row `write`,
409 /// return true if anything changed.
411 /// This is used when computing transitive reachability because if
412 /// you have an edge `write -> read`, because in that case
413 /// `write` can reach everything that `read` can (and
414 /// potentially more).
415 pub fn merge(&mut self, read: R, write: R) -> bool {
416 let (read_start, read_end) = self.range(read);
417 let (write_start, write_end) = self.range(write);
418 let vector = &mut self.vector[..];
419 let mut changed = false;
420 for (read_index, write_index) in (read_start..read_end).zip(write_start..write_end) {
421 let v1 = vector[write_index];
422 let v2 = v1 | vector[read_index];
423 vector[write_index] = v2;
429 /// Iterates through all the columns set to true in a given row of
431 pub fn iter<'a>(&'a self, row: R) -> BitIter<'a, C> {
432 let (start, end) = self.range(row);
435 iter: self.vector[start..end].iter().enumerate(),
441 /// A moderately sparse bit matrix, in which rows are instantiated lazily.
443 /// Initially, every row has no explicit representation. If any bit within a
444 /// row is set, the entire row is instantiated as
445 /// `Some(<full-column-width-BitArray>)`. Furthermore, any previously
446 /// uninstantiated rows prior to it will be instantiated as `None`. Those prior
447 /// rows may themselves become fully instantiated later on if any of their bits
449 #[derive(Clone, Debug)]
450 pub struct SparseBitMatrix<R, C>
456 rows: IndexVec<R, Option<BitArray<C>>>,
459 impl<R: Idx, C: Idx> SparseBitMatrix<R, C> {
460 /// Create a new empty sparse bit matrix with no rows or columns.
461 pub fn new(num_columns: usize) -> Self {
464 rows: IndexVec::new(),
468 fn ensure_row(&mut self, row: R) -> &mut BitArray<C> {
469 // Instantiate any missing rows up to and including row `row` with an
471 self.rows.ensure_contains_elem(row, || None);
473 // Then replace row `row` with a full BitArray if necessary.
474 let num_columns = self.num_columns;
475 self.rows[row].get_or_insert_with(|| BitArray::new(num_columns))
478 /// Sets the cell at `(row, column)` to true. Put another way, insert
479 /// `column` to the bitset for `row`.
481 /// Returns true if this changed the matrix, and false otherwise.
482 pub fn add(&mut self, row: R, column: C) -> bool {
483 self.ensure_row(row).insert(column)
486 /// Do the bits from `row` contain `column`? Put another way, is
487 /// the matrix cell at `(row, column)` true? Put yet another way,
488 /// if the matrix represents (transitive) reachability, can
489 /// `row` reach `column`?
490 pub fn contains(&self, row: R, column: C) -> bool {
491 self.row(row).map_or(false, |r| r.contains(column))
494 /// Add the bits from row `read` to the bits from row `write`,
495 /// return true if anything changed.
497 /// This is used when computing transitive reachability because if
498 /// you have an edge `write -> read`, because in that case
499 /// `write` can reach everything that `read` can (and
500 /// potentially more).
501 pub fn merge(&mut self, read: R, write: R) -> bool {
502 if read == write || self.row(read).is_none() {
506 self.ensure_row(write);
507 if let (Some(bitvec_read), Some(bitvec_write)) = self.rows.pick2_mut(read, write) {
508 bitvec_write.merge(bitvec_read)
514 /// Merge a row, `from`, into the `into` row.
515 pub fn merge_into(&mut self, into: R, from: &BitArray<C>) -> bool {
516 self.ensure_row(into).merge(from)
519 /// Add all bits to the given row.
520 pub fn add_all(&mut self, row: R) {
521 self.ensure_row(row).insert_all();
524 pub fn rows(&self) -> impl Iterator<Item = R> {
528 /// Iterates through all the columns set to true in a given row of
530 pub fn iter<'a>(&'a self, row: R) -> impl Iterator<Item = C> + 'a {
531 self.row(row).into_iter().flat_map(|r| r.iter())
534 pub fn row(&self, row: R) -> Option<&BitArray<C>> {
535 if let Some(Some(row)) = self.rows.get(row) {
544 fn num_words<C: Idx>(elements: C) -> usize {
545 (elements.index() + WORD_BITS - 1) / WORD_BITS
549 fn word_mask<C: Idx>(index: C) -> (usize, Word) {
550 let index = index.index();
551 let word = index / WORD_BITS;
552 let mask = 1 << (index % WORD_BITS);
557 fn test_clear_above() {
561 let mut idx_buf: BitArray<usize> = BitArray::new_filled(128);
562 idx_buf.clear_above(i);
564 let elems: Vec<usize> = idx_buf.iter().collect();
565 let expected: Vec<usize> = (0..cmp::min(i, 128)).collect();
566 assert_eq!(elems, expected);
571 fn test_set_up_to() {
574 vec![BitArray::new_empty(128), BitArray::new_filled(128)]
577 idx_buf.set_up_to(i);
579 let elems: Vec<usize> = idx_buf.iter().collect();
580 let expected: Vec<usize> = (0..i).collect();
581 assert_eq!(elems, expected);
587 fn test_new_filled() {
589 let idx_buf = BitArray::new_filled(i);
590 let elems: Vec<usize> = idx_buf.iter().collect();
591 let expected: Vec<usize> = (0..i).collect();
592 assert_eq!(elems, expected);
597 fn bitvec_iter_works() {
598 let mut bitvec: BitArray<usize> = BitArray::new(100);
609 bitvec.iter().collect::<Vec<_>>(),
610 [1, 10, 19, 62, 63, 64, 65, 66, 99]
615 fn bitvec_iter_works_2() {
616 let mut bitvec: BitArray<usize> = BitArray::new(319);
622 assert_eq!(bitvec.iter().collect::<Vec<_>>(), [0, 127, 191, 255, 319]);
626 fn union_two_vecs() {
627 let mut vec1: BitArray<usize> = BitArray::new(65);
628 let mut vec2: BitArray<usize> = BitArray::new(65);
629 assert!(vec1.insert(3));
630 assert!(!vec1.insert(3));
631 assert!(vec2.insert(5));
632 assert!(vec2.insert(64));
633 assert!(vec1.merge(&vec2));
634 assert!(!vec1.merge(&vec2));
635 assert!(vec1.contains(3));
636 assert!(!vec1.contains(4));
637 assert!(vec1.contains(5));
638 assert!(!vec1.contains(63));
639 assert!(vec1.contains(64));
644 let mut vec1: BitVector<usize> = BitVector::with_capacity(65);
646 assert!(vec1.insert(index));
647 assert!(!vec1.insert(index));
651 // Check if the bits set before growing are still set
653 assert!(vec1.contains(index));
656 // Check if the new bits are all un-set
657 for index in 65..128 {
658 assert!(!vec1.contains(index));
661 // Check that we can set all new bits without running out of bounds
662 for index in 65..128 {
663 assert!(vec1.insert(index));
664 assert!(!vec1.insert(index));
669 fn matrix_intersection() {
670 let mut vec1: BitMatrix<usize, usize> = BitMatrix::new(200, 200);
672 // (*) Elements reachable from both 2 and 65.
676 vec1.add(2, 10); // (*)
677 vec1.add(2, 64); // (*)
680 vec1.add(2, 160); // (*)
686 vec1.add(65, 10); // (*)
687 vec1.add(65, 64); // (*)
690 vec1.add(65, 160); // (*)
692 let intersection = vec1.intersection(2, 64);
693 assert!(intersection.is_empty());
695 let intersection = vec1.intersection(2, 65);
696 assert_eq!(intersection, &[10, 64, 160]);
701 let mut matrix: BitMatrix<usize, usize> = BitMatrix::new(64, 100);
709 let mut iter = expected.iter();
710 for i in matrix.iter(2) {
711 let j = *iter.next().unwrap();
714 assert!(iter.next().is_none());
716 let expected = [22, 75];
717 let mut iter = expected.iter();
718 for i in matrix.iter(3) {
719 let j = *iter.next().unwrap();
722 assert!(iter.next().is_none());
725 let mut iter = expected.iter();
726 for i in matrix.iter(4) {
727 let j = *iter.next().unwrap();
730 assert!(iter.next().is_none());
732 let expected = [22, 75];
733 let mut iter = expected.iter();
734 for i in matrix.iter(5) {
735 let j = *iter.next().unwrap();
738 assert!(iter.next().is_none());
742 fn sparse_matrix_iter() {
743 let mut matrix: SparseBitMatrix<usize, usize> = SparseBitMatrix::new(100);
751 let mut iter = expected.iter();
752 for i in matrix.iter(2) {
753 let j = *iter.next().unwrap();
756 assert!(iter.next().is_none());
758 let expected = [22, 75];
759 let mut iter = expected.iter();
760 for i in matrix.iter(3) {
761 let j = *iter.next().unwrap();
764 assert!(iter.next().is_none());
767 let mut iter = expected.iter();
768 for i in matrix.iter(4) {
769 let j = *iter.next().unwrap();
772 assert!(iter.next().is_none());
774 let expected = [22, 75];
775 let mut iter = expected.iter();
776 for i in matrix.iter(5) {
777 let j = *iter.next().unwrap();
780 assert!(iter.next().is_none());