BloomFilter

Struct BloomFilter 

Source
pub struct BloomFilter { /* private fields */ }
Expand description

A space-efficient probabilistic data structure for set membership testing.

Bloom filters can have false positives but never false negatives. If contains() returns false, the item is definitely not in the set. If contains() returns true, the item is probably in the set.

§Memory Usage

For a given number of items n and false positive rate p:

  • Optimal bits: m = -n * ln(p) / (ln(2)^2)n * 9.6 for 1% FPR
  • Optimal hashes: k = (m/n) * ln(2) ≈ 7 for 1% FPR

§Example

use rustywallet_bloom::BloomFilter;

let mut filter = BloomFilter::new(100_000, 0.01);
filter.insert("address1");
assert!(filter.contains("address1"));

Implementations§

Source§

impl BloomFilter

Source

pub fn new(expected_items: usize, false_positive_rate: f64) -> Self

Creates a new 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::BloomFilter;

// 1 million items with 1% false positive rate
let filter = BloomFilter::new(1_000_000, 0.01);
Source

pub fn with_params(num_bits: usize, num_hashes: usize) -> Self

Creates a Bloom filter with explicit parameters.

Use this if you need precise control over the filter size.

§Arguments
  • num_bits - Total number of bits in the filter
  • 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.

After insertion, contains() will always return true for this item.

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 len(&self) -> usize

Returns the number of items inserted.

Source

pub fn is_empty(&self) -> bool

Returns true if no items have been inserted.

Source

pub fn memory_usage(&self) -> usize

Returns the memory usage in bytes.

Source

pub fn num_bits(&self) -> usize

Returns the number of bits in the filter.

Source

pub fn num_hashes(&self) -> usize

Returns the number of hash functions used.

Source

pub fn estimated_fpr(&self) -> f64

Returns the estimated false positive rate based on current fill.

Source

pub fn clear(&mut self)

Clears all items from the filter.

Trait Implementations§

Source§

impl Debug for BloomFilter

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.