tree stats

master
magical 2022-01-23 00:24:37 +00:00
parent 223c046887
commit a3fa9b2387
2 changed files with 81 additions and 1 deletions

76
pmap.go
View File

@ -209,3 +209,79 @@ func insert(n interface{}, shift, hash uint32, key Key, val Value, hashFn hashFu
newNode = _insert(n, shift) newNode = _insert(n, shift)
return return
} }
type stats struct {
count int
maxHeight int
avgHeight float64
avgDegree float64
nodeCount int
leafCount int
collisionCount int
collidedKeys int
emptySlots int
}
func (p pmap) stats() stats {
var s stats
var th float64
var td float64
var visit func(n interface{}, h int)
visit = func(n interface{}, h int) {
switch n := n.(type) {
case leaf:
s.count++
s.leafCount++
th += float64(h)
if s.maxHeight < h {
s.maxHeight = h
}
case *node:
s.count++
s.nodeCount++
s.emptySlots += bits.OnesCount32(^n.bitmap)
td += float64(bits.OnesCount32(n.bitmap))
for i := range n.child {
if n.child[i] != nil {
visit(n.child[i], h+1)
}
}
case *collision:
s.collisionCount++
s.collidedKeys += len(n.leaf)
default:
panic("pmap: unhandled case in stats")
}
}
visit(p.root, 1)
if s.leafCount > 0 {
s.avgHeight = th / float64(s.leafCount)
}
if s.nodeCount > 0 {
s.avgDegree = td / float64(s.nodeCount)
}
return s
}
func (s stats) String() string {
return fmt.Sprintf(
"count = %d\n"+
"maxHeight = %d\n"+
"avgHeight = %g\n"+
"avgDegree = %g\n"+
"nodeCount = %d\n"+
"leafCount = %d\n"+
"collisionCount = %d\n"+
"collidedKeys = %d\n"+
"emptySlots = %d\n",
s.count,
s.maxHeight,
s.avgHeight,
s.avgDegree,
s.nodeCount,
s.leafCount,
s.collisionCount,
s.collidedKeys,
s.emptySlots,
)
}

View File

@ -1,6 +1,9 @@
package pmap package pmap
import "testing" import (
"fmt"
"testing"
)
func TestPmap(t *testing.T) { func TestPmap(t *testing.T) {
p := New() p := New()
@ -17,6 +20,7 @@ func TestPmap(t *testing.T) {
t.Errorf("Get(%d) = %v %v, want %v %v", i, v, ok, i, true) t.Errorf("Get(%d) = %v %v, want %v %v", i, v, ok, i, true)
} }
} }
fmt.Print(p.(pmap).stats())
} }
func BenchmarkGet(b *testing.B) { func BenchmarkGet(b *testing.B) {