-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitset.go
107 lines (96 loc) · 2.38 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// molecular is a 3D physics engine written in Go
// Copyright (C) 2023 Kevin Z <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package molecular
import (
"encoding/binary"
)
type Bitset struct {
data []uint32
}
// NewBitset creates a Bitset with at least `n` bits
func NewBitset(n int) *Bitset {
return &Bitset{
data: make([]uint32, (n+31)/32),
}
}
// Cap returns the bit slots of the Bitset
func (b *Bitset) Cap() int {
return len(b.data) * 32
}
// Get will return true if the target bit is one
func (b *Bitset) Get(i int) bool {
n := i % 32
i /= 32
if i >= len(b.data) {
return false
}
return b.data[i]&(1<<n) != 0
}
// Set set the bit to one
func (b *Bitset) Set(i int) {
n := i % 32
i /= 32
if i >= len(b.data) {
b.data = growToLen(b.data, i+1)
}
b.data[i] |= 1 << n
}
// Clear set the bit to zero
func (b *Bitset) Clear(i int) {
n := i % 32
i /= 32
if i >= len(b.data) {
b.data = growToLen(b.data, i+1)
}
b.data[i] &^= 1 << n
}
// Flip toggle the bit, and returns the old value
func (b *Bitset) Flip(i int) bool {
n := i % 32
i /= 32
if i >= len(b.data) {
b.data = growToLen(b.data, i+1)
} else if b.data[i]&(1<<n) != 0 {
b.data[i] &^= 1 << n
return true
}
b.data[i] |= 1 << n
return false
}
func (b *Bitset) String() string {
const prefix = "bitset:"
buf := make([]byte, len(prefix)+len(b.data)*32)
copy(buf, prefix)
for i, v := range b.data {
for j := 0; j < 32; j++ {
n := i*32 + j + len(prefix)
if v&(1<<j) == 0 {
buf[n] = '0'
} else {
buf[n] = '1'
}
}
}
return (string)(buf)
}
// Bytes encode the Bitset to bytes use LittleEndian mode
func (b *Bitset) Bytes() (buf []byte) {
buf = make([]byte, len(b.data)*4)
for i, v := range b.data {
binary.LittleEndian.PutUint32(buf[i*4:], v)
}
return
}