-
Notifications
You must be signed in to change notification settings - Fork 3
/
node_analysis.go
80 lines (67 loc) · 1.52 KB
/
node_analysis.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
package frozen
type nodeAnalysis struct {
nodes int
leaves int
elements int
depths []int
// [depth][count(children)]count
nodeFillses map[int]map[int]int
leafFillses map[int]map[int]int
hashers map[hasher]int
}
func incrementFills(fillses map[int]map[int]int, depth, count int) {
fills, ok := fillses[depth]
if !ok {
fills = map[int]int{}
fillses[depth] = fills
}
fills[count] = fills[count] + 1
}
func (a *nodeAnalysis) node(n *node, depth int) {
a.nodes++
incrementFills(a.nodeFillses, depth, n.mask.Count())
}
func (a *nodeAnalysis) leaf(l *leaf, depth int) {
a.leaves++
count := l.count()
a.elements += count
for len(a.depths) <= depth {
a.depths = append(a.depths, 0)
}
a.depths[depth]++
incrementFills(a.leafFillses, depth, count)
}
func (a *nodeAnalysis) element(elem interface{}) {
a.elements++
if a.hashers != nil {
h := newHasher(elem, 0)
a.hashers[h] = a.hashers[h] + 1
}
}
func (n *node) profile(includeHasher bool) *nodeAnalysis {
result := &nodeAnalysis{
nodeFillses: map[int]map[int]int{},
leafFillses: map[int]map[int]int{},
}
if includeHasher {
result.hashers = map[hasher]int{}
}
n.profileImpl(result, 0)
return result
}
func (n *node) profileImpl(a *nodeAnalysis, depth int) {
switch {
case n == nil:
return
case n.isLeaf():
a.leaf(n.leaf(), depth)
for i := n.leaf().iterator(); i.Next(); {
a.element(i.Value())
}
default:
a.node(n, depth)
for mask := n.mask; mask != 0; mask = mask.Next() {
n.children[mask.Index()].profileImpl(a, depth+1)
}
}
}