-
Notifications
You must be signed in to change notification settings - Fork 1
/
bbhash_fmt2.go
86 lines (78 loc) · 2.12 KB
/
bbhash_fmt2.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
package bbhash
import (
"fmt"
"strings"
)
func (bb BBHash2) String() string {
var b strings.Builder
lvlSz := make([]uint64, 0)
lvlEntries := make([]uint64, 0)
for _, bx := range bb.partitions {
for lvl, bv := range bx.bits {
if lvl >= len(lvlSz) {
// first partition at lvl; start new counters for size and entries
lvlSz = append(lvlSz, bv.size())
lvlEntries = append(lvlEntries, bv.onesCount())
} else {
lvlSz[lvl] += bv.size()
lvlEntries[lvl] += bv.onesCount()
}
}
}
b.WriteString(fmt.Sprintf("BBHash2(entries=%d, levels=%d, mem bits=%d, wire bits=%d, size=%s, bits per key=%3.1f)\n",
bb.entries(), len(lvlSz), bb.numBits(), bb.wireBits(), bb.space(), bb.BitsPerKey()))
for lvl := 0; lvl < len(lvlSz); lvl++ {
sz := lvlSz[lvl]
entries := lvlEntries[lvl]
b.WriteString(fmt.Sprintf(" %d: %d / %d bits (%s)\n", lvl, entries, sz, readableSize(sz/8)))
}
return b.String()
}
func (bb BBHash2) MaxMinLevels() (max, min int) {
max = 0
min = 999
for _, bx := range bb.partitions {
if max < bx.Levels() {
max = bx.Levels()
}
if min > bx.Levels() {
min = bx.Levels()
}
}
return max, min
}
func (bb BBHash2) BitsPerKey() float64 {
return float64(bb.wireBits()) / float64(bb.entries())
}
func (bb BBHash2) space() string {
return readableSize(bb.wireBits() / 8)
}
func (bb BBHash2) entries() uint64 {
var sz uint64
for _, bx := range bb.partitions {
sz += bx.entries()
}
return sz
}
// numBits returns the number of in-memory bits used to represent the minimal perfect hash.
func (bb BBHash2) numBits() (sz uint64) {
const sizeOfInt = 64
for _, bx := range bb.partitions {
sz += bx.numBits()
sz += sizeOfInt // to account for the offset
}
return sz
}
// wireBits returns the number of on-the-wire bits used to represent the minimal perfect hash on the wire.
func (bb BBHash2) wireBits() (sz uint64) {
if len(bb.partitions) == 1 {
// no need to account for the offset since there is only one partition
return bb.partitions[0].wireBits()
}
const sizeOfInt = 64
for _, bx := range bb.partitions {
sz += bx.wireBits()
sz += sizeOfInt // to account for the offset
}
return sz
}