-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbitmap.go
More file actions
42 lines (37 loc) · 782 Bytes
/
bitmap.go
File metadata and controls
42 lines (37 loc) · 782 Bytes
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
package common
const (
MaxBitSize = 0x01 << 32 // 最大支持的偏移量
)
type BitMap struct {
data []byte // 数据
size uint64 // 最大偏移量
}
func NewBitMap(max uint64) *BitMap {
size := max
if max > MaxBitSize {
size = MaxBitSize
}
return &BitMap{
data: make([]byte, size>>3), // 每个key 8位,所以slice长度为最大偏移量右移3
size: size,
}
}
func (b *BitMap) SetBit(offset uint64, val uint8) bool {
index, pos := offset/8, offset%8
if offset >= b.size {
return false
}
if val == 0 {
b.data[index] &^= 0x01 << pos
} else {
b.data[index] |= 0x01 << pos
}
return true
}
func (b BitMap) GetBit(offset uint64) uint8 {
index, pos := offset/8, offset%8
if offset >= b.size {
return 0
}
return (b.data[index] >> pos) & 0x01
}