-
Notifications
You must be signed in to change notification settings - Fork 1
/
container.go
777 lines (681 loc) · 17.5 KB
/
container.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
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
/*
* Copyright 2021 Dgraph Labs, Inc. and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sroar
import (
"encoding/hex"
"fmt"
"math"
"math/bits"
"os"
"strings"
)
// container uses extra 4 []uint16 in the front as header.
// container[0] is used for storing the size of the container, expressed in Uint16.
// The container size cannot exceed the vicinity of 8KB. At 8KB, we switch from packed arrays to
// bitmaps. We can fit the entire uint16 worth of bitmaps in 8KB (2^16 / 8 = 8
// KB).
const (
typeArray uint16 = 0x00
typeBitmap uint16 = 0x01
// Container header.
indexSize int = 0
indexType int = 1
indexCardinality int = 2
// Index 2 and 3 is used for cardinality. We need 2 uint16s to store cardinality because
// 2^16 will not fit in uint16.
startIdx uint16 = 4
minContainerSize = 64 // In Uint16.
// Bitmap container can contain 2^16 integers. Each integer would use one bit to represent.
// Given that our data is represented in []uint16s, that'd mean the size of container to store
// it would be divided by 16.
// 4 for header and 4096 for storing bitmap container. In Uint16.
maxContainerSize = 4 + (1<<16)/16
)
func dataAt(data []uint16, i int) uint16 { return data[int(startIdx)+i] }
func incrCardinality(data []uint16) {
cur := getCardinality(data)
if cur+1 > math.MaxUint16 {
data[indexCardinality+1] = 1
} else {
data[indexCardinality]++
}
}
var invalidCardinality int = math.MaxUint16 + 10
var maxCardinality int = math.MaxUint16 + 1
func getCardinality(data []uint16) int {
// This sum has to be done using two ints to avoid overflow.
return int(data[indexCardinality]) + int(data[indexCardinality+1])
}
func setCardinality(data []uint16, c int) {
if c > math.MaxUint16 {
data[indexCardinality] = math.MaxUint16
data[indexCardinality+1] = uint16(c - math.MaxUint16)
} else {
data[indexCardinality] = uint16(c)
data[indexCardinality+1] = 0
}
}
func zeroOutContainer(c []uint16) {
switch c[indexType] {
case typeArray:
array(c).zeroOut()
case typeBitmap:
bitmap(c).zeroOut()
}
}
func removeRangeContainer(c []uint16, lo, hi uint16) {
switch c[indexType] {
case typeArray:
array(c).removeRange(lo, hi)
case typeBitmap:
bitmap(c).removeRange(lo, hi)
}
}
func calculateAndSetCardinality(data []uint16) {
if data[indexType] != typeBitmap {
panic("Non-bitmap containers should always have cardinality set correctly")
}
b := bitmap(data)
card := b.cardinality()
setCardinality(b, card)
}
type array []uint16
// find returns the index of the first element >= x.
// The index is based on data portion of the container, ignoring startIdx.
// If the element > than all elements present, then N is returned where N = cardinality of the
// container.
func (c array) find(x uint16) int {
N := getCardinality(c)
for i := int(startIdx); i < int(startIdx)+N; i++ {
if len(c) <= int(i) {
panic(fmt.Sprintf("find: %d len(c) %d <= i %d\n", x, len(c), i))
}
if c[i] >= x {
return int(i - int(startIdx))
}
}
return N
}
func (c array) rank(x uint16) int {
N := getCardinality(c)
idx := c.find(x)
if idx == N {
return -1
}
return idx
}
func (c array) has(x uint16) bool {
N := getCardinality(c)
idx := c.find(x)
if idx == N {
return false
}
return c[int(startIdx)+idx] == x
}
func (c array) add(x uint16) bool {
idx := c.find(x)
N := getCardinality(c)
offset := int(startIdx) + idx
if int(idx) < N {
if c[offset] == x {
return false
}
// The entry at offset is the first entry, which is greater than x. Move it to the right.
copy(c[offset+1:], c[offset:])
}
if offset >= len(c) {
fmt.Printf("offset: %d len(c): %d idx: %d N: %d isFull: %v\n", offset, len(c), idx, N, c.isFull())
fmt.Printf("array:\n%s\n", hex.Dump(toByteSlice(c)))
for ei, ee := range c {
fmt.Printf("[%d] %d\n", ei, ee)
}
fmt.Printf("Trying to add x: %d\n", x)
os.Exit(1)
}
c[offset] = x
incrCardinality(c)
return true
}
func (c array) remove(x uint16) bool {
idx := c.find(x)
N := getCardinality(c)
offset := int(startIdx) + idx
if int(idx) < N {
if c[offset] != x {
return false
}
copy(c[offset:], c[offset+1:])
setCardinality(c, N-1)
return true
}
return false
}
func (c array) removeRange(lo, hi uint16) {
if hi < lo {
panic(fmt.Sprintf("args must satisfy lo <= hi, got lo: %d, hi: %d\n", lo, hi))
}
loIdx := c.find(lo)
hiIdx := c.find(hi)
st := int(startIdx)
loVal := c[st+loIdx]
N := getCardinality(c)
// remove range doesn't intersect with any element in the array.
if hi < loVal || loIdx == N {
return
}
if hiIdx == N {
if loIdx > 0 {
c = c[:int(startIdx)+loIdx-1]
} else {
c = c[:int(startIdx)]
}
setCardinality(c, loIdx)
return
}
if c[st+hiIdx] == hi {
hiIdx++
}
copy(c[st+loIdx:], c[st+hiIdx:])
setCardinality(c, N-hiIdx+loIdx)
}
func (c array) zeroOut() {
setCardinality(c, 0)
}
// TODO: Figure out how memory allocation would work in these situations. Perhaps use allocator here?
func (c array) andArray(other array) []uint16 {
min := min(getCardinality(c), getCardinality(other))
setc := c.all()
seto := other.all()
out := make([]uint16, int(startIdx)+min+1)
num := uint16(intersection2by2(setc, seto, out[startIdx:]))
// Truncate out to how many values were found.
out = out[:startIdx+num+1]
out[indexType] = typeArray
out[indexSize] = uint16(len(out))
setCardinality(out, int(num))
return out
}
// TODO: We can do this operation in-place on the src array.
func (c array) andNotArray(other array, buf []uint16) []uint16 {
max := getCardinality(c)
out := make([]uint16, int(startIdx)+max+1)
andRes := array(c.andArray(other)).all()
srcVals := array(c).all()
num := uint16(difference(srcVals, andRes, out[startIdx:]))
// Truncate out to how many values were found.
out = out[:startIdx+num+1]
out[indexType] = typeArray
out[indexSize] = uint16(len(out))
setCardinality(out, int(num))
return out
}
func (c array) orArray(other array, buf []uint16, runMode int) []uint16 {
// We ignore runInline for this call.
max := getCardinality(c) + getCardinality(other)
if max > 4096 {
// Use bitmap container.
out := bitmap(c.toBitmapContainer(buf))
// For now, just keep it as a bitmap. No need to change if the
// cardinality is smaller than 4096.
out.orArray(other, nil, runMode|runInline)
// Return out because out is pointing to buf. This would allow the
// receiver to copy out.
return out
}
// The output would be of typeArray.
out := buf[:int(startIdx)+max]
num := union2by2(c.all(), other.all(), out[startIdx:])
out[indexType] = typeArray
out[indexSize] = uint16(len(out))
setCardinality(out, num)
return out
}
var tmp = make([]uint16, 8192)
func (c array) andBitmap(other bitmap) []uint16 {
out := make([]uint16, int(startIdx)+getCardinality(c)+2) // some extra space.
out[indexType] = typeArray
pos := startIdx
for _, x := range c.all() {
out[pos] = x
pos += other.bitValue(x)
}
// Ensure we have at least one empty slot at the end.
res := out[:pos+1]
res[indexSize] = uint16(len(res))
setCardinality(res, int(pos-startIdx))
return res
}
// TODO: Write an optmized version of this function.
func (c array) andNotBitmap(other bitmap, buf []uint16) []uint16 {
assert(len(buf) == maxContainerSize)
res := array(buf)
Memclr(res)
res[indexSize] = 4
for _, e := range c.all() {
if !other.has(e) {
res.add(e)
}
}
return res
}
func (c array) isFull() bool {
N := getCardinality(c)
return int(startIdx)+N >= len(c)
}
func (c array) all() []uint16 {
N := getCardinality(c)
return c[startIdx : int(startIdx)+N]
}
func (c array) minimum() uint16 {
N := getCardinality(c)
if N == 0 {
return 0
}
return c[startIdx]
}
func (c array) maximum() uint16 {
N := getCardinality(c)
if N == 0 {
return 0
}
return c[int(startIdx)+N-1]
}
func (c array) toBitmapContainer(buf []uint16) []uint16 {
if len(buf) == 0 {
buf = make([]uint16, maxContainerSize)
} else {
assert(len(buf) == maxContainerSize)
assert(len(buf) == copy(buf, empty))
}
b := bitmap(buf)
b[indexSize] = maxContainerSize
b[indexType] = typeBitmap
setCardinality(b, getCardinality(c))
data := b[startIdx:]
for _, x := range c.all() {
idx := x >> 4
pos := x & 0xF
data[idx] |= bitmapMask[pos]
}
return b
}
func (c array) String() string {
var b strings.Builder
b.WriteString(fmt.Sprintf("Size: %d\n", c[0]))
for i, val := range c[startIdx:] {
b.WriteString(fmt.Sprintf("%d: %d\n", i, val))
}
return b.String()
}
type bitmap []uint16
var bitmapMask []uint16
func init() {
bitmapMask = make([]uint16, 16)
for i := 0; i < 16; i++ {
bitmapMask[i] = 1 << (15 - i)
}
}
func (b bitmap) add(x uint16) bool {
idx := x >> 4
pos := x & 0xF
if has := b[startIdx+idx] & bitmapMask[pos]; has > 0 {
return false
}
b[startIdx+idx] |= bitmapMask[pos]
incrCardinality(b)
return true
}
func (b bitmap) remove(x uint16) bool {
idx := x >> 4
pos := x & 0xF
c := getCardinality(b)
if has := b[startIdx+idx] & bitmapMask[pos]; has > 0 {
b[startIdx+idx] ^= bitmapMask[pos]
setCardinality(b, c-1)
return true
}
return false
}
func (b bitmap) removeRange(lo, hi uint16) {
loIdx := lo >> 4
loPos := lo & 0xF
hiIdx := hi >> 4
hiPos := hi & 0xF
N := getCardinality(b)
var removed int
for i := loIdx + 1; i < hiIdx; i++ {
removed += bits.OnesCount16(b[startIdx+i])
b[startIdx+i] = 0
}
if loIdx == hiIdx {
for p := loPos; p <= hiPos; p++ {
if b[startIdx+loIdx]&bitmapMask[p] > 0 {
removed++
}
b[startIdx+loIdx] &= ^bitmapMask[p]
}
setCardinality(b, N-removed)
return
}
for p := loPos; p < 1<<4; p++ {
if b[startIdx+loIdx]&bitmapMask[p] > 0 {
removed++
}
b[startIdx+loIdx] &= ^bitmapMask[p]
}
for p := uint16(0); p <= hiPos; p++ {
if b[startIdx+hiIdx]&bitmapMask[p] > 0 {
removed++
}
b[startIdx+hiIdx] &= ^bitmapMask[p]
}
setCardinality(b, N-removed)
}
func (b bitmap) has(x uint16) bool {
idx := x >> 4
pos := x & 0xF
has := b[startIdx+idx] & bitmapMask[pos]
return has > 0
}
func (b bitmap) rank(x uint16) int {
idx := x >> 4
pos := x & 0xF
if b[startIdx+idx]&bitmapMask[pos] == 0 {
return -1
}
var rank int
for i := 0; i < int(idx); i++ {
rank += bits.OnesCount16(b[int(startIdx)+i])
}
for p := uint16(0); p <= pos; p++ {
if b[startIdx+idx]&bitmapMask[p] > 0 {
rank++
}
}
return rank - 1
}
// TODO: This can perhaps be using SIMD instructions.
func (b bitmap) andBitmap(other bitmap) []uint16 {
out := make([]uint16, maxContainerSize)
out[indexSize] = maxContainerSize
out[indexType] = typeBitmap
var num int
for i := int(startIdx); i < len(b); i++ {
out[i] = b[i] & other[i]
num += bits.OnesCount16(out[i])
}
setCardinality(out, num)
return out
}
func (b bitmap) orBitmap(other bitmap, buf []uint16, runMode int) []uint16 {
if runMode&runInline > 0 {
buf = b
} else {
copy(buf, b) // Copy over first.
}
buf[indexSize] = maxContainerSize
buf[indexType] = typeBitmap
if num := getCardinality(b); num == maxCardinality {
// do nothing. bitmap is already full.
} else if runMode&runLazy > 0 || num == invalidCardinality {
data := buf[startIdx:]
for i, v := range other[startIdx:] {
data[i] |= v
}
setCardinality(buf, invalidCardinality)
} else {
var num int
data := buf[startIdx:]
for i, v := range other[startIdx:] {
data[i] |= v
// We are going to iterate over the entire container. So, we can
// just recount the cardinality, starting from num=0.
num += bits.OnesCount16(data[i])
}
setCardinality(buf, num)
}
if runMode&runInline > 0 {
return nil
}
return buf
}
func (b bitmap) andNotBitmap(other bitmap) []uint16 {
var num int
data := b[startIdx:]
for i, v := range other[startIdx:] {
data[i] = data[i] ^ (data[i] & v)
num += bits.OnesCount16(data[i])
}
setCardinality(b, num)
return b
}
func (b bitmap) andNotArray(other array) []uint16 {
for _, e := range other.all() {
b.remove(e)
}
return b
}
func (b bitmap) orArray(other array, buf []uint16, runMode int) []uint16 {
if runMode&runInline > 0 {
buf = b
} else {
copy(buf, b)
}
if num := getCardinality(b); num == maxCardinality {
// do nothing. This bitmap is already full.
} else if runMode&runLazy > 0 || num == invalidCardinality {
// Avoid calculating the cardinality to speed up operations.
for _, x := range other.all() {
idx := x / 16
pos := x % 16
buf[startIdx+idx] |= bitmapMask[pos]
}
setCardinality(buf, invalidCardinality)
} else {
num := getCardinality(buf)
for _, x := range other.all() {
idx := x / 16
pos := x % 16
val := &buf[4+idx]
before := bits.OnesCount16(*val)
*val |= bitmapMask[pos]
after := bits.OnesCount16(*val)
num += after - before
}
setCardinality(buf, num)
}
if runMode&runInline > 0 {
return nil
}
return buf
}
func (b bitmap) all() []uint16 {
var res []uint16
data := b[startIdx:]
for idx := uint16(0); idx < uint16(len(data)); idx++ {
x := data[idx]
// TODO: This could potentially be optimized.
for pos := uint16(0); pos < 16; pos++ {
if x&bitmapMask[pos] > 0 {
res = append(res, (idx<<4)|pos)
}
}
}
return res
}
//TODO: It can be optimized.
func (b bitmap) selectAt(idx int) uint16 {
data := b[startIdx:]
n := uint16(len(data))
for i := uint16(0); i < n; i++ {
x := data[i]
c := bits.OnesCount16(x)
if idx < c {
for pos := uint16(0); pos < 16; pos++ {
if idx == 0 && x&bitmapMask[pos] > 0 {
return i*16 + pos
}
if x&bitmapMask[pos] > 0 {
idx--
}
}
}
idx -= c
}
panic("should not reach here")
}
// bitValue returns a 0 or a 1 depending upon whether x is present in the bitmap, where 1 means
// present and 0 means absent.
func (b bitmap) bitValue(x uint16) uint16 {
idx := x >> 4
return (b[4+idx] >> (15 - (x & 0xF))) & 1
}
func (b bitmap) isFull() bool {
return false
}
func (b bitmap) minimum() uint16 {
N := getCardinality(b)
if N == 0 {
return 0
}
for i, x := range b[startIdx:] {
lz := bits.LeadingZeros16(x)
if lz == 16 {
continue
}
return uint16(16*i + lz)
}
panic("We shouldn't reach here")
}
func (b bitmap) maximum() uint16 {
N := getCardinality(b)
if N == 0 {
return 0
}
for i := len(b) - 1; i >= int(startIdx); i-- {
x := b[i]
tz := bits.TrailingZeros16(x)
if tz == 16 {
continue
}
return uint16(16*i + 15 - tz)
}
panic("We shouldn't reach here")
}
func (b bitmap) cardinality() int {
var num int
for _, x := range b[startIdx:] {
num += bits.OnesCount16(x)
}
return num
}
var zeroContainer = make([]uint16, maxContainerSize)
func (b bitmap) zeroOut() {
setCardinality(b, 0)
copy(b[startIdx:], zeroContainer[startIdx:])
}
var (
runInline = 0x01
runLazy = 0x02
)
func containerOr(ac, bc, buf []uint16, runMode int) []uint16 {
at := ac[indexType]
bt := bc[indexType]
if at == typeArray && bt == typeArray {
left := array(ac)
right := array(bc)
// We can't always inline this function. If the right container has
// enough entries, trying to do a union with the left container inplace
// could end up overwriting the left container entries. So, we use a
// buffer to hold all output, and then copy it over to left.
//
// TODO: If right doesn't have a lot of entries, we could just iterate
// over left and merge the entries from right inplace. Would be faster
// than copying over all entries into buffer. Worth trying that approach.
return left.orArray(right, buf, runMode)
}
if at == typeArray && bt == typeBitmap {
left := array(ac)
right := bitmap(bc)
// Don't run inline for this call.
return right.orArray(left, buf, runMode&^runInline)
}
// These two following cases can be fully inlined.
if at == typeBitmap && bt == typeArray {
left := bitmap(ac)
right := array(bc)
return left.orArray(right, buf, runMode)
}
if at == typeBitmap && bt == typeBitmap {
left := bitmap(ac)
right := bitmap(bc)
return left.orBitmap(right, buf, runMode)
}
panic("containerAnd: We should not reach here")
}
func containerAnd(ac, bc []uint16) []uint16 {
at := ac[indexType]
bt := bc[indexType]
if at == typeArray && bt == typeArray {
left := array(ac)
right := array(bc)
return left.andArray(right)
}
if at == typeArray && bt == typeBitmap {
left := array(ac)
right := bitmap(bc)
return left.andBitmap(right)
}
if at == typeBitmap && bt == typeArray {
left := bitmap(ac)
right := array(bc)
out := right.andBitmap(left)
return out
}
if at == typeBitmap && bt == typeBitmap {
left := bitmap(ac)
right := bitmap(bc)
return left.andBitmap(right)
}
panic("containerAnd: We should not reach here")
}
// TODO: Optimize this function.
func containerAndNot(ac, bc, buf []uint16) []uint16 {
at := ac[indexType]
bt := bc[indexType]
if at == typeArray && bt == typeArray {
left := array(ac)
right := array(bc)
return left.andNotArray(right, buf)
}
if at == typeArray && bt == typeBitmap {
left := array(ac)
right := bitmap(bc)
return left.andNotBitmap(right, buf)
}
if at == typeBitmap && bt == typeArray {
left := bitmap(ac)
right := array(bc)
out := left.andNotArray(right)
return out
}
if at == typeBitmap && bt == typeBitmap {
left := bitmap(ac)
right := bitmap(bc)
return left.andNotBitmap(right)
}
panic("containerAndNot: We should not reach here")
}