pub struct Bloomfilter { /* private fields */ }Expand description
Bloom Filter
Implementations§
Source§impl Bloomfilter
impl Bloomfilter
Sourcepub fn new(n_bits: usize, n_hash_functions: usize) -> Self
pub fn new(n_bits: usize, n_hash_functions: usize) -> Self
Get a new bloom filter with a bitmap of length n_bits and n_hash_functions
hash functions.
§Arguments
-
n_bits: How many bits should the bitmap of the bloomfilter have? More bits make collision (False Positives) more unlikely. -
n_hash_functions: How many hash functions should be used?
§Examples
use bloomfilter_rs::filter;
let bloom_filter = filter::Bloomfilter::new(
10,
20
);Sourcepub fn new_with_seeds(n_bits: usize, seeds: Vec<u64>) -> Self
pub fn new_with_seeds(n_bits: usize, seeds: Vec<u64>) -> Self
Generate a bloomfilter with non-random seeds.
§Arguments
-
n_bits: How many bits should the bitmap of the bloomfilter have? More bits make collision (False Positives) more unlikely. -
seeds: Provide custom seeds to parameterize the hash functions. Given a different seed, the hash function would hash the same objects to different hashes. You should not provide the same seeds twice.
§Examples
use bloomfilter_rs::filter;
let bloom_filter = filter::Bloomfilter::new_with_seeds(
10,
vec![1, 10, 100],
);Sourcepub fn new_with_seeds_and_custom_hash_function(
n_bits: usize,
seeds: Vec<u64>,
hash_function: fn(&[u8], u64) -> u64,
) -> Self
pub fn new_with_seeds_and_custom_hash_function( n_bits: usize, seeds: Vec<u64>, hash_function: fn(&[u8], u64) -> u64, ) -> Self
Generate a bloomfilter with non-random seeds and a custom hash function.
§Arguments
-
n_bits: How many bits should the bitmap of the bloomfilter have? More bits make collision (False Positives) more unlikely. -
seeds: Provide custom seeds to parameterize the hash functions. Given a different seed, the hash function would hash the same objects to different hashes. You should not provide the same seeds twice. -
hash_function: Specify a custom hash function. Hash functions should take in the bytes of the data and a seed to parmetrize the behaviour of the hash function. Given different hash values the function should show different behaviour.
§Examples
use bloomfilter_rs::filter;
let bloom_filter = filter::Bloomfilter::new_with_seeds_and_custom_hash_function(
10,
vec![1, 10, 100],
|bytes, seed| -> u64
{(bytes.iter().map(|elem| *elem as u64).sum::<u64>() + seed).into()}
);Sourcepub fn from_specification(
expected_number_of_entries: usize,
acceptable_false_positive_rate: f64,
) -> Self
pub fn from_specification( expected_number_of_entries: usize, acceptable_false_positive_rate: f64, ) -> Self
Create a random bloomfilter given its supposed characteristics.
Give the number of expected entries and the acceptable false positive rates, we can create a hashfilter that will have the expected characteristic. If we use more entries then expected, the false positive rate might decrease.
§Arguments:
- expected_nubmber_of_entries: How many entries do you expect in the hash filter?
- acceptable_false_positive_rate: What false positive rate is acceptabel for your use-case?
§Examples
use bloomfilter_rs::filter;
let my_hash_filter = filter::Bloomfilter::from_specification(10, 0.5);Sourcepub fn number_of_bits(&self) -> usize
pub fn number_of_bits(&self) -> usize
How many bits does the bitmap of the filter have?
§Examples
use bloomfilter_rs::filter;
let bloom_filter = filter::Bloomfilter::new(
10,
20
);
println!("{}", bloom_filter.number_of_bits())Sourcepub fn insert(&mut self, object_as_string: &str)
pub fn insert(&mut self, object_as_string: &str)
Insert an object into the bloomfilter. The object should be a string.
If you map higher-level objects (struct ect.) to string, make sure that the representation does exactly identify each object (e.g. two different objects cannot map to the same string representation).
§Arguments
object: The object to insert.
§Examples
use bloomfilter_rs::filter;
let mut bloom_filter = filter::Bloomfilter::new(
10,
20,
);
bloom_filter.insert("To be inserted");Sourcepub fn insert_object_as_bytes(&mut self, object_as_bytes: &[u8])
pub fn insert_object_as_bytes(&mut self, object_as_bytes: &[u8])
Sourcepub fn contains(&self, object_as_string: &str) -> bool
pub fn contains(&self, object_as_string: &str) -> bool
Test that an object is in the hash filter. The objects needs to be string.
The bloomfilter is probabilistic in nature. False negatives cannot occur (if bloomfilter.contains
returns false, that will always be true), but false positives can occur.
§Arguments
object_as_string: The object to check if it is contained in the bloomfilter.
§Examples
use bloomfilter_rs::filter;
let mut bloom_filter = filter::Bloomfilter::new(
10,
20,
);
bloom_filter.contains("To be inserted");Sourcepub fn contains_object_as_bytes(&self, object_as_bytes: &[u8]) -> bool
pub fn contains_object_as_bytes(&self, object_as_bytes: &[u8]) -> bool
Test that an object as byte-array is in the hash filter.
§Arguments
object_as_bytes: The object to check if it is contained in the bloomfilter.
§Examples
use bloomfilter_rs::filter;
let mut bloom_filter = filter::Bloomfilter::new(
10,
20,
);
bloom_filter.contains_object_as_bytes("To be inserted".as_bytes());