[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 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<512>

Creates a new instance of BuilderWithFalsePositiveRate to construct a BloomFilter 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 bloom = BloomFilter::with_false_pos(0.001).expected_items(1000);
source

pub fn with_num_bits(num_bits: usize) -> BuilderWithBits<512>

Creates a new instance of BuilderWithBits to construct a BloomFilter 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 bloom = BloomFilter::with_num_bits(1024).hashes(4);
source

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

Creates a new instance of BuilderWithBits to construct a BloomFilter initialized with bit vector bit_vec.

To fit the bit block size, bit_vec will be padded with 0u64s and the end.

§Panics

Panics if the bit vector, bit_vec, is empty.

§Examples
use fastbloom::BloomFilter;
let bloom = BloomFilter::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

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

Auto Trait Implementations§

§

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

§

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