Added quantiling UDAFs
[com/gs-lite.git] / src / lib / gscphftaaux / Manku_HH.cc
1 /* ------------------------------------------------\r
2  Copyright 2014 AT&T Intellectual Property\r
3  Licensed under the Apache License, Version 2.0 (the "License");\r
4  you may not use this file except in compliance with the License.\r
5  You may obtain a copy of the License at\r
6  \r
7  http://www.apache.org/licenses/LICENSE-2.0\r
8  \r
9  Unless required by applicable law or agreed to in writing, software\r
10  distributed under the License is distributed on an "AS IS" BASIS,\r
11  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
12  See the License for the specific language governing permissions and\r
13  limitations under the License.\r
14  ------------------------------------------- */\r
15 //#########################################\r
16 // Stateful functions for Manku and Motwani\r
17 // heavy hitters algorithm\r
18 //########################################\r
19 \r
20 #include <iostream>\r
21 #define max(x,y) ((x)>(y)?(x):(y))\r
22 \r
23 struct MankuHHstate {\r
24     unsigned int count;\r
25     unsigned int count_bucket;\r
26     unsigned int need_to_clean;\r
27 };\r
28 \r
29 void _sfun_state_clean_init_manku_hh_state(void *s){\r
30     struct MankuHHstate *state = (struct MankuHHstate *)s;\r
31     \r
32     state->count = 0;\r
33     state->count_bucket = 1;\r
34     state->need_to_clean = 0;\r
35     \r
36 }\r
37 \r
38 void _sfun_state_dirty_init_manku_hh_state(void *s_new, void *s_old, int curr_num_samples){\r
39     struct MankuHHstate *state_new  = (struct MankuHHstate *)s_new;\r
40     struct MankuHHstate *state_old  = (struct MankuHHstate *)s_old;\r
41     \r
42     state_new->count = 0;\r
43     state_new->count_bucket = 1;\r
44 }\r
45 \r
46 void _sfun_state_final_init_manku_hh_state(void *s, int curr_num_samples){\r
47     struct MankuHHstate *state  = (struct MankuHHstate *)s;\r
48 }\r
49 \r
50 int local_count(void *s,int curr_num_samples, unsigned int bucket_size){\r
51     struct MankuHHstate *state  = (struct MankuHHstate *)s;\r
52     \r
53     state->count++;\r
54     if(state->count >= bucket_size){\r
55         //cout << "bucket_size: " << bucket_size << "\n";\r
56         state->count_bucket++;\r
57         state->need_to_clean = 1;\r
58         //cout << "bucket_count: " << state->count_bucket << "\n";\r
59         //cout << "packet count: " << state->count << "\n";\r
60         state->count = 0;\r
61         return 1;\r
62     }\r
63     return 0;\r
64 }\r
65 \r
66 unsigned int current_bucket(void *s, int curr_num_samples){\r
67     struct MankuHHstate *state  = (struct MankuHHstate *)s;\r
68     if(state->need_to_clean == 1){\r
69         state->need_to_clean = 0;\r
70         return (state->count_bucket)-1;\r
71     }\r
72     return state->count_bucket;\r
73 }\r