Skip to content

Commit

Permalink
refactor: use hash_one() to hash data
Browse files Browse the repository at this point in the history
  • Loading branch information
domodwyer committed Jul 24, 2024
1 parent 7647c51 commit 5f0a408
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions src/bloom.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{bitmap::CompressedBitmap, FilterSize};
use std::collections::hash_map::RandomState;
use std::hash::{BuildHasher, Hash, Hasher};
use std::hash::{BuildHasher, Hash};
use std::marker::PhantomData;

// TODO(dom): AND, XOR, NOT + examples
Expand Down Expand Up @@ -250,14 +250,10 @@ where
/// assert!(b.contains(&&user));
/// ```
pub fn insert(&mut self, data: &'_ T) {
// Generate a hash (u64) value for data
let mut hasher = self.hasher.build_hasher();
data.hash(&mut hasher);

// Split the u64 hash into several smaller values to use as unique
// indexes in the bitmap.
hasher
.finish()
// Generate a hash (u64) value for data and split the u64 hash into
// several smaller values to use as unique indexes in the bitmap.
self.hasher
.hash_one(data)
.to_be_bytes()
.chunks(self.key_size as usize)
.for_each(|chunk| self.bitmap.set(bytes_to_usize_key(chunk), true));
Expand All @@ -270,11 +266,8 @@ where
/// been inserted into the filter.
pub fn contains(&self, data: &'_ T) -> bool {
// Generate a hash (u64) value for data
let mut hasher = self.hasher.build_hasher();
data.hash(&mut hasher);

hasher
.finish()
self.hasher
.hash_one(data)
.to_be_bytes()
.chunks(self.key_size as usize)
.any(|chunk| self.bitmap.get(bytes_to_usize_key(chunk)))
Expand Down

0 comments on commit 5f0a408

Please sign in to comment.