Skip to main content

CountMinSketch

Struct CountMinSketch 

Source
pub struct CountMinSketch { /* private fields */ }
Expand description

A lock-free probabilistic data structure for frequency estimation.

This implementation optimizes for CPU cache locality and multithreaded throughput by bit-packing 8-bit saturating counters into 64-bit atomic blocks.

Implementations§

Source§

impl CountMinSketch

Source

pub fn new(width: usize, depth: usize) -> Self

Creates a new probabilistic estimator with a specified geometry.

The physical storage is calculated by mapping the requested logical width to 64-bit atomic blocks (8 counters per block).

§Arguments
  • width - The logical number of counters per row. This is rounded to the next power of two to enable bitwise indexing.
  • depth - The number of independent hash functions (rows) used to minimize the probability of overestimation.
§Panics

Panics if depth exceeds 8, as the implementation relies on a fixed set of static prime seeds for hashing independence.

Source

pub fn increment<K: Eq + Hash>(&self, key: &K)

Increments the frequency counters for the given key.

This operation is performed across all rows (defined by depth) using a saturating add. Counters will not exceed 255.

§Arguments
  • key - The item whose frequency should be increased.
Source

pub fn decrement<K: Eq + Hash>(&self, key: &K)

Decrements the frequency counters for the given key.

This is used for manual aging or item removal within the sketch. Counters will not underflow below 0.

§Arguments
  • key - The item whose frequency should be decreased.
Source

pub fn decay(&self)

Performs an aging operation by halving all counters in the sketch.

This uses a bitwise trick to divide eight 8-bit counters by 2 simultaneously within each 64-bit word.

Source

pub fn contains<K: Eq + Hash>(&self, key: &K) -> bool

Returns true if the key has an estimated frequency greater than zero.

Derived by taking the minimum frequency observed across all rows.

§Arguments
  • key - The item to check for presence in the sketch.
Source

pub fn get<K: Eq + Hash>(&self, key: &K) -> u8

Returns the estimated frequency of the provided key.

The estimate is derived by taking the minimum value found across all rows (defined by depth). Due to the probabilistic nature of the sketch, this value is an upper bound of the actual frequency.

§Arguments
  • key - The item whose frequency estimate is being requested.

Auto Trait Implementations§

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> 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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V