const FNV_OFFSET: u64 = 0xcbf29ce484222325;
const FNV_PRIME: u64 = 0x100000001b3;
const BUCKET_SIZE: usize = 4;
const MAX_KICKS: usize = 500;
pub struct CuckooFilter {
buckets: Vec<[u8; BUCKET_SIZE]>,
mask: usize,
count: usize,
rng_state: u64,
}
impl CuckooFilter {
pub fn with_capacity(expected_entries: usize) -> Self {
let needed = (expected_entries.max(1) * 105 / 100) / BUCKET_SIZE + 1;
let num_buckets = needed.max(2).next_power_of_two();
Self {
buckets: vec![[0u8; BUCKET_SIZE]; num_buckets],
mask: num_buckets - 1,
count: 0,
rng_state: 0x9e3779b97f4a7c15,
}
}
pub fn len(&self) -> usize {
self.count
}
pub fn is_empty(&self) -> bool {
self.count == 0
}
pub fn bucket_count(&self) -> usize {
self.buckets.len()
}
pub fn insert(&mut self, key: &str) -> bool {
let (fp, i1, i2) = self.indices(key);
if self.try_place(i1, fp) || self.try_place(i2, fp) {
self.count += 1;
return true;
}
let mut bucket_idx = if self.rand_bit() { i1 } else { i2 };
let mut victim = fp;
for _ in 0..MAX_KICKS {
let slot = (self.next_random() as usize) % BUCKET_SIZE;
std::mem::swap(&mut victim, &mut self.buckets[bucket_idx][slot]);
bucket_idx ^= alt_index_of_fp(victim) & self.mask;
if self.try_place(bucket_idx, victim) {
self.count += 1;
return true;
}
}
false
}
pub fn contains(&self, key: &str) -> bool {
let (fp, i1, i2) = self.indices(key);
self.bucket_has(i1, fp) || self.bucket_has(i2, fp)
}
pub fn delete(&mut self, key: &str) -> bool {
let (fp, i1, i2) = self.indices(key);
if self.bucket_remove(i1, fp) || self.bucket_remove(i2, fp) {
self.count -= 1;
return true;
}
false
}
fn indices(&self, key: &str) -> (u8, usize, usize) {
let h = mix(fnv1a64(key.as_bytes()));
let fp = ((h & 0xff) as u8).max(1);
let i1 = (h >> 8) as usize & self.mask;
let i2 = (i1 ^ alt_index_of_fp(fp)) & self.mask;
(fp, i1, i2)
}
fn try_place(&mut self, i: usize, fp: u8) -> bool {
for slot in &mut self.buckets[i] {
if *slot == 0 {
*slot = fp;
return true;
}
}
false
}
fn bucket_has(&self, i: usize, fp: u8) -> bool {
self.buckets[i].contains(&fp)
}
fn bucket_remove(&mut self, i: usize, fp: u8) -> bool {
for slot in &mut self.buckets[i] {
if *slot == fp {
*slot = 0;
return true;
}
}
false
}
fn rand_bit(&mut self) -> bool {
self.next_random() & 1 != 0
}
fn next_random(&mut self) -> u64 {
self.rng_state = self
.rng_state
.wrapping_mul(6364136223846793005)
.wrapping_add(1442695040888963407);
self.rng_state
}
}
fn alt_index_of_fp(fp: u8) -> usize {
(fp as u64).wrapping_mul(0x5bd1e9955_u64) as usize
}
fn fnv1a64(bytes: &[u8]) -> u64 {
let mut h = FNV_OFFSET;
for &b in bytes {
h ^= b as u64;
h = h.wrapping_mul(FNV_PRIME);
}
h
}
fn mix(mut h: u64) -> u64 {
h ^= h >> 30;
h = h.wrapping_mul(0xbf58476d1ce4e5b9);
h ^= h >> 27;
h = h.wrapping_mul(0x94d049bb133111eb);
h ^= h >> 31;
h
}
#[cfg(feature = "harness")]
pub mod recipe;