-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathPCUSketch.h
195 lines (143 loc) · 4.34 KB
/
PCUSketch.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
#ifndef _PCUSKETCH_H
#define _PCUSKETCH_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 PCUSketch
{
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:
PCUSketch(int _word_num, int _d, int word_size);
void Insert(const char * str);
int 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);
~PCUSketch();
};
//Just for the consistency of the interface;
//For PCUSketch.h, the word_size must be 64;
PCUSketch::PCUSketch(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 PCUSketch::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;
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(min_value != 15)
{
for(int i = 0; i < d; i++)
{
value[i] = (counter[0][my_word_index] >> (counter_offset[i] << 2)) & 0xF;
counter[0][my_word_index] += value[i] == min_value ? ((uint64)0x1 << (counter_offset[i] << 2)) : 0;
}
return;
}
for(int i = 0; i < d; i++)
{
value[i] = (counter[0][my_word_index] >> (counter_offset[i] << 2)) & 0xF;
if(value[i] == 0)
continue;
counter[0][my_word_index] &= (~((uint64)0xF << (counter_offset[i] << 2)));
carry(index[i]);
}
return;
}
int PCUSketch::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 PCUSketch::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 PCUSketch::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 //_PCUSKETCH_H