Skip to main content

BloomFilter

Struct BloomFilter 

Source
pub struct BloomFilter<T, B = SipHasherBuilder> { /* private fields */ }
Expand description

A space-efficient probabilistic data structure to test for membership in a set.

At its core, a bloom filter is a bit array, initially all set to zero. K hash functions map each element to K bits in the bit array. An element definitely does not exist in the bloom filter if any of the K bits are unset. An element is possibly in the set if all of the K bits are set. This particular implementation of a bloom filter uses two hash functions to simulate K hash functions. Additionally, it operates on only one “slice” in order to have predictable memory usage.

§Examples

use probabilistic_collections::bloom::BloomFilter;

let mut filter = BloomFilter::<String>::new(10, 0.01);

assert!(!filter.contains("foo"));
filter.insert("foo");
assert!(filter.contains("foo"));

filter.clear();
assert!(!filter.contains("foo"));

assert_eq!(filter.len(), 96);
assert_eq!(filter.hasher_count(), 7);

Implementations§

Source§

impl<T> BloomFilter<T>

Source

pub fn new(item_count: usize, fpp: f64) -> Self

Constructs a new, empty BloomFilter with an estimated max capacity of item_count items, and a maximum false positive probability of fpp.

§Examples
use probabilistic_collections::bloom::BloomFilter;

let filter = BloomFilter::<String>::new(10, 0.01);
Source

pub fn from_item_count(bit_count: usize, item_count: usize) -> Self

Constructs a new, empty BloomFilter with bit_count bits, and an estimated max capacity of item_count items.

§Examples
use probabilistic_collections::bloom::BloomFilter;

let filter = BloomFilter::<String>::from_item_count(100, 10);
Source

pub fn from_fpp(bit_count: usize, fpp: f64) -> Self

Constructs a new, empty BloomFilter with bit_count bits, and a maximum false positive probability of fpp.

§Examples
use probabilistic_collections::bloom::BloomFilter;

let filter = BloomFilter::<String>::from_fpp(100, 0.01);
Source§

impl<T, B> BloomFilter<T, B>
where B: BuildHasher,

Source

pub fn with_hashers(item_count: usize, fpp: f64, hash_builders: [B; 2]) -> Self

Constructs a new, empty BloomFilter with an estimated max capacity of item_count items, a maximum false positive probability of fpp, and two hasher builders for double hashing.

§Examples
use probabilistic_collections::bloom::BloomFilter;
use probabilistic_collections::SipHasherBuilder;

let filter = BloomFilter::<String>::with_hashers(
    10,
    0.01,
    [SipHasherBuilder::from_seed(0, 0), SipHasherBuilder::from_seed(1, 1)],
);
Source

pub fn from_item_count_with_hashers( bit_count: usize, item_count: usize, hash_builders: [B; 2], ) -> Self

Constructs a new, empty BloomFilter with bit_count bits, an estimated max capacity of item_count items, and two hasher builders for double hashing.

§Examples
use probabilistic_collections::bloom::BloomFilter;
use probabilistic_collections::SipHasherBuilder;

let filter = BloomFilter::<String>::from_item_count_with_hashers(
    100,
    10,
    [SipHasherBuilder::from_seed(0, 0), SipHasherBuilder::from_seed(1, 1)],
);
Source

pub fn from_fpp_with_hashers( bit_count: usize, fpp: f64, hash_builders: [B; 2], ) -> Self

Constructs a new, empty BloomFilter with bit_count bits, a maximum false positive probability of fpp, and two hasher builders for double hashing.

§Examples
use probabilistic_collections::bloom::BloomFilter;
use probabilistic_collections::SipHasherBuilder;

let filter = BloomFilter::<String>::from_fpp_with_hashers(
    100,
    0.01,
    [SipHasherBuilder::from_seed(0, 0), SipHasherBuilder::from_seed(1, 1)],
);
Source

pub fn insert<U>(&mut self, item: &U)
where T: Borrow<U>, U: Hash + ?Sized,

Inserts an element into the bloom filter.

§Examples
use probabilistic_collections::bloom::BloomFilter;

let mut filter = BloomFilter::<String>::new(10, 0.01);

filter.insert("foo");
Source

pub fn contains<U>(&self, item: &U) -> bool
where T: Borrow<U>, U: Hash + ?Sized,

Checks if an element is possibly in the bloom filter.

§Examples
use probabilistic_collections::bloom::BloomFilter;

let mut filter = BloomFilter::<String>::new(10, 0.01);

assert!(!filter.contains("foo"));
filter.insert("foo");
assert!(filter.contains("foo"));
Source

pub fn len(&self) -> usize

Returns the number of bits in the bloom filter.

§Examples
use probabilistic_collections::bloom::BloomFilter;

let filter = BloomFilter::<String>::from_fpp(100, 0.01);

assert_eq!(filter.len(), 100);
Source

pub fn is_empty(&self) -> bool

Returns true if the bloom filter is empty.

§Examples
use probabilistic_collections::bloom::BloomFilter;

let filter = BloomFilter::<String>::from_fpp(100, 0.01);

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

pub fn hasher_count(&self) -> usize

Returns the number of hash functions used by the bloom filter.

§Examples
use probabilistic_collections::bloom::BloomFilter;

let filter = BloomFilter::<String>::new(10, 0.01);

assert_eq!(filter.hasher_count(), 7);
Source

pub fn clear(&mut self)

Clears the bloom filter, removing all elements.

§Examples
use probabilistic_collections::bloom::BloomFilter;

let mut filter = BloomFilter::<String>::new(10, 0.01);

filter.insert("foo");
filter.clear();

assert!(!filter.contains("foo"));
Source

pub fn count_ones(&self) -> usize

Returns the number of set bits in the bloom filter.

§Examples
use probabilistic_collections::bloom::BloomFilter;
use probabilistic_collections::SipHasherBuilder;

let mut filter = BloomFilter::<String>::from_fpp_with_hashers(
    100,
    0.01,
    [SipHasherBuilder::from_seed(0, 0), SipHasherBuilder::from_seed(1, 1)],
);
filter.insert("foo");

assert_eq!(filter.count_ones(), 7);
Source

pub fn count_zeros(&self) -> usize

Returns the number of unset bits in the bloom filter.

§Examples
use probabilistic_collections::bloom::BloomFilter;
use probabilistic_collections::SipHasherBuilder;

let mut filter = BloomFilter::<String>::from_fpp_with_hashers(
    100,
    0.01,
    [SipHasherBuilder::from_seed(0, 0), SipHasherBuilder::from_seed(1, 1)],
);
filter.insert("foo");

assert_eq!(filter.count_zeros(), 93);
Source

pub fn estimated_fpp(&self) -> f64

Returns the estimated false positive probability of the bloom filter. This value will increase as more items are added.

§Examples
use probabilistic_collections::bloom::BloomFilter;

let mut filter = BloomFilter::<String>::new(100, 0.01);
assert!(filter.estimated_fpp() < std::f64::EPSILON);

filter.insert("foo");
assert!(filter.estimated_fpp() > std::f64::EPSILON);
assert!(filter.estimated_fpp() < 0.01);
Source

pub fn hashers(&self) -> &[B; 2]

Returns a reference to the bloom filter’s hasher builders.

§Examples
use probabilistic_collections::bloom::BloomFilter;

let filter = BloomFilter::<String>::new(10, 0.01);
let hashers = filter.hashers();

Trait Implementations§

Source§

impl<T: Debug, B: Debug> Debug for BloomFilter<T, B>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T: PartialEq, B: PartialEq> PartialEq for BloomFilter<T, B>

Source§

fn eq(&self, other: &BloomFilter<T, B>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T, B> StructuralPartialEq for BloomFilter<T, B>

Auto Trait Implementations§

§

impl<T, B> Freeze for BloomFilter<T, B>
where B: Freeze,

§

impl<T, B> RefUnwindSafe for BloomFilter<T, B>

§

impl<T, B> Send for BloomFilter<T, B>
where T: Send, B: Send,

§

impl<T, B> Sync for BloomFilter<T, B>
where T: Sync, B: Sync,

§

impl<T, B> Unpin for BloomFilter<T, B>
where T: Unpin, B: Unpin,

§

impl<T, B> UnsafeUnpin for BloomFilter<T, B>
where B: UnsafeUnpin,

§

impl<T, B> UnwindSafe for BloomFilter<T, B>
where T: UnwindSafe, B: 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> 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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V