Skip to main content

SpaceSaving

Struct SpaceSaving 

Source
pub struct SpaceSaving<T: Hash + Eq + Clone + Debug> { /* private fields */ }
Available on crate features frequency and std only.
Expand description

Space-Saving algorithm for finding frequent items

The Space-Saving algorithm maintains a summary of the k most frequent items with the following guarantees:

  • Any item with true frequency > n/k is guaranteed to be in the summary
  • The maximum overcount error for any item is at most n/k

§Example

use flowstats::frequency::SpaceSaving;
use flowstats::traits::HeavyHitters;

let mut ss = SpaceSaving::new(10); // Track top 10

// Add some items
for _ in 0..100 { ss.add("apple"); }
for _ in 0..50 { ss.add("banana"); }
for _ in 0..25 { ss.add("cherry"); }
for _ in 0..10 { ss.add("date"); }

// Get top 3 items
let top = ss.top_k(3);
println!("Top items: {:?}", top);

Implementations§

Source§

impl<T: Hash + Eq + Clone + Debug> SpaceSaving<T>

Source

pub fn new(capacity: usize) -> Self

Create a new Space-Saving structure with the given capacity

§Arguments
  • capacity - Maximum number of items to track (k)
Source

pub fn capacity(&self) -> usize

Get the capacity (k)

Source

pub fn num_tracked(&self) -> usize

Get the number of distinct items currently tracked

Source

pub fn total_count(&self) -> u64

Get the total count

Source

pub fn add(&mut self, item: T)

Add an item to the structure

Source

pub fn add_count(&mut self, item: T, count: u64)

Add an item with a specific count

Source

pub fn estimate(&self, item: &T) -> u64

Estimate the frequency of an item

Source

pub fn error(&self, item: &T) -> u64

Get the error bound for an item’s estimate

Source

pub fn guaranteed_count(&self, item: &T) -> u64

Get guaranteed minimum count for an item

Returns (count - error), which is guaranteed to be at most the true count.

Source

pub fn contains(&self, item: &T) -> bool

Check if an item is currently tracked

Trait Implementations§

Source§

impl<T: Clone + Hash + Eq + Clone + Debug> Clone for SpaceSaving<T>

Source§

fn clone(&self) -> SpaceSaving<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + Hash + Eq + Clone + Debug> Debug for SpaceSaving<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Hash + Eq + Clone + Debug> FrequencySketch for SpaceSaving<T>

Source§

fn estimate_frequency(&self, item: &T) -> u64

Estimate frequency of an item
Source§

fn exceeds_threshold(&self, item: &Self::Item, threshold: u64) -> bool

Check if frequency exceeds threshold
Source§

impl<T: Hash + Eq + Clone + Debug> HeavyHitters for SpaceSaving<T>

Source§

fn heavy_hitters(&self, threshold: f64) -> Vec<(T, u64)>

Get items with estimated frequency above threshold Read more
Source§

fn top_k(&self, k: usize) -> Vec<(T, u64)>

Get top-k most frequent items
Source§

impl<T: Hash + Eq + Clone + Debug + Serialize> Serialize for SpaceSaving<T>

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<T: Hash + Eq + Clone + Debug> Sketch for SpaceSaving<T>

Source§

type Item = T

The type of item this sketch processes
Source§

fn update(&mut self, item: &T)

Add an item to the sketch
Source§

fn merge(&mut self, _other: &Self) -> Result<(), MergeError>

Merge another sketch into this one Read more
Source§

fn clear(&mut self)

Reset sketch to empty state
Source§

fn size_bytes(&self) -> usize

Memory usage in bytes
Source§

fn count(&self) -> u64

Number of items processed
Source§

fn is_empty(&self) -> bool

Check if sketch is empty

Auto Trait Implementations§

§

impl<T> Freeze for SpaceSaving<T>

§

impl<T> RefUnwindSafe for SpaceSaving<T>
where T: RefUnwindSafe,

§

impl<T> Send for SpaceSaving<T>
where T: Send,

§

impl<T> Sync for SpaceSaving<T>
where T: Sync,

§

impl<T> Unpin for SpaceSaving<T>
where T: Unpin,

§

impl<T> UnwindSafe for SpaceSaving<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.