BloomFilter

Struct BloomFilter 

Source
pub struct BloomFilter<T: AsRef<[u8]>> {
    pub bits: Vec<u8>,
    /* private fields */
}
Expand description

Defines a bloom filter for items of a given type provided a capacity and a desired false positive rate.

Fields§

§bits: Vec<u8>

Implementations§

Source§

impl<T: AsRef<[u8]>> BloomFilter<T>

Source

pub fn new(capacity: u32, desired_fp_rate: f32) -> BloomFilter<T>

Creates a new bloom filter using the package’s default hasher with a specified capacity and desired false positive rate. In order to customize the bloom filter further, such as using a custom hash function, use the BloomBuilder struct instead.

§Example
use flowerbloom::BloomFilter;
let capacity = 1000;
let desired_fp_rate = 0.01;
let mut bf = BloomFilter::new(capacity, desired_fp_rate);

bf.insert("hello");
bf.insert("world");

if !bf.has("nyan") {
    println!("definitely not in the bloom filter");
}
Source

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

Insert an element into the bloom filter

§Example
use flowerbloom::BloomFilter;
let capacity = 1000;
let desired_fp_rate = 0.01;
let mut bf = BloomFilter::new(capacity, desired_fp_rate);

bf.insert("foo");
bf.insert("bar");
bf.insert("baz");
Source

pub fn has(&self, elem: T) -> bool

Checks if the bloom filter contains a specified element. The bloom filter can produce false positives from this function at the rate specified upon the struct’s creation. It will never produce false negatives, however.

§Example
use flowerbloom::{BloomBuilder, BloomFilter};

/// Initialize a bloom filter with a default hasher over strings.
let capacity: u32 = 50;
let desired_fp_rate: f32 = 0.03;
let mut bf: BloomFilter<&str> = BloomBuilder::new(capacity, desired_fp_rate)
                .build();

bf.insert("foo");
bf.insert("bar");
bf.insert("baz");

if !bf.has("nyan") {
    println!("definitely not in the bloom filter");
}
Source

pub fn clear(&mut self)

Clear all set bits of the bloom filter, setting them back to zero.

Trait Implementations§

Source§

impl<T: AsRef<[u8]>> Display for BloomFilter<T>

Displays the bloom filter as a lowercase hex string.

Source§

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

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

impl<T: AsRef<[u8]>> FromIterator<T> for BloomFilter<T>

Converts an iterator into a bloom filter with a default hasher and sensible false positive rate of 0.03.

§Example
use flowerbloom::{BloomFilter};

let items = vec!["foo", "bar", "baz"];
let bf: BloomFilter<&str> = items.into_iter().collect();
let _ = bf.has("nyan");
Source§

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

Creates a value from an iterator. Read more

Auto Trait Implementations§

§

impl<T> Freeze for BloomFilter<T>

§

impl<T> RefUnwindSafe for BloomFilter<T>

§

impl<T> Send for BloomFilter<T>

§

impl<T> Sync for BloomFilter<T>

§

impl<T> Unpin for BloomFilter<T>

§

impl<T> UnwindSafe for BloomFilter<T>

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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.