-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathnset.go
More file actions
487 lines (355 loc) · 12.3 KB
/
nset.go
File metadata and controls
487 lines (355 loc) · 12.3 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
package nset
import (
"bytes"
"fmt"
"math/bits"
"reflect"
"strings"
"unsafe"
)
var _ fmt.Stringer = &NSet[uint8]{}
type BucketType uint8
type StorageType uint64
const (
BucketCount = 128
// StorageTypeBits is the number of bits used per storage unit in each bucket.
//
// NOTE: this must be a power of 2, otherwise FastModPower2 will break and must be replaced by a normal x%y
// NOTE: GetStorageUnitIndex must be adjusted if this value is changed
StorageTypeBits = 64
BucketIndexingBits = 7
)
// IntsIf is limited to uint32 because we can store ALL 4 Billion uint32 numbers
// in 512MB with NSet (instead of the normal 16GB for an array of all uint32s).
// But if we allow uint64 (or int, since int can be 64-bit) users can easily put a big 64-bit number and use more RAM than maybe Google and crash.
type IntsIf interface {
uint8 | uint16 | uint32
}
type Bucket struct {
Data []StorageType
StorageUnitCount uint32
}
type NSet[T IntsIf] struct {
Buckets [BucketCount]Bucket
//StorageUnitCount the number of uint64 integers that are used to indicate presence of numbers in the set
StorageUnitCount uint32
shiftAmount T
SetBits uint64
}
func (n *NSet[T]) Add(x T) {
bucket := n.GetBucketFromValue(x)
unitIndex := n.GetStorageUnitIndex(x)
if unitIndex >= bucket.StorageUnitCount {
storageUnitsToAdd := unitIndex - bucket.StorageUnitCount + 1
bucket.Data = append(bucket.Data, make([]StorageType, storageUnitsToAdd)...)
n.StorageUnitCount += storageUnitsToAdd
bucket.StorageUnitCount += storageUnitsToAdd
}
oldStorage := bucket.Data[unitIndex]
newStorage := oldStorage | n.GetBitMask(x)
bucket.Data[unitIndex] = newStorage
n.SetBits += uint64(bits.OnesCount64(uint64(^oldStorage) & uint64(newStorage)))
}
func (n *NSet[T]) AddMany(values ...T) {
for i := 0; i < len(values); i++ {
x := values[i]
bucket := n.GetBucketFromValue(x)
unitIndex := n.GetStorageUnitIndex(x)
if unitIndex >= bucket.StorageUnitCount {
storageUnitsToAdd := unitIndex - bucket.StorageUnitCount + 1
bucket.Data = append(bucket.Data, make([]StorageType, storageUnitsToAdd)...)
n.StorageUnitCount += storageUnitsToAdd
bucket.StorageUnitCount += storageUnitsToAdd
}
oldStorage := bucket.Data[unitIndex]
newStorage := oldStorage | n.GetBitMask(x)
bucket.Data[unitIndex] = newStorage
n.SetBits += uint64(bits.OnesCount64(uint64(^oldStorage) & uint64(newStorage)))
}
}
func (n *NSet[T]) Remove(x T) {
b := n.GetBucketFromValue(x)
unitIndex := n.GetStorageUnitIndex(x)
if unitIndex >= b.StorageUnitCount {
return
}
oldStorage := b.Data[unitIndex]
newStorage := oldStorage &^ n.GetBitMask(x)
b.Data[unitIndex] = newStorage
n.SetBits -= uint64(bits.OnesCount64(uint64(oldStorage) & uint64(^newStorage)))
}
func (n *NSet[T]) Contains(x T) bool {
return n.isSet(x)
}
func (n *NSet[T]) ContainsAny(values ...T) bool {
for _, x := range values {
if n.isSet(x) {
return true
}
}
return false
}
func (n *NSet[T]) ContainsAll(values ...T) bool {
for _, x := range values {
if !n.isSet(x) {
return false
}
}
return true
}
func (n *NSet[T]) isSet(x T) bool {
b := n.GetBucketFromValue(x)
unitIndex := n.GetStorageUnitIndex(x)
return unitIndex < b.StorageUnitCount && b.Data[unitIndex]&n.GetBitMask(x) != 0
}
func (n *NSet[T]) GetBucketFromValue(x T) *Bucket {
return &n.Buckets[n.GetBucketIndex(x)]
}
func (n *NSet[T]) GetBucketIndex(x T) BucketType {
//Use the top 'n' bits as the index to the bucket
return BucketType(x >> n.shiftAmount)
}
func (n *NSet[T]) GetStorageUnitIndex(x T) uint32 {
//The top 'n' bits are used to select the bucket so we need to remove them before finding storage
//unit and bit mask. This is done by shifting left by 4 which removes the top 'n' bits,
//then shifting right by 4 which puts the bits back to their original place, but now
//the top 'n' bits are zeros.
// Since StorageTypeBits is known and is a power of 2, we can replace the division
// with a right shift.
//
// The below return is equal to: return uint32(((x << BucketIndexingBits) >> BucketIndexingBits) / StorageTypeBits)
return uint32(((x << BucketIndexingBits) >> BucketIndexingBits) >> 6)
}
func (n *NSet[T]) GetBitMask(x T) StorageType {
//Removes top 'n' bits
return 1 << FastModPower2(((x<<BucketIndexingBits)>>BucketIndexingBits), StorageTypeBits)
}
// Union does n1=Union(n1, n2), so the current set will be updated
// such that its a union of its old value and the passed set
func (n *NSet[T]) Union(otherSet *NSet[T]) {
for i := 0; i < BucketCount; i++ {
b1 := &n.Buckets[i]
b2 := &otherSet.Buckets[i]
if b1.StorageUnitCount < b2.StorageUnitCount {
storageUnitsToAdd := b2.StorageUnitCount - b1.StorageUnitCount
b1.Data = append(b1.Data, make([]StorageType, storageUnitsToAdd)...)
b1.StorageUnitCount += storageUnitsToAdd
n.StorageUnitCount += storageUnitsToAdd
}
for j := 0; j < len(b1.Data) && j < len(b2.Data); j++ {
oldStorage := b1.Data[j]
newStorage := oldStorage | b2.Data[j]
b1.Data[j] = newStorage
n.SetBits += uint64(bits.OnesCount64(uint64(^oldStorage) & uint64(newStorage)))
}
}
}
// GetIntersection returns a new set that's the intersection between
// this set and the passed set
func (n *NSet[T]) GetIntersection(otherSet *NSet[T]) *NSet[T] {
outSet := NewNSet[T]()
for i := 0; i < BucketCount; i++ {
b1 := &n.Buckets[i]
b2 := &otherSet.Buckets[i]
newB := &outSet.Buckets[i]
for j := uint32(0); j < b1.StorageUnitCount && j < b2.StorageUnitCount; j++ {
if b1.Data[j]&b2.Data[j] == 0 {
continue
}
if newB.StorageUnitCount < j+1 {
storageUnitsToAdd := j + 1 - newB.StorageUnitCount
newB.Data = append(newB.Data, make([]StorageType, storageUnitsToAdd)...)
newB.StorageUnitCount += storageUnitsToAdd
outSet.StorageUnitCount += storageUnitsToAdd
}
newStorage := b1.Data[j] & b2.Data[j]
newB.Data[j] = newStorage
outSet.SetBits += uint64(bits.OnesCount64(uint64(newStorage)))
}
}
return outSet
}
// GetDifference returns a new set that contains the elements in this set
// that are not in the passed set.
//
// For example, if s1=(1,2,3,4,5) and s2=(1,3,4), the output is
// s3=Diff(s1,s2)=(2,5)
func (n *NSet[T]) GetDifference(otherSet *NSet[T]) *NSet[T] {
outSet := NewNSet[T]()
for i := 0; i < BucketCount; i++ {
b1 := &n.Buckets[i]
b2 := &otherSet.Buckets[i]
outSet.StorageUnitCount += b1.StorageUnitCount
newB := &outSet.Buckets[i]
newB.StorageUnitCount = b1.StorageUnitCount
newB.Data = make([]StorageType, newB.StorageUnitCount)
for j := uint32(0); j < b1.StorageUnitCount && j < b2.StorageUnitCount; j++ {
newStorage := b1.Data[j] & (^b2.Data[j])
newB.Data[j] = newStorage
outSet.SetBits += uint64(bits.OnesCount64(uint64(newStorage)))
}
if b1.StorageUnitCount > b2.StorageUnitCount {
copy(newB.Data[b2.StorageUnitCount:], b1.Data[b2.StorageUnitCount:])
for j := uint32(b2.StorageUnitCount); j < newB.StorageUnitCount; j++ {
storage := newB.Data[j]
outSet.SetBits += uint64(bits.OnesCount64(uint64(storage)))
}
}
}
return outSet
}
// GetAllElements returns all the added numbers added to NSet.
//
// NOTE: Be careful with this if you have a lot of elements in NSet because NSet is compressed while the returned array is not.
// In the worst case (all uint32s stored) the returned array will be ~4.2 billion elements and will use 16+ GBs of RAM.
func (n *NSet[T]) GetAllElements() []T {
elements := make([]T, 0, n.SetBits)
if n.SetBits == 0 {
return elements
}
for i := 0; i < BucketCount; i++ {
//bucketIndexBits are the bits removed from the original value to use for bucket indexing.
//We will use this to restore the original value 'x' once an intersection is detected
bucketIndexBits := T(i << n.shiftAmount)
b1 := &n.Buckets[i]
for j := 0; j < len(b1.Data); j++ {
storageUnit := b1.Data[j]
if storageUnit == 0 {
continue
}
onesCount := bits.OnesCount64(uint64(storageUnit))
mask := StorageType(1 << 0) //This will be used to check set bits. Numbers will be reconstructed only for set bits
firstStorageUnitValue := T(j*StorageTypeBits) | bucketIndexBits //StorageUnitIndex = noBucketBitsX / StorageTypeBits. So: noBucketBitsX = StorageUnitIndex * StorageTypeBits; Then: x = noBucketBitsX | bucketIndexBits
for k := T(0); onesCount > 0 && k < StorageTypeBits; k++ {
if storageUnit&mask > 0 {
elements = append(elements, firstStorageUnitValue+k)
onesCount--
}
mask <<= 1
}
}
}
return elements
}
func (n *NSet[T]) IsEq(otherSet *NSet[T]) bool {
if n.SetBits != otherSet.SetBits {
return false
}
//Equal storage unit count doesn't mean all buckets have same size, so we check per bucket
for i := 0; i < len(n.Buckets); i++ {
if n.Buckets[i].StorageUnitCount != otherSet.Buckets[i].StorageUnitCount {
return false
}
}
for i := 0; i < len(n.Buckets); i++ {
b1 := &n.Buckets[i]
b2 := &otherSet.Buckets[i]
// The .Data[0] will panic if either unit count is zero, so these checks
// both avoid that panic and provide an early exit
bucketsEqual := (b1.StorageUnitCount == 0 && b2.StorageUnitCount == 0) ||
(b1.StorageUnitCount == b2.StorageUnitCount && bytes.Equal(
unsafe.Slice((*byte)(unsafe.Pointer(&b1.Data[0])), len(b1.Data)*int(unsafe.Sizeof(b1.Data[0]))),
unsafe.Slice((*byte)(unsafe.Pointer(&b2.Data[0])), len(b2.Data)*int(unsafe.Sizeof(b2.Data[0]))),
))
if !bucketsEqual {
return false
}
}
return true
}
func (n *NSet[T]) HasIntersection(otherSet *NSet[T]) bool {
for i := 0; i < len(n.Buckets); i++ {
b1 := &n.Buckets[i]
b2 := &otherSet.Buckets[i]
for j := 0; j < len(b1.Data) && j < len(b2.Data); j++ {
if b1.Data[j]&b2.Data[j] > 0 {
return true
}
}
}
return false
}
// String returns a string of the storage as bytes separated by spaces. A comma is between each storage unit
func (n *NSet[T]) String() string {
b := strings.Builder{}
b.Grow(int(n.StorageUnitCount*StorageTypeBits + n.StorageUnitCount*2))
for i := 0; i < len(n.Buckets); i++ {
bucket := &n.Buckets[i]
for j := 0; j < len(bucket.Data); j++ {
x := bucket.Data[j]
shiftAmount := StorageTypeBits - 8
for shiftAmount >= 0 {
byteToShow := uint8(x >> shiftAmount)
if shiftAmount > 0 {
b.WriteString(fmt.Sprintf("%08b ", byteToShow))
} else {
b.WriteString(fmt.Sprintf("%08b", byteToShow))
}
shiftAmount -= 8
}
b.WriteString(", ")
}
}
return b.String()
}
func (n *NSet[T]) Copy() *NSet[T] {
newSet := NewNSet[T]()
for i := 0; i < len(n.Buckets); i++ {
b := &n.Buckets[i]
newB := &newSet.Buckets[i]
newB.StorageUnitCount = b.StorageUnitCount
newB.Data = make([]StorageType, len(b.Data))
copy(newB.Data, b.Data)
}
newSet.StorageUnitCount = n.StorageUnitCount
return newSet
}
// Len returns the number of values stored (i.e. bits set to 1).
// It is the same as NSet.SetBits.
func (n *NSet[T]) Len() uint64 {
return n.SetBits
}
func UnionSets[T IntsIf](set1, set2 *NSet[T]) *NSet[T] {
newSet := NewNSet[T]()
// This is an optimization that makes it so that we only need to count bits
// when doing union with set2
newSet.SetBits = set1.SetBits
for i := 0; i < BucketCount; i++ {
b1 := &set1.Buckets[i]
b2 := &set2.Buckets[i]
//Size bucket
bucketSize := b1.StorageUnitCount
if b2.StorageUnitCount > bucketSize {
bucketSize = b2.StorageUnitCount
}
newB := &newSet.Buckets[i]
newB.Data = make([]StorageType, bucketSize)
newB.StorageUnitCount = bucketSize
newSet.StorageUnitCount += bucketSize
//Union fields of both sets on the new set
copy(newB.Data, b1.Data)
for j := 0; j < len(b2.Data); j++ {
oldStorage := newB.Data[j]
newStorage := oldStorage | b2.Data[j]
newB.Data[j] = newStorage
newSet.SetBits += uint64(bits.OnesCount64(uint64(^oldStorage) & uint64(newStorage)))
}
}
return newSet
}
// FastModPower2 is a fast version of x%y that only works when y is a power of 2
func FastModPower2[T uint8 | uint16 | uint32 | uint64](x, y T) T {
return x & (y - 1)
}
func NewNSet[T IntsIf]() *NSet[T] {
n := &NSet[T]{
Buckets: [BucketCount]Bucket{},
StorageUnitCount: 0,
//We use this to either extract or clear the top 'n' bits, as they are used to select the bucket
shiftAmount: T(reflect.TypeOf(*new(T)).Bits()) - BucketIndexingBits,
}
for i := 0; i < len(n.Buckets); i++ {
n.Buckets[i].Data = make([]StorageType, 0)
}
return n
}