pub struct SpaceSaving<K>{ /* private fields */ }Expand description
Streaming top-K tracker with O(K) memory.
§Examples
use anomstream_core::SpaceSaving;
let mut ss: SpaceSaving<u32> = SpaceSaving::with_default_capacity().unwrap();
// 1 000 observations of "10" versus 5 of "99".
for _ in 0..1_000 {
ss.observe(10_u32);
}
for _ in 0..5 {
ss.observe(99_u32);
}
let top = ss.top_k(1);
assert_eq!(top[0].key, 10);
assert_eq!(top[0].estimate, 1_000);Implementations§
Source§impl<K> SpaceSaving<K>
impl<K> SpaceSaving<K>
Sourcepub fn new(capacity: usize) -> RcfResult<Self>
pub fn new(capacity: usize) -> RcfResult<Self>
Build a tracker with caller-chosen capacity.
§Errors
Returns RcfError::InvalidConfig on capacity == 0.
Sourcepub fn with_default_capacity() -> RcfResult<Self>
pub fn with_default_capacity() -> RcfResult<Self>
Default tracker — capacity = 128.
§Errors
Never in practice — DEFAULT_CAPACITY is a positive
compile-time constant.
Sourcepub fn total(&self) -> u64
pub fn total(&self) -> u64
Total observations — sum of every Self::observe weight.
Sourcepub fn error_bound(&self) -> u64
pub fn error_bound(&self) -> u64
Worst-case per-estimate error bound (N/K). Keys whose
true frequency exceeds this are guaranteed to be tracked.
Sourcepub fn observe_weighted(&mut self, key: K, weight: u64)
pub fn observe_weighted(&mut self, key: K, weight: u64)
Ingest key with caller-supplied weight — byte-count
heavy hitters in NDR workloads (per-packet bytes, not
just packet counts).
Sourcepub fn estimate(&self, key: &K) -> Option<HeavyHitterEntry>
pub fn estimate(&self, key: &K) -> Option<HeavyHitterEntry>
Frequency estimate for key. Returns None when the key
is not tracked (its true count may still be up to
Self::error_bound).
Sourcepub fn top_k(&self, n: usize) -> Vec<HeavyHitter<K>>
pub fn top_k(&self, n: usize) -> Vec<HeavyHitter<K>>
Ranked top-n snapshot — sorted by descending estimate.
n is clamped to Self::len.
Sourcepub fn iter(&self) -> impl Iterator<Item = (&K, &HeavyHitterEntry)>
pub fn iter(&self) -> impl Iterator<Item = (&K, &HeavyHitterEntry)>
Iterate every tracked (&K, HeavyHitterEntry) in
insertion order (not ranked). Useful for ad-hoc scans /
serialisation sinks.
Trait Implementations§
Source§impl<K> Clone for SpaceSaving<K>
impl<K> Clone for SpaceSaving<K>
Source§fn clone(&self) -> SpaceSaving<K>
fn clone(&self) -> SpaceSaving<K>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<K> Debug for SpaceSaving<K>
impl<K> Debug for SpaceSaving<K>
Source§impl<'de, K> Deserialize<'de> for SpaceSaving<K>
impl<'de, K> Deserialize<'de> for SpaceSaving<K>
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<K> Freeze for SpaceSaving<K>
impl<K> RefUnwindSafe for SpaceSaving<K>where
K: RefUnwindSafe,
impl<K> Send for SpaceSaving<K>where
K: Send,
impl<K> Sync for SpaceSaving<K>where
K: Sync,
impl<K> Unpin for SpaceSaving<K>where
K: Unpin,
impl<K> UnsafeUnpin for SpaceSaving<K>
impl<K> UnwindSafe for SpaceSaving<K>where
K: UnwindSafe,
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