-
Notifications
You must be signed in to change notification settings - Fork 25
/
utils.go
276 lines (230 loc) · 5.31 KB
/
utils.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright 2019 Gregory Petrosyan <gregory.petrosyan@gmail.com>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package rapid
import (
"math"
"math/bits"
)
const (
biasLabel = "bias"
intBitsLabel = "intbits"
coinFlipLabel = "coinflip"
dieRollLabel = "dieroll"
repeatLabel = "@repeat"
)
func bitmask64(n uint) uint64 {
return uint64(1)<<n - 1
}
func genFloat01(s bitStream) float64 {
return float64(s.drawBits(53)) * 0x1.0p-53
}
func genGeom(s bitStream, p float64) uint64 {
assert(p > 0 && p <= 1)
f := genFloat01(s)
n := math.Log1p(-f) / math.Log1p(-p)
return uint64(n)
}
func genUintNNoReject(s bitStream, max uint64) uint64 {
bitlen := bits.Len64(max)
i := s.beginGroup(intBitsLabel, false)
u := s.drawBits(bitlen)
s.endGroup(i, false)
if u > max {
u = max
}
return u
}
func genUintNUnbiased(s bitStream, max uint64) uint64 {
bitlen := bits.Len64(max)
for {
i := s.beginGroup(intBitsLabel, false)
u := s.drawBits(bitlen)
ok := u <= max
s.endGroup(i, !ok)
if ok {
return u
}
}
}
func genUintNBiased(s bitStream, max uint64) (uint64, bool, bool) {
bitlen := bits.Len64(max)
i := s.beginGroup(biasLabel, false)
m := math.Max(8, (float64(bitlen)+48)/7)
n := genGeom(s, 1/(m+1)) + 1
s.endGroup(i, false)
if int(n) < bitlen {
bitlen = int(n)
} else if int(n) >= 64-(16-int(m))*4 {
bitlen = 65
}
for {
i := s.beginGroup(intBitsLabel, false)
u := s.drawBits(bitlen)
ok := bitlen > 64 || u <= max
s.endGroup(i, !ok)
if bitlen > 64 {
u = max
}
if u <= max {
return u, u == 0 && n == 1, u == max && bitlen >= int(n)
}
}
}
func genUintN(s bitStream, max uint64, bias bool) (uint64, bool, bool) {
if bias {
return genUintNBiased(s, max)
} else {
return genUintNUnbiased(s, max), false, false
}
}
func genUintRange(s bitStream, min uint64, max uint64, bias bool) (uint64, bool, bool) {
if min > max {
assertf(false, "invalid range [%v, %v]", min, max) // avoid allocations in the fast path
}
u, lOverflow, rOverflow := genUintN(s, max-min, bias)
return min + u, lOverflow, rOverflow
}
func genIntRange(s bitStream, min int64, max int64, bias bool) (int64, bool, bool) {
if min > max {
assertf(false, "invalid range [%v, %v]", min, max) // avoid allocations in the fast path
}
var posMin, negMin uint64
var pNeg float64
if min >= 0 {
posMin = uint64(min)
pNeg = 0
} else if max <= 0 {
negMin = uint64(-max)
pNeg = 1
} else {
posMin = 0
negMin = 1
pos := uint64(max) + 1
neg := uint64(-min)
pNeg = float64(neg) / (float64(neg) + float64(pos))
if bias {
pNeg = 0.5
}
}
if flipBiasedCoin(s, pNeg) {
u, lOverflow, rOverflow := genUintRange(s, negMin, uint64(-min), bias)
return -int64(u), rOverflow, lOverflow && max <= 0
} else {
u, lOverflow, rOverflow := genUintRange(s, posMin, uint64(max), bias)
return int64(u), lOverflow && min >= 0, rOverflow
}
}
func genIndex(s bitStream, n int, bias bool) int {
assert(n > 0)
u, _, _ := genUintN(s, uint64(n-1), bias)
return int(u)
}
func flipBiasedCoin(s bitStream, p float64) bool {
assert(p >= 0 && p <= 1)
i := s.beginGroup(coinFlipLabel, false)
f := genFloat01(s)
s.endGroup(i, false)
return f >= 1-p
}
type loadedDie struct {
table []int
}
func newLoadedDie(weights []int) *loadedDie {
assert(len(weights) > 0)
if len(weights) == 1 {
return &loadedDie{
table: []int{0},
}
}
total := 0
for _, w := range weights {
assert(w > 0 && w < 100)
total += w
}
table := make([]int, total)
i := 0
for n, w := range weights {
for j := i; i < j+w; i++ {
table[i] = n
}
}
return &loadedDie{
table: table,
}
}
func (d *loadedDie) roll(s bitStream) int {
i := s.beginGroup(dieRollLabel, false)
ix := genIndex(s, len(d.table), false)
s.endGroup(i, false)
return d.table[ix]
}
type repeat struct {
minCount int
maxCount int
avgCount float64
pContinue float64
count int
group int
rejected bool
rejections int
forceStop bool
label string
}
func newRepeat(minCount int, maxCount int, avgCount float64, label string) *repeat {
if minCount < 0 {
minCount = 0
}
if maxCount < 0 {
maxCount = math.MaxInt
}
if avgCount < 0 {
avgCount = float64(minCount) + math.Min(math.Max(float64(minCount), small), (float64(maxCount)-float64(minCount))/2)
}
return &repeat{
minCount: minCount,
maxCount: maxCount,
avgCount: avgCount,
pContinue: 1 - 1/(1+avgCount-float64(minCount)), // TODO was no -minCount intentional?
group: -1,
label: label + repeatLabel,
}
}
func (r *repeat) avg() int {
return int(math.Ceil(r.avgCount))
}
func (r *repeat) more(s bitStream) bool {
if r.group >= 0 {
s.endGroup(r.group, r.rejected)
}
r.group = s.beginGroup(r.label, true)
r.rejected = false
pCont := r.pContinue
if r.count < r.minCount {
pCont = 1
} else if r.forceStop || r.count >= r.maxCount {
pCont = 0
}
cont := flipBiasedCoin(s, pCont)
if cont {
r.count++
} else {
s.endGroup(r.group, false)
}
return cont
}
func (r *repeat) reject() {
assert(r.count > 0)
r.count--
r.rejected = true
r.rejections++
if r.rejections > r.count*2 {
if r.count >= r.minCount {
r.forceStop = true
} else {
panic(invalidData("too many rejections in repeat"))
}
}
}