streaming_algorithms  0.0.5
A collection of streaming data algorithms
p2.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 /** Piece_wise parabolic predicition (P2).
8  * Dynamic calculation of quantiles and histograms without storing
9  * observations. http://www.cs.wustl.edu/~jain/papers/ftp/psqr.pdf
10  * @file */
11 
12 #ifndef sa_p2_h_
13 #define sa_p2_h_
14 
15 #include <stddef.h>
16 
19 
20 #ifdef __cplusplus
21 extern "C"
22 {
23 #endif
24 
25 /**
26  * Allocates and initializes the data structure.
27  *
28  * @param p p_quantile to calculate (e.g. 0.5 == median)
29  */
31 
32 /**
33  * Zeros out the quantile.
34  *
35  * @param p2q Quantile struct
36  */
38 
39 /**
40  * Updates the quantile with the provided observation.
41  *
42  * @param p2q Quantile struct
43  * @param x Observation to add
44  * @return p_quantile estimate
45  *
46  */
47 double
48 sa_add_p2_quantile(sa_p2_quantile *p2q, double x);
49 
50 /**
51  * Returns the number of observations that are less than or equal to the marker.
52  *
53  * @param p2q Quantile struct
54  * @param marker Selects the percentile
55  * 0 = min
56  * 1 = p/2
57  * 2 = p
58  * 3 = (1+p)/2
59  * 4 = max
60  */
61 unsigned long long
62 sa_count_p2_quantile(sa_p2_quantile *p2q, unsigned short marker);
63 
64 /**
65  * Returns the estimated quantile value for the specified marker.
66  *
67  * @param p2q Quantile struct
68  * @param marker 0-4 see sa_count_p2_quantile
69  */
70 double
71 sa_estimate_p2_quantile(sa_p2_quantile *p2q, unsigned short marker);
72 
73 /**
74  * Free the associated memory.
75  *
76  * @param p2q Quantile struct
77  *
78  */
80 
81 /**
82  * Serialize the internal state to a buffer.
83  *
84  * @param p2q Quantile struct
85  * @param len Length of the returned buffer
86  *
87  * @return char* Serialized representation MUST be freed by the caller
88  */
89 char* sa_serialize_p2_quantile(sa_p2_quantile *p2q, size_t *len);
90 
91 /**
92  * Restores the internal state from the serialized output.
93  *
94  * @param p2q Quantile struct
95  * @param buf Buffer containing the output of serialize_p2_quantile
96  * @param len Length of the buffer
97  *
98  * @return 0 = success
99  * 1 = invalid buffer length
100  * 2 = invalid cnt
101  * 3 = mis-matched percentile
102  *
103  */
104 int
105 sa_deserialize_p2_quantile(sa_p2_quantile *p2q, const char *buf, size_t len);
106 
107 /**
108  * Allocates and initializes the data structures.
109  *
110  * @param buckets Number of histogram buckets
111  *
112  */
113 sa_p2_histogram* sa_create_p2_histogram(unsigned short buckets);
114 
115 /**
116  * Zeros out the histogram counters.
117  *
118  * @param p2h Histogram struct
119  */
121 
122 /**
123  * Updates the histogram with the provided observation.
124  *
125  * @param p2h Histogram struct
126  * @param x Observation to add
127  */
128 void sa_add_p2_histogram(sa_p2_histogram *p2h, double x);
129 
130 /**
131  * Gets the number of observations that are less than or equal to the marker.
132  * *
133  * @param p2h Histogram struct
134  * @param marker Selects the percentile (marker/buckets)
135  */
136 unsigned long long
137 sa_count_p2_histogram(sa_p2_histogram *p2h, unsigned short marker);
138 
139 /**
140  * Gets the estimated quantile value for the specified marker.
141  *
142  * @param p2h Histogram struct
143  * @param marker Selects the percentile (marker/buckets)
144 */
145 double
146 sa_estimate_p2_histogram(sa_p2_histogram *p2h, unsigned short marker);
147 
148 /**
149  * Free the associated memory.
150  *
151  * @param p2h Histogram struct
152  *
153  */
155 
156 /**
157  * Serialize the internal state to a buffer.
158  *
159  * @param p2h Histogram struct
160  * @param len Length of the returned buffer
161  *
162  * @return char* Serialized representation MUST be freed by the caller
163  */
164 char* sa_serialize_p2_histogram(sa_p2_histogram *p2h, size_t *len);
165 
166 /**
167  * Restores the internal state from the serialized output.
168  *
169  * @param p2h Histogram struct
170  * @param buf Buffer containing the output of serialize_p2_histogram
171  * @param len Length of the buffer
172  *
173  * @return 0 = success
174  * 1 = invalid buffer length
175  * 2 = invalid cnt
176  *
177  */
178 int
179 sa_deserialize_p2_histogram(sa_p2_histogram *p2h, const char *buf, size_t len);
180 
181 #ifdef __cplusplus
182 }
183 #endif
184 
185 #endif
struct sa_p2_histogram sa_p2_histogram
Definition: p2.h:18
double sa_add_p2_quantile(sa_p2_quantile *p2q, double x)
Updates the quantile with the provided observation.
double sa_estimate_p2_histogram(sa_p2_histogram *p2h, unsigned short marker)
Gets the estimated quantile value for the specified marker.
unsigned long long sa_count_p2_histogram(sa_p2_histogram *p2h, unsigned short marker)
Gets the number of observations that are less than or equal to the marker.
int sa_deserialize_p2_quantile(sa_p2_quantile *p2q, const char *buf, size_t len)
Restores the internal state from the serialized output.
void sa_init_p2_quantile(sa_p2_quantile *p2q)
Zeros out the quantile.
unsigned long long sa_count_p2_quantile(sa_p2_quantile *p2q, unsigned short marker)
Returns the number of observations that are less than or equal to the marker.
void sa_add_p2_histogram(sa_p2_histogram *p2h, double x)
Updates the histogram with the provided observation.
void sa_init_p2_histogram(sa_p2_histogram *p2h)
Zeros out the histogram counters.
char * sa_serialize_p2_quantile(sa_p2_quantile *p2q, size_t *len)
Serialize the internal state to a buffer.
sa_p2_quantile * sa_create_p2_quantile(double p)
Allocates and initializes the data structure.
char * sa_serialize_p2_histogram(sa_p2_histogram *p2h, size_t *len)
Serialize the internal state to a buffer.
int sa_deserialize_p2_histogram(sa_p2_histogram *p2h, const char *buf, size_t len)
Restores the internal state from the serialized output.
void sa_destroy_p2_quantile(sa_p2_quantile *p2q)
Free the associated memory.
void sa_destroy_p2_histogram(sa_p2_histogram *p2h)
Free the associated memory.
double sa_estimate_p2_quantile(sa_p2_quantile *p2q, unsigned short marker)
Returns the estimated quantile value for the specified marker.
struct sa_p2_quantile sa_p2_quantile
Definition: p2.h:17
sa_p2_histogram * sa_create_p2_histogram(unsigned short buckets)
Allocates and initializes the data structures.