2015-04-08 17:53:56 -05:00
|
|
|
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2018-02-15 12:47:40 -06:00
|
|
|
use std::collections::BTreeMap;
|
|
|
|
use std::collections::btree_map::Entry;
|
|
|
|
use std::marker::PhantomData;
|
2016-04-02 21:58:34 -05:00
|
|
|
use std::iter::FromIterator;
|
2018-02-15 12:47:40 -06:00
|
|
|
use indexed_vec::{Idx, IndexVec};
|
2016-04-02 21:58:34 -05:00
|
|
|
|
2018-02-14 17:30:49 -06:00
|
|
|
type Word = u128;
|
|
|
|
const WORD_BITS: usize = 128;
|
|
|
|
|
2015-04-07 05:12:13 -05:00
|
|
|
/// A very simple BitVector type.
|
2016-06-01 12:23:56 -05:00
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
2015-04-07 05:12:13 -05:00
|
|
|
pub struct BitVector {
|
2018-02-14 17:30:49 -06:00
|
|
|
data: Vec<Word>,
|
2015-04-07 05:12:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BitVector {
|
2017-01-12 14:13:02 -06:00
|
|
|
#[inline]
|
2015-04-07 05:12:13 -05:00
|
|
|
pub fn new(num_bits: usize) -> BitVector {
|
2018-02-14 17:30:49 -06:00
|
|
|
let num_words = words(num_bits);
|
2018-02-22 12:53:54 -06:00
|
|
|
BitVector {
|
|
|
|
data: vec![0; num_words],
|
|
|
|
}
|
2015-04-07 05:12:13 -05:00
|
|
|
}
|
|
|
|
|
2017-01-12 14:13:02 -06:00
|
|
|
#[inline]
|
2016-08-05 18:42:41 -05:00
|
|
|
pub fn clear(&mut self) {
|
|
|
|
for p in &mut self.data {
|
|
|
|
*p = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-01 18:05:56 -06:00
|
|
|
pub fn count(&self) -> usize {
|
|
|
|
self.data.iter().map(|e| e.count_ones() as usize).sum()
|
|
|
|
}
|
|
|
|
|
2017-01-12 14:13:02 -06:00
|
|
|
#[inline]
|
2015-04-07 05:12:13 -05:00
|
|
|
pub fn contains(&self, bit: usize) -> bool {
|
2015-08-18 16:36:32 -05:00
|
|
|
let (word, mask) = word_mask(bit);
|
2015-04-07 05:12:13 -05:00
|
|
|
(self.data[word] & mask) != 0
|
|
|
|
}
|
|
|
|
|
2016-01-21 10:57:43 -06:00
|
|
|
/// Returns true if the bit has changed.
|
2017-01-12 14:13:02 -06:00
|
|
|
#[inline]
|
2015-04-07 05:12:13 -05:00
|
|
|
pub fn insert(&mut self, bit: usize) -> bool {
|
2015-08-18 16:36:32 -05:00
|
|
|
let (word, mask) = word_mask(bit);
|
2015-04-07 05:12:13 -05:00
|
|
|
let data = &mut self.data[word];
|
|
|
|
let value = *data;
|
2016-01-21 10:57:43 -06:00
|
|
|
let new_value = value | mask;
|
|
|
|
*data = new_value;
|
|
|
|
new_value != value
|
2015-04-07 05:12:13 -05:00
|
|
|
}
|
2015-08-18 16:36:32 -05:00
|
|
|
|
2018-01-25 23:14:54 -06:00
|
|
|
/// Returns true if the bit has changed.
|
|
|
|
#[inline]
|
|
|
|
pub fn remove(&mut self, bit: usize) -> bool {
|
|
|
|
let (word, mask) = word_mask(bit);
|
|
|
|
let data = &mut self.data[word];
|
|
|
|
let value = *data;
|
|
|
|
let new_value = value & !mask;
|
|
|
|
*data = new_value;
|
|
|
|
new_value != value
|
|
|
|
}
|
|
|
|
|
2017-01-12 14:13:02 -06:00
|
|
|
#[inline]
|
2015-08-18 16:36:32 -05:00
|
|
|
pub fn insert_all(&mut self, all: &BitVector) -> bool {
|
|
|
|
assert!(self.data.len() == all.data.len());
|
|
|
|
let mut changed = false;
|
|
|
|
for (i, j) in self.data.iter_mut().zip(&all.data) {
|
|
|
|
let value = *i;
|
|
|
|
*i = value | *j;
|
2016-03-05 07:40:33 -06:00
|
|
|
if value != *i {
|
|
|
|
changed = true;
|
|
|
|
}
|
2015-08-18 16:36:32 -05:00
|
|
|
}
|
|
|
|
changed
|
|
|
|
}
|
|
|
|
|
2017-01-12 14:13:02 -06:00
|
|
|
#[inline]
|
2015-08-18 16:36:32 -05:00
|
|
|
pub fn grow(&mut self, num_bits: usize) {
|
2018-02-14 17:30:49 -06:00
|
|
|
let num_words = words(num_bits);
|
2016-04-22 13:07:23 -05:00
|
|
|
if self.data.len() < num_words {
|
|
|
|
self.data.resize(num_words, 0)
|
2016-04-02 21:58:34 -05:00
|
|
|
}
|
2015-08-18 16:36:32 -05:00
|
|
|
}
|
2016-01-31 11:17:15 -06:00
|
|
|
|
|
|
|
/// Iterates over indexes of set bits in a sorted order
|
2017-01-12 14:13:02 -06:00
|
|
|
#[inline]
|
2016-01-31 11:17:15 -06:00
|
|
|
pub fn iter<'a>(&'a self) -> BitVectorIter<'a> {
|
|
|
|
BitVectorIter {
|
|
|
|
iter: self.data.iter(),
|
|
|
|
current: 0,
|
2016-03-05 07:40:33 -06:00
|
|
|
idx: 0,
|
2016-01-31 11:17:15 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct BitVectorIter<'a> {
|
2018-02-14 17:30:49 -06:00
|
|
|
iter: ::std::slice::Iter<'a, Word>,
|
|
|
|
current: Word,
|
2016-03-05 07:40:33 -06:00
|
|
|
idx: usize,
|
2016-01-31 11:17:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Iterator for BitVectorIter<'a> {
|
|
|
|
type Item = usize;
|
|
|
|
fn next(&mut self) -> Option<usize> {
|
|
|
|
while self.current == 0 {
|
|
|
|
self.current = if let Some(&i) = self.iter.next() {
|
|
|
|
if i == 0 {
|
2018-02-14 17:30:49 -06:00
|
|
|
self.idx += WORD_BITS;
|
2016-01-31 11:17:15 -06:00
|
|
|
continue;
|
|
|
|
} else {
|
2018-02-14 17:30:49 -06:00
|
|
|
self.idx = words(self.idx) * WORD_BITS;
|
2016-01-31 11:17:15 -06:00
|
|
|
i
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let offset = self.current.trailing_zeros() as usize;
|
|
|
|
self.current >>= offset;
|
|
|
|
self.current >>= 1; // shift otherwise overflows for 0b1000_0000_…_0000
|
|
|
|
self.idx += offset + 1;
|
|
|
|
return Some(self.idx - 1);
|
|
|
|
}
|
2015-08-18 16:36:32 -05:00
|
|
|
}
|
|
|
|
|
2016-04-02 21:58:34 -05:00
|
|
|
impl FromIterator<bool> for BitVector {
|
2018-02-22 12:53:54 -06:00
|
|
|
fn from_iter<I>(iter: I) -> BitVector
|
|
|
|
where
|
|
|
|
I: IntoIterator<Item = bool>,
|
|
|
|
{
|
2016-04-02 21:58:34 -05:00
|
|
|
let iter = iter.into_iter();
|
|
|
|
let (len, _) = iter.size_hint();
|
2018-02-14 17:30:49 -06:00
|
|
|
// Make the minimum length for the bitvector WORD_BITS bits since that's
|
2016-04-02 21:58:34 -05:00
|
|
|
// the smallest non-zero size anyway.
|
2018-02-14 17:30:49 -06:00
|
|
|
let len = if len < WORD_BITS { WORD_BITS } else { len };
|
2016-04-02 21:58:34 -05:00
|
|
|
let mut bv = BitVector::new(len);
|
|
|
|
for (idx, val) in iter.enumerate() {
|
|
|
|
if idx > len {
|
|
|
|
bv.grow(idx);
|
|
|
|
}
|
|
|
|
if val {
|
|
|
|
bv.insert(idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bv
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-05 19:12:53 -05:00
|
|
|
/// 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`.
|
2017-09-22 21:13:19 -05:00
|
|
|
#[derive(Clone, Debug)]
|
2015-08-18 16:36:32 -05:00
|
|
|
pub struct BitMatrix {
|
2016-08-05 19:12:53 -05:00
|
|
|
columns: usize,
|
2018-02-14 17:30:49 -06:00
|
|
|
vector: Vec<Word>,
|
2015-08-18 16:36:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BitMatrix {
|
2017-10-30 03:48:09 -05:00
|
|
|
/// Create a new `rows x columns` matrix, initially empty.
|
2016-08-05 19:12:53 -05:00
|
|
|
pub fn new(rows: usize, columns: usize) -> BitMatrix {
|
2015-08-18 16:36:32 -05:00
|
|
|
// For every element, we need one bit for every other
|
2018-02-14 17:30:49 -06:00
|
|
|
// element. Round up to an even number of words.
|
|
|
|
let words_per_row = words(columns);
|
2015-08-18 16:36:32 -05:00
|
|
|
BitMatrix {
|
2017-08-07 00:54:09 -05:00
|
|
|
columns,
|
2018-02-14 17:30:49 -06:00
|
|
|
vector: vec![0; rows * words_per_row],
|
2015-08-18 16:36:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-05 19:12:53 -05:00
|
|
|
/// The range of bits for a given row.
|
|
|
|
fn range(&self, row: usize) -> (usize, usize) {
|
2018-02-14 17:30:49 -06:00
|
|
|
let words_per_row = words(self.columns);
|
|
|
|
let start = row * words_per_row;
|
|
|
|
(start, start + words_per_row)
|
2015-08-18 16:36:32 -05:00
|
|
|
}
|
|
|
|
|
2017-10-30 03:48:09 -05:00
|
|
|
/// Sets the cell at `(row, column)` to true. Put another way, add
|
|
|
|
/// `column` to the bitset for `row`.
|
|
|
|
///
|
2018-02-15 16:35:11 -06:00
|
|
|
/// Returns true if this changed the matrix, and false otherwise.
|
2017-10-30 03:48:09 -05:00
|
|
|
pub fn add(&mut self, row: usize, column: usize) -> bool {
|
|
|
|
let (start, _) = self.range(row);
|
|
|
|
let (word, mask) = word_mask(column);
|
2017-08-01 07:03:03 -05:00
|
|
|
let vector = &mut self.vector[..];
|
2016-03-05 07:40:33 -06:00
|
|
|
let v1 = vector[start + word];
|
2015-08-18 16:36:32 -05:00
|
|
|
let v2 = v1 | mask;
|
2016-03-05 07:40:33 -06:00
|
|
|
vector[start + word] = v2;
|
2015-08-18 16:36:32 -05:00
|
|
|
v1 != v2
|
|
|
|
}
|
|
|
|
|
2017-10-30 03:48:09 -05:00
|
|
|
/// Do the bits from `row` contain `column`? Put another way, is
|
|
|
|
/// the matrix cell at `(row, column)` true? Put yet another way,
|
|
|
|
/// if the matrix represents (transitive) reachability, can
|
|
|
|
/// `row` reach `column`?
|
|
|
|
pub fn contains(&self, row: usize, column: usize) -> bool {
|
|
|
|
let (start, _) = self.range(row);
|
|
|
|
let (word, mask) = word_mask(column);
|
2016-03-05 07:40:33 -06:00
|
|
|
(self.vector[start + word] & mask) != 0
|
2015-08-18 16:36:32 -05:00
|
|
|
}
|
|
|
|
|
2017-10-30 03:48:09 -05:00
|
|
|
/// Returns those indices that are true in rows `a` and `b`. This
|
|
|
|
/// is an O(n) operation where `n` is the number of elements
|
|
|
|
/// (somewhat independent from the actual size of the
|
2015-08-18 16:36:32 -05:00
|
|
|
/// intersection, in particular).
|
|
|
|
pub fn intersection(&self, a: usize, b: usize) -> Vec<usize> {
|
|
|
|
let (a_start, a_end) = self.range(a);
|
|
|
|
let (b_start, b_end) = self.range(b);
|
2016-08-05 19:12:53 -05:00
|
|
|
let mut result = Vec::with_capacity(self.columns);
|
2015-08-18 16:36:32 -05:00
|
|
|
for (base, (i, j)) in (a_start..a_end).zip(b_start..b_end).enumerate() {
|
|
|
|
let mut v = self.vector[i] & self.vector[j];
|
2018-02-14 17:30:49 -06:00
|
|
|
for bit in 0..WORD_BITS {
|
2016-03-05 07:40:33 -06:00
|
|
|
if v == 0 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if v & 0x1 != 0 {
|
2018-02-14 17:30:49 -06:00
|
|
|
result.push(base * WORD_BITS + bit);
|
2016-03-05 07:40:33 -06:00
|
|
|
}
|
2015-08-18 16:36:32 -05:00
|
|
|
v >>= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2017-10-30 03:48:09 -05:00
|
|
|
/// Add the bits from row `read` to the bits from row `write`,
|
2015-08-18 16:36:32 -05:00
|
|
|
/// return true if anything changed.
|
|
|
|
///
|
2015-08-21 13:40:07 -05:00
|
|
|
/// This is used when computing transitive reachability because if
|
|
|
|
/// you have an edge `write -> read`, because in that case
|
|
|
|
/// `write` can reach everything that `read` can (and
|
2015-08-18 16:36:32 -05:00
|
|
|
/// potentially more).
|
2015-08-21 13:40:07 -05:00
|
|
|
pub fn merge(&mut self, read: usize, write: usize) -> bool {
|
|
|
|
let (read_start, read_end) = self.range(read);
|
|
|
|
let (write_start, write_end) = self.range(write);
|
2015-08-18 16:36:32 -05:00
|
|
|
let vector = &mut self.vector[..];
|
|
|
|
let mut changed = false;
|
2016-03-05 07:40:33 -06:00
|
|
|
for (read_index, write_index) in (read_start..read_end).zip(write_start..write_end) {
|
2015-08-21 13:40:07 -05:00
|
|
|
let v1 = vector[write_index];
|
|
|
|
let v2 = v1 | vector[read_index];
|
|
|
|
vector[write_index] = v2;
|
2015-08-18 16:36:32 -05:00
|
|
|
changed = changed | (v1 != v2);
|
|
|
|
}
|
|
|
|
changed
|
|
|
|
}
|
2016-08-05 19:12:53 -05:00
|
|
|
|
2017-10-30 03:48:09 -05:00
|
|
|
/// Iterates through all the columns set to true in a given row of
|
|
|
|
/// the matrix.
|
2016-08-05 19:12:53 -05:00
|
|
|
pub fn iter<'a>(&'a self, row: usize) -> BitVectorIter<'a> {
|
|
|
|
let (start, end) = self.range(row);
|
|
|
|
BitVectorIter {
|
|
|
|
iter: self.vector[start..end].iter(),
|
|
|
|
current: 0,
|
|
|
|
idx: 0,
|
|
|
|
}
|
|
|
|
}
|
2015-08-18 16:36:32 -05:00
|
|
|
}
|
|
|
|
|
2018-02-15 12:47:40 -06:00
|
|
|
#[derive(Clone, Debug)]
|
2018-02-22 12:53:54 -06:00
|
|
|
pub struct SparseBitMatrix<R, C>
|
|
|
|
where
|
|
|
|
R: Idx,
|
|
|
|
C: Idx,
|
|
|
|
{
|
2018-02-15 12:47:40 -06:00
|
|
|
vector: IndexVec<R, SparseBitSet<C>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<R: Idx, C: Idx> SparseBitMatrix<R, C> {
|
|
|
|
/// Create a new `rows x columns` matrix, initially empty.
|
|
|
|
pub fn new(rows: R, _columns: C) -> SparseBitMatrix<R, C> {
|
|
|
|
SparseBitMatrix {
|
|
|
|
vector: IndexVec::from_elem_n(SparseBitSet::new(), rows.index()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the cell at `(row, column)` to true. Put another way, insert
|
|
|
|
/// `column` to the bitset for `row`.
|
|
|
|
///
|
|
|
|
/// Returns true if this changed the matrix, and false otherwise.
|
|
|
|
pub fn add(&mut self, row: R, column: C) -> bool {
|
|
|
|
self.vector[row].insert(column)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Do the bits from `row` contain `column`? Put another way, is
|
|
|
|
/// the matrix cell at `(row, column)` true? Put yet another way,
|
|
|
|
/// if the matrix represents (transitive) reachability, can
|
|
|
|
/// `row` reach `column`?
|
|
|
|
pub fn contains(&self, row: R, column: C) -> bool {
|
|
|
|
self.vector[row].contains(column)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add the bits from row `read` to the bits from row `write`,
|
|
|
|
/// return true if anything changed.
|
|
|
|
///
|
|
|
|
/// This is used when computing transitive reachability because if
|
|
|
|
/// you have an edge `write -> read`, because in that case
|
|
|
|
/// `write` can reach everything that `read` can (and
|
|
|
|
/// potentially more).
|
|
|
|
pub fn merge(&mut self, read: R, write: R) -> bool {
|
|
|
|
let mut changed = false;
|
|
|
|
|
|
|
|
if read != write {
|
|
|
|
let (bit_set_read, bit_set_write) = self.vector.pick2_mut(read, write);
|
|
|
|
|
|
|
|
for read_val in bit_set_read.iter() {
|
|
|
|
changed = changed | bit_set_write.insert(read_val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
changed
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Iterates through all the columns set to true in a given row of
|
|
|
|
/// the matrix.
|
|
|
|
pub fn iter<'a>(&'a self, row: R) -> impl Iterator<Item = C> + 'a {
|
|
|
|
self.vector[row].iter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct SparseBitSet<I: Idx> {
|
|
|
|
chunk_bits: BTreeMap<u32, Word>,
|
|
|
|
_marker: PhantomData<I>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct SparseChunk<I> {
|
|
|
|
key: u32,
|
|
|
|
bits: Word,
|
|
|
|
_marker: PhantomData<I>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<I: Idx> SparseChunk<I> {
|
|
|
|
pub fn one(index: I) -> Self {
|
|
|
|
let index = index.index();
|
|
|
|
let key_usize = index / 128;
|
|
|
|
let key = key_usize as u32;
|
|
|
|
assert_eq!(key as usize, key_usize);
|
|
|
|
SparseChunk {
|
|
|
|
key,
|
|
|
|
bits: 1 << (index % 128),
|
2018-02-22 12:53:54 -06:00
|
|
|
_marker: PhantomData,
|
2018-02-15 12:47:40 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn any(&self) -> bool {
|
|
|
|
self.bits != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn iter(&self) -> impl Iterator<Item = I> {
|
|
|
|
let base = self.key as usize * 128;
|
|
|
|
let mut bits = self.bits;
|
2018-02-22 12:53:54 -06:00
|
|
|
(0..128)
|
|
|
|
.map(move |i| {
|
|
|
|
let current_bits = bits;
|
|
|
|
bits >>= 1;
|
|
|
|
(i, current_bits)
|
|
|
|
})
|
|
|
|
.take_while(|&(_, bits)| bits != 0)
|
|
|
|
.filter_map(move |(i, bits)| {
|
|
|
|
if (bits & 1) != 0 {
|
|
|
|
Some(I::new(base + i))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
2018-02-15 12:47:40 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<I: Idx> SparseBitSet<I> {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
SparseBitSet {
|
|
|
|
chunk_bits: BTreeMap::new(),
|
2018-02-22 12:53:54 -06:00
|
|
|
_marker: PhantomData,
|
2018-02-15 12:47:40 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn capacity(&self) -> usize {
|
|
|
|
self.chunk_bits.len() * 128
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn contains_chunk(&self, chunk: SparseChunk<I>) -> SparseChunk<I> {
|
|
|
|
SparseChunk {
|
2018-02-22 12:53:54 -06:00
|
|
|
bits: self.chunk_bits
|
|
|
|
.get(&chunk.key)
|
|
|
|
.map_or(0, |bits| bits & chunk.bits),
|
2018-02-15 12:47:40 -06:00
|
|
|
..chunk
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn insert_chunk(&mut self, chunk: SparseChunk<I>) -> SparseChunk<I> {
|
|
|
|
if chunk.bits == 0 {
|
|
|
|
return chunk;
|
|
|
|
}
|
|
|
|
let bits = self.chunk_bits.entry(chunk.key).or_insert(0);
|
|
|
|
let old_bits = *bits;
|
|
|
|
let new_bits = old_bits | chunk.bits;
|
|
|
|
*bits = new_bits;
|
|
|
|
let changed = new_bits ^ old_bits;
|
|
|
|
SparseChunk {
|
|
|
|
bits: changed,
|
|
|
|
..chunk
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn remove_chunk(&mut self, chunk: SparseChunk<I>) -> SparseChunk<I> {
|
|
|
|
if chunk.bits == 0 {
|
|
|
|
return chunk;
|
|
|
|
}
|
|
|
|
let changed = match self.chunk_bits.entry(chunk.key) {
|
|
|
|
Entry::Occupied(mut bits) => {
|
|
|
|
let old_bits = *bits.get();
|
|
|
|
let new_bits = old_bits & !chunk.bits;
|
|
|
|
if new_bits == 0 {
|
|
|
|
bits.remove();
|
|
|
|
} else {
|
|
|
|
bits.insert(new_bits);
|
|
|
|
}
|
|
|
|
new_bits ^ old_bits
|
|
|
|
}
|
2018-02-22 12:53:54 -06:00
|
|
|
Entry::Vacant(_) => 0,
|
2018-02-15 12:47:40 -06:00
|
|
|
};
|
|
|
|
SparseChunk {
|
|
|
|
bits: changed,
|
|
|
|
..chunk
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn clear(&mut self) {
|
|
|
|
self.chunk_bits.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn chunks<'a>(&'a self) -> impl Iterator<Item = SparseChunk<I>> + 'a {
|
2018-02-22 12:53:54 -06:00
|
|
|
self.chunk_bits.iter().map(|(&key, &bits)| SparseChunk {
|
|
|
|
key,
|
|
|
|
bits,
|
|
|
|
_marker: PhantomData,
|
2018-02-15 12:47:40 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn contains(&self, index: I) -> bool {
|
|
|
|
self.contains_chunk(SparseChunk::one(index)).any()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn insert(&mut self, index: I) -> bool {
|
|
|
|
self.insert_chunk(SparseChunk::one(index)).any()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn remove(&mut self, index: I) -> bool {
|
|
|
|
self.remove_chunk(SparseChunk::one(index)).any()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn iter<'a>(&'a self) -> impl Iterator<Item = I> + 'a {
|
|
|
|
self.chunks().flat_map(|chunk| chunk.iter())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-12 14:13:02 -06:00
|
|
|
#[inline]
|
2018-02-14 17:30:49 -06:00
|
|
|
fn words(elements: usize) -> usize {
|
|
|
|
(elements + WORD_BITS - 1) / WORD_BITS
|
2015-08-18 16:36:32 -05:00
|
|
|
}
|
|
|
|
|
2017-01-12 14:13:02 -06:00
|
|
|
#[inline]
|
2018-02-14 17:30:49 -06:00
|
|
|
fn word_mask(index: usize) -> (usize, Word) {
|
|
|
|
let word = index / WORD_BITS;
|
|
|
|
let mask = 1 << (index % WORD_BITS);
|
2015-08-18 16:36:32 -05:00
|
|
|
(word, mask)
|
|
|
|
}
|
|
|
|
|
2016-01-31 11:17:15 -06:00
|
|
|
#[test]
|
|
|
|
fn bitvec_iter_works() {
|
|
|
|
let mut bitvec = BitVector::new(100);
|
|
|
|
bitvec.insert(1);
|
|
|
|
bitvec.insert(10);
|
|
|
|
bitvec.insert(19);
|
|
|
|
bitvec.insert(62);
|
|
|
|
bitvec.insert(63);
|
|
|
|
bitvec.insert(64);
|
|
|
|
bitvec.insert(65);
|
|
|
|
bitvec.insert(66);
|
|
|
|
bitvec.insert(99);
|
2018-02-22 12:53:54 -06:00
|
|
|
assert_eq!(
|
|
|
|
bitvec.iter().collect::<Vec<_>>(),
|
|
|
|
[1, 10, 19, 62, 63, 64, 65, 66, 99]
|
|
|
|
);
|
2016-01-31 11:17:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-06-11 10:29:58 -05:00
|
|
|
fn bitvec_iter_works_2() {
|
2016-01-31 11:17:15 -06:00
|
|
|
let mut bitvec = BitVector::new(319);
|
|
|
|
bitvec.insert(0);
|
|
|
|
bitvec.insert(127);
|
|
|
|
bitvec.insert(191);
|
|
|
|
bitvec.insert(255);
|
|
|
|
bitvec.insert(319);
|
|
|
|
assert_eq!(bitvec.iter().collect::<Vec<_>>(), [0, 127, 191, 255, 319]);
|
|
|
|
}
|
|
|
|
|
2015-08-18 16:36:32 -05:00
|
|
|
#[test]
|
|
|
|
fn union_two_vecs() {
|
|
|
|
let mut vec1 = BitVector::new(65);
|
|
|
|
let mut vec2 = BitVector::new(65);
|
|
|
|
assert!(vec1.insert(3));
|
|
|
|
assert!(!vec1.insert(3));
|
|
|
|
assert!(vec2.insert(5));
|
|
|
|
assert!(vec2.insert(64));
|
|
|
|
assert!(vec1.insert_all(&vec2));
|
|
|
|
assert!(!vec1.insert_all(&vec2));
|
|
|
|
assert!(vec1.contains(3));
|
|
|
|
assert!(!vec1.contains(4));
|
|
|
|
assert!(vec1.contains(5));
|
|
|
|
assert!(!vec1.contains(63));
|
|
|
|
assert!(vec1.contains(64));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn grow() {
|
|
|
|
let mut vec1 = BitVector::new(65);
|
2018-02-22 12:53:54 -06:00
|
|
|
for index in 0..65 {
|
2016-04-22 13:07:23 -05:00
|
|
|
assert!(vec1.insert(index));
|
|
|
|
assert!(!vec1.insert(index));
|
|
|
|
}
|
2015-08-18 16:36:32 -05:00
|
|
|
vec1.grow(128);
|
2016-04-22 13:07:23 -05:00
|
|
|
|
|
|
|
// Check if the bits set before growing are still set
|
2018-02-22 12:53:54 -06:00
|
|
|
for index in 0..65 {
|
2016-04-22 13:07:23 -05:00
|
|
|
assert!(vec1.contains(index));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the new bits are all un-set
|
2018-02-22 12:53:54 -06:00
|
|
|
for index in 65..128 {
|
2016-04-22 13:07:23 -05:00
|
|
|
assert!(!vec1.contains(index));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that we can set all new bits without running out of bounds
|
2018-02-22 12:53:54 -06:00
|
|
|
for index in 65..128 {
|
2016-04-22 13:07:23 -05:00
|
|
|
assert!(vec1.insert(index));
|
|
|
|
assert!(!vec1.insert(index));
|
|
|
|
}
|
2015-08-18 16:36:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn matrix_intersection() {
|
2016-08-05 19:12:53 -05:00
|
|
|
let mut vec1 = BitMatrix::new(200, 200);
|
2015-08-18 16:36:32 -05:00
|
|
|
|
2015-08-21 13:40:07 -05:00
|
|
|
// (*) Elements reachable from both 2 and 65.
|
|
|
|
|
2015-08-18 16:36:32 -05:00
|
|
|
vec1.add(2, 3);
|
|
|
|
vec1.add(2, 6);
|
2015-08-21 13:40:07 -05:00
|
|
|
vec1.add(2, 10); // (*)
|
|
|
|
vec1.add(2, 64); // (*)
|
2015-08-18 16:36:32 -05:00
|
|
|
vec1.add(2, 65);
|
|
|
|
vec1.add(2, 130);
|
2015-08-21 13:40:07 -05:00
|
|
|
vec1.add(2, 160); // (*)
|
|
|
|
|
|
|
|
vec1.add(64, 133);
|
2015-08-18 16:36:32 -05:00
|
|
|
|
|
|
|
vec1.add(65, 2);
|
|
|
|
vec1.add(65, 8);
|
2015-08-21 13:40:07 -05:00
|
|
|
vec1.add(65, 10); // (*)
|
|
|
|
vec1.add(65, 64); // (*)
|
2015-08-18 16:36:32 -05:00
|
|
|
vec1.add(65, 68);
|
|
|
|
vec1.add(65, 133);
|
2015-08-21 13:40:07 -05:00
|
|
|
vec1.add(65, 160); // (*)
|
2015-08-18 16:36:32 -05:00
|
|
|
|
|
|
|
let intersection = vec1.intersection(2, 64);
|
|
|
|
assert!(intersection.is_empty());
|
|
|
|
|
|
|
|
let intersection = vec1.intersection(2, 65);
|
2015-08-21 13:40:07 -05:00
|
|
|
assert_eq!(intersection, &[10, 64, 160]);
|
2015-04-07 05:12:13 -05:00
|
|
|
}
|
2016-08-05 19:12:53 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn matrix_iter() {
|
|
|
|
let mut matrix = BitMatrix::new(64, 100);
|
|
|
|
matrix.add(3, 22);
|
|
|
|
matrix.add(3, 75);
|
|
|
|
matrix.add(2, 99);
|
|
|
|
matrix.add(4, 0);
|
|
|
|
matrix.merge(3, 5);
|
|
|
|
|
|
|
|
let expected = [99];
|
|
|
|
let mut iter = expected.iter();
|
|
|
|
for i in matrix.iter(2) {
|
|
|
|
let j = *iter.next().unwrap();
|
|
|
|
assert_eq!(i, j);
|
|
|
|
}
|
|
|
|
assert!(iter.next().is_none());
|
|
|
|
|
|
|
|
let expected = [22, 75];
|
|
|
|
let mut iter = expected.iter();
|
|
|
|
for i in matrix.iter(3) {
|
|
|
|
let j = *iter.next().unwrap();
|
|
|
|
assert_eq!(i, j);
|
|
|
|
}
|
|
|
|
assert!(iter.next().is_none());
|
|
|
|
|
|
|
|
let expected = [0];
|
|
|
|
let mut iter = expected.iter();
|
|
|
|
for i in matrix.iter(4) {
|
|
|
|
let j = *iter.next().unwrap();
|
|
|
|
assert_eq!(i, j);
|
|
|
|
}
|
|
|
|
assert!(iter.next().is_none());
|
|
|
|
|
|
|
|
let expected = [22, 75];
|
|
|
|
let mut iter = expected.iter();
|
|
|
|
for i in matrix.iter(5) {
|
|
|
|
let j = *iter.next().unwrap();
|
|
|
|
assert_eq!(i, j);
|
|
|
|
}
|
|
|
|
assert!(iter.next().is_none());
|
|
|
|
}
|