pub struct BloomFilter<const BLOCK_SIZE_BITS: usize = 512, S = CloneBuildHasher<RandomDefaultHasher>> { /* 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, chunked into 512, 256, 128, or 64 bit “blocks”, to track item membership.
To insert, a number of bits, based on the item’s hash, are set in the underlying bit vector.
To check membership, a number of bits, based on the item’s hash, are checked 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 b100m_filter::BloomFilter;
let num_blocks = 4; // by default, each block is 512 bits
let filter = BloomFilter::builder(num_blocks).items(["42", "🦀"].iter());
assert!(filter.contains("42"));
assert!(filter.contains("🦀"));
Use any hasher:
use b100m_filter::BloomFilter;
use ahash::RandomState;
let num_blocks = 4; // by default, each block is 512 bits
let filter = BloomFilter::builder(num_blocks)
.hasher(RandomState::default())
.items(["42", "🦀"].iter());
Implementations§
Source§impl BloomFilter
impl BloomFilter
Sourcepub fn builder(num_blocks: usize) -> Builder<512>
pub fn builder(num_blocks: usize) -> Builder<512>
Creates a new instance of Builder
to construct a BloomFilter
with num_blocks
number of blocks for tracking item membership.
Each block is 512 bits of memory.
Use builder256
, builder128
, or builder64
for more speed
but slightly higher false positive rates.
§Examples
use b100m_filter::BloomFilter;
let bloom = BloomFilter::builder(16).hashes(4);
Sourcepub fn builder512(num_blocks: usize) -> Builder<512>
pub fn builder512(num_blocks: usize) -> Builder<512>
Creates a new instance of Builder
to construct a BloomFilter
with num_blocks
number of blocks for tracking item membership.
Each block is 512 bits of memory.
Use builder256
, builder128
, or builder64
for more speed
but slightly higher false positive rates.
§Examples
use b100m_filter::BloomFilter;
let bloom = BloomFilter::builder512(16).hashes(4);
Sourcepub fn builder256(num_blocks: usize) -> Builder<256>
pub fn builder256(num_blocks: usize) -> Builder<256>
Creates a new instance of Builder
to construct a BloomFilter
with num_blocks
number of blocks for tracking item membership.
Each block is 256 bits of memory.
Builder<256>
is faster but less accurate than Builder<512>
.
§Examples
use b100m_filter::BloomFilter;
let bloom = BloomFilter::builder256(16).hashes(4);
Sourcepub fn builder128(num_blocks: usize) -> Builder<128>
pub fn builder128(num_blocks: usize) -> Builder<128>
Creates a new instance of Builder
to construct a BloomFilter
with num_blocks
number of blocks for tracking item membership.
Each block is 128 bits of memory.
Builder<128>
is faster but less accurate than Builder<256>
.
§Examples
use b100m_filter::BloomFilter;
let bloom = BloomFilter::builder128(16).hashes(8);
Sourcepub fn builder64(num_blocks: usize) -> Builder<64>
pub fn builder64(num_blocks: usize) -> Builder<64>
Creates a new instance of Builder
to construct a BloomFilter
with num_blocks
number of blocks for tracking item membership.
Each block is 64 bits of memory.
Builder<64>
is faster but less accurate than Builder<128>
.
§Examples
use b100m_filter::BloomFilter;
let bloom = BloomFilter::builder64(16).hashes(8);
Source§impl<const BLOCK_SIZE_BITS: usize, S: BuildHasher> BloomFilter<BLOCK_SIZE_BITS, S>
impl<const BLOCK_SIZE_BITS: usize, S: BuildHasher> BloomFilter<BLOCK_SIZE_BITS, S>
Sourcepub fn insert(&mut self, val: &(impl Hash + ?Sized))
pub fn insert(&mut self, val: &(impl Hash + ?Sized))
Adds a value to the bloom filter.
§Examples
use b100m_filter::BloomFilter;
let mut bloom = BloomFilter::builder(4).hashes(4);
bloom.insert(&2);
assert!(bloom.contains(&2));
Sourcepub fn contains(&self, val: &(impl Hash + ?Sized)) -> bool
pub fn contains(&self, val: &(impl Hash + ?Sized)) -> bool
Returns false
if the bloom filter definitely does not contain a value.
Returns true
if the bloom filter may contain a value, with a degree of certainty.
§Examples
use b100m_filter::BloomFilter;
let bloom = BloomFilter::builder(4).items([1, 2, 3].iter());
assert!(bloom.contains(&1));
Sourcepub fn num_hashes(&self) -> u64
pub fn num_hashes(&self) -> u64
Returns the effective number of hashes per item. In other words, the number of bits derived per item.
For performance reasons, the number of bits is rounded to down to a power of 2, depending on BLOCK_SIZE_BITS
.
Trait Implementations§
Source§impl<const BLOCK_SIZE_BITS: usize, S: Clone> Clone for BloomFilter<BLOCK_SIZE_BITS, S>
impl<const BLOCK_SIZE_BITS: usize, S: Clone> Clone for BloomFilter<BLOCK_SIZE_BITS, S>
Source§fn clone(&self) -> BloomFilter<BLOCK_SIZE_BITS, S>
fn clone(&self) -> BloomFilter<BLOCK_SIZE_BITS, S>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<T, const BLOCK_SIZE_BITS: usize, S: BuildHasher> Extend<T> for BloomFilter<BLOCK_SIZE_BITS, S>where
T: Hash,
impl<T, const BLOCK_SIZE_BITS: usize, S: BuildHasher> Extend<T> for BloomFilter<BLOCK_SIZE_BITS, 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
)