use crate::vec::{Idx, IndexVec};
use arrayvec::ArrayVec;
+use smallvec::{smallvec, SmallVec};
use std::fmt;
use std::iter;
use std::marker::PhantomData;
#[derive(Eq, PartialEq, Hash, Decodable, Encodable)]
pub struct BitSet<T> {
domain_size: usize,
- words: Vec<Word>,
+ words: SmallVec<[Word; 2]>,
marker: PhantomData<T>,
}
#[inline]
pub fn new_empty(domain_size: usize) -> BitSet<T> {
let num_words = num_words(domain_size);
- BitSet { domain_size, words: vec![0; num_words], marker: PhantomData }
+ BitSet { domain_size, words: smallvec![0; num_words], marker: PhantomData }
}
/// Creates a new, filled bitset with a given `domain_size`.
#[inline]
pub fn new_filled(domain_size: usize) -> BitSet<T> {
let num_words = num_words(domain_size);
- let mut result = BitSet { domain_size, words: vec![!0; num_words], marker: PhantomData };
+ let mut result =
+ BitSet { domain_size, words: smallvec![!0; num_words], marker: PhantomData };
result.clear_excess_bits();
result
}
pub struct BitMatrix<R: Idx, C: Idx> {
num_rows: usize,
num_columns: usize,
- words: Vec<Word>,
+ words: SmallVec<[Word; 2]>,
marker: PhantomData<(R, C)>,
}
BitMatrix {
num_rows,
num_columns,
- words: vec![0; num_rows * words_per_row],
+ words: smallvec![0; num_rows * words_per_row],
marker: PhantomData,
}
}
/// 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 {
+ pub fn iter(&self, row: R) -> impl Iterator<Item = C> + '_ {
self.row(row).into_iter().flat_map(|r| r.iter())
}