-
Notifications
You must be signed in to change notification settings - Fork 9
/
bitset.go
60 lines (50 loc) · 1.05 KB
/
bitset.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
package blockmania
import (
"math/bits"
)
type bitset struct {
commits []uint64
prepares []uint64
}
func (b *bitset) clone() *bitset {
n := &bitset{
commits: make([]uint64, len(b.commits)),
prepares: make([]uint64, len(b.prepares)),
}
copy(n.commits, b.commits)
copy(n.prepares, b.prepares)
return n
}
func (b *bitset) commitCount() int {
c := 0
for _, word := range b.commits {
c += bits.OnesCount64(word)
}
return c
}
func (b *bitset) hasCommit(v uint64) bool {
return b.commits[int(v>>6)]&(1<<(v&63)) != 0
}
func (b *bitset) hasPrepare(v uint64) bool {
return b.prepares[int(v>>6)]&(1<<(v&63)) != 0
}
func (b *bitset) prepareCount() int {
c := 0
for _, word := range b.prepares {
c += bits.OnesCount64(word)
}
return c
}
func (b *bitset) setCommit(v uint64) {
b.commits[int(v>>6)] |= 1 << (v & 63)
}
func (b *bitset) setPrepare(v uint64) {
b.prepares[int(v>>6)] |= 1 << (v & 63)
}
func newBitset(size int) *bitset {
words := (size + 63) >> 6
return &bitset{
commits: make([]uint64, words),
prepares: make([]uint64, words),
}
}