-
Notifications
You must be signed in to change notification settings - Fork 0
/
bit_flags.go
91 lines (77 loc) · 1.52 KB
/
bit_flags.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
package utils
type BitFlag uint8
func (bf *BitFlag) Set(flag BitFlag) {
*bf |= flag
}
func (bf *BitFlag) SetMany(flags ...BitFlag) {
for _, flag := range flags {
*bf |= flag
}
}
func (bf *BitFlag) Get(flag BitFlag) BitFlag {
return *bf & flag
}
func (bf *BitFlag) GetMany(flags ...BitFlag) BitFlag {
var mask BitFlag
mask.SetMany(flags...)
return *bf & mask
}
func (bf *BitFlag) Clear(flag BitFlag) {
*bf &^= flag
}
func (bf *BitFlag) ClearMany(flags ...BitFlag) {
for _, flag := range flags {
*bf &^= flag
}
}
func (bf *BitFlag) ClearAll() {
*bf = 0
}
func (bf *BitFlag) Toggle(flag BitFlag) {
*bf ^= flag
}
func (bf *BitFlag) ToggleMany(flags ...BitFlag) {
for _, flag := range flags {
*bf ^= flag
}
}
// Test with multiple flags is effectively TestAny
func (bf *BitFlag) Test(flag BitFlag) bool {
return *bf&flag != 0
}
func (bf *BitFlag) TestAny(flags ...BitFlag) bool {
for _, flag := range flags {
if *bf&flag != 0 {
return true
}
}
return false
}
func (bf *BitFlag) TestAll(flags ...BitFlag) bool {
for _, flag := range flags {
if *bf&flag == 0 {
return false
}
}
return true
}
// Cleared with multiple flags is effectively ClearedAll
func (bf *BitFlag) Cleared(flag BitFlag) bool {
return *bf&flag == 0
}
func (bf *BitFlag) ClearedAny(flags ...BitFlag) bool {
for _, flag := range flags {
if *bf&flag == 0 {
return true
}
}
return false
}
func (bf *BitFlag) ClearedAll(flags ...BitFlag) bool {
for _, flag := range flags {
if *bf&flag != 0 {
return false
}
}
return true
}