-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhistogram.go
More file actions
88 lines (69 loc) · 1.47 KB
/
histogram.go
File metadata and controls
88 lines (69 loc) · 1.47 KB
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
87
88
// Copyright 2024 Leon Hwang.
// SPDX-License-Identifier: Apache-2.0
package main
import (
"bytes"
"fmt"
"strings"
"golang.org/x/exp/constraints"
)
func min[T constraints.Integer](a, b T) T {
if a <= b {
return a
}
return b
}
func printStars[T constraints.Integer](b *bytes.Buffer, val, maxVal T, width int) {
var nStars, nSpaces int
var needPlus bool
nStars = int(min(val, maxVal) * T(width) / maxVal)
nSpaces = width - nStars
needPlus = val > maxVal
fmt.Fprint(b, strings.Repeat("*", nStars))
fmt.Fprint(b, strings.Repeat(" ", nSpaces))
if needPlus {
fmt.Fprint(b, "+")
}
}
func PrintLog2Hist[T constraints.Integer](vals []T, valType string) {
var idxMax int = -1
var valMax T
for i, v := range vals {
if v > 0 {
idxMax = i
}
if v > valMax {
valMax = v
}
}
if idxMax < 0 {
return
}
if idxMax <= 32 {
fmt.Printf("%10s%-14s : %-13s distribution\n", valType, "", "count")
} else {
fmt.Printf("%20s%-24s : %-13s distribution\n", valType, "", "count")
}
var stars int
if idxMax <= 32 {
stars = 40
} else {
stars = 20
}
for i := 0; i <= idxMax; i++ {
low, high := (uint64(1)<<(i+1))>>1, (uint64(1)<<(i+1))-1
if low == high {
low -= 1
}
var b bytes.Buffer
val := vals[i]
if idxMax <= 32 {
fmt.Fprintf(&b, "%10d -> %-10d : %-13d |", low, high, val)
} else {
fmt.Fprintf(&b, "%20d -> %-20d : %-13d |", low, high, val)
}
printStars(&b, val, valMax, stars)
fmt.Fprint(&b, "|\n")
fmt.Print(b.String())
}
}