Struct bloom_filter_rs::BloomFilter
source · pub struct BloomFilter<T> { /* private fields */ }
Expand description
BloomFilter
An implementation of a bloom filter
Implementations§
source§impl<T: BloomHasher> BloomFilter<T>
impl<T: BloomHasher> BloomFilter<T>
sourcepub fn new(hasher: T, k: u32, array_size: u64) -> Self
pub fn new(hasher: T, k: u32, array_size: u64) -> Self
Create a new BloomFilter
given a hasher
,
the number of hash functions to use,
and the size of the underlying bit array.
Typically, this function should not be called directly unless, the optimal number of hash functions and optimal array size are already known.
sourcepub fn optimal(hasher: T, max_elements: u64, error_rate: f64) -> Self
pub fn optimal(hasher: T, max_elements: u64, error_rate: f64) -> Self
Create a BloomFilter
by computing its optimal parameters.
This function computes the optimal array size using
-(n * ln(p)) / ln(2) ^ 2
and computes the optimal number of hash functions using
m / n * ln(2)
sourcepub fn insert_all<B: AsRef<[u8]>>(&mut self, slice: &[B])
pub fn insert_all<B: AsRef<[u8]>>(&mut self, slice: &[B])
Insert a slice of slices of bytes into the BloomFilter
.
sourcepub fn contains<B: AsRef<[u8]>>(&self, bytes: B) -> bool
pub fn contains<B: AsRef<[u8]>>(&self, bytes: B) -> bool
Check whether a slice of bytes exists in the BloomFilter
.
This is a probabilistic function that may return a false positive but will never return a false negative.
Examples
extern crate bloom_filter_rs as bloom_filter;
use std::vec::Vec;
use bloom_filter::{BloomFilter, Murmur3};
let words = vec!["Hello", "I", "am", "some", "words"];
let mut bloom_filter = BloomFilter::optimal(Murmur3, words.len() as u64, 0.01);
bloom_filter.insert_all(&words);
for word in words.iter() {
assert!(bloom_filter.contains(&word));
}
sourcepub fn false_positive_rate(&self) -> f64
pub fn false_positive_rate(&self) -> f64
Calculate the expected false positive rate given the current state of
the BloomFilter
.