Small changes.

This commit is contained in:
2018-01-03 20:14:58 +01:00
parent b9a5b10e1c
commit 75baabef1c

View File

@@ -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<BitHasher>;
type BitHashMap<K, V> = HashMap<K, V, BitBuildHasher>;
type BitHashSet<T> = HashSet<T, BitBuildHasher>;
type Block = u64;
type Storage = BitHashMap<u64, Block>;
@@ -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<usize> for BitSet {
fn from_iter<I: IntoIterator<Item = usize>>(iter: I) -> BitSet {
impl FromIterator<u64> for BitSet {
#[inline]
fn from_iter<I: IntoIterator<Item = u64>>(iter: I) -> BitSet {
let mut set = BitSet::new();
set.extend(iter);
set
}
}
impl Extend<usize> for BitSet {
impl Extend<u64> for BitSet {
#[inline]
fn extend<I: IntoIterator<Item = usize>>(&mut self, iter: I) {
fn extend<I: IntoIterator<Item = u64>>(&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<I: IntoIterator<Item = &'a usize>>(&mut self, iter: I) {
fn extend<I: IntoIterator<Item = &'a u64>>(&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();