From f4756295957cf4d013fc6b1cb0c2b16919786797 Mon Sep 17 00:00:00 2001 From: logaritmisk Date: Sat, 6 Jan 2018 23:42:36 +0100 Subject: [PATCH] Did stuff, stuff that doens't work. --- src/block.rs | 31 ++++++++++++---- src/lib.rs | 99 +++++++++++++++++++++++++++++----------------------- 2 files changed, 80 insertions(+), 50 deletions(-) diff --git a/src/block.rs b/src/block.rs index 0268b4b..b15d512 100644 --- a/src/block.rs +++ b/src/block.rs @@ -1,21 +1,40 @@ use std::mem; +use std::ops::*; +use std::hash::Hash; +use std::convert::From; -pub trait BitBlock { - const ONE: Self; +pub trait BitBlock + : Copy + + Add + + Div + + From + + Sub + + Shl + + Shr + + Not + + BitAnd + + BitOr + + BitXor + + Rem + + Eq + + Ord + + Hash { const ZERO: Self; + const ONE: Self; - fn bits() -> usize; + fn bits() -> u8; fn count_ones(self) -> usize; } macro_rules! bit_block_impl { ($(($t: ty, $size: expr)),*) => ($( impl BitBlock for $t { + const ZERO: Self = 0; const ONE: Self = 1; - const ZERO: Self = 1; #[inline] - fn bits() -> usize { $size } + fn bits() -> u8 { $size } + #[inline] fn count_ones(self) -> usize { self.count_ones() as usize } } @@ -27,5 +46,5 @@ bit_block_impl!{ (u16, 16), (u32, 32), (u64, 64), - (usize, mem::size_of::() * 8) + (usize, (mem::size_of::() * 8) as u8) } diff --git a/src/lib.rs b/src/lib.rs index c49ab1c..a8d27de 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,56 +15,61 @@ mod hasher; mod block; use hasher::BitBuildHasher; - -const BITS: u64 = 64; - -type Block = u64; -type Storage = HashMap; +use block::BitBlock; pub type BitHashMap = HashMap; #[inline] -fn block_bit(x: &u64, d: &u64) -> (u64, u64) { - (x / d, x % d) +fn get_block_and_bit(x: T) -> (T, B) +where + T: BitBlock, + B: BitBlock, +{ + (x / T::from(B::bits()), x % T::from(B::bits()) } #[derive(PartialEq)] -pub struct BitSet { - blocks: Storage, +pub struct BitSet { + blocks: HashMap, nbits: usize, } -impl BitSet { +impl BitSet +where + T: BitBlock, + B: BitBlock, +{ #[inline] - pub fn new() -> BitSet { + pub fn new() -> BitSet { BitSet { - blocks: Storage::default(), + blocks: HashMap::default(), nbits: 0, } } #[inline] - pub fn with_capacity(capacity: usize) -> BitSet { + pub fn with_capacity(capacity: usize) -> BitSet { BitSet { - blocks: Storage::with_capacity_and_hasher(capacity / BITS as usize, Default::default()), + blocks: HashMap::with_capacity_and_hasher(capacity / B::bits() as usize, Default::default()), nbits: 0, } } #[inline] pub fn capacity(&self) -> usize { - self.blocks.capacity() * BITS as usize + self.blocks.capacity() * B::bits() as usize } pub fn reserve(&mut self, additional: usize) { - self.blocks.reserve(additional / BITS as usize) + self.blocks.reserve(additional / B::bits() as usize) } pub fn shrink_to_fit(&mut self) { - self.blocks.retain(|_, block| *block != 0); + self.blocks.retain(|_, block| *block != B::ZERO); self.blocks.shrink_to_fit() } + /* pub fn iter(&self) -> Iter { Iter { iter: self.blocks.iter(), @@ -73,6 +78,7 @@ impl BitSet { bit: BITS, } } + */ pub fn len(&self) -> usize { self.nbits @@ -87,16 +93,16 @@ impl BitSet { self.nbits = 0; } - pub fn contains(&self, value: &u64) -> bool { - let (block, bit) = block_bit(value, &BITS); + pub fn contains(&self, value: &T) -> bool { + let (block, bit) = get_block_and_bit::(*value); match self.blocks.get(&block) { - Some(block) => (block & (1 << bit)) != 0, + Some(block) => (block & (B::ONE << bit)) != B::ZERO, None => false, } } - - pub fn is_subset(&self, other: &BitSet) -> bool { + /* + pub fn is_subset(&self, other: &BitSet) -> bool { if self.nbits > other.nbits { false } else { @@ -110,12 +116,12 @@ impl BitSet { } #[inline] - pub fn is_superset(&self, other: &BitSet) -> bool { + pub fn is_superset(&self, other: &BitSet) -> bool { other.is_subset(self) } pub fn insert(&mut self, value: u64) -> bool { - let (block, bit) = block_bit(&value, &BITS); + let (block, bit) = get_block_and_bit::(&value); let block = self.blocks.entry(block).or_insert(0); let n = 1 << bit; @@ -131,7 +137,7 @@ impl BitSet { } pub fn remove(&mut self, value: &u64) -> bool { - let (block, bit) = block_bit(value, &BITS); + let (block, bit) = get_block_and_bit::(value); let block = self.blocks.entry(block).or_insert(0); let n = 1 << bit; @@ -156,24 +162,25 @@ impl BitSet { .map(|block| block.count_ones() as usize) .sum(); } + */ } - -impl fmt::Debug for BitSet { +/* +impl fmt::Debug for BitSet { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_set().entries(self.iter()).finish() } } -impl FromIterator for BitSet { +impl FromIterator for BitSet { #[inline] - fn from_iter>(iter: I) -> BitSet { + fn from_iter>(iter: I) -> BitSet { let mut set = BitSet::new(); set.extend(iter); set } } -impl Extend for BitSet { +impl Extend for BitSet { #[inline] fn extend>(&mut self, iter: I) { for i in iter { @@ -182,7 +189,7 @@ impl Extend for BitSet { } } -impl<'a> Extend<&'a u64> for BitSet { +impl<'a, B> Extend<&'a u64> for BitSet { #[inline] fn extend>(&mut self, iter: I) { for i in iter { @@ -191,14 +198,14 @@ impl<'a> Extend<&'a u64> for BitSet { } } -impl Default for BitSet { +impl Default for BitSet { #[inline] - fn default() -> BitSet { + fn default() -> BitSet { BitSet::new() } } -impl ops::BitOr for BitSet { +impl ops::BitOr for BitSet { type Output = Self; #[inline] @@ -216,7 +223,7 @@ impl ops::BitOr for BitSet { } } -impl<'a> ops::BitOr<&'a Self> for BitSet { +impl<'a, B> ops::BitOr<&'a Self> for BitSet { type Output = Self; #[inline] @@ -248,7 +255,7 @@ pub struct IntoIter { bit: u64, } -impl<'a> IntoIterator for &'a BitSet { +impl<'a, B> IntoIterator for &'a BitSet { type Item = u64; type IntoIter = Iter<'a>; @@ -257,7 +264,7 @@ impl<'a> IntoIterator for &'a BitSet { } } -impl IntoIterator for BitSet { +impl IntoIterator for BitSet { type Item = u64; type IntoIter = IntoIter; @@ -330,8 +337,9 @@ impl Iterator for IntoIter { } } } - -impl Serialize for BitSet { +*/ +/* +impl Serialize for BitSet { #[inline] fn serialize(&self, serializer: S) -> Result where @@ -345,15 +353,15 @@ impl Serialize for BitSet { } } -impl<'de> Deserialize<'de> for BitSet { +impl<'de, B> Deserialize<'de> for BitSet { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { - struct SeqVisitor; + struct SeqVisitor(PhantomData); - impl<'de> Visitor<'de> for SeqVisitor { - type Value = BitSet; + impl<'de, B> Visitor<'de> for SeqVisitor { + type Value = BitSet; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("a sequence") @@ -383,7 +391,7 @@ impl<'de> Deserialize<'de> for BitSet { where D: Deserializer<'de>, { - struct SeqInPlaceVisitor<'a>(&'a mut BitSet); + struct SeqInPlaceVisitor<'a>(&'a mut BitSet); impl<'a, 'de> Visitor<'de> for SeqInPlaceVisitor<'a> { type Value = (); @@ -410,6 +418,7 @@ impl<'de> Deserialize<'de> for BitSet { deserializer.deserialize_seq(SeqInPlaceVisitor(place)) } } +*/ #[cfg(test)] mod tests { @@ -644,6 +653,7 @@ mod tests { assert_eq!(set.contains(&5), true); } + /* #[test] fn test_serde_serialize() { let mut set = BitSet::new(); @@ -662,4 +672,5 @@ mod tests { ], ); } + */ }