-
Notifications
You must be signed in to change notification settings - Fork 1
/
bitset.go
89 lines (77 loc) · 1.27 KB
/
bitset.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 liter
import (
"io"
)
type BitSet []uint64
var _ Packet = (*BitSet)(nil)
func NewBitSet() (s *BitSet) {
s = new(BitSet)
*s = make(BitSet, 0)
return
}
func NewBitSetWith(size int) (s *BitSet) {
leng := size / 64
if size%64 != 0 {
leng++
}
s = new(BitSet)
*s = make(BitSet, leng)
return
}
func (s BitSet) Encode(p *PacketBuilder) {
p.VarInt((VarInt)(len(s)))
for _, v := range s {
p.Long((Long)(v))
}
}
func (s *BitSet) DecodeFrom(r *PacketReader) (err error) {
var (
ok bool
l VarInt
v Long
)
if l, ok = r.VarInt(); !ok {
return io.EOF
}
*s = make(BitSet, l)
for i, _ := range *s {
if v, ok = r.Long(); !ok {
return io.EOF
}
(*s)[i] = (uint64)(v)
}
return
}
func (s *BitSet) Set(index int, value bool) {
i, b := index/64, (uint64)(1)<<(index%64)
arr := *s
if i >= len(arr) {
for i >= len(arr) {
arr = append(arr, 0)
}
*s = arr
}
if value {
arr[i] |= b
} else {
arr[i] &^= b
}
}
func (s *BitSet) Open(index int) {
s.Set(index, true)
}
func (s *BitSet) Close(index int) {
s.Set(index, true)
}
func (s *BitSet) Get(index int) (value bool) {
i, b := index/64, (uint64)(1)<<(index%64)
if i >= len(*s) {
return false
}
return (*s)[i]&b != 0
}
func (s *BitSet) Clear() {
for i, _ := range *s {
(*s)[i] = 0
}
}