-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbitmap.go
49 lines (44 loc) · 869 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
45
46
47
48
49
package godata
// bit map or bit array.
type BitMap struct {
bits []uint8
max uint64
}
// return a new bit map.
func NewBitMap(max uint64) *BitMap {
return &BitMap{
max: max,
bits: make([]uint8, max>>3+1),
}
}
// is right return true ,or false
func (b *BitMap) Add(num uint64) {
if num > b.max {
return
}
byteIndex := num >> 3
position := num & 0x7
b.bits[byteIndex] |= 1<< position
}
func (b *BitMap) IsExit(num uint64) bool {
if num > b.max {
return false
}
byteIndex := num >> 3
position := num & 0x7
return (b.bits[byteIndex]) & (1<<position) != 0
}
// init the bitMap.
func (b *BitMap) Init() {
b.bits = make([]uint8, b.max>>3+1)
}
func(b *BitMap)Delete(num uint64){
if num > b.max {
return
}
byteIndex := num >> 3
position := num & 0x7
if (b.bits[byteIndex]) & (1<< position) != 0{
b.bits[byteIndex] ^= (1<<position)
}
}