-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitset.go
More file actions
504 lines (420 loc) · 10.5 KB
/
bitset.go
File metadata and controls
504 lines (420 loc) · 10.5 KB
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
package mind
import (
"math/bits"
)
type UInt interface {
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}
// BitSet32 is the default BitSet
type BitSet32 = BitSet[uint32]
type BitSet[U UInt] struct {
data []uint64
count int // cached popcount; -1 means dirty (needs recount)
}
const defaultSize = (1 << 16) / 64
// NewBitSet creates a new BitSet
func NewBitSet[U UInt]() *BitSet[U] {
return &BitSet[U]{data: make([]uint64, 0, defaultSize)}
}
// NewEmptyBitSet creates a new BitSet with len and cap = 0
func NewEmptyBitSet[U UInt]() *BitSet[U] {
return &BitSet[U]{data: make([]uint64, 0)}
}
// NewBitSetWithCapacity creates a new BitSet with starting capacity
func NewBitSetWithCapacity[U UInt](bits int) *BitSet[U] {
words := (bits + 63) >> 6
return &BitSet[U]{data: make([]uint64, 0, words)}
}
// NewBitSetFrom creates a new BitSet from given values
func NewBitSetFrom[U UInt](values ...U) *BitSet[U] {
var maxVal U
for _, v := range values {
if v > maxVal {
maxVal = v
}
}
b := NewBitSetWithCapacity[U](int(maxVal) + 1)
for _, v := range values {
b.Set(v)
}
return b
}
//go:inline
func (b *BitSet[U]) grow(targetIndex int) {
needed := targetIndex + 1 - len(b.data)
if needed > 0 {
// runtime optimizes this allocation pattern heavily.
// it will handle the capacity doubling strategy for us.
b.data = append(b.data, make([]uint64, needed)...)
}
}
// Set inserts or updates the key in the BitSet
func (b *BitSet[U]) Set(value U) {
// i>>6 is equals i/64 but faster
// i&63 is the same: i%64, but faster
index := int(value) >> 6
bit := uint64(1) << (value & 63)
if index >= len(b.data) {
b.grow(index)
}
old := b.data[index]
b.data[index] = old | bit
// if the bit was not already set, increment count
if old&bit == 0 && b.count >= 0 {
b.count++
}
}
// UnSet removes the key from the BitSet. Clear the bit value to 0.
func (b *BitSet[U]) UnSet(value U) bool {
index := int(value) >> 6
if index < len(b.data) {
bit := uint64(1) << (value & 63)
old := b.data[index]
b.data[index] = old &^ bit
// if the bit was set, decrement count
if old&bit != 0 && b.count >= 0 {
b.count--
}
return true
}
return false
}
// Contains check, is the value saved in the BitSet
func (b *BitSet[U]) Contains(value U) bool {
index := int(value) >> 6
if index >= len(b.data) {
return false
}
return (b.data[index] & (1 << (value & 63))) != 0
}
// ValueOnIndex returns the Value of the dx-th matched item.
// For exmaple: BitSet Values: [1, 2, 8, 42, 1028]
// 0 -> 1
// 1 -> 2
// 2 -> 8
// 3 -> 42
// 4 -> 1028
// 5 -> not found
func (b *BitSet[U]) ValueOnIndex(idx uint32) (uint32, bool) {
for i, word := range b.data {
if word == 0 {
continue
}
// counts how many '1's are in this 64-bit word in a single CPU cycle.
pop := uint32(bits.OnesCount64(word))
// if the matches we need are further ahead, skip this ENTIRE block!
if idx >= pop {
idx -= pop
continue
}
// the exact bit we want is inside this specific 64-bit word.
// We use Brian Kernighan's Algorithm to clear the lowest set bit 'k' times.
for j := uint32(0); j < idx; j++ {
word &= word - 1 // Magic: Erases the lowest '1' bit
}
// Now, the bit we are looking for is the lowest remaining '1'.
// TrailingZeros64 tells us exactly which bit position it is (0 to 63).
bitPos := uint32(bits.TrailingZeros64(word))
// calculate the absolute index in the List
absoluteIndex := uint32(i*64) + bitPos
return absoluteIndex, true
}
return 0, false
}
// Range iterates over set bits between 'from' and 'to' (inclusive).
// It calls 'visit' for each found bit. If 'visit' returns false, iteration stops.
func (b *BitSet[U]) Range(from, to U, visit func(v U) bool) {
if from > to || len(b.data) == 0 {
return
}
startWord := int(from >> 6)
endWord := int(to >> 6)
// bounds check
if startWord >= len(b.data) {
return
}
if endWord >= len(b.data) {
endWord = len(b.data) - 1
}
for i := startWord; i <= endWord; i++ {
w := b.data[i]
if w == 0 {
continue
}
if i == startWord {
w &= (^uint64(0) << (from & 63))
}
if i == endWord {
w &= (^uint64(0) >> (63 - (to & 63)))
}
for w != 0 {
t := bits.TrailingZeros64(w)
val := U(i<<6) + U(t)
if !visit(val) {
return
}
w &= w - 1
}
}
}
// Min return the min value where an Bit is set
// [1, 3, 100] => 1
// if no max found, return -1
func (b *BitSet[U]) Min() int {
bd := b.data
for i, w := range bd {
if w != 0 {
// bits.TrailingZeros64 returns the number of zero bits
// before the first set bit (the "1").
// Example: w = ...1000 (binary) -> TrailingZeros64 returns 3.
// The index of that bit is exactly 3.
return (i << 6) + bits.TrailingZeros64(w)
}
}
return -1
}
// Max return the max value where an Bit is set
// [1, 3, 100] => 100
// if no max found, return -1
func (b *BitSet[U]) Max() int {
bl := len(b.data)
bd := b.data
for i := bl - 1; i >= 0; i-- {
w := bd[i]
if w != 0 {
// bits.Len64 returns the minimum bits to represent w.
// Example: w = 0...0101 (binary) -> Len64 returns 3.
// The index of that bit is 3 - 1 = 2.
return (i << 6) + (bits.Len64(w) - 1)
}
}
return -1
}
// MaxSetIndex return the max index where an Bit is set
func (b *BitSet[U]) MaxSetIndex() int {
bl := len(b.data)
bd := b.data
for i := bl - 1; i >= 0; i-- {
if bd[i] != 0 {
return i
}
}
return -1
}
// Count returns how many bits are set in the BitSet.
// Uses a cached value when available (O(1)), recounts only after bulk operations.
func (b *BitSet[U]) Count() int {
if b.count >= 0 {
return b.count
}
n := 0
for _, w := range b.data {
n += bits.OnesCount64(w)
}
b.count = n
return n
}
// IsEmpty there are no bits set
func (b *BitSet[U]) IsEmpty() bool {
if b.count == 0 {
return true
}
if b.count > 0 {
return false
}
// count is dirty, check words directly
for _, w := range b.data {
if w != 0 {
return false
}
}
return true
}
// Len returns the len of the bit slice
func (b *BitSet[U]) Len() int { return len(b.data) }
// how many bytes is using
func (b *BitSet[U]) usedBytes() int { return 24 + (len(b.data) * 8) }
// Clear removes all bits
func (b *BitSet[U]) Clear() {
b.data = b.data[:0]
b.count = 0
}
// Copy copy the complete BitSet.
func (b *BitSet[U]) Copy() *BitSet[U] {
target := make([]uint64, len(b.data))
copy(target, b.data)
return &BitSet[U]{data: target, count: b.count}
}
// CopyInto copies the current BitSet into the provided buffer.
// It returns a new BitSet wrapper sharing the provided buffer.
// Assumption: cap(buf) >= len(b.data), if not, then panic.
func (b *BitSet[U]) CopyInto(buf []uint64) *BitSet[U] {
needed := len(b.data)
if cap(buf) < needed {
panic("BitSet.CopyInto: buffer too small")
}
target := buf[:needed]
copy(target, b.data)
return &BitSet[U]{data: target, count: b.count}
}
// And is the logical AND of two BitSet
// In this BitSet is the result, this means the values will be overwritten!
func (b *BitSet[U]) And(other *BitSet[U]) {
l := min(len(b.data), len(other.data))
// zero out the tail to prevent "Zombie Bits"
clear(b.data[l:])
b.data = b.data[:l]
// BCE: Bounds Check Elimination
a := b.data
o := other.data[:l]
for i := range l {
a[i] &= o[i]
}
b.count = -1 // invalidate cached count
}
// Or is the logical OR of two BitSet
func (b *BitSet[U]) Or(other *BitSet[U]) {
od := other.data
ol := len(od)
bl := len(b.data)
if ol == 0 {
return
}
if bl == 0 {
b.data = other.data
return
}
overlap := min(bl, ol)
// Ensure b.data has enough length for the result
if bl < ol {
if cap(b.data) >= ol {
b.data = b.data[:ol]
} else {
b.grow(ol - 1)
}
// Copy non-overlapping tail: 0 | x = x
copy(b.data[overlap:ol], od[overlap:ol])
}
// OR the overlapping words
dst := b.data[:overlap]
src := od[:overlap]
for i := range overlap {
dst[i] |= src[i]
}
b.count = -1 // invalidate cached count
}
func (b *BitSet[U]) flipTheBit(val U) {
word, bit := val/64, val%64
// If the bit is 0, (1<<bit) sets it.
// If the bit is 1, (1<<bit) clears it.
b.data[word] ^= (1 << bit)
}
// XOr is the logical XOR of two BitSet
func (b *BitSet[U]) Xor(other *BitSet[U]) {
bl := len(b.data)
ol := len(other.data)
overlap := min(bl, ol)
if overlap == 0 {
return
}
// If 'other' is longer, we simply append its tail to 'b'.
// Why? Because: 0 (current tail of b) XOR Value (tail of other) = Value.
if ol > bl {
b.data = append(b.data, other.data[bl:]...)
}
// if 'b' is longer, its tail remains untouched.
// value (tail of b) XOR 0 (implicit tail of other) = Value.
bd := b.data
od := other.data
_ = bd[overlap-1]
_ = od[overlap-1]
for i := range overlap {
bd[i] ^= od[i]
}
b.count = -1 // invalidate cached count
}
// AndNot removes all elements from the current set that exist in another set.
// Known as "Bit Clear" or "Set Difference"
//
// Example: [1, 2, 110, 2345] AndNot [2, 110] => [1, 2345]
func (b *BitSet[U]) AndNot(other *BitSet[U]) {
if len(other.data) == 0 || len(b.data) == 0 {
return
}
bd := b.data
od := other.data
l := min(len(bd), len(od))
// eliminates checks inside the loop.
_ = bd[l-1]
_ = od[l-1]
for i := range l {
bd[i] &^= od[i]
}
b.count = -1 // invalidate cached count
}
// Shrink trims the bitset to ensure that len(b.data) always points to the last truly useful word.
//
// Operation Can Grow? Can Shrink?
// OR Yes No
// XOR Yes Yes
// AND No Yes
// AND NOT No Yes
func (b *BitSet[U]) Shrink() {
bd := b.data
// start from the end
i := len(bd) - 1
for i >= 0 && bd[i] == 0 {
i--
}
b.data = bd[:i+1]
if i < 0 {
b.count = 0
}
}
// Values iterate over the complete BitSet and call the yield function, for every value
func (b *BitSet[U]) Values(yield func(U) bool) {
for i, w := range b.data {
for w != 0 {
t := bits.TrailingZeros64(w)
val := (i << 6) + t
if !yield(U(val)) {
return
}
w &= (w - 1)
}
}
}
func (b *BitSet[U]) ValuesBatch(yield func([]U) bool) {
const batchSize = 256
buffer := make([]U, batchSize)
pos := 0
for i, w := range b.data {
base := i << 6
for w != 0 {
t := bits.TrailingZeros64(w)
buffer[pos] = U(base + t)
pos++
// If buffer is full, yield the whole batch
if pos == batchSize {
if !yield(buffer) {
return
}
pos = 0
}
w &= (w - 1)
}
}
// Yield the final partial batch
if pos > 0 {
yield(buffer[:pos])
}
}
// ToSlice create a new slice which contains all saved values
func (b *BitSet[U]) ToSlice() []U {
res := make([]U, 0, b.Count())
b.Values(func(v U) bool {
res = append(res, v)
return true
})
return res
}