Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
count-min-sketch.H
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
31# ifndef COUNT_MIN_SKETCH_H
32# define COUNT_MIN_SKETCH_H
33
44# include <cmath>
45# include <algorithm>
46# include <concepts>
47# include <cstdint>
48# include <limits>
49
50# include <ah-errors.H>
51# include <tpl_array.H>
52# include <hash-fct.H>
53
54namespace Aleph
55{
56
69 template <typename T, std::unsigned_integral CounterT = size_t>
71 {
72 size_t w_;
73 size_t d_;
76 CounterT total_count_ = 0;
78 public:
83 Count_Min_Sketch(const size_t w, const size_t d)
84 : w_(w), d_(d)
85 {
86 ah_domain_error_if(w == 0 or d == 0)
87 << "Count_Min_Sketch: dimensions must be positive";
88
89 ah_domain_error_if(w > std::numeric_limits<size_t>::max() / d)
90 << "Count_Min_Sketch: product of dimensions w*d overflows size_t (got "
91 << w << " * " << d << ")";
92
93 table_ = Array<CounterT>(w_ * d_, CounterT(0));
95
96 for (size_t i = 0; i < d_; ++i)
97 seeds_.append(static_cast<uint32_t>(dft_hash_fct(i, 0x9e3779b9)));
98 }
99
104 Count_Min_Sketch(const Count_Min_Sketch & other) = default;
105
118 {
119 swap(other);
120 }
121
128
136 {
137 swap(other);
138 return *this;
139 }
140
144 void swap(Count_Min_Sketch & other) noexcept
145 {
146 std::swap(w_, other.w_);
147 std::swap(d_, other.d_);
148 table_.swap(other.table_);
149 seeds_.swap(other.seeds_);
150 std::swap(total_count_, other.total_count_);
151 }
152
163 static Count_Min_Sketch from_error_bounds(const double epsilon, const double delta)
164 {
165 ah_domain_error_if(epsilon <= 0.0 or not std::isfinite(epsilon))
166 << "Count_Min_Sketch::from_error_bounds: epsilon must be positive and finite (got " << epsilon << ")";
167 ah_domain_error_if(delta <= 0.0 or delta >= 1.0 or not std::isfinite(delta))
168 << "Count_Min_Sketch::from_error_bounds: delta must be in (0, 1) and finite (got " << delta << ")";
169
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());
173
174 ah_domain_error_if(w_dbl > max_sz or d_dbl > max_sz)
175 << "Count_Min_Sketch::from_error_bounds: calculated dimensions exceed size_t limits";
176
177 return Count_Min_Sketch(static_cast<size_t>(w_dbl), static_cast<size_t>(d_dbl));
178 }
179
184 void update(const T & val, CounterT count = 1)
185 {
186 if (count == 0) return;
187
188 constexpr CounterT max_val = std::numeric_limits<CounterT>::max();
189
190 for (size_t i = 0; i < d_; ++i)
191 {
192 const size_t h = murmur3hash(val, seeds_[i]) % w_;
193 const size_t idx = i * w_ + h;
194 if (max_val - table_[idx] < count)
195 table_(idx) = max_val;
196 else
197 table_(idx) += count;
198 }
199
200 if (max_val - total_count_ < count)
201 total_count_ = max_val;
202 else
204 }
205
209 [[nodiscard]] CounterT estimate(const T & val) const
210 {
211 CounterT min_val = std::numeric_limits<CounterT>::max();
212 for (size_t i = 0; i < d_; ++i)
213 {
214 const size_t h = murmur3hash(val, seeds_[i]) % w_;
215 min_val = std::min(min_val, table_[i * w_ + h]);
216 }
217 return min_val;
218 }
219
223 void merge(const Count_Min_Sketch & other)
224 {
225 ah_domain_error_if(w_ != other.w_ or d_ != other.d_)
226 << "Count_Min_Sketch::merge: dimension mismatch";
227
228 for (size_t i = 0; i < d_; ++i)
229 ah_domain_error_if(seeds_(i) != other.seeds_[i])
230 << "Count_Min_Sketch::merge: hash seed mismatch at index " << i;
231
232 constexpr CounterT max_val = std::numeric_limits<CounterT>::max();
233
234 for (size_t i = 0; i < table_.size(); ++i)
235 {
236 if (max_val - table_[i] < other.table_[i])
237 table_(i) = max_val;
238 else
239 table_(i) += other.table_[i];
240 }
241
242 if (max_val - total_count_ < other.total_count_)
243 total_count_ = max_val;
244 else
245 total_count_ += other.total_count_;
246 }
247
249 void clear()
250 {
251 for (size_t i = 0; i < table_.size(); ++i)
252 table_(i) = 0;
253 total_count_ = 0;
254 }
255
261 [[nodiscard]] size_t width() const noexcept { return w_; }
262
268 [[nodiscard]] size_t depth() const noexcept { return d_; }
269
275 [[nodiscard]] CounterT total_count() const noexcept { return total_count_; }
276 };
277
278} // namespace Aleph
279
280# endif // COUNT_MIN_SKETCH_H
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:670
long double h
Definition btreepic.C:154
long double w
Definition btreepic.C:153
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:141
void swap(Array &s) noexcept
Swap this with s
Definition tpl_array.H:313
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:331
void reserve(size_t cap)
Reserves cap cells into the array.
Definition tpl_array.H:402
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
Definition ah-zip.H:152
size_t dft_hash_fct(const Key &key) noexcept
Primary default hash: best speed/quality trade-off.
Definition hash-fct.H:1030
size_t murmur3hash(const Key &key, std::uint32_t seed)
Definition hash-fct.H:361
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
Dynamic array container with automatic resizing.