rust_dsa

Struct BloomFilter

Source
pub struct BloomFilter<T, const M: usize, const K: usize, S = RandomState> { /* private fields */ }
Expand description

A Bloom filter.

Note that the number of bits in the Bloom filter is 64 times M because bits are stored in an array of u64’s.

§Example

use rust_dsa::BloomFilter;

// First, we create a new Bloom filter.
let mut filter: BloomFilter<_, 32, 2> = BloomFilter::new();

// We can insert some values.
filter.insert('a');
filter.insert('b');
filter.insert('c');

// We can check if the set probably contains values.
assert!(filter.maybe_contains(&'a'));
assert!(filter.maybe_contains(&'b'));
assert!(filter.maybe_contains(&'c'));

// For values not in the set, we may get false positives
// assert!(!filter.maybe_contains(&'z'));

// We can estimate the false positive rate
assert!(filter.false_positive_rate() < 1e-5);

Implementations§

Source§

impl<T, const M: usize, const K: usize, S> BloomFilter<T, M, K, S>

Source

pub fn new() -> Self
where S: Default,

Creates an empty Bloom filter.

§Panics

Panics if M or K is zero.

Source

pub fn with_hashers(hash_builders: [S; K]) -> Self

Creates an empty Bloom filter which will use the given hash builders.

§Panics

Panics if M or K is zero.

Source

pub fn is_empty(&self) -> bool

Checks if a Bloom filter is empty (i.e., if all the bits are zero).

§Example
use rust_dsa::BloomFilter;

let mut filter: BloomFilter::<_, 4, 2> = BloomFilter::new();
assert!(filter.is_empty());

filter.insert(1);
assert!(!filter.is_empty());
Source

pub fn clear(&mut self)

Sets all the bits to zero.

§Example
use rust_dsa::BloomFilter;

let mut filter: BloomFilter::<_, 8, 4> = "hello".chars().collect();
assert!(!filter.is_empty());

filter.clear();
assert!(filter.is_empty());
Source

pub fn count_ones(&self) -> usize

Counts the number of set bits in the Bloom filter.

§Example
use rust_dsa::BloomFilter;

let mut filter: BloomFilter::<_, 8, 4> = BloomFilter::from([1, 2, 3]);
assert!(filter.count_ones() > 0);

filter.clear();
assert_eq!(filter.count_ones(), 0);
Source

pub fn epsilon(&self, n: usize) -> f64

Estimates the false positive rate of this Bloom filter if it had n elements in it.

§Example
use rust_dsa::BloomFilter;

let filter: BloomFilter<char, 8, 4> = BloomFilter::new();

let expected = 0.15966130015118526;
assert!((filter.epsilon(128) - expected).abs() < 1e-10);
Source

pub fn false_positive_rate(&self) -> f64

Estimates the false positive rate of this Bloom filter based on how many bits are set.

§Example
use rust_dsa::BloomFilter;

let filter: BloomFilter<_, 8, 2> = "abcdefgh".chars().collect();

let expected = 0.0009765625;
assert!((filter.false_positive_rate() - expected) < 1e-10);
Source

pub fn approx_len(&self) -> f64

Estimates the number of items in this Bloom filter based on how many bits are set.

§Example
use rust_dsa::BloomFilter;

let filter: BloomFilter<_, 16, 4> = BloomFilter::from(["foo", "bar", "baz"]);

assert_eq!(filter.approx_len().round(), 3.0);
Source§

impl<T, const M: usize, const K: usize, S> BloomFilter<T, M, K, S>
where T: Hash, S: BuildHasher,

Source

pub fn insert(&mut self, value: T)

Inserts a value into the Bloom filter.

§Example
use rust_dsa::BloomFilter;

let mut filter: BloomFilter<_, 10, 4> = BloomFilter::new();

assert!(!filter.maybe_contains(&1)); // guaranteed to return false

filter.insert(1);
filter.insert(2);
filter.insert(3);

// guaranteed to return true
assert!(filter.maybe_contains(&1));
assert!(filter.maybe_contains(&2));
assert!(filter.maybe_contains(&3));

// filter.maybe_contains(4); // may return true or false
Source

pub fn maybe_contains(&mut self, value: &T) -> bool

Queries the Bloom filter for a given value.

If the Bloom filter contains the value, this function is guaranteed to return true, but it may return false positives.

§Example
use rust_dsa::BloomFilter;

let mut filter: BloomFilter<_, 8, 2> = BloomFilter::new();
assert!(!filter.maybe_contains(&1));

filter.insert(1);
assert!(filter.maybe_contains(&1));

Trait Implementations§

Source§

impl<T: Clone, const M: usize, const K: usize, S: Clone> Clone for BloomFilter<T, M, K, S>

Source§

fn clone(&self) -> BloomFilter<T, M, K, S>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T, const M: usize, const K: usize, S> Default for BloomFilter<T, M, K, S>
where S: Default,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T, const M: usize, const K: usize, S> Extend<T> for BloomFilter<T, M, K, S>
where T: Hash, S: BuildHasher + Default,

Source§

fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<T, const M: usize, const K: usize, S, const N: usize> From<[T; N]> for BloomFilter<T, M, K, S>
where T: Hash, S: BuildHasher + Default,

Source§

fn from(array: [T; N]) -> Self

Converts to this type from the input type.
Source§

impl<T, const M: usize, const K: usize, S> FromIterator<T> for BloomFilter<T, M, K, S>
where T: Hash, S: BuildHasher + Default,

Source§

fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self

Creates a value from an iterator. Read more

Auto Trait Implementations§

§

impl<T, const M: usize, const K: usize, S> Freeze for BloomFilter<T, M, K, S>
where S: Freeze,

§

impl<T, const M: usize, const K: usize, S> RefUnwindSafe for BloomFilter<T, M, K, S>

§

impl<T, const M: usize, const K: usize, S> Send for BloomFilter<T, M, K, S>
where S: Send, T: Send,

§

impl<T, const M: usize, const K: usize, S> Sync for BloomFilter<T, M, K, S>
where S: Sync, T: Sync,

§

impl<T, const M: usize, const K: usize, S> Unpin for BloomFilter<T, M, K, S>
where S: Unpin, T: Unpin,

§

impl<T, const M: usize, const K: usize, S> UnwindSafe for BloomFilter<T, M, K, S>
where S: UnwindSafe, T: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.