-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathba.go
177 lines (151 loc) · 3.78 KB
/
ba.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package bitarray
import (
"math"
"math/bits"
)
type Bit = uint64
const (
Zero = Bit(iota)
One
)
// BitArray is an array data structure that compactly stores bits.
// Bits externally represented as `bool` are stored internally as `uint64`s.
// The total number of bits stored is set at creation and is immutable.
type BitArray struct {
// buf is a backing array that bits writes into by default when the no. of bits requested to allocate is
// < 512. Only if more is asked, we'll skip buf and allocate directly into bits
buf [8]Bit
bits []Bit
n int // no. of bits
}
// New creates a new BitArray of `n` bits. If n <= 512, no allocation is done.
func New(n int) (ba BitArray) {
nblk := nbitsToNblks(n)
ba.n = n
ba.bits = ba.buf[:]
if nblk <= 8 {
ba.bits = ba.buf[:nblk]
return
}
ba.bits = append(ba.bits, make([]Bit, nblk-len(ba.buf))...)
return
}
// Copy copies src into dst.
func Copy(dst, src *BitArray) {
if dst != src && src != nil {
if src.n != dst.n {
panic("size of bit arrays must be the same for copy")
}
if dst.n == 0 {
// nothing to do here, since the source `oa` has nothing to copy from
return
}
copy(dst.bits, src.bits)
}
}
// FromStr creates a BitArray from a bit string
func FromStr(bs string) BitArray {
ba := New(len(bs))
for i, b := range bs {
if b == '1' {
ba.Set(i)
}
}
return ba
}
// FromUint64 creates a BitArray from the bit representation of u.
func FromUint64(u uint64) BitArray {
ba := New(64)
ba.bits[0] = u
return ba
}
// Size returns the no. of bits stored.
func (ba *BitArray) Size() int { return ba.n }
// Set sets the bit at position k.
func (ba *BitArray) Set(k int) { bi, si := biandsi(k); set(&ba.bits[bi], si) }
// SetAll sets all the bits.
func (ba *BitArray) SetAll() {
for i := range ba.bits {
ba.bits[i] = math.MaxUint64
}
}
// Clr clears the bit at position k.
func (ba *BitArray) Clr(k int) { bi, si := biandsi(k); clr(&ba.bits[bi], si) }
// ClrAll clears all the bits.
func (ba *BitArray) ClrAll() {
for i := range ba.bits {
ba.bits[i] = 0
}
}
// ChkSet returns the value of the bit at position k before setting it.
func (ba *BitArray) ChkSet(k int) (b bool) {
bi, si := biandsi(k)
u := &ba.bits[bi]
b = chk(*u, si) != 0
if !b {
set(u, si)
}
return
}
// ChkClr returns the value of the bit at position k before clearing it.
func (ba *BitArray) ChkClr(k int) (b bool) {
bi, si := biandsi(k)
u := &ba.bits[bi]
b = chk(*u, si) != 0
if b {
clr(u, si)
}
return
}
// Tgl toggles the bit at position k.
func (ba *BitArray) Tgl(k int) {
bi, si := biandsi(k)
ba.bits[bi] ^= 1 << si
}
// Cnt returns the number of set bits.
func (ba *BitArray) Cnt() (n int) {
for _, b := range ba.bits {
n += bits.OnesCount64(b)
}
return
}
// Chk returns the value of the bit at position k.
func (ba *BitArray) Chk(k int) bool {
bi, si := biandsi(k)
return chk(ba.bits[bi], si) != 0
}
// Put sets the value of the bit at position k to v.
func (ba *BitArray) Put(k int, v Bit) {
bi, si := biandsi(k)
put(&ba.bits[bi], si, v)
}
// Swap swaps the value of bit at position k with v. On return, v contains the old value.
func (ba *BitArray) Swap(k int, b *Bit) {
bi, si := biandsi(k)
t := &ba.bits[bi]
ob := chk(*t, si)
if ob == *b {
return
}
put(t, si, ob)
*b = ob
}
func (ba *BitArray) String() string {
sb := make([]byte, ba.n)
for i := range sb {
sb[i] = '0'
if ba.Chk(i) {
sb[i] = '1'
}
}
return string(sb)
}
func nbitsToNblks(n int) int { return int(math.Ceil(float64(n) / 64)) }
func set(u *uint64, si uint64) { *u |= 1 << si }
func clr(u *uint64, si uint64) { *u &= ^(1 << si) }
func chk(u uint64, si uint64) Bit { return (u >> si) & 1 }
func put(u *uint64, si uint64, b Bit) { *u = (*u & ^(1 << si)) | (b << si) }
func biandsi(k int) (uint64, uint64) {
i := uint64(k)
return i / 64, i % 64
}