use crate::thetacommon::constants::MAX_THETA;
#[derive(Clone, Copy, Debug)]
pub enum ThetaFamilySketchMetadata {
Empty {
seed_hash: u16,
},
NonEmpty {
seed_hash: u16,
theta: u64,
ordered: bool,
num_retained: usize,
},
}
#[derive(Clone, Debug)]
pub enum CompactSketchState<E> {
Empty {
seed_hash: u16,
},
NonEmpty {
retained_entries: Vec<E>,
theta: u64,
seed_hash: u16,
ordered: bool,
},
}
impl<E> CompactSketchState<E> {
pub fn empty(seed_hash: u16) -> Self {
Self::Empty { seed_hash }
}
pub fn non_empty(retained_entries: Vec<E>, theta: u64, seed_hash: u16, ordered: bool) -> Self {
Self::NonEmpty {
retained_entries,
theta,
seed_hash,
ordered,
}
}
pub fn theta(&self) -> u64 {
match self {
Self::Empty { .. } => MAX_THETA,
Self::NonEmpty { theta, .. } => *theta,
}
}
pub fn is_empty(&self) -> bool {
matches!(self, Self::Empty { .. })
}
pub fn is_estimation_mode(&self) -> bool {
matches!(self, Self::NonEmpty { theta, .. } if *theta < MAX_THETA)
}
pub fn seed_hash(&self) -> u16 {
match self {
Self::Empty { seed_hash } | Self::NonEmpty { seed_hash, .. } => *seed_hash,
}
}
pub fn retained_entries(&self) -> &[E] {
match self {
Self::Empty { .. } => &[],
Self::NonEmpty {
retained_entries, ..
} => retained_entries,
}
}
pub fn retained_entries_capacity(&self) -> usize {
match self {
Self::Empty { .. } => 0,
Self::NonEmpty {
retained_entries, ..
} => retained_entries.capacity(),
}
}
pub fn is_ordered(&self) -> bool {
match self {
Self::Empty { .. } => true,
Self::NonEmpty { ordered, .. } => *ordered,
}
}
#[cfg(feature = "theta")]
pub fn map_retained_entries<T>(self, mut f: impl FnMut(E) -> T) -> CompactSketchState<T> {
match self {
Self::Empty { seed_hash } => CompactSketchState::empty(seed_hash),
Self::NonEmpty {
retained_entries,
theta,
seed_hash,
ordered,
} => CompactSketchState::non_empty(
retained_entries.into_iter().map(&mut f).collect(),
theta,
seed_hash,
ordered,
),
}
}
}