-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathgocroaring.go
More file actions
1344 lines (1220 loc) · 45.4 KB
/
Copy pathgocroaring.go
File metadata and controls
1344 lines (1220 loc) · 45.4 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
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
997
998
999
1000
// Package gocroaring is a wrapper for CRoaring in go.
// It provides a fast compressed bitmap data structure.
// See http://roaringbitmap.org for details.
//
// The package exposes two types: Bitmap, which stores 32-bit integers and
// wraps the roaring_bitmap_t C type, and Bitmap64, which stores 64-bit
// integers and wraps the roaring64_bitmap_t C type.
//
// Bitmaps hold memory allocated by C. That memory is released automatically
// once the Go value becomes unreachable (we rely on runtime.AddCleanup), but
// you may call Free explicitly to release it eagerly.
package gocroaring
/*
#cgo CFLAGS: -O3 -std=c11
// None of the CRoaring entry points below calls back into Go, and none of them
// retains a pointer to the memory it is handed. Saying so lets cgo use the
// cheaper calling convention and keeps the Go buffers we pass from escaping to
// the heap.
//
// The frozen views are the exception: they keep the buffer they are given, so
// they are deliberately absent from the noescape list.
#cgo noescape bitset_create
#cgo noescape bitset_free
#cgo noescape bitset_size_in_words
#cgo noescape gocroaring_deserialize_validate
#cgo noescape roaring_bitmap_add
#cgo noescape roaring_bitmap_add_bulk
#cgo noescape roaring_bitmap_add_checked
#cgo noescape roaring_bitmap_add_many
#cgo noescape roaring_bitmap_add_offset
#cgo noescape roaring_bitmap_add_range
#cgo noescape roaring_bitmap_add_range_closed
#cgo noescape roaring_bitmap_and
#cgo noescape roaring_bitmap_and_cardinality
#cgo noescape roaring_bitmap_and_inplace
#cgo noescape roaring_bitmap_andnot
#cgo noescape roaring_bitmap_andnot_cardinality
#cgo noescape roaring_bitmap_andnot_inplace
#cgo noescape roaring_bitmap_clear
#cgo noescape roaring_bitmap_contains
#cgo noescape roaring_bitmap_contains_bulk
#cgo noescape roaring_bitmap_contains_range
#cgo noescape roaring_bitmap_contains_range_closed
#cgo noescape roaring_bitmap_copy
#cgo noescape roaring_bitmap_create
#cgo noescape roaring_bitmap_create_with_capacity
#cgo noescape roaring_bitmap_deserialize_safe
#cgo noescape roaring_bitmap_equals
#cgo noescape roaring_bitmap_flip
#cgo noescape roaring_bitmap_flip_closed
#cgo noescape roaring_bitmap_flip_inplace
#cgo noescape roaring_bitmap_flip_inplace_closed
#cgo noescape roaring_bitmap_free
#cgo noescape roaring_bitmap_from_range
#cgo noescape roaring_bitmap_frozen_serialize
#cgo noescape roaring_bitmap_frozen_size_in_bytes
#cgo noescape roaring_bitmap_get_cardinality
#cgo noescape roaring_bitmap_get_copy_on_write
#cgo noescape roaring_bitmap_get_index
#cgo noescape roaring_bitmap_internal_validate
#cgo noescape roaring_bitmap_intersect
#cgo noescape roaring_bitmap_intersect_with_range
#cgo noescape roaring_bitmap_is_empty
#cgo noescape roaring_bitmap_is_strict_subset
#cgo noescape roaring_bitmap_is_subset
#cgo noescape roaring_bitmap_jaccard_index
#cgo noescape roaring_bitmap_lazy_or
#cgo noescape roaring_bitmap_lazy_or_inplace
#cgo noescape roaring_bitmap_lazy_xor
#cgo noescape roaring_bitmap_lazy_xor_inplace
#cgo noescape roaring_bitmap_maximum
#cgo noescape roaring_bitmap_minimum
#cgo noescape roaring_bitmap_of_ptr
#cgo noescape roaring_bitmap_or
#cgo noescape roaring_bitmap_or_cardinality
#cgo noescape roaring_bitmap_or_inplace
#cgo noescape roaring_bitmap_or_many
#cgo noescape roaring_bitmap_or_many_heap
#cgo noescape roaring_bitmap_overwrite
#cgo noescape roaring_bitmap_portable_deserialize_safe
#cgo noescape roaring_bitmap_portable_deserialize_size
#cgo noescape roaring_bitmap_portable_serialize
#cgo noescape roaring_bitmap_portable_size_in_bytes
#cgo noescape roaring_bitmap_range_cardinality
#cgo noescape roaring_bitmap_range_cardinality_closed
#cgo noescape roaring_bitmap_range_uint32_array
#cgo noescape roaring_bitmap_rank
#cgo noescape roaring_bitmap_rank_many
#cgo noescape roaring_bitmap_remove
#cgo noescape roaring_bitmap_remove_checked
#cgo noescape roaring_bitmap_remove_many
#cgo noescape roaring_bitmap_remove_range
#cgo noescape roaring_bitmap_remove_range_closed
#cgo noescape roaring_bitmap_remove_run_compression
#cgo noescape roaring_bitmap_repair_after_lazy
#cgo noescape roaring_bitmap_run_optimize
#cgo noescape roaring_bitmap_select
#cgo noescape roaring_bitmap_serialize
#cgo noescape roaring_bitmap_set_copy_on_write
#cgo noescape roaring_bitmap_shrink_to_fit
#cgo noescape roaring_bitmap_size_in_bytes
#cgo noescape roaring_bitmap_statistics
#cgo noescape roaring_bitmap_to_bitset
#cgo noescape roaring_bitmap_to_uint32_array
#cgo noescape roaring_bitmap_xor
#cgo noescape roaring_bitmap_xor_cardinality
#cgo noescape roaring_bitmap_xor_inplace
#cgo noescape roaring_bitmap_xor_many
#cgo nocallback bitset_create
#cgo nocallback bitset_free
#cgo nocallback bitset_size_in_words
#cgo nocallback gocroaring_deserialize_validate
#cgo nocallback roaring_bitmap_add
#cgo nocallback roaring_bitmap_add_bulk
#cgo nocallback roaring_bitmap_add_checked
#cgo nocallback roaring_bitmap_add_many
#cgo nocallback roaring_bitmap_add_offset
#cgo nocallback roaring_bitmap_add_range
#cgo nocallback roaring_bitmap_add_range_closed
#cgo nocallback roaring_bitmap_and
#cgo nocallback roaring_bitmap_and_cardinality
#cgo nocallback roaring_bitmap_and_inplace
#cgo nocallback roaring_bitmap_andnot
#cgo nocallback roaring_bitmap_andnot_cardinality
#cgo nocallback roaring_bitmap_andnot_inplace
#cgo nocallback roaring_bitmap_clear
#cgo nocallback roaring_bitmap_contains
#cgo nocallback roaring_bitmap_contains_bulk
#cgo nocallback roaring_bitmap_contains_range
#cgo nocallback roaring_bitmap_contains_range_closed
#cgo nocallback roaring_bitmap_copy
#cgo nocallback roaring_bitmap_create
#cgo nocallback roaring_bitmap_create_with_capacity
#cgo nocallback roaring_bitmap_deserialize_safe
#cgo nocallback roaring_bitmap_equals
#cgo nocallback roaring_bitmap_flip
#cgo nocallback roaring_bitmap_flip_closed
#cgo nocallback roaring_bitmap_flip_inplace
#cgo nocallback roaring_bitmap_flip_inplace_closed
#cgo nocallback roaring_bitmap_free
#cgo nocallback roaring_bitmap_from_range
#cgo nocallback roaring_bitmap_frozen_serialize
#cgo nocallback roaring_bitmap_frozen_size_in_bytes
#cgo nocallback roaring_bitmap_frozen_view
#cgo nocallback roaring_bitmap_get_cardinality
#cgo nocallback roaring_bitmap_get_copy_on_write
#cgo nocallback roaring_bitmap_get_index
#cgo nocallback roaring_bitmap_internal_validate
#cgo nocallback roaring_bitmap_intersect
#cgo nocallback roaring_bitmap_intersect_with_range
#cgo nocallback roaring_bitmap_is_empty
#cgo nocallback roaring_bitmap_is_strict_subset
#cgo nocallback roaring_bitmap_is_subset
#cgo nocallback roaring_bitmap_jaccard_index
#cgo nocallback roaring_bitmap_lazy_or
#cgo nocallback roaring_bitmap_lazy_or_inplace
#cgo nocallback roaring_bitmap_lazy_xor
#cgo nocallback roaring_bitmap_lazy_xor_inplace
#cgo nocallback roaring_bitmap_maximum
#cgo nocallback roaring_bitmap_minimum
#cgo nocallback roaring_bitmap_of_ptr
#cgo nocallback roaring_bitmap_or
#cgo nocallback roaring_bitmap_or_cardinality
#cgo nocallback roaring_bitmap_or_inplace
#cgo nocallback roaring_bitmap_or_many
#cgo nocallback roaring_bitmap_or_many_heap
#cgo nocallback roaring_bitmap_overwrite
#cgo nocallback roaring_bitmap_portable_deserialize_frozen
#cgo nocallback roaring_bitmap_portable_deserialize_safe
#cgo nocallback roaring_bitmap_portable_deserialize_size
#cgo nocallback roaring_bitmap_portable_serialize
#cgo nocallback roaring_bitmap_portable_size_in_bytes
#cgo nocallback roaring_bitmap_range_cardinality
#cgo nocallback roaring_bitmap_range_cardinality_closed
#cgo nocallback roaring_bitmap_range_uint32_array
#cgo nocallback roaring_bitmap_rank
#cgo nocallback roaring_bitmap_rank_many
#cgo nocallback roaring_bitmap_remove
#cgo nocallback roaring_bitmap_remove_checked
#cgo nocallback roaring_bitmap_remove_many
#cgo nocallback roaring_bitmap_remove_range
#cgo nocallback roaring_bitmap_remove_range_closed
#cgo nocallback roaring_bitmap_remove_run_compression
#cgo nocallback roaring_bitmap_repair_after_lazy
#cgo nocallback roaring_bitmap_run_optimize
#cgo nocallback roaring_bitmap_select
#cgo nocallback roaring_bitmap_serialize
#cgo nocallback roaring_bitmap_set_copy_on_write
#cgo nocallback roaring_bitmap_shrink_to_fit
#cgo nocallback roaring_bitmap_size_in_bytes
#cgo nocallback roaring_bitmap_statistics
#cgo nocallback roaring_bitmap_to_bitset
#cgo nocallback roaring_bitmap_to_uint32_array
#cgo nocallback roaring_bitmap_xor
#cgo nocallback roaring_bitmap_xor_cardinality
#cgo nocallback roaring_bitmap_xor_inplace
#cgo nocallback roaring_bitmap_xor_many
#include "roaring.h"
// Deserialize and validate in a single cgo crossing: two calls across the
// Go/C boundary would cost roughly twice as much as one.
static inline roaring_bitmap_t *gocroaring_deserialize_validate(
const char *buf, size_t maxbytes, const char **reason) {
roaring_bitmap_t *r = roaring_bitmap_portable_deserialize_safe(buf, maxbytes);
if (r == NULL) {
*reason = "deserialization failed";
return NULL;
}
if (!roaring_bitmap_internal_validate(r, reason)) {
roaring_bitmap_free(r);
return NULL;
}
return r;
}
*/
import "C"
import (
"bytes"
"errors"
"fmt"
"runtime"
"strconv"
"unsafe"
)
// CRoaringMajor, CRoaringMinor and CRoaringRevision report the version of the
// bundled CRoaring library.
const CRoaringMajor = C.ROARING_VERSION_MAJOR
const CRoaringMinor = C.ROARING_VERSION_MINOR
const CRoaringRevision = C.ROARING_VERSION_REVISION
// CRoaringVersion is the version of the bundled CRoaring library, as a string.
const CRoaringVersion = C.ROARING_VERSION
// FrozenAlignment is the alignment that a buffer must have before it can back
// a frozen view. See AlignedBuffer and ReadFrozenView.
const FrozenAlignment = 32
var (
// ErrNotEnoughSpace is returned by the serialization routines when the
// provided buffer is too small.
ErrNotEnoughSpace = errors.New("not enough space")
// ErrEmptyBuffer is returned when a deserialization routine is handed an
// empty buffer.
ErrEmptyBuffer = errors.New("empty buffer")
// ErrDeserialize is returned when a buffer does not contain a valid bitmap.
ErrDeserialize = errors.New("failed to read roaring bitmap")
// ErrMisaligned is returned by the frozen view routines when the buffer is
// not aligned on a FrozenAlignment boundary.
ErrMisaligned = fmt.Errorf("buffer is not aligned on a %d-byte boundary", FrozenAlignment)
// ErrNoSuchElement is returned by Select when the rank is out of range.
ErrNoSuchElement = errors.New("no such element")
)
// Bitmap is a compressed bitmap of 32-bit integers.
//
// A Bitmap is not safe for concurrent modification.
type Bitmap struct {
cpointer *C.roaring_bitmap_t
cleanup runtime.Cleanup
// pinned keeps the buffer backing a frozen view alive for as long as the
// bitmap is alive. It is nil for ordinary bitmaps.
pinned *byte
}
// wrap takes ownership of a C bitmap and arranges for it to be freed once the
// returned Bitmap becomes unreachable. It panics if p is nil.
func wrap(p *C.roaring_bitmap_t) *Bitmap {
if p == nil {
panic("C code returned a null pointer.")
}
rb := &Bitmap{cpointer: p}
// runtime.AddCleanup is cheaper than runtime.SetFinalizer: the bitmap is
// reclaimed in a single garbage collection cycle and it is never
// resurrected. The C pointer is passed as the cleanup argument so that the
// closure does not capture rb, which would keep it alive forever.
rb.cleanup = runtime.AddCleanup(rb, func(p *C.roaring_bitmap_t) {
C.roaring_bitmap_free(p)
}, p)
return rb
}
// Free releases the memory held by the bitmap. Using the bitmap afterwards is
// a mistake. Calling Free more than once is harmless.
func (rb *Bitmap) Free() {
if rb.cpointer == nil {
return
}
rb.cleanup.Stop()
C.roaring_bitmap_free(rb.cpointer)
rb.cpointer = nil
rb.pinned = nil
}
// New creates a new Bitmap with any number of initial values.
// This function may panic if the allocation failed.
func New(x ...uint32) *Bitmap {
if len(x) == 0 {
return wrap(C.roaring_bitmap_create())
}
rb := wrap(C.roaring_bitmap_of_ptr(C.size_t(len(x)), (*C.uint32_t)(unsafe.Pointer(&x[0]))))
runtime.KeepAlive(x)
return rb
}
// NewWithCapacity creates a new Bitmap with room for the given number of
// containers, avoiding some reallocations when the bitmap is populated.
// This function may panic if the allocation failed.
func NewWithCapacity(capacity uint32) *Bitmap {
return wrap(C.roaring_bitmap_create_with_capacity(C.uint32_t(capacity)))
}
// FromRange creates a bitmap containing min, min+step, min+2*step... up to but
// not including max. An empty range yields an empty bitmap. The step must be
// strictly positive.
// This function may panic if the allocation failed.
func FromRange(min, max uint64, step uint32) *Bitmap {
if step == 0 {
panic("gocroaring: FromRange requires a strictly positive step")
}
// C returns a null pointer for an empty range, which is not an error.
if max > 0x100000000 {
max = 0x100000000
}
if max <= min {
return New()
}
return wrap(C.roaring_bitmap_from_range(C.uint64_t(min), C.uint64_t(max), C.uint32_t(step)))
}
// Clone creates a copy of the Bitmap.
// This function may panic if the allocation failed.
func (rb *Bitmap) Clone() *Bitmap {
b := wrap(C.roaring_bitmap_copy(rb.cpointer))
runtime.KeepAlive(rb)
return b
}
// Assign copies x2 over rb, returning false if the copy failed.
func (rb *Bitmap) Assign(x2 *Bitmap) bool {
if rb.aliases(x2) {
return true // already a copy of itself
}
answer := bool(C.roaring_bitmap_overwrite(rb.cpointer, x2.cpointer))
runtime.KeepAlive(rb)
runtime.KeepAlive(x2)
return answer
}
// Printf writes a description of the bitmap to stdout.
func (rb *Bitmap) Printf() {
fmt.Print("{")
i := rb.Iterator()
counter := 30
for i.HasNext() {
counter = counter - 1
if counter == 0 {
fmt.Print("...")
}
fmt.Print(i.Next())
if i.HasNext() {
fmt.Print(",")
}
}
fmt.Print("}")
}
// String creates a string representation of the Bitmap.
func (rb *Bitmap) String() string {
var buffer bytes.Buffer
buffer.WriteString("{")
i := rb.Iterator()
counter := 0
for i.HasNext() {
// to avoid exhausting the memory
if counter > 0x40000 {
buffer.WriteString("...")
break
}
if counter > 0 {
buffer.WriteString(",")
}
buffer.WriteString(strconv.FormatUint(uint64(i.Next()), 10))
counter++
}
buffer.WriteString("}")
return buffer.String()
}
////////////////////////////////////////////////////////////////////////////////
// Adding and removing values
////////////////////////////////////////////////////////////////////////////////
// Add the integer(s) x to the bitmap.
func (rb *Bitmap) Add(x ...uint32) {
switch len(x) {
case 0:
return
case 1:
C.roaring_bitmap_add(rb.cpointer, C.uint32_t(x[0]))
default:
C.roaring_bitmap_add_many(rb.cpointer, C.size_t(len(x)), (*C.uint32_t)(unsafe.Pointer(&x[0])))
runtime.KeepAlive(x)
}
runtime.KeepAlive(rb)
}
// AddChecked adds the integer x to the bitmap and reports whether a new value
// was actually added.
func (rb *Bitmap) AddChecked(x uint32) bool {
answer := bool(C.roaring_bitmap_add_checked(rb.cpointer, C.uint32_t(x)))
runtime.KeepAlive(rb)
return answer
}
// AddRange adds all values in the range [min, max).
func (rb *Bitmap) AddRange(min, max uint64) {
C.roaring_bitmap_add_range(rb.cpointer, C.uint64_t(min), C.uint64_t(max))
runtime.KeepAlive(rb)
}
// AddRangeClosed adds all values in the range [min, max].
func (rb *Bitmap) AddRangeClosed(min, max uint32) {
C.roaring_bitmap_add_range_closed(rb.cpointer, C.uint32_t(min), C.uint32_t(max))
runtime.KeepAlive(rb)
}
// Remove the integer x from the bitmap.
func (rb *Bitmap) Remove(x uint32) {
C.roaring_bitmap_remove(rb.cpointer, C.uint32_t(x))
runtime.KeepAlive(rb)
}
// RemoveChecked removes the integer x from the bitmap and reports whether a
// value was actually removed.
func (rb *Bitmap) RemoveChecked(x uint32) bool {
answer := bool(C.roaring_bitmap_remove_checked(rb.cpointer, C.uint32_t(x)))
runtime.KeepAlive(rb)
return answer
}
// RemoveMany removes the integer(s) x from the bitmap.
func (rb *Bitmap) RemoveMany(x ...uint32) {
if len(x) == 0 {
return
}
C.roaring_bitmap_remove_many(rb.cpointer, C.size_t(len(x)), (*C.uint32_t)(unsafe.Pointer(&x[0])))
runtime.KeepAlive(x)
runtime.KeepAlive(rb)
}
// RemoveRange removes all values in the range [min, max).
func (rb *Bitmap) RemoveRange(min, max uint64) {
C.roaring_bitmap_remove_range(rb.cpointer, C.uint64_t(min), C.uint64_t(max))
runtime.KeepAlive(rb)
}
// RemoveRangeClosed removes all values in the range [min, max].
func (rb *Bitmap) RemoveRangeClosed(min, max uint32) {
C.roaring_bitmap_remove_range_closed(rb.cpointer, C.uint32_t(min), C.uint32_t(max))
runtime.KeepAlive(rb)
}
// Clear removes all elements from the bitmap.
func (rb *Bitmap) Clear() {
C.roaring_bitmap_clear(rb.cpointer)
runtime.KeepAlive(rb)
}
// BulkContext accelerates repeated accesses to a bitmap when the values are
// provided in ascending order. A context is tied to the bitmap it was last
// used with: it must not be reused across bitmaps, and it must be discarded
// whenever the bitmap is modified by anything other than AddBulk.
type BulkContext struct {
ctx C.roaring_bulk_context_t
}
// NewBulkContext returns a fresh context for use with AddBulk and
// ContainsBulk.
func NewBulkContext() *BulkContext {
return &BulkContext{}
}
// AddBulk adds the integer x to the bitmap, using ctx to remember the last
// container visited. Values should be provided in ascending order.
func (rb *Bitmap) AddBulk(ctx *BulkContext, x uint32) {
C.roaring_bitmap_add_bulk(rb.cpointer, &ctx.ctx, C.uint32_t(x))
runtime.KeepAlive(rb)
runtime.KeepAlive(ctx)
}
// ContainsBulk reports whether the integer x is in the bitmap, using ctx to
// remember the last container visited. Values should be provided in ascending
// order.
func (rb *Bitmap) ContainsBulk(ctx *BulkContext, x uint32) bool {
answer := bool(C.roaring_bitmap_contains_bulk(rb.cpointer, &ctx.ctx, C.uint32_t(x)))
runtime.KeepAlive(rb)
runtime.KeepAlive(ctx)
return answer
}
////////////////////////////////////////////////////////////////////////////////
// Queries
////////////////////////////////////////////////////////////////////////////////
// Contains returns true if the integer is contained in the bitmap.
func (rb *Bitmap) Contains(x uint32) bool {
answer := bool(C.roaring_bitmap_contains(rb.cpointer, C.uint32_t(x)))
runtime.KeepAlive(rb)
return answer
}
// ContainsRange returns true if all the integers in the range [x, y) are
// contained in the bitmap.
func (rb *Bitmap) ContainsRange(x, y uint64) bool {
answer := bool(C.roaring_bitmap_contains_range(rb.cpointer, C.uint64_t(x), C.uint64_t(y)))
runtime.KeepAlive(rb)
return answer
}
// ContainsRangeClosed returns true if all the integers in the range [x, y] are
// contained in the bitmap.
func (rb *Bitmap) ContainsRangeClosed(x, y uint32) bool {
answer := bool(C.roaring_bitmap_contains_range_closed(rb.cpointer, C.uint32_t(x), C.uint32_t(y)))
runtime.KeepAlive(rb)
return answer
}
// Cardinality returns the number of integers contained in the bitmap.
func (rb *Bitmap) Cardinality() uint64 {
answer := uint64(C.roaring_bitmap_get_cardinality(rb.cpointer))
runtime.KeepAlive(rb)
return answer
}
// GetCardinality returns the number of integers contained in the bitmap.
func (rb *Bitmap) GetCardinality() uint64 {
return rb.Cardinality()
}
// RangeCardinality returns the number of integers in the bitmap that fall in
// the range [min, max).
func (rb *Bitmap) RangeCardinality(min, max uint64) uint64 {
answer := uint64(C.roaring_bitmap_range_cardinality(rb.cpointer, C.uint64_t(min), C.uint64_t(max)))
runtime.KeepAlive(rb)
return answer
}
// RangeCardinalityClosed returns the number of integers in the bitmap that
// fall in the range [min, max].
func (rb *Bitmap) RangeCardinalityClosed(min, max uint32) uint64 {
answer := uint64(C.roaring_bitmap_range_cardinality_closed(rb.cpointer, C.uint32_t(min), C.uint32_t(max)))
runtime.KeepAlive(rb)
return answer
}
// IsEmpty returns true if the Bitmap is empty (it is faster than doing
// Cardinality() == 0).
func (rb *Bitmap) IsEmpty() bool {
answer := bool(C.roaring_bitmap_is_empty(rb.cpointer))
runtime.KeepAlive(rb)
return answer
}
// Maximum returns the largest of the integers contained in the bitmap,
// or 0 if the bitmap is empty.
func (rb *Bitmap) Maximum() uint32 {
answer := uint32(C.roaring_bitmap_maximum(rb.cpointer))
runtime.KeepAlive(rb)
return answer
}
// Minimum returns the smallest of the integers contained in the bitmap,
// or math.MaxUint32 if the bitmap is empty.
func (rb *Bitmap) Minimum() uint32 {
answer := uint32(C.roaring_bitmap_minimum(rb.cpointer))
runtime.KeepAlive(rb)
return answer
}
// Rank returns the number of values smaller or equal to x.
func (rb *Bitmap) Rank(x uint32) uint64 {
answer := uint64(C.roaring_bitmap_rank(rb.cpointer, C.uint32_t(x)))
runtime.KeepAlive(rb)
return answer
}
// RankMany returns the rank of each value in vals. The values must be sorted
// in ascending order. It is faster than calling Rank repeatedly.
func (rb *Bitmap) RankMany(vals []uint32) []uint64 {
answer := make([]uint64, len(vals))
if len(vals) == 0 {
return answer
}
begin := (*C.uint32_t)(unsafe.Pointer(&vals[0]))
end := (*C.uint32_t)(unsafe.Pointer(&vals[len(vals)-1]))
// The C API takes a one-past-the-end pointer.
end = (*C.uint32_t)(unsafe.Add(unsafe.Pointer(end), unsafe.Sizeof(vals[0])))
C.roaring_bitmap_rank_many(rb.cpointer, begin, end, (*C.uint64_t)(unsafe.Pointer(&answer[0])))
runtime.KeepAlive(vals)
runtime.KeepAlive(answer)
runtime.KeepAlive(rb)
return answer
}
// GetIndex returns the index of x in the bitmap, or -1 if x is not present.
// Unlike Rank, it distinguishes a missing value from a value of rank zero.
func (rb *Bitmap) GetIndex(x uint32) int64 {
answer := int64(C.roaring_bitmap_get_index(rb.cpointer, C.uint32_t(x)))
runtime.KeepAlive(rb)
return answer
}
// Select returns the element having the designated rank, if it exists.
func (rb *Bitmap) Select(rank uint32) (uint32, error) {
var element C.uint32_t
exists := bool(C.roaring_bitmap_select(rb.cpointer, C.uint32_t(rank), &element))
runtime.KeepAlive(rb)
if !exists {
return 0, ErrNoSuchElement
}
return uint32(element), nil
}
// Equals returns true if the two bitmaps contain the same integers.
func (rb *Bitmap) Equals(o interface{}) bool {
srb, ok := o.(*Bitmap)
if !ok {
return false
}
answer := bool(C.roaring_bitmap_equals(rb.cpointer, srb.cpointer))
runtime.KeepAlive(rb)
runtime.KeepAlive(srb)
return answer
}
// IsSubset returns true if every integer of rb is also in x2.
func (rb *Bitmap) IsSubset(x2 *Bitmap) bool {
answer := bool(C.roaring_bitmap_is_subset(rb.cpointer, x2.cpointer))
runtime.KeepAlive(rb)
runtime.KeepAlive(x2)
return answer
}
// IsStrictSubset returns true if every integer of rb is also in x2 and the two
// bitmaps differ.
func (rb *Bitmap) IsStrictSubset(x2 *Bitmap) bool {
answer := bool(C.roaring_bitmap_is_strict_subset(rb.cpointer, x2.cpointer))
runtime.KeepAlive(rb)
runtime.KeepAlive(x2)
return answer
}
// Intersect checks whether the two bitmaps intersect.
func (rb *Bitmap) Intersect(x2 *Bitmap) bool {
answer := bool(C.roaring_bitmap_intersect(rb.cpointer, x2.cpointer))
runtime.KeepAlive(rb)
runtime.KeepAlive(x2)
return answer
}
// IntersectWithRange checks whether the bitmap intersects the range [x, y).
func (rb *Bitmap) IntersectWithRange(x, y uint64) bool {
answer := bool(C.roaring_bitmap_intersect_with_range(rb.cpointer, C.uint64_t(x), C.uint64_t(y)))
runtime.KeepAlive(rb)
return answer
}
// AndCardinality computes the size of the intersection between two bitmaps.
func (rb *Bitmap) AndCardinality(x2 *Bitmap) uint64 {
answer := uint64(C.roaring_bitmap_and_cardinality(rb.cpointer, x2.cpointer))
runtime.KeepAlive(rb)
runtime.KeepAlive(x2)
return answer
}
// OrCardinality computes the size of the union between two bitmaps.
func (rb *Bitmap) OrCardinality(x2 *Bitmap) uint64 {
answer := uint64(C.roaring_bitmap_or_cardinality(rb.cpointer, x2.cpointer))
runtime.KeepAlive(rb)
runtime.KeepAlive(x2)
return answer
}
// XorCardinality computes the size of the symmetric difference between two
// bitmaps.
func (rb *Bitmap) XorCardinality(x2 *Bitmap) uint64 {
answer := uint64(C.roaring_bitmap_xor_cardinality(rb.cpointer, x2.cpointer))
runtime.KeepAlive(rb)
runtime.KeepAlive(x2)
return answer
}
// AndNotCardinality computes the size of the difference between two bitmaps.
func (rb *Bitmap) AndNotCardinality(x2 *Bitmap) uint64 {
answer := uint64(C.roaring_bitmap_andnot_cardinality(rb.cpointer, x2.cpointer))
runtime.KeepAlive(rb)
runtime.KeepAlive(x2)
return answer
}
// JaccardIndex computes the Jaccard index between two bitmaps.
func (rb *Bitmap) JaccardIndex(x2 *Bitmap) float64 {
answer := float64(C.roaring_bitmap_jaccard_index(rb.cpointer, x2.cpointer))
runtime.KeepAlive(rb)
runtime.KeepAlive(x2)
return answer
}
// InternalValidate performs internal consistency checks. It is useful after
// deserializing bitmaps from untrusted sources. It returns nil if the bitmap
// is consistent, and an error describing the problem otherwise.
func (rb *Bitmap) InternalValidate() error {
var reason *C.char
ok := bool(C.roaring_bitmap_internal_validate(rb.cpointer, &reason))
runtime.KeepAlive(rb)
if ok {
return nil
}
return errors.New(C.GoString(reason))
}
// GetCopyOnWrite reports whether the bitmap uses copy-on-write containers.
func (rb *Bitmap) GetCopyOnWrite() bool {
answer := bool(C.roaring_bitmap_get_copy_on_write(rb.cpointer))
runtime.KeepAlive(rb)
return answer
}
// SetCopyOnWrite turns copy-on-write on or off. Copy-on-write saves memory and
// avoids copies, but it requires more care in a threaded context. If you
// enable it, enable it on all of your bitmaps: mixing bitmaps with and without
// copy-on-write is unsafe.
func (rb *Bitmap) SetCopyOnWrite(cow bool) {
C.roaring_bitmap_set_copy_on_write(rb.cpointer, C.bool(cow))
runtime.KeepAlive(rb)
}
////////////////////////////////////////////////////////////////////////////////
// In-place set operations
////////////////////////////////////////////////////////////////////////////////
// aliases reports whether the two wrappers refer to the same C bitmap. Several
// of the in-place C routines assert that their operands are distinct, so we
// answer the aliased cases ourselves rather than let the C library abort.
func (rb *Bitmap) aliases(x2 *Bitmap) bool {
return rb == x2 || rb.cpointer == x2.cpointer
}
// And computes the intersection between two bitmaps and stores the result in
// the current bitmap.
func (rb *Bitmap) And(x2 *Bitmap) {
if rb.aliases(x2) {
return // x AND x is x
}
C.roaring_bitmap_and_inplace(rb.cpointer, x2.cpointer)
runtime.KeepAlive(rb)
runtime.KeepAlive(x2)
}
// Xor computes the symmetric difference between two bitmaps and stores the
// result in the current bitmap.
func (rb *Bitmap) Xor(x2 *Bitmap) {
if rb.aliases(x2) {
rb.Clear() // x XOR x is empty
return
}
C.roaring_bitmap_xor_inplace(rb.cpointer, x2.cpointer)
runtime.KeepAlive(rb)
runtime.KeepAlive(x2)
}
// Or computes the union between two bitmaps and stores the result in the
// current bitmap.
func (rb *Bitmap) Or(x2 *Bitmap) {
if rb.aliases(x2) {
return // x OR x is x
}
C.roaring_bitmap_or_inplace(rb.cpointer, x2.cpointer)
runtime.KeepAlive(rb)
runtime.KeepAlive(x2)
}
// AndNot computes the difference between two bitmaps and stores the result in
// the current bitmap.
func (rb *Bitmap) AndNot(x2 *Bitmap) {
if rb.aliases(x2) {
rb.Clear() // x ANDNOT x is empty
return
}
C.roaring_bitmap_andnot_inplace(rb.cpointer, x2.cpointer)
runtime.KeepAlive(rb)
runtime.KeepAlive(x2)
}
// LazyOrInplace computes the union with x2 in place, leaving the bitmap in an
// invalid state until RepairAfterLazy is called. Set bitsetconversion to true
// to eagerly convert containers to bitsets when it might help.
func (rb *Bitmap) LazyOrInplace(x2 *Bitmap, bitsetconversion bool) {
if rb.aliases(x2) {
return // x OR x is x
}
C.roaring_bitmap_lazy_or_inplace(rb.cpointer, x2.cpointer, C.bool(bitsetconversion))
runtime.KeepAlive(rb)
runtime.KeepAlive(x2)
}
// LazyXorInplace computes the symmetric difference with x2 in place, leaving
// the bitmap in an invalid state until RepairAfterLazy is called.
func (rb *Bitmap) LazyXorInplace(x2 *Bitmap) {
if rb.aliases(x2) {
rb.Clear() // x XOR x is empty
return
}
C.roaring_bitmap_lazy_xor_inplace(rb.cpointer, x2.cpointer)
runtime.KeepAlive(rb)
runtime.KeepAlive(x2)
}
// RepairAfterLazy restores a bitmap produced by the lazy operations to a valid
// state. It must be called before any other operation.
func (rb *Bitmap) RepairAfterLazy() {
C.roaring_bitmap_repair_after_lazy(rb.cpointer)
runtime.KeepAlive(rb)
}
// Flip negates the bits in the given range (i.e., [rangeStart, rangeEnd)): any
// integer present in this range and in the bitmap is removed, and any integer
// in the range that was absent is added.
func (rb *Bitmap) Flip(rangeStart, rangeEnd uint64) {
C.roaring_bitmap_flip_inplace(rb.cpointer, C.uint64_t(rangeStart), C.uint64_t(rangeEnd))
runtime.KeepAlive(rb)
}
// FlipClosed negates the bits in the given range (i.e., [rangeStart, rangeEnd]).
func (rb *Bitmap) FlipClosed(rangeStart, rangeEnd uint32) {
C.roaring_bitmap_flip_inplace_closed(rb.cpointer, C.uint32_t(rangeStart), C.uint32_t(rangeEnd))
runtime.KeepAlive(rb)
}
// RunOptimize improves the compression of the bitmap (call this after
// populating a new bitmap); it returns true if the bitmap was modified.
func (rb *Bitmap) RunOptimize() bool {
answer := bool(C.roaring_bitmap_run_optimize(rb.cpointer))
runtime.KeepAlive(rb)
return answer
}
// RemoveRunCompression removes run-length encoding even when it is more space
// efficient; it returns whether a change was applied.
func (rb *Bitmap) RemoveRunCompression() bool {
answer := bool(C.roaring_bitmap_remove_run_compression(rb.cpointer))
runtime.KeepAlive(rb)
return answer
}
// ShrinkToFit releases unused memory and returns how many bytes were freed.
func (rb *Bitmap) ShrinkToFit() int {
answer := int(C.roaring_bitmap_shrink_to_fit(rb.cpointer))
runtime.KeepAlive(rb)
return answer
}
////////////////////////////////////////////////////////////////////////////////
// Set operations returning a new bitmap
////////////////////////////////////////////////////////////////////////////////
// Or computes the union between two bitmaps and returns the result.
// This function may panic if the allocation failed.
func Or(x1, x2 *Bitmap) *Bitmap {
b := wrap(C.roaring_bitmap_or(x1.cpointer, x2.cpointer))
runtime.KeepAlive(x1)
runtime.KeepAlive(x2)
return b
}
// And computes the intersection between two bitmaps and returns the result.
// This function may panic if the allocation failed.
func And(x1, x2 *Bitmap) *Bitmap {
b := wrap(C.roaring_bitmap_and(x1.cpointer, x2.cpointer))
runtime.KeepAlive(x1)
runtime.KeepAlive(x2)
return b
}
// Xor computes the symmetric difference between two bitmaps and returns the
// result.
// This function may panic if the allocation failed.
func Xor(x1, x2 *Bitmap) *Bitmap {
b := wrap(C.roaring_bitmap_xor(x1.cpointer, x2.cpointer))
runtime.KeepAlive(x1)
runtime.KeepAlive(x2)
return b
}
// AndNot computes the difference between two bitmaps and returns the result.
// This function may panic if the allocation failed.
func AndNot(x1, x2 *Bitmap) *Bitmap {
b := wrap(C.roaring_bitmap_andnot(x1.cpointer, x2.cpointer))
runtime.KeepAlive(x1)
runtime.KeepAlive(x2)
return b
}
// LazyOr computes the union between two bitmaps, leaving the result in an
// invalid state until RepairAfterLazy is called on it. Set bitsetconversion to
// true to eagerly convert containers to bitsets when it might help.
// This function may panic if the allocation failed.
func LazyOr(x1, x2 *Bitmap, bitsetconversion bool) *Bitmap {
b := wrap(C.roaring_bitmap_lazy_or(x1.cpointer, x2.cpointer, C.bool(bitsetconversion)))
runtime.KeepAlive(x1)
runtime.KeepAlive(x2)
return b
}
// LazyXor computes the symmetric difference between two bitmaps, leaving the
// result in an invalid state until RepairAfterLazy is called on it.
// This function may panic if the allocation failed.
func LazyXor(x1, x2 *Bitmap) *Bitmap {
b := wrap(C.roaring_bitmap_lazy_xor(x1.cpointer, x2.cpointer))
runtime.KeepAlive(x1)
runtime.KeepAlive(x2)
return b
}
// Flip negates the bits in the given range (i.e., [rangeStart, rangeEnd)) and
// returns the result.
// This function may panic if the allocation failed.
func Flip(bm *Bitmap, rangeStart, rangeEnd uint64) *Bitmap {
b := wrap(C.roaring_bitmap_flip(bm.cpointer, C.uint64_t(rangeStart), C.uint64_t(rangeEnd)))
runtime.KeepAlive(bm)
return b
}
// FlipClosed negates the bits in the given range (i.e., [rangeStart, rangeEnd])
// and returns the result.
// This function may panic if the allocation failed.
func FlipClosed(bm *Bitmap, rangeStart, rangeEnd uint32) *Bitmap {
b := wrap(C.roaring_bitmap_flip_closed(bm.cpointer, C.uint32_t(rangeStart), C.uint32_t(rangeEnd)))
runtime.KeepAlive(bm)
return b
}
// AddOffset returns a copy of the bitmap with the given (possibly negative)
// offset added to every value. Values that fall outside the 32-bit range are
// dropped.
// This function may panic if the allocation failed.
func (rb *Bitmap) AddOffset(offset int64) *Bitmap {
b := wrap(C.roaring_bitmap_add_offset(rb.cpointer, C.int64_t(offset)))
runtime.KeepAlive(rb)
return b
}
// cpointers collects the C pointers of a slice of bitmaps.
func cpointers(bitmaps []*Bitmap) []*C.roaring_bitmap_t {
po := make([]*C.roaring_bitmap_t, len(bitmaps))
for i, v := range bitmaps {
po[i] = v.cpointer
}
return po
}
// FastOr computes the union between many bitmaps quickly, as opposed to having
// to call Or repeatedly.
// This function may panic if the allocation failed.
func FastOr(bitmaps ...*Bitmap) *Bitmap {
if len(bitmaps) == 0 {
return New()
}
po := cpointers(bitmaps)
b := wrap(C.roaring_bitmap_or_many(C.size_t(len(po)), (**C.roaring_bitmap_t)(unsafe.Pointer(&po[0]))))
runtime.KeepAlive(bitmaps)
runtime.KeepAlive(po)
return b
}
// FastOrHeap computes the union between many bitmaps using a heap. It can be
// faster than FastOr when the bitmaps are numerous and of uneven sizes.
// This function may panic if the allocation failed.
func FastOrHeap(bitmaps ...*Bitmap) *Bitmap {
if len(bitmaps) == 0 {
return New()
}
po := cpointers(bitmaps)
b := wrap(C.roaring_bitmap_or_many_heap(C.uint32_t(len(po)), (**C.roaring_bitmap_t)(unsafe.Pointer(&po[0]))))
runtime.KeepAlive(bitmaps)
runtime.KeepAlive(po)
return b
}
// FastXor computes the symmetric difference between many bitmaps quickly, as
// opposed to having to call Xor repeatedly.
// This function may panic if the allocation failed.
func FastXor(bitmaps ...*Bitmap) *Bitmap {
if len(bitmaps) == 0 {
return New()
}