-
Notifications
You must be signed in to change notification settings - Fork 8
/
CMSketch.h
92 lines (80 loc) · 1.46 KB
/
CMSketch.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
#ifndef _CMSKETCH_H
#define _CMSKETCH_H
#include <algorithm>
#include <cstring>
#include <string.h>
#include "params.h"
#include "BOBHash.h"
#include <iostream>
using namespace std;
class CMSketch
{
private:
BOBHash * bobhash[MAX_HASH_NUM];
int index[MAX_HASH_NUM];
int *counter[MAX_HASH_NUM];
int w, d;
int MAX_CNT;
int counter_index_size;
uint64_t hash_value;
public:
CMSketch(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;
for(int i = 0; i < d; i++)
{
bobhash[i] = new BOBHash(i + 1000);
}
}
void Insert(const char * str)
{
for(int i = 0; i < d; i++)
{
index[i] = (bobhash[i]->run(str, strlen(str))) % w;
if(counter[i][index[i]] != MAX_CNT)
{
counter[i][index[i]]++;
}
}
}
int Query(const char *str)
{
int min_value = MAX_CNT;
int temp;
for(int i = 0; i < d; i++)
{
index[i] = (bobhash[i]->run(str, strlen(str))) % w;
temp = counter[i][index[i]];
min_value = temp < min_value ? temp : min_value;
}
return min_value;
}
void Delete(const char * str)
{
for(int i = 0; i < d; i++)
{
index[i] = (bobhash[i]->run(str, strlen(str))) % w;
counter[i][index[i]] --;
}
}
~CMSketch()
{
for(int i = 0; i < d; i++)
{
delete []counter[i];
}
for(int i = 0; i < d; i++)
{
delete bobhash[i];
}
}
};
#endif//_CMSKETCH_H