/
pmc.go
218 lines (189 loc) · 4.58 KB
/
pmc.go
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
/*
Package pmc description
*/
package pmc
import (
"fmt"
"math/rand"
"math"
"errors"
"github.com/willf/bitset"
"github.com/dgryski/go-farm"
)
var ( // change to const
n_max float64 = 1e5
phi_const float64 = 0.77351
)
type Sketch struct {
m uint64
w uint64
l uint64
bitmap *bitset.BitSet
n uint64
cachedP float64
changed bool
}
func New(l uint64, m uint64, w uint64) (*Sketch, error) {
if l == 0 || m == 0 || w == 0 {
return nil, errors.New("All parameters must be > 0")
} else if l > (1 << w) - 1 || l > (1 << 64) - 1 {
return nil, errors.New("l must be < 2**w and < 2**64")
}
return &Sketch{l: l, m: m, w: w, bitmap: bitset.New(uint(l)), changed: true}, nil
}
// Algorithm 1 B[H(f,i,j)] = 1
func (sketch *Sketch) PmcCount(f []byte) error {
index, err := sketch.getIndexF(f)
if err != nil {
// fmt.Printf("%v\n", err.Error())
return err
}
if sketch.bitmap.Test(uint(index) % uint(sketch.l)) {
// fmt.Printf("Bit at index %v is already set. Sketch changed %v\n", index, sketch.changed)
return nil
}
sketch.n++
sketch.changed = true
sketch.bitmap.Set(uint(index) % uint(sketch.l))
return nil
}
// Algorithm 2
func (sketch *Sketch) getZSum(f []byte) uint64 {
z := uint64(0)
for i := uint64(0); i < sketch.m; i++ {
for j := uint64(0); j < sketch.w; j++ {
if !sketch.bitmap.Test(uint(sketch.getIndexFIJ(f, i, j))) {
z += j
break
}
}
}
return z
}
// Algorithm 3
func (sketch *Sketch) getEmptyRows(f []byte) uint64 {
k := uint64(0)
for i := uint64(0); i < sketch.m; i ++ {
if !sketch.bitmap.Test(uint(sketch.getIndexFIJ(f, i, 0))) {
k++
}
}
return k
}
// Algorithm 4
func (sketch *Sketch) PmcEstimate(f []byte) (uint64, error) {
k := float64(sketch.getEmptyRows(f))
p := sketch.p()
m := float64(sketch.m)
phi := sketch.phi(p)
estimate := 0.0
if kp := k/(1 - p); kp > 0.3*m {
// fmt.Println("Small multiplicity")
estimate = -2*m*math.Log(kp/m)
} else {
// fmt.Println("large multiplicity")
if sketch.n <= 0 {
return 0, errors.New("sketch.n should be positive")
}
z := float64(sketch.getZSum(f))
estimate = m*math.Pow(2, z/m)/phi
// fmt.Printf("z = %v \tphi = %v\n", z, phi)
}
estimate = math.Ceil(math.Abs(estimate))
return uint64(estimate), nil
}
/**** Public Helpers ****/
func (sketch *Sketch) PrintBitmap() {
fmt.Printf("Non-zero bits for sketch %v\n", sketch)
count := 0
for i := uint(0); i < uint(sketch.l) ; i++ {
if sketch.bitmap.Test(i) {
fmt.Printf(" %v\n", i)
count++
}
}
fmt.Printf("Total: %v\n", count)
}
func (sketch *Sketch) PrintVirtualMatrix(flow []byte) {
for i := uint64(0); i < sketch.m; i++ {
for j := uint64(0); j < sketch.w; j++ {
pos := sketch.getIndexFIJ(flow, i, j)
if sketch.bitmap.Test(uint(pos)) {
fmt.Print(1)
} else {
fmt.Print(0)
}
}
fmt.Println("")
}
}
func (sketch *Sketch) GetFillRate() float64 {
return sketch.p() * 100
}
/**** Private Helpers ****/
func (sketch *Sketch) p() float64 {
if !sketch.changed {
return sketch.cachedP
}
count := 0.0
for i := uint(0); i < uint(sketch.l) ; i++ {
if sketch.bitmap.Test(i) {
count++
}
}
sketch.cachedP = count/float64(sketch.l)
sketch.changed = false
return sketch.cachedP
}
func (sketch *Sketch) phi(p float64) float64 {
n := float64(sketch.n)
if n >= n_max {
return phi_const
}
// fmt.Printf("n = %v, sketch.expZ(n, p) = %v\n", n, sketch.expZ(n, p))
return math.Pow(2, sketch.expZ(n, p))/n
}
func (sketch *Sketch) expZ(n float64, p float64) float64 {
exp := 0.0
w := float64(sketch.w)
for k := float64(0); k < w; k++ {
exp += k * (qk(k, n, p) - qk(k+1, n, p))
}
return exp
}
func qk(k float64, n float64, p float64) float64 {
product := 1.0
for i := float64(0); i < k; i++ {
product *= (1 - math.Pow((1 - math.Pow(2, -i)), n) * (1 - p))
}
return product
}
// Get index into sketch bitmap
func (sketch *Sketch) getIndexF(f []byte) (uint64, error) {
i := uniform(sketch.m)
j, err := geometric(sketch.w)
if err != nil {
// fmt.Printf("%v\n", err.Error())
return 0, err
}
return sketch.getIndexFIJ(f, i, j), err
}
func (sketch *Sketch) getIndexFIJ(f []byte, i uint64, j uint64) uint64 {
return farm.Hash64WithSeeds(f, i, j) % sketch.l
}
func uniform (m uint64) uint64 {
return uint64(rand.Int63n(int64(m))) % m
}
// random number generator based off a geometric distribution
func geometric(w uint64) (j uint64, e error) {
if w > 64 {
return 0, errors.New("input parameter w to Geometric function must be <= 64") // uint64 is 64 bit
}
uniform := rand.Uint32()
for i := uint32(0); i < uint32(w); i++ {
if uniform & (1 << uint32(i)) != 0 {
return uint64(i), nil
}
}
return w, nil
}