BloomFilter

Struct BloomFilter 

Source
pub struct BloomFilter<S = DefaultHasher> { /* private fields */ }
Expand description

A space efficient approximate membership set data structure. False positives from contains are possible, but false negatives are not, i.e. contains for all items in the set is guaranteed to return true, while contains for all items not in the set probably return false.

Self is supported by an underlying bit vector to track item membership. To insert, a number of bits are set at positions based on the item’s hash in the underlying bit vector. To check membership, a number of bits are checked at positions based on the item’s hash in the underlying bit vector.

Once constructed, neither the Bloom filter’s underlying memory usage nor number of bits per item change.

§Examples

Basic usage:

use fastbloom::BloomFilter;

let mut filter = BloomFilter::with_num_bits(1024).expected_items(2);
filter.insert("42");
filter.insert("🦀");

Instantiate with a target false positive rate:

use fastbloom::BloomFilter;

let filter = BloomFilter::with_false_pos(0.001).items(["42", "🦀"]);
assert!(filter.contains("42"));
assert!(filter.contains("🦀"));

Use any hasher:

use fastbloom::BloomFilter;
use ahash::RandomState;

let filter = BloomFilter::with_num_bits(1024)
    .hasher(RandomState::default())
    .items(["42", "🦀"]);

Implementations§

Source§

impl BloomFilter

Source

pub fn with_false_pos(fp: f64) -> BuilderWithFalsePositiveRate

Creates a new builder instance to construct a Self with a target false positive rate of fp. The memory size of the underlying bit vector is dependent on the false positive rate and the expected number of items.

§Panics

Panics if the false positive rate, fp, is 0.

§Examples
use fastbloom::BloomFilter;
let filter = BloomFilter::with_false_pos(0.001).expected_items(1000);
Source

pub fn with_num_bits(num_bits: usize) -> BuilderWithBits

Creates a builder instance to construct a Self with num_bits number of bits for tracking item membership.

§Panics

Panics if the number of bits, num_bits, is 0.

§Examples
use fastbloom::BloomFilter;
let filter = BloomFilter::with_num_bits(1024).hashes(4);
Source

pub fn from_vec(bit_vec: Vec<u64>) -> BuilderWithBits

Creates a builder instance to construct a Self initialized with bit vector bit_vec.

§Panics

Panics if the bit vector, bit_vec, is empty.

§Examples
use fastbloom::BloomFilter;

let orig = BloomFilter::with_false_pos(0.001).seed(&42).items([1, 2]);
let num_hashes = orig.num_hashes();
let new = BloomFilter::from_vec(orig.iter().collect()).seed(&42).hashes(num_hashes);

assert!(new.contains(&1));
assert!(new.contains(&2));
Source§

impl<S: BuildHasher> BloomFilter<S>

Source

pub fn contains(&self, val: &(impl Hash + ?Sized)) -> bool

Checks if an element is possibly in the Bloom filter.

§Returns

true if the item is possibly in the Bloom filter, false otherwise.

§Examples
use fastbloom::BloomFilter;

let bloom = BloomFilter::with_num_bits(1024).items([1, 2, 3]);
assert!(bloom.contains(&1));
Source

pub fn contains_hash(&self, source_hash: u64) -> bool

Checks if the hash of an element is possibly in the Bloom filter. That is the element is pre-hashed and all subsequent hashes are derived from this “source” hash.

§Returns

true if the item is possibly in the Bloom filter, false otherwise.

Source

pub fn num_hashes(&self) -> u32

Returns the number of hashes per item.

Source

pub fn num_bits(&self) -> usize

Returns the total number of in-memory bits supporting the Bloom filter.

Source

pub fn iter(&self) -> impl Iterator<Item = u64> + '_

Returns an iterator over the raw bit values of this Bloom filter.

Source

pub fn as_slice(&self) -> &[u64]

Returns the underlying slice of this Bloom filter’s bit contents.

Source

pub fn source_hash(&self, val: &(impl Hash + ?Sized)) -> u64

Returns the hash of val using this Bloom filter’s hasher. The resulting value can be used in Self::contains_hash or Self::insert_hash. All subsequent hashes are derived from this source hash. This is useful for pre-computing hash values in order to store them or send them over the network.

Source§

impl<S: BuildHasher> BloomFilter<S>

Source

pub fn insert(&mut self, val: &(impl Hash + ?Sized)) -> bool

Inserts an element into the Bloom filter.

§Returns

true if the item may have been previously in the Bloom filter (indicating a potential false positive), false otherwise.

§Examples
use fastbloom::BloomFilter;

let mut bloom = BloomFilter::with_num_bits(1024).hashes(4);
bloom.insert(&2);
assert!(bloom.contains(&2));
Source

pub fn insert_hash(&mut self, hash: u64) -> bool

Inserts the hash of an element into the Bloom filter. That is the element is pre-hashed and all subsequent hashes are derived from this “source” hash.

§Returns

true if the item may have been previously in the Bloom filter (indicating a potential false positive), false otherwise.

Source

pub fn insert_all<T: Hash, I: IntoIterator<Item = T>>(&mut self, iter: I)

Inserts all the items in iter into the self.

Source

pub fn clear(&mut self)

Clear all of the bits in the Bloom filter, removing all items.

Source

pub fn union(&mut self, other: &BloomFilter<S>)

Unions other into self. The hashers of both Bloomfilters must be identical (this is not enforced!).

§Panics

Panics if the other Bloomfilter has a different number of bits or hashes than self.

§Example
use fastbloom::BloomFilter;

let mut bloom = BloomFilter::with_num_bits(4096).seed(&1).hashes(4);
let mut other = BloomFilter::with_num_bits(4096).seed(&1).hashes(4);
bloom.insert_all(0..=1000);
other.insert_all(500..=1500);
bloom.union(&other);

for x in 0..=2000 {
    assert_eq!(bloom.contains(&x), bloom.contains(&x) || other.contains(&x));
}
Source

pub fn intersect(&mut self, other: &BloomFilter<S>)

Intersects other onto self. The hashers of both Bloomfilters must be identical (this is not enforced!).

§Panics

Panics if the other Bloomfilter has a different number of bits or hashes than self.

§Example
use fastbloom::BloomFilter;

let mut bloom = BloomFilter::with_num_bits(4096).seed(&1).hashes(4);
let mut other = BloomFilter::with_num_bits(4096).seed(&1).hashes(4);
bloom.insert_all(0..=1000);
other.insert_all(500..=1500);
bloom.intersect(&other);

for x in 0..=2000 {
    assert_eq!(bloom.contains(&x), bloom.contains(&x) && other.contains(&x));
}

Trait Implementations§

Source§

impl<S: Clone> Clone for BloomFilter<S>

Source§

fn clone(&self) -> BloomFilter<S>

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<S: Debug> Debug for BloomFilter<S>

Source§

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

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

impl<T, S: BuildHasher> Extend<T> for BloomFilter<S>
where T: Hash,

Source§

fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<S: BuildHasher> PartialEq for BloomFilter<S>

Source§

fn eq(&self, other: &Self) -> 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<S: BuildHasher> Eq for BloomFilter<S>

Auto Trait Implementations§

§

impl<S> Freeze for BloomFilter<S>
where S: Freeze,

§

impl<S> RefUnwindSafe for BloomFilter<S>
where S: RefUnwindSafe,

§

impl<S> Send for BloomFilter<S>
where S: Send,

§

impl<S> Sync for BloomFilter<S>
where S: Sync,

§

impl<S> Unpin for BloomFilter<S>
where S: Unpin,

§

impl<S> UnwindSafe for BloomFilter<S>
where S: 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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V