pub struct CountingBloomFilter { /* private fields */ }Expand description
A counting Bloom filter that supports removal of items.
Uses 4-bit counters (0-15) for each position, allowing items to be inserted multiple times and removed. This uses 4x more memory than a standard Bloom filter but enables deletion.
§Example
use rustywallet_bloom::CountingBloomFilter;
let mut filter = CountingBloomFilter::new(10_000, 0.01);
filter.insert("address1");
filter.insert("address2");
assert!(filter.contains("address1"));
filter.remove("address1").unwrap();
assert!(!filter.contains("address1"));
assert!(filter.contains("address2"));Implementations§
Source§impl CountingBloomFilter
impl CountingBloomFilter
Sourcepub fn new(expected_items: usize, false_positive_rate: f64) -> Self
pub fn new(expected_items: usize, false_positive_rate: f64) -> Self
Creates a new counting Bloom filter optimized for the expected number of items and desired false positive rate.
§Arguments
expected_items- Expected number of items to insertfalse_positive_rate- Desired false positive rate (e.g., 0.01 for 1%)
§Example
use rustywallet_bloom::CountingBloomFilter;
// 100,000 items with 1% false positive rate
let filter = CountingBloomFilter::new(100_000, 0.01);Sourcepub fn with_params(num_counters: usize, num_hashes: usize) -> Self
pub fn with_params(num_counters: usize, num_hashes: usize) -> Self
Creates a counting Bloom filter with explicit parameters.
§Arguments
num_counters- Total number of counter positionsnum_hashes- Number of hash functions to use
Sourcepub fn insert<T: AsRef<[u8]>>(&mut self, item: T)
pub fn insert<T: AsRef<[u8]>>(&mut self, item: T)
Inserts an item into the filter by incrementing relevant counters.
Returns Ok(()) on success, or Err(CountingBloomError::CounterOverflow)
if any counter would exceed the maximum value (15).
§Example
use rustywallet_bloom::CountingBloomFilter;
let mut filter = CountingBloomFilter::new(1000, 0.01);
filter.insert("item1");
assert!(filter.contains("item1"));Sourcepub fn try_insert<T: AsRef<[u8]>>(
&mut self,
item: T,
) -> Result<(), CountingBloomError>
pub fn try_insert<T: AsRef<[u8]>>( &mut self, item: T, ) -> Result<(), CountingBloomError>
Inserts an item with overflow checking.
Returns Err(CountingBloomError::CounterOverflow) if any counter
would exceed the maximum value.
Sourcepub fn remove<T: AsRef<[u8]>>(
&mut self,
item: T,
) -> Result<(), CountingBloomError>
pub fn remove<T: AsRef<[u8]>>( &mut self, item: T, ) -> Result<(), CountingBloomError>
Removes an item from the filter by decrementing relevant counters.
Returns Ok(()) on success, or Err(CountingBloomError::CounterUnderflow)
if any counter would go below zero (item not in filter).
§Warning
Removing an item that was never inserted can corrupt the filter, causing false negatives for other items.
§Example
use rustywallet_bloom::CountingBloomFilter;
let mut filter = CountingBloomFilter::new(1000, 0.01);
filter.insert("item1");
assert!(filter.contains("item1"));
filter.remove("item1").unwrap();
assert!(!filter.contains("item1"));Sourcepub fn contains<T: AsRef<[u8]>>(&self, item: T) -> bool
pub fn contains<T: AsRef<[u8]>>(&self, item: T) -> bool
Checks if an item might be in the filter.
Returns false if the item is definitely not in the set.
Returns true if the item is probably in the set (may be false positive).
Sourcepub fn count_estimate<T: AsRef<[u8]>>(&self, item: T) -> u8
pub fn count_estimate<T: AsRef<[u8]>>(&self, item: T) -> u8
Returns the approximate count for an item.
This returns the minimum counter value across all hash positions, which approximates how many times the item was inserted.
Sourcepub fn memory_usage(&self) -> usize
pub fn memory_usage(&self) -> usize
Returns the memory usage in bytes.
Sourcepub fn num_counters(&self) -> usize
pub fn num_counters(&self) -> usize
Returns the number of counter positions.
Sourcepub fn num_hashes(&self) -> usize
pub fn num_hashes(&self) -> usize
Returns the number of hash functions used.