forked from gorgonia/gorgonia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitmap.go
83 lines (69 loc) · 1.8 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
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
package gorgonia
import "math/bits"
const (
bitmapBits = 64
)
// bitmap is a very simple bitmap. It only supports Set, IsSet and Clear methods. It's mostly used for tracking which element has been set
type bitmap struct {
n []uint64
max int
}
// newBitmap creates a new bitmap.
func newBitmap(size int) *bitmap {
q, r := divmod(size, bitmapBits)
if r > 0 {
q++
}
return &bitmap{
n: make([]uint64, q),
max: size,
}
}
// Set sets the ith bit of the bit map to 1. It panics if i is greater or equal to the defined max
func (bm *bitmap) Set(i int) {
if i >= bm.max || i < 0 {
panic("Index out of range")
}
block, pos := divmod(i, bitmapBits)
bm.n[block] |= uint64(1) << uint64(pos)
}
// IsSet returns true if the ith bit is set. It panics if the i is greater or equal to the defined max
func (bm *bitmap) IsSet(i int) bool {
if i >= bm.max || i < 0 {
panic("Index out of range")
}
block, pos := divmod(i, bitmapBits)
return bm.n[block]>>uint64(pos)&uint64(1) == uint64(1)
}
// Clear clears the ith bit. It panics if i is greater or equal to the defined max
func (bm *bitmap) Clear(i int) {
if i >= bm.max || i < 0 {
panic("Index out of range")
}
block, pos := divmod(i, bitmapBits)
bm.n[block] &= ^(uint64(1) << uint64(pos))
}
// BlocksWithZero finds the first block with zeroes in the bit. atleast specifies how many consecutive zeroes need be found
func (bm *bitmap) BlocksWithZero(atleast int) int {
retVal := -1
for i, b := range bm.n {
if bits.OnesCount64(b) != bitmapBits {
// shortcut:
if bits.LeadingZeros64(b) > atleast {
return i
}
var consecutive int
for j := 0; j < bitmapBits; j++ {
if b>>uint64(j)&uint64(1) == 0 {
consecutive++
} else {
consecutive = 0
}
if consecutive > atleast {
return i
}
}
}
}
return retVal
}