streaming_algorithms  0.0.5
A collection of streaming data algorithms
cm_sketch.h
Go to the documentation of this file.
1 /* -*- Mode: C; tab_width: 8; indent_tabs_mode: nil; c_basic_offset: 2 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 /** Count-min sketch https://en.wikipedia.org/wiki/Count%E2%80%93min_sketch
8  * @file */
9 
10 #ifndef sa_cm_sketch_h_
11 #define sa_cm_sketch_h_
12 
13 #include <stdint.h>
14 
15 typedef struct sa_cm_sketch sa_cm_sketch;
16 
17 #ifdef __cplusplus
18 extern "C"
19 {
20 #endif
21 
22 /**
23  * Allocate and initialize the data structure.
24  *
25  * @param epsilon Approximation factor
26  *
27  * @param delta Probability of failure
28  *
29  * @return Count-min sketch struct
30  *
31  */
32 sa_cm_sketch* sa_create_cms(double epsilon, double delta);
33 
34 /**
35  * Zero out the data structure.
36  *
37  * @param cms Count-min sketch struct
38  */
39 void sa_init_cms(sa_cm_sketch *cms);
40 
41 /**
42  * Free the associated memory.
43  *
44  * @param cms Count-min sketch struct
45  *
46  */
47 void sa_destroy_cms(sa_cm_sketch *cms);
48 
49 /**
50  * Point query the frequency count of item.
51  *
52  * @param cms Count-min sketch struct
53  * @param item Item to query
54  * @param len Length of the item in bytes
55  *
56  * @return int Estimated count
57  */
58 uint32_t sa_point_query_cms(sa_cm_sketch *cms, void *item, size_t len);
59 
60 /**
61  * Increment/Decrement the Count-min sketch with the specified item and value.
62  *
63  * @param cms Count-min sketch struct
64  * @param item Item to add
65  * @param len Length of the item in bytes
66  * @param n Number of items to add/remove
67  *
68  * @return int Estimated count
69  */
70 uint32_t sa_update_cms(sa_cm_sketch *cms, void *item, size_t len, int n);
71 
72 /**
73  * Return the total number of items added to the sketch.
74  *
75  * @param cms Count-min sketch struct
76  *
77  * @return size_t Number of items added to the sketch
78  */
79 uint64_t sa_item_count_cms(sa_cm_sketch *cms);
80 
81 /**
82  * Return the total number of unique items added to the sketch.
83  *
84  * @param cms Count-min sketch struct
85  *
86  * @return size_t Number of unique items added to the sketch
87  */
88 uint64_t sa_unique_count_cms(sa_cm_sketch *cms);
89 
90 /**
91  * Serialize the internal state to a buffer.
92  *
93  * @param cms Count-min sketch struct
94  * @param len Length of the returned buffer
95  *
96  * @return char* Serialized representation MUST be freed by the caller
97  */
98 char* sa_serialize_cms(sa_cm_sketch *cms, size_t *len);
99 
100 /**
101  * Restore the internal state from the serialized output.
102  *
103  * @param cms Count-min sketch struct
104  * @param buf Buffer containing the output of serialize_cms
105  * @param len Length of the buffer
106  *
107  * @return 0 = success
108  * 1 = invalid buffer length
109  *
110  */
111 int sa_deserialize_cms(sa_cm_sketch *cms, const char *buf, size_t len);
112 
113 #ifdef __cplusplus
114 }
115 #endif
116 
117 #endif
struct sa_cm_sketch sa_cm_sketch
Definition: cm_sketch.h:15
void sa_init_cms(sa_cm_sketch *cms)
Zero out the data structure.
void sa_destroy_cms(sa_cm_sketch *cms)
Free the associated memory.
int sa_deserialize_cms(sa_cm_sketch *cms, const char *buf, size_t len)
Restore the internal state from the serialized output.
sa_cm_sketch * sa_create_cms(double epsilon, double delta)
Allocate and initialize the data structure.
uint32_t sa_update_cms(sa_cm_sketch *cms, void *item, size_t len, int n)
Increment/Decrement the Count-min sketch with the specified item and value.
uint64_t sa_unique_count_cms(sa_cm_sketch *cms)
Return the total number of unique items added to the sketch.
uint64_t sa_item_count_cms(sa_cm_sketch *cms)
Return the total number of items added to the sketch.
char * sa_serialize_cms(sa_cm_sketch *cms, size_t *len)
Serialize the internal state to a buffer.
uint32_t sa_point_query_cms(sa_cm_sketch *cms, void *item, size_t len)
Point query the frequency count of item.