-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbitset.go
69 lines (56 loc) · 1.41 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
61
62
63
64
65
66
67
68
69
// This file is subject to a 1-clause BSD license.
// Its contents can be found in the enclosed LICENSE file.
package evdev
import (
"math"
"unsafe"
)
const (
WordBitSize = 64
WordByteSize = 8
)
// A word is part of a bitset.
type Word uint64
// Bitset defines a set of bit values.
type Bitset []Word
// NewBitset creates a new bitset of the given size.
func NewBitset(bits int) Bitset {
size := int(math.Ceil((float64(bits) / WordBitSize)))
return make(Bitset, size)
}
// Len returns the number of bits in the set.
func (b Bitset) Len() int {
return len(b) * WordBitSize
}
// Bytes returns the bitset as a byte slice.
// This is the same memory, so any changes to the returned slice,
// will affect the bitset.
func (b Bitset) Bytes() []byte {
if len(b) == 0 {
return nil
}
size := len(b) * WordByteSize
return (*(*[1<<31 - 1]byte)(unsafe.Pointer(&b[0])))[:size]
}
// Set sets the bit at the given index.
func (b Bitset) Set(i int) {
w := i / WordBitSize
if i < 0 || w >= len(b) {
return
}
bit := Word(1 << uint(i%WordBitSize))
b[w] &^= bit
b[w] ^= bit
}
// Unset clears the bit at the given index.
func (b Bitset) Unset(i int) {
w := i / WordBitSize
if i >= 0 && w < len(b) {
b[w] &^= 1 << uint(i%WordBitSize)
}
}
// Test returns true if the bit at the given index is set.
func (b Bitset) Test(i int) bool {
w := i / WordBitSize
return i >= 0 && w < len(b) && ((b[w]>>uint(i%WordBitSize))&1) == 1
}