-
Notifications
You must be signed in to change notification settings - Fork 0
/
psketch.cpp
142 lines (123 loc) · 3.21 KB
/
psketch.cpp
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
#include "psketch.h"
#include <iostream>
#include "util.h"
using namespace std;
CMSketch::CMSketch(double eps, double delta) :
w(ceil(exp(1)/eps)),
d(ceil(log(1/delta))) {
C.resize(d);
for (auto i = 0U; i < d; i++) {
C[i].resize(w, 0);
}
//srand(time(NULL));
srand(0);
hashes.resize(d);
for (auto i = 0U; i < d; i++) {
hashes[i].first = rand_int();
hashes[i].second = rand_int();
}
}
void CMSketch::clear() {
for (auto i = 0U; i < d; i++) {
fill(C[i].begin(), C[i].end(), 0);
}
}
void CMSketch::update(ull item, int c) {
for (auto j = 0U; j < d; j++) {
auto hashval = (hashes[j].first * item + hashes[j].second) % w;
C[j][hashval] += c;
}
}
void CMSketch::update(const char *str, int c) {
update(hashstr(str), c);
}
int CMSketch::estimate(ull item) {
auto val = numeric_limits<int>::max();
for (auto j = 0U; j < d; j++) {
auto hashval = (hashes[j].first * item + hashes[j].second) % w;
val = min(val, C[j][hashval]);
}
return val;
}
int CMSketch::estimate(const char *str) {
return estimate(hashstr(str));
}
unsigned long long CMSketch::memory_usage() {
return d * w * sizeof(int);
}
PCMSketch::PCMSketch(double eps, double delta, genType const& gen) : CMSketch(eps, delta){
pla.resize(d);
for (auto i = 0U; i < d; i++) {
pla[i].resize(w, nullptr);
for (auto j = 0U; j < w; j++) {
pla[i][j] = gen(i,j);
}
}
}
PCMSketch::~PCMSketch() {
for (auto i = 0U; i < d; i++) {
for (auto j = 0U; j < w; j++) {
delete pla[i][j];
}
}
}
void PCMSketch::clear() {
CMSketch::clear();
for (auto i = 0U; i < d; i++) {
for (auto j= 0U; j < w; j++) {
pla[i][j]->clear();
}
}
}
void PCMSketch::update(ull t, ull item, int c) {
assert(c > 0);
for (auto j = 0U; j < d; j++) {
auto hashval = (hashes[j].first * item + hashes[j].second) % w;
C[j][hashval] += c;
pla[j][hashval]->feed({t, (double)C[j][hashval]});
}
}
double PCMSketch::estimate(ull item, ull t) {
double vals[d];
for (auto j = 0U; j < d; j++) {
auto hashval = (hashes[j].first * item + hashes[j].second) % w;
vals[j] = pla[j][hashval]->estimate(t);
}
sort(vals, vals + d);
if (d & 1) { // d is odd
return vals[d/2];
} else {
return (vals[d/2] + vals[(d/2)-1]) / 2;
}
}
void PCMSketch::update(ull t, const char *str, int c) {
update(t, hashstr(str), c);
}
double PCMSketch::estimate(const char *str, ull t) {
return estimate(hashstr(str), t);
}
double PCMSketch::burstiness(ull item, ull t, ull tau) {
auto c = estimate(item, t);
if (t < tau) {
return c;
}
else {
auto b = estimate(item, t - tau);
if (t < 2 * tau) {
return c - 2 * b;
}
else {
auto a = estimate(item, t - 2 * tau);
return c - 2 * b + a;
}
}
}
ull PCMSketch::memory_usage() {
ull sum = 0;
for (auto i = 0U; i < d; i++) {
for (auto j = 0U; j < w; j++) {
sum += pla[i][j]->memory_usage();
}
}
return CMSketch::memory_usage() + sum;
}