1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//! [`TopK`] — bounded top-K counter (Misra-Gries / Space-Saving).
use std::{collections::HashMap, hash::Hash};
/// Bounded "top K by count" tracker.
///
/// Exact when fewer than `k` distinct keys have been observed.
/// Above that, applies the **Misra-Gries** algorithm: when a new
/// distinct key arrives and the table is full, every counter is
/// decremented by one and any counter that hits 0 is evicted to
/// make room.
///
/// Worst-case overestimate for any key (real or estimated) is
/// bounded by the minimum retained count.
#[derive(Clone)]
pub struct TopK<K: Hash + Eq + Clone> {
k: usize,
counts: HashMap<K, u64>,
}
impl<K: Hash + Eq + Clone> TopK<K> {
pub fn new(k: usize) -> Self {
assert!(k > 0, "k must be > 0");
Self {
k,
counts: HashMap::new(),
}
}
/// Convenience constructor with `k = usize::MAX` — every
/// observed key is retained, no Misra-Gries eviction ever
/// fires. Useful for offline / bounded-input contexts where
/// memory pressure isn't a concern.
///
/// Prefer [`Self::new`] with an explicit cap when memory
/// pressure matters. New in 0.12.0 (plan 130).
pub fn new_unbounded() -> Self {
Self::new(usize::MAX)
}
/// Observe `key` once.
pub fn observe(&mut self, key: K) {
self.observe_n(key, 1);
}
/// Observe `key` with weight `count` (≥ 1).
pub fn observe_n(&mut self, key: K, count: u64) {
if count == 0 {
return;
}
if let Some(c) = self.counts.get_mut(&key) {
*c += count;
return;
}
if self.counts.len() < self.k {
self.counts.insert(key, count);
return;
}
// Table is full: decrement every counter by `count`,
// evict any that hit 0.
let mut to_evict: Vec<K> = Vec::new();
for (k, c) in self.counts.iter_mut() {
if *c <= count {
to_evict.push(k.clone());
} else {
*c -= count;
}
}
for k in to_evict {
self.counts.remove(&k);
}
// If we made room, insert with the residual count.
if self.counts.len() < self.k {
self.counts.insert(key, count);
}
}
/// Top entries sorted by count descending.
pub fn top(&self) -> Vec<(&K, u64)> {
let mut v: Vec<(&K, u64)> = self.counts.iter().map(|(k, c)| (k, *c)).collect();
v.sort_by_key(|b| std::cmp::Reverse(b.1));
v
}
/// Estimated count for `key`. Returns `0` for keys not in the
/// table — note that an evicted key may have been observed
/// many times; the table can't distinguish "never seen" from
/// "evicted".
pub fn estimate(&self, key: &K) -> u64 {
self.counts.get(key).copied().unwrap_or(0)
}
pub fn clear(&mut self) {
self.counts.clear();
}
pub fn len(&self) -> usize {
self.counts.len()
}
pub fn is_empty(&self) -> bool {
self.counts.is_empty()
}
}
impl<K: Hash + Eq + Clone> crate::correlate::Mergeable for TopK<K> {
/// Misra-Gries / Space-Saving merge (Metwally, Agrawal, Abbadi
/// PODS '05): sum counters for shared keys, then re-truncate
/// to capacity by Misra-Gries decrement (subtract the
/// (k+1)-th largest from all counters, evict the zeroed
/// ones).
///
/// **Capacity invariant**: both shards must share the same
/// `k`. Panics on mismatch — this is a config bug, not a
/// runtime condition we silently paper over.
fn merge(&mut self, other: Self) {
assert_eq!(
self.k, other.k,
"TopK::merge requires matching k (lhs={}, rhs={})",
self.k, other.k
);
// 1. Sum counters for shared keys; add new keys.
for (k, c) in other.counts {
*self.counts.entry(k).or_insert(0) += c;
}
// 2. Re-truncate to capacity (if we overflowed).
if self.counts.len() <= self.k {
return;
}
// Pivot on the (k+1)-th largest count; subtract it from
// every counter, drop the zeroed ones. This preserves
// the Misra-Gries error bound.
let mut counts: Vec<u64> = self.counts.values().copied().collect();
counts.sort_unstable_by(|a, b| b.cmp(a)); // descending
let pivot = counts[self.k];
let to_remove: Vec<K> = self
.counts
.iter()
.filter(|&(_, &c)| c <= pivot)
.map(|(k, _)| k.clone())
.collect();
for k in to_remove {
self.counts.remove(&k);
}
for c in self.counts.values_mut() {
*c = c.saturating_sub(pivot);
}
}
}
impl<K: Hash + Eq + Clone + std::fmt::Debug> std::fmt::Debug for TopK<K> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("TopK")
.field("k", &self.k)
.field("counts", &self.counts)
.finish()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn exact_counts_when_under_capacity() {
let mut t: TopK<u32> = TopK::new(4);
for &k in &[1, 1, 2, 3, 3, 3] {
t.observe(k);
}
assert_eq!(t.estimate(&1), 2);
assert_eq!(t.estimate(&2), 1);
assert_eq!(t.estimate(&3), 3);
}
#[test]
fn top_returns_descending() {
let mut t: TopK<u32> = TopK::new(4);
for &k in &[1, 1, 2, 3, 3, 3] {
t.observe(k);
}
let top = t.top();
assert_eq!(top.len(), 3);
assert_eq!(*top[0].0, 3);
assert_eq!(*top[1].0, 1);
assert_eq!(*top[2].0, 2);
}
#[test]
fn over_capacity_keeps_heaviest_hitters() {
let mut t: TopK<u32> = TopK::new(2);
// 3 distinct keys with 1, 2, 3 occurrences.
t.observe_n(1, 1);
t.observe_n(2, 2);
t.observe_n(3, 3);
let top = t.top();
// Misra-Gries: 1 is evicted via decrement, 3 dominates.
assert!(top.iter().any(|(k, _)| **k == 3));
}
#[test]
fn clear_resets_state() {
let mut t: TopK<u32> = TopK::new(2);
t.observe(1);
t.clear();
assert!(t.is_empty());
assert_eq!(t.estimate(&1), 0);
}
#[test]
fn observe_n_zero_is_noop() {
let mut t: TopK<u32> = TopK::new(2);
t.observe_n(1, 0);
assert!(t.is_empty());
}
#[test]
fn new_unbounded_never_evicts() {
let mut t: TopK<u32> = TopK::new_unbounded();
for i in 0..1000 {
t.observe(i);
}
// Every key kept with exact count 1 — no Misra-Gries
// eviction.
for i in 0..1000 {
assert_eq!(t.estimate(&i), 1);
}
}
}