@@ -54,6 +54,14 @@ impl CountMinSketch {
5454 ///
5555 /// Panics if `num_hashes` is 0, `num_buckets` is less than 3, or the
5656 /// total table size exceeds the supported limit.
57+ ///
58+ /// # Examples
59+ ///
60+ /// ```rust
61+ /// # use datasketches::countmin::CountMinSketch;
62+ /// let sketch = CountMinSketch::new(4, 128);
63+ /// assert_eq!(sketch.num_buckets(), 128);
64+ /// ```
5765 pub fn new ( num_hashes : u8 , num_buckets : u32 ) -> Self {
5866 Self :: with_seed ( num_hashes, num_buckets, DEFAULT_UPDATE_SEED )
5967 }
@@ -64,6 +72,14 @@ impl CountMinSketch {
6472 ///
6573 /// Panics if `num_hashes` is 0, `num_buckets` is less than 3, or the
6674 /// total table size exceeds the supported limit.
75+ ///
76+ /// # Examples
77+ ///
78+ /// ```rust
79+ /// # use datasketches::countmin::CountMinSketch;
80+ /// let sketch = CountMinSketch::with_seed(4, 64, 42);
81+ /// assert_eq!(sketch.seed(), 42);
82+ /// ```
6783 pub fn with_seed ( num_hashes : u8 , num_buckets : u32 , seed : u64 ) -> Self {
6884 let entries = entries_for_config ( num_hashes, num_buckets) ;
6985 Self :: make ( num_hashes, num_buckets, seed, entries)
@@ -127,11 +143,29 @@ impl CountMinSketch {
127143 }
128144
129145 /// Updates the sketch with a single occurrence of the item.
146+ ///
147+ /// # Examples
148+ ///
149+ /// ```rust
150+ /// # use datasketches::countmin::CountMinSketch;
151+ /// let mut sketch = CountMinSketch::new(4, 128);
152+ /// sketch.update("apple");
153+ /// assert!(sketch.estimate("apple") >= 1);
154+ /// ```
130155 pub fn update < T : Hash > ( & mut self , item : T ) {
131156 self . update_with_weight ( item, 1 ) ;
132157 }
133158
134159 /// Updates the sketch with the given item and weight.
160+ ///
161+ /// # Examples
162+ ///
163+ /// ```rust
164+ /// # use datasketches::countmin::CountMinSketch;
165+ /// let mut sketch = CountMinSketch::new(4, 128);
166+ /// sketch.update_with_weight("banana", 3);
167+ /// assert!(sketch.estimate("banana") >= 3);
168+ /// ```
135169 pub fn update_with_weight < T : Hash > ( & mut self , item : T , weight : i64 ) {
136170 if weight == 0 {
137171 return ;
@@ -147,6 +181,15 @@ impl CountMinSketch {
147181 }
148182
149183 /// Returns the estimated frequency of the given item.
184+ ///
185+ /// # Examples
186+ ///
187+ /// ```rust
188+ /// # use datasketches::countmin::CountMinSketch;
189+ /// let mut sketch = CountMinSketch::new(4, 128);
190+ /// sketch.update_with_weight("pear", 2);
191+ /// assert!(sketch.estimate("pear") >= 2);
192+ /// ```
150193 pub fn estimate < T : Hash > ( & self , item : T ) -> i64 {
151194 let num_buckets = self . num_buckets as usize ;
152195 let mut min = i64:: MAX ;
@@ -178,6 +221,20 @@ impl CountMinSketch {
178221 /// # Panics
179222 ///
180223 /// Panics if the sketches have incompatible configurations.
224+ ///
225+ /// # Examples
226+ ///
227+ /// ```rust
228+ /// # use datasketches::countmin::CountMinSketch;
229+ /// let mut left = CountMinSketch::new(4, 128);
230+ /// let mut right = CountMinSketch::new(4, 128);
231+ ///
232+ /// left.update("apple");
233+ /// right.update_with_weight("banana", 2);
234+ ///
235+ /// left.merge(&right);
236+ /// assert!(left.estimate("banana") >= 2);
237+ /// ```
181238 pub fn merge ( & mut self , other : & CountMinSketch ) {
182239 if std:: ptr:: eq ( self , other) {
183240 panic ! ( "Cannot merge a sketch with itself." ) ;
@@ -195,6 +252,17 @@ impl CountMinSketch {
195252 }
196253
197254 /// Serializes this sketch into the DataSketches Count-Min format.
255+ ///
256+ /// # Examples
257+ ///
258+ /// ```rust
259+ /// # use datasketches::countmin::CountMinSketch;
260+ /// # let mut sketch = CountMinSketch::new(4, 128);
261+ /// # sketch.update("apple");
262+ /// let bytes = sketch.serialize();
263+ /// let decoded = CountMinSketch::deserialize(&bytes).unwrap();
264+ /// assert!(decoded.estimate("apple") >= 1);
265+ /// ```
198266 pub fn serialize ( & self ) -> Vec < u8 > {
199267 let header_size = PREAMBLE_LONGS_SHORT as usize * LONG_SIZE_BYTES ;
200268 let payload_size = if self . is_empty ( ) {
@@ -227,11 +295,33 @@ impl CountMinSketch {
227295 }
228296
229297 /// Deserializes a sketch from bytes using the default seed.
298+ ///
299+ /// # Examples
300+ ///
301+ /// ```rust
302+ /// # use datasketches::countmin::CountMinSketch;
303+ /// # let mut sketch = CountMinSketch::new(4, 64);
304+ /// # sketch.update("apple");
305+ /// # let bytes = sketch.serialize();
306+ /// let decoded = CountMinSketch::deserialize(&bytes).unwrap();
307+ /// assert!(decoded.estimate("apple") >= 1);
308+ /// ```
230309 pub fn deserialize ( bytes : & [ u8 ] ) -> Result < Self , Error > {
231310 Self :: deserialize_with_seed ( bytes, DEFAULT_UPDATE_SEED )
232311 }
233312
234313 /// Deserializes a sketch from bytes using the provided seed.
314+ ///
315+ /// # Examples
316+ ///
317+ /// ```rust
318+ /// # use datasketches::countmin::CountMinSketch;
319+ /// # let mut sketch = CountMinSketch::with_seed(4, 64, 7);
320+ /// # sketch.update("apple");
321+ /// # let bytes = sketch.serialize();
322+ /// let decoded = CountMinSketch::deserialize_with_seed(&bytes, 7).unwrap();
323+ /// assert!(decoded.estimate("apple") >= 1);
324+ /// ```
235325 pub fn deserialize_with_seed ( bytes : & [ u8 ] , seed : u64 ) -> Result < Self , Error > {
236326 fn make_error ( tag : & ' static str ) -> impl FnOnce ( std:: io:: Error ) -> Error {
237327 move |_| Error :: insufficient_data ( tag)
0 commit comments