bloom-lib 1.0.0

Probabilistic data structure library: Bloom filters, Cuckoo filters, Count-Min Sketch, HyperLogLog, MinHash, and Top-K. Tunable false-positive rates, serializable state, merge support, and streaming-safe updates.
Documentation
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
//! A Count-Min Sketch for approximate frequency estimation.

use core::{hash::BuildHasher, marker::PhantomData};

use alloc::{vec, vec::Vec};

use crate::{
    hash::{reduce, DefaultHashBuilder, HashPair},
    Error,
};

/// Euler's number, used to size the sketch width from a target error.
const E: f64 = core::f64::consts::E;

/// A sublinear-space frequency estimator.
///
/// A Count-Min Sketch counts how many times it has seen each item using a fixed
/// grid of counters, far smaller than a real `HashMap<T, u64>` would be. Each
/// item is hashed into one counter per row and those counters are incremented;
/// the estimate for an item is the *minimum* across its rows. The estimate never
/// undercounts and overcounts by a bounded amount with high probability — the
/// width controls the error magnitude `epsilon`, the depth controls the
/// confidence `delta`.
///
/// The sketch is generic over the item type `T` and a
/// [`BuildHasher`](core::hash::BuildHasher) `S`, defaulting to the deterministic
/// [`DefaultHashBuilder`](crate::hash::DefaultHashBuilder).
///
/// # Examples
///
/// ```
/// use bloom_lib::CountMinSketch;
///
/// // ~0.1% error with 99.9% confidence.
/// let mut sketch = CountMinSketch::new(0.001, 0.001).unwrap();
///
/// sketch.increment("apple");
/// sketch.add("apple", 4);
/// sketch.increment("banana");
///
/// assert!(sketch.estimate("apple") >= 5); // never undercounts
/// assert_eq!(sketch.estimate("cherry"), 0);
/// ```
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CountMinSketch<T: ?Sized, S = DefaultHashBuilder> {
    counters: Vec<u64>,
    width: usize,
    depth: usize,
    total: u64,
    #[cfg_attr(feature = "serde", serde(skip))]
    hasher: S,
    #[cfg_attr(feature = "serde", serde(skip))]
    _marker: PhantomData<fn(&T)>,
}

impl<T: ?Sized> CountMinSketch<T, DefaultHashBuilder> {
    /// Creates a sketch sized for an error factor `epsilon` at confidence
    /// `1 - delta`, using the default hasher.
    ///
    /// The estimate for any item never undercounts and, with probability at
    /// least `1 - delta`, overcounts by at most `epsilon * N`, where `N` is the
    /// total of all counts added. Smaller `epsilon` widens the grid; smaller
    /// `delta` deepens it.
    ///
    /// # Parameters
    ///
    /// - `epsilon`: the relative error factor. Must be in `(0.0, 1.0)`. The
    ///   width is `ceil(e / epsilon)`.
    /// - `delta`: the failure probability. Must be in `(0.0, 1.0)`. The depth is
    ///   `ceil(ln(1 / delta))`.
    ///
    /// # Errors
    ///
    /// Returns [`Error::InvalidParameter`] if either argument is not a finite
    /// value in the open interval `(0.0, 1.0)`.
    ///
    /// # Examples
    ///
    /// ```
    /// use bloom_lib::CountMinSketch;
    ///
    /// let sketch = CountMinSketch::<&str>::new(0.01, 0.01).unwrap();
    /// assert!(sketch.width() >= 271);
    /// assert!(sketch.depth() >= 4);
    /// ```
    pub fn new(epsilon: f64, delta: f64) -> Result<Self, Error> {
        Self::with_hasher(epsilon, delta, DefaultHashBuilder)
    }

    /// Creates a sketch with an explicit `width` and `depth`, using the default
    /// hasher.
    ///
    /// # Parameters
    ///
    /// - `width`: counters per row. Must be non-zero.
    /// - `depth`: number of rows (independent hashes). Must be non-zero.
    ///
    /// # Errors
    ///
    /// Returns [`Error::InvalidParameter`] if either argument is zero.
    ///
    /// # Examples
    ///
    /// ```
    /// use bloom_lib::CountMinSketch;
    ///
    /// let sketch = CountMinSketch::<u32>::with_dimensions(2_048, 5).unwrap();
    /// assert_eq!(sketch.width(), 2_048);
    /// assert_eq!(sketch.depth(), 5);
    /// ```
    pub fn with_dimensions(width: usize, depth: usize) -> Result<Self, Error> {
        Self::with_dimensions_and_hasher(width, depth, DefaultHashBuilder)
    }
}

impl<T: ?Sized, S: BuildHasher> CountMinSketch<T, S> {
    /// Creates a sketch from `epsilon`/`delta` with a caller-supplied hasher.
    ///
    /// # Errors
    ///
    /// Returns [`Error::InvalidParameter`] if either argument is not a finite
    /// value in `(0.0, 1.0)`.
    ///
    /// # Examples
    ///
    /// ```
    /// # #[cfg(feature = "std")] {
    /// use std::collections::hash_map::RandomState;
    /// use bloom_lib::CountMinSketch;
    ///
    /// let sketch: CountMinSketch<&str, RandomState> =
    ///     CountMinSketch::with_hasher(0.01, 0.01, RandomState::new()).unwrap();
    /// # }
    /// ```
    pub fn with_hasher(epsilon: f64, delta: f64, hasher: S) -> Result<Self, Error> {
        if !(epsilon.is_finite() && epsilon > 0.0 && epsilon < 1.0) {
            return Err(Error::InvalidParameter {
                param: "epsilon",
                reason: "must be a finite value in the open interval (0.0, 1.0)",
            });
        }
        if !(delta.is_finite() && delta > 0.0 && delta < 1.0) {
            return Err(Error::InvalidParameter {
                param: "delta",
                reason: "must be a finite value in the open interval (0.0, 1.0)",
            });
        }

        let width = libm::ceil(E / epsilon) as usize;
        let depth = libm::ceil(libm::log(1.0 / delta)) as usize;
        Self::with_dimensions_and_hasher(width.max(1), depth.max(1), hasher)
    }

    /// Creates a sketch with an explicit geometry and a caller-supplied hasher.
    ///
    /// # Errors
    ///
    /// Returns [`Error::InvalidParameter`] if `width` or `depth` is zero.
    pub fn with_dimensions_and_hasher(
        width: usize,
        depth: usize,
        hasher: S,
    ) -> Result<Self, Error> {
        if width == 0 {
            return Err(Error::InvalidParameter {
                param: "width",
                reason: "must be greater than zero",
            });
        }
        if depth == 0 {
            return Err(Error::InvalidParameter {
                param: "depth",
                reason: "must be greater than zero",
            });
        }

        Ok(Self {
            counters: vec![0u64; width * depth],
            width,
            depth,
            total: 0,
            hasher,
            _marker: PhantomData,
        })
    }

    /// Records `count` additional occurrences of `item`.
    ///
    /// Counters saturate at [`u64::MAX`] rather than overflowing, so an
    /// adversarial or runaway stream degrades gracefully instead of panicking or
    /// wrapping.
    ///
    /// # Examples
    ///
    /// ```
    /// use bloom_lib::CountMinSketch;
    ///
    /// let mut sketch = CountMinSketch::new(0.01, 0.01).unwrap();
    /// sketch.add("page-view", 250);
    /// assert!(sketch.estimate("page-view") >= 250);
    /// ```
    pub fn add(&mut self, item: &T, count: u64)
    where
        T: core::hash::Hash,
    {
        let pair = HashPair::new(item, &self.hasher);
        let width = self.width as u64;
        for row in 0..self.depth {
            let column = reduce(pair.nth(row as u64), width) as usize;
            let cell = &mut self.counters[row * self.width + column];
            *cell = cell.saturating_add(count);
        }
        self.total = self.total.saturating_add(count);
    }

    /// Records a single occurrence of `item`. Shorthand for `add(item, 1)`.
    ///
    /// # Examples
    ///
    /// ```
    /// use bloom_lib::CountMinSketch;
    ///
    /// let mut sketch = CountMinSketch::new(0.01, 0.01).unwrap();
    /// sketch.increment("hit");
    /// sketch.increment("hit");
    /// assert!(sketch.estimate("hit") >= 2);
    /// ```
    #[inline]
    pub fn increment(&mut self, item: &T)
    where
        T: core::hash::Hash,
    {
        self.add(item, 1);
    }

    /// Estimates the number of times `item` has been added.
    ///
    /// The estimate is the minimum counter across all rows. It never undercounts
    /// the true total and overcounts only by the sketch's error bound.
    ///
    /// # Examples
    ///
    /// ```
    /// use bloom_lib::CountMinSketch;
    ///
    /// let mut sketch = CountMinSketch::new(0.001, 0.001).unwrap();
    /// for _ in 0..100 {
    ///     sketch.increment("frequent");
    /// }
    /// let estimate = sketch.estimate("frequent");
    /// assert!((100..=110).contains(&estimate));
    /// ```
    #[must_use]
    pub fn estimate(&self, item: &T) -> u64
    where
        T: core::hash::Hash,
    {
        let pair = HashPair::new(item, &self.hasher);
        let width = self.width as u64;
        let mut min = u64::MAX;
        for row in 0..self.depth {
            let column = reduce(pair.nth(row as u64), width) as usize;
            let value = self.counters[row * self.width + column];
            if value < min {
                min = value;
            }
        }
        min
    }

    /// The sum of every count added (saturating).
    ///
    /// Unlike per-item estimates, this is exact up to saturation, because every
    /// `add` contributes to it directly.
    ///
    /// # Examples
    ///
    /// ```
    /// use bloom_lib::CountMinSketch;
    ///
    /// let mut sketch = CountMinSketch::new(0.01, 0.01).unwrap();
    /// sketch.add("a", 3);
    /// sketch.add("b", 7);
    /// assert_eq!(sketch.total_count(), 10);
    /// ```
    #[inline]
    #[must_use]
    pub fn total_count(&self) -> u64 {
        self.total
    }

    /// The number of counters per row.
    #[inline]
    #[must_use]
    pub fn width(&self) -> usize {
        self.width
    }

    /// The number of rows (independent hash functions).
    #[inline]
    #[must_use]
    pub fn depth(&self) -> usize {
        self.depth
    }

    /// Resets every counter to zero, retaining the allocation.
    ///
    /// # Examples
    ///
    /// ```
    /// use bloom_lib::CountMinSketch;
    ///
    /// let mut sketch = CountMinSketch::new(0.01, 0.01).unwrap();
    /// sketch.increment("x");
    /// sketch.clear();
    /// assert_eq!(sketch.estimate("x"), 0);
    /// assert_eq!(sketch.total_count(), 0);
    /// ```
    pub fn clear(&mut self) {
        self.counters.iter_mut().for_each(|cell| *cell = 0);
        self.total = 0;
    }

    /// Merges `other` into `self` by summing counters cell by cell (saturating).
    ///
    /// After the merge, the sketch estimates frequencies as if every item from
    /// both sketches had been added to one. Both sketches must share their
    /// geometry.
    ///
    /// # Errors
    ///
    /// Returns [`Error::IncompatibleParameters`] if the two sketches differ in
    /// width or depth.
    ///
    /// # Examples
    ///
    /// ```
    /// use bloom_lib::CountMinSketch;
    ///
    /// let mut a = CountMinSketch::with_dimensions(512, 4).unwrap();
    /// let mut b = CountMinSketch::with_dimensions(512, 4).unwrap();
    /// a.add("shared", 2);
    /// b.add("shared", 3);
    ///
    /// a.merge(&b).unwrap();
    /// assert!(a.estimate("shared") >= 5);
    /// ```
    pub fn merge(&mut self, other: &Self) -> Result<(), Error> {
        if self.width != other.width || self.depth != other.depth {
            return Err(Error::IncompatibleParameters);
        }
        for (dst, src) in self.counters.iter_mut().zip(other.counters.iter()) {
            *dst = dst.saturating_add(*src);
        }
        self.total = self.total.saturating_add(other.total);
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used)]

    use super::*;

    #[test]
    fn test_new_rejects_out_of_range() {
        assert!(matches!(
            CountMinSketch::<&str>::new(0.0, 0.1),
            Err(Error::InvalidParameter { .. })
        ));
        assert!(matches!(
            CountMinSketch::<&str>::new(0.1, 1.0),
            Err(Error::InvalidParameter { .. })
        ));
    }

    #[test]
    fn test_with_dimensions_rejects_zero() {
        assert!(matches!(
            CountMinSketch::<u8>::with_dimensions(0, 4),
            Err(Error::InvalidParameter { .. })
        ));
        assert!(matches!(
            CountMinSketch::<u8>::with_dimensions(64, 0),
            Err(Error::InvalidParameter { .. })
        ));
    }

    #[test]
    fn test_estimate_never_undercounts() {
        let mut sketch = CountMinSketch::new(0.001, 0.001).unwrap();
        for i in 0..1_000u32 {
            let count = u64::from(i % 7) + 1;
            sketch.add(&i, count);
        }
        for i in 0..1_000u32 {
            let truth = u64::from(i % 7) + 1;
            assert!(
                sketch.estimate(&i) >= truth,
                "estimate undercounted item {i}"
            );
        }
    }

    #[test]
    fn test_absent_item_estimates_low() {
        let mut sketch = CountMinSketch::new(0.001, 0.001).unwrap();
        for i in 0..100u32 {
            sketch.increment(&i);
        }
        // An item never added estimates zero in a lightly-loaded sketch.
        assert_eq!(sketch.estimate(&9_999u32), 0);
    }

    #[test]
    fn test_total_count_is_exact() {
        let mut sketch = CountMinSketch::new(0.01, 0.01).unwrap();
        sketch.add("a", 10);
        sketch.add("b", 20);
        sketch.increment("c");
        assert_eq!(sketch.total_count(), 31);
    }

    #[test]
    fn test_saturating_add() {
        let mut sketch = CountMinSketch::<str>::with_dimensions(16, 2).unwrap();
        sketch.add("x", u64::MAX);
        sketch.add("x", 5);
        assert_eq!(sketch.estimate("x"), u64::MAX);
        assert_eq!(sketch.total_count(), u64::MAX);
    }

    #[test]
    fn test_clear() {
        let mut sketch = CountMinSketch::new(0.01, 0.01).unwrap();
        sketch.add("x", 9);
        sketch.clear();
        assert_eq!(sketch.estimate("x"), 0);
        assert_eq!(sketch.total_count(), 0);
    }

    #[test]
    fn test_merge_sums_counts() {
        let mut a = CountMinSketch::with_dimensions(512, 4).unwrap();
        let mut b = CountMinSketch::with_dimensions(512, 4).unwrap();
        a.add("shared", 2);
        b.add("shared", 3);
        a.merge(&b).unwrap();
        assert!(a.estimate("shared") >= 5);
        assert_eq!(a.total_count(), 5);
    }

    #[test]
    fn test_merge_rejects_incompatible() {
        let mut a = CountMinSketch::<&str>::with_dimensions(512, 4).unwrap();
        let b = CountMinSketch::<&str>::with_dimensions(256, 4).unwrap();
        assert_eq!(a.merge(&b), Err(Error::IncompatibleParameters));
    }
}