Bloom2

Struct 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 union(&mut self, other: &Self)

Union two Bloom2 instances (of identical configuration), returning the merged combination of both.

The returned filter will return โ€œtrueโ€ for all calls to Bloom2::contains() for all values that would return true for one (or both) of the inputs, and will return โ€œfalseโ€ for all values that return false from both inputs.

ยงPanics

This method panics if the two Bloom2 instances have different configuration.

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.

Sourceยง

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

Source

pub fn compress(self) -> Bloom2<H, CompressedBitmap, T>

Compress the bitmap to reduce memory consumption.

The compressed representation is optimised for reads, but subsequent inserts will be slower. This reduction is O(n) in time, and up to O(2n) in space.

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 duplicate 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<'de, H, B, T> Deserialize<'de> for Bloom2<H, B, T>
where H: BuildHasher + Default, B: Bitmap + Deserialize<'de>,

Sourceยง

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Sourceยง

impl<H, T> From<Bloom2<H, VecBitmap, T>> for Bloom2<H, CompressedBitmap, T>
where H: BuildHasher,

Sourceยง

fn from(v: Bloom2<H, VecBitmap, T>) -> Self

Converts to this type from the input type.
Sourceยง

impl<H, B, T: PartialEq> PartialEq for Bloom2<H, B, T>

Sourceยง

fn eq(&self, other: &Bloom2<H, B, T>) -> 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<H, B, T> Serialize for Bloom2<H, B, T>
where H: BuildHasher, B: Bitmap + Serialize,

Sourceยง

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Sourceยง

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

Auto Trait Implementationsยง

ยง

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

ยง

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

ยง

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

ยง

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

ยง

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

ยง

impl<H, B, T> UnwindSafe for Bloom2<H, B, T>
where H: UnwindSafe, B: 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, dest: *mut u8)

๐Ÿ”ฌThis is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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.
Sourceยง

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,