-
Notifications
You must be signed in to change notification settings - Fork 1
/
stats.go
86 lines (72 loc) · 1.92 KB
/
stats.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 btree
import (
"math"
)
// GetHeight returns an item in the tree with key @key, and it's height in the tree
func (t *Tree[T]) GetHeight(key CompareAgainst[T]) (result T, depth int) {
return t.getHeight(t.root, key)
}
func (t *Tree[T]) RebalanceStats() (inserts, deletes uint64, balancePerInsert, balancePerDelete float64) {
return t.insertCount, t.removeCount,
float64(t.insertRebalanceCount) / float64(t.insertCount),
float64(t.removeRebalanceCount) / float64(t.removeCount)
}
func (t *Tree[T]) getHeight(h *node[T], item CompareAgainst[T]) (T, int) {
if h == nil {
var ref T
return ref, 0
}
switch item(h.i) {
case -1:
result, depth := t.getHeight(h.l, item)
return result, depth + 1
case 1:
result, depth := t.getHeight(h.l, item)
return result, depth + 1
default:
return h.i, 0
}
}
// HeightStats returns the average and standard deviation of the height
// of elements in the tree
func (t *Tree[T]) HeightStats() (avg, stddev float64) {
av := &AvgVar{}
heightStats(t.root, 0, av)
return av.GetAvg(), av.GetStdDev()
}
func heightStats[T any](h *node[T], d int, av *AvgVar) {
if h == nil {
return
}
av.Add(float64(d))
if h.l != nil {
heightStats(h.l, d+1, av)
}
if h.r != nil {
heightStats(h.r, d+1, av)
}
}
// AvgVar maintains the average and variance of a stream of numbers
// in a space-efficient manner.
type AvgVar struct {
count int64
sum, sumsq float64
}
func (av *AvgVar) Init() {
av.count = 0
av.sum = 0.0
av.sumsq = 0.0
}
func (av *AvgVar) Add(sample float64) {
av.count++
av.sum += sample
av.sumsq += sample * sample
}
func (av *AvgVar) GetCount() int64 { return av.count }
func (av *AvgVar) GetAvg() float64 { return av.sum / float64(av.count) }
func (av *AvgVar) GetTotal() float64 { return av.sum }
func (av *AvgVar) GetVar() float64 {
a := av.GetAvg()
return av.sumsq/float64(av.count) - a*a
}
func (av *AvgVar) GetStdDev() float64 { return math.Sqrt(av.GetVar()) }