31# ifndef COUNT_MIN_SKETCH_H
32# define COUNT_MIN_SKETCH_H
69 template <
typename T, std::
unsigned_
integral CounterT =
size_t>
87 <<
"Count_Min_Sketch: dimensions must be positive";
90 <<
"Count_Min_Sketch: product of dimensions w*d overflows size_t (got "
91 <<
w <<
" * " << d <<
")";
96 for (
size_t i = 0; i <
d_; ++i)
146 std::swap(
w_, other.w_);
147 std::swap(
d_, other.d_);
148 table_.swap(other.table_);
166 <<
"Count_Min_Sketch::from_error_bounds: epsilon must be positive and finite (got " << epsilon <<
")";
168 <<
"Count_Min_Sketch::from_error_bounds: delta must be in (0, 1) and finite (got " << delta <<
")";
170 const double w_dbl = std::ceil(std::exp(1.0) / epsilon);
171 const double d_dbl = std::ceil(std::log(1.0 / delta));
172 const double max_sz =
static_cast<double>(std::numeric_limits<size_t>::max());
175 <<
"Count_Min_Sketch::from_error_bounds: calculated dimensions exceed size_t limits";
177 return Count_Min_Sketch(
static_cast<size_t>(w_dbl),
static_cast<size_t>(d_dbl));
186 if (
count == 0)
return;
188 constexpr CounterT max_val = std::numeric_limits<CounterT>::max();
190 for (
size_t i = 0; i <
d_; ++i)
193 const size_t idx = i *
w_ +
h;
211 CounterT min_val = std::numeric_limits<CounterT>::max();
212 for (
size_t i = 0; i <
d_; ++i)
215 min_val = std::min(min_val,
table_[i *
w_ +
h]);
226 <<
"Count_Min_Sketch::merge: dimension mismatch";
228 for (
size_t i = 0; i <
d_; ++i)
230 <<
"Count_Min_Sketch::merge: hash seed mismatch at index " << i;
232 constexpr CounterT max_val = std::numeric_limits<CounterT>::max();
234 for (
size_t i = 0; i <
table_.size(); ++i)
251 for (
size_t i = 0; i <
table_.size(); ++i)
261 [[nodiscard]]
size_t width() const noexcept {
return w_; }
268 [[nodiscard]]
size_t depth() const noexcept {
return d_; }
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Simple dynamic array with automatic resizing and functional operations.
void swap(Array &s) noexcept
Swap this with s
T & append(const T &data)
Append a copy of data
void reserve(size_t cap)
Reserves cap cells into the array.
Count-Min Sketch frequency estimator.
Count_Min_Sketch(Count_Min_Sketch &&other)
Take the counters of another sketch, which is left as an empty sketch of the same dimensions.
void merge(const Count_Min_Sketch &other)
Merge another sketch into this one.
size_t w_
Width of the sketch.
Count_Min_Sketch(const Count_Min_Sketch &other)=default
Copy the dimensions and the counters of another sketch.
size_t d_
Depth (number of hash functions).
size_t depth() const noexcept
Number of hash functions (independent counter rows).
Array< CounterT > table_
Flat array for counters [d_ x w_].
Count_Min_Sketch & operator=(const Count_Min_Sketch &other)=default
Copy the dimensions and the counters of another sketch.
CounterT estimate(const T &val) const
Estimate frequency of an element.
CounterT total_count() const noexcept
Sum of all event weights inserted so far.
void clear()
Clear all counters.
static Count_Min_Sketch from_error_bounds(const double epsilon, const double delta)
Create a sketch with target error bounds.
void update(const T &val, CounterT count=1)
Increment count for an element.
size_t width() const noexcept
Number of counter columns (hash buckets per row).
CounterT total_count_
Total updates processed.
Count_Min_Sketch(const size_t w, const size_t d)
Construct with explicit dimensions.
Array< uint32_t > seeds_
Random seeds for hash functions.
void swap(Count_Min_Sketch &other) noexcept
Exchange in O(1) the state of this sketch and another.
Standard hash functions for Aleph types.
Main namespace for Aleph-w library functions.
std::decay_t< typename HeadC::Item_Type > T
size_t dft_hash_fct(const Key &key) noexcept
Primary default hash: best speed/quality trade-off.
size_t murmur3hash(const Key &key, std::uint32_t seed)
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Dynamic array container with automatic resizing.