-
Notifications
You must be signed in to change notification settings - Fork 0
/
linlog.go
66 lines (53 loc) · 1.45 KB
/
linlog.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
// Package linlog implements linear-log bucketing
/*
http://pvk.ca/Blog/2015/06/27/linear-log-bucketing-fast-versatile-simple/
*/
package linlog
import (
"math/bits"
)
// BinOf rounds size as appropriate, and returns the rounded size and bucket number.
func BinOf(size uint64, linear, subbin uint64) (rounded uint64, bucket uint64) {
nBits := lb(size | (1 << uint(linear)))
shift := nBits - subbin
mask := uint64(1<<shift - 1)
round := size + mask /* XXX: overflow. */
subIndex := round >> shift
xrange := nBits - linear
return (round & ^mask), (xrange << subbin) + subIndex
}
// BinDownOf rounds size down, and returns the rounded size and bucket number.
func BinDownOf(size uint64, linear, subbin uint64) (rounded uint64, bucket uint64) {
nBits := lb(size | (1 << linear))
shift := nBits - subbin
subIndex := size >> shift
xrange := nBits - linear
return (subIndex << shift), (xrange << subbin) + subIndex
}
func Bins(max uint64, linear, subbin uint64) []uint64 {
var buckets []uint64
buckets = append(buckets, 0)
var size uint64
var incr uint64 = 1 << (linear - subbin)
for size < (1 << linear) {
size += incr
if size >= max {
break
}
buckets = append(buckets, size)
}
for size < max {
for steps := uint64(0); steps < (1 << subbin); steps++ {
size += incr
buckets = append(buckets, size)
if size > max {
break
}
}
incr <<= 1
}
return buckets
}
func lb(x uint64) uint64 {
return uint64(63 - bits.LeadingZeros64(x))
}