Skip to main content

PartitionedBloomFilter

Struct PartitionedBloomFilter 

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

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

This particular implementation of a bloom filter uses K partitions and K hash functions. Each hash function maps to a bit in its respective partition. A partitioned bloom filter is more robust than its traditional counterpart, but the memory usage is varies based on how many hash functions you are using.

§Examples

use probabilistic_collections::bloom::PartitionedBloomFilter;

let mut filter = PartitionedBloomFilter::<String>::from_item_count(10, 0.01);

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

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

assert_eq!(filter.len(), 98);
assert_eq!(filter.bit_count(), 14);
assert_eq!(filter.hasher_count(), 7);

Implementations§

Source§

impl<T> PartitionedBloomFilter<T>

Source

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

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

§Examples
use probabilistic_collections::bloom::PartitionedBloomFilter;

let filter = PartitionedBloomFilter::<String>::from_item_count(10, 0.01);
Source

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

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

§Examples
use probabilistic_collections::bloom::PartitionedBloomFilter;

let filter = PartitionedBloomFilter::<String>::from_bit_count(10, 0.01);
Source§

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

Source

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

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

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

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

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

Constructs a new, empty PartitionedBloomFilter with bit_count bits per partition, a maximum false positive probability of fpp, and two hash builders for double hashing.

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

let filter = PartitionedBloomFilter::<String>::from_bit_count_with_hashers(
    10,
    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::PartitionedBloomFilter;

let mut filter = PartitionedBloomFilter::<String>::from_item_count(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::PartitionedBloomFilter;

let mut filter = PartitionedBloomFilter::<String>::from_item_count(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::PartitionedBloomFilter;

let filter = PartitionedBloomFilter::<String>::from_item_count(10, 0.01);

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

pub fn is_empty(&self) -> bool

Returns true if the bloom filter is empty.

§Examples
use probabilistic_collections::bloom::PartitionedBloomFilter;

let filter = PartitionedBloomFilter::<String>::from_item_count(10, 0.01);

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

pub fn bit_count(&self) -> usize

Returns the number of bits in each partition in the bloom filter.

§Examples
use probabilistic_collections::bloom::PartitionedBloomFilter;

let filter = PartitionedBloomFilter::<String>::from_item_count(10, 0.01);

assert_eq!(filter.bit_count(), 14);
Source

pub fn hasher_count(&self) -> usize

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

§Examples
use probabilistic_collections::bloom::PartitionedBloomFilter;

let filter = PartitionedBloomFilter::<String>::from_item_count(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::PartitionedBloomFilter;

let mut filter = PartitionedBloomFilter::<String>::from_item_count(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::PartitionedBloomFilter;
use probabilistic_collections::SipHasherBuilder;

let mut filter = PartitionedBloomFilter::<String>::from_item_count_with_hashers(
    10,
    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::PartitionedBloomFilter;
use probabilistic_collections::SipHasherBuilder;

let mut filter = PartitionedBloomFilter::<String>::from_item_count_with_hashers(
    10,
    0.01,
    [SipHasherBuilder::from_seed(0, 0), SipHasherBuilder::from_seed(1, 1)],
);
filter.insert("foo");

assert_eq!(filter.count_zeros(), 91);
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.

This is a fairly rough estimate as it takes the overall fill ratio of all partitions instead of considering each partition individually.

§Examples
use probabilistic_collections::bloom::PartitionedBloomFilter;

let mut filter = PartitionedBloomFilter::<String>::from_item_count(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::PartitionedBloomFilter;

let filter = PartitionedBloomFilter::<String>::from_item_count(100, 0.01);
let hashers = filter.hashers();

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

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

§

impl<T, B> UnwindSafe for PartitionedBloomFilter<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