pub struct BloomFilter<S = DefaultHasher> { /* private fields */ }Expand description
A space efficient approximate membership set data structure.
False positives from contains are possible, but false negatives
are not, i.e. contains for all items in the set is guaranteed to return
true, while contains for all items not in the set probably return false.
BloomFilter is supported by an underlying bit vector to track item membership.
To insert, a number of bits are set at positions based on the item’s hash in the underlying bit vector.
To check membership, a number of bits are checked at positions based on the item’s hash in the underlying bit vector.
Once constructed, neither the Bloom filter’s underlying memory usage nor number of bits per item change.
§Examples
Basic usage:
use fastbloom::BloomFilter;
let mut filter = BloomFilter::with_num_bits(1024).expected_items(2);
filter.insert("42");
filter.insert("🦀");Instantiate with a target false positive rate:
use fastbloom::BloomFilter;
let filter = BloomFilter::with_false_pos(0.001).items(["42", "🦀"]);
assert!(filter.contains("42"));
assert!(filter.contains("🦀"));Use any hasher:
use fastbloom::BloomFilter;
use ahash::RandomState;
let filter = BloomFilter::with_num_bits(1024)
.hasher(RandomState::default())
.items(["42", "🦀"]);Implementations§
Source§impl BloomFilter
impl BloomFilter
Sourcepub fn with_false_pos(fp: f64) -> BuilderWithFalsePositiveRate
pub fn with_false_pos(fp: f64) -> BuilderWithFalsePositiveRate
Creates a new instance of BuilderWithFalsePositiveRate to construct a BloomFilter with a target false positive rate of fp.
The memory size of the underlying bit vector is dependent on the false positive rate and the expected number of items.
§Panics
Panics if the false positive rate, fp, is 0.
§Examples
use fastbloom::BloomFilter;
let bloom = BloomFilter::with_false_pos(0.001).expected_items(1000);Sourcepub fn with_num_bits(num_bits: usize) -> BuilderWithBits
pub fn with_num_bits(num_bits: usize) -> BuilderWithBits
Creates a new instance of BuilderWithBits to construct a BloomFilter with num_bits number of bits for tracking item membership.
§Panics
Panics if the number of bits, num_bits, is 0.
§Examples
use fastbloom::BloomFilter;
let bloom = BloomFilter::with_num_bits(1024).hashes(4);Sourcepub fn from_vec(bit_vec: Vec<u64>) -> BuilderWithBits
pub fn from_vec(bit_vec: Vec<u64>) -> BuilderWithBits
Creates a new instance of BuilderWithBits to construct a BloomFilter initialized with bit vector bit_vec.
§Panics
Panics if the bit vector, bit_vec, is empty.
§Examples
use fastbloom::BloomFilter;
let orig = BloomFilter::with_false_pos(0.001).seed(&42).items([1, 2]);
let num_hashes = orig.num_hashes();
let new = BloomFilter::from_vec(orig.as_slice().to_vec()).seed(&42).hashes(num_hashes);
assert!(new.contains(&1));
assert!(new.contains(&2));Source§impl<S: BuildHasher> BloomFilter<S>
impl<S: BuildHasher> BloomFilter<S>
Sourcepub fn insert(&mut self, val: &(impl Hash + ?Sized)) -> bool
pub fn insert(&mut self, val: &(impl Hash + ?Sized)) -> bool
Inserts an element into the Bloom filter.
§Returns
true if the item may have been previously in the Bloom filter (indicating a potential false positive),
false otherwise.
§Examples
use fastbloom::BloomFilter;
let mut bloom = BloomFilter::with_num_bits(1024).hashes(4);
bloom.insert(&2);
assert!(bloom.contains(&2));Sourcepub fn insert_hash(&mut self, hash: u64) -> bool
pub fn insert_hash(&mut self, hash: u64) -> bool
Inserts the hash of an element into the Bloom filter. That is the element is pre-hashed and all subsequent hashes are derived from this “source” hash.
§Returns
true if the item may have been previously in the Bloom filter (indicating a potential false positive),
false otherwise.
Sourcepub fn contains_hash(&self, source_hash: u64) -> bool
pub fn contains_hash(&self, source_hash: u64) -> bool
Checks if the hash of an element is possibly in the Bloom filter. That is the element is pre-hashed and all subsequent hashes are derived from this “source” hash.
§Returns
true if the item is possibly in the Bloom filter, false otherwise.
Sourcepub fn num_hashes(&self) -> u32
pub fn num_hashes(&self) -> u32
Returns the number of hashes per item.
Sourcepub fn num_bits(&self) -> usize
pub fn num_bits(&self) -> usize
Returns the total number of in-memory bits supporting the Bloom filter.
Sourcepub fn as_slice(&self) -> &[u64]
pub fn as_slice(&self) -> &[u64]
Returns a u64 slice of this BloomFilter’s contents.
§Examples
use fastbloom::BloomFilter;
let data = vec![0x517cc1b727220a95; 8];
let bloom = BloomFilter::from_vec(data.clone()).hashes(4);
assert_eq!(bloom.as_slice().to_vec(), data);Sourcepub fn source_hash(&self, val: &(impl Hash + ?Sized)) -> u64
pub fn source_hash(&self, val: &(impl Hash + ?Sized)) -> u64
Returns the hash of val using this Bloom filter’s hasher.
The resulting value can be used in Self::contains_hash or Self::insert_hash.
All subsequent hashes are derived from this source hash.
This is useful for pre-computing hash values in order to store them or send them over the network.
Trait Implementations§
Source§impl<S: Clone> Clone for BloomFilter<S>
impl<S: Clone> Clone for BloomFilter<S>
Source§fn clone(&self) -> BloomFilter<S>
fn clone(&self) -> BloomFilter<S>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<S: Debug> Debug for BloomFilter<S>
impl<S: Debug> Debug for BloomFilter<S>
Source§impl<T, S: BuildHasher> Extend<T> for BloomFilter<S>where
T: Hash,
impl<T, S: BuildHasher> Extend<T> for BloomFilter<S>where
T: Hash,
Source§fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)