Struct cuckoofilter::CuckooFilter
source · [−]pub struct CuckooFilter<H> { /* private fields */ }
Expand description
A cuckoo filter class exposes a Bloomier filter interface, providing methods of add, delete, contains.
Examples
extern crate cuckoofilter;
let words = vec!["foo", "bar", "xylophone", "milagro"];
let mut cf = cuckoofilter::CuckooFilter::new();
let mut insertions = 0;
for s in &words {
if cf.test_and_add(s).unwrap() {
insertions += 1;
}
}
assert_eq!(insertions, words.len());
assert_eq!(cf.len(), words.len());
// Re-add the first element.
cf.add(words[0]);
assert_eq!(cf.len(), words.len() + 1);
for s in &words {
cf.delete(s);
}
assert_eq!(cf.len(), 1);
assert!(!cf.is_empty());
cf.delete(words[0]);
assert_eq!(cf.len(), 0);
assert!(cf.is_empty());
Implementations
sourceimpl<H> CuckooFilter<H> where
H: Hasher + Default,
impl<H> CuckooFilter<H> where
H: Hasher + Default,
sourcepub fn with_capacity(cap: usize) -> Self
pub fn with_capacity(cap: usize) -> Self
Constructs a Cuckoo Filter with a given max capacity
sourcepub fn add<T: ?Sized + Hash>(&mut self, data: &T) -> Result<(), CuckooError>
pub fn add<T: ?Sized + Hash>(&mut self, data: &T) -> Result<(), CuckooError>
Adds data
to the filter. Returns Ok
if the insertion was successful,
but could fail with a NotEnoughSpace
error, especially when the filter
is nearing its capacity.
Note that while you can put any hashable type in the same filter, beware
for side effects like that the same number can have diferent hashes
depending on the type.
So for the filter, 4711i64 isn’t the same as 4711u64.
Note: When this returns NotEnoughSpace
, the element given was
actually added to the filter, but some random other element was
removed. This might improve in the future.
sourcepub fn test_and_add<T: ?Sized + Hash>(
&mut self,
data: &T
) -> Result<bool, CuckooError>
pub fn test_and_add<T: ?Sized + Hash>(
&mut self,
data: &T
) -> Result<bool, CuckooError>
Adds data
to the filter if it does not exist in the filter yet.
Returns Ok(true)
if data
was not yet present in the filter and added
successfully.
sourcepub fn export(&self) -> ExportedCuckooFilter
pub fn export(&self) -> ExportedCuckooFilter
Exports fingerprints in all buckets, along with the filter’s length for storage.
The filter can be recovered by passing the ExportedCuckooFilter
struct to the
from
method of CuckooFilter
.
sourcepub fn memory_usage(&self) -> usize
pub fn memory_usage(&self) -> usize
Number of bytes the filter occupies in memory
Trait Implementations
sourceimpl Default for CuckooFilter<DefaultHasher>
impl Default for CuckooFilter<DefaultHasher>
sourceimpl<H> From<&'_ CuckooFilter<H>> for ExportedCuckooFilter where
H: Hasher + Default,
impl<H> From<&'_ CuckooFilter<H>> for ExportedCuckooFilter where
H: Hasher + Default,
sourcefn from(cuckoo: &CuckooFilter<H>) -> Self
fn from(cuckoo: &CuckooFilter<H>) -> Self
Converts a CuckooFilter
into a simplified version which can be serialized and stored
for later use.
sourceimpl<H> From<ExportedCuckooFilter> for CuckooFilter<H>
impl<H> From<ExportedCuckooFilter> for CuckooFilter<H>
sourcefn from(exported: ExportedCuckooFilter) -> Self
fn from(exported: ExportedCuckooFilter) -> Self
Converts a simplified representation of a filter used for export to a fully functioning version.
Contents
values
- A serialized version of theCuckooFilter
’s memory, where the fingerprints in each bucket are chained one after another, then in turn all buckets are chained together.length
- The number of valid fingerprints inside theCuckooFilter
. This value is used as a time saving method, otherwise all fingerprints would need to be checked for equivalence against the null pattern.
Auto Trait Implementations
impl<H> RefUnwindSafe for CuckooFilter<H> where
H: RefUnwindSafe,
impl<H> Send for CuckooFilter<H> where
H: Send,
impl<H> Sync for CuckooFilter<H> where
H: Sync,
impl<H> Unpin for CuckooFilter<H> where
H: Unpin,
impl<H> UnwindSafe for CuckooFilter<H> where
H: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more