-
Notifications
You must be signed in to change notification settings - Fork 276
/
Copy pathroaring.c
3401 lines (3091 loc) · 129 KB
/
roaring.c
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
#include <assert.h>
#include <inttypes.h>
#include <limits.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <roaring/roaring.h>
// Include after roaring.h
#include <roaring/array_util.h>
#include <roaring/bitset_util.h>
#include <roaring/containers/containers.h>
#include <roaring/roaring_array.h>
#ifdef __cplusplus
using namespace ::roaring::internal;
extern "C" {
namespace roaring {
namespace api {
#endif
#define CROARING_SERIALIZATION_ARRAY_UINT32 1
#define CROARING_SERIALIZATION_CONTAINER 2
extern inline int roaring_trailing_zeroes(unsigned long long input_num);
extern inline int roaring_leading_zeroes(unsigned long long input_num);
extern inline void roaring_bitmap_init_cleared(roaring_bitmap_t *r);
extern inline bool roaring_bitmap_get_copy_on_write(const roaring_bitmap_t *r);
extern inline void roaring_bitmap_set_copy_on_write(roaring_bitmap_t *r,
bool cow);
extern inline roaring_bitmap_t *roaring_bitmap_create(void);
extern inline void roaring_bitmap_add_range(roaring_bitmap_t *r, uint64_t min,
uint64_t max);
extern inline void roaring_bitmap_remove_range(roaring_bitmap_t *r,
uint64_t min, uint64_t max);
static inline bool is_cow(const roaring_bitmap_t *r) {
return r->high_low_container.flags & ROARING_FLAG_COW;
}
static inline bool is_frozen(const roaring_bitmap_t *r) {
return r->high_low_container.flags & ROARING_FLAG_FROZEN;
}
// this is like roaring_bitmap_add, but it populates pointer arguments in such a
// way
// that we can recover the container touched, which, in turn can be used to
// accelerate some functions (when you repeatedly need to add to the same
// container)
static inline container_t *containerptr_roaring_bitmap_add(roaring_bitmap_t *r,
uint32_t val,
uint8_t *type,
int *index) {
roaring_array_t *ra = &r->high_low_container;
uint16_t hb = val >> 16;
const int i = ra_get_index(ra, hb);
if (i >= 0) {
ra_unshare_container_at_index(ra, (uint16_t)i);
container_t *c = ra_get_container_at_index(ra, (uint16_t)i, type);
uint8_t new_type = *type;
container_t *c2 = container_add(c, val & 0xFFFF, *type, &new_type);
*index = i;
if (c2 != c) {
container_free(c, *type);
ra_set_container_at_index(ra, i, c2, new_type);
*type = new_type;
return c2;
} else {
return c;
}
} else {
array_container_t *new_ac = array_container_create();
container_t *c =
container_add(new_ac, val & 0xFFFF, ARRAY_CONTAINER_TYPE, type);
// we could just assume that it stays an array container
ra_insert_new_key_value_at(ra, -i - 1, hb, c, *type);
*index = -i - 1;
return c;
}
}
roaring_bitmap_t *roaring_bitmap_create_with_capacity(uint32_t cap) {
roaring_bitmap_t *ans =
(roaring_bitmap_t *)roaring_malloc(sizeof(roaring_bitmap_t));
if (!ans) {
return NULL;
}
bool is_ok = ra_init_with_capacity(&ans->high_low_container, cap);
if (!is_ok) {
roaring_free(ans);
return NULL;
}
return ans;
}
bool roaring_bitmap_init_with_capacity(roaring_bitmap_t *r, uint32_t cap) {
return ra_init_with_capacity(&r->high_low_container, cap);
}
static inline void add_bulk_impl(roaring_bitmap_t *r,
roaring_bulk_context_t *context,
uint32_t val) {
uint16_t key = val >> 16;
if (context->container == NULL || context->key != key) {
uint8_t typecode;
int idx;
context->container =
containerptr_roaring_bitmap_add(r, val, &typecode, &idx);
context->typecode = typecode;
context->idx = idx;
context->key = key;
} else {
// no need to seek the container, it is at hand
// because we already have the container at hand, we can do the
// insertion directly, bypassing the roaring_bitmap_add call
uint8_t new_typecode;
container_t *container2 = container_add(
context->container, val & 0xFFFF, context->typecode, &new_typecode);
if (container2 != context->container) {
// rare instance when we need to change the container type
container_free(context->container, context->typecode);
ra_set_container_at_index(&r->high_low_container, context->idx,
container2, new_typecode);
context->typecode = new_typecode;
context->container = container2;
}
}
}
void roaring_bitmap_add_many(roaring_bitmap_t *r, size_t n_args,
const uint32_t *vals) {
uint32_t val;
const uint32_t *start = vals;
const uint32_t *end = vals + n_args;
const uint32_t *current_val = start;
if (n_args == 0) {
return;
}
uint8_t typecode;
int idx;
container_t *container;
val = *current_val;
container = containerptr_roaring_bitmap_add(r, val, &typecode, &idx);
roaring_bulk_context_t context = {container, idx, (uint16_t)(val >> 16),
typecode};
for (; current_val != end; current_val++) {
memcpy(&val, current_val, sizeof(val));
add_bulk_impl(r, &context, val);
}
}
void roaring_bitmap_add_bulk(roaring_bitmap_t *r,
roaring_bulk_context_t *context, uint32_t val) {
add_bulk_impl(r, context, val);
}
bool roaring_bitmap_contains_bulk(const roaring_bitmap_t *r,
roaring_bulk_context_t *context,
uint32_t val) {
uint16_t key = val >> 16;
if (context->container == NULL || context->key != key) {
int32_t start_idx = -1;
if (context->container != NULL && context->key < key) {
start_idx = context->idx;
}
int idx = ra_advance_until(&r->high_low_container, key, start_idx);
if (idx == ra_get_size(&r->high_low_container)) {
return false;
}
uint8_t typecode;
context->container = ra_get_container_at_index(
&r->high_low_container, (uint16_t)idx, &typecode);
context->typecode = typecode;
context->idx = idx;
context->key =
ra_get_key_at_index(&r->high_low_container, (uint16_t)idx);
// ra_advance_until finds the next key >= the target, we found a later
// container.
if (context->key != key) {
return false;
}
}
// context is now set up
return container_contains(context->container, val & 0xFFFF,
context->typecode);
}
roaring_bitmap_t *roaring_bitmap_of_ptr(size_t n_args, const uint32_t *vals) {
roaring_bitmap_t *answer = roaring_bitmap_create();
roaring_bitmap_add_many(answer, n_args, vals);
return answer;
}
roaring_bitmap_t *roaring_bitmap_of(size_t n_args, ...) {
// todo: could be greatly optimized but we do not expect this call to ever
// include long lists
roaring_bitmap_t *answer = roaring_bitmap_create();
roaring_bulk_context_t context = CROARING_ZERO_INITIALIZER;
va_list ap;
va_start(ap, n_args);
for (size_t i = 0; i < n_args; i++) {
uint32_t val = va_arg(ap, uint32_t);
roaring_bitmap_add_bulk(answer, &context, val);
}
va_end(ap);
return answer;
}
static inline uint64_t minimum_uint64(uint64_t a, uint64_t b) {
return (a < b) ? a : b;
}
roaring_bitmap_t *roaring_bitmap_from_range(uint64_t min, uint64_t max,
uint32_t step) {
if (max >= UINT64_C(0x100000000)) {
max = UINT64_C(0x100000000);
}
if (step == 0) return NULL;
if (max <= min) return NULL;
roaring_bitmap_t *answer = roaring_bitmap_create();
if (step >= (1 << 16)) {
for (uint32_t value = (uint32_t)min; value < max; value += step) {
roaring_bitmap_add(answer, value);
}
return answer;
}
uint64_t min_tmp = min;
do {
uint32_t key = (uint32_t)min_tmp >> 16;
uint32_t container_min = min_tmp & 0xFFFF;
uint32_t container_max =
(uint32_t)minimum_uint64(max - (key << 16), 1 << 16);
uint8_t type;
container_t *container = container_from_range(
&type, container_min, container_max, (uint16_t)step);
ra_append(&answer->high_low_container, (uint16_t)key, container, type);
uint32_t gap = container_max - container_min + step - 1;
min_tmp += gap - (gap % step);
} while (min_tmp < max);
// cardinality of bitmap will be ((uint64_t) max - min + step - 1 ) / step
return answer;
}
void roaring_bitmap_add_range_closed(roaring_bitmap_t *r, uint32_t min,
uint32_t max) {
if (min > max) {
return;
}
roaring_array_t *ra = &r->high_low_container;
uint32_t min_key = min >> 16;
uint32_t max_key = max >> 16;
int32_t num_required_containers = max_key - min_key + 1;
int32_t suffix_length =
count_greater(ra->keys, ra->size, (uint16_t)max_key);
int32_t prefix_length =
count_less(ra->keys, ra->size - suffix_length, (uint16_t)min_key);
int32_t common_length = ra->size - prefix_length - suffix_length;
if (num_required_containers > common_length) {
ra_shift_tail(ra, suffix_length,
num_required_containers - common_length);
}
int32_t src = prefix_length + common_length - 1;
int32_t dst = ra->size - suffix_length - 1;
for (uint32_t key = max_key; key != min_key - 1;
key--) { // beware of min_key==0
uint32_t container_min = (min_key == key) ? (min & 0xffff) : 0;
uint32_t container_max = (max_key == key) ? (max & 0xffff) : 0xffff;
container_t *new_container;
uint8_t new_type;
if (src >= 0 && ra->keys[src] == key) {
ra_unshare_container_at_index(ra, (uint16_t)src);
new_container =
container_add_range(ra->containers[src], ra->typecodes[src],
container_min, container_max, &new_type);
if (new_container != ra->containers[src]) {
container_free(ra->containers[src], ra->typecodes[src]);
}
src--;
} else {
new_container = container_from_range(&new_type, container_min,
container_max + 1, 1);
}
ra_replace_key_and_container_at_index(ra, dst, (uint16_t)key,
new_container, new_type);
dst--;
}
}
void roaring_bitmap_remove_range_closed(roaring_bitmap_t *r, uint32_t min,
uint32_t max) {
if (min > max) {
return;
}
roaring_array_t *ra = &r->high_low_container;
uint32_t min_key = min >> 16;
uint32_t max_key = max >> 16;
int32_t src = count_less(ra->keys, ra->size, (uint16_t)min_key);
int32_t dst = src;
while (src < ra->size && ra->keys[src] <= max_key) {
uint32_t container_min =
(min_key == ra->keys[src]) ? (min & 0xffff) : 0;
uint32_t container_max =
(max_key == ra->keys[src]) ? (max & 0xffff) : 0xffff;
ra_unshare_container_at_index(ra, (uint16_t)src);
container_t *new_container;
uint8_t new_type;
new_container =
container_remove_range(ra->containers[src], ra->typecodes[src],
container_min, container_max, &new_type);
if (new_container != ra->containers[src]) {
container_free(ra->containers[src], ra->typecodes[src]);
}
if (new_container) {
ra_replace_key_and_container_at_index(ra, dst, ra->keys[src],
new_container, new_type);
dst++;
}
src++;
}
if (src > dst) {
ra_shift_tail(ra, ra->size - src, dst - src);
}
}
void roaring_bitmap_printf(const roaring_bitmap_t *r) {
const roaring_array_t *ra = &r->high_low_container;
printf("{");
for (int i = 0; i < ra->size; ++i) {
container_printf_as_uint32_array(ra->containers[i], ra->typecodes[i],
((uint32_t)ra->keys[i]) << 16);
if (i + 1 < ra->size) {
printf(",");
}
}
printf("}");
}
void roaring_bitmap_printf_describe(const roaring_bitmap_t *r) {
const roaring_array_t *ra = &r->high_low_container;
printf("{");
for (int i = 0; i < ra->size; ++i) {
printf("%d: %s (%d)", ra->keys[i],
get_full_container_name(ra->containers[i], ra->typecodes[i]),
container_get_cardinality(ra->containers[i], ra->typecodes[i]));
if (ra->typecodes[i] == SHARED_CONTAINER_TYPE) {
printf("(shared count = %" PRIu32 " )",
croaring_refcount_get(
&(CAST_shared(ra->containers[i])->counter)));
}
if (i + 1 < ra->size) {
printf(", ");
}
}
printf("}");
}
/**
* (For advanced users.)
* Collect statistics about the bitmap
*/
void roaring_bitmap_statistics(const roaring_bitmap_t *r,
roaring_statistics_t *stat) {
const roaring_array_t *ra = &r->high_low_container;
memset(stat, 0, sizeof(*stat));
stat->n_containers = ra->size;
stat->min_value = roaring_bitmap_minimum(r);
stat->max_value = roaring_bitmap_maximum(r);
for (int i = 0; i < ra->size; ++i) {
uint8_t truetype =
get_container_type(ra->containers[i], ra->typecodes[i]);
uint32_t card =
container_get_cardinality(ra->containers[i], ra->typecodes[i]);
uint32_t sbytes =
container_size_in_bytes(ra->containers[i], ra->typecodes[i]);
stat->cardinality += card;
switch (truetype) {
case BITSET_CONTAINER_TYPE:
stat->n_bitset_containers++;
stat->n_values_bitset_containers += card;
stat->n_bytes_bitset_containers += sbytes;
break;
case ARRAY_CONTAINER_TYPE:
stat->n_array_containers++;
stat->n_values_array_containers += card;
stat->n_bytes_array_containers += sbytes;
break;
case RUN_CONTAINER_TYPE:
stat->n_run_containers++;
stat->n_values_run_containers += card;
stat->n_bytes_run_containers += sbytes;
break;
default:
assert(false);
roaring_unreachable;
}
}
}
/*
* Checks that:
* - Array containers are sorted and contain no duplicates
* - Range containers are sorted and contain no overlapping ranges
* - Roaring containers are sorted by key and there are no duplicate keys
* - The correct container type is use for each container (e.g. bitmaps aren't
* used for small containers)
*/
bool roaring_bitmap_internal_validate(const roaring_bitmap_t *r,
const char **reason) {
const char *reason_local;
if (reason == NULL) {
// Always allow assigning through *reason
reason = &reason_local;
}
*reason = NULL;
const roaring_array_t *ra = &r->high_low_container;
if (ra->size < 0) {
*reason = "negative size";
return false;
}
if (ra->allocation_size < 0) {
*reason = "negative allocation size";
return false;
}
if (ra->size > ra->allocation_size) {
*reason = "more containers than allocated space";
return false;
}
if (ra->flags & ~(ROARING_FLAG_COW | ROARING_FLAG_FROZEN)) {
*reason = "invalid flags";
return false;
}
if (ra->size == 0) {
return true;
}
if (ra->keys == NULL) {
*reason = "keys is NULL";
return false;
}
if (ra->typecodes == NULL) {
*reason = "typecodes is NULL";
return false;
}
if (ra->containers == NULL) {
*reason = "containers is NULL";
return false;
}
uint32_t prev_key = ra->keys[0];
for (int32_t i = 1; i < ra->size; ++i) {
if (ra->keys[i] <= prev_key) {
*reason = "keys not strictly increasing";
return false;
}
prev_key = ra->keys[i];
}
for (int32_t i = 0; i < ra->size; ++i) {
if (!container_internal_validate(ra->containers[i], ra->typecodes[i],
reason)) {
// reason should already be set
if (*reason == NULL) {
*reason = "container failed to validate but no reason given";
}
return false;
}
}
return true;
}
roaring_bitmap_t *roaring_bitmap_copy(const roaring_bitmap_t *r) {
roaring_bitmap_t *ans =
(roaring_bitmap_t *)roaring_malloc(sizeof(roaring_bitmap_t));
if (!ans) {
return NULL;
}
if (!ra_init_with_capacity( // allocation of list of containers can fail
&ans->high_low_container, r->high_low_container.size)) {
roaring_free(ans);
return NULL;
}
if (!ra_overwrite( // memory allocation of individual containers may fail
&r->high_low_container, &ans->high_low_container, is_cow(r))) {
roaring_bitmap_free(ans); // overwrite should leave in freeable state
return NULL;
}
roaring_bitmap_set_copy_on_write(ans, is_cow(r));
return ans;
}
bool roaring_bitmap_overwrite(roaring_bitmap_t *dest,
const roaring_bitmap_t *src) {
roaring_bitmap_set_copy_on_write(dest, is_cow(src));
return ra_overwrite(&src->high_low_container, &dest->high_low_container,
is_cow(src));
}
void roaring_bitmap_free(const roaring_bitmap_t *r) {
if (r == NULL) {
return;
}
if (!is_frozen(r)) {
ra_clear((roaring_array_t *)&r->high_low_container);
}
roaring_free((roaring_bitmap_t *)r);
}
void roaring_bitmap_clear(roaring_bitmap_t *r) {
ra_reset(&r->high_low_container);
}
void roaring_bitmap_add(roaring_bitmap_t *r, uint32_t val) {
roaring_array_t *ra = &r->high_low_container;
const uint16_t hb = val >> 16;
const int i = ra_get_index(ra, hb);
uint8_t typecode;
if (i >= 0) {
ra_unshare_container_at_index(ra, (uint16_t)i);
container_t *container =
ra_get_container_at_index(ra, (uint16_t)i, &typecode);
uint8_t newtypecode = typecode;
container_t *container2 =
container_add(container, val & 0xFFFF, typecode, &newtypecode);
if (container2 != container) {
container_free(container, typecode);
ra_set_container_at_index(&r->high_low_container, i, container2,
newtypecode);
}
} else {
array_container_t *newac = array_container_create();
container_t *container =
container_add(newac, val & 0xFFFF, ARRAY_CONTAINER_TYPE, &typecode);
// we could just assume that it stays an array container
ra_insert_new_key_value_at(&r->high_low_container, -i - 1, hb,
container, typecode);
}
}
bool roaring_bitmap_add_checked(roaring_bitmap_t *r, uint32_t val) {
const uint16_t hb = val >> 16;
const int i = ra_get_index(&r->high_low_container, hb);
uint8_t typecode;
bool result = false;
if (i >= 0) {
ra_unshare_container_at_index(&r->high_low_container, (uint16_t)i);
container_t *container = ra_get_container_at_index(
&r->high_low_container, (uint16_t)i, &typecode);
const int oldCardinality =
container_get_cardinality(container, typecode);
uint8_t newtypecode = typecode;
container_t *container2 =
container_add(container, val & 0xFFFF, typecode, &newtypecode);
if (container2 != container) {
container_free(container, typecode);
ra_set_container_at_index(&r->high_low_container, i, container2,
newtypecode);
result = true;
} else {
const int newCardinality =
container_get_cardinality(container, newtypecode);
result = oldCardinality != newCardinality;
}
} else {
array_container_t *newac = array_container_create();
container_t *container =
container_add(newac, val & 0xFFFF, ARRAY_CONTAINER_TYPE, &typecode);
// we could just assume that it stays an array container
ra_insert_new_key_value_at(&r->high_low_container, -i - 1, hb,
container, typecode);
result = true;
}
return result;
}
void roaring_bitmap_remove(roaring_bitmap_t *r, uint32_t val) {
const uint16_t hb = val >> 16;
const int i = ra_get_index(&r->high_low_container, hb);
uint8_t typecode;
if (i >= 0) {
ra_unshare_container_at_index(&r->high_low_container, (uint16_t)i);
container_t *container = ra_get_container_at_index(
&r->high_low_container, (uint16_t)i, &typecode);
uint8_t newtypecode = typecode;
container_t *container2 =
container_remove(container, val & 0xFFFF, typecode, &newtypecode);
if (container2 != container) {
container_free(container, typecode);
ra_set_container_at_index(&r->high_low_container, i, container2,
newtypecode);
}
if (container_get_cardinality(container2, newtypecode) != 0) {
ra_set_container_at_index(&r->high_low_container, i, container2,
newtypecode);
} else {
ra_remove_at_index_and_free(&r->high_low_container, i);
}
}
}
bool roaring_bitmap_remove_checked(roaring_bitmap_t *r, uint32_t val) {
const uint16_t hb = val >> 16;
const int i = ra_get_index(&r->high_low_container, hb);
uint8_t typecode;
bool result = false;
if (i >= 0) {
ra_unshare_container_at_index(&r->high_low_container, (uint16_t)i);
container_t *container = ra_get_container_at_index(
&r->high_low_container, (uint16_t)i, &typecode);
const int oldCardinality =
container_get_cardinality(container, typecode);
uint8_t newtypecode = typecode;
container_t *container2 =
container_remove(container, val & 0xFFFF, typecode, &newtypecode);
if (container2 != container) {
container_free(container, typecode);
ra_set_container_at_index(&r->high_low_container, i, container2,
newtypecode);
}
const int newCardinality =
container_get_cardinality(container2, newtypecode);
if (newCardinality != 0) {
ra_set_container_at_index(&r->high_low_container, i, container2,
newtypecode);
} else {
ra_remove_at_index_and_free(&r->high_low_container, i);
}
result = oldCardinality != newCardinality;
}
return result;
}
void roaring_bitmap_remove_many(roaring_bitmap_t *r, size_t n_args,
const uint32_t *vals) {
if (n_args == 0 || r->high_low_container.size == 0) {
return;
}
int32_t pos =
-1; // position of the container used in the previous iteration
for (size_t i = 0; i < n_args; i++) {
uint16_t key = (uint16_t)(vals[i] >> 16);
if (pos < 0 || key != r->high_low_container.keys[pos]) {
pos = ra_get_index(&r->high_low_container, key);
}
if (pos >= 0) {
uint8_t new_typecode;
container_t *new_container;
new_container = container_remove(
r->high_low_container.containers[pos], vals[i] & 0xffff,
r->high_low_container.typecodes[pos], &new_typecode);
if (new_container != r->high_low_container.containers[pos]) {
container_free(r->high_low_container.containers[pos],
r->high_low_container.typecodes[pos]);
ra_replace_key_and_container_at_index(&r->high_low_container,
pos, key, new_container,
new_typecode);
}
if (!container_nonzero_cardinality(new_container, new_typecode)) {
container_free(new_container, new_typecode);
ra_remove_at_index(&r->high_low_container, pos);
pos = -1;
}
}
}
}
// there should be some SIMD optimizations possible here
roaring_bitmap_t *roaring_bitmap_and(const roaring_bitmap_t *x1,
const roaring_bitmap_t *x2) {
uint8_t result_type = 0;
const int length1 = x1->high_low_container.size,
length2 = x2->high_low_container.size;
uint32_t neededcap = length1 > length2 ? length2 : length1;
roaring_bitmap_t *answer = roaring_bitmap_create_with_capacity(neededcap);
roaring_bitmap_set_copy_on_write(answer, is_cow(x1) || is_cow(x2));
int pos1 = 0, pos2 = 0;
while (pos1 < length1 && pos2 < length2) {
const uint16_t s1 =
ra_get_key_at_index(&x1->high_low_container, (uint16_t)pos1);
const uint16_t s2 =
ra_get_key_at_index(&x2->high_low_container, (uint16_t)pos2);
if (s1 == s2) {
uint8_t type1, type2;
container_t *c1 = ra_get_container_at_index(&x1->high_low_container,
(uint16_t)pos1, &type1);
container_t *c2 = ra_get_container_at_index(&x2->high_low_container,
(uint16_t)pos2, &type2);
container_t *c = container_and(c1, type1, c2, type2, &result_type);
if (container_nonzero_cardinality(c, result_type)) {
ra_append(&answer->high_low_container, s1, c, result_type);
} else {
container_free(c, result_type); // otherwise: memory leak!
}
++pos1;
++pos2;
} else if (s1 < s2) { // s1 < s2
pos1 = ra_advance_until(&x1->high_low_container, s2, pos1);
} else { // s1 > s2
pos2 = ra_advance_until(&x2->high_low_container, s1, pos2);
}
}
return answer;
}
/**
* Compute the union of 'number' bitmaps.
*/
roaring_bitmap_t *roaring_bitmap_or_many(size_t number,
const roaring_bitmap_t **x) {
if (number == 0) {
return roaring_bitmap_create();
}
if (number == 1) {
return roaring_bitmap_copy(x[0]);
}
roaring_bitmap_t *answer =
roaring_bitmap_lazy_or(x[0], x[1], LAZY_OR_BITSET_CONVERSION);
for (size_t i = 2; i < number; i++) {
roaring_bitmap_lazy_or_inplace(answer, x[i], LAZY_OR_BITSET_CONVERSION);
}
roaring_bitmap_repair_after_lazy(answer);
return answer;
}
/**
* Compute the xor of 'number' bitmaps.
*/
roaring_bitmap_t *roaring_bitmap_xor_many(size_t number,
const roaring_bitmap_t **x) {
if (number == 0) {
return roaring_bitmap_create();
}
if (number == 1) {
return roaring_bitmap_copy(x[0]);
}
roaring_bitmap_t *answer = roaring_bitmap_lazy_xor(x[0], x[1]);
for (size_t i = 2; i < number; i++) {
roaring_bitmap_lazy_xor_inplace(answer, x[i]);
}
roaring_bitmap_repair_after_lazy(answer);
return answer;
}
// inplace and (modifies its first argument).
void roaring_bitmap_and_inplace(roaring_bitmap_t *x1,
const roaring_bitmap_t *x2) {
if (x1 == x2) return;
int pos1 = 0, pos2 = 0, intersection_size = 0;
const int length1 = ra_get_size(&x1->high_low_container);
const int length2 = ra_get_size(&x2->high_low_container);
// any skipped-over or newly emptied containers in x1
// have to be freed.
while (pos1 < length1 && pos2 < length2) {
const uint16_t s1 =
ra_get_key_at_index(&x1->high_low_container, (uint16_t)pos1);
const uint16_t s2 =
ra_get_key_at_index(&x2->high_low_container, (uint16_t)pos2);
if (s1 == s2) {
uint8_t type1, type2, result_type;
container_t *c1 = ra_get_container_at_index(&x1->high_low_container,
(uint16_t)pos1, &type1);
container_t *c2 = ra_get_container_at_index(&x2->high_low_container,
(uint16_t)pos2, &type2);
// We do the computation "in place" only when c1 is not a shared
// container. Rationale: using a shared container safely with in
// place computation would require making a copy and then doing the
// computation in place which is likely less efficient than avoiding
// in place entirely and always generating a new container.
container_t *c =
(type1 == SHARED_CONTAINER_TYPE)
? container_and(c1, type1, c2, type2, &result_type)
: container_iand(c1, type1, c2, type2, &result_type);
if (c != c1) { // in this instance a new container was created, and
// we need to free the old one
container_free(c1, type1);
}
if (container_nonzero_cardinality(c, result_type)) {
ra_replace_key_and_container_at_index(&x1->high_low_container,
intersection_size, s1, c,
result_type);
intersection_size++;
} else {
container_free(c, result_type);
}
++pos1;
++pos2;
} else if (s1 < s2) {
pos1 = ra_advance_until_freeing(&x1->high_low_container, s2, pos1);
} else { // s1 > s2
pos2 = ra_advance_until(&x2->high_low_container, s1, pos2);
}
}
// if we ended early because x2 ran out, then all remaining in x1 should be
// freed
while (pos1 < length1) {
container_free(x1->high_low_container.containers[pos1],
x1->high_low_container.typecodes[pos1]);
++pos1;
}
// all containers after this have either been copied or freed
ra_downsize(&x1->high_low_container, intersection_size);
}
roaring_bitmap_t *roaring_bitmap_or(const roaring_bitmap_t *x1,
const roaring_bitmap_t *x2) {
uint8_t result_type = 0;
const int length1 = x1->high_low_container.size,
length2 = x2->high_low_container.size;
if (0 == length1) {
return roaring_bitmap_copy(x2);
}
if (0 == length2) {
return roaring_bitmap_copy(x1);
}
roaring_bitmap_t *answer =
roaring_bitmap_create_with_capacity(length1 + length2);
roaring_bitmap_set_copy_on_write(answer, is_cow(x1) || is_cow(x2));
int pos1 = 0, pos2 = 0;
uint8_t type1, type2;
uint16_t s1 = ra_get_key_at_index(&x1->high_low_container, (uint16_t)pos1);
uint16_t s2 = ra_get_key_at_index(&x2->high_low_container, (uint16_t)pos2);
while (true) {
if (s1 == s2) {
container_t *c1 = ra_get_container_at_index(&x1->high_low_container,
(uint16_t)pos1, &type1);
container_t *c2 = ra_get_container_at_index(&x2->high_low_container,
(uint16_t)pos2, &type2);
container_t *c = container_or(c1, type1, c2, type2, &result_type);
// since we assume that the initial containers are non-empty, the
// result here
// can only be non-empty
ra_append(&answer->high_low_container, s1, c, result_type);
++pos1;
++pos2;
if (pos1 == length1) break;
if (pos2 == length2) break;
s1 = ra_get_key_at_index(&x1->high_low_container, (uint16_t)pos1);
s2 = ra_get_key_at_index(&x2->high_low_container, (uint16_t)pos2);
} else if (s1 < s2) { // s1 < s2
container_t *c1 = ra_get_container_at_index(&x1->high_low_container,
(uint16_t)pos1, &type1);
// c1 = container_clone(c1, type1);
c1 = get_copy_of_container(c1, &type1, is_cow(x1));
if (is_cow(x1)) {
ra_set_container_at_index(&x1->high_low_container, pos1, c1,
type1);
}
ra_append(&answer->high_low_container, s1, c1, type1);
pos1++;
if (pos1 == length1) break;
s1 = ra_get_key_at_index(&x1->high_low_container, (uint16_t)pos1);
} else { // s1 > s2
container_t *c2 = ra_get_container_at_index(&x2->high_low_container,
(uint16_t)pos2, &type2);
// c2 = container_clone(c2, type2);
c2 = get_copy_of_container(c2, &type2, is_cow(x2));
if (is_cow(x2)) {
ra_set_container_at_index(&x2->high_low_container, pos2, c2,
type2);
}
ra_append(&answer->high_low_container, s2, c2, type2);
pos2++;
if (pos2 == length2) break;
s2 = ra_get_key_at_index(&x2->high_low_container, (uint16_t)pos2);
}
}
if (pos1 == length1) {
ra_append_copy_range(&answer->high_low_container,
&x2->high_low_container, pos2, length2,
is_cow(x2));
} else if (pos2 == length2) {
ra_append_copy_range(&answer->high_low_container,
&x1->high_low_container, pos1, length1,
is_cow(x1));
}
return answer;
}
// inplace or (modifies its first argument).
void roaring_bitmap_or_inplace(roaring_bitmap_t *x1,
const roaring_bitmap_t *x2) {
uint8_t result_type = 0;
int length1 = x1->high_low_container.size;
const int length2 = x2->high_low_container.size;
if (0 == length2) return;
if (0 == length1) {
roaring_bitmap_overwrite(x1, x2);
return;
}
int pos1 = 0, pos2 = 0;
uint8_t type1, type2;
uint16_t s1 = ra_get_key_at_index(&x1->high_low_container, (uint16_t)pos1);
uint16_t s2 = ra_get_key_at_index(&x2->high_low_container, (uint16_t)pos2);
while (true) {
if (s1 == s2) {
container_t *c1 = ra_get_container_at_index(&x1->high_low_container,
(uint16_t)pos1, &type1);
if (!container_is_full(c1, type1)) {
container_t *c2 = ra_get_container_at_index(
&x2->high_low_container, (uint16_t)pos2, &type2);
container_t *c =
(type1 == SHARED_CONTAINER_TYPE)
? container_or(c1, type1, c2, type2, &result_type)
: container_ior(c1, type1, c2, type2, &result_type);
if (c != c1) { // in this instance a new container was created,
// and we need to free the old one
container_free(c1, type1);
}
ra_set_container_at_index(&x1->high_low_container, pos1, c,
result_type);
}
++pos1;
++pos2;
if (pos1 == length1) break;
if (pos2 == length2) break;
s1 = ra_get_key_at_index(&x1->high_low_container, (uint16_t)pos1);
s2 = ra_get_key_at_index(&x2->high_low_container, (uint16_t)pos2);
} else if (s1 < s2) { // s1 < s2
pos1++;
if (pos1 == length1) break;
s1 = ra_get_key_at_index(&x1->high_low_container, (uint16_t)pos1);
} else { // s1 > s2
container_t *c2 = ra_get_container_at_index(&x2->high_low_container,
(uint16_t)pos2, &type2);
c2 = get_copy_of_container(c2, &type2, is_cow(x2));
if (is_cow(x2)) {
ra_set_container_at_index(&x2->high_low_container, pos2, c2,
type2);
}
// container_t *c2_clone = container_clone(c2, type2);
ra_insert_new_key_value_at(&x1->high_low_container, pos1, s2, c2,
type2);
pos1++;
length1++;
pos2++;
if (pos2 == length2) break;
s2 = ra_get_key_at_index(&x2->high_low_container, (uint16_t)pos2);
}
}
if (pos1 == length1) {
ra_append_copy_range(&x1->high_low_container, &x2->high_low_container,
pos2, length2, is_cow(x2));
}
}
roaring_bitmap_t *roaring_bitmap_xor(const roaring_bitmap_t *x1,
const roaring_bitmap_t *x2) {
uint8_t result_type = 0;
const int length1 = x1->high_low_container.size,
length2 = x2->high_low_container.size;
if (0 == length1) {