pub struct HyperLogLog { /* private fields */ }Expand description
Probabilistic distinct-count sketch.
§Examples
use anomstream_core::HyperLogLog;
let mut hll = HyperLogLog::with_default_precision();
for ip in 0..10_000_u32 {
hll.add(&ip.to_le_bytes());
}
let est = hll.estimate();
let err = (est as i64 - 10_000).unsigned_abs() as f64 / 10_000.0;
assert!(err < 0.05); // 3× the theoretical 1.625 % — conservativeImplementations§
Source§impl HyperLogLog
impl HyperLogLog
Sourcepub fn new(precision: u8) -> RcfResult<Self>
pub fn new(precision: u8) -> RcfResult<Self>
Build a sketch with caller-chosen precision.
§Errors
Returns RcfError::InvalidConfig when precision is
outside [MIN_PRECISION, MAX_PRECISION].
Sourcepub fn with_default_precision() -> Self
pub fn with_default_precision() -> Self
Default sketch — p = 12, 4 096 registers, ≈ 1.625 % std
error, ~4 KiB memory.
§Panics
Never in practice — DEFAULT_PRECISION is a compile-time
constant validated against the [MIN, MAX] range.
Sourcepub fn register_count(&self) -> usize
pub fn register_count(&self) -> usize
Register count m = 2^p.
Sourcepub fn total_added(&self) -> u64
pub fn total_added(&self) -> u64
Total values passed through Self::add.
Sourcepub fn memory_bytes(&self) -> usize
pub fn memory_bytes(&self) -> usize
Memory footprint in bytes (register bank only).
Sourcepub fn add<T: Hash + ?Sized>(&mut self, value: &T)
pub fn add<T: Hash + ?Sized>(&mut self, value: &T)
Ingest a Hash-able value. Pre-hashes through
DefaultHasher (SipHash) so per-key distribution is
uniform irrespective of the user type’s own Hash impl
quality.
Sourcepub fn add_bytes(&mut self, key: &[u8])
pub fn add_bytes(&mut self, key: &[u8])
Ingest a raw byte key. Cheaper when the caller already has
a fixed-size fingerprint (e.g. [u8; 16] IP, flow-hash
tuple) — skips the generic Hash dispatch.
Sourcepub fn add_hash(&mut self, hash: u64)
pub fn add_hash(&mut self, hash: u64)
Ingest a caller-supplied 64-bit hash. Escape hatch for
callers with a stronger hasher (e.g. xxhash, siphash with
a keyed seed) — the sketch’s accuracy depends on
hash % 2^p being uniform.
Sourcepub fn merge(&mut self, other: &Self) -> RcfResult<()>
pub fn merge(&mut self, other: &Self) -> RcfResult<()>
Fold other into self by taking a per-register maximum.
Two sketches must share the same precision — HLL merge is
the whole reason the sketch is decomposable across shards
/ time windows.
§Errors
Returns RcfError::InvalidConfig when the two sketches
disagree on precision.
Trait Implementations§
Source§impl Clone for HyperLogLog
impl Clone for HyperLogLog
Source§fn clone(&self) -> HyperLogLog
fn clone(&self) -> HyperLogLog
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for HyperLogLog
impl Debug for HyperLogLog
Source§impl<'de> Deserialize<'de> for HyperLogLog
impl<'de> Deserialize<'de> for HyperLogLog
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Auto Trait Implementations§
impl Freeze for HyperLogLog
impl RefUnwindSafe for HyperLogLog
impl Send for HyperLogLog
impl Sync for HyperLogLog
impl Unpin for HyperLogLog
impl UnsafeUnpin for HyperLogLog
impl UnwindSafe for HyperLogLog
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more