forked from zhouyangpkuer/Pyramid_Sketch_Framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPASketch.h
387 lines (299 loc) · 8.19 KB
/
PASketch.h
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#ifndef _PASKETCH_H
#define _PASKETCH_H
#include <algorithm>
#include <cstring>
#include <string.h>
#include "params.h"
#include "BOBHash.h"
#include <iostream>
#include <math.h>
using namespace std;
typedef unsigned long long int uint64;
class PASketch
{
private:
int d;
uint64 *counter[60];
int *new_count;
int *old_count;
char **items;
int word_index_size, counter_index_size;
int word_num, counter_num;
//word_num is the number of words in the first level.
BOBHash * bobhash[MAX_HASH_NUM];
public:
PASketch(int _word_num, int _d, int word_size);
void Insert(const char * str);
void PC_Insert(const char * str);
void PC_Insert_num(const char * str, int num);
int my_get_value(int index);
int InsertAndQuery(const char *str);
int Query(const char *str);
int PC_Query(const char *str);
//carry from the lower layer to the higher layer, maybe we will allocate the new memory;
void carry(int index);
int get_value(int index);
int find_element_in_filter(const char *str);
int find_empty_in_filter();
~PASketch();
};
int PASketch::find_element_in_filter(const char *str)
{
for(int i = 0; i < FILTER_SIZE; i++)
{
if(strcmp(str, items[i]) == 0)
return i;
}
return -1;
}
//can finish in finding element in filter
int PASketch::find_empty_in_filter()
{
for(int i = 0; i < FILTER_SIZE; i++)
{
if(strlen(items[i]) == 0)
return i;
}
return -1;
}
//Just for the consistency of the interface;
//For PASketch.h, the word_size must be 64;
PASketch::PASketch(int _word_num, int _d, int word_size)
{
d = _d;
word_num = _word_num;
//for calculating the four hash value constrained in one certain word;
word_index_size = 18;
counter_index_size = (int)(log(word_size) / log(2)) - 2;//4-8->16-256 counters in one word;
counter_num = (_word_num << counter_index_size);
for(int i = 0; i < 15; i++)
{
counter[i] = new uint64[word_num >> i];
memset(counter[i], 0, sizeof(uint64) * (word_num >> i));
}
for(int i = 0; i < d; i++)
bobhash[i] = new BOBHash(i + 1000);
items = new char *[FILTER_SIZE];
for(int i = 0; i < FILTER_SIZE; i++)
{
items[i] = new char[100];
items[i][0] = '\0';
}
new_count = new int[FILTER_SIZE];
old_count = new int[FILTER_SIZE];
memset(new_count, 0, sizeof(int) * FILTER_SIZE);
memset(old_count, 0, sizeof(int) * FILTER_SIZE);
}
void PASketch::Insert(const char * str)
{
int index = find_element_in_filter(str);
int index_empty = find_empty_in_filter();
int estimate_value, min_index, min_value, hash_value, temp;
if(index != -1)
{
new_count[index] += 1;
return;
}
else if(index_empty != -1)
{
strcpy(items[index_empty], str);
new_count[index_empty] = 1;
old_count[index_empty] = 0;
}
else
{
estimate_value = InsertAndQuery(str);
min_index = 0;
min_value = (1 << 30);
for(int i = 0; i < FILTER_SIZE; i++)
{
if(strlen(items[i]) != 0 && min_value > new_count[i])
{
min_value = new_count[i];
min_index = i;
}
}
if(estimate_value > min_value)
{
temp = new_count[min_index] - old_count[min_index];
if(temp > 0)
{
for(int i = 0; i < temp; i++)
{
PC_Insert(items[min_index]);
}
}
strcpy(items[min_index], str);
new_count[min_index] = estimate_value;
old_count[min_index] = estimate_value;
}
}
}
int PASketch::Query(const char *str)
{
int index = find_element_in_filter(str);
if(index != -1)
{
return new_count[index];
}
int hash_value;
int estimate_value = PC_Query(str);
return estimate_value;
}
void PASketch::PC_Insert(const char *str)
{
int min_value = 1 << 30;
int value[MAX_HASH_NUM];
int index[MAX_HASH_NUM];
int counter_offset[MAX_HASH_NUM];
uint64 hash_value = (bobhash[0]->run(str, strlen(str)));
int my_word_index = (hash_value & ((1 << word_index_size) - 1)) % word_num;
hash_value >>= word_index_size;
int flag = 0xFFFF;
for(int i = 0; i < d; i++)
{
counter_offset[i] = (hash_value & 0xFFF) % (1 << counter_index_size);
index[i] = ((my_word_index << counter_index_size) + counter_offset[i]) % counter_num;
hash_value >>= counter_index_size;
value[i] = (counter[0][my_word_index] >> (counter_offset[i] << 2)) & 0xF;
if(((flag >> counter_offset[i]) & 1) == 0)
continue;
flag &= (~(1 << counter_offset[i]));
if (value[i] == 15)
{
counter[0][my_word_index] &= (~((uint64)0xF << (counter_offset[i] << 2)));
carry(index[i]);
}
else
{
counter[0][my_word_index] += ((uint64)0x1 << (counter_offset[i] << 2));
}
}
return;
}
int PASketch::PC_Query(const char *str)
{
int min_value = 1 << 30;
int value[MAX_HASH_NUM];
int index[MAX_HASH_NUM];
int counter_offset[MAX_HASH_NUM];
uint64 hash_value = (bobhash[0]->run(str, strlen(str)));
int my_word_index = (hash_value & ((1 << word_index_size) - 1)) % word_num;
hash_value >>= word_index_size;
for(int i = 0; i < d; i++)
{
counter_offset[i] = (hash_value & 0xFFF) % (1 << counter_index_size);
index[i] = ((my_word_index << counter_index_size) + counter_offset[i]) % counter_num;
hash_value >>= counter_index_size;
value[i] = (counter[0][my_word_index] >> (counter_offset[i] << 2)) & 0xF;
value[i] += get_value(index[i]);
min_value = value[i] < min_value ? value[i] : min_value;
}
return min_value;
}
int PASketch::InsertAndQuery(const char *str)
{
int min_value = 1 << 30;
int value[MAX_HASH_NUM];
int index[MAX_HASH_NUM];
int counter_offset[MAX_HASH_NUM];
uint64 hash_value = (bobhash[0]->run(str, strlen(str)));
int my_word_index = (hash_value & ((1 << word_index_size) - 1)) % word_num;
hash_value >>= word_index_size;
int flag = 0xFFFF;
for(int i = 0; i < d; i++)
{
counter_offset[i] = (hash_value & 0xFFF) % (1 << counter_index_size);
index[i] = ((my_word_index << counter_index_size) + counter_offset[i]) % counter_num;
hash_value >>= counter_index_size;
if(((flag >> counter_offset[i]) & 1) == 0)
continue;
flag &= (~(1 << counter_offset[i]));
value[i] = (counter[0][my_word_index] >> (counter_offset[i] << 2)) & 0xF;
if (value[i] == 15)
{
counter[0][my_word_index] &= (~((uint64)0xF << (counter_offset[i] << 2)));
value[i] == my_get_value(index[i]);
}
else
{
counter[0][my_word_index] += ((uint64)0x1 << (counter_offset[i] << 2));
value[i] += get_value(index[i]) + 1;
}
min_value = value[i] < min_value ? value[i] : min_value;
}
return min_value;
}
int PASketch::my_get_value(int index)
{
int left_or_right, anti_left_or_right;
int high_value = 0;
int value;
int word_index = index >> 4;
int offset = index & 0xF;
bool flag = true;
for(int i = 1; i < 15; i++)
{
left_or_right = word_index & 1;
anti_left_or_right = (left_or_right ^ 1);
word_index >>= 1;
if(flag)
{
counter[i][word_index] |= ((uint64)0x1 << (2 + left_or_right + (offset << 2)));
value = (counter[i][word_index] >> (offset << 2)) & 0xF;
if((value & 3) != 3)
{
counter[i][word_index] += ((uint64)0x1 << (offset << 2));
flag = false;
}
else
{
counter[i][word_index] &= (~((uint64)0x3 << (offset << 2)));
}
}
value = (counter[i][word_index] >> (offset << 2)) & 0xF;
if(((value >> (2 + left_or_right)) & 1) == 0)
return high_value;
high_value += ((value & 3) - ((value >> (2 + anti_left_or_right)) & 1)) * (1 << (2 + 2 * i));
}
}
void PASketch::carry(int index)
{
int left_or_right;
int value;
int word_index = index >> 4;
int offset = index & 0xF;
for(int i = 1; i < 15; i++)
{
left_or_right = word_index & 1;
word_index >>= 1;
counter[i][word_index] |= ((uint64)0x1 << (2 + left_or_right + (offset << 2)));
value = (counter[i][word_index] >> (offset << 2)) & 0xF;
if((value & 3) != 3)
{
counter[i][word_index] += ((uint64)0x1 << (offset << 2));
return;
}
counter[i][word_index] &= (~((uint64)0x3 << (offset << 2)));
}
}
int PASketch::get_value(int index)
{
int left_or_right;
int anti_left_or_right;
int value;
int word_index = index >> 4;
int offset = index & 0xF;
int high_value = 0;
for(int i = 1; i < 15; i++)
{
left_or_right = word_index & 1;
anti_left_or_right = (left_or_right ^ 1);
word_index >>= 1;
value = (counter[i][word_index] >> (offset << 2)) & 0xF;
if(((value >> (2 + left_or_right)) & 1) == 0)
return high_value;
high_value += ((value & 3) - ((value >> (2 + anti_left_or_right)) & 1)) * (1 << (2 + 2 * i));
}
}
#endif //_PASKETCH_H