-
Notifications
You must be signed in to change notification settings - Fork 0
/
cwl.go
80 lines (70 loc) · 1.5 KB
/
cwl.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 nlm
import "math/rand"
// CountWeightedList holds a weighted list of runes and allows
// selection of random runes weighted by count.
type CountWeightedList[T comparable] struct {
Items []T `json:"items"`
CumulativeWeights []int `json:"weights"`
Total int `json:"total"`
}
// NewCountWeightedList returns a pointer to a CountWeightedList structure
// from which you can select random elements weighted by count.
func NewCountWeightedList[T comparable](counts map[T]int) *CountWeightedList[T] {
n := len(counts)
cwl := CountWeightedList[T]{make([]T, n), make([]int, n), 0}
sum := 0
i := 0
for k, v := range counts {
sum += v
cwl.Items[i] = k
cwl.CumulativeWeights[i] = sum
i++
}
cwl.Total = sum
return &cwl
}
// GetRandomItem returns a random rune from the weighted list.
func (cwl *CountWeightedList[T]) GetRandomItem() T {
if cwl.Total == 0 {
var ret T
return ret
}
i := rand.Int() % cwl.Total
idx := binarySearch(cwl.CumulativeWeights, i)
return cwl.Items[idx]
}
func binarySearch(a []int, n int) int {
l := len(a)
si := l / 2
p := si
for {
switch a[p] > n {
case true:
switch si > 1 {
case true:
si = si / 2
p -= si
case false:
for i := p; i > 0; i-- {
if a[i-1] <= n {
return i
}
}
return 0
}
case false:
switch si > 1 {
case true:
si = si / 2
p += si
case false:
for i := p + 1; i < len(a); i++ {
if a[i] > n {
return i
}
}
return len(a)
}
}
}
}