-
Notifications
You must be signed in to change notification settings - Fork 1
/
extras.go
67 lines (59 loc) · 1.27 KB
/
extras.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
package trie
import (
"fmt"
"strings"
)
func (es edges) GoString() string {
if es == nil {
return "edges(nil)"
}
if len(es) == 0 {
return "edges{}"
}
strs := make([]string, 0, len(es))
for _, n := range es {
ns := strings.Replace(n.GoString(), "\n", "\n\t", -1)
strs = append(strs, ns)
}
return "edges{\n\t" + strings.Join(strs, ",\n\t") + ",\n}"
}
func (es edges) valid() bool {
for _, e := range es {
if e == nil {
return false
}
}
return true
}
func (n *Node) GoString() string {
if n == nil {
return "(*Node)(nil)"
}
es := strings.Replace(n.edges.GoString(), "\n", "\n\t", -1)
if n.value == nil {
return fmt.Sprintf("&Node{\n\tkey: Key(%q),\n\tedges: %s,\n}", n.key, es)
}
return fmt.Sprintf("&Node{\n\tkey: Key(%q),\n\tvalue: %#v,\n\tedges: %s,\n}", n.key, n.value, es)
}
func (n *Node) Histogram() map[uint8]int {
h := make(map[uint8]int, 256)
n.histogram(h)
return h
}
func (n *Node) histogram(h map[uint8]int) {
h[uint8(len(n.edges))]++
for _, nd := range n.edges {
nd.histogram(h)
}
}
func (t *Txn) PrintHistogram() {
// fmt.Printf("mutable nodes: %d, new nodes: %d\n", len(t.mut), t.newNodes)
h := t.root.Histogram()
for i := 0; i < 256; i++ {
n := h[uint8(i)]
if n == 0 {
continue
}
fmt.Printf("%3d: %d\n", i, n)
}
}