forked from zhouyangpkuer/Pyramid_Sketch_Framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPCMSketch.h
279 lines (208 loc) · 6.17 KB
/
PCMSketch.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
#ifndef _PCMSKETCH_H
#define _PCMSKETCH_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 PCMSketch
{
private:
int d;
uint64 *counter[60];
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:
PCMSketch(int _word_num, int _d, int word_size);
void Insert(const char * str);
int Query(const char *str);
void Delete(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);
void down_carry(int index);
~PCMSketch();
};
//Just for the consistency of the interface;
//For PCMSketch.h, the word_size must be 64;
PCMSketch::PCMSketch(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);
}
void PCMSketch::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 PCMSketch::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;
}
void PCMSketch::Delete(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;
// min_value = value[i] < min_value ? value[i] : min_value;
if(((flag >> counter_offset[i]) & 1) == 0)
continue;
flag &= (~(1 << counter_offset[i]));
if (value[i] == 0)
{
counter[0][my_word_index] |= ((uint64)0xF << (counter_offset[i] << 2));
down_carry(index[i]);
}
else
{
counter[0][my_word_index] -= ((uint64)0x1 << (counter_offset[i] << 2));
}
}
return;
}
void PCMSketch::down_carry(int index)
{
int left_or_right, up_left_or_right;
int value, up_value;
int word_index = index >> 4, up_word_index;
int offset = index & 0xF;
int up_offset = offset;
for(int i = 1; i < 15; i++)
{
left_or_right = word_index & 1;
word_index >>= 1;
up_word_index = (word_index >> 1);
up_left_or_right = up_word_index & 1;
value = (counter[i][word_index] >> (offset << 2)) & 0xF;
if((value & 3) >= 2)
{
counter[i][word_index] -= ((uint64)0x1 << (offset << 2));
return;
}
else if((value & 3) == 1)
{
up_value = (counter[i + 1][up_word_index] >> (up_offset << 2)) & 0xF;
//change this layer's flag bit;
if(((up_value >> (2 + up_left_or_right)) & 1) == 0)
{
counter[i][word_index] &= (~((uint64)0x1 << (2 + left_or_right + (offset << 2))));
}
counter[i][word_index] -= ((uint64)0x1 << (offset << 2));
return;
}
else
{
counter[i][word_index] |= ((uint64)0x3 << (offset << 2));
}
}
}
void PCMSketch::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 PCMSketch::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 //_PCMSKETCH_H