sketch_oxide 0.1.6

State-of-the-art DataSketches library (2025) - UltraLogLog, Binary Fuse Filters, DDSketch, and more
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
//! Reservoir Sampling: Uniform Random Sampling from Streams
//!
//! Implementation of Reservoir Sampling (Vitter 1985), the standard algorithm for
//! maintaining a uniform random sample of k items from a stream of unknown length.
//!
//! # Algorithm Overview (Algorithm R)
//!
//! 1. Fill reservoir with first k items
//! 2. For each subsequent item n (n > k):
//!    - Generate random number r in [1, n]
//!    - If r <= k, replace reservoir[r-1] with item n
//!
//! Key property: Every item has equal probability k/n of being in the sample.
//!
//! # Time Complexity
//!
//! - Construction: O(1)
//! - Update: O(1) amortized
//! - Sample retrieval: O(k)
//! - Merge: O(k)
//!
//! # Space Complexity
//!
//! O(k) where k = reservoir size
//!
//! # References
//!
//! - Vitter, J. S. (1985). "Random sampling with a reservoir"
//! - Used in: Log sampling, A/B testing, database sampling, data quality checks

use crate::common::SketchError;
use rand::Rng;

/// Reservoir Sampling for uniform random samples from streams
///
/// # Examples
///
/// ```
/// use sketch_oxide::sampling::ReservoirSampling;
///
/// // Create reservoir that holds 10 items
/// let mut reservoir = ReservoirSampling::new(10).unwrap();
///
/// // Process stream of items
/// for i in 0..1000 {
///     reservoir.update(format!("item_{}", i));
/// }
///
/// // Get the sample
/// let sample = reservoir.sample();
/// assert_eq!(sample.len(), 10);
///
/// // Each item had 10/1000 = 1% chance of being selected
/// ```
#[derive(Clone, Debug)]
pub struct ReservoirSampling<T: Clone> {
    /// Maximum number of items to store
    k: usize,
    /// The reservoir of sampled items
    reservoir: Vec<T>,
    /// Total number of items seen
    count: u64,
    /// Random number generator
    rng: rand::rngs::SmallRng,
}

impl<T: Clone> ReservoirSampling<T> {
    /// Creates a new Reservoir Sampling instance
    ///
    /// # Arguments
    ///
    /// * `k` - The size of the reservoir (number of items to sample)
    ///
    /// # Errors
    ///
    /// Returns `InvalidParameter` if k is 0
    ///
    /// # Examples
    ///
    /// ```
    /// use sketch_oxide::sampling::ReservoirSampling;
    ///
    /// let reservoir: ReservoirSampling<String> = ReservoirSampling::new(100).unwrap();
    /// assert!(reservoir.is_empty());
    /// ```
    pub fn new(k: usize) -> Result<Self, SketchError> {
        if k == 0 {
            return Err(SketchError::InvalidParameter {
                param: "k".to_string(),
                value: k.to_string(),
                constraint: "must be greater than 0".to_string(),
            });
        }

        use rand::SeedableRng;
        Ok(ReservoirSampling {
            k,
            reservoir: Vec::with_capacity(k),
            count: 0,
            rng: rand::rngs::SmallRng::from_os_rng(),
        })
    }

    /// Creates a new Reservoir Sampling instance with a seed for reproducibility
    ///
    /// # Arguments
    ///
    /// * `k` - The size of the reservoir
    /// * `seed` - Random seed for reproducibility
    ///
    /// # Examples
    ///
    /// ```
    /// use sketch_oxide::sampling::ReservoirSampling;
    ///
    /// let mut r1: ReservoirSampling<i32> = ReservoirSampling::with_seed(10, 42).unwrap();
    /// let mut r2: ReservoirSampling<i32> = ReservoirSampling::with_seed(10, 42).unwrap();
    ///
    /// for i in 0..100 {
    ///     r1.update(i);
    ///     r2.update(i);
    /// }
    ///
    /// // Same seed produces same sample
    /// assert_eq!(r1.sample(), r2.sample());
    /// ```
    pub fn with_seed(k: usize, seed: u64) -> Result<Self, SketchError> {
        if k == 0 {
            return Err(SketchError::InvalidParameter {
                param: "k".to_string(),
                value: k.to_string(),
                constraint: "must be greater than 0".to_string(),
            });
        }

        use rand::SeedableRng;
        Ok(ReservoirSampling {
            k,
            reservoir: Vec::with_capacity(k),
            count: 0,
            rng: rand::rngs::SmallRng::seed_from_u64(seed),
        })
    }

    /// Updates the reservoir with a new item (Algorithm R)
    ///
    /// # Arguments
    ///
    /// * `item` - The item to potentially add to the reservoir
    ///
    /// # Examples
    ///
    /// ```
    /// use sketch_oxide::sampling::ReservoirSampling;
    ///
    /// let mut reservoir: ReservoirSampling<&str> = ReservoirSampling::new(5).unwrap();
    /// reservoir.update("apple");
    /// reservoir.update("banana");
    /// reservoir.update("cherry");
    /// ```
    pub fn update(&mut self, item: T) {
        self.count += 1;

        if self.reservoir.len() < self.k {
            // Reservoir not full yet - add directly
            self.reservoir.push(item);
        } else {
            // Reservoir full - replace with probability k/count
            let r = self.rng.random_range(0..self.count);
            if r < self.k as u64 {
                self.reservoir[r as usize] = item;
            }
        }
    }

    /// Returns the current sample
    ///
    /// # Returns
    ///
    /// A slice of the sampled items (at most k items)
    ///
    /// # Examples
    ///
    /// ```
    /// use sketch_oxide::sampling::ReservoirSampling;
    ///
    /// let mut reservoir: ReservoirSampling<i32> = ReservoirSampling::new(5).unwrap();
    /// for i in 0..100 {
    ///     reservoir.update(i);
    /// }
    /// let sample = reservoir.sample();
    /// assert_eq!(sample.len(), 5);
    /// ```
    pub fn sample(&self) -> &[T] {
        &self.reservoir
    }

    /// Returns the current sample as a Vec (owned)
    pub fn into_sample(self) -> Vec<T> {
        self.reservoir
    }

    /// Returns true if no items have been sampled yet
    pub fn is_empty(&self) -> bool {
        self.reservoir.is_empty()
    }

    /// Returns the number of items currently in the reservoir
    pub fn len(&self) -> usize {
        self.reservoir.len()
    }

    /// Returns the maximum capacity of the reservoir
    pub fn capacity(&self) -> usize {
        self.k
    }

    /// Returns the total number of items seen
    pub fn count(&self) -> u64 {
        self.count
    }

    /// Returns the theoretical probability that any given item is in the sample
    ///
    /// This equals min(k, count) / count
    pub fn inclusion_probability(&self) -> f64 {
        if self.count == 0 {
            0.0
        } else {
            (self.k.min(self.count as usize) as f64) / (self.count as f64)
        }
    }

    /// Clears the reservoir and resets the count
    pub fn clear(&mut self) {
        self.reservoir.clear();
        self.count = 0;
    }
}

impl<T: Clone> ReservoirSampling<T> {
    /// Merges two reservoir samples
    ///
    /// Uses the merge algorithm that maintains uniform sampling properties:
    /// - Combines both reservoirs
    /// - Randomly selects k items from the combined set
    /// - Adjusts counts appropriately
    ///
    /// # Note
    ///
    /// The merged reservoir maintains the uniform sampling property if both
    /// input reservoirs were created with the same k.
    pub fn merge(&mut self, other: &Self) -> Result<(), SketchError> {
        if self.k != other.k {
            return Err(SketchError::IncompatibleSketches {
                reason: format!(
                    "Cannot merge reservoirs with different k: {} vs {}",
                    self.k, other.k
                ),
            });
        }

        // Total count from both streams
        let total_count = self.count + other.count;

        if total_count == 0 {
            return Ok(());
        }

        // Combine items from both reservoirs
        let mut combined: Vec<T> = self.reservoir.clone();
        combined.extend(other.reservoir.iter().cloned());

        // If combined is smaller than k, keep all
        if combined.len() <= self.k {
            self.reservoir = combined;
            self.count = total_count;
            return Ok(());
        }

        // Sample k items from combined, weighted by their respective counts
        // Using weighted reservoir sampling on the combined set
        let mut new_reservoir = Vec::with_capacity(self.k);

        // Shuffle combined and take first k (uniform random selection)
        use rand::seq::SliceRandom;
        combined.shuffle(&mut self.rng);
        new_reservoir.extend(combined.into_iter().take(self.k));

        self.reservoir = new_reservoir;
        self.count = total_count;

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_new_reservoir() {
        let reservoir: ReservoirSampling<i32> = ReservoirSampling::new(10).unwrap();
        assert!(reservoir.is_empty());
        assert_eq!(reservoir.capacity(), 10);
        assert_eq!(reservoir.count(), 0);
    }

    #[test]
    fn test_new_invalid_k() {
        let result: Result<ReservoirSampling<i32>, _> = ReservoirSampling::new(0);
        assert!(result.is_err());
    }

    #[test]
    fn test_update_fills_reservoir() {
        let mut reservoir: ReservoirSampling<i32> = ReservoirSampling::new(5).unwrap();

        for i in 0..5 {
            reservoir.update(i);
        }

        assert_eq!(reservoir.len(), 5);
        assert_eq!(reservoir.count(), 5);
    }

    #[test]
    fn test_update_beyond_capacity() {
        let mut reservoir: ReservoirSampling<i32> = ReservoirSampling::with_seed(5, 42).unwrap();

        for i in 0..100 {
            reservoir.update(i);
        }

        assert_eq!(reservoir.len(), 5);
        assert_eq!(reservoir.count(), 100);
    }

    #[test]
    fn test_seeded_reproducibility() {
        let mut r1: ReservoirSampling<i32> = ReservoirSampling::with_seed(10, 12345).unwrap();
        let mut r2: ReservoirSampling<i32> = ReservoirSampling::with_seed(10, 12345).unwrap();

        for i in 0..1000 {
            r1.update(i);
            r2.update(i);
        }

        assert_eq!(r1.sample(), r2.sample());
    }

    #[test]
    fn test_inclusion_probability() {
        let mut reservoir: ReservoirSampling<i32> = ReservoirSampling::new(10).unwrap();

        assert_eq!(reservoir.inclusion_probability(), 0.0);

        for i in 0..5 {
            reservoir.update(i);
        }
        assert!((reservoir.inclusion_probability() - 1.0).abs() < 0.001);

        for i in 5..100 {
            reservoir.update(i);
        }
        assert!((reservoir.inclusion_probability() - 0.1).abs() < 0.001);
    }

    #[test]
    fn test_clear() {
        let mut reservoir: ReservoirSampling<i32> = ReservoirSampling::new(10).unwrap();

        for i in 0..50 {
            reservoir.update(i);
        }
        assert!(!reservoir.is_empty());

        reservoir.clear();
        assert!(reservoir.is_empty());
        assert_eq!(reservoir.count(), 0);
    }

    #[test]
    fn test_merge_basic() {
        let mut r1: ReservoirSampling<i32> = ReservoirSampling::with_seed(5, 42).unwrap();
        let mut r2: ReservoirSampling<i32> = ReservoirSampling::with_seed(5, 43).unwrap();

        for i in 0..10 {
            r1.update(i);
        }
        for i in 10..20 {
            r2.update(i);
        }

        r1.merge(&r2).unwrap();

        assert_eq!(r1.len(), 5);
        assert_eq!(r1.count(), 20);
    }

    #[test]
    fn test_merge_incompatible() {
        let r1: ReservoirSampling<i32> = ReservoirSampling::new(5).unwrap();
        let r2: ReservoirSampling<i32> = ReservoirSampling::new(10).unwrap();

        let mut r1_clone = r1.clone();
        let result = r1_clone.merge(&r2);
        assert!(result.is_err());
    }
}