-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitarray.go
466 lines (428 loc) · 9.37 KB
/
bitarray.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
// Package bitarray implements a bit array.
//
// The least significant bit is at index 0. In the string representation used by
// [Parse], [MustParse], and [BitArray.String] it is the rightmost digit.
// The byte slice used by [FromBytes] and [BitArray.ToBytes] is in big-endian order.
package bitarray
import (
"bytes"
"encoding/gob"
"fmt"
"math"
"math/bits"
"slices"
"strings"
)
const bitsN = 8
// BitArray type.
type BitArray struct {
size int
data []uint8
}
// New creates a new BitArray with size bits and the bits at the given indexes
// set to 1. Panics if size <= 0 or one of the indexes is out of range.
func New(size int, idx ...int) *BitArray {
if size <= 0 {
panic("size must be > 0")
}
n, r := size/bitsN, size%bitsN
if r > 0 {
n++
}
ba := BitArray{size, make([]uint8, n)}
for _, i := range idx {
ba.Set(i)
}
return &ba
}
// Parse creates a new BitArray by parsing the given string. Space characters are ignored.
// Returns an error if one of the characters in the string is not space, 0, or 1.
func Parse(s string) (*BitArray, error) {
rs := []rune(strings.ReplaceAll(s, " ", ""))
rsLen := len(rs)
ba := New(len(rs))
for i, c := range rs {
if c == '1' {
ba.set(rsLen - 1 - i)
} else if rs[i] != '0' {
return nil, fmt.Errorf("unknown character: %c", rs[i])
}
}
return ba, nil
}
// MustParse creates a new BitArray by parsing the given string. Space characters are
// ignored. Panics if one of the characters in the string is not space, 0, or 1.
func MustParse(s string) *BitArray {
ba, err := Parse(s)
if err != nil {
panic(err)
}
return ba
}
// Clone clones the BitArray.
func Clone(ba *BitArray) *BitArray {
sl := make([]uint8, len(ba.data))
copy(sl, ba.data)
return &BitArray{ba.size, sl}
}
// Clear sets all bits to 0.
func (ba *BitArray) Clear() {
for i := range ba.data {
ba.data[i] = 0
}
}
// SetAll sets all bits to 1.
func (ba *BitArray) SetAll() {
if x := ba.size % bitsN; x == 0 {
ba.data[len(ba.data)-1] = math.MaxUint8
} else {
ba.data[len(ba.data)-1] = (2 << (x - 1)) - 1
}
for i := 0; i < len(ba.data)-1; i++ {
ba.data[i] = math.MaxUint8
}
}
// Set sets the bit at index idx to 1.
func (ba *BitArray) Set(idx int) {
ba.checkIdx(idx)
ba.set(idx)
}
func (ba *BitArray) set(idx int) {
n, i := idx/bitsN, idx%bitsN
ba.data[n] |= 1 << i
}
// Unset sets the bit at index idx to 0.
func (ba *BitArray) Unset(idx int) {
ba.checkIdx(idx)
ba.unset(idx)
}
func (ba *BitArray) unset(idx int) {
n, i := idx/bitsN, idx%bitsN
ba.data[n] &^= 1 << i
}
// Toggle toggles the state of the bit at index idx and reports whether it is set after being toggled.
func (ba *BitArray) Toggle(idx int) bool {
ba.checkIdx(idx)
b := ba.get(idx)
if b {
ba.unset(idx)
} else {
ba.set(idx)
}
return !b
}
// Get reports whether the bit at index idx is set.
func (ba *BitArray) Get(idx int) bool {
ba.checkIdx(idx)
return ba.get(idx)
}
func (ba *BitArray) get(idx int) bool {
n, i := idx/bitsN, idx%bitsN
return ba.data[n]&(1<<i) != 0
}
// And sets ba = ba & other (bitwise AND).
func (ba *BitArray) And(other *BitArray) {
ba.checkSize(other)
for i := 0; i < len(ba.data)-1; i++ {
ba.data[i] &= other.data[i]
}
if ba.size%bitsN == 0 {
i := len(ba.data) - 1
ba.data[i] &= other.data[i]
} else {
for i := (ba.size / bitsN) * bitsN; i < ba.size; i++ {
if !other.get(i) {
ba.unset(i)
}
}
}
}
// Or sets ba = ba | other (bitwise OR).
func (ba *BitArray) Or(other *BitArray) {
ba.checkSize(other)
for i := 0; i < len(ba.data)-1; i++ {
ba.data[i] |= other.data[i]
}
if ba.size%bitsN == 0 {
i := len(ba.data) - 1
ba.data[i] |= other.data[i]
} else {
for i := (ba.size / bitsN) * bitsN; i < ba.size; i++ {
if other.get(i) {
ba.set(i)
}
}
}
}
// Xor sets ba = ba ^ other (bitwise XOR).
func (ba *BitArray) Xor(other *BitArray) {
ba.checkSize(other)
for i := 0; i < len(ba.data)-1; i++ {
ba.data[i] ^= other.data[i]
}
if ba.size%bitsN == 0 {
i := len(ba.data) - 1
ba.data[i] ^= other.data[i]
} else {
for i := (ba.size / bitsN) * bitsN; i < ba.size; i++ {
b1 := ba.get(i)
b2 := other.get(i)
b := (b1 || b2) && !(b1 && b2)
if b && !b1 {
ba.set(i)
} else if !b && b1 {
ba.unset(i)
}
}
}
}
// AndNot sets ba = ba &^ other (bit clear).
func (ba *BitArray) AndNot(other *BitArray) {
ba.checkSize(other)
for i := 0; i < len(ba.data)-1; i++ {
ba.data[i] &^= other.data[i]
}
if ba.size%bitsN == 0 {
i := len(ba.data) - 1
ba.data[i] &^= other.data[i]
} else {
for i := (ba.size / bitsN) * bitsN; i < ba.size; i++ {
if ba.get(i) && other.get(i) {
ba.unset(i)
}
}
}
}
// Not sets ba = ^ba.
func (ba *BitArray) Not() {
for i := 0; i < len(ba.data)-1; i++ {
ba.data[i] = ^ba.data[i]
}
if ba.size%bitsN == 0 {
i := len(ba.data) - 1
ba.data[i] = ^ba.data[i]
} else {
for i := (ba.size / bitsN) * bitsN; i < ba.size; i++ {
if ba.get(i) {
ba.unset(i)
} else {
ba.set(i)
}
}
}
}
// Rotate rotates the bit array by |n| bits. If n > 0 to the left, if n < 0 to the right.
func (ba *BitArray) Rotate(n int) {
n = n % ba.size
var aux *BitArray
if n > 0 {
aux = Slice(ba, ba.size-n, ba.size)
} else if n < 0 {
aux = Slice(ba, 0, -n)
}
start, end := ba.moveBits(n)
for i := start; i < end; i++ {
if aux.get(i - start) {
ba.set(i)
} else {
ba.unset(i)
}
}
}
// Shift shifts the bit array by |n| bits. If n > 0 to the left, if n < 0 to the right.
func (ba *BitArray) Shift(n int) {
if n > 0 && n >= ba.size || n < 0 && -n >= ba.size {
ba.Clear()
return
}
start, end := ba.moveBits(n)
for i := start; i < end; i++ {
ba.unset(i)
}
}
func (ba *BitArray) moveBits(n int) (int, int) {
if n == 0 {
return 0, 0
}
var start, end int
if n > 0 {
for i := ba.size - 1; i >= n; i-- {
if ba.get(i - n) {
ba.set(i)
} else {
ba.unset(i)
}
}
end = n
} else {
n = -n
for i := 0; i < ba.size-n; i++ {
if ba.get(i + n) {
ba.set(i)
} else {
ba.unset(i)
}
}
start = ba.size - n
end = ba.size
}
return start, end
}
// Equal reports whether the two bit arrays are equal.
func (ba *BitArray) Equal(other *BitArray) bool {
if ba.size != other.size {
return false
}
for i := range ba.data {
if ba.data[i] != other.data[i] {
return false
}
}
return true
}
// Count returns the number of set bits.
func (ba *BitArray) Count() int {
cnt := 0
for _, x := range ba.data {
cnt += bits.OnesCount8(x)
}
return cnt
}
// LeadingZeros returns the number of leading unset bits.
func (ba *BitArray) LeadingZeros() int {
cnt := 0
for i := len(ba.data) - 1; i >= 0; i-- {
if ba.data[i] == 0 {
cnt += bitsN
} else {
cnt += bits.LeadingZeros8(ba.data[i])
break
}
}
if x := ba.size % bitsN; x != 0 {
cnt -= bitsN - x
}
return cnt
}
// TrailingZeros returns the number of trailing unset bits.
func (ba *BitArray) TrailingZeros() int {
cnt := 0
for _, x := range ba.data {
if x == 0 {
cnt += bitsN
} else {
cnt += bits.TrailingZeros8(x)
break
}
}
if cnt > ba.size {
cnt = ba.size
}
return cnt
}
// Size returns the size of the bit array.
func (ba *BitArray) Size() int {
return ba.size
}
// String returns a string representation of the bit array.
func (ba *BitArray) String() string {
baLen := len(ba.data)
n := ba.size % bitsN
if n == 0 {
n = bitsN
}
s := fmt.Sprintf("%0*b", n, ba.data[baLen-1])
if baLen > 1 {
for i := baLen - 2; i >= 0; i-- {
s += fmt.Sprintf("%0*b", bitsN, ba.data[i])
}
}
return s
}
func (ba *BitArray) checkIdx(idx int) {
if idx < 0 || idx >= ba.size {
panic("index out of range")
}
}
func (ba *BitArray) checkSize(other *BitArray) {
if ba.size != other.size {
panic("bit array sizes must be equal")
}
}
// MarshalBinary implements the [encoding.BinaryMarshaler] interface.
func (ba *BitArray) MarshalBinary() ([]byte, error) {
b := new(bytes.Buffer)
enc := gob.NewEncoder(b)
err := enc.Encode(ba.size)
if err != nil {
return nil, err
}
err = enc.Encode(ba.data)
if err != nil {
return nil, err
}
return b.Bytes(), nil
}
// UnmarshalBinary implements the [encoding.BinaryUnmarshaler] interface.
func (ba *BitArray) UnmarshalBinary(data []byte) error {
b := bytes.NewReader(data)
dec := gob.NewDecoder(b)
err := dec.Decode(&ba.size)
if err != nil {
return err
}
err = dec.Decode(&ba.data)
if err != nil {
return err
}
return nil
}
// Slice returns a new BitArray with the bits from ba at indexes [start, end).
func Slice(ba *BitArray, start, end int) *BitArray {
ba.checkIdx(start)
ba.checkIdx(end - 1)
result := New(end - start)
idx := 0
for i := start; i < end; i++ {
if ba.get(i) {
result.set(idx)
}
idx++
}
return result
}
// Concat returns a new BitArray with the bits from ba1 and ba2 concatenated.
func Concat(ba1, ba2 *BitArray) *BitArray {
ba := New(ba1.size + ba2.size)
n := ba2.size / bitsN
for i := 0; i < n; i++ {
ba.data[i] = ba2.data[i]
}
for i := n * bitsN; i < ba2.size; i++ {
if ba2.get(i) {
ba.set(i)
}
}
i2 := 0
for i := ba2.size; i < ba.size; i++ {
if ba1.get(i2) {
ba.set(i)
}
i2++
}
return ba
}
// FromBytes creates a new BitArray from the byte slice. Panics if len(bytes) == 0.
func FromBytes(bytes []byte) *BitArray {
if len(bytes) == 0 {
panic("size must be > 0")
}
slices.Reverse(bytes)
return &BitArray{size: len(bytes) * bitsN, data: bytes}
}
// ToBytes returns the bit array as a byte slice.
func (ba *BitArray) ToBytes() []byte {
bytes := ba.data
slices.Reverse(bytes)
return bytes
}