-
Notifications
You must be signed in to change notification settings - Fork 12
/
count_min_sketch.cpp
204 lines (198 loc) · 5.45 KB
/
count_min_sketch.cpp
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
/*
* C++ Program to Implement Count Min Sketch
*/
class CountMinSketch
{
private:
ui w,d;
float eps;
float gamma;
ui aj, bj;
ui total;
int **C;
int **hashes;
void genajbj(int **hashes, int i);
public:
/*
* Constructor
*/
CountMinSketch(float ep, float gamm)
{
if (!(0.009 <= ep && ep < 1))
{
cout << "eps must be in this range: [0.01, 1)" << endl;
exit(1);
}
else if (!(0 < gamm && gamm < 1))
{
cout << "gamma must be in this range: (0,1)" << endl;
exit(1);
}
eps = ep;
gamma = gamm;
w = ceil(exp(1) / eps);
d = ceil(log(1 / gamma));
total = 0;
C = new int *[d];
ui i, j;
for (i = 0; i < d; i++)
{
C[i] = new int[w];
for (j = 0; j < w; j++)
{
C[i][j] = 0;
}
}
srand(time(NULL));
hashes = new int* [d];
for (i = 0; i < d; i++)
{
hashes[i] = new int[2];
genajbj(hashes, i);
}
}
/*
* Destructor
*/
~CountMinSketch()
{
ui i;
for (i = 0; i < d; i++)
{
delete[] C[i];
}
delete[] C;
for (i = 0; i < d; i++)
{
delete[] hashes[i];
}
delete[] hashes;
}
/*
* Update Int Value
*/
void update(int item, int c)
{
total = total + c;
ui hashval = NULL;
for (ui j = 0; j < d; j++)
{
hashval = (hashes[j][0] * item + hashes[j][1]) % w;
C[j][hashval] = C[j][hashval] + c;
}
}
/*
* Update Str Data
*/
void update(const char *str, int c)
{
int hashval = hashstr(str);
update(hashval, c);
}
/*
* Estimate Min Value
*/
ui estimate(int item)
{
int minval = numeric_limits<int>::max();
unsigned int hashval = NULL;
for (unsigned int j = 0; j < d; j++)
{
hashval = (hashes[j][0] * item + hashes[j][1]) % w;
minval = MIN(minval, C[j][hashval]);
}
return minval;
}
/*
* Estimate Min Str Value
*/
ui estimate(const char *str)
{
int hashval = hashstr(str);
return estimate(hashval);
}
/*
* Total Count
*/
ui totalcount()
{
return total;
}
/*
* Hashing String to Int
*/
ui hashstr(const char *str)
{
unsigned long hash = 5381;
int c;
while (c = *str++)
{
hash = ((hash << 5) + hash) + c;
}
return hash;
}
};
/*
* Generate Random Hashes
*/
void CountMinSketch::genajbj(int** hashes, int i)
{
hashes[i][0] = int(float(rand())*float(LONG_PRIME)/float(RAND_MAX) + 1);
hashes[i][1] = int(float(rand())*float(LONG_PRIME)/float(RAND_MAX) + 1);
}
/*
* Main Contains menu
*/
int main()
{
const char *ar_str[] = { "hello", "some", "one", "hello", "alice",
"one", "lady", "let", "us", "lady",
"alice", "in", "us", "wonderland", "lady",
"lady", "some", "hello", "none", "pie" };
CountMinSketch c(0.01, 0.1);
ui i, total = 0, count;
map<const char *, int> mapitems;
map<const char *, int>::const_iterator it;
for (i = 0; i < 20; i++)
{
if ((it = mapitems.find(ar_str[i])) != mapitems.end())
{
mapitems[ar_str[i]] += i;
}
else
{
mapitems[ar_str[i]] = i;
}
c.update(ar_str[i], i);
total = total + i;
}
// 1. test for items in ar_str
for (it = mapitems.begin(); it != mapitems.end(); it++)
{
if (c.estimate(it->first) != mapitems[it->first])
{
cout << "Incorrect count for " << it->first << ":->error: ";
count = abs(int(c.estimate(it->first)-mapitems[it->first]));
cout << count << endl;
}
else
{
cout << "Correct count for '" << it->first;
cout << "' :-->count: " << c.estimate(it->first) << endl;
}
}
cout << "c.totalcount()==total ? ";
cout << (c.totalcount() == total ? "True" : "False") << endl;
cout << "Sketch Total: " << c.totalcount() << endl;
// 2. test for items not in ar_str
cout << "Testing for strings not in ar_str..." << endl;
const char *arn_str[] = {"sanfoundry", "hello", "linux", "us"};
for (i = 0 ; i < 4; i++)
{
if (!c.estimate(arn_str[i]))
cout<<arn_str[i] << " not found in string array"<<endl;
else
cout<<arn_str[i] << " found in string array"<<endl;
}
return 0;
}