Small changes.
This commit is contained in:
42
src/lib.rs
42
src/lib.rs
@@ -1,7 +1,7 @@
|
|||||||
use std::ops;
|
use std::ops;
|
||||||
use std::iter::FromIterator;
|
use std::iter::FromIterator;
|
||||||
use std::default::Default;
|
use std::default::Default;
|
||||||
use std::hash::{Hasher, BuildHasherDefault};
|
use std::hash::{BuildHasherDefault, Hasher};
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{HashMap, HashSet};
|
||||||
|
|
||||||
const BITS: u64 = 64;
|
const BITS: u64 = 64;
|
||||||
@@ -36,7 +36,6 @@ type BitBuildHasher = BuildHasherDefault<BitHasher>;
|
|||||||
type BitHashMap<K, V> = HashMap<K, V, BitBuildHasher>;
|
type BitHashMap<K, V> = HashMap<K, V, BitBuildHasher>;
|
||||||
type BitHashSet<T> = HashSet<T, BitBuildHasher>;
|
type BitHashSet<T> = HashSet<T, BitBuildHasher>;
|
||||||
|
|
||||||
|
|
||||||
type Block = u64;
|
type Block = u64;
|
||||||
type Storage = BitHashMap<u64, Block>;
|
type Storage = BitHashMap<u64, Block>;
|
||||||
|
|
||||||
@@ -87,12 +86,14 @@ impl BitSet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn insert(&mut self, value: usize) -> bool {
|
pub fn insert(&mut self, value: u64) -> bool {
|
||||||
let (block, bit) = block_bit(value as u64, BITS);
|
let (block, bit) = block_bit(value, BITS);
|
||||||
let block = self.blocks.entry(block).or_insert(0);
|
let block = self.blocks.entry(block).or_insert(0);
|
||||||
|
|
||||||
if (*block & (1 << bit)) == 0 {
|
let n = 1 << bit;
|
||||||
*block |= 1 << bit;
|
|
||||||
|
if (*block & n) == 0 {
|
||||||
|
*block |= n;
|
||||||
self.nbits += 1;
|
self.nbits += 1;
|
||||||
|
|
||||||
true
|
true
|
||||||
@@ -102,12 +103,14 @@ impl BitSet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn remove(&mut self, value: usize) -> bool {
|
pub fn remove(&mut self, value: u64) -> bool {
|
||||||
let (block, bit) = block_bit(value as u64, BITS);
|
let (block, bit) = block_bit(value, BITS);
|
||||||
let block = self.blocks.entry(block).or_insert(0);
|
let block = self.blocks.entry(block).or_insert(0);
|
||||||
|
|
||||||
if (*block & (1 << bit)) != 0 {
|
let n = 1 << bit;
|
||||||
*block &= !(1 << bit);
|
|
||||||
|
if (*block & n) != 0 {
|
||||||
|
*block &= !n;
|
||||||
self.nbits -= 1;
|
self.nbits -= 1;
|
||||||
|
|
||||||
true
|
true
|
||||||
@@ -117,8 +120,8 @@ impl BitSet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn contains(&self, value: usize) -> bool {
|
pub fn contains(&self, value: u64) -> bool {
|
||||||
let (block, bit) = block_bit(value as u64, BITS);
|
let (block, bit) = block_bit(value, BITS);
|
||||||
|
|
||||||
match self.blocks.get(&block) {
|
match self.blocks.get(&block) {
|
||||||
None => false,
|
None => false,
|
||||||
@@ -134,26 +137,27 @@ impl Default for BitSet {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromIterator<usize> for BitSet {
|
impl FromIterator<u64> for BitSet {
|
||||||
fn from_iter<I: IntoIterator<Item = usize>>(iter: I) -> BitSet {
|
#[inline]
|
||||||
|
fn from_iter<I: IntoIterator<Item = u64>>(iter: I) -> BitSet {
|
||||||
let mut set = BitSet::new();
|
let mut set = BitSet::new();
|
||||||
set.extend(iter);
|
set.extend(iter);
|
||||||
set
|
set
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Extend<usize> for BitSet {
|
impl Extend<u64> for BitSet {
|
||||||
#[inline]
|
#[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 {
|
for i in iter {
|
||||||
self.insert(i);
|
self.insert(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Extend<&'a usize> for BitSet {
|
impl<'a> Extend<&'a u64> for BitSet {
|
||||||
#[inline]
|
#[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 {
|
for i in iter {
|
||||||
self.insert(*i);
|
self.insert(*i);
|
||||||
}
|
}
|
||||||
@@ -163,6 +167,7 @@ impl<'a> Extend<&'a usize> for BitSet {
|
|||||||
impl ops::BitOr for BitSet {
|
impl ops::BitOr for BitSet {
|
||||||
type Output = Self;
|
type Output = Self;
|
||||||
|
|
||||||
|
#[inline]
|
||||||
fn bitor(self, rhs: Self) -> Self {
|
fn bitor(self, rhs: Self) -> Self {
|
||||||
let mut blocks = self.blocks.clone();
|
let mut blocks = self.blocks.clone();
|
||||||
|
|
||||||
@@ -185,6 +190,7 @@ impl ops::BitOr for BitSet {
|
|||||||
impl<'a> ops::BitOr<&'a Self> for BitSet {
|
impl<'a> ops::BitOr<&'a Self> for BitSet {
|
||||||
type Output = Self;
|
type Output = Self;
|
||||||
|
|
||||||
|
#[inline]
|
||||||
fn bitor(self, rhs: &'a Self) -> Self {
|
fn bitor(self, rhs: &'a Self) -> Self {
|
||||||
let mut blocks = self.blocks.clone();
|
let mut blocks = self.blocks.clone();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user