CountingBloomFilter

Struct CountingBloomFilter 

Source
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

Source

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 insert
  • false_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);
Source

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 positions
  • num_hashes - Number of hash functions to use
Source

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"));
Source

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.

Source

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"));
Source

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).

Source

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.

Source

pub fn len(&self) -> usize

Returns the number of items inserted (minus removed).

Source

pub fn is_empty(&self) -> bool

Returns true if no items are in the filter.

Source

pub fn memory_usage(&self) -> usize

Returns the memory usage in bytes.

Source

pub fn num_counters(&self) -> usize

Returns the number of counter positions.

Source

pub fn num_hashes(&self) -> usize

Returns the number of hash functions used.

Source

pub fn clear(&mut self)

Clears all items from the filter.

Trait Implementations§

Source§

impl Debug for CountingBloomFilter

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.