Struct bloom2::Bloom2

source ·
pub struct Bloom2<H, B, T>where
    H: BuildHasher,
    B: Bitmap,{ /* private fields */ }
Expand description

A fast, memory efficient, sparse bloom filter.

Most users can quickly initialise a Bloom2 instance by calling Bloom2::default() and start inserting anything that implements the Hash trait:

use bloom2::Bloom2;

let mut b = Bloom2::default();
b.insert(&"hello 🐐");
assert!(b.contains(&"hello 🐐"));

Initialising a Bloom2 this way uses some sensible default values for a easy-to-use, memory efficient filter. If you want to tune the probability of a false-positive lookup, change the hashing algorithm, memory size of the filter, etc, a BloomFilterBuilder can be used to initialise a Bloom2 instance with the desired properties.

The sparse nature of this filter trades a small amount of insert performance for decreased memory usage. For filters initialised infrequently and held for a meaningful duration of time, this is almost always worth the marginally increased insert latency. When testing performance, be sure to use a release build - there’s a significant performance difference!

Implementations§

source§

impl<H, B, T> Bloom2<H, B, T>where H: BuildHasher, B: Bitmap, T: Hash,

source

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

Insert places data into the bloom filter.

Any subsequent calls to contains for the same data will always return true.

Insertion is significantly faster in release builds.

The data provided can be anything that implements the Hash trait, for example:

use bloom2::Bloom2;

let mut b = Bloom2::default();
b.insert(&"hello 🐐");
assert!(b.contains(&"hello 🐐"));

let mut b = Bloom2::default();
b.insert(&vec!["fox", "cat", "banana"]);
assert!(b.contains(&vec!["fox", "cat", "banana"]));

let mut b = Bloom2::default();
let data: [u8; 4] = [1, 2, 3, 42];
b.insert(&data);
assert!(b.contains(&data));

As well as structs if they implement the Hash trait, which be helpfully derived:

#[derive(Hash)]
struct User {
    id: u64,
    email: String,
}

let user = User{
    id: 42,
    email: "dom@itsallbroken.com".to_string(),
};

b.insert(&&user);
assert!(b.contains(&&user));
source

pub fn contains(&self, data: &T) -> bool

Checks if data exists in the filter.

If contains returns true, hash has probably been inserted previously. If contains returns false, hash has definitely not been inserted into the filter.

source

pub fn byte_size(&mut self) -> usize

Return the byte size of this filter.

source§

impl<H, T> Bloom2<H, CompressedBitmap, T>where H: BuildHasher,

source

pub fn shrink_to_fit(&mut self)

Minimise the memory usage of this instance by by shrinking the underlying vectors, discarding their excess capacity.

Trait Implementations§

source§

impl<H, B, T: Clone> Clone for Bloom2<H, B, T>where H: BuildHasher + Clone, B: Bitmap + Clone,

source§

fn clone(&self) -> Bloom2<H, B, T>

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<H, B, T: Debug> Debug for Bloom2<H, B, T>where H: BuildHasher + Debug, B: Bitmap + Debug,

source§

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

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

impl<T> Default for Bloom2<RandomState, CompressedBitmap, T>where T: Hash,

Initialise a Bloom2 instance using the default implementation of BloomFilterBuilder.

It is the equivalent of:

use bloom2::BloomFilterBuilder;

let mut b = BloomFilterBuilder::default().build();
source§

fn default() -> Self

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

impl<H, B, T: PartialEq> PartialEq<Bloom2<H, B, T>> for Bloom2<H, B, T>where H: BuildHasher + PartialEq, B: Bitmap + PartialEq,

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<H, B, T> StructuralPartialEq for Bloom2<H, B, T>where H: BuildHasher, B: Bitmap,

Auto Trait Implementations§

§

impl<H, B, T> RefUnwindSafe for Bloom2<H, B, T>where B: RefUnwindSafe, H: RefUnwindSafe, T: RefUnwindSafe,

§

impl<H, B, T> Send for Bloom2<H, B, T>where B: Send, H: Send, T: Send,

§

impl<H, B, T> Sync for Bloom2<H, B, T>where B: Sync, H: Sync, T: Sync,

§

impl<H, B, T> Unpin for Bloom2<H, B, T>where B: Unpin, H: Unpin, T: Unpin,

§

impl<H, B, T> UnwindSafe for Bloom2<H, B, T>where B: UnwindSafe, H: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.