-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
bep33.go
49 lines (42 loc) · 857 Bytes
/
bep33.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
package krpc
import (
"crypto/sha1"
"math"
"math/bits"
"net"
)
const (
m = 256 * 8
k = 2
)
type ScrapeBloomFilter [256]byte
// Note that if you intend for an IP to be in the IPv4 space, you might want to trim it to 4 bytes
// with IP.To4.
func (me *ScrapeBloomFilter) AddIp(ip net.IP) {
h := sha1.New()
h.Write(ip)
var sum [20]byte
h.Sum(sum[:0])
me.addK(int(sum[0]) | int(sum[1])<<8)
me.addK(int(sum[2]) | int(sum[3])<<8)
}
func (me *ScrapeBloomFilter) addK(index int) {
index %= m
me[index/8] |= 1 << (index % 8)
}
func (me ScrapeBloomFilter) countZeroes() (ret int) {
for _, i := range me {
ret += 8 - bits.OnesCount8(i)
}
return
}
func (me *ScrapeBloomFilter) EstimateCount() float64 {
if me == nil {
return 0
}
c := float64(me.countZeroes())
if c > m-1 {
c = m - 1
}
return math.Log(c/m) / (k * math.Log(1.-1./m))
}