use crate::error::Error;
use crate::hash::DEFAULT_UPDATE_SEED;
use crate::theta::CompactThetaSketch;
use crate::theta::ThetaSketchView;
use crate::theta::hash_table::ThetaEntry;
use crate::thetacommon::intersection::IntersectionMergePolicy;
use crate::thetacommon::intersection::IntersectionState;
#[derive(Debug)]
pub struct ThetaIntersection {
state: IntersectionState<ThetaEntry, NoopIntersectionPolicy>,
}
impl Default for ThetaIntersection {
fn default() -> Self {
Self::with_seed(DEFAULT_UPDATE_SEED).unwrap()
}
}
#[derive(Debug)]
struct NoopIntersectionPolicy;
impl IntersectionMergePolicy<ThetaEntry> for NoopIntersectionPolicy {
fn merge(&self, _existing: &mut ThetaEntry, _incoming: ThetaEntry) {}
}
impl ThetaIntersection {
pub fn with_seed(seed: u64) -> Result<Self, Error> {
Ok(Self {
state: IntersectionState::new(seed, NoopIntersectionPolicy)?,
})
}
pub fn update<'a>(&mut self, sketch: impl Into<ThetaSketchView<'a>>) -> Result<(), Error> {
let sketch = sketch.into();
self.state.update(sketch)
}
pub fn has_result(&self) -> bool {
self.state.has_result()
}
pub fn estimated_size(&self) -> usize {
size_of::<Self>() + self.state.estimated_size()
}
pub fn to_sketch(&self, ordered: bool) -> Option<CompactThetaSketch> {
self.state
.to_compact_sketch_state(ordered)
.map(|compact_state| {
CompactThetaSketch::from_compact_state(
compact_state.map_retained_entries(|entry| entry.hash()),
)
})
}
}