Progress.

This commit is contained in:
2018-01-07 03:09:24 +01:00
parent f475629595
commit 0a91d99044
2 changed files with 98 additions and 53 deletions

View File

@@ -15,29 +15,29 @@ mod hasher;
mod block;
use hasher::BitBuildHasher;
use block::BitBlock;
use block::Block;
pub type BitHashMap<V> = HashMap<u64, V, BitBuildHasher>;
#[inline]
fn get_block_and_bit<T, B>(x: T) -> (T, B)
fn index<T, B>(value: T) -> (T, B)
where
T: BitBlock,
B: BitBlock,
T: Block,
B: Block,
{
(x / T::from(B::bits()), x % T::from(B::bits())
(T::block::<B>(value), B::bit(value))
}
#[derive(PartialEq)]
pub struct BitSet<T: BitBlock, B: BitBlock = u64> {
pub struct BitSet<T: Block, B: Block = u64> {
blocks: HashMap<T, B, BitBuildHasher>,
nbits: usize,
}
impl<T, B> BitSet<T, B>
where
T: BitBlock,
B: BitBlock,
T: Block,
B: Block,
{
#[inline]
pub fn new() -> BitSet<T, B> {
@@ -50,7 +50,10 @@ where
#[inline]
pub fn with_capacity(capacity: usize) -> BitSet<B> {
BitSet {
blocks: HashMap::with_capacity_and_hasher(capacity / B::bits() as usize, Default::default()),
blocks: HashMap::with_capacity_and_hasher(
capacity / B::bits() as usize,
Default::default(),
),
nbits: 0,
}
}
@@ -94,40 +97,40 @@ where
}
pub fn contains(&self, value: &T) -> bool {
let (block, bit) = get_block_and_bit::<T, B>(*value);
let (block, bit) = index::<T, B>(*value);
match self.blocks.get(&block) {
Some(block) => (block & (B::ONE << bit)) != B::ZERO,
Some(block) => (*block & (B::ONE << bit)) != B::ZERO,
None => false,
}
}
/*
pub fn is_subset(&self, other: &BitSet<B>) -> bool {
pub fn is_subset(&self, other: &BitSet<T, B>) -> bool {
if self.nbits > other.nbits {
false
} else {
self.blocks
.iter()
.all(|(key, block_a)| match other.blocks.get(key) {
Some(block_b) => block_a | block_b == *block_b,
None => *block_a == 0,
Some(block_b) => *block_a | *block_b == *block_b,
None => *block_a == B::ZERO,
})
}
}
#[inline]
pub fn is_superset(&self, other: &BitSet<B>) -> bool {
pub fn is_superset(&self, other: &BitSet<T, B>) -> bool {
other.is_subset(self)
}
pub fn insert(&mut self, value: u64) -> bool {
let (block, bit) = get_block_and_bit::<B>(&value);
let block = self.blocks.entry(block).or_insert(0);
pub fn insert(&mut self, value: T) -> bool {
let (block, bit) = index::<T, B>(value);
let block = self.blocks.entry(block).or_insert(B::ZERO);
let n = 1 << bit;
let n = B::ONE << bit;
if (*block & n) == 0 {
*block |= n;
if (*block & n) == B::ZERO {
*block = *block | n;
self.nbits += 1;
true
@@ -136,14 +139,14 @@ where
}
}
pub fn remove(&mut self, value: &u64) -> bool {
let (block, bit) = get_block_and_bit::<B>(value);
let block = self.blocks.entry(block).or_insert(0);
pub fn remove(&mut self, value: &T) -> bool {
let (block, bit) = index::<T, B>(*value);
let block = self.blocks.entry(block).or_insert(B::ZERO);
let n = 1 << bit;
let n = B::ONE << bit;
if (*block & n) != 0 {
*block &= !n;
if (*block & n) != B::ZERO {
*block = *block & !n;
self.nbits -= 1;
true
@@ -154,93 +157,97 @@ where
pub fn union_with(&mut self, other: &Self) {
for (key, value) in &other.blocks {
*self.blocks.entry(*key).or_insert(0) |= value;
let block = self.blocks.entry(*key).or_insert(B::ZERO);
*block = *block | *value;
}
self.nbits = self.blocks
.values()
.map(|block| block.count_ones() as usize)
.map(|block| B::count_ones(*block))
.sum();
}
*/
}
/*
impl<B> fmt::Debug for BitSet<B> {
impl<T, B> fmt::Debug for BitSet<T, B> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_set().entries(self.iter()).finish()
}
}
*/
impl<B> FromIterator<u64> for BitSet<B> {
impl<T, B> FromIterator<T> for BitSet<T, B> where T: Block, B: Block {
#[inline]
fn from_iter<I: IntoIterator<Item = u64>>(iter: I) -> BitSet<B> {
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> BitSet<T, B> {
let mut set = BitSet::new();
set.extend(iter);
set
}
}
impl<B> Extend<u64> for BitSet<B> {
impl<T, B> Extend<T> for BitSet<T, B> where T: Block, B: Block {
#[inline]
fn extend<I: IntoIterator<Item = u64>>(&mut self, iter: I) {
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
for i in iter {
self.insert(i);
}
}
}
impl<'a, B> Extend<&'a u64> for BitSet<B> {
impl<'a, T: 'a, B> Extend<&'a T> for BitSet<T, B> where T: Block, B: Block {
#[inline]
fn extend<I: IntoIterator<Item = &'a u64>>(&mut self, iter: I) {
fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {
for i in iter {
self.insert(*i);
}
}
}
impl<B> Default for BitSet<B> {
impl<T, B> Default for BitSet<T, B> where T: Block, B: Block {
#[inline]
fn default() -> BitSet<B> {
fn default() -> BitSet<T, B> {
BitSet::new()
}
}
impl<B> ops::BitOr for BitSet<B> {
impl<T, B> ops::BitOr for BitSet<T, B> where T: Block, B: Block {
type Output = Self;
#[inline]
fn bitor(mut self, rhs: Self) -> Self {
for (key, value) in &rhs.blocks {
*self.blocks.entry(*key).or_insert(0) |= value;
let block = self.blocks.entry(*key).or_insert(B::ZERO);
*block = *block | *value;
}
self.nbits = self.blocks
.values()
.map(|block| block.count_ones() as usize)
.map(|block| block.count_ones())
.sum();
self
}
}
impl<'a, B> ops::BitOr<&'a Self> for BitSet<B> {
impl<'a, T, B> ops::BitOr<&'a Self> for BitSet<T, B> where T: Block, B: Block {
type Output = Self;
#[inline]
fn bitor(mut self, rhs: &'a Self) -> Self {
for (key, value) in &rhs.blocks {
*self.blocks.entry(*key).or_insert(0) |= value;
let block = self.blocks.entry(*key).or_insert(B::ZERO);
*block = *block | *value;
}
self.nbits = self.blocks
.values()
.map(|block| block.count_ones() as usize)
.map(|block| block.count_ones())
.sum();
self
}
}
/*
pub struct Iter<'a> {
iter: std::collections::hash_map::Iter<'a, u64, u64>,
block: u64,