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,
impl<H, B, T> Bloom2<H, B, T>where H: BuildHasher, B: Bitmap, T: Hash,
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));
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.
Trait Implementations§
source§impl<H, B, T: Clone> Clone for Bloom2<H, B, T>where
H: BuildHasher + Clone,
B: Bitmap + Clone,
impl<H, B, T: Clone> Clone for Bloom2<H, B, T>where H: BuildHasher + Clone, B: Bitmap + Clone,
source§impl<H, B, T: Debug> Debug for Bloom2<H, B, T>where
H: BuildHasher + Debug,
B: Bitmap + Debug,
impl<H, B, T: Debug> Debug for Bloom2<H, B, T>where H: BuildHasher + Debug, B: Bitmap + Debug,
source§impl<T> Default for Bloom2<RandomState, CompressedBitmap, T>where
T: Hash,
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();