use std::hash::Hash;
use std::num::NonZeroU64;
use crate::thetacommon::SketchEntry;
use crate::thetacommon::hash_table::SketchHashTable;
pub type ThetaHashTable = SketchHashTable<ThetaEntry>;
#[derive(Debug, Clone, Copy)]
pub struct ThetaEntry {
hash: NonZeroU64,
}
impl ThetaEntry {
pub(super) fn new(hash: u64) -> Self {
let hash = NonZeroU64::new(hash).expect("hash must be non-zero");
Self { hash }
}
pub fn hash(&self) -> u64 {
self.hash.get()
}
}
impl SketchEntry for ThetaEntry {
fn hash(&self) -> u64 {
self.hash.get()
}
}
impl ThetaHashTable {
pub fn try_insert<T: Hash>(&mut self, value: T) -> bool {
let hash = self.hash(value);
self.try_insert_hash(hash)
}
pub fn try_insert_hash(&mut self, hash: u64) -> bool {
self.upsert_entry(hash, |existing| {
if existing.is_some() {
None
} else {
Some(ThetaEntry::new(hash))
}
})
}
}