-
Notifications
You must be signed in to change notification settings - Fork 1
/
bitmap.go
54 lines (48 loc) · 1.09 KB
/
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
45
46
47
48
49
50
51
52
53
54
// Package bitmap implements Bitmap in Go.
package bitmap
// MaxBitmapSize is the maximum bitmap size (in bits).
const MaxBitmapSize uint64 = 0x01 << 40
// Bitmap represents a bitmap.
type Bitmap struct {
data []byte
bitsize uint64
}
// New creates a new Bitmap.
func New(size uint64) *Bitmap {
if size < 1 || size > MaxBitmapSize {
size = MaxBitmapSize
}
r := size & 7
if r != 0 {
r = 1
}
return &Bitmap{
data: make([]byte, (size>>3)+r),
bitsize: size,
}
}
// SetBit sets bit at `offset` to value `v`.
func (b *Bitmap) SetBit(offset uint64, v bool) bool {
if offset > b.bitsize {
return false
}
index, bit := offset>>3, offset&7 // offset/8, offset%8
if v {
b.data[index] |= 0x01 << uint64(bit)
} else {
b.data[index] &^= 0x01 << uint64(bit)
}
return true
}
// GetBit returns the value of bit at `offset`.
func (b *Bitmap) GetBit(offset uint64) bool {
if offset > b.bitsize {
return false
}
index, bit := offset>>3, offset&7
return (b.data[index]>>uint64(bit))&0x01 != 0
}
// Size returns the bitmap size (in bits).
func (b *Bitmap) Size() uint64 {
return b.bitsize
}