forked from zhouyangpkuer/Pyramid_Sketch_Framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSketch.h
135 lines (121 loc) · 2.12 KB
/
CSketch.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
#ifndef _CSKETCH_H
#define _CSKETCH_H
#include <algorithm>
#include <cstring>
#include <string.h>
#include "params.h"
#include "BOBHash.h"
#include <iostream>
#include <algorithm>
using namespace std;
class CSketch
{
private:
BOBHash * bobhash[MAX_HASH_NUM * 2];
int index[MAX_HASH_NUM];
int *counter[MAX_HASH_NUM];
int w, d;
int MAX_CNT, MIN_CNT;
int counter_index_size;
uint64_t hash_value;
public:
CSketch(int _w, int _d)
{
counter_index_size = 20;
w = _w;
d = _d;
for(int i = 0; i < d; i++)
{
counter[i] = new int[w];
memset(counter[i], 0, sizeof(int) * w);
}
MAX_CNT = (1 << (COUNTER_SIZE - 1)) - 1;
MIN_CNT = (-(1 << (COUNTER_SIZE - 1)));
for(int i = 0; i < d * 2; i++)
{
bobhash[i] = new BOBHash(i + 1000);
}
}
void Insert(const char * str)
{
int g = 0;
for(int i = 0; i < d; i++)
{
index[i] = (bobhash[i]->run(str, strlen(str))) % w;
g = (bobhash[i + d]->run(str, strlen(str))) % 2;
if(g == 0)
{
if(counter[i][index[i]] != MAX_CNT)
{
counter[i][index[i]]++;
}
}
else
{
if(counter[i][index[i]] != MIN_CNT)
{
counter[i][index[i]]--;
}
}
}
}
void Delete(const char * str)
{
int g = 0;
for(int i = 0; i < d; i++)
{
index[i] = (bobhash[i]->run(str, strlen(str))) % w;
g = (bobhash[i + d]->run(str, strlen(str))) % 2;
if(g == 1)
{
if(counter[i][index[i]] != MAX_CNT)
{
counter[i][index[i]]++;
}
}
else
{
if(counter[i][index[i]] != MIN_CNT)
{
counter[i][index[i]]--;
}
}
}
}
int Query(const char *str)
{
int temp;
int res[MAX_HASH_NUM];
int g;
for(int i = 0; i < d; i++)
{
index[i] = (bobhash[i]->run(str, strlen(str))) % w;
temp = counter[i][index[i]];
g = (bobhash[i + d]->run(str, strlen(str))) % 2;
res[i] = (g == 0 ? temp : -temp);
}
sort(res, res + d);
int r;
if(d % 2 == 0)
{
r = (res[d / 2] + res[d / 2 - 1]) / 2;
}
else
{
r = res[d / 2];
}
return r;
}
~CSketch()
{
for(int i = 0; i < d; i++)
{
delete []counter[i];
}
for(int i = 0; i < d * 2; i++)
{
delete bobhash[i];
}
}
};
#endif//_CSKETCH_H