pub struct BloomFilter<H: Hasher = Sha256> { /* private fields */ }Expand description
A Bloom Filter.
This implementation uses the Kirsch-Mitzenmacher optimization to derive k hash functions
from two hash values, which are in turn derived from a single hash digest. This provides
efficient hashing for BloomFilter::insert and BloomFilter::contains operations.
§Hasher Selection
The H type parameter specifies the hash function to use. It defaults to Sha256.
The hasher’s digest must be at least 16 bytes (128 bits) long, this is enforced at
compile time.
When choosing a hasher, consider:
-
Security: If the bloom filter accepts untrusted input, use a cryptographically secure hash function to prevent attackers from crafting inputs that cause excessive collisions (degrading the filter to always return
true). -
Determinism: If the bloom filter must produce consistent results across runs or machines (e.g. for serialization or consensus-critical applications), avoid keyed or randomized hash functions. Both Sha256 and Blake3 are deterministic.
-
Performance: Hash function performance varies with the size of items inserted and queried. Sha256 is faster for smaller items (up to ~2KB), while Blake3 is faster for larger items (4KB+).
Implementations§
Source§impl<H: Hasher> BloomFilter<H>
impl<H: Hasher> BloomFilter<H>
Sourcepub fn new(hashers: NonZeroU8, bits: NonZeroUsize) -> Self
pub fn new(hashers: NonZeroU8, bits: NonZeroUsize) -> Self
Creates a new BloomFilter with hashers hash functions and bits bits.
The number of bits will be rounded up to the next power of 2. If that would overflow, the maximum power of 2 for the platform (2^63 on 64-bit) is used.
Sourcepub fn with_rate(expected_items: NonZeroUsize, fp_rate: BigRational) -> Self
pub fn with_rate(expected_items: NonZeroUsize, fp_rate: BigRational) -> Self
Creates a new BloomFilter with optimal parameters for the expected number of items and desired false positive rate.
Uses exact rational arithmetic for full determinism across all platforms.
§Arguments
expected_items- Number of items expected to be insertedfp_rate- False positive rate as a rational (e.g.,BigRational::from_frac_u64(1, 100)for 1%)
§Panics
Panics if fp_rate is not in (0, 1).
Sourcepub const fn bits(&self) -> NonZeroUsize
pub const fn bits(&self) -> NonZeroUsize
Returns the number of bits used by the filter.
Sourcepub fn insert(&mut self, item: &[u8])
pub fn insert(&mut self, item: &[u8])
Inserts an item into the BloomFilter.
Sourcepub fn contains(&self, item: &[u8]) -> bool
pub fn contains(&self, item: &[u8]) -> bool
Checks if an item is possibly in the BloomFilter.
Returns true if the item is probably in the set, and false if it is definitely not.
Sourcepub fn estimated_false_positive_rate(&self) -> BigRational
pub fn estimated_false_positive_rate(&self) -> BigRational
Estimates the current false positive probability.
This approximates the false positive rate as f^k where f is the fill ratio
(proportion of bits set to 1) and k is the number of hash functions.
Returns a BigRational for exact representation and cross-platform determinism.
Sourcepub fn estimated_count(&self) -> BigRational
pub fn estimated_count(&self) -> BigRational
Estimates the number of items that have been inserted.
Uses the formula n = -(m/k) * ln(1 - x/m) where m is the number of bits,
k is the number of hash functions, and x is the number of bits set to 1.
Returns a BigRational using log2_floor for the logarithm computation.
Sourcepub fn optimal_hashers(expected_items: usize, bits: usize) -> u8
pub fn optimal_hashers(expected_items: usize, bits: usize) -> u8
Calculates the optimal number of hash functions for a given capacity and bit count.
Uses BigRational for determinism. The result is clamped to [1, 16] since
beyond ~10-12 hashes provides negligible improvement while increasing CPU cost.
Sourcepub fn optimal_bits(expected_items: usize, fp_rate: &BigRational) -> usize
pub fn optimal_bits(expected_items: usize, fp_rate: &BigRational) -> usize
Calculates the optimal number of bits for a given capacity and false positive rate.
Uses exact rational arithmetic for full determinism across all platforms. The result is rounded up to the next power of 2. If that would overflow, the maximum power of 2 for the platform (2^63 on 64-bit) is used.
Formula: m = -n * log2(p) / ln(2)
§Panics
Panics if fp_rate is not in (0, 1).
Trait Implementations§
Source§impl<H: Clone + Hasher> Clone for BloomFilter<H>
impl<H: Clone + Hasher> Clone for BloomFilter<H>
Source§fn clone(&self) -> BloomFilter<H>
fn clone(&self) -> BloomFilter<H>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<H: Hasher> EncodeSize for BloomFilter<H>
impl<H: Hasher> EncodeSize for BloomFilter<H>
Source§fn encode_size(&self) -> usize
fn encode_size(&self) -> usize
Source§impl<H: Hasher> PartialEq for BloomFilter<H>
impl<H: Hasher> PartialEq for BloomFilter<H>
Source§impl<H: Hasher> Read for BloomFilter<H>
impl<H: Hasher> Read for BloomFilter<H>
Source§impl<H: Hasher> Write for BloomFilter<H>
impl<H: Hasher> Write for BloomFilter<H>
impl<H: Hasher> Eq for BloomFilter<H>
Auto Trait Implementations§
impl<H> Freeze for BloomFilter<H>
impl<H> RefUnwindSafe for BloomFilter<H>where
H: RefUnwindSafe,
impl<H> Send for BloomFilter<H>
impl<H> Sync for BloomFilter<H>
impl<H> Unpin for BloomFilter<H>where
H: Unpin,
impl<H> UnwindSafe for BloomFilter<H>where
H: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Encode for Twhere
T: Write + EncodeSize,
impl<T> Encode for Twhere
T: Write + EncodeSize,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more