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.
Self 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 builder instance to construct a Self 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 filter = 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
Sourcepub fn from_vec(bit_vec: Vec<u64>) -> BuilderWithBits
pub fn from_vec(bit_vec: Vec<u64>) -> BuilderWithBits
Creates a builder instance to construct a Self 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.iter().collect()).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 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 iter(&self) -> impl Iterator<Item = u64> + '_
pub fn iter(&self) -> impl Iterator<Item = u64> + '_
Returns an iterator over the raw bit values of this Bloom filter.
Sourcepub fn as_slice(&self) -> &[u64]
pub fn as_slice(&self) -> &[u64]
Returns the underlying slice of this Bloom filter’s bit contents.
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.
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 insert_all<T: Hash, I: IntoIterator<Item = T>>(&mut self, iter: I)
pub fn insert_all<T: Hash, I: IntoIterator<Item = T>>(&mut self, iter: I)
Inserts all the items in iter into the self.
Sourcepub fn union(&mut self, other: &BloomFilter<S>)
pub fn union(&mut self, other: &BloomFilter<S>)
Unions other into self. The hashers of both Bloomfilters must be identical (this is not enforced!).
§Panics
Panics if the other Bloomfilter has a different number of bits or hashes than self.
§Example
use fastbloom::BloomFilter;
let mut bloom = BloomFilter::with_num_bits(4096).seed(&1).hashes(4);
let mut other = BloomFilter::with_num_bits(4096).seed(&1).hashes(4);
bloom.insert_all(0..=1000);
other.insert_all(500..=1500);
bloom.union(&other);
for x in 0..=2000 {
assert_eq!(bloom.contains(&x), bloom.contains(&x) || other.contains(&x));
}Sourcepub fn intersect(&mut self, other: &BloomFilter<S>)
pub fn intersect(&mut self, other: &BloomFilter<S>)
Intersects other onto self. The hashers of both Bloomfilters must be identical (this is not enforced!).
§Panics
Panics if the other Bloomfilter has a different number of bits or hashes than self.
§Example
use fastbloom::BloomFilter;
let mut bloom = BloomFilter::with_num_bits(4096).seed(&1).hashes(4);
let mut other = BloomFilter::with_num_bits(4096).seed(&1).hashes(4);
bloom.insert_all(0..=1000);
other.insert_all(500..=1500);
bloom.intersect(&other);
for x in 0..=2000 {
assert_eq!(bloom.contains(&x), bloom.contains(&x) && other.contains(&x));
}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)