/
bitmask.go
91 lines (79 loc) · 1.8 KB
/
bitmask.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
84
85
86
87
88
89
90
91
package numa
import (
"fmt"
"math/bits"
"strconv"
"strings"
)
// Bitmask is used for syscall or other operation in NUMA API.
type Bitmask []uint64
// Set sets the No.i bit to v.
func (b Bitmask) Set(i int, v bool) {
if n := i / 64; n < len(b) {
if v {
b[n] |= uint64(1) << uint64(i%64)
} else {
b[n] &= ^(uint64(1) << uint64(i%64))
}
}
}
// Get returns the No.i bit of this bitmask.
func (b Bitmask) Get(i int) bool {
if n := i / 64; n < len(b) {
return (b[n]>>uint64(i%64))&0x1 != 0
}
return false
}
// ClearAll clear all bits of this bitmask.
func (b Bitmask) ClearAll() {
for i := range b {
b[i] = 0
}
}
// SetAll clear all bits of this bitmask.
func (b Bitmask) SetAll() {
for i := range b {
b[i] = ^uint64(0)
}
}
// OnesCount returns the number of one bits ("population count") in this bitmask.
func (b Bitmask) OnesCount() (n int) {
for _, v := range b {
n += bits.OnesCount64(v)
}
return
}
// String returns the hex string of this bitmask.
func (b Bitmask) String() string {
s := make([]string, 0, len(b))
for _, v := range b {
s = append(s, fmt.Sprintf("%016X", v))
}
return strings.Join(s, ",")
}
// Text returns the digital bit text of this bitmask.
func (b Bitmask) Text() string {
var (
s []string
len = b.Len() * 8
)
for i := 0; i < len; i++ {
if b.Get(i) {
s = append(s, strconv.Itoa(i))
}
}
return strings.Join(s, ",")
}
// Len returns the bitmask length.
func (b Bitmask) Len() int { return len(b) * 64 }
// Clone return a duplicated bitmask.
func (b Bitmask) Clone() Bitmask {
bb := make(Bitmask, len(b))
copy(bb, b)
return bb
}
// NewBitmask returns a bitmask, which length always rounded to a multiple of
// sizeof(uint64). The input param n represents the bit count of this bitmask.
func NewBitmask(n int) Bitmask {
return make(Bitmask, (n+63)/64)
}