-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmsketch.cpp
284 lines (215 loc) · 5.77 KB
/
cmsketch.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// In this program we will determine the strength of the input password given by the user
// based on the frequency of the appearance of each password in the "passwords.txt"
// If the input given doesn't appear in the file then we give super strong!
// Scale goes from seriously give something better - super strong
// Anestis Kyrkenidis AM:3016 first attempt
// Include standard headers
# include "cmsketch.h"
// Basic Constructor
CMSketch :: CMSketch ( float e, float d, int md )
{
count = 0;
// epsilon is 0.01 <= epsilon < 1
// delta is 0 < delta < 1
epsilon = e;
delta = d;
// new width and depth values
// as shown in a newer paper of the original Count-Min Sketch authors
width = ceil( 2/epsilon );
depth = ceil( -log2( 1-delta ) );
mode = md;
// epsilon = (float) 2 / width;
// confidence = 1 - 1 / pow( 2, depth );
table = new int* [depth];
hashAB = new int* [depth];
for (int i = 0; i < depth; i++ )
{
table[i] = new int [width];
hashAB[i] = new int[2];
}
initTables( table, width, depth );
initHashAB( hashAB, depth );
}
// Copy Constructor
CMSketch :: CMSketch ( const CMSketch& cms )
{
count = 0;
// epsilon is 0.01 <= epsilon < 1
// delta is 0 < delta < 1
epsilon = cms.epsilon;
delta = cms.delta;
// new width and depth values
// as shown in a newer paper of the original Count-Min Sketch authors
width = ceil( 2/epsilon );
depth = ceil( -log2( 1-delta ) );
mode = cms.mode;
// epsilon = (float) 2 / width;
// confidence = 1 - 1 / pow( 2, depth );
table = new int* [depth];
hashAB = new int* [depth];
for (int i = 0; i < depth; i++ )
{
table[i] = new int [width];
hashAB[i] = new int[2];
}
initTables( table, width, depth );
initHashAB( hashAB, depth );
}
void CMSketch :: initTables ( int** table, int width, int depth )
{
int i,j;
for ( i = 0; i < depth; i++)
{
for ( j = 0; j < width; j++ )
{
table[i][j] = 0;
}
}
}
void CMSketch :: initHashAB ( int** hashAB, int depth )
{
int i;
srand( unsigned (time( NULL )) );
for ( i = 0; i < depth; i++ )
{
hashAB[i][0] = int(float( rand() ) * float( PRIME ) / float( RAND_MAX ) + 1);
hashAB[i][1] = int(float( rand() ) * float( PRIME ) / float( RAND_MAX ) + 1);
}
}
void CMSketch :: addItem ( int item, int times )
{
std::size_t hash_item = item;
int i;
count += times;
for ( i = 0; i < depth; i++ )
{
table[i][hash( hash_item, i )] += times;
}
}
/*
void CMSketch :: addItem ( string item, int times )
{
std::size_t hash_item = std::hash<std::string>{}(item);
int i;
count += times;
for ( i = 0; i < depth; i++ )
{
table[i][hash( hash_item, i )] += times;
}
}
*/
unsigned int CMSketch :: hash ( std::size_t item, int i )
{
std::size_t hash_item;
// hash function : ( a * x + b ) mod p
if ( mode == 0 )
hash_item = ( ( long(hashAB[i][0])*item )
+ hashAB[i][1] ) % width;
// hash function : ( ( a * x + b ) mod p ) mod w
else if( mode == 1 )
hash_item = ( ( ( long(hashAB[i][0]) * item )
+ hashAB[i][1] ) % PRIME ) % width;
else if( mode == 2 )
{
hash_item = (long(hashAB[i][0])*item);
hash_item += hash_item >> 32;
hash_item &= PRIME;
hash_item = ((int)hash_item) % width;
}
return (unsigned int) hash_item;
}
unsigned int CMSketch :: estimatePoint ( std::size_t item )
{
std::size_t hash_item = item;
int i;
int estimate = RAND_MAX;
for ( i = 0; i < depth; i++ )
{
estimate = MIN( estimate, table[i][hash( hash_item, i )] );
}
// should be estimate < actual_appearance + epsilon*N,
// where N is the sum of appearances of the inputs
return estimate;
}
/*
unsigned int CMSketch :: estimatePoint ( string item )
{
std::size_t hash_item = std::hash<std::string>{}(item);
int i;
int estimate = RAND_MAX;
for ( i = 0; i < depth; i++ )
{
estimate = MIN( estimate, table[i][hash( hash_item, i )] );
}
// should be estimate < actual_appearance + epsilon*N,
// where N is the sum of appearances of the inputs
return estimate;
}
*/
unsigned int CMSketch :: estimateInnerProduct ( int w, int d, int** new_table )
{
int i, j;
int temp, inner;
inner = RAND_MAX;
// check if the two CMSketches are the same ( in terms of width and depth )
if ( width != w || depth != d )
{
cout << "The two Count-Min Sketches are not compatible. " << endl;
return 0;
}
for ( j = 0; j < depth; j++ )
{
temp = 0;
// std::inner_product exists in <numeric>
for ( i = 0; i < width; i++ )
temp += ( table[j][i] * new_table[j][i]);
inner = MIN( inner, temp );
}
return inner;
}
unsigned int CMSketch :: return_count (int max_hash)
{
unsigned int cmscount = 0;
for ( int i = 0; i < max_hash; i++ )
cmscount += estimatePoint(i);
return cmscount;
}
unsigned long int CMSketch :: return_actual_count ()
{
return count;
}
int** CMSketch :: return_table ( )
{
return table;
}
std::size_t CMSketch :: return_byte_size ()
{
return width * depth * 8;
}
float CMSketch :: return_epsilon ()
{
return epsilon;
}
float CMSketch :: return_delta ()
{
return delta;
}
int CMSketch :: return_width ()
{
return width;
}
int CMSketch :: return_depth ()
{
return depth;
}
CMSketch :: ~CMSketch ()
{
int i;
for ( i = 0; i < depth; i++ )
{
delete[] table[i];
delete[] hashAB[i];
}
delete[] table;
delete[] hashAB;
}