[go: up one dir, main page]

Struct fastbloom::BloomFilter

source ·
pub struct BloomFilter<const BLOCK_SIZE_BITS: usize = 512, 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.

BloomFilter is supported by an underlying bit vector, chunked into 512, 256, 128, or 64 bit “blocks”, to track item membership. To insert, a number of bits are set at positions based on the item’s hash in one of the underlying bit vector’s block. To check membership, a number of bits are checked at positions based on the item’s hash in one of the underlying bit vector’s block.

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

§Examples

Basic usage:

use fastbloom::BloomFilter;

let num_bits = 1024;

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

Instantiate from a collection of items:

use fastbloom::BloomFilter;

let num_bits = 1024;

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

Use any hasher:

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

let num_bits = 1024;

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

Implementations§

source§

impl BloomFilter

source

pub fn builder(num_bits: usize) -> Builder<512>

Creates a new instance of Builder to construct a BloomFilter with num_bits number of bits for tracking item membership.

The BloomFilter built from the returned builder will have a block size of 512 bits.

Use either

for more speed but slightly higher false positive rates.

§Examples
use fastbloom::BloomFilter;

let bloom = BloomFilter::builder(1024).hashes(4);
source§

impl BloomFilter<64, DefaultHasher>

source

pub fn builder_from_bits(num_bits: usize) -> Builder<64>

Creates a new instance of Builder to construct a BloomFilter with num_bits number of bits for tracking item membership.

The BloomFilter built from the returned builder will have a block size of 64 bits.

BloomFilter<64> is faster but less accurate than BloomFilter<128>

§Examples
use fastbloom::BloomFilter;

let bloom = BloomFilter::<64>::builder_from_bits(1024).hashes(4);
source

pub fn builder_from_vec(bit_vec: Vec<u64>) -> Builder<64>

Creates a new instance of Builder to construct a BloomFilter initialized with bit vector bit_vec. The BloomFilter built from the returned builder will have a block size of 64 bits.

§Examples
use fastbloom::BloomFilter;

let bloom = BloomFilter::<64>::builder_from_vec(vec![0x517cc1b727220a95]).hashes(4);
source§

impl BloomFilter<128, DefaultHasher>

source

pub fn builder_from_bits(num_bits: usize) -> Builder<128>

Creates a new instance of Builder to construct a BloomFilter with num_bits number of bits for tracking item membership.

The BloomFilter built from the returned builder will have a block size of 128 bits.

BloomFilter<128> is faster but less accurate than BloomFilter<256>

§Examples
use fastbloom::BloomFilter;

let bloom = BloomFilter::<128>::builder_from_bits(1024).hashes(4);
source

pub fn builder_from_vec(bit_vec: Vec<u64>) -> Builder<128>

Creates a new instance of Builder to construct a BloomFilter initialized with bit vector bit_vec. The BloomFilter built from the returned builder will have a block size of 128 bits. To fit a 128 bit block size, bit_vec will be padded with 0u64 to have a length multiple of 2.

§Examples
use fastbloom::BloomFilter;

let bloom = BloomFilter::<128>::builder_from_vec(vec![0x517cc1b727220a95; 2]).hashes(4);
source§

impl BloomFilter<256, DefaultHasher>

source

pub fn builder_from_bits(num_bits: usize) -> Builder<256>

Creates a new instance of Builder to construct a BloomFilter with num_bits number of bits for tracking item membership.

The BloomFilter built from the returned builder will have a block size of 256 bits.

BloomFilter<256> is faster but less accurate than BloomFilter<512>

§Examples
use fastbloom::BloomFilter;

let bloom = BloomFilter::<256>::builder_from_bits(1024).hashes(4);
source

pub fn builder_from_vec(bit_vec: Vec<u64>) -> Builder<256>

Creates a new instance of Builder to construct a BloomFilter initialized with bit vector bit_vec. The BloomFilter built from the returned builder will have a block size of 256 bits. To fit a 256 bit block size, bit_vec will be padded with 0u64 to have a length multiple of 4.

§Examples
use fastbloom::BloomFilter;

let bloom = BloomFilter::<256>::builder_from_vec(vec![0x517cc1b727220a95; 4]).hashes(4);
source§

impl BloomFilter<512, DefaultHasher>

source

pub fn builder_from_bits(num_bits: usize) -> Builder<512>

Creates a new instance of Builder to construct a BloomFilter with num_bits number of bits for tracking item membership.

The returned BloomFilter has a block size of 512 bits.

Use either

for more speed but slightly higher false positive rates.

§Examples
use fastbloom::BloomFilter;

let bloom = BloomFilter::<512>::builder_from_bits(1024).hashes(4);
source

pub fn builder_from_vec(bit_vec: Vec<u64>) -> Builder<512>

Creates a new instance of Builder to construct a BloomFilter initialized with bit vector bit_vec. The BloomFilter built from the returned builder will have a block size of 512 bits. To fit a 512 bit block size, bit_vec will be padded with 0u64 to have a length multiple of 8.

§Examples
use fastbloom::BloomFilter;

let bloom = BloomFilter::<256>::builder_from_vec(vec![0x517cc1b727220a95; 8]).hashes(4);
source§

impl<const BLOCK_SIZE_BITS: usize, S: BuildHasher> BloomFilter<BLOCK_SIZE_BITS, S>

source

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

Adds a value to the bloom filter. Returns true if the item may have already been a member.

§Examples
use fastbloom::BloomFilter;

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

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

Returns false if the bloom filter definitely does not contain a value. Returns true if the bloom filter may contain a value, with a degree of certainty.

§Examples
use fastbloom::BloomFilter;

let bloom = BloomFilter::builder(1024).items([1, 2, 3]);
assert!(bloom.contains(&1));
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 num_blocks(&self) -> usize

Returns the total number of in-memory blocks supporting the bloom filter. Each block is BLOCK_SIZE_BITS bits.

source

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

Returns a u64 slice of this BloomFilter’s contents.

§Examples
use fastbloom::BloomFilter;

let data = vec![0x517cc1b727220a95; 8];
let bloom = BloomFilter::<512>::builder_from_vec(data.clone()).hashes(4);
assert_eq!(bloom.as_slice().to_vec(), data);
source

pub fn clear(&mut self)

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

Trait Implementations§

source§

impl<const BLOCK_SIZE_BITS: usize, S: Clone> Clone for BloomFilter<BLOCK_SIZE_BITS, S>

source§

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

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<const BLOCK_SIZE_BITS: usize, S: Debug> Debug for BloomFilter<BLOCK_SIZE_BITS, S>

source§

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

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

impl<T, const BLOCK_SIZE_BITS: usize, S: BuildHasher> Extend<T> for BloomFilter<BLOCK_SIZE_BITS, 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<const BLOCK_SIZE_BITS: usize, S: BuildHasher> PartialEq for BloomFilter<BLOCK_SIZE_BITS, S>

source§

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

Auto Trait Implementations§

§

impl<const BLOCK_SIZE_BITS: usize, S> RefUnwindSafe for BloomFilter<BLOCK_SIZE_BITS, S>
where S: RefUnwindSafe,

§

impl<const BLOCK_SIZE_BITS: usize, S> Send for BloomFilter<BLOCK_SIZE_BITS, S>
where S: Send,

§

impl<const BLOCK_SIZE_BITS: usize, S> Sync for BloomFilter<BLOCK_SIZE_BITS, S>
where S: Sync,

§

impl<const BLOCK_SIZE_BITS: usize, S> Unpin for BloomFilter<BLOCK_SIZE_BITS, S>
where S: Unpin,

§

impl<const BLOCK_SIZE_BITS: usize, S> UnwindSafe for BloomFilter<BLOCK_SIZE_BITS, 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> 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,

§

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>,

§

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>,

§

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