/
bitmap.go
44 lines (37 loc) · 976 Bytes
/
bitmap.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
package token
import (
"math/bits"
"github.com/KSpaceer/yamly/engines/yayamls/yamlchar"
)
type conformationBitmap uint16
func (b conformationBitmap) Set(cst yamlchar.CharSetType, val bool) conformationBitmap {
if val {
return b.setTrue(cst)
}
return b.setFalse(cst)
}
func (b conformationBitmap) Get(cst yamlchar.CharSetType) (bool, bool) {
shift := 2 * bits.TrailingZeros16(uint16(cst))
val := (b & (3 << shift)) >> shift
switch val {
case 0, 3:
return false, false
case 1:
return true, true
case 2:
return false, true
}
return false, false
}
func (b conformationBitmap) setTrue(cst yamlchar.CharSetType) conformationBitmap {
shift := 2 * bits.TrailingZeros16(uint16(cst))
// remove false
b &= ^(1 << (1 + shift))
return b | (1 << shift)
}
func (b conformationBitmap) setFalse(cst yamlchar.CharSetType) conformationBitmap {
shift := 1 + 2*bits.TrailingZeros16(uint16(cst))
// remove true
b &= ^(1 << (shift - 1))
return b | (1 << shift)
}