Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::Count_Min_Sketch< T, CounterT > Class Template Reference

Count-Min Sketch frequency estimator. More...

#include <count-min-sketch.H>

Collaboration diagram for Aleph::Count_Min_Sketch< T, CounterT >:
[legend]

Public Member Functions

 Count_Min_Sketch (const size_t w, const size_t d)
 Construct with explicit dimensions.
 
 Count_Min_Sketch (const Count_Min_Sketch &other)=default
 Copy the dimensions and the counters of another sketch.
 
 Count_Min_Sketch (Count_Min_Sketch &&other)
 Take the counters of another sketch, which is left as an empty sketch of the same dimensions.
 
Count_Min_Sketch & operator= (const Count_Min_Sketch &other)=default
 Copy the dimensions and the counters of another sketch.
 
Count_Min_Sketch & operator= (Count_Min_Sketch &&other) noexcept
 Exchange this sketch with another, which is left with the previous state of this one.
 
void swap (Count_Min_Sketch &other) noexcept
 Exchange in O(1) the state of this sketch and another.
 
void update (const T &val, CounterT count=1)
 Increment count for an element.
 
CounterT estimate (const T &val) const
 Estimate frequency of an element.
 
void merge (const Count_Min_Sketch &other)
 Merge another sketch into this one.
 
void clear ()
 Clear all counters.
 
size_t width () const noexcept
 Number of counter columns (hash buckets per row).
 
size_t depth () const noexcept
 Number of hash functions (independent counter rows).
 
CounterT total_count () const noexcept
 Sum of all event weights inserted so far.
 

Static Public Member Functions

static Count_Min_Sketch from_error_bounds (const double epsilon, const double delta)
 Create a sketch with target error bounds.
 

Private Attributes

size_t w_
 Width of the sketch.
 
size_t d_
 Depth (number of hash functions).
 
Array< CounterT > table_
 Flat array for counters [d_ x w_].
 
Array< uint32_t > seeds_
 Random seeds for hash functions.
 
CounterT total_count_ = 0
 Total updates processed.
 

Detailed Description

template<typename T, std::unsigned_integral CounterT = size_t>
class Aleph::Count_Min_Sketch< T, CounterT >

Count-Min Sketch frequency estimator.

Provides approximate frequency queries for elements in a stream. The estimate is always an upper bound (no false negatives).

Memory complexity is O(w * d), where:

  • w = width (related to error magnitude)
  • d = depth (related to confidence)
Template Parameters
TType of elements to count.
CounterTUnsigned integral type for counters (default: size_t).

Definition at line 70 of file count-min-sketch.H.

Constructor & Destructor Documentation

◆ Count_Min_Sketch() [1/3]

template<typename T , std::unsigned_integral CounterT = size_t>
Aleph::Count_Min_Sketch< T, CounterT >::Count_Min_Sketch ( const size_t  w,
const size_t  d 
)
inline

◆ Count_Min_Sketch() [2/3]

template<typename T , std::unsigned_integral CounterT = size_t>
Aleph::Count_Min_Sketch< T, CounterT >::Count_Min_Sketch ( const Count_Min_Sketch< T, CounterT > &  other)
default

Copy the dimensions and the counters of another sketch.

Parameters
[in]othersketch to copy.
Exceptions
std::bad_allocif the counters cannot be allocated.

◆ Count_Min_Sketch() [3/3]

template<typename T , std::unsigned_integral CounterT = size_t>
Aleph::Count_Min_Sketch< T, CounterT >::Count_Min_Sketch ( Count_Min_Sketch< T, CounterT > &&  other)
inline

Take the counters of another sketch, which is left as an empty sketch of the same dimensions.

The implicit move took the counters but copied the dimensions: the moved-from sketch then indexed counters that it no longer had.

Parameters
[in,out]othersketch to move from.
Exceptions
std::bad_allocif the counters left to other cannot be allocated; other is then unchanged.
Note
Complexity: O(w * d), for the counters left to other.

Definition at line 117 of file count-min-sketch.H.

References Aleph::Count_Min_Sketch< T, CounterT >::swap().

Member Function Documentation

◆ clear()

template<typename T , std::unsigned_integral CounterT = size_t>
void Aleph::Count_Min_Sketch< T, CounterT >::clear ( )
inline

◆ depth()

template<typename T , std::unsigned_integral CounterT = size_t>
size_t Aleph::Count_Min_Sketch< T, CounterT >::depth ( ) const
inlinenoexcept

Number of hash functions (independent counter rows).

Returns
size_t The depth d passed at construction.
Exceptions
none
Note
Complexity: O(1).

Definition at line 268 of file count-min-sketch.H.

References Aleph::Count_Min_Sketch< T, CounterT >::d_.

◆ estimate()

template<typename T , std::unsigned_integral CounterT = size_t>
CounterT Aleph::Count_Min_Sketch< T, CounterT >::estimate ( const T &  val) const
inline

◆ from_error_bounds()

template<typename T , std::unsigned_integral CounterT = size_t>
static Count_Min_Sketch Aleph::Count_Min_Sketch< T, CounterT >::from_error_bounds ( const double  epsilon,
const double  delta 
)
inlinestatic

Create a sketch with target error bounds.

Calculates optimal w and d for given epsilon and delta. Estimate will be within epsilon * total_count from actual with probability at least 1 - delta.

Parameters
[in]epsilonRelative error bound (must be > 0).
[in]deltaConfidence bound (error probability, must be in (0, 1)).
Returns
A new Count_Min_Sketch instance.

Definition at line 163 of file count-min-sketch.H.

References ah_domain_error_if.

Referenced by TEST().

◆ merge()

template<typename T , std::unsigned_integral CounterT = size_t>
void Aleph::Count_Min_Sketch< T, CounterT >::merge ( const Count_Min_Sketch< T, CounterT > &  other)
inline

◆ operator=() [1/2]

template<typename T , std::unsigned_integral CounterT = size_t>
Count_Min_Sketch & Aleph::Count_Min_Sketch< T, CounterT >::operator= ( const Count_Min_Sketch< T, CounterT > &  other)
default

Copy the dimensions and the counters of another sketch.

Parameters
[in]othersketch to copy.
Returns
this sketch.
Exceptions
std::bad_allocif the counters cannot be allocated.

◆ operator=() [2/2]

template<typename T , std::unsigned_integral CounterT = size_t>
Count_Min_Sketch & Aleph::Count_Min_Sketch< T, CounterT >::operator= ( Count_Min_Sketch< T, CounterT > &&  other)
inlinenoexcept

Exchange this sketch with another, which is left with the previous state of this one.

Parameters
[in,out]othersketch to move from.
Returns
this sketch.
Note
Complexity: O(1).

Definition at line 135 of file count-min-sketch.H.

References Aleph::Count_Min_Sketch< T, CounterT >::swap().

◆ swap()

template<typename T , std::unsigned_integral CounterT = size_t>
void Aleph::Count_Min_Sketch< T, CounterT >::swap ( Count_Min_Sketch< T, CounterT > &  other)
inlinenoexcept

◆ total_count()

template<typename T , std::unsigned_integral CounterT = size_t>
CounterT Aleph::Count_Min_Sketch< T, CounterT >::total_count ( ) const
inlinenoexcept

Sum of all event weights inserted so far.

Returns
CounterT Cumulative total of all update() weight arguments.
Exceptions
none
Note
Complexity: O(1).

Definition at line 275 of file count-min-sketch.H.

References Aleph::Count_Min_Sketch< T, CounterT >::total_count_.

Referenced by Moved_From_Case< Count_Min_Sketch< int > >::is_empty().

◆ update()

template<typename T , std::unsigned_integral CounterT = size_t>
void Aleph::Count_Min_Sketch< T, CounterT >::update ( const T &  val,
CounterT  count = 1 
)
inline

◆ width()

template<typename T , std::unsigned_integral CounterT = size_t>
size_t Aleph::Count_Min_Sketch< T, CounterT >::width ( ) const
inlinenoexcept

Number of counter columns (hash buckets per row).

Returns
size_t The width w passed at construction.
Exceptions
none
Note
Complexity: O(1).

Definition at line 261 of file count-min-sketch.H.

References Aleph::Count_Min_Sketch< T, CounterT >::w_.

Referenced by TEST().

Member Data Documentation

◆ d_

◆ seeds_

◆ table_

◆ total_count_

template<typename T , std::unsigned_integral CounterT = size_t>
CounterT Aleph::Count_Min_Sketch< T, CounterT >::total_count_ = 0
private

◆ w_


The documentation for this class was generated from the following file: