From 75baabef1cce248d8162c6923353823bf9f19d82 Mon Sep 17 00:00:00 2001 From: logaritmisk Date: Wed, 3 Jan 2018 20:14:58 +0100 Subject: [PATCH] Small changes. --- src/lib.rs | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 7a23fe4..a1b3ff2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,7 @@ use std::ops; use std::iter::FromIterator; use std::default::Default; -use std::hash::{Hasher, BuildHasherDefault}; +use std::hash::{BuildHasherDefault, Hasher}; use std::collections::{HashMap, HashSet}; const BITS: u64 = 64; @@ -36,7 +36,6 @@ type BitBuildHasher = BuildHasherDefault; type BitHashMap = HashMap; type BitHashSet = HashSet; - type Block = u64; type Storage = BitHashMap; @@ -87,12 +86,14 @@ impl BitSet { } #[inline] - pub fn insert(&mut self, value: usize) -> bool { - let (block, bit) = block_bit(value as u64, BITS); + pub fn insert(&mut self, value: u64) -> bool { + let (block, bit) = block_bit(value, BITS); let block = self.blocks.entry(block).or_insert(0); - if (*block & (1 << bit)) == 0 { - *block |= 1 << bit; + let n = 1 << bit; + + if (*block & n) == 0 { + *block |= n; self.nbits += 1; true @@ -102,12 +103,14 @@ impl BitSet { } #[inline] - pub fn remove(&mut self, value: usize) -> bool { - let (block, bit) = block_bit(value as u64, BITS); + pub fn remove(&mut self, value: u64) -> bool { + let (block, bit) = block_bit(value, BITS); let block = self.blocks.entry(block).or_insert(0); - if (*block & (1 << bit)) != 0 { - *block &= !(1 << bit); + let n = 1 << bit; + + if (*block & n) != 0 { + *block &= !n; self.nbits -= 1; true @@ -117,8 +120,8 @@ impl BitSet { } #[inline] - pub fn contains(&self, value: usize) -> bool { - let (block, bit) = block_bit(value as u64, BITS); + pub fn contains(&self, value: u64) -> bool { + let (block, bit) = block_bit(value, BITS); match self.blocks.get(&block) { None => false, @@ -134,26 +137,27 @@ impl Default for BitSet { } } -impl FromIterator for BitSet { - fn from_iter>(iter: I) -> BitSet { +impl FromIterator for BitSet { + #[inline] + 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) { + fn extend>(&mut self, iter: I) { for i in iter { self.insert(i); } } } -impl<'a> Extend<&'a usize> for BitSet { +impl<'a> Extend<&'a u64> for BitSet { #[inline] - fn extend>(&mut self, iter: I) { + fn extend>(&mut self, iter: I) { for i in iter { self.insert(*i); } @@ -163,6 +167,7 @@ impl<'a> Extend<&'a usize> for BitSet { impl ops::BitOr for BitSet { type Output = Self; + #[inline] fn bitor(self, rhs: Self) -> Self { let mut blocks = self.blocks.clone(); @@ -185,6 +190,7 @@ impl ops::BitOr for BitSet { impl<'a> ops::BitOr<&'a Self> for BitSet { type Output = Self; + #[inline] fn bitor(self, rhs: &'a Self) -> Self { let mut blocks = self.blocks.clone();