-
Notifications
You must be signed in to change notification settings - Fork 2
/
uint.go
89 lines (75 loc) · 1.04 KB
/
uint.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
package constraints
import (
"math"
"math/bits"
)
type Uint struct {
Min uint
Max uint
}
func UintDefault() Uint {
if bits.UintSize == 32 {
return Uint{
Min: 0,
Max: math.MaxUint32,
}
}
return Uint{
Min: 0,
Max: uint(math.MaxUint64),
}
}
type Uint8 struct {
Min uint8
Max uint8
}
func Uint8Default() Uint8 {
return Uint8{
Min: 0,
Max: math.MaxUint8,
}
}
type Uint16 struct {
Min uint16
Max uint16
}
func Uint16Default() Uint16 {
return Uint16{
Min: 0,
Max: math.MaxUint16,
}
}
type Uint32 struct {
Min uint32
Max uint32
}
func Uint32Default() Uint32 {
return Uint32{
Min: 0,
Max: math.MaxUint32,
}
}
type Uint64 struct {
Min uint64
Max uint64
}
func (u Uint64) Baised(bias Bias) Uint64 {
diff := u.Max - u.Min
bitSize := bits.Len64(diff)
factor := biasedFactor(bias, bitSize)
if factor == bitSize {
diff = 0
} else {
diff = diff >> (factor % bitSize)
}
return Uint64{
Min: u.Min,
Max: u.Min + diff,
}
}
func Uint64Default() Uint64 {
return Uint64{
Min: 0,
Max: math.MaxUint64,
}
}