Skip to main content

CuckooFilter

Struct CuckooFilter 

Source
pub struct CuckooFilter<T: ?Sized, S = DefaultHashBuilder> { /* private fields */ }
Available on crate feature alloc only.
Expand description

A probabilistic set that supports deletion.

Like a BloomFilter, a Cuckoo filter answers approximate membership queries in a fraction of the memory an exact set would need, and never reports a false negative for an item that is present. Unlike a Bloom filter it supports remove: items can be deleted without rebuilding the structure.

The filter stores a short fingerprint of each item (16 bits here) in one of two candidate buckets, relocating existing fingerprints as needed to make room — the “cuckoo” behaviour. The false-positive rate is fixed by the fingerprint width at roughly 0.012%; for a tunable rate without deletion, use a BloomFilter.

The filter is generic over the item type T and a BuildHasher S, defaulting to the deterministic DefaultHashBuilder.

§Examples

use bloom_lib::CuckooFilter;

let mut filter = CuckooFilter::new(1_000).unwrap();

filter.insert("alice").unwrap();
assert!(filter.contains("alice"));

assert!(filter.remove("alice"));
assert!(!filter.contains("alice"));

Implementations§

Source§

impl<T: ?Sized> CuckooFilter<T, DefaultHashBuilder>

Source

pub fn new(capacity: usize) -> Result<Self, Error>

Creates a filter able to hold roughly capacity items, using the default hasher.

The number of buckets is rounded up to a power of two with headroom, so the reported capacity is at least capacity and usually larger. Inserting close to the reported capacity may begin to fail with Error::CapacityExceeded; size with margin for write-heavy workloads.

§Parameters
  • capacity: the expected number of distinct items. Must be non-zero.
§Errors

Returns Error::InvalidParameter if capacity is zero.

§Examples
use bloom_lib::CuckooFilter;

let filter = CuckooFilter::<&str>::new(10_000).unwrap();
assert!(filter.capacity() >= 10_000);
assert!(filter.is_empty());
Source§

impl<T: ?Sized, S: BuildHasher> CuckooFilter<T, S>

Source

pub fn with_hasher(capacity: usize, hasher: S) -> Result<Self, Error>

Creates a filter for roughly capacity items with a caller-supplied hasher.

§Errors

Returns Error::InvalidParameter if capacity is zero.

§Examples
use std::collections::hash_map::RandomState;
use bloom_lib::CuckooFilter;

let filter: CuckooFilter<&str, RandomState> =
    CuckooFilter::with_hasher(1_000, RandomState::new()).unwrap();
Source

pub fn insert(&mut self, item: &T) -> Result<(), Error>
where T: Hash,

Inserts item into the filter.

Duplicates are permitted: inserting the same item twice stores two fingerprints and requires two remove calls to fully evict. This matches the semantics of the original Cuckoo filter and keeps remove exact with respect to insertions.

§Errors

Returns Error::CapacityExceeded when the filter is too full to place the item within its relocation budget. The filter is unchanged in that case and remains usable for lookups and deletions.

§Examples
use bloom_lib::CuckooFilter;

let mut filter = CuckooFilter::new(100).unwrap();
filter.insert(&42u32).unwrap();
assert!(filter.contains(&42u32));
Source

pub fn contains(&self, item: &T) -> bool
where T: Hash,

Tests whether item is in the filter.

Returns false only if the item was definitely never inserted (or has since been removed). A return of true means the item is probably present, with a false-positive probability of roughly 0.012%.

§Examples
use bloom_lib::CuckooFilter;

let mut filter = CuckooFilter::new(100).unwrap();
filter.insert("present").unwrap();
assert!(filter.contains("present"));
assert!(!filter.contains("absent"));
Source

pub fn remove(&mut self, item: &T) -> bool
where T: Hash,

Removes one occurrence of item, returning true if it was present.

Only a single stored fingerprint is removed per call, mirroring insert. Because fingerprints collide, removing an item that was never inserted but collides with a present one can delete the wrong fingerprint; this is the same false-positive trade-off that governs contains. Only delete items you previously inserted.

§Examples
use bloom_lib::CuckooFilter;

let mut filter = CuckooFilter::new(100).unwrap();
filter.insert("temp").unwrap();
assert!(filter.remove("temp"));
assert!(!filter.remove("temp")); // already gone
Source

pub fn clear(&mut self)

Removes every item, retaining the allocation.

§Examples
use bloom_lib::CuckooFilter;

let mut filter = CuckooFilter::new(100).unwrap();
filter.insert("x").unwrap();
filter.clear();
assert!(filter.is_empty());
Source

pub fn len(&self) -> usize

The number of fingerprints currently stored.

§Examples
use bloom_lib::CuckooFilter;

let mut filter = CuckooFilter::new(100).unwrap();
filter.insert("a").unwrap();
filter.insert("b").unwrap();
assert_eq!(filter.len(), 2);
Source

pub fn is_empty(&self) -> bool

Returns true if the filter holds no items.

Source

pub fn capacity(&self) -> usize

The maximum number of fingerprints the table can hold.

This is num_buckets * 4. Practical fill tops out a little below this value before insertions start to fail.

Source

pub fn load_factor(&self) -> f64

The current load factor in [0.0, 1.0].

§Examples
use bloom_lib::CuckooFilter;

let filter = CuckooFilter::<u32>::new(100).unwrap();
assert_eq!(filter.load_factor(), 0.0);

Trait Implementations§

Source§

impl<T: Clone + ?Sized, S: Clone> Clone for CuckooFilter<T, S>

Source§

fn clone(&self) -> CuckooFilter<T, S>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + ?Sized, S: Debug> Debug for CuckooFilter<T, S>

Source§

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

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

impl<'de, T: ?Sized, S> Deserialize<'de> for CuckooFilter<T, S>
where S: Default,

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<T: ?Sized, S> Serialize for CuckooFilter<T, S>

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<T, S> Freeze for CuckooFilter<T, S>
where S: Freeze, T: ?Sized,

§

impl<T, S> RefUnwindSafe for CuckooFilter<T, S>
where S: RefUnwindSafe, T: ?Sized,

§

impl<T, S> Send for CuckooFilter<T, S>
where S: Send, T: ?Sized,

§

impl<T, S> Sync for CuckooFilter<T, S>
where S: Sync, T: ?Sized,

§

impl<T, S> Unpin for CuckooFilter<T, S>
where S: Unpin, T: ?Sized,

§

impl<T, S> UnsafeUnpin for CuckooFilter<T, S>
where S: UnsafeUnpin, T: ?Sized,

§

impl<T, S> UnwindSafe for CuckooFilter<T, S>
where S: UnwindSafe, T: ?Sized,

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<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,