forked from RoaringBitmap/roaring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
roaring.go
996 lines (911 loc) · 28.3 KB
/
roaring.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
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
// Package roaring is an implementation of Roaring Bitmaps in Go.
// They provide fast compressed bitmap data structures (also called bitset).
// They are ideally suited to represent sets of integers over
// relatively small ranges.
// See http://roaringbitmap.org for details.
package roaring
import (
"bytes"
"encoding/base64"
"fmt"
"io"
"strconv"
)
// RoaringBitmap represents a compressed bitmap where you can add integers.
type RoaringBitmap struct {
highlowcontainer roaringArray
}
func (b *RoaringBitmap) ToBase64() (string, error) {
buf := new(bytes.Buffer)
_, err := b.WriteTo(buf)
return base64.StdEncoding.EncodeToString(buf.Bytes()), err
}
func (b *RoaringBitmap) FromBase64(str string) (int, error) {
data, err := base64.StdEncoding.DecodeString(str)
if err != nil {
return 0, err
}
buf := bytes.NewBuffer(data)
return b.ReadFrom(buf)
}
// Write out a serialized version of this bitmap to stream
func (b *RoaringBitmap) WriteTo(stream io.Writer) (int, error) {
return b.highlowcontainer.writeTo(stream)
}
// Read a serialized version of this bitmap from stream
func (b *RoaringBitmap) ReadFrom(stream io.Reader) (int, error) {
return b.highlowcontainer.readFrom(stream)
}
// NewRoaringBitmap creates a new empty RoaringBitmap
func NewRoaringBitmap() *RoaringBitmap {
return &RoaringBitmap{*newRoaringArray()}
}
// Clear removes all content from the RoaringBitmap and frees the memory
func (rb *RoaringBitmap) Clear() {
rb.highlowcontainer = *newRoaringArray()
}
// ToArray creates a new slice containing all of the integers stored in the RoaringBitmap in sorted order
func (rb *RoaringBitmap) ToArray() []uint32 {
array := make([]uint32, rb.GetCardinality())
pos := 0
pos2 := 0
for pos < rb.highlowcontainer.size() {
hs := toIntUnsigned(rb.highlowcontainer.getKeyAtIndex(pos)) << 16
c := rb.highlowcontainer.getContainerAtIndex(pos)
pos++
c.fillLeastSignificant16bits(array, pos2, hs)
pos2 += c.getCardinality()
}
return array
}
// GetSizeInBytes estimates the memory usage of the RoaringBitmap. Note that this
// might differ slightly from the amount of bytes required for persistent storage
func (rb *RoaringBitmap) GetSizeInBytes() uint64 {
size := uint64(8)
for _, c := range rb.highlowcontainer.containers {
size += uint64(2) + uint64(c.getSizeInBytes())
}
return size
}
// GetSerializedSizeInBytes computes the serialized size in bytes the RoaringBitmap. It should correspond to the
// number of bytes written when invoking WriteTo
func (rb *RoaringBitmap) GetSerializedSizeInBytes() uint64 {
return rb.highlowcontainer.serializedSizeInBytes()
}
// IntIterable allows you to iterate over the values in a RoaringBitmap
type IntIterable interface {
HasNext() bool
Next() uint32
}
type intIterator struct {
pos int
hs uint32
iter shortIterable
highlowcontainer *roaringArray
}
// HasNext returns true if there are more integers to iterate over
func (ii *intIterator) HasNext() bool {
return ii.pos < ii.highlowcontainer.size()
}
func (ii *intIterator) init() {
if ii.highlowcontainer.size() > ii.pos {
ii.iter = ii.highlowcontainer.getContainerAtIndex(ii.pos).getShortIterator()
ii.hs = toIntUnsigned(ii.highlowcontainer.getKeyAtIndex(ii.pos)) << 16
}
}
// Next returns the next integer
func (ii *intIterator) Next() uint32 {
x := toIntUnsigned(ii.iter.next()) | ii.hs
if !ii.iter.hasNext() {
ii.pos = ii.pos + 1
ii.init()
}
return x
}
func newIntIterator(a *RoaringBitmap) *intIterator {
p := new(intIterator)
p.pos = 0
p.highlowcontainer = &a.highlowcontainer
p.init()
return p
}
// String creates a string representation of the RoaringBitmap
func (rb *RoaringBitmap) String() string {
// inspired by https://github.com/fzandona/goroar/blob/master/roaringbitmap.go
var buffer bytes.Buffer
start := []byte("{")
buffer.Write(start)
i := rb.Iterator()
for i.HasNext() {
buffer.WriteString(strconv.Itoa(int(i.Next())))
if i.HasNext() { // todo: optimize
buffer.WriteString(",")
}
}
buffer.WriteString("}")
return buffer.String()
}
// Iterator creates a new IntIterable to iterate over the integers contained in the bitmap, in sorted order
func (rb *RoaringBitmap) Iterator() IntIterable {
return newIntIterator(rb)
}
// Clone creates a copy of the RoaringBitmap
func (rb *RoaringBitmap) Clone() *RoaringBitmap {
ptr := new(RoaringBitmap)
ptr.highlowcontainer = *rb.highlowcontainer.clone()
return ptr
}
// Contains returns true if the integer is contained in the bitmap
func (rb *RoaringBitmap) Contains(x uint32) bool {
hb := highbits(x)
c := rb.highlowcontainer.getContainer(hb)
return c != nil && c.contains(lowbits(x))
}
// Contains returns true if the integer is contained in the bitmap (this is a convenience method, the parameter is casted to uint32 and Contains is called)
func (rb *RoaringBitmap) ContainsInt(x int) bool {
return rb.Contains(uint32(x))
}
// Equals returns true if the two bitmaps contain the same integers
func (rb *RoaringBitmap) Equals(o interface{}) bool {
srb, ok := o.(*RoaringBitmap)
if ok {
return srb.highlowcontainer.equals(rb.highlowcontainer)
}
return false
}
// Add the integer x to the bitmap
func (rb *RoaringBitmap) Add(x uint32) {
hb := highbits(x)
ra := &rb.highlowcontainer
i := ra.getIndex(hb)
if i >= 0 {
var c container
if ra.hasDirty() {
c = ra.getWritableContainerAtIndex(i)
} else {
c = ra.getContainerAtIndex(i)
}
c = c.add(lowbits(x))
rb.highlowcontainer.setContainerAtIndex(i, c)
} else {
newac := newArrayContainer()
rb.highlowcontainer.insertNewKeyValueAt(-i-1, hb, newac.add(lowbits(x)))
}
}
// Add the integer x to the bitmap and return true if it was added (false if the integer was already present)
func (rb *RoaringBitmap) CheckedAdd(x uint32) bool {
// TODO: add unit tests for this method
hb := highbits(x)
i := rb.highlowcontainer.getIndex(hb)
if i >= 0 {
C := rb.highlowcontainer.getContainerAtIndex(i)
oldcard := C.getCardinality()
C = C.add(lowbits(x))
rb.highlowcontainer.setContainerAtIndex(i, C)
return C.getCardinality() > oldcard
} else {
newac := newArrayContainer()
rb.highlowcontainer.insertNewKeyValueAt(-i-1, hb, newac.add(lowbits(x)))
return true
}
}
// Add the integer x to the bitmap (convenience method: the parameter is casted to uint32 and we call Add)
func (rb *RoaringBitmap) AddInt(x int) {
rb.Add(uint32(x))
}
// Remove the integer x from the bitmap
func (rb *RoaringBitmap) Remove(x uint32) {
hb := highbits(x)
i := rb.highlowcontainer.getIndex(hb)
if i >= 0 {
c := rb.highlowcontainer.getWritableContainerAtIndex(i).remove(lowbits(x))
rb.highlowcontainer.setContainerAtIndex(i, c.remove(lowbits(x)))
if rb.highlowcontainer.getContainerAtIndex(i).getCardinality() == 0 {
rb.highlowcontainer.removeAtIndex(i)
}
}
}
// Remove the integer x from the bitmap and return true if the integer was effectively remove (and false if the integer was not present)
func (rb *RoaringBitmap) CheckedRemove(x uint32) bool {
// TODO: add unit tests for this method
hb := highbits(x)
i := rb.highlowcontainer.getIndex(hb)
if i >= 0 {
C := rb.highlowcontainer.getContainerAtIndex(i)
oldcard := C.getCardinality()
C = C.remove(lowbits(x))
rb.highlowcontainer.setContainerAtIndex(i, C)
if rb.highlowcontainer.getContainerAtIndex(i).getCardinality() == 0 {
rb.highlowcontainer.removeAtIndex(i)
return true
}
return C.getCardinality() < oldcard
} else {
return false
}
}
// IsEmpty returns true if the RoaringBitmap is empty (it is faster than doing (GetCardinality() == 0))
func (rb *RoaringBitmap) IsEmpty() bool {
return rb.highlowcontainer.size() == 0
}
// GetCardinality returns the number of integers contained in the bitmap
func (rb *RoaringBitmap) GetCardinality() uint64 {
size := uint64(0)
for _, c := range rb.highlowcontainer.containers {
size += uint64(c.getCardinality())
}
return size
}
// Rank returns the number of integers that are smaller or equal to x (Rank(infinity) would be GetCardinality())
func (rb *RoaringBitmap) Rank(x uint32) uint32 {
size := uint32(0)
for i := 0; i < rb.highlowcontainer.size(); i++ {
key := rb.highlowcontainer.getKeyAtIndex(i)
if key > highbits(x) {
return size
}
if key < highbits(x) {
size += uint32(rb.highlowcontainer.getContainerAtIndex(i).getCardinality())
} else {
return size + uint32(rb.highlowcontainer.getContainerAtIndex(i).rank(lowbits(x)))
}
}
return size
}
// Select returns the xth integer in the bitmap
func (rb *RoaringBitmap) Select(x uint32) (uint32, error) {
if rb.GetCardinality() <= uint64(x) {
return 0, fmt.Errorf("Can't find %dth integer in a bitmap with only %d items", x, rb.GetCardinality())
}
remaining := x
for i := 0; i < rb.highlowcontainer.size(); i++ {
c := rb.highlowcontainer.getContainerAtIndex(i)
if remaining >= uint32(c.getCardinality()) {
remaining -= uint32(c.getCardinality())
} else {
key := rb.highlowcontainer.getKeyAtIndex(i)
return uint32(key)<<16 + uint32(c.selectInt(uint16(remaining))), nil
}
}
return 0, fmt.Errorf("Can't find %dth integer in a bitmap with only %d items", x, rb.GetCardinality())
}
// And computes the intersection between two bitmaps and stores the result in the current bitmap
func (rb *RoaringBitmap) And(x2 *RoaringBitmap) {
pos1 := 0
pos2 := 0
intersectionsize := 0
length1 := rb.highlowcontainer.size()
length2 := x2.highlowcontainer.size()
main:
for {
if pos1 < length1 && pos2 < length2 {
s1 := rb.highlowcontainer.getKeyAtIndex(pos1)
s2 := x2.highlowcontainer.getKeyAtIndex(pos2)
for {
if s1 == s2 {
c1 := rb.highlowcontainer.getWritableContainerAtIndex(pos1)
c2 := x2.highlowcontainer.getContainerAtIndex(pos2)
diff := c1.iand(c2)
if diff.getCardinality() > 0 {
rb.highlowcontainer.replaceKeyAndContainerAtIndex(intersectionsize, s1, diff)
intersectionsize++
}
pos1++
pos2++
if (pos1 == length1) || (pos2 == length2) {
break main
}
s1 = rb.highlowcontainer.getKeyAtIndex(pos1)
s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
} else if s1 < s2 {
pos1 = rb.highlowcontainer.advanceUntil(s2, pos1)
if pos1 == length1 {
break main
}
s1 = rb.highlowcontainer.getKeyAtIndex(pos1)
} else { //s1 > s2
pos2 = x2.highlowcontainer.advanceUntil(s1, pos2)
if pos2 == length2 {
break main
}
s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
}
}
} else {
break
}
}
rb.highlowcontainer.resize(intersectionsize)
}
// OrCardinality returns the cardinality of the union between two bitmaps, bitmaps are not modified
func (x1 *RoaringBitmap) OrCardinality(x2 *RoaringBitmap) uint64 {
pos1 := 0
pos2 := 0
length1 := x1.highlowcontainer.size()
length2 := x2.highlowcontainer.size()
answer := uint64(0)
main:
for {
if (pos1 < length1) && (pos2 < length2) {
s1 := x1.highlowcontainer.getKeyAtIndex(pos1)
s2 := x2.highlowcontainer.getKeyAtIndex(pos2)
for {
if s1 < s2 {
answer += uint64(x1.highlowcontainer.getContainerAtIndex(pos1).getCardinality())
pos1++
if pos1 == length1 {
break main
}
s1 = x1.highlowcontainer.getKeyAtIndex(pos1)
} else if s1 > s2 {
answer += uint64(x2.highlowcontainer.getContainerAtIndex(pos2).getCardinality())
pos2++
if pos2 == length2 {
break main
}
s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
} else {
// TODO: could be faster if we did not have to materialize the container
answer += uint64(x1.highlowcontainer.getContainerAtIndex(pos1).or(x2.highlowcontainer.getContainerAtIndex(pos2)).getCardinality())
pos1++
pos2++
if (pos1 == length1) || (pos2 == length2) {
break main
}
s1 = x1.highlowcontainer.getKeyAtIndex(pos1)
s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
}
}
} else {
break
}
}
for ; pos1 < length1; pos1++ {
answer += uint64(x1.highlowcontainer.getContainerAtIndex(pos1).getCardinality())
}
for ; pos2 < length2; pos2++ {
answer += uint64(x2.highlowcontainer.getContainerAtIndex(pos2).getCardinality())
}
return answer
}
// AndCardinality returns the cardinality of the intersection between two bitmaps, bitmaps are not modified
func (rb *RoaringBitmap) AndCardinality(x2 *RoaringBitmap) uint64 {
pos1 := 0
pos2 := 0
answer := uint64(0)
length1 := rb.highlowcontainer.size()
length2 := x2.highlowcontainer.size()
main:
for {
if pos1 < length1 && pos2 < length2 {
s1 := rb.highlowcontainer.getKeyAtIndex(pos1)
s2 := x2.highlowcontainer.getKeyAtIndex(pos2)
for {
if s1 == s2 {
c1 := rb.highlowcontainer.getWritableContainerAtIndex(pos1)
c2 := x2.highlowcontainer.getContainerAtIndex(pos2)
diff := c1.and(c2)
answer += uint64(diff.getCardinality()) // TODO: could be faster if we did not have to compute diff
pos1++
pos2++
if (pos1 == length1) || (pos2 == length2) {
break main
}
s1 = rb.highlowcontainer.getKeyAtIndex(pos1)
s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
} else if s1 < s2 {
pos1 = rb.highlowcontainer.advanceUntil(s2, pos1)
if pos1 == length1 {
break main
}
s1 = rb.highlowcontainer.getKeyAtIndex(pos1)
} else { //s1 > s2
pos2 = x2.highlowcontainer.advanceUntil(s1, pos2)
if pos2 == length2 {
break main
}
s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
}
}
} else {
break
}
}
return answer
}
// Intersects checks whether two bitmap intersects, bitmaps are not modified
func (rb *RoaringBitmap) Intersects(x2 *RoaringBitmap) bool {
pos1 := 0
pos2 := 0
length1 := rb.highlowcontainer.size()
length2 := x2.highlowcontainer.size()
main:
for {
if pos1 < length1 && pos2 < length2 {
s1 := rb.highlowcontainer.getKeyAtIndex(pos1)
s2 := x2.highlowcontainer.getKeyAtIndex(pos2)
for {
if s1 == s2 {
c1 := rb.highlowcontainer.getContainerAtIndex(pos1)
c2 := x2.highlowcontainer.getContainerAtIndex(pos2)
if c1.intersects(c2) {
return true
}
pos1++
pos2++
if (pos1 == length1) || (pos2 == length2) {
break main
}
s1 = rb.highlowcontainer.getKeyAtIndex(pos1)
s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
} else if s1 < s2 {
pos1 = rb.highlowcontainer.advanceUntil(s2, pos1)
if pos1 == length1 {
break main
}
s1 = rb.highlowcontainer.getKeyAtIndex(pos1)
} else { //s1 > s2
pos2 = x2.highlowcontainer.advanceUntil(s1, pos2)
if pos2 == length2 {
break main
}
s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
}
}
} else {
break
}
}
return false
}
// Xor computes the symmetric difference between two bitmaps and stores the result in the current bitmap
func (rb *RoaringBitmap) Xor(x2 *RoaringBitmap) {
pos1 := 0
pos2 := 0
length1 := rb.highlowcontainer.size()
length2 := x2.highlowcontainer.size()
for {
if (pos1 < length1) && (pos2 < length2) {
s1 := rb.highlowcontainer.getKeyAtIndex(pos1)
s2 := x2.highlowcontainer.getKeyAtIndex(pos2)
if s1 < s2 {
pos1 = rb.highlowcontainer.advanceUntil(s2, pos1)
if pos1 == length1 {
break
}
} else if s1 > s2 {
c := x2.highlowcontainer.getWritableContainerAtIndex(pos2)
rb.highlowcontainer.insertNewKeyValueAt(pos1, x2.highlowcontainer.getKeyAtIndex(pos2), c)
length1++
pos1++
pos2++
} else {
// TODO: couple be computed in-place for reduced memory usage
c := rb.highlowcontainer.getContainerAtIndex(pos1).xor(x2.highlowcontainer.getContainerAtIndex(pos2))
if c.getCardinality() > 0 {
rb.highlowcontainer.setContainerAtIndex(pos1, c)
pos1++
} else {
rb.highlowcontainer.removeAtIndex(pos1)
length1--
}
pos2++
}
} else {
break
}
}
if pos1 == length1 {
rb.highlowcontainer.appendCopyMany(x2.highlowcontainer, pos2, length2)
}
}
// Or computes the union between two bitmaps and stores the result in the current bitmap
func (rb *RoaringBitmap) Or(x2 *RoaringBitmap) {
results := Or(rb, x2) // Todo: could be computed in-place for reduced memory usage
rb.highlowcontainer = results.highlowcontainer
}
// AndNot computes the difference between two bitmaps and stores the result in the current bitmap
func (rb *RoaringBitmap) AndNot(x2 *RoaringBitmap) {
pos1 := 0
pos2 := 0
intersectionsize := 0
length1 := rb.highlowcontainer.size()
length2 := x2.highlowcontainer.size()
main:
for {
if pos1 < length1 && pos2 < length2 {
s1 := rb.highlowcontainer.getKeyAtIndex(pos1)
s2 := x2.highlowcontainer.getKeyAtIndex(pos2)
for {
if s1 == s2 {
c1 := rb.highlowcontainer.getWritableContainerAtIndex(pos1)
c2 := x2.highlowcontainer.getContainerAtIndex(pos2)
diff := c1.iandNot(c2)
if diff.getCardinality() > 0 {
rb.highlowcontainer.replaceKeyAndContainerAtIndex(intersectionsize, s1, diff)
intersectionsize++
}
pos1++
pos2++
if (pos1 == length1) || (pos2 == length2) {
break main
}
s1 = rb.highlowcontainer.getKeyAtIndex(pos1)
s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
} else if s1 < s2 {
c1 := rb.highlowcontainer.getWritableContainerAtIndex(pos1)
rb.highlowcontainer.replaceKeyAndContainerAtIndex(intersectionsize, s1, c1)
intersectionsize++
pos1++
if pos1 == length1 {
break main
}
s1 = rb.highlowcontainer.getKeyAtIndex(pos1)
} else { //s1 > s2
pos2 = x2.highlowcontainer.advanceUntil(s1, pos2)
if pos2 == length2 {
break main
}
s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
}
}
} else {
break
}
}
// TODO:implement as a copy
for pos1 < length1 {
c1 := rb.highlowcontainer.getContainerAtIndex(pos1)
s1 := rb.highlowcontainer.getKeyAtIndex(pos1)
rb.highlowcontainer.replaceKeyAndContainerAtIndex(intersectionsize, s1, c1)
intersectionsize++
pos1++
}
rb.highlowcontainer.resize(intersectionsize)
}
// Or computes the union between two bitmaps and returns the result
func Or(x1, x2 *RoaringBitmap) *RoaringBitmap {
answer := NewRoaringBitmap()
pos1 := 0
pos2 := 0
length1 := x1.highlowcontainer.size()
length2 := x2.highlowcontainer.size()
main:
for (pos1 < length1) && (pos2 < length2) {
s1 := x1.highlowcontainer.getKeyAtIndex(pos1)
s2 := x2.highlowcontainer.getKeyAtIndex(pos2)
for {
if s1 < s2 {
answer.highlowcontainer.appendCopy(x1.highlowcontainer, pos1)
pos1++
if pos1 == length1 {
break main
}
s1 = x1.highlowcontainer.getKeyAtIndex(pos1)
} else if s1 > s2 {
answer.highlowcontainer.appendCopy(x2.highlowcontainer, pos2)
pos2++
if pos2 == length2 {
break main
}
s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
} else {
answer.highlowcontainer.appendContainer(s1, x1.highlowcontainer.getContainerAtIndex(pos1).or(x2.highlowcontainer.getContainerAtIndex(pos2)))
pos1++
pos2++
if (pos1 == length1) || (pos2 == length2) {
break main
}
s1 = x1.highlowcontainer.getKeyAtIndex(pos1)
s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
}
}
}
if pos1 == length1 {
answer.highlowcontainer.appendCopyMany(x2.highlowcontainer, pos2, length2)
} else if pos2 == length2 {
answer.highlowcontainer.appendCopyMany(x1.highlowcontainer, pos1, length1)
}
return answer
}
// And computes the intersection between two bitmaps and returns the result
func And(x1, x2 *RoaringBitmap) *RoaringBitmap {
answer := NewRoaringBitmap()
pos1 := 0
pos2 := 0
length1 := x1.highlowcontainer.size()
length2 := x2.highlowcontainer.size()
main:
for pos1 < length1 && pos2 < length2 {
s1 := x1.highlowcontainer.getKeyAtIndex(pos1)
s2 := x2.highlowcontainer.getKeyAtIndex(pos2)
for {
if s1 == s2 {
C := x1.highlowcontainer.getContainerAtIndex(pos1)
C = C.and(x2.highlowcontainer.getContainerAtIndex(pos2))
if C.getCardinality() > 0 {
answer.highlowcontainer.appendContainer(s1, C)
}
pos1++
pos2++
if (pos1 == length1) || (pos2 == length2) {
break main
}
s1 = x1.highlowcontainer.getKeyAtIndex(pos1)
s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
} else if s1 < s2 {
pos1 = x1.highlowcontainer.advanceUntil(s2, pos1)
if pos1 == length1 {
break main
}
s1 = x1.highlowcontainer.getKeyAtIndex(pos1)
} else { // s1 > s2
pos2 = x2.highlowcontainer.advanceUntil(s1, pos2)
if pos2 == length2 {
break main
}
s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
}
}
}
return answer
}
// Xor computes the symmetric difference between two bitmaps and returns the result
func Xor(x1, x2 *RoaringBitmap) *RoaringBitmap {
answer := NewRoaringBitmap()
pos1 := 0
pos2 := 0
length1 := x1.highlowcontainer.size()
length2 := x2.highlowcontainer.size()
for {
if (pos1 < length1) && (pos2 < length2) {
s1 := x1.highlowcontainer.getKeyAtIndex(pos1)
s2 := x2.highlowcontainer.getKeyAtIndex(pos2)
if s1 < s2 {
answer.highlowcontainer.appendCopy(x1.highlowcontainer, pos1)
pos1++
} else if s1 > s2 {
answer.highlowcontainer.appendCopy(x2.highlowcontainer, pos2)
pos2++
} else {
c := x1.highlowcontainer.getContainerAtIndex(pos1).xor(x2.highlowcontainer.getContainerAtIndex(pos2))
if c.getCardinality() > 0 {
answer.highlowcontainer.appendContainer(s1, c)
}
pos1++
pos2++
}
} else {
break
}
}
if pos1 == length1 {
answer.highlowcontainer.appendCopyMany(x2.highlowcontainer, pos2, length2)
} else if pos2 == length2 {
answer.highlowcontainer.appendCopyMany(x1.highlowcontainer, pos1, length1)
}
return answer
}
// AndNot computes the difference between two bitmaps and returns the result
func AndNot(x1, x2 *RoaringBitmap) *RoaringBitmap {
answer := NewRoaringBitmap()
pos1 := 0
pos2 := 0
length1 := x1.highlowcontainer.size()
length2 := x2.highlowcontainer.size()
main:
for {
if pos1 < length1 && pos2 < length2 {
s1 := x1.highlowcontainer.getKeyAtIndex(pos1)
s2 := x2.highlowcontainer.getKeyAtIndex(pos2)
for {
if s1 < s2 {
answer.highlowcontainer.appendCopy(x1.highlowcontainer, pos1)
pos1++
if pos1 == length1 {
break main
}
s1 = x1.highlowcontainer.getKeyAtIndex(pos1)
} else if s1 == s2 {
c1 := x1.highlowcontainer.getContainerAtIndex(pos1)
c2 := x2.highlowcontainer.getContainerAtIndex(pos2)
diff := c1.andNot(c2)
if diff.getCardinality() > 0 {
answer.highlowcontainer.appendContainer(s1, diff)
}
pos1++
pos2++
if (pos1 == length1) || (pos2 == length2) {
break main
}
s1 = x1.highlowcontainer.getKeyAtIndex(pos1)
s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
} else { //s1 > s2
pos2 = x2.highlowcontainer.advanceUntil(s1, pos2)
if pos2 == length2 {
break main
}
s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
}
}
} else {
break
}
}
if pos2 == length2 {
answer.highlowcontainer.appendCopyMany(x1.highlowcontainer, pos1, length1)
}
return answer
}
// BitmapOf generates a new bitmap filled with the specified integer
func BitmapOf(dat ...uint32) *RoaringBitmap {
ans := NewRoaringBitmap()
for _, i := range dat {
ans.Add(i)
}
return ans
}
// Flip negates the bits in the given range, any integer present in this range and in the bitmap is removed,
// and any integer present in the range and not in the bitmap is added
func (rb *RoaringBitmap) Flip(rangeStart, rangeEnd uint32) {
if rangeStart >= rangeEnd {
return
}
hbStart := highbits(rangeStart)
lbStart := lowbits(rangeStart)
hbLast := highbits(rangeEnd - 1)
lbLast := lowbits(rangeEnd - 1)
max := toIntUnsigned(maxLowBit())
for hb := hbStart; hb <= hbLast; hb++ {
containerStart := uint32(0)
if hb == hbStart {
containerStart = toIntUnsigned(lbStart)
}
containerLast := max
if hb == hbLast {
containerLast = toIntUnsigned(lbLast)
}
i := rb.highlowcontainer.getIndex(hb)
if i >= 0 {
c := rb.highlowcontainer.getWritableContainerAtIndex(i).inot(int(containerStart), int(containerLast))
if c.getCardinality() > 0 {
rb.highlowcontainer.setContainerAtIndex(i, c)
} else {
rb.highlowcontainer.removeAtIndex(i)
}
} else { // *think* the range of ones must never be
// empty.
rb.highlowcontainer.insertNewKeyValueAt(-i-1, hb, rangeOfOnes(int(containerStart), int(containerLast)))
}
}
}
// FlipInt calls Flip after casting the parameters to uint32 (convenience method)
func (rb *RoaringBitmap) FlipInt(rangeStart, rangeEnd int) {
rb.Flip(uint32(rangeStart), uint32(rangeEnd))
}
// Add the integers in [rangeStart, rangeEnd) to the bitmap
func (rb *RoaringBitmap) AddRange(rangeStart, rangeEnd uint32) {
if rangeStart >= rangeEnd {
return
}
hbStart := toIntUnsigned(highbits(rangeStart))
lbStart := toIntUnsigned(lowbits(rangeStart))
hbLast := toIntUnsigned(highbits(rangeEnd - 1))
lbLast := toIntUnsigned(lowbits(rangeEnd - 1))
max := toIntUnsigned(maxLowBit())
for hb := uint16(hbStart); hb <= uint16(hbLast); hb++ {
containerStart := uint32(0)
if hb == uint16(hbStart) {
containerStart = lbStart
}
containerLast := max
if hb == uint16(hbLast) {
containerLast = lbLast
}
i := rb.highlowcontainer.getIndex(hb)
if i >= 0 {
c := rb.highlowcontainer.getContainerAtIndex(i).iaddRange(int(containerStart), int(containerLast+1))
rb.highlowcontainer.setContainerAtIndex(i, c)
} else { // *think* the range of ones must never be
// empty.
rb.highlowcontainer.insertNewKeyValueAt(-i-1, hb, rangeOfOnes(int(containerStart), int(containerLast)))
}
}
}
// Remove the integers in [rangeStart, rangeEnd) from the bitmap
func (rb *RoaringBitmap) RemoveRange(rangeStart, rangeEnd uint32) {
if rangeStart >= rangeEnd {
return
}
hbStart := toIntUnsigned(highbits(rangeStart))
lbStart := toIntUnsigned(lowbits(rangeStart))
hbLast := toIntUnsigned(highbits(rangeEnd - 1))
lbLast := toIntUnsigned(lowbits(rangeEnd - 1))
max := toIntUnsigned(maxLowBit())
if hbStart == hbLast {
i := rb.highlowcontainer.getIndex(uint16(hbStart))
if i < 0 {
return
}
c := rb.highlowcontainer.getContainerAtIndex(i).iremoveRange(int(lbStart), int(lbLast+1))
if c.getCardinality() > 0 {
rb.highlowcontainer.setContainerAtIndex(i, c)
} else {
rb.highlowcontainer.removeAtIndex(i)
}
return
}
ifirst := rb.highlowcontainer.getIndex(uint16(hbStart))
ilast := rb.highlowcontainer.getIndex(uint16(hbLast))
if ifirst >= 0 {
if lbStart != 0 {
c := rb.highlowcontainer.getContainerAtIndex(ifirst).iremoveRange(int(lbStart), int(max+1))
if c.getCardinality() > 0 {
rb.highlowcontainer.setContainerAtIndex(ifirst, c)
ifirst++
}
}
} else {
ifirst = -ifirst - 1
}
if ilast >= 0 {
if lbLast != max {
c := rb.highlowcontainer.getContainerAtIndex(ilast).iremoveRange(int(0), int(lbLast+1))
if c.getCardinality() > 0 {
rb.highlowcontainer.setContainerAtIndex(ilast, c)
} else {
ilast++
}
} else {
ilast++
}
} else {
ilast = -ilast - 1
}
rb.highlowcontainer.removeIndexRange(ifirst, ilast)
}
// Flip negates the bits in the given range, any integer present in this range and in the bitmap is removed,
// and any integer present in the range and not in the bitmap is added, a new bitmap is returned leaving
// the current bitmap unchanged
func Flip(bm *RoaringBitmap, rangeStart, rangeEnd uint32) *RoaringBitmap {
if rangeStart >= rangeEnd {
return bm.Clone()
}
answer := NewRoaringBitmap()
hbStart := highbits(rangeStart)
lbStart := lowbits(rangeStart)
hbLast := highbits(rangeEnd - 1)
lbLast := lowbits(rangeEnd - 1)
// copy the containers before the active area
answer.highlowcontainer.appendCopiesUntil(bm.highlowcontainer, hbStart)
max := toIntUnsigned(maxLowBit())
for hb := hbStart; hb <= hbLast; hb++ {
containerStart := uint32(0)
if hb == hbStart {
containerStart = toIntUnsigned(lbStart)
}
containerLast := max
if hb == hbLast {
containerLast = toIntUnsigned(lbLast)
}
i := bm.highlowcontainer.getIndex(hb)
j := answer.highlowcontainer.getIndex(hb)
if i >= 0 {
c := bm.highlowcontainer.getContainerAtIndex(i).not(int(containerStart), int(containerLast))
if c.getCardinality() > 0 {
answer.highlowcontainer.insertNewKeyValueAt(-j-1, hb, c)
}
} else { // *think* the range of ones must never be
// empty.
answer.highlowcontainer.insertNewKeyValueAt(-j-1, hb,
rangeOfOnes(int(containerStart), int(containerLast)))
}
}
// copy the containers after the active area.
answer.highlowcontainer.appendCopiesAfter(bm.highlowcontainer, hbLast)
return answer
}
// FlipInt calls Flip after casting the parameters to uint32 (convenience method)
func FlipInt(bm *RoaringBitmap, rangeStart, rangeEnd int) *RoaringBitmap {
return Flip(bm, uint32(rangeStart), uint32(rangeEnd))
}