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>
impl<H, B, T> Bloom2<H, B, T>
Sourcepub fn insert(&mut self, data: &T)
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));Sourcepub fn contains(&self, data: &T) -> bool
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.
Sourcepub fn union(&mut self, other: &Self)
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ยงimpl<H, T> Bloom2<H, CompressedBitmap, T>where
H: BuildHasher,
impl<H, T> Bloom2<H, CompressedBitmap, T>where
H: BuildHasher,
Sourcepub fn shrink_to_fit(&mut self)
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,
impl<H, T> Bloom2<H, VecBitmap, T>where
H: BuildHasher,
Sourcepub fn compress(self) -> Bloom2<H, CompressedBitmap, T>
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<T> Default for Bloom2<RandomState, CompressedBitmap, T>where
T: Hash,
Initialise a Bloom2 instance using the default implementation of
BloomFilterBuilder.
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();