-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitList.go
48 lines (41 loc) · 940 Bytes
/
bitList.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
package migemo
import "fmt"
type BitList struct {
Words []uint64
Size int
}
func NewBitList() *BitList {
return &BitList{
Words: make([]uint64, 0, 8),
Size: 0,
}
}
func NewBitListWithSize(size int) *BitList {
return &BitList{
Words: make([]uint64, (size+63)/64),
Size: size,
}
}
func (self *BitList) Add(value bool) {
if len(self.Words) < (self.Size+1+63)/64 {
self.Words = append(self.Words, 0)
}
self.Set(self.Size, value)
self.Size++
}
func (self *BitList) Set(pos int, value bool) {
if self.Size < pos {
panic(fmt.Sprintf("index out of range [%d] with length %d", pos, self.Size))
}
if value {
self.Words[pos/64] |= uint64(1) << (pos % 64)
} else {
self.Words[pos/64] &= ^(uint64(1) << (pos % 64))
}
}
func (self *BitList) Get(pos int) bool {
if self.Size < pos {
panic(fmt.Sprintf("index out of range [%d] with length %d", pos, self.Size))
}
return (self.Words[pos/64]>>(pos%64))&1 == 1
}