Debian Codegraph @

contrib/eggbitset/roaring.c

raw
1 /*
2 * Amalgamated copy of CRoaring 0.2.66, modified for GTK to reduce compiler
3 * warnings.
4 *
5 * Copyright 2016-2020 The CRoaring authors
6 * Copyright 2020 Benjamin Otte
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 * SPDX-License-Identifier: Apache-2.0
21 */
22
23 #pragma GCC diagnostic push
24 #pragma GCC diagnostic ignored "-Wdeclaration-after-statement"
25
26 #include "roaring.h"
27
28 /* used for http://dmalloc.com/ Dmalloc - Debug Malloc Library */
29 #ifdef DMALLOC
30 #include "dmalloc.h"
31 #endif
32
33 /* begin file src/array_util.c */
34 #include <assert.h>
35 #include <stdbool.h>
36 #include <stdint.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40
41
42 #ifdef USESSE4
43 // used by intersect_vector16
44 ALIGNED(0x1000)
45 static const uint8_t shuffle_mask16[] = {
46 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
47 0xFF, 0xFF, 0xFF, 0xFF, 0, 1, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
48 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 2, 3, 0xFF, 0xFF,
49 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
50 0, 1, 2, 3, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
51 0xFF, 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339 0xFF, 0xFF, 0xFF, 0xFF, 4, 5, 6, 7, 8, 9, 12, 13,
340 14, 15, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0, 1, 4, 5,
341 6, 7, 8, 9, 12, 13, 14, 15, 0xFF, 0xFF, 0xFF, 0xFF,
342 2, 3, 4, 5, 6, 7, 8, 9, 12, 13, 14, 15,
343 0xFF, 0xFF, 0xFF, 0xFF, 0, 1, 2, 3, 4, 5, 6, 7,
344 8, 9, 12, 13, 14, 15, 0xFF, 0xFF, 10, 11, 12, 13,
345 14, 15, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
346 0, 1, 10, 11, 12, 13, 14, 15, 0xFF, 0xFF, 0xFF, 0xFF,
347 0xFF, 0xFF, 0xFF, 0xFF, 2, 3, 10, 11, 12, 13, 14, 15,
348 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0, 1, 2, 3,
349 10, 11, 12, 13, 14, 15, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
350 4, 5, 10, 11, 12, 13, 14, 15, 0xFF, 0xFF, 0xFF, 0xFF,
351 0xFF, 0xFF, 0xFF, 0xFF, 0, 1, 4, 5, 10, 11, 12, 13,
352 14, 15, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 2, 3, 4, 5,
353 10, 11, 12, 13, 14, 15, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
354 0, 1, 2, 3, 4, 5, 10, 11, 12, 13, 14, 15,
355 0xFF, 0xFF, 0xFF, 0xFF, 6, 7, 10, 11, 12, 13, 14, 15,
356 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0, 1, 6, 7,
357 10, 11, 12, 13, 14, 15, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
358 2, 3, 6, 7, 10, 11, 12, 13, 14, 15, 0xFF, 0xFF,
359 0xFF, 0xFF, 0xFF, 0xFF, 0, 1, 2, 3, 6, 7, 10, 11,
360 12, 13, 14, 15, 0xFF, 0xFF, 0xFF, 0xFF, 4, 5, 6, 7,
361 10, 11, 12, 13, 14, 15, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
362 0, 1, 4, 5, 6, 7, 10, 11, 12, 13, 14, 15,
363 0xFF, 0xFF, 0xFF, 0xFF, 2, 3, 4, 5, 6, 7, 10, 11,
364 12, 13, 14, 15, 0xFF, 0xFF, 0xFF, 0xFF, 0, 1, 2, 3,
365 4, 5, 6, 7, 10, 11, 12, 13, 14, 15, 0xFF, 0xFF,
366 8, 9, 10, 11, 12, 13, 14, 15, 0xFF, 0xFF, 0xFF, 0xFF,
367 0xFF, 0xFF, 0xFF, 0xFF, 0, 1, 8, 9, 10, 11, 12, 13,
368 14, 15, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 2, 3, 8, 9,
369 10, 11, 12, 13, 14, 15, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
370 0, 1, 2, 3, 8, 9, 10, 11, 12, 13, 14, 15,
371 0xFF, 0xFF, 0xFF, 0xFF, 4, 5, 8, 9, 10, 11, 12, 13,
372 14, 15, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0, 1, 4, 5,
373 8, 9, 10, 11, 12, 13, 14, 15, 0xFF, 0xFF, 0xFF, 0xFF,
374 2, 3, 4, 5, 8, 9, 10, 11, 12, 13, 14, 15,
375 0xFF, 0xFF, 0xFF, 0xFF, 0, 1, 2, 3, 4, 5, 8, 9,
376 10, 11, 12, 13, 14, 15, 0xFF, 0xFF, 6, 7, 8, 9,
377 10, 11, 12, 13, 14, 15, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
378 0, 1, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
379 0xFF, 0xFF, 0xFF, 0xFF, 2, 3, 6, 7, 8, 9, 10, 11,
380 12, 13, 14, 15, 0xFF, 0xFF, 0xFF, 0xFF, 0, 1, 2, 3,
381 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0xFF, 0xFF,
382 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
383 0xFF, 0xFF, 0xFF, 0xFF, 0, 1, 4, 5, 6, 7, 8, 9,
384 10, 11, 12, 13, 14, 15, 0xFF, 0xFF, 2, 3, 4, 5,
385 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0xFF, 0xFF,
386 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
387 12, 13, 14, 15};
388
389 /**
390 * From Schlegel et al., Fast Sorted-Set Intersection using SIMD Instructions
391 * Optimized by D. Lemire on May 3rd 2013
392 */
393 int32_t intersect_vector16(const uint16_t *__restrict__ A, size_t s_a,
394 const uint16_t *__restrict__ B, size_t s_b,
395 uint16_t *C) {
396 size_t count = 0;
397 size_t i_a = 0, i_b = 0;
398 const int vectorlength = sizeof(__m128i) / sizeof(uint16_t);
399 const size_t st_a = (s_a / vectorlength) * vectorlength;
400 const size_t st_b = (s_b / vectorlength) * vectorlength;
401 __m128i v_a, v_b;
402 if ((i_a < st_a) && (i_b < st_b)) {
403 v_a = _mm_lddqu_si128((__m128i *)&A[i_a]);
404 v_b = _mm_lddqu_si128((__m128i *)&B[i_b]);
405 while ((A[i_a] == 0) || (B[i_b] == 0)) {
406 const __m128i res_v = _mm_cmpestrm(
407 v_b, vectorlength, v_a, vectorlength,
408 _SIDD_UWORD_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_BIT_MASK);
409 const int r = _mm_extract_epi32(res_v, 0);
410 __m128i sm16 = _mm_load_si128((const __m128i *)shuffle_mask16 + r);
411 __m128i p = _mm_shuffle_epi8(v_a, sm16);
412 _mm_storeu_si128((__m128i *)&C[count], p); // can overflow
413 count += _mm_popcnt_u32(r);
414 const uint16_t a_max = A[i_a + vectorlength - 1];
415 const uint16_t b_max = B[i_b + vectorlength - 1];
416 if (a_max <= b_max) {
417 i_a += vectorlength;
418 if (i_a == st_a) break;
419 v_a = _mm_lddqu_si128((__m128i *)&A[i_a]);
420 }
421 if (b_max <= a_max) {
422 i_b += vectorlength;
423 if (i_b == st_b) break;
424 v_b = _mm_lddqu_si128((__m128i *)&B[i_b]);
425 }
426 }
427 if ((i_a < st_a) && (i_b < st_b))
428 while (true) {
429 const __m128i res_v = _mm_cmpistrm(
430 v_b, v_a,
431 _SIDD_UWORD_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_BIT_MASK);
432 const int r = _mm_extract_epi32(res_v, 0);
433 __m128i sm16 =
434 _mm_load_si128((const __m128i *)shuffle_mask16 + r);
435 __m128i p = _mm_shuffle_epi8(v_a, sm16);
436 _mm_storeu_si128((__m128i *)&C[count], p); // can overflow
437 count += _mm_popcnt_u32(r);
438 const uint16_t a_max = A[i_a + vectorlength - 1];
439 const uint16_t b_max = B[i_b + vectorlength - 1];
440 if (a_max <= b_max) {
441 i_a += vectorlength;
442 if (i_a == st_a) break;
443 v_a = _mm_lddqu_si128((__m128i *)&A[i_a]);
444 }
445 if (b_max <= a_max) {
446 i_b += vectorlength;
447 if (i_b == st_b) break;
448 v_b = _mm_lddqu_si128((__m128i *)&B[i_b]);
449 }
450 }
451 }
452 // intersect the tail using scalar intersection
453 while (i_a < s_a && i_b < s_b) {
454 uint16_t a = A[i_a];
455 uint16_t b = B[i_b];
456 if (a < b) {
457 i_a++;
458 } else if (b < a) {
459 i_b++;
460 } else {
461 C[count] = a; //==b;
462 count++;
463 i_a++;
464 i_b++;
465 }
466 }
467 return (int32_t)count;
468 }
469
470 int32_t intersect_vector16_cardinality(const uint16_t *__restrict__ A,
471 size_t s_a,
472 const uint16_t *__restrict__ B,
473 size_t s_b) {
474 size_t count = 0;
475 size_t i_a = 0, i_b = 0;
476 const int vectorlength = sizeof(__m128i) / sizeof(uint16_t);
477 const size_t st_a = (s_a / vectorlength) * vectorlength;
478 const size_t st_b = (s_b / vectorlength) * vectorlength;
479 __m128i v_a, v_b;
480 if ((i_a < st_a) && (i_b < st_b)) {
481 v_a = _mm_lddqu_si128((__m128i *)&A[i_a]);
482 v_b = _mm_lddqu_si128((__m128i *)&B[i_b]);
483 while ((A[i_a] == 0) || (B[i_b] == 0)) {
484 const __m128i res_v = _mm_cmpestrm(
485 v_b, vectorlength, v_a, vectorlength,
486 _SIDD_UWORD_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_BIT_MASK);
487 const int r = _mm_extract_epi32(res_v, 0);
488 count += _mm_popcnt_u32(r);
489 const uint16_t a_max = A[i_a + vectorlength - 1];
490 const uint16_t b_max = B[i_b + vectorlength - 1];
491 if (a_max <= b_max) {
492 i_a += vectorlength;
493 if (i_a == st_a) break;
494 v_a = _mm_lddqu_si128((__m128i *)&A[i_a]);
495 }
496 if (b_max <= a_max) {
497 i_b += vectorlength;
498 if (i_b == st_b) break;
499 v_b = _mm_lddqu_si128((__m128i *)&B[i_b]);
500 }
501 }
502 if ((i_a < st_a) && (i_b < st_b))
503 while (true) {
504 const __m128i res_v = _mm_cmpistrm(
505 v_b, v_a,
506 _SIDD_UWORD_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_BIT_MASK);
507 const int r = _mm_extract_epi32(res_v, 0);
508 count += _mm_popcnt_u32(r);
509 const uint16_t a_max = A[i_a + vectorlength - 1];
510 const uint16_t b_max = B[i_b + vectorlength - 1];
511 if (a_max <= b_max) {
512 i_a += vectorlength;
513 if (i_a == st_a) break;
514 v_a = _mm_lddqu_si128((__m128i *)&A[i_a]);
515 }
516 if (b_max <= a_max) {
517 i_b += vectorlength;
518 if (i_b == st_b) break;
519 v_b = _mm_lddqu_si128((__m128i *)&B[i_b]);
520 }
521 }
522 }
523 // intersect the tail using scalar intersection
524 while (i_a < s_a && i_b < s_b) {
525 uint16_t a = A[i_a];
526 uint16_t b = B[i_b];
527 if (a < b) {
528 i_a++;
529 } else if (b < a) {
530 i_b++;
531 } else {
532 count++;
533 i_a++;
534 i_b++;
535 }
536 }
537 return (int32_t)count;
538 }
539
540 /////////
541 // Warning:
542 // This function may not be safe if A == C or B == C.
543 /////////
544 int32_t difference_vector16(const uint16_t *__restrict__ A, size_t s_a,
545 const uint16_t *__restrict__ B, size_t s_b,
546 uint16_t *C) {
547 // we handle the degenerate case
548 if (s_a == 0) return 0;
549 if (s_b == 0) {
550 if (A != C) memcpy(C, A, sizeof(uint16_t) * s_a);
551 return (int32_t)s_a;
552 }
553 // handle the leading zeroes, it is messy but it allows us to use the fast
554 // _mm_cmpistrm intrinsic safely
555 int32_t count = 0;
556 if ((A[0] == 0) || (B[0] == 0)) {
557 if ((A[0] == 0) && (B[0] == 0)) {
558 A++;
559 s_a--;
560 B++;
561 s_b--;
562 } else if (A[0] == 0) {
563 C[count++] = 0;
564 A++;
565 s_a--;
566 } else {
567 B++;
568 s_b--;
569 }
570 }
571 // at this point, we have two non-empty arrays, made of non-zero
572 // increasing values.
573 size_t i_a = 0, i_b = 0;
574 const size_t vectorlength = sizeof(__m128i) / sizeof(uint16_t);
575 const size_t st_a = (s_a / vectorlength) * vectorlength;
576 const size_t st_b = (s_b / vectorlength) * vectorlength;
577 if ((i_a < st_a) && (i_b < st_b)) { // this is the vectorized code path
578 __m128i v_a, v_b; //, v_bmax;
579 // we load a vector from A and a vector from B
580 v_a = _mm_lddqu_si128((__m128i *)&A[i_a]);
581 v_b = _mm_lddqu_si128((__m128i *)&B[i_b]);
582 // we have a runningmask which indicates which values from A have been
583 // spotted in B, these don't get written out.
584 __m128i runningmask_a_found_in_b = _mm_setzero_si128();
585 /****
586 * start of the main vectorized loop
587 *****/
588 while (true) {
589 // afoundinb will contain a mask indicate for each entry in A
590 // whether it is seen
591 // in B
592 const __m128i a_found_in_b =
593 _mm_cmpistrm(v_b, v_a, _SIDD_UWORD_OPS | _SIDD_CMP_EQUAL_ANY |
594 _SIDD_BIT_MASK);
595 runningmask_a_found_in_b =
596 _mm_or_si128(runningmask_a_found_in_b, a_found_in_b);
597 // we always compare the last values of A and B
598 const uint16_t a_max = A[i_a + vectorlength - 1];
599 const uint16_t b_max = B[i_b + vectorlength - 1];
600 if (a_max <= b_max) {
601 // Ok. In this code path, we are ready to write our v_a
602 // because there is no need to read more from B, they will
603 // all be large values.
604 const int bitmask_belongs_to_difference =
605 _mm_extract_epi32(runningmask_a_found_in_b, 0) ^ 0xFF;
606 /*** next few lines are probably expensive *****/
607 __m128i sm16 = _mm_load_si128((const __m128i *)shuffle_mask16 +
608 bitmask_belongs_to_difference);
609 __m128i p = _mm_shuffle_epi8(v_a, sm16);
610 _mm_storeu_si128((__m128i *)&C[count], p); // can overflow
611 count += _mm_popcnt_u32(bitmask_belongs_to_difference);
612 // we advance a
613 i_a += vectorlength;
614 if (i_a == st_a) // no more
615 break;
616 runningmask_a_found_in_b = _mm_setzero_si128();
617 v_a = _mm_lddqu_si128((__m128i *)&A[i_a]);
618 }
619 if (b_max <= a_max) {
620 // in this code path, the current v_b has become useless
621 i_b += vectorlength;
622 if (i_b == st_b) break;
623 v_b = _mm_lddqu_si128((__m128i *)&B[i_b]);
624 }
625 }
626 // at this point, either we have i_a == st_a, which is the end of the
627 // vectorized processing,
628 // or we have i_b == st_b, and we are not done processing the vector...
629 // so we need to finish it off.
630 if (i_a < st_a) { // we have unfinished business...
631 uint16_t buffer[8]; // buffer to do a masked load
632 memset(buffer, 0, 8 * sizeof(uint16_t));
633 memcpy(buffer, B + i_b, (s_b - i_b) * sizeof(uint16_t));
634 v_b = _mm_lddqu_si128((__m128i *)buffer);
635 const __m128i a_found_in_b =
636 _mm_cmpistrm(v_b, v_a, _SIDD_UWORD_OPS | _SIDD_CMP_EQUAL_ANY |
637 _SIDD_BIT_MASK);
638 runningmask_a_found_in_b =
639 _mm_or_si128(runningmask_a_found_in_b, a_found_in_b);
640 const int bitmask_belongs_to_difference =
641 _mm_extract_epi32(runningmask_a_found_in_b, 0) ^ 0xFF;
642 __m128i sm16 = _mm_load_si128((const __m128i *)shuffle_mask16 +
643 bitmask_belongs_to_difference);
644 __m128i p = _mm_shuffle_epi8(v_a, sm16);
645 _mm_storeu_si128((__m128i *)&C[count], p); // can overflow
646 count += _mm_popcnt_u32(bitmask_belongs_to_difference);
647 i_a += vectorlength;
648 }
649 // at this point we should have i_a == st_a and i_b == st_b
650 }
651 // do the tail using scalar code
652 while (i_a < s_a && i_b < s_b) {
653 uint16_t a = A[i_a];
654 uint16_t b = B[i_b];
655 if (b < a) {
656 i_b++;
657 } else if (a < b) {
658 C[count] = a;
659 count++;
660 i_a++;
661 } else { //==
662 i_a++;
663 i_b++;
664 }
665 }
666 if (i_a < s_a) {
667 if(C == A) {
668 assert((size_t)count <= i_a);
669 if((size_t)count < i_a) {
670 memmove(C + count, A + i_a, sizeof(uint16_t) * (s_a - i_a));
671 }
672 } else {
673 for(size_t i = 0; i < (s_a - i_a); i++) {
674 C[count + i] = A[i + i_a];
675 }
676 }
677 count += (int32_t)(s_a - i_a);
678 }
679 return count;
680 }
681
682 #endif // USESSE4
683
684
685
686 #ifdef USE_OLD_SKEW_INTERSECT
687 // TODO: given enough experience with the new skew intersect, drop the old one from the code base.
688
689
690 /* Computes the intersection between one small and one large set of uint16_t.
691 * Stores the result into buffer and return the number of elements. */
692 int32_t intersect_skewed_uint16(const uint16_t *small, size_t size_s,
693 const uint16_t *large, size_t size_l,
694 uint16_t *buffer) {
695 size_t pos = 0, idx_l = 0, idx_s = 0;
696
697 if (0 == size_s) {
698 return 0;
699 }
700
701 uint16_t val_l = large[idx_l], val_s = small[idx_s];
702
703 while (true) {
704 if (val_l < val_s) {
705 idx_l = advanceUntil(large, (int32_t)idx_l, (int32_t)size_l, val_s);
706 if (idx_l == size_l) break;
707 val_l = large[idx_l];
708 } else if (val_s < val_l) {
709 idx_s++;
710 if (idx_s == size_s) break;
711 val_s = small[idx_s];
712 } else {
713 buffer[pos++] = val_s;
714 idx_s++;
715 if (idx_s == size_s) break;
716 val_s = small[idx_s];
717 idx_l = advanceUntil(large, (int32_t)idx_l, (int32_t)size_l, val_s);
718 if (idx_l == size_l) break;
719 val_l = large[idx_l];
720 }
721 }
722
723 return (int32_t)pos;
724 }
725 #else // USE_OLD_SKEW_INTERSECT
726
727
728 /**
729 * Branchless binary search going after 4 values at once.
730 * Assumes that array is sorted.
731 * You have that array[*index1] >= target1, array[*index12] >= target2, ...
732 * except when *index1 = n, in which case you know that all values in array are
733 * smaller than target1, and so forth.
734 * It has logarithmic complexity.
735 */
736 static void binarySearch4(const uint16_t *array, int32_t n, uint16_t target1,
737 uint16_t target2, uint16_t target3, uint16_t target4,
738 int32_t *index1, int32_t *index2, int32_t *index3,
739 int32_t *index4) {
740 const uint16_t *base1 = array;
741 const uint16_t *base2 = array;
742 const uint16_t *base3 = array;
743 const uint16_t *base4 = array;
744 if (n == 0)
745 return;
746 while (n > 1) {
747 int32_t half = n >> 1;
748 base1 = (base1[half] < target1) ? &base1[half] : base1;
749 base2 = (base2[half] < target2) ? &base2[half] : base2;
750 base3 = (base3[half] < target3) ? &base3[half] : base3;
751 base4 = (base4[half] < target4) ? &base4[half] : base4;
752 n -= half;
753 }
754 *index1 = (int32_t)((*base1 < target1) + base1 - array);
755 *index2 = (int32_t)((*base2 < target2) + base2 - array);
756 *index3 = (int32_t)((*base3 < target3) + base3 - array);
757 *index4 = (int32_t)((*base4 < target4) + base4 - array);
758 }
759
760 /**
761 * Branchless binary search going after 2 values at once.
762 * Assumes that array is sorted.
763 * You have that array[*index1] >= target1, array[*index12] >= target2.
764 * except when *index1 = n, in which case you know that all values in array are
765 * smaller than target1, and so forth.
766 * It has logarithmic complexity.
767 */
768 static void binarySearch2(const uint16_t *array, int32_t n, uint16_t target1,
769 uint16_t target2, int32_t *index1, int32_t *index2) {
770 const uint16_t *base1 = array;
771 const uint16_t *base2 = array;
772 if (n == 0)
773 return;
774 while (n > 1) {
775 int32_t half = n >> 1;
776 base1 = (base1[half] < target1) ? &base1[half] : base1;
777 base2 = (base2[half] < target2) ? &base2[half] : base2;
778 n -= half;
779 }
780 *index1 = (int32_t)((*base1 < target1) + base1 - array);
781 *index2 = (int32_t)((*base2 < target2) + base2 - array);
782 }
783
784 /* Computes the intersection between one small and one large set of uint16_t.
785 * Stores the result into buffer and return the number of elements.
786 * Processes the small set in blocks of 4 values calling binarySearch4
787 * and binarySearch2. This approach can be slightly superior to a conventional
788 * galloping search in some instances.
789 */
790 int32_t intersect_skewed_uint16(const uint16_t *small, size_t size_s,
791 const uint16_t *large, size_t size_l,
792 uint16_t *buffer) {
793 size_t pos = 0, idx_l = 0, idx_s = 0;
794
795 if (0 == size_s) {
796 return 0;
797 }
798 int32_t index1 = 0, index2 = 0, index3 = 0, index4 = 0;
799 while ((idx_s + 4 <= size_s) && (idx_l < size_l)) {
800 uint16_t target1 = small[idx_s];
801 uint16_t target2 = small[idx_s + 1];
802 uint16_t target3 = small[idx_s + 2];
803 uint16_t target4 = small[idx_s + 3];
804 binarySearch4(large + idx_l, (int32_t)(size_l - idx_l), target1, target2, target3,
805 target4, &index1, &index2, &index3, &index4);
806 if ((index1 + idx_l < size_l) && (large[idx_l + index1] == target1)) {
807 buffer[pos++] = target1;
808 }
809 if ((index2 + idx_l < size_l) && (large[idx_l + index2] == target2)) {
810 buffer[pos++] = target2;
811 }
812 if ((index3 + idx_l < size_l) && (large[idx_l + index3] == target3)) {
813 buffer[pos++] = target3;
814 }
815 if ((index4 + idx_l < size_l) && (large[idx_l + index4] == target4)) {
816 buffer[pos++] = target4;
817 }
818 idx_s += 4;
819 idx_l += index4;
820 }
821 if ((idx_s + 2 <= size_s) && (idx_l < size_l)) {
822 uint16_t target1 = small[idx_s];
823 uint16_t target2 = small[idx_s + 1];
824 binarySearch2(large + idx_l, (int32_t)(size_l - idx_l), target1, target2, &index1,
825 &index2);
826 if ((index1 + idx_l < size_l) && (large[idx_l + index1] == target1)) {
827 buffer[pos++] = target1;
828 }
829 if ((index2 + idx_l < size_l) && (large[idx_l + index2] == target2)) {
830 buffer[pos++] = target2;
831 }
832 idx_s += 2;
833 idx_l += index2;
834 }
835 if ((idx_s < size_s) && (idx_l < size_l)) {
836 uint16_t val_s = small[idx_s];
837 int32_t index = binarySearch(large + idx_l, (int32_t)(size_l - idx_l), val_s);
838 if (index >= 0)
839 buffer[pos++] = val_s;
840 }
841 return (int32_t)pos;
842 }
843
844
845 #endif //USE_OLD_SKEW_INTERSECT
846
847
848 // TODO: this could be accelerated, possibly, by using binarySearch4 as above.
849 int32_t intersect_skewed_uint16_cardinality(const uint16_t *small,
850 size_t size_s,
851 const uint16_t *large,
852 size_t size_l) {
853 size_t pos = 0, idx_l = 0, idx_s = 0;
854
855 if (0 == size_s) {
856 return 0;
857 }
858
859 uint16_t val_l = large[idx_l], val_s = small[idx_s];
860
861 while (true) {
862 if (val_l < val_s) {
863 idx_l = advanceUntil(large, (int32_t)idx_l, (int32_t)size_l, val_s);
864 if (idx_l == size_l) break;
865 val_l = large[idx_l];
866 } else if (val_s < val_l) {
867 idx_s++;
868 if (idx_s == size_s) break;
869 val_s = small[idx_s];
870 } else {
871 pos++;
872 idx_s++;
873 if (idx_s == size_s) break;
874 val_s = small[idx_s];
875 idx_l = advanceUntil(large, (int32_t)idx_l, (int32_t)size_l, val_s);
876 if (idx_l == size_l) break;
877 val_l = large[idx_l];
878 }
879 }
880
881 return (int32_t)pos;
882 }
883
884 bool intersect_skewed_uint16_nonempty(const uint16_t *small, size_t size_s,
885 const uint16_t *large, size_t size_l) {
886 size_t idx_l = 0, idx_s = 0;
887
888 if (0 == size_s) {
889 return false;
890 }
891
892 uint16_t val_l = large[idx_l], val_s = small[idx_s];
893
894 while (true) {
895 if (val_l < val_s) {
896 idx_l = advanceUntil(large, (int32_t)idx_l, (int32_t)size_l, val_s);
897 if (idx_l == size_l) break;
898 val_l = large[idx_l];
899 } else if (val_s < val_l) {
900 idx_s++;
901 if (idx_s == size_s) break;
902 val_s = small[idx_s];
903 } else {
904 return true;
905 }
906 }
907
908 return false;
909 }
910
911 /**
912 * Generic intersection function.
913 */
914 int32_t intersect_uint16(const uint16_t *A, const size_t lenA,
915 const uint16_t *B, const size_t lenB, uint16_t *out) {
916 const uint16_t *initout = out;
917 if (lenA == 0 || lenB == 0) return 0;
918 const uint16_t *endA = A + lenA;
919 const uint16_t *endB = B + lenB;
920
921 while (1) {
922 while (*A < *B) {
923 SKIP_FIRST_COMPARE:
924 if (++A == endA) return (int32_t)(out - initout);
925 }
926 while (*A > *B) {
927 if (++B == endB) return (int32_t)(out - initout);
928 }
929 if (*A == *B) {
930 *out++ = *A;
931 if (++A == endA || ++B == endB) return (int32_t)(out - initout);
932 } else {
933 goto SKIP_FIRST_COMPARE;
934 }
935 }
936 return (int32_t)(out - initout); // NOTREACHED
937 }
938
939 int32_t intersect_uint16_cardinality(const uint16_t *A, const size_t lenA,
940 const uint16_t *B, const size_t lenB) {
941 int32_t answer = 0;
942 if (lenA == 0 || lenB == 0) return 0;
943 const uint16_t *endA = A + lenA;
944 const uint16_t *endB = B + lenB;
945
946 while (1) {
947 while (*A < *B) {
948 SKIP_FIRST_COMPARE:
949 if (++A == endA) return answer;
950 }
951 while (*A > *B) {
952 if (++B == endB) return answer;
953 }
954 if (*A == *B) {
955 ++answer;
956 if (++A == endA || ++B == endB) return answer;
957 } else {
958 goto SKIP_FIRST_COMPARE;
959 }
960 }
961 return answer; // NOTREACHED
962 }
963
964
965 bool intersect_uint16_nonempty(const uint16_t *A, const size_t lenA,
966 const uint16_t *B, const size_t lenB) {
967 if (lenA == 0 || lenB == 0) return 0;
968 const uint16_t *endA = A + lenA;
969 const uint16_t *endB = B + lenB;
970
971 while (1) {
972 while (*A < *B) {
973 SKIP_FIRST_COMPARE:
974 if (++A == endA) return false;
975 }
976 while (*A > *B) {
977 if (++B == endB) return false;
978 }
979 if (*A == *B) {
980 return true;
981 } else {
982 goto SKIP_FIRST_COMPARE;
983 }
984 }
985 return false; // NOTREACHED
986 }
987
988
989
990 /**
991 * Generic intersection function.
992 */
993 size_t intersection_uint32(const uint32_t *A, const size_t lenA,
994 const uint32_t *B, const size_t lenB,
995 uint32_t *out) {
996 const uint32_t *initout = out;
997 if (lenA == 0 || lenB == 0) return 0;
998 const uint32_t *endA = A + lenA;
999 const uint32_t *endB = B + lenB;
1000
1001 while (1) {
1002 while (*A < *B) {
1003 SKIP_FIRST_COMPARE:
1004 if (++A == endA) return (out - initout);
1005 }
1006 while (*A > *B) {
1007 if (++B == endB) return (out - initout);
1008 }
1009 if (*A == *B) {
1010 *out++ = *A;
1011 if (++A == endA || ++B == endB) return (out - initout);
1012 } else {
1013 goto SKIP_FIRST_COMPARE;
1014 }
1015 }
1016 return (out - initout); // NOTREACHED
1017 }
1018
1019 size_t intersection_uint32_card(const uint32_t *A, const size_t lenA,
1020 const uint32_t *B, const size_t lenB) {
1021 if (lenA == 0 || lenB == 0) return 0;
1022 size_t card = 0;
1023 const uint32_t *endA = A + lenA;
1024 const uint32_t *endB = B + lenB;
1025
1026 while (1) {
1027 while (*A < *B) {
1028 SKIP_FIRST_COMPARE:
1029 if (++A == endA) return card;
1030 }
1031 while (*A > *B) {
1032 if (++B == endB) return card;
1033 }
1034 if (*A == *B) {
1035 card++;
1036 if (++A == endA || ++B == endB) return card;
1037 } else {
1038 goto SKIP_FIRST_COMPARE;
1039 }
1040 }
1041 return card; // NOTREACHED
1042 }
1043
1044 // can one vectorize the computation of the union? (Update: Yes! See
1045 // union_vector16).
1046
1047 size_t union_uint16(const uint16_t *set_1, size_t size_1, const uint16_t *set_2,
1048 size_t size_2, uint16_t *buffer) {
1049 size_t pos = 0, idx_1 = 0, idx_2 = 0;
1050
1051 if (0 == size_2) {
1052 memmove(buffer, set_1, size_1 * sizeof(uint16_t));
1053 return size_1;
1054 }
1055 if (0 == size_1) {
1056 memmove(buffer, set_2, size_2 * sizeof(uint16_t));
1057 return size_2;
1058 }
1059
1060 uint16_t val_1 = set_1[idx_1], val_2 = set_2[idx_2];
1061
1062 while (true) {
1063 if (val_1 < val_2) {
1064 buffer[pos++] = val_1;
1065 ++idx_1;
1066 if (idx_1 >= size_1) break;
1067 val_1 = set_1[idx_1];
1068 } else if (val_2 < val_1) {
1069 buffer[pos++] = val_2;
1070 ++idx_2;
1071 if (idx_2 >= size_2) break;
1072 val_2 = set_2[idx_2];
1073 } else {
1074 buffer[pos++] = val_1;
1075 ++idx_1;
1076 ++idx_2;
1077 if (idx_1 >= size_1 || idx_2 >= size_2) break;
1078 val_1 = set_1[idx_1];
1079 val_2 = set_2[idx_2];
1080 }
1081 }
1082
1083 if (idx_1 < size_1) {
1084 const size_t n_elems = size_1 - idx_1;
1085 memmove(buffer + pos, set_1 + idx_1, n_elems * sizeof(uint16_t));
1086 pos += n_elems;
1087 } else if (idx_2 < size_2) {
1088 const size_t n_elems = size_2 - idx_2;
1089 memmove(buffer + pos, set_2 + idx_2, n_elems * sizeof(uint16_t));
1090 pos += n_elems;
1091 }
1092
1093 return pos;
1094 }
1095
1096 int difference_uint16(const uint16_t *a1, int length1, const uint16_t *a2,
1097 int length2, uint16_t *a_out) {
1098 int out_card = 0;
1099 int k1 = 0, k2 = 0;
1100 if (length1 == 0) return 0;
1101 if (length2 == 0) {
1102 if (a1 != a_out) memcpy(a_out, a1, sizeof(uint16_t) * length1);
1103 return length1;
1104 }
1105 uint16_t s1 = a1[k1];
1106 uint16_t s2 = a2[k2];
1107 while (true) {
1108 if (s1 < s2) {
1109 a_out[out_card++] = s1;
1110 ++k1;
1111 if (k1 >= length1) {
1112 break;
1113 }
1114 s1 = a1[k1];
1115 } else if (s1 == s2) {
1116 ++k1;
1117 ++k2;
1118 if (k1 >= length1) {
1119 break;
1120 }
1121 if (k2 >= length2) {
1122 memmove(a_out + out_card, a1 + k1,
1123 sizeof(uint16_t) * (length1 - k1));
1124 return out_card + length1 - k1;
1125 }
1126 s1 = a1[k1];
1127 s2 = a2[k2];
1128 } else { // if (val1>val2)
1129 ++k2;
1130 if (k2 >= length2) {
1131 memmove(a_out + out_card, a1 + k1,
1132 sizeof(uint16_t) * (length1 - k1));
1133 return out_card + length1 - k1;
1134 }
1135 s2 = a2[k2];
1136 }
1137 }
1138 return out_card;
1139 }
1140
1141 int32_t xor_uint16(const uint16_t *array_1, int32_t card_1,
1142 const uint16_t *array_2, int32_t card_2, uint16_t *out) {
1143 int32_t pos1 = 0, pos2 = 0, pos_out = 0;
1144 while (pos1 < card_1 && pos2 < card_2) {
1145 const uint16_t v1 = array_1[pos1];
1146 const uint16_t v2 = array_2[pos2];
1147 if (v1 == v2) {
1148 ++pos1;
1149 ++pos2;
1150 continue;
1151 }
1152 if (v1 < v2) {
1153 out[pos_out++] = v1;
1154 ++pos1;
1155 } else {
1156 out[pos_out++] = v2;
1157 ++pos2;
1158 }
1159 }
1160 if (pos1 < card_1) {
1161 const size_t n_elems = card_1 - pos1;
1162 memcpy(out + pos_out, array_1 + pos1, n_elems * sizeof(uint16_t));
1163 pos_out += (int32_t)n_elems;
1164 } else if (pos2 < card_2) {
1165 const size_t n_elems = card_2 - pos2;
1166 memcpy(out + pos_out, array_2 + pos2, n_elems * sizeof(uint16_t));
1167 pos_out += (int32_t)n_elems;
1168 }
1169 return pos_out;
1170 }
1171
1172 #ifdef USESSE4
1173
1174 /***
1175 * start of the SIMD 16-bit union code
1176 *
1177 */
1178
1179 // Assuming that vInput1 and vInput2 are sorted, produces a sorted output going
1180 // from vecMin all the way to vecMax
1181 // developed originally for merge sort using SIMD instructions.
1182 // Standard merge. See, e.g., Inoue and Taura, SIMD- and Cache-Friendly
1183 // Algorithm for Sorting an Array of Structures
1184 static inline void sse_merge(const __m128i *vInput1,
1185 const __m128i *vInput2, // input 1 & 2
1186 __m128i *vecMin, __m128i *vecMax) { // output
1187 __m128i vecTmp;
1188 vecTmp = _mm_min_epu16(*vInput1, *vInput2);
1189 *vecMax = _mm_max_epu16(*vInput1, *vInput2);
1190 vecTmp = _mm_alignr_epi8(vecTmp, vecTmp, 2);
1191 *vecMin = _mm_min_epu16(vecTmp, *vecMax);
1192 *vecMax = _mm_max_epu16(vecTmp, *vecMax);
1193 vecTmp = _mm_alignr_epi8(*vecMin, *vecMin, 2);
1194 *vecMin = _mm_min_epu16(vecTmp, *vecMax);
1195 *vecMax = _mm_max_epu16(vecTmp, *vecMax);
1196 vecTmp = _mm_alignr_epi8(*vecMin, *vecMin, 2);
1197 *vecMin = _mm_min_epu16(vecTmp, *vecMax);
1198 *vecMax = _mm_max_epu16(vecTmp, *vecMax);
1199 vecTmp = _mm_alignr_epi8(*vecMin, *vecMin, 2);
1200 *vecMin = _mm_min_epu16(vecTmp, *vecMax);
1201 *vecMax = _mm_max_epu16(vecTmp, *vecMax);
1202 vecTmp = _mm_alignr_epi8(*vecMin, *vecMin, 2);
1203 *vecMin = _mm_min_epu16(vecTmp, *vecMax);
1204 *vecMax = _mm_max_epu16(vecTmp, *vecMax);
1205 vecTmp = _mm_alignr_epi8(*vecMin, *vecMin, 2);
1206 *vecMin = _mm_min_epu16(vecTmp, *vecMax);
1207 *vecMax = _mm_max_epu16(vecTmp, *vecMax);
1208 vecTmp = _mm_alignr_epi8(*vecMin, *vecMin, 2);
1209 *vecMin = _mm_min_epu16(vecTmp, *vecMax);
1210 *vecMax = _mm_max_epu16(vecTmp, *vecMax);
1211 *vecMin = _mm_alignr_epi8(*vecMin, *vecMin, 2);
1212 }
1213
1214 // used by store_unique, generated by simdunion.py
1215 static uint8_t uniqshuf[] = {
1216 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb,
1217 0xc, 0xd, 0xe, 0xf, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9,
1218 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0xFF, 0xFF, 0x0, 0x1, 0x4, 0x5,
1219 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0xFF, 0xFF,
1220 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
1221 0xFF, 0xFF, 0xFF, 0xFF, 0x0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 0x3, 0x4, 0x5, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1549 0xFF, 0xFF, 0xFF, 0xFF, 0x0, 0x1, 0x4, 0x5, 0xFF, 0xFF, 0xFF, 0xFF,
1550 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x4, 0x5, 0xFF, 0xFF,
1551 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1552 0x0, 0x1, 0x2, 0x3, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1553 0xFF, 0xFF, 0xFF, 0xFF, 0x2, 0x3, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1554 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0, 0x1, 0xFF, 0xFF,
1555 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1556 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1557 0xFF, 0xFF, 0xFF, 0xFF};
1558
1559 // write vector new, while omitting repeated values assuming that previously
1560 // written vector was "old"
1561 static inline int store_unique(__m128i old, __m128i newval, uint16_t *output) {
1562 __m128i vecTmp = _mm_alignr_epi8(newval, old, 16 - 2);
1563 // lots of high latency instructions follow (optimize?)
1564 int M = _mm_movemask_epi8(
1565 _mm_packs_epi16(_mm_cmpeq_epi16(vecTmp, newval), _mm_setzero_si128()));
1566 int numberofnewvalues = 8 - _mm_popcnt_u32(M);
1567 __m128i key = _mm_lddqu_si128((const __m128i *)uniqshuf + M);
1568 __m128i val = _mm_shuffle_epi8(newval, key);
1569 _mm_storeu_si128((__m128i *)output, val);
1570 return numberofnewvalues;
1571 }
1572
1573 // working in-place, this function overwrites the repeated values
1574 // could be avoided?
1575 static inline uint32_t unique(uint16_t *out, uint32_t len) {
1576 uint32_t pos = 1;
1577 for (uint32_t i = 1; i < len; ++i) {
1578 if (out[i] != out[i - 1]) {
1579 out[pos++] = out[i];
1580 }
1581 }
1582 return pos;
1583 }
1584
1585 // use with qsort, could be avoided
1586 static int uint16_compare(const void *a, const void *b) {
1587 return (*(uint16_t *)a - *(uint16_t *)b);
1588 }
1589
1590 // a one-pass SSE union algorithm
1591 // This function may not be safe if array1 == output or array2 == output.
1592 uint32_t union_vector16(const uint16_t *__restrict__ array1, uint32_t length1,
1593 const uint16_t *__restrict__ array2, uint32_t length2,
1594 uint16_t *__restrict__ output) {
1595 if ((length1 < 8) || (length2 < 8)) {
1596 return (uint32_t)union_uint16(array1, length1, array2, length2, output);
1597 }
1598 __m128i vA, vB, V, vecMin, vecMax;
1599 __m128i laststore;
1600 uint16_t *initoutput = output;
1601 uint32_t len1 = length1 / 8;
1602 uint32_t len2 = length2 / 8;
1603 uint32_t pos1 = 0;
1604 uint32_t pos2 = 0;
1605 // we start the machine
1606 vA = _mm_lddqu_si128((const __m128i *)array1 + pos1);
1607 pos1++;
1608 vB = _mm_lddqu_si128((const __m128i *)array2 + pos2);
1609 pos2++;
1610 sse_merge(&vA, &vB, &vecMin, &vecMax);
1611 laststore = _mm_set1_epi16(-1);
1612 output += store_unique(laststore, vecMin, output);
1613 laststore = vecMin;
1614 if ((pos1 < len1) && (pos2 < len2)) {
1615 uint16_t curA, curB;
1616 curA = array1[8 * pos1];
1617 curB = array2[8 * pos2];
1618 while (true) {
1619 if (curA <= curB) {
1620 V = _mm_lddqu_si128((const __m128i *)array1 + pos1);
1621 pos1++;
1622 if (pos1 < len1) {
1623 curA = array1[8 * pos1];
1624 } else {
1625 break;
1626 }
1627 } else {
1628 V = _mm_lddqu_si128((const __m128i *)array2 + pos2);
1629 pos2++;
1630 if (pos2 < len2) {
1631 curB = array2[8 * pos2];
1632 } else {
1633 break;
1634 }
1635 }
1636 sse_merge(&V, &vecMax, &vecMin, &vecMax);
1637 output += store_unique(laststore, vecMin, output);
1638 laststore = vecMin;
1639 }
1640 sse_merge(&V, &vecMax, &vecMin, &vecMax);
1641 output += store_unique(laststore, vecMin, output);
1642 laststore = vecMin;
1643 }
1644 // we finish the rest off using a scalar algorithm
1645 // could be improved?
1646 //
1647 // copy the small end on a tmp buffer
1648 uint32_t len = (uint32_t)(output - initoutput);
1649 uint16_t buffer[16];
1650 uint32_t leftoversize = store_unique(laststore, vecMax, buffer);
1651 if (pos1 == len1) {
1652 memcpy(buffer + leftoversize, array1 + 8 * pos1,
1653 (length1 - 8 * len1) * sizeof(uint16_t));
1654 leftoversize += length1 - 8 * len1;
1655 qsort(buffer, leftoversize, sizeof(uint16_t), uint16_compare);
1656
1657 leftoversize = unique(buffer, leftoversize);
1658 len += (uint32_t)union_uint16(buffer, leftoversize, array2 + 8 * pos2,
1659 length2 - 8 * pos2, output);
1660 } else {
1661 memcpy(buffer + leftoversize, array2 + 8 * pos2,
1662 (length2 - 8 * len2) * sizeof(uint16_t));
1663 leftoversize += length2 - 8 * len2;
1664 qsort(buffer, leftoversize, sizeof(uint16_t), uint16_compare);
1665 leftoversize = unique(buffer, leftoversize);
1666 len += (uint32_t)union_uint16(buffer, leftoversize, array1 + 8 * pos1,
1667 length1 - 8 * pos1, output);
1668 }
1669 return len;
1670 }
1671
1672 /**
1673 * End of the SIMD 16-bit union code
1674 *
1675 */
1676
1677 /**
1678 * Start of SIMD 16-bit XOR code
1679 */
1680
1681 // write vector new, while omitting repeated values assuming that previously
1682 // written vector was "old"
1683 static inline int store_unique_xor(__m128i old, __m128i newval,
1684 uint16_t *output) {
1685 __m128i vecTmp1 = _mm_alignr_epi8(newval, old, 16 - 4);
1686 __m128i vecTmp2 = _mm_alignr_epi8(newval, old, 16 - 2);
1687 __m128i equalleft = _mm_cmpeq_epi16(vecTmp2, vecTmp1);
1688 __m128i equalright = _mm_cmpeq_epi16(vecTmp2, newval);
1689 __m128i equalleftoright = _mm_or_si128(equalleft, equalright);
1690 int M = _mm_movemask_epi8(
1691 _mm_packs_epi16(equalleftoright, _mm_setzero_si128()));
1692 int numberofnewvalues = 8 - _mm_popcnt_u32(M);
1693 __m128i key = _mm_lddqu_si128((const __m128i *)uniqshuf + M);
1694 __m128i val = _mm_shuffle_epi8(vecTmp2, key);
1695 _mm_storeu_si128((__m128i *)output, val);
1696 return numberofnewvalues;
1697 }
1698
1699 // working in-place, this function overwrites the repeated values
1700 // could be avoided? Warning: assumes len > 0
1701 static inline uint32_t unique_xor(uint16_t *out, uint32_t len) {
1702 uint32_t pos = 1;
1703 for (uint32_t i = 1; i < len; ++i) {
1704 if (out[i] != out[i - 1]) {
1705 out[pos++] = out[i];
1706 } else
1707 pos--; // if it is identical to previous, delete it
1708 }
1709 return pos;
1710 }
1711
1712 // a one-pass SSE xor algorithm
1713 uint32_t xor_vector16(const uint16_t *__restrict__ array1, uint32_t length1,
1714 const uint16_t *__restrict__ array2, uint32_t length2,
1715 uint16_t *__restrict__ output) {
1716 if ((length1 < 8) || (length2 < 8)) {
1717 return xor_uint16(array1, length1, array2, length2, output);
1718 }
1719 __m128i vA, vB, V, vecMin, vecMax;
1720 __m128i laststore;
1721 uint16_t *initoutput = output;
1722 uint32_t len1 = length1 / 8;
1723 uint32_t len2 = length2 / 8;
1724 uint32_t pos1 = 0;
1725 uint32_t pos2 = 0;
1726 // we start the machine
1727 vA = _mm_lddqu_si128((const __m128i *)array1 + pos1);
1728 pos1++;
1729 vB = _mm_lddqu_si128((const __m128i *)array2 + pos2);
1730 pos2++;
1731 sse_merge(&vA, &vB, &vecMin, &vecMax);
1732 laststore = _mm_set1_epi16(-1);
1733 uint16_t buffer[17];
1734 output += store_unique_xor(laststore, vecMin, output);
1735
1736 laststore = vecMin;
1737 if ((pos1 < len1) && (pos2 < len2)) {
1738 uint16_t curA, curB;
1739 curA = array1[8 * pos1];
1740 curB = array2[8 * pos2];
1741 while (true) {
1742 if (curA <= curB) {
1743 V = _mm_lddqu_si128((const __m128i *)array1 + pos1);
1744 pos1++;
1745 if (pos1 < len1) {
1746 curA = array1[8 * pos1];
1747 } else {
1748 break;
1749 }
1750 } else {
1751 V = _mm_lddqu_si128((const __m128i *)array2 + pos2);
1752 pos2++;
1753 if (pos2 < len2) {
1754 curB = array2[8 * pos2];
1755 } else {
1756 break;
1757 }
1758 }
1759 sse_merge(&V, &vecMax, &vecMin, &vecMax);
1760 // conditionally stores the last value of laststore as well as all
1761 // but the
1762 // last value of vecMin
1763 output += store_unique_xor(laststore, vecMin, output);
1764 laststore = vecMin;
1765 }
1766 sse_merge(&V, &vecMax, &vecMin, &vecMax);
1767 // conditionally stores the last value of laststore as well as all but
1768 // the
1769 // last value of vecMin
1770 output += store_unique_xor(laststore, vecMin, output);
1771 laststore = vecMin;
1772 }
1773 uint32_t len = (uint32_t)(output - initoutput);
1774
1775 // we finish the rest off using a scalar algorithm
1776 // could be improved?
1777 // conditionally stores the last value of laststore as well as all but the
1778 // last value of vecMax,
1779 // we store to "buffer"
1780 int leftoversize = store_unique_xor(laststore, vecMax, buffer);
1781 uint16_t vec7 = _mm_extract_epi16(vecMax, 7);
1782 uint16_t vec6 = _mm_extract_epi16(vecMax, 6);
1783 if (vec7 != vec6) buffer[leftoversize++] = vec7;
1784 if (pos1 == len1) {
1785 memcpy(buffer + leftoversize, array1 + 8 * pos1,
1786 (length1 - 8 * len1) * sizeof(uint16_t));
1787 leftoversize += length1 - 8 * len1;
1788 if (leftoversize == 0) { // trivial case
1789 memcpy(output, array2 + 8 * pos2,
1790 (length2 - 8 * pos2) * sizeof(uint16_t));
1791 len += (length2 - 8 * pos2);
1792 } else {
1793 qsort(buffer, leftoversize, sizeof(uint16_t), uint16_compare);
1794 leftoversize = unique_xor(buffer, leftoversize);
1795 len += xor_uint16(buffer, leftoversize, array2 + 8 * pos2,
1796 length2 - 8 * pos2, output);
1797 }
1798 } else {
1799 memcpy(buffer + leftoversize, array2 + 8 * pos2,
1800 (length2 - 8 * len2) * sizeof(uint16_t));
1801 leftoversize += length2 - 8 * len2;
1802 if (leftoversize == 0) { // trivial case
1803 memcpy(output, array1 + 8 * pos1,
1804 (length1 - 8 * pos1) * sizeof(uint16_t));
1805 len += (length1 - 8 * pos1);
1806 } else {
1807 qsort(buffer, leftoversize, sizeof(uint16_t), uint16_compare);
1808 leftoversize = unique_xor(buffer, leftoversize);
1809 len += xor_uint16(buffer, leftoversize, array1 + 8 * pos1,
1810 length1 - 8 * pos1, output);
1811 }
1812 }
1813 return len;
1814 }
1815
1816 /**
1817 * End of SIMD 16-bit XOR code
1818 */
1819
1820 #endif // USESSE4
1821
1822 size_t union_uint32(const uint32_t *set_1, size_t size_1, const uint32_t *set_2,
1823 size_t size_2, uint32_t *buffer) {
1824 size_t pos = 0, idx_1 = 0, idx_2 = 0;
1825
1826 if (0 == size_2) {
1827 memmove(buffer, set_1, size_1 * sizeof(uint32_t));
1828 return size_1;
1829 }
1830 if (0 == size_1) {
1831 memmove(buffer, set_2, size_2 * sizeof(uint32_t));
1832 return size_2;
1833 }
1834
1835 uint32_t val_1 = set_1[idx_1], val_2 = set_2[idx_2];
1836
1837 while (true) {
1838 if (val_1 < val_2) {
1839 buffer[pos++] = val_1;
1840 ++idx_1;
1841 if (idx_1 >= size_1) break;
1842 val_1 = set_1[idx_1];
1843 } else if (val_2 < val_1) {
1844 buffer[pos++] = val_2;
1845 ++idx_2;
1846 if (idx_2 >= size_2) break;
1847 val_2 = set_2[idx_2];
1848 } else {
1849 buffer[pos++] = val_1;
1850 ++idx_1;
1851 ++idx_2;
1852 if (idx_1 >= size_1 || idx_2 >= size_2) break;
1853 val_1 = set_1[idx_1];
1854 val_2 = set_2[idx_2];
1855 }
1856 }
1857
1858 if (idx_1 < size_1) {
1859 const size_t n_elems = size_1 - idx_1;
1860 memmove(buffer + pos, set_1 + idx_1, n_elems * sizeof(uint32_t));
1861 pos += n_elems;
1862 } else if (idx_2 < size_2) {
1863 const size_t n_elems = size_2 - idx_2;
1864 memmove(buffer + pos, set_2 + idx_2, n_elems * sizeof(uint32_t));
1865 pos += n_elems;
1866 }
1867
1868 return pos;
1869 }
1870
1871 size_t union_uint32_card(const uint32_t *set_1, size_t size_1,
1872 const uint32_t *set_2, size_t size_2) {
1873 size_t pos = 0, idx_1 = 0, idx_2 = 0;
1874
1875 if (0 == size_2) {
1876 return size_1;
1877 }
1878 if (0 == size_1) {
1879 return size_2;
1880 }
1881
1882 uint32_t val_1 = set_1[idx_1], val_2 = set_2[idx_2];
1883
1884 while (true) {
1885 if (val_1 < val_2) {
1886 ++idx_1;
1887 ++pos;
1888 if (idx_1 >= size_1) break;
1889 val_1 = set_1[idx_1];
1890 } else if (val_2 < val_1) {
1891 ++idx_2;
1892 ++pos;
1893 if (idx_2 >= size_2) break;
1894 val_2 = set_2[idx_2];
1895 } else {
1896 ++idx_1;
1897 ++idx_2;
1898 ++pos;
1899 if (idx_1 >= size_1 || idx_2 >= size_2) break;
1900 val_1 = set_1[idx_1];
1901 val_2 = set_2[idx_2];
1902 }
1903 }
1904
1905 if (idx_1 < size_1) {
1906 const size_t n_elems = size_1 - idx_1;
1907 pos += n_elems;
1908 } else if (idx_2 < size_2) {
1909 const size_t n_elems = size_2 - idx_2;
1910 pos += n_elems;
1911 }
1912 return pos;
1913 }
1914
1915
1916
1917 size_t fast_union_uint16(const uint16_t *set_1, size_t size_1, const uint16_t *set_2,
1918 size_t size_2, uint16_t *buffer) {
1919 #ifdef ROARING_VECTOR_OPERATIONS_ENABLED
1920 // compute union with smallest array first
1921 if (size_1 < size_2) {
1922 return union_vector16(set_1, (uint32_t)size_1,
1923 set_2, (uint32_t)size_2, buffer);
1924 } else {
1925 return union_vector16(set_2, (uint32_t)size_2,
1926 set_1, (uint32_t)size_1, buffer);
1927 }
1928 #else
1929 // compute union with smallest array first
1930 if (size_1 < size_2) {
1931 return union_uint16(
1932 set_1, size_1, set_2, size_2, buffer);
1933 } else {
1934 return union_uint16(
1935 set_2, size_2, set_1, size_1, buffer);
1936 }
1937 #endif
1938 }
1939
1940 bool memequals(const void *s1, const void *s2, size_t n) {
1941 if (n == 0) {
1942 return true;
1943 }
1944 #ifdef USEAVX
1945 const uint8_t *ptr1 = (const uint8_t *)s1;
1946 const uint8_t *ptr2 = (const uint8_t *)s2;
1947 const uint8_t *end1 = ptr1 + n;
1948 const uint8_t *end8 = ptr1 + n/8*8;
1949 const uint8_t *end32 = ptr1 + n/32*32;
1950
1951 while (ptr1 < end32) {
1952 __m256i r1 = _mm256_loadu_si256((const __m256i*)ptr1);
1953 __m256i r2 = _mm256_loadu_si256((const __m256i*)ptr2);
1954 int mask = _mm256_movemask_epi8(_mm256_cmpeq_epi8(r1, r2));
1955 if ((uint32_t)mask != UINT32_MAX) {
1956 return false;
1957 }
1958 ptr1 += 32;
1959 ptr2 += 32;
1960 }
1961
1962 while (ptr1 < end8) {
1963 uint64_t v1 = *((const uint64_t*)ptr1);
1964 uint64_t v2 = *((const uint64_t*)ptr2);
1965 if (v1 != v2) {
1966 return false;
1967 }
1968 ptr1 += 8;
1969 ptr2 += 8;
1970 }
1971
1972 while (ptr1 < end1) {
1973 if (*ptr1 != *ptr2) {
1974 return false;
1975 }
1976 ptr1++;
1977 ptr2++;
1978 }
1979
1980 return true;
1981 #else
1982 return memcmp(s1, s2, n) == 0;
1983 #endif
1984 }
1985 /* end file src/array_util.c */
1986 /* begin file src/bitset_util.c */
1987 #include <assert.h>
1988 #include <stdint.h>
1989 #include <stdio.h>
1990 #include <stdlib.h>
1991 #include <string.h>
1992
1993
1994 #ifdef IS_X64
1995 static uint8_t lengthTable[256] = {
1996 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4,
1997 2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
1998 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 1, 2, 2, 3, 2, 3, 3, 4,
1999 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
2000 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6,
2001 4, 5, 5, 6, 5, 6, 6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2002 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5,
2003 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
2004 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6,
2005 4, 5, 5, 6, 5, 6, 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
2006 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8};
2007 #endif
2008
2009 #ifdef USEAVX
2010 ALIGNED(32)
2011 static uint32_t vecDecodeTable[256][8] = {
2012 {0, 0, 0, 0, 0, 0, 0, 0}, /* 0x00 (00000000) */
2013 {1, 0, 0, 0, 0, 0, 0, 0}, /* 0x01 (00000001) */
2014 {2, 0, 0, 0, 0, 0, 0, 0}, /* 0x02 (00000010) */
2015 {1, 2, 0, 0, 0, 0, 0, 0}, /* 0x03 (00000011) */
2016 {3, 0, 0, 0, 0, 0, 0, 0}, /* 0x04 (00000100) */
2017 {1, 3, 0, 0, 0, 0, 0, 0}, /* 0x05 (00000101) */
2018 {2, 3, 0, 0, 0, 0, 0, 0}, /* 0x06 (00000110) */
2019 {1, 2, 3, 0, 0, 0, 0, 0}, /* 0x07 (00000111) */
2020 {4, 0, 0, 0, 0, 0, 0, 0}, /* 0x08 (00001000) */
2021 {1, 4, 0, 0, 0, 0, 0, 0}, /* 0x09 (00001001) */
2022 {2, 4, 0, 0, 0, 0, 0, 0}, /* 0x0A (00001010) */
2023 {1, 2, 4, 0, 0, 0, 0, 0}, /* 0x0B (00001011) */
2024 {3, 4, 0, 0, 0, 0, 0, 0}, /* 0x0C (00001100) */
2025 {1, 3, 4, 0, 0, 0, 0, 0}, /* 0x0D (00001101) */
2026 {2, 3, 4, 0, 0, 0, 0, 0}, /* 0x0E (00001110) */
2027 {1, 2, 3, 4, 0, 0, 0, 0}, /* 0x0F (00001111) */
2028 {5, 0, 0, 0, 0, 0, 0, 0}, /* 0x10 (00010000) */
2029 {1, 5, 0, 0, 0, 0, 0, 0}, /* 0x11 (00010001) */
2030 {2, 5, 0, 0, 0, 0, 0, 0}, /* 0x12 (00010010) */
2031 {1, 2, 5, 0, 0, 0, 0, 0}, /* 0x13 (00010011) */
2032 {3, 5, 0, 0, 0, 0, 0, 0}, /* 0x14 (00010100) */
2033 {1, 3, 5, 0, 0, 0, 0, 0}, /* 0x15 (00010101) */
2034 {2, 3, 5, 0, 0, 0, 0, 0}, /* 0x16 (00010110) */
2035 {1, 2, 3, 5, 0, 0, 0, 0}, /* 0x17 (00010111) */
2036 {4, 5, 0, 0, 0, 0, 0, 0}, /* 0x18 (00011000) */
2037 {1, 4, 5, 0, 0, 0, 0, 0}, /* 0x19 (00011001) */
2038 {2, 4, 5, 0, 0, 0, 0, 0}, /* 0x1A (00011010) */
2039 {1, 2, 4, 5, 0, 0, 0, 0}, /* 0x1B (00011011) */
2040 {3, 4, 5, 0, 0, 0, 0, 0}, /* 0x1C (00011100) */
2041 {1, 3, 4, 5, 0, 0, 0, 0}, /* 0x1D (00011101) */
2042 {2, 3, 4, 5, 0, 0, 0, 0}, /* 0x1E (00011110) */
2043 {1, 2, 3, 4, 5, 0, 0, 0}, /* 0x1F (00011111) */
2044 {6, 0, 0, 0, 0, 0, 0, 0}, /* 0x20 (00100000) */
2045 {1, 6, 0, 0, 0, 0, 0, 0}, /* 0x21 (00100001) */
2046 {2, 6, 0, 0, 0, 0, 0, 0}, /* 0x22 (00100010) */
2047 {1, 2, 6, 0, 0, 0, 0, 0}, /* 0x23 (00100011) */
2048 {3, 6, 0, 0, 0, 0, 0, 0}, /* 0x24 (00100100) */
2049 {1, 3, 6, 0, 0, 0, 0, 0}, /* 0x25 (00100101) */
2050 {2, 3, 6, 0, 0, 0, 0, 0}, /* 0x26 (00100110) */
2051 {1, 2, 3, 6, 0, 0, 0, 0}, /* 0x27 (00100111) */
2052 {4, 6, 0, 0, 0, 0, 0, 0}, /* 0x28 (00101000) */
2053 {1, 4, 6, 0, 0, 0, 0, 0}, /* 0x29 (00101001) */
2054 {2, 4, 6, 0, 0, 0, 0, 0}, /* 0x2A (00101010) */
2055 {1, 2, 4, 6, 0, 0, 0, 0}, /* 0x2B (00101011) */
2056 {3, 4, 6, 0, 0, 0, 0, 0}, /* 0x2C (00101100) */
2057 {1, 3, 4, 6, 0, 0, 0, 0}, /* 0x2D (00101101) */
2058 {2, 3, 4, 6, 0, 0, 0, 0}, /* 0x2E (00101110) */
2059 {1, 2, 3, 4, 6, 0, 0, 0}, /* 0x2F (00101111) */
2060 {5, 6, 0, 0, 0, 0, 0, 0}, /* 0x30 (00110000) */
2061 {1, 5, 6, 0, 0, 0, 0, 0}, /* 0x31 (00110001) */
2062 {2, 5, 6, 0, 0, 0, 0, 0}, /* 0x32 (00110010) */
2063 {1, 2, 5, 6, 0, 0, 0, 0}, /* 0x33 (00110011) */
2064 {3, 5, 6, 0, 0, 0, 0, 0}, /* 0x34 (00110100) */
2065 {1, 3, 5, 6, 0, 0, 0, 0}, /* 0x35 (00110101) */
2066 {2, 3, 5, 6, 0, 0, 0, 0}, /* 0x36 (00110110) */
2067 {1, 2, 3, 5, 6, 0, 0, 0}, /* 0x37 (00110111) */
2068 {4, 5, 6, 0, 0, 0, 0, 0}, /* 0x38 (00111000) */
2069 {1, 4, 5, 6, 0, 0, 0, 0}, /* 0x39 (00111001) */
2070 {2, 4, 5, 6, 0, 0, 0, 0}, /* 0x3A (00111010) */
2071 {1, 2, 4, 5, 6, 0, 0, 0}, /* 0x3B (00111011) */
2072 {3, 4, 5, 6, 0, 0, 0, 0}, /* 0x3C (00111100) */
2073 {1, 3, 4, 5, 6, 0, 0, 0}, /* 0x3D (00111101) */
2074 {2, 3, 4, 5, 6, 0, 0, 0}, /* 0x3E (00111110) */
2075 {1, 2, 3, 4, 5, 6, 0, 0}, /* 0x3F (00111111) */
2076 {7, 0, 0, 0, 0, 0, 0, 0}, /* 0x40 (01000000) */
2077 {1, 7, 0, 0, 0, 0, 0, 0}, /* 0x41 (01000001) */
2078 {2, 7, 0, 0, 0, 0, 0, 0}, /* 0x42 (01000010) */
2079 {1, 2, 7, 0, 0, 0, 0, 0}, /* 0x43 (01000011) */
2080 {3, 7, 0, 0, 0, 0, 0, 0}, /* 0x44 (01000100) */
2081 {1, 3, 7, 0, 0, 0, 0, 0}, /* 0x45 (01000101) */
2082 {2, 3, 7, 0, 0, 0, 0, 0}, /* 0x46 (01000110) */
2083 {1, 2, 3, 7, 0, 0, 0, 0}, /* 0x47 (01000111) */
2084 {4, 7, 0, 0, 0, 0, 0, 0}, /* 0x48 (01001000) */
2085 {1, 4, 7, 0, 0, 0, 0, 0}, /* 0x49 (01001001) */
2086 {2, 4, 7, 0, 0, 0, 0, 0}, /* 0x4A (01001010) */
2087 {1, 2, 4, 7, 0, 0, 0, 0}, /* 0x4B (01001011) */
2088 {3, 4, 7, 0, 0, 0, 0, 0}, /* 0x4C (01001100) */
2089 {1, 3, 4, 7, 0, 0, 0, 0}, /* 0x4D (01001101) */
2090 {2, 3, 4, 7, 0, 0, 0, 0}, /* 0x4E (01001110) */
2091 {1, 2, 3, 4, 7, 0, 0, 0}, /* 0x4F (01001111) */
2092 {5, 7, 0, 0, 0, 0, 0, 0}, /* 0x50 (01010000) */
2093 {1, 5, 7, 0, 0, 0, 0, 0}, /* 0x51 (01010001) */
2094 {2, 5, 7, 0, 0, 0, 0, 0}, /* 0x52 (01010010) */
2095 {1, 2, 5, 7, 0, 0, 0, 0}, /* 0x53 (01010011) */
2096 {3, 5, 7, 0, 0, 0, 0, 0}, /* 0x54 (01010100) */
2097 {1, 3, 5, 7, 0, 0, 0, 0}, /* 0x55 (01010101) */
2098 {2, 3, 5, 7, 0, 0, 0, 0}, /* 0x56 (01010110) */
2099 {1, 2, 3, 5, 7, 0, 0, 0}, /* 0x57 (01010111) */
2100 {4, 5, 7, 0, 0, 0, 0, 0}, /* 0x58 (01011000) */
2101 {1, 4, 5, 7, 0, 0, 0, 0}, /* 0x59 (01011001) */
2102 {2, 4, 5, 7, 0, 0, 0, 0}, /* 0x5A (01011010) */
2103 {1, 2, 4, 5, 7, 0, 0, 0}, /* 0x5B (01011011) */
2104 {3, 4, 5, 7, 0, 0, 0, 0}, /* 0x5C (01011100) */
2105 {1, 3, 4, 5, 7, 0, 0, 0}, /* 0x5D (01011101) */
2106 {2, 3, 4, 5, 7, 0, 0, 0}, /* 0x5E (01011110) */
2107 {1, 2, 3, 4, 5, 7, 0, 0}, /* 0x5F (01011111) */
2108 {6, 7, 0, 0, 0, 0, 0, 0}, /* 0x60 (01100000) */
2109 {1, 6, 7, 0, 0, 0, 0, 0}, /* 0x61 (01100001) */
2110 {2, 6, 7, 0, 0, 0, 0, 0}, /* 0x62 (01100010) */
2111 {1, 2, 6, 7, 0, 0, 0, 0}, /* 0x63 (01100011) */
2112 {3, 6, 7, 0, 0, 0, 0, 0}, /* 0x64 (01100100) */
2113 {1, 3, 6, 7, 0, 0, 0, 0}, /* 0x65 (01100101) */
2114 {2, 3, 6, 7, 0, 0, 0, 0}, /* 0x66 (01100110) */
2115 {1, 2, 3, 6, 7, 0, 0, 0}, /* 0x67 (01100111) */
2116 {4, 6, 7, 0, 0, 0, 0, 0}, /* 0x68 (01101000) */
2117 {1, 4, 6, 7, 0, 0, 0, 0}, /* 0x69 (01101001) */
2118 {2, 4, 6, 7, 0, 0, 0, 0}, /* 0x6A (01101010) */
2119 {1, 2, 4, 6, 7, 0, 0, 0}, /* 0x6B (01101011) */
2120 {3, 4, 6, 7, 0, 0, 0, 0}, /* 0x6C (01101100) */
2121 {1, 3, 4, 6, 7, 0, 0, 0}, /* 0x6D (01101101) */
2122 {2, 3, 4, 6, 7, 0, 0, 0}, /* 0x6E (01101110) */
2123 {1, 2, 3, 4, 6, 7, 0, 0}, /* 0x6F (01101111) */
2124 {5, 6, 7, 0, 0, 0, 0, 0}, /* 0x70 (01110000) */
2125 {1, 5, 6, 7, 0, 0, 0, 0}, /* 0x71 (01110001) */
2126 {2, 5, 6, 7, 0, 0, 0, 0}, /* 0x72 (01110010) */
2127 {1, 2, 5, 6, 7, 0, 0, 0}, /* 0x73 (01110011) */
2128 {3, 5, 6, 7, 0, 0, 0, 0}, /* 0x74 (01110100) */
2129 {1, 3, 5, 6, 7, 0, 0, 0}, /* 0x75 (01110101) */
2130 {2, 3, 5, 6, 7, 0, 0, 0}, /* 0x76 (01110110) */
2131 {1, 2, 3, 5, 6, 7, 0, 0}, /* 0x77 (01110111) */
2132 {4, 5, 6, 7, 0, 0, 0, 0}, /* 0x78 (01111000) */
2133 {1, 4, 5, 6, 7, 0, 0, 0}, /* 0x79 (01111001) */
2134 {2, 4, 5, 6, 7, 0, 0, 0}, /* 0x7A (01111010) */
2135 {1, 2, 4, 5, 6, 7, 0, 0}, /* 0x7B (01111011) */
2136 {3, 4, 5, 6, 7, 0, 0, 0}, /* 0x7C (01111100) */
2137 {1, 3, 4, 5, 6, 7, 0, 0}, /* 0x7D (01111101) */
2138 {2, 3, 4, 5, 6, 7, 0, 0}, /* 0x7E (01111110) */
2139 {1, 2, 3, 4, 5, 6, 7, 0}, /* 0x7F (01111111) */
2140 {8, 0, 0, 0, 0, 0, 0, 0}, /* 0x80 (10000000) */
2141 {1, 8, 0, 0, 0, 0, 0, 0}, /* 0x81 (10000001) */
2142 {2, 8, 0, 0, 0, 0, 0, 0}, /* 0x82 (10000010) */
2143 {1, 2, 8, 0, 0, 0, 0, 0}, /* 0x83 (10000011) */
2144 {3, 8, 0, 0, 0, 0, 0, 0}, /* 0x84 (10000100) */
2145 {1, 3, 8, 0, 0, 0, 0, 0}, /* 0x85 (10000101) */
2146 {2, 3, 8, 0, 0, 0, 0, 0}, /* 0x86 (10000110) */
2147 {1, 2, 3, 8, 0, 0, 0, 0}, /* 0x87 (10000111) */
2148 {4, 8, 0, 0, 0, 0, 0, 0}, /* 0x88 (10001000) */
2149 {1, 4, 8, 0, 0, 0, 0, 0}, /* 0x89 (10001001) */
2150 {2, 4, 8, 0, 0, 0, 0, 0}, /* 0x8A (10001010) */
2151 {1, 2, 4, 8, 0, 0, 0, 0}, /* 0x8B (10001011) */
2152 {3, 4, 8, 0, 0, 0, 0, 0}, /* 0x8C (10001100) */
2153 {1, 3, 4, 8, 0, 0, 0, 0}, /* 0x8D (10001101) */
2154 {2, 3, 4, 8, 0, 0, 0, 0}, /* 0x8E (10001110) */
2155 {1, 2, 3, 4, 8, 0, 0, 0}, /* 0x8F (10001111) */
2156 {5, 8, 0, 0, 0, 0, 0, 0}, /* 0x90 (10010000) */
2157 {1, 5, 8, 0, 0, 0, 0, 0}, /* 0x91 (10010001) */
2158 {2, 5, 8, 0, 0, 0, 0, 0}, /* 0x92 (10010010) */
2159 {1, 2, 5, 8, 0, 0, 0, 0}, /* 0x93 (10010011) */
2160 {3, 5, 8, 0, 0, 0, 0, 0}, /* 0x94 (10010100) */
2161 {1, 3, 5, 8, 0, 0, 0, 0}, /* 0x95 (10010101) */
2162 {2, 3, 5, 8, 0, 0, 0, 0}, /* 0x96 (10010110) */
2163 {1, 2, 3, 5, 8, 0, 0, 0}, /* 0x97 (10010111) */
2164 {4, 5, 8, 0, 0, 0, 0, 0}, /* 0x98 (10011000) */
2165 {1, 4, 5, 8, 0, 0, 0, 0}, /* 0x99 (10011001) */
2166 {2, 4, 5, 8, 0, 0, 0, 0}, /* 0x9A (10011010) */
2167 {1, 2, 4, 5, 8, 0, 0, 0}, /* 0x9B (10011011) */
2168 {3, 4, 5, 8, 0, 0, 0, 0}, /* 0x9C (10011100) */
2169 {1, 3, 4, 5, 8, 0, 0, 0}, /* 0x9D (10011101) */
2170 {2, 3, 4, 5, 8, 0, 0, 0}, /* 0x9E (10011110) */
2171 {1, 2, 3, 4, 5, 8, 0, 0}, /* 0x9F (10011111) */
2172 {6, 8, 0, 0, 0, 0, 0, 0}, /* 0xA0 (10100000) */
2173 {1, 6, 8, 0, 0, 0, 0, 0}, /* 0xA1 (10100001) */
2174 {2, 6, 8, 0, 0, 0, 0, 0}, /* 0xA2 (10100010) */
2175 {1, 2, 6, 8, 0, 0, 0, 0}, /* 0xA3 (10100011) */
2176 {3, 6, 8, 0, 0, 0, 0, 0}, /* 0xA4 (10100100) */
2177 {1, 3, 6, 8, 0, 0, 0, 0}, /* 0xA5 (10100101) */
2178 {2, 3, 6, 8, 0, 0, 0, 0}, /* 0xA6 (10100110) */
2179 {1, 2, 3, 6, 8, 0, 0, 0}, /* 0xA7 (10100111) */
2180 {4, 6, 8, 0, 0, 0, 0, 0}, /* 0xA8 (10101000) */
2181 {1, 4, 6, 8, 0, 0, 0, 0}, /* 0xA9 (10101001) */
2182 {2, 4, 6, 8, 0, 0, 0, 0}, /* 0xAA (10101010) */
2183 {1, 2, 4, 6, 8, 0, 0, 0}, /* 0xAB (10101011) */
2184 {3, 4, 6, 8, 0, 0, 0, 0}, /* 0xAC (10101100) */
2185 {1, 3, 4, 6, 8, 0, 0, 0}, /* 0xAD (10101101) */
2186 {2, 3, 4, 6, 8, 0, 0, 0}, /* 0xAE (10101110) */
2187 {1, 2, 3, 4, 6, 8, 0, 0}, /* 0xAF (10101111) */
2188 {5, 6, 8, 0, 0, 0, 0, 0}, /* 0xB0 (10110000) */
2189 {1, 5, 6, 8, 0, 0, 0, 0}, /* 0xB1 (10110001) */
2190 {2, 5, 6, 8, 0, 0, 0, 0}, /* 0xB2 (10110010) */
2191 {1, 2, 5, 6, 8, 0, 0, 0}, /* 0xB3 (10110011) */
2192 {3, 5, 6, 8, 0, 0, 0, 0}, /* 0xB4 (10110100) */
2193 {1, 3, 5, 6, 8, 0, 0, 0}, /* 0xB5 (10110101) */
2194 {2, 3, 5, 6, 8, 0, 0, 0}, /* 0xB6 (10110110) */
2195 {1, 2, 3, 5, 6, 8, 0, 0}, /* 0xB7 (10110111) */
2196 {4, 5, 6, 8, 0, 0, 0, 0}, /* 0xB8 (10111000) */
2197 {1, 4, 5, 6, 8, 0, 0, 0}, /* 0xB9 (10111001) */
2198 {2, 4, 5, 6, 8, 0, 0, 0}, /* 0xBA (10111010) */
2199 {1, 2, 4, 5, 6, 8, 0, 0}, /* 0xBB (10111011) */
2200 {3, 4, 5, 6, 8, 0, 0, 0}, /* 0xBC (10111100) */
2201 {1, 3, 4, 5, 6, 8, 0, 0}, /* 0xBD (10111101) */
2202 {2, 3, 4, 5, 6, 8, 0, 0}, /* 0xBE (10111110) */
2203 {1, 2, 3, 4, 5, 6, 8, 0}, /* 0xBF (10111111) */
2204 {7, 8, 0, 0, 0, 0, 0, 0}, /* 0xC0 (11000000) */
2205 {1, 7, 8, 0, 0, 0, 0, 0}, /* 0xC1 (11000001) */
2206 {2, 7, 8, 0, 0, 0, 0, 0}, /* 0xC2 (11000010) */
2207 {1, 2, 7, 8, 0, 0, 0, 0}, /* 0xC3 (11000011) */
2208 {3, 7, 8, 0, 0, 0, 0, 0}, /* 0xC4 (11000100) */
2209 {1, 3, 7, 8, 0, 0, 0, 0}, /* 0xC5 (11000101) */
2210 {2, 3, 7, 8, 0, 0, 0, 0}, /* 0xC6 (11000110) */
2211 {1, 2, 3, 7, 8, 0, 0, 0}, /* 0xC7 (11000111) */
2212 {4, 7, 8, 0, 0, 0, 0, 0}, /* 0xC8 (11001000) */
2213 {1, 4, 7, 8, 0, 0, 0, 0}, /* 0xC9 (11001001) */
2214 {2, 4, 7, 8, 0, 0, 0, 0}, /* 0xCA (11001010) */
2215 {1, 2, 4, 7, 8, 0, 0, 0}, /* 0xCB (11001011) */
2216 {3, 4, 7, 8, 0, 0, 0, 0}, /* 0xCC (11001100) */
2217 {1, 3, 4, 7, 8, 0, 0, 0}, /* 0xCD (11001101) */
2218 {2, 3, 4, 7, 8, 0, 0, 0}, /* 0xCE (11001110) */
2219 {1, 2, 3, 4, 7, 8, 0, 0}, /* 0xCF (11001111) */
2220 {5, 7, 8, 0, 0, 0, 0, 0}, /* 0xD0 (11010000) */
2221 {1, 5, 7, 8, 0, 0, 0, 0}, /* 0xD1 (11010001) */
2222 {2, 5, 7, 8, 0, 0, 0, 0}, /* 0xD2 (11010010) */
2223 {1, 2, 5, 7, 8, 0, 0, 0}, /* 0xD3 (11010011) */
2224 {3, 5, 7, 8, 0, 0, 0, 0}, /* 0xD4 (11010100) */
2225 {1, 3, 5, 7, 8, 0, 0, 0}, /* 0xD5 (11010101) */
2226 {2, 3, 5, 7, 8, 0, 0, 0}, /* 0xD6 (11010110) */
2227 {1, 2, 3, 5, 7, 8, 0, 0}, /* 0xD7 (11010111) */
2228 {4, 5, 7, 8, 0, 0, 0, 0}, /* 0xD8 (11011000) */
2229 {1, 4, 5, 7, 8, 0, 0, 0}, /* 0xD9 (11011001) */
2230 {2, 4, 5, 7, 8, 0, 0, 0}, /* 0xDA (11011010) */
2231 {1, 2, 4, 5, 7, 8, 0, 0}, /* 0xDB (11011011) */
2232 {3, 4, 5, 7, 8, 0, 0, 0}, /* 0xDC (11011100) */
2233 {1, 3, 4, 5, 7, 8, 0, 0}, /* 0xDD (11011101) */
2234 {2, 3, 4, 5, 7, 8, 0, 0}, /* 0xDE (11011110) */
2235 {1, 2, 3, 4, 5, 7, 8, 0}, /* 0xDF (11011111) */
2236 {6, 7, 8, 0, 0, 0, 0, 0}, /* 0xE0 (11100000) */
2237 {1, 6, 7, 8, 0, 0, 0, 0}, /* 0xE1 (11100001) */
2238 {2, 6, 7, 8, 0, 0, 0, 0}, /* 0xE2 (11100010) */
2239 {1, 2, 6, 7, 8, 0, 0, 0}, /* 0xE3 (11100011) */
2240 {3, 6, 7, 8, 0, 0, 0, 0}, /* 0xE4 (11100100) */
2241 {1, 3, 6, 7, 8, 0, 0, 0}, /* 0xE5 (11100101) */
2242 {2, 3, 6, 7, 8, 0, 0, 0}, /* 0xE6 (11100110) */
2243 {1, 2, 3, 6, 7, 8, 0, 0}, /* 0xE7 (11100111) */
2244 {4, 6, 7, 8, 0, 0, 0, 0}, /* 0xE8 (11101000) */
2245 {1, 4, 6, 7, 8, 0, 0, 0}, /* 0xE9 (11101001) */
2246 {2, 4, 6, 7, 8, 0, 0, 0}, /* 0xEA (11101010) */
2247 {1, 2, 4, 6, 7, 8, 0, 0}, /* 0xEB (11101011) */
2248 {3, 4, 6, 7, 8, 0, 0, 0}, /* 0xEC (11101100) */
2249 {1, 3, 4, 6, 7, 8, 0, 0}, /* 0xED (11101101) */
2250 {2, 3, 4, 6, 7, 8, 0, 0}, /* 0xEE (11101110) */
2251 {1, 2, 3, 4, 6, 7, 8, 0}, /* 0xEF (11101111) */
2252 {5, 6, 7, 8, 0, 0, 0, 0}, /* 0xF0 (11110000) */
2253 {1, 5, 6, 7, 8, 0, 0, 0}, /* 0xF1 (11110001) */
2254 {2, 5, 6, 7, 8, 0, 0, 0}, /* 0xF2 (11110010) */
2255 {1, 2, 5, 6, 7, 8, 0, 0}, /* 0xF3 (11110011) */
2256 {3, 5, 6, 7, 8, 0, 0, 0}, /* 0xF4 (11110100) */
2257 {1, 3, 5, 6, 7, 8, 0, 0}, /* 0xF5 (11110101) */
2258 {2, 3, 5, 6, 7, 8, 0, 0}, /* 0xF6 (11110110) */
2259 {1, 2, 3, 5, 6, 7, 8, 0}, /* 0xF7 (11110111) */
2260 {4, 5, 6, 7, 8, 0, 0, 0}, /* 0xF8 (11111000) */
2261 {1, 4, 5, 6, 7, 8, 0, 0}, /* 0xF9 (11111001) */
2262 {2, 4, 5, 6, 7, 8, 0, 0}, /* 0xFA (11111010) */
2263 {1, 2, 4, 5, 6, 7, 8, 0}, /* 0xFB (11111011) */
2264 {3, 4, 5, 6, 7, 8, 0, 0}, /* 0xFC (11111100) */
2265 {1, 3, 4, 5, 6, 7, 8, 0}, /* 0xFD (11111101) */
2266 {2, 3, 4, 5, 6, 7, 8, 0}, /* 0xFE (11111110) */
2267 {1, 2, 3, 4, 5, 6, 7, 8} /* 0xFF (11111111) */
2268 };
2269
2270 #endif // #ifdef USEAVX
2271
2272 #ifdef IS_X64
2273 // same as vecDecodeTable but in 16 bits
2274 ALIGNED(32)
2275 static uint16_t vecDecodeTable_uint16[256][8] = {
2276 {0, 0, 0, 0, 0, 0, 0, 0}, /* 0x00 (00000000) */
2277 {1, 0, 0, 0, 0, 0, 0, 0}, /* 0x01 (00000001) */
2278 {2, 0, 0, 0, 0, 0, 0, 0}, /* 0x02 (00000010) */
2279 {1, 2, 0, 0, 0, 0, 0, 0}, /* 0x03 (00000011) */
2280 {3, 0, 0, 0, 0, 0, 0, 0}, /* 0x04 (00000100) */
2281 {1, 3, 0, 0, 0, 0, 0, 0}, /* 0x05 (00000101) */
2282 {2, 3, 0, 0, 0, 0, 0, 0}, /* 0x06 (00000110) */
2283 {1, 2, 3, 0, 0, 0, 0, 0}, /* 0x07 (00000111) */
2284 {4, 0, 0, 0, 0, 0, 0, 0}, /* 0x08 (00001000) */
2285 {1, 4, 0, 0, 0, 0, 0, 0}, /* 0x09 (00001001) */
2286 {2, 4, 0, 0, 0, 0, 0, 0}, /* 0x0A (00001010) */
2287 {1, 2, 4, 0, 0, 0, 0, 0}, /* 0x0B (00001011) */
2288 {3, 4, 0, 0, 0, 0, 0, 0}, /* 0x0C (00001100) */
2289 {1, 3, 4, 0, 0, 0, 0, 0}, /* 0x0D (00001101) */
2290 {2, 3, 4, 0, 0, 0, 0, 0}, /* 0x0E (00001110) */
2291 {1, 2, 3, 4, 0, 0, 0, 0}, /* 0x0F (00001111) */
2292 {5, 0, 0, 0, 0, 0, 0, 0}, /* 0x10 (00010000) */
2293 {1, 5, 0, 0, 0, 0, 0, 0}, /* 0x11 (00010001) */
2294 {2, 5, 0, 0, 0, 0, 0, 0}, /* 0x12 (00010010) */
2295 {1, 2, 5, 0, 0, 0, 0, 0}, /* 0x13 (00010011) */
2296 {3, 5, 0, 0, 0, 0, 0, 0}, /* 0x14 (00010100) */
2297 {1, 3, 5, 0, 0, 0, 0, 0}, /* 0x15 (00010101) */
2298 {2, 3, 5, 0, 0, 0, 0, 0}, /* 0x16 (00010110) */
2299 {1, 2, 3, 5, 0, 0, 0, 0}, /* 0x17 (00010111) */
2300 {4, 5, 0, 0, 0, 0, 0, 0}, /* 0x18 (00011000) */
2301 {1, 4, 5, 0, 0, 0, 0, 0}, /* 0x19 (00011001) */
2302 {2, 4, 5, 0, 0, 0, 0, 0}, /* 0x1A (00011010) */
2303 {1, 2, 4, 5, 0, 0, 0, 0}, /* 0x1B (00011011) */
2304 {3, 4, 5, 0, 0, 0, 0, 0}, /* 0x1C (00011100) */
2305 {1, 3, 4, 5, 0, 0, 0, 0}, /* 0x1D (00011101) */
2306 {2, 3, 4, 5, 0, 0, 0, 0}, /* 0x1E (00011110) */
2307 {1, 2, 3, 4, 5, 0, 0, 0}, /* 0x1F (00011111) */
2308 {6, 0, 0, 0, 0, 0, 0, 0}, /* 0x20 (00100000) */
2309 {1, 6, 0, 0, 0, 0, 0, 0}, /* 0x21 (00100001) */
2310 {2, 6, 0, 0, 0, 0, 0, 0}, /* 0x22 (00100010) */
2311 {1, 2, 6, 0, 0, 0, 0, 0}, /* 0x23 (00100011) */
2312 {3, 6, 0, 0, 0, 0, 0, 0}, /* 0x24 (00100100) */
2313 {1, 3, 6, 0, 0, 0, 0, 0}, /* 0x25 (00100101) */
2314 {2, 3, 6, 0, 0, 0, 0, 0}, /* 0x26 (00100110) */
2315 {1, 2, 3, 6, 0, 0, 0, 0}, /* 0x27 (00100111) */
2316 {4, 6, 0, 0, 0, 0, 0, 0}, /* 0x28 (00101000) */
2317 {1, 4, 6, 0, 0, 0, 0, 0}, /* 0x29 (00101001) */
2318 {2, 4, 6, 0, 0, 0, 0, 0}, /* 0x2A (00101010) */
2319 {1, 2, 4, 6, 0, 0, 0, 0}, /* 0x2B (00101011) */
2320 {3, 4, 6, 0, 0, 0, 0, 0}, /* 0x2C (00101100) */
2321 {1, 3, 4, 6, 0, 0, 0, 0}, /* 0x2D (00101101) */
2322 {2, 3, 4, 6, 0, 0, 0, 0}, /* 0x2E (00101110) */
2323 {1, 2, 3, 4, 6, 0, 0, 0}, /* 0x2F (00101111) */
2324 {5, 6, 0, 0, 0, 0, 0, 0}, /* 0x30 (00110000) */
2325 {1, 5, 6, 0, 0, 0, 0, 0}, /* 0x31 (00110001) */
2326 {2, 5, 6, 0, 0, 0, 0, 0}, /* 0x32 (00110010) */
2327 {1, 2, 5, 6, 0, 0, 0, 0}, /* 0x33 (00110011) */
2328 {3, 5, 6, 0, 0, 0, 0, 0}, /* 0x34 (00110100) */
2329 {1, 3, 5, 6, 0, 0, 0, 0}, /* 0x35 (00110101) */
2330 {2, 3, 5, 6, 0, 0, 0, 0}, /* 0x36 (00110110) */
2331 {1, 2, 3, 5, 6, 0, 0, 0}, /* 0x37 (00110111) */
2332 {4, 5, 6, 0, 0, 0, 0, 0}, /* 0x38 (00111000) */
2333 {1, 4, 5, 6, 0, 0, 0, 0}, /* 0x39 (00111001) */
2334 {2, 4, 5, 6, 0, 0, 0, 0}, /* 0x3A (00111010) */
2335 {1, 2, 4, 5, 6, 0, 0, 0}, /* 0x3B (00111011) */
2336 {3, 4, 5, 6, 0, 0, 0, 0}, /* 0x3C (00111100) */
2337 {1, 3, 4, 5, 6, 0, 0, 0}, /* 0x3D (00111101) */
2338 {2, 3, 4, 5, 6, 0, 0, 0}, /* 0x3E (00111110) */
2339 {1, 2, 3, 4, 5, 6, 0, 0}, /* 0x3F (00111111) */
2340 {7, 0, 0, 0, 0, 0, 0, 0}, /* 0x40 (01000000) */
2341 {1, 7, 0, 0, 0, 0, 0, 0}, /* 0x41 (01000001) */
2342 {2, 7, 0, 0, 0, 0, 0, 0}, /* 0x42 (01000010) */
2343 {1, 2, 7, 0, 0, 0, 0, 0}, /* 0x43 (01000011) */
2344 {3, 7, 0, 0, 0, 0, 0, 0}, /* 0x44 (01000100) */
2345 {1, 3, 7, 0, 0, 0, 0, 0}, /* 0x45 (01000101) */
2346 {2, 3, 7, 0, 0, 0, 0, 0}, /* 0x46 (01000110) */
2347 {1, 2, 3, 7, 0, 0, 0, 0}, /* 0x47 (01000111) */
2348 {4, 7, 0, 0, 0, 0, 0, 0}, /* 0x48 (01001000) */
2349 {1, 4, 7, 0, 0, 0, 0, 0}, /* 0x49 (01001001) */
2350 {2, 4, 7, 0, 0, 0, 0, 0}, /* 0x4A (01001010) */
2351 {1, 2, 4, 7, 0, 0, 0, 0}, /* 0x4B (01001011) */
2352 {3, 4, 7, 0, 0, 0, 0, 0}, /* 0x4C (01001100) */
2353 {1, 3, 4, 7, 0, 0, 0, 0}, /* 0x4D (01001101) */
2354 {2, 3, 4, 7, 0, 0, 0, 0}, /* 0x4E (01001110) */
2355 {1, 2, 3, 4, 7, 0, 0, 0}, /* 0x4F (01001111) */
2356 {5, 7, 0, 0, 0, 0, 0, 0}, /* 0x50 (01010000) */
2357 {1, 5, 7, 0, 0, 0, 0, 0}, /* 0x51 (01010001) */
2358 {2, 5, 7, 0, 0, 0, 0, 0}, /* 0x52 (01010010) */
2359 {1, 2, 5, 7, 0, 0, 0, 0}, /* 0x53 (01010011) */
2360 {3, 5, 7, 0, 0, 0, 0, 0}, /* 0x54 (01010100) */
2361 {1, 3, 5, 7, 0, 0, 0, 0}, /* 0x55 (01010101) */
2362 {2, 3, 5, 7, 0, 0, 0, 0}, /* 0x56 (01010110) */
2363 {1, 2, 3, 5, 7, 0, 0, 0}, /* 0x57 (01010111) */
2364 {4, 5, 7, 0, 0, 0, 0, 0}, /* 0x58 (01011000) */
2365 {1, 4, 5, 7, 0, 0, 0, 0}, /* 0x59 (01011001) */
2366 {2, 4, 5, 7, 0, 0, 0, 0}, /* 0x5A (01011010) */
2367 {1, 2, 4, 5, 7, 0, 0, 0}, /* 0x5B (01011011) */
2368 {3, 4, 5, 7, 0, 0, 0, 0}, /* 0x5C (01011100) */
2369 {1, 3, 4, 5, 7, 0, 0, 0}, /* 0x5D (01011101) */
2370 {2, 3, 4, 5, 7, 0, 0, 0}, /* 0x5E (01011110) */
2371 {1, 2, 3, 4, 5, 7, 0, 0}, /* 0x5F (01011111) */
2372 {6, 7, 0, 0, 0, 0, 0, 0}, /* 0x60 (01100000) */
2373 {1, 6, 7, 0, 0, 0, 0, 0}, /* 0x61 (01100001) */
2374 {2, 6, 7, 0, 0, 0, 0, 0}, /* 0x62 (01100010) */
2375 {1, 2, 6, 7, 0, 0, 0, 0}, /* 0x63 (01100011) */
2376 {3, 6, 7, 0, 0, 0, 0, 0}, /* 0x64 (01100100) */
2377 {1, 3, 6, 7, 0, 0, 0, 0}, /* 0x65 (01100101) */
2378 {2, 3, 6, 7, 0, 0, 0, 0}, /* 0x66 (01100110) */
2379 {1, 2, 3, 6, 7, 0, 0, 0}, /* 0x67 (01100111) */
2380 {4, 6, 7, 0, 0, 0, 0, 0}, /* 0x68 (01101000) */
2381 {1, 4, 6, 7, 0, 0, 0, 0}, /* 0x69 (01101001) */
2382 {2, 4, 6, 7, 0, 0, 0, 0}, /* 0x6A (01101010) */
2383 {1, 2, 4, 6, 7, 0, 0, 0}, /* 0x6B (01101011) */
2384 {3, 4, 6, 7, 0, 0, 0, 0}, /* 0x6C (01101100) */
2385 {1, 3, 4, 6, 7, 0, 0, 0}, /* 0x6D (01101101) */
2386 {2, 3, 4, 6, 7, 0, 0, 0}, /* 0x6E (01101110) */
2387 {1, 2, 3, 4, 6, 7, 0, 0}, /* 0x6F (01101111) */
2388 {5, 6, 7, 0, 0, 0, 0, 0}, /* 0x70 (01110000) */
2389 {1, 5, 6, 7, 0, 0, 0, 0}, /* 0x71 (01110001) */
2390 {2, 5, 6, 7, 0, 0, 0, 0}, /* 0x72 (01110010) */
2391 {1, 2, 5, 6, 7, 0, 0, 0}, /* 0x73 (01110011) */
2392 {3, 5, 6, 7, 0, 0, 0, 0}, /* 0x74 (01110100) */
2393 {1, 3, 5, 6, 7, 0, 0, 0}, /* 0x75 (01110101) */
2394 {2, 3, 5, 6, 7, 0, 0, 0}, /* 0x76 (01110110) */
2395 {1, 2, 3, 5, 6, 7, 0, 0}, /* 0x77 (01110111) */
2396 {4, 5, 6, 7, 0, 0, 0, 0}, /* 0x78 (01111000) */
2397 {1, 4, 5, 6, 7, 0, 0, 0}, /* 0x79 (01111001) */
2398 {2, 4, 5, 6, 7, 0, 0, 0}, /* 0x7A (01111010) */
2399 {1, 2, 4, 5, 6, 7, 0, 0}, /* 0x7B (01111011) */
2400 {3, 4, 5, 6, 7, 0, 0, 0}, /* 0x7C (01111100) */
2401 {1, 3, 4, 5, 6, 7, 0, 0}, /* 0x7D (01111101) */
2402 {2, 3, 4, 5, 6, 7, 0, 0}, /* 0x7E (01111110) */
2403 {1, 2, 3, 4, 5, 6, 7, 0}, /* 0x7F (01111111) */
2404 {8, 0, 0, 0, 0, 0, 0, 0}, /* 0x80 (10000000) */
2405 {1, 8, 0, 0, 0, 0, 0, 0}, /* 0x81 (10000001) */
2406 {2, 8, 0, 0, 0, 0, 0, 0}, /* 0x82 (10000010) */
2407 {1, 2, 8, 0, 0, 0, 0, 0}, /* 0x83 (10000011) */
2408 {3, 8, 0, 0, 0, 0, 0, 0}, /* 0x84 (10000100) */
2409 {1, 3, 8, 0, 0, 0, 0, 0}, /* 0x85 (10000101) */
2410 {2, 3, 8, 0, 0, 0, 0, 0}, /* 0x86 (10000110) */
2411 {1, 2, 3, 8, 0, 0, 0, 0}, /* 0x87 (10000111) */
2412 {4, 8, 0, 0, 0, 0, 0, 0}, /* 0x88 (10001000) */
2413 {1, 4, 8, 0, 0, 0, 0, 0}, /* 0x89 (10001001) */
2414 {2, 4, 8, 0, 0, 0, 0, 0}, /* 0x8A (10001010) */
2415 {1, 2, 4, 8, 0, 0, 0, 0}, /* 0x8B (10001011) */
2416 {3, 4, 8, 0, 0, 0, 0, 0}, /* 0x8C (10001100) */
2417 {1, 3, 4, 8, 0, 0, 0, 0}, /* 0x8D (10001101) */
2418 {2, 3, 4, 8, 0, 0, 0, 0}, /* 0x8E (10001110) */
2419 {1, 2, 3, 4, 8, 0, 0, 0}, /* 0x8F (10001111) */
2420 {5, 8, 0, 0, 0, 0, 0, 0}, /* 0x90 (10010000) */
2421 {1, 5, 8, 0, 0, 0, 0, 0}, /* 0x91 (10010001) */
2422 {2, 5, 8, 0, 0, 0, 0, 0}, /* 0x92 (10010010) */
2423 {1, 2, 5, 8, 0, 0, 0, 0}, /* 0x93 (10010011) */
2424 {3, 5, 8, 0, 0, 0, 0, 0}, /* 0x94 (10010100) */
2425 {1, 3, 5, 8, 0, 0, 0, 0}, /* 0x95 (10010101) */
2426 {2, 3, 5, 8, 0, 0, 0, 0}, /* 0x96 (10010110) */
2427 {1, 2, 3, 5, 8, 0, 0, 0}, /* 0x97 (10010111) */
2428 {4, 5, 8, 0, 0, 0, 0, 0}, /* 0x98 (10011000) */
2429 {1, 4, 5, 8, 0, 0, 0, 0}, /* 0x99 (10011001) */
2430 {2, 4, 5, 8, 0, 0, 0, 0}, /* 0x9A (10011010) */
2431 {1, 2, 4, 5, 8, 0, 0, 0}, /* 0x9B (10011011) */
2432 {3, 4, 5, 8, 0, 0, 0, 0}, /* 0x9C (10011100) */
2433 {1, 3, 4, 5, 8, 0, 0, 0}, /* 0x9D (10011101) */
2434 {2, 3, 4, 5, 8, 0, 0, 0}, /* 0x9E (10011110) */
2435 {1, 2, 3, 4, 5, 8, 0, 0}, /* 0x9F (10011111) */
2436 {6, 8, 0, 0, 0, 0, 0, 0}, /* 0xA0 (10100000) */
2437 {1, 6, 8, 0, 0, 0, 0, 0}, /* 0xA1 (10100001) */
2438 {2, 6, 8, 0, 0, 0, 0, 0}, /* 0xA2 (10100010) */
2439 {1, 2, 6, 8, 0, 0, 0, 0}, /* 0xA3 (10100011) */
2440 {3, 6, 8, 0, 0, 0, 0, 0}, /* 0xA4 (10100100) */
2441 {1, 3, 6, 8, 0, 0, 0, 0}, /* 0xA5 (10100101) */
2442 {2, 3, 6, 8, 0, 0, 0, 0}, /* 0xA6 (10100110) */
2443 {1, 2, 3, 6, 8, 0, 0, 0}, /* 0xA7 (10100111) */
2444 {4, 6, 8, 0, 0, 0, 0, 0}, /* 0xA8 (10101000) */
2445 {1, 4, 6, 8, 0, 0, 0, 0}, /* 0xA9 (10101001) */
2446 {2, 4, 6, 8, 0, 0, 0, 0}, /* 0xAA (10101010) */
2447 {1, 2, 4, 6, 8, 0, 0, 0}, /* 0xAB (10101011) */
2448 {3, 4, 6, 8, 0, 0, 0, 0}, /* 0xAC (10101100) */
2449 {1, 3, 4, 6, 8, 0, 0, 0}, /* 0xAD (10101101) */
2450 {2, 3, 4, 6, 8, 0, 0, 0}, /* 0xAE (10101110) */
2451 {1, 2, 3, 4, 6, 8, 0, 0}, /* 0xAF (10101111) */
2452 {5, 6, 8, 0, 0, 0, 0, 0}, /* 0xB0 (10110000) */
2453 {1, 5, 6, 8, 0, 0, 0, 0}, /* 0xB1 (10110001) */
2454 {2, 5, 6, 8, 0, 0, 0, 0}, /* 0xB2 (10110010) */
2455 {1, 2, 5, 6, 8, 0, 0, 0}, /* 0xB3 (10110011) */
2456 {3, 5, 6, 8, 0, 0, 0, 0}, /* 0xB4 (10110100) */
2457 {1, 3, 5, 6, 8, 0, 0, 0}, /* 0xB5 (10110101) */
2458 {2, 3, 5, 6, 8, 0, 0, 0}, /* 0xB6 (10110110) */
2459 {1, 2, 3, 5, 6, 8, 0, 0}, /* 0xB7 (10110111) */
2460 {4, 5, 6, 8, 0, 0, 0, 0}, /* 0xB8 (10111000) */
2461 {1, 4, 5, 6, 8, 0, 0, 0}, /* 0xB9 (10111001) */
2462 {2, 4, 5, 6, 8, 0, 0, 0}, /* 0xBA (10111010) */
2463 {1, 2, 4, 5, 6, 8, 0, 0}, /* 0xBB (10111011) */
2464 {3, 4, 5, 6, 8, 0, 0, 0}, /* 0xBC (10111100) */
2465 {1, 3, 4, 5, 6, 8, 0, 0}, /* 0xBD (10111101) */
2466 {2, 3, 4, 5, 6, 8, 0, 0}, /* 0xBE (10111110) */
2467 {1, 2, 3, 4, 5, 6, 8, 0}, /* 0xBF (10111111) */
2468 {7, 8, 0, 0, 0, 0, 0, 0}, /* 0xC0 (11000000) */
2469 {1, 7, 8, 0, 0, 0, 0, 0}, /* 0xC1 (11000001) */
2470 {2, 7, 8, 0, 0, 0, 0, 0}, /* 0xC2 (11000010) */
2471 {1, 2, 7, 8, 0, 0, 0, 0}, /* 0xC3 (11000011) */
2472 {3, 7, 8, 0, 0, 0, 0, 0}, /* 0xC4 (11000100) */
2473 {1, 3, 7, 8, 0, 0, 0, 0}, /* 0xC5 (11000101) */
2474 {2, 3, 7, 8, 0, 0, 0, 0}, /* 0xC6 (11000110) */
2475 {1, 2, 3, 7, 8, 0, 0, 0}, /* 0xC7 (11000111) */
2476 {4, 7, 8, 0, 0, 0, 0, 0}, /* 0xC8 (11001000) */
2477 {1, 4, 7, 8, 0, 0, 0, 0}, /* 0xC9 (11001001) */
2478 {2, 4, 7, 8, 0, 0, 0, 0}, /* 0xCA (11001010) */
2479 {1, 2, 4, 7, 8, 0, 0, 0}, /* 0xCB (11001011) */
2480 {3, 4, 7, 8, 0, 0, 0, 0}, /* 0xCC (11001100) */
2481 {1, 3, 4, 7, 8, 0, 0, 0}, /* 0xCD (11001101) */
2482 {2, 3, 4, 7, 8, 0, 0, 0}, /* 0xCE (11001110) */
2483 {1, 2, 3, 4, 7, 8, 0, 0}, /* 0xCF (11001111) */
2484 {5, 7, 8, 0, 0, 0, 0, 0}, /* 0xD0 (11010000) */
2485 {1, 5, 7, 8, 0, 0, 0, 0}, /* 0xD1 (11010001) */
2486 {2, 5, 7, 8, 0, 0, 0, 0}, /* 0xD2 (11010010) */
2487 {1, 2, 5, 7, 8, 0, 0, 0}, /* 0xD3 (11010011) */
2488 {3, 5, 7, 8, 0, 0, 0, 0}, /* 0xD4 (11010100) */
2489 {1, 3, 5, 7, 8, 0, 0, 0}, /* 0xD5 (11010101) */
2490 {2, 3, 5, 7, 8, 0, 0, 0}, /* 0xD6 (11010110) */
2491 {1, 2, 3, 5, 7, 8, 0, 0}, /* 0xD7 (11010111) */
2492 {4, 5, 7, 8, 0, 0, 0, 0}, /* 0xD8 (11011000) */
2493 {1, 4, 5, 7, 8, 0, 0, 0}, /* 0xD9 (11011001) */
2494 {2, 4, 5, 7, 8, 0, 0, 0}, /* 0xDA (11011010) */
2495 {1, 2, 4, 5, 7, 8, 0, 0}, /* 0xDB (11011011) */
2496 {3, 4, 5, 7, 8, 0, 0, 0}, /* 0xDC (11011100) */
2497 {1, 3, 4, 5, 7, 8, 0, 0}, /* 0xDD (11011101) */
2498 {2, 3, 4, 5, 7, 8, 0, 0}, /* 0xDE (11011110) */
2499 {1, 2, 3, 4, 5, 7, 8, 0}, /* 0xDF (11011111) */
2500 {6, 7, 8, 0, 0, 0, 0, 0}, /* 0xE0 (11100000) */
2501 {1, 6, 7, 8, 0, 0, 0, 0}, /* 0xE1 (11100001) */
2502 {2, 6, 7, 8, 0, 0, 0, 0}, /* 0xE2 (11100010) */
2503 {1, 2, 6, 7, 8, 0, 0, 0}, /* 0xE3 (11100011) */
2504 {3, 6, 7, 8, 0, 0, 0, 0}, /* 0xE4 (11100100) */
2505 {1, 3, 6, 7, 8, 0, 0, 0}, /* 0xE5 (11100101) */
2506 {2, 3, 6, 7, 8, 0, 0, 0}, /* 0xE6 (11100110) */
2507 {1, 2, 3, 6, 7, 8, 0, 0}, /* 0xE7 (11100111) */
2508 {4, 6, 7, 8, 0, 0, 0, 0}, /* 0xE8 (11101000) */
2509 {1, 4, 6, 7, 8, 0, 0, 0}, /* 0xE9 (11101001) */
2510 {2, 4, 6, 7, 8, 0, 0, 0}, /* 0xEA (11101010) */
2511 {1, 2, 4, 6, 7, 8, 0, 0}, /* 0xEB (11101011) */
2512 {3, 4, 6, 7, 8, 0, 0, 0}, /* 0xEC (11101100) */
2513 {1, 3, 4, 6, 7, 8, 0, 0}, /* 0xED (11101101) */
2514 {2, 3, 4, 6, 7, 8, 0, 0}, /* 0xEE (11101110) */
2515 {1, 2, 3, 4, 6, 7, 8, 0}, /* 0xEF (11101111) */
2516 {5, 6, 7, 8, 0, 0, 0, 0}, /* 0xF0 (11110000) */
2517 {1, 5, 6, 7, 8, 0, 0, 0}, /* 0xF1 (11110001) */
2518 {2, 5, 6, 7, 8, 0, 0, 0}, /* 0xF2 (11110010) */
2519 {1, 2, 5, 6, 7, 8, 0, 0}, /* 0xF3 (11110011) */
2520 {3, 5, 6, 7, 8, 0, 0, 0}, /* 0xF4 (11110100) */
2521 {1, 3, 5, 6, 7, 8, 0, 0}, /* 0xF5 (11110101) */
2522 {2, 3, 5, 6, 7, 8, 0, 0}, /* 0xF6 (11110110) */
2523 {1, 2, 3, 5, 6, 7, 8, 0}, /* 0xF7 (11110111) */
2524 {4, 5, 6, 7, 8, 0, 0, 0}, /* 0xF8 (11111000) */
2525 {1, 4, 5, 6, 7, 8, 0, 0}, /* 0xF9 (11111001) */
2526 {2, 4, 5, 6, 7, 8, 0, 0}, /* 0xFA (11111010) */
2527 {1, 2, 4, 5, 6, 7, 8, 0}, /* 0xFB (11111011) */
2528 {3, 4, 5, 6, 7, 8, 0, 0}, /* 0xFC (11111100) */
2529 {1, 3, 4, 5, 6, 7, 8, 0}, /* 0xFD (11111101) */
2530 {2, 3, 4, 5, 6, 7, 8, 0}, /* 0xFE (11111110) */
2531 {1, 2, 3, 4, 5, 6, 7, 8} /* 0xFF (11111111) */
2532 };
2533
2534 #endif
2535
2536 #ifdef USEAVX
2537
2538 size_t bitset_extract_setbits_avx2(uint64_t *array, size_t length, void *vout,
2539 size_t outcapacity, uint32_t base) {
2540 uint32_t *out = (uint32_t *)vout;
2541 uint32_t *initout = out;
2542 __m256i baseVec = _mm256_set1_epi32(base - 1);
2543 __m256i incVec = _mm256_set1_epi32(64);
2544 __m256i add8 = _mm256_set1_epi32(8);
2545 uint32_t *safeout = out + outcapacity;
2546 size_t i = 0;
2547 for (; (i < length) && (out + 64 <= safeout); ++i) {
2548 uint64_t w = array[i];
2549 if (w == 0) {
2550 baseVec = _mm256_add_epi32(baseVec, incVec);
2551 } else {
2552 for (int k = 0; k < 4; ++k) {
2553 uint8_t byteA = (uint8_t)w;
2554 uint8_t byteB = (uint8_t)(w >> 8);
2555 w >>= 16;
2556 __m256i vecA =
2557 _mm256_load_si256((const __m256i *)vecDecodeTable[byteA]);
2558 __m256i vecB =
2559 _mm256_load_si256((const __m256i *)vecDecodeTable[byteB]);
2560 uint8_t advanceA = lengthTable[byteA];
2561 uint8_t advanceB = lengthTable[byteB];
2562 vecA = _mm256_add_epi32(baseVec, vecA);
2563 baseVec = _mm256_add_epi32(baseVec, add8);
2564 vecB = _mm256_add_epi32(baseVec, vecB);
2565 baseVec = _mm256_add_epi32(baseVec, add8);
2566 _mm256_storeu_si256((__m256i *)out, vecA);
2567 out += advanceA;
2568 _mm256_storeu_si256((__m256i *)out, vecB);
2569 out += advanceB;
2570 }
2571 }
2572 }
2573 base += i * 64;
2574 for (; (i < length) && (out < safeout); ++i) {
2575 uint64_t w = array[i];
2576 while ((w != 0) && (out < safeout)) {
2577 uint64_t t = w & (~w + 1); // on x64, should compile to BLSI (careful: the Intel compiler seems to fail)
2578 int r = __builtin_ctzll(w); // on x64, should compile to TZCNT
2579 uint32_t val = r + base;
2580 memcpy(out, &val,
2581 sizeof(uint32_t)); // should be compiled as a MOV on x64
2582 out++;
2583 w ^= t;
2584 }
2585 base += 64;
2586 }
2587 return out - initout;
2588 }
2589 #endif // USEAVX
2590
2591 size_t bitset_extract_setbits(uint64_t *bitset, size_t length, void *vout,
2592 uint32_t base) {
2593 int outpos = 0;
2594 uint32_t *out = (uint32_t *)vout;
2595 for (size_t i = 0; i < length; ++i) {
2596 uint64_t w = bitset[i];
2597 while (w != 0) {
2598 uint64_t t = w & (~w + 1); // on x64, should compile to BLSI (careful: the Intel compiler seems to fail)
2599 int r = __builtin_ctzll(w); // on x64, should compile to TZCNT
2600 uint32_t val = r + base;
2601 memcpy(out + outpos, &val,
2602 sizeof(uint32_t)); // should be compiled as a MOV on x64
2603 outpos++;
2604 w ^= t;
2605 }
2606 base += 64;
2607 }
2608 return outpos;
2609 }
2610
2611 size_t bitset_extract_intersection_setbits_uint16(const uint64_t * __restrict__ bitset1,
2612 const uint64_t * __restrict__ bitset2,
2613 size_t length, uint16_t *out,
2614 uint16_t base) {
2615 int outpos = 0;
2616 for (size_t i = 0; i < length; ++i) {
2617 uint64_t w = bitset1[i] & bitset2[i];
2618 while (w != 0) {
2619 uint64_t t = w & (~w + 1);
2620 int r = __builtin_ctzll(w);
2621 out[outpos++] = r + base;
2622 w ^= t;
2623 }
2624 base += 64;
2625 }
2626 return outpos;
2627 }
2628
2629 #ifdef IS_X64
2630 /*
2631 * Given a bitset containing "length" 64-bit words, write out the position
2632 * of all the set bits to "out" as 16-bit integers, values start at "base" (can
2633 *be set to zero).
2634 *
2635 * The "out" pointer should be sufficient to store the actual number of bits
2636 *set.
2637 *
2638 * Returns how many values were actually decoded.
2639 *
2640 * This function uses SSE decoding.
2641 */
2642 size_t bitset_extract_setbits_sse_uint16(const uint64_t *bitset, size_t length,
2643 uint16_t *out, size_t outcapacity,
2644 uint16_t base) {
2645 uint16_t *initout = out;
2646 __m128i baseVec = _mm_set1_epi16(base - 1);
2647 __m128i incVec = _mm_set1_epi16(64);
2648 __m128i add8 = _mm_set1_epi16(8);
2649 uint16_t *safeout = out + outcapacity;
2650 const int numberofbytes = 2; // process two bytes at a time
2651 size_t i = 0;
2652 for (; (i < length) && (out + numberofbytes * 8 <= safeout); ++i) {
2653 uint64_t w = bitset[i];
2654 if (w == 0) {
2655 baseVec = _mm_add_epi16(baseVec, incVec);
2656 } else {
2657 for (int k = 0; k < 4; ++k) {
2658 uint8_t byteA = (uint8_t)w;
2659 uint8_t byteB = (uint8_t)(w >> 8);
2660 w >>= 16;
2661 __m128i vecA = _mm_load_si128(
2662 (const __m128i *)vecDecodeTable_uint16[byteA]);
2663 __m128i vecB = _mm_load_si128(
2664 (const __m128i *)vecDecodeTable_uint16[byteB]);
2665 uint8_t advanceA = lengthTable[byteA];
2666 uint8_t advanceB = lengthTable[byteB];
2667 vecA = _mm_add_epi16(baseVec, vecA);
2668 baseVec = _mm_add_epi16(baseVec, add8);
2669 vecB = _mm_add_epi16(baseVec, vecB);
2670 baseVec = _mm_add_epi16(baseVec, add8);
2671 _mm_storeu_si128((__m128i *)out, vecA);
2672 out += advanceA;
2673 _mm_storeu_si128((__m128i *)out, vecB);
2674 out += advanceB;
2675 }
2676 }
2677 }
2678 base += (uint16_t)(i * 64);
2679 for (; (i < length) && (out < safeout); ++i) {
2680 uint64_t w = bitset[i];
2681 while ((w != 0) && (out < safeout)) {
2682 uint64_t t = w & (~w + 1);
2683 int r = __builtin_ctzll(w);
2684 *out = r + base;
2685 out++;
2686 w ^= t;
2687 }
2688 base += 64;
2689 }
2690 return out - initout;
2691 }
2692 #endif
2693
2694 /*
2695 * Given a bitset containing "length" 64-bit words, write out the position
2696 * of all the set bits to "out", values start at "base" (can be set to zero).
2697 *
2698 * The "out" pointer should be sufficient to store the actual number of bits
2699 *set.
2700 *
2701 * Returns how many values were actually decoded.
2702 */
2703 size_t bitset_extract_setbits_uint16(const uint64_t *bitset, size_t length,
2704 uint16_t *out, uint16_t base) {
2705 int outpos = 0;
2706 for (size_t i = 0; i < length; ++i) {
2707 uint64_t w = bitset[i];
2708 while (w != 0) {
2709 uint64_t t = w & (~w + 1);
2710 int r = __builtin_ctzll(w);
2711 out[outpos++] = r + base;
2712 w ^= t;
2713 }
2714 base += 64;
2715 }
2716 return outpos;
2717 }
2718
2719 #if defined(ASMBITMANIPOPTIMIZATION)
2720
2721 uint64_t bitset_set_list_withcard(void *bitset, uint64_t card,
2722 const uint16_t *list, uint64_t length) {
2723 uint64_t offset, load, pos;
2724 uint64_t shift = 6;
2725 const uint16_t *end = list + length;
2726 if (!length) return card;
2727 // TODO: could unroll for performance, see bitset_set_list
2728 // bts is not available as an intrinsic in GCC
2729 __asm volatile(
2730 "1:\n"
2731 "movzwq (%[list]), %[pos]\n"
2732 "shrx %[shift], %[pos], %[offset]\n"
2733 "mov (%[bitset],%[offset],8), %[load]\n"
2734 "bts %[pos], %[load]\n"
2735 "mov %[load], (%[bitset],%[offset],8)\n"
2736 "sbb $-1, %[card]\n"
2737 "add $2, %[list]\n"
2738 "cmp %[list], %[end]\n"
2739 "jnz 1b"
2740 : [card] "+&r"(card), [list] "+&r"(list), [load] "=&r"(load),
2741 [pos] "=&r"(pos), [offset] "=&r"(offset)
2742 : [end] "r"(end), [bitset] "r"(bitset), [shift] "r"(shift));
2743 return card;
2744 }
2745
2746 void bitset_set_list(void *bitset, const uint16_t *list, uint64_t length) {
2747 uint64_t pos;
2748 const uint16_t *end = list + length;
2749
2750 uint64_t shift = 6;
2751 uint64_t offset;
2752 uint64_t load;
2753 for (; list + 3 < end; list += 4) {
2754 pos = list[0];
2755 __asm volatile(
2756 "shrx %[shift], %[pos], %[offset]\n"
2757 "mov (%[bitset],%[offset],8), %[load]\n"
2758 "bts %[pos], %[load]\n"
2759 "mov %[load], (%[bitset],%[offset],8)"
2760 : [load] "=&r"(load), [offset] "=&r"(offset)
2761 : [bitset] "r"(bitset), [shift] "r"(shift), [pos] "r"(pos));
2762 pos = list[1];
2763 __asm volatile(
2764 "shrx %[shift], %[pos], %[offset]\n"
2765 "mov (%[bitset],%[offset],8), %[load]\n"
2766 "bts %[pos], %[load]\n"
2767 "mov %[load], (%[bitset],%[offset],8)"
2768 : [load] "=&r"(load), [offset] "=&r"(offset)
2769 : [bitset] "r"(bitset), [shift] "r"(shift), [pos] "r"(pos));
2770 pos = list[2];
2771 __asm volatile(
2772 "shrx %[shift], %[pos], %[offset]\n"
2773 "mov (%[bitset],%[offset],8), %[load]\n"
2774 "bts %[pos], %[load]\n"
2775 "mov %[load], (%[bitset],%[offset],8)"
2776 : [load] "=&r"(load), [offset] "=&r"(offset)
2777 : [bitset] "r"(bitset), [shift] "r"(shift), [pos] "r"(pos));
2778 pos = list[3];
2779 __asm volatile(
2780 "shrx %[shift], %[pos], %[offset]\n"
2781 "mov (%[bitset],%[offset],8), %[load]\n"
2782 "bts %[pos], %[load]\n"
2783 "mov %[load], (%[bitset],%[offset],8)"
2784 : [load] "=&r"(load), [offset] "=&r"(offset)
2785 : [bitset] "r"(bitset), [shift] "r"(shift), [pos] "r"(pos));
2786 }
2787
2788 while (list != end) {
2789 pos = list[0];
2790 __asm volatile(
2791 "shrx %[shift], %[pos], %[offset]\n"
2792 "mov (%[bitset],%[offset],8), %[load]\n"
2793 "bts %[pos], %[load]\n"
2794 "mov %[load], (%[bitset],%[offset],8)"
2795 : [load] "=&r"(load), [offset] "=&r"(offset)
2796 : [bitset] "r"(bitset), [shift] "r"(shift), [pos] "r"(pos));
2797 list++;
2798 }
2799 }
2800
2801 uint64_t bitset_clear_list(void *bitset, uint64_t card, const uint16_t *list,
2802 uint64_t length) {
2803 uint64_t offset, load, pos;
2804 uint64_t shift = 6;
2805 const uint16_t *end = list + length;
2806 if (!length) return card;
2807 // btr is not available as an intrinsic in GCC
2808 __asm volatile(
2809 "1:\n"
2810 "movzwq (%[list]), %[pos]\n"
2811 "shrx %[shift], %[pos], %[offset]\n"
2812 "mov (%[bitset],%[offset],8), %[load]\n"
2813 "btr %[pos], %[load]\n"
2814 "mov %[load], (%[bitset],%[offset],8)\n"
2815 "sbb $0, %[card]\n"
2816 "add $2, %[list]\n"
2817 "cmp %[list], %[end]\n"
2818 "jnz 1b"
2819 : [card] "+&r"(card), [list] "+&r"(list), [load] "=&r"(load),
2820 [pos] "=&r"(pos), [offset] "=&r"(offset)
2821 : [end] "r"(end), [bitset] "r"(bitset), [shift] "r"(shift)
2822 :
2823 /* clobbers */ "memory");
2824 return card;
2825 }
2826
2827 #else
2828 uint64_t bitset_clear_list(void *bitset, uint64_t card, const uint16_t *list,
2829 uint64_t length) {
2830 uint64_t offset, load, newload, pos, index;
2831 const uint16_t *end = list + length;
2832 while (list != end) {
2833 pos = *(const uint16_t *)list;
2834 offset = pos >> 6;
2835 index = pos % 64;
2836 load = ((uint64_t *)bitset)[offset];
2837 newload = load & ~(UINT64_C(1) << index);
2838 card -= (load ^ newload) >> index;
2839 ((uint64_t *)bitset)[offset] = newload;
2840 list++;
2841 }
2842 return card;
2843 }
2844
2845 uint64_t bitset_set_list_withcard(void *bitset, uint64_t card,
2846 const uint16_t *list, uint64_t length) {
2847 uint64_t offset, load, newload, pos, index;
2848 const uint16_t *end = list + length;
2849 while (list != end) {
2850 pos = *(const uint16_t *)list;
2851 offset = pos >> 6;
2852 index = pos % 64;
2853 load = ((uint64_t *)bitset)[offset];
2854 newload = load | (UINT64_C(1) << index);
2855 card += (load ^ newload) >> index;
2856 ((uint64_t *)bitset)[offset] = newload;
2857 list++;
2858 }
2859 return card;
2860 }
2861
2862 void bitset_set_list(void *bitset, const uint16_t *list, uint64_t length) {
2863 uint64_t offset, load, newload, pos, index;
2864 const uint16_t *end = list + length;
2865 while (list != end) {
2866 pos = *(const uint16_t *)list;
2867 offset = pos >> 6;
2868 index = pos % 64;
2869 load = ((uint64_t *)bitset)[offset];
2870 newload = load | (UINT64_C(1) << index);
2871 ((uint64_t *)bitset)[offset] = newload;
2872 list++;
2873 }
2874 }
2875
2876 #endif
2877
2878 /* flip specified bits */
2879 /* TODO: consider whether worthwhile to make an asm version */
2880
2881 uint64_t bitset_flip_list_withcard(void *bitset, uint64_t card,
2882 const uint16_t *list, uint64_t length) {
2883 uint64_t offset, load, newload, pos, index;
2884 const uint16_t *end = list + length;
2885 while (list != end) {
2886 pos = *(const uint16_t *)list;
2887 offset = pos >> 6;
2888 index = pos % 64;
2889 load = ((uint64_t *)bitset)[offset];
2890 newload = load ^ (UINT64_C(1) << index);
2891 // todo: is a branch here all that bad?
2892 card +=
2893 (1 - 2 * (((UINT64_C(1) << index) & load) >> index)); // +1 or -1
2894 ((uint64_t *)bitset)[offset] = newload;
2895 list++;
2896 }
2897 return card;
2898 }
2899
2900 void bitset_flip_list(void *bitset, const uint16_t *list, uint64_t length) {
2901 uint64_t offset, load, newload, pos, index;
2902 const uint16_t *end = list + length;
2903 while (list != end) {
2904 pos = *(const uint16_t *)list;
2905 offset = pos >> 6;
2906 index = pos % 64;
2907 load = ((uint64_t *)bitset)[offset];
2908 newload = load ^ (UINT64_C(1) << index);
2909 ((uint64_t *)bitset)[offset] = newload;
2910 list++;
2911 }
2912 }
2913 /* end file src/bitset_util.c */
2914 /* begin file src/containers/array.c */
2915 /*
2916 * array.c
2917 *
2918 */
2919
2920 #include <assert.h>
2921 #include <stdio.h>
2922 #include <stdlib.h>
2923
2924 /* Create a new array with capacity size. Return NULL in case of failure. */
2925 array_container_t *array_container_create_given_capacity(int32_t size) {
2926 array_container_t *container;
2927
2928 container = (array_container_t *)malloc(sizeof(array_container_t));
2929 assert (container);
2930
2931 if( size <= 0 ) { // we don't want to rely on malloc(0)
2932 container->array = NULL;
2933 } else {
2934 container->array = (uint16_t *)malloc(sizeof(uint16_t) * size);
2935 assert (container->array);
2936 }
2937
2938 container->capacity = size;
2939 container->cardinality = 0;
2940
2941 return container;
2942 }
2943
2944 /* Create a new array. Return NULL in case of failure. */
2945 array_container_t *array_container_create(void) {
2946 return array_container_create_given_capacity(ARRAY_DEFAULT_INIT_SIZE);
2947 }
2948
2949 /* Create a new array containing all values in [min,max). */
2950 array_container_t * array_container_create_range(uint32_t min, uint32_t max) {
2951 array_container_t * answer = array_container_create_given_capacity(max - min + 1);
2952 if(answer == NULL) return answer;
2953 answer->cardinality = 0;
2954 for(uint32_t k = min; k < max; k++) {
2955 answer->array[answer->cardinality++] = k;
2956 }
2957 return answer;
2958 }
2959
2960 /* Duplicate container */
2961 array_container_t *array_container_clone(const array_container_t *src) {
2962 array_container_t *newcontainer =
2963 array_container_create_given_capacity(src->capacity);
2964 if (newcontainer == NULL) return NULL;
2965
2966 newcontainer->cardinality = src->cardinality;
2967
2968 memcpy(newcontainer->array, src->array,
2969 src->cardinality * sizeof(uint16_t));
2970
2971 return newcontainer;
2972 }
2973
2974 int array_container_shrink_to_fit(array_container_t *src) {
2975 if (src->cardinality == src->capacity) return 0; // nothing to do
2976 int savings = src->capacity - src->cardinality;
2977 src->capacity = src->cardinality;
2978 if( src->capacity == 0) { // we do not want to rely on realloc for zero allocs
2979 free(src->array);
2980 src->array = NULL;
2981 } else {
2982 uint16_t *oldarray = src->array;
2983 src->array =
2984 (uint16_t *)realloc(oldarray, src->capacity * sizeof(uint16_t));
2985 }
2986 return savings;
2987 }
2988
2989 /* Free memory. */
2990 void array_container_free(array_container_t *arr) {
2991 if(arr->array != NULL) {// Jon Strabala reports that some tools complain otherwise
2992 free(arr->array);
2993 arr->array = NULL; // pedantic
2994 }
2995 free(arr);
2996 }
2997
2998 static inline int32_t grow_capacity(int32_t capacity) {
2999 return (capacity <= 0) ? ARRAY_DEFAULT_INIT_SIZE
3000 : capacity < 64 ? capacity * 2
3001 : capacity < 1024 ? capacity * 3 / 2
3002 : capacity * 5 / 4;
3003 }
3004
3005 static inline int32_t clamp(int32_t val, int32_t min, int32_t max) {
3006 return ((val < min) ? min : (val > max) ? max : val);
3007 }
3008
3009 void array_container_grow(array_container_t *container, int32_t min,
3010 bool preserve) {
3011
3012 int32_t max = (min <= DEFAULT_MAX_SIZE ? DEFAULT_MAX_SIZE : 65536);
3013 int32_t new_capacity = clamp(grow_capacity(container->capacity), min, max);
3014
3015 container->capacity = new_capacity;
3016 uint16_t *array = container->array;
3017
3018 if (preserve) {
3019 container->array =
3020 (uint16_t *)realloc(array, new_capacity * sizeof(uint16_t));
3021 } else {
3022 // Jon Strabala reports that some tools complain otherwise
3023 if (array != NULL) {
3024 free(array);
3025 }
3026 container->array = (uint16_t *)malloc(new_capacity * sizeof(uint16_t));
3027 }
3028
3029 // handle the case where realloc fails
3030 if (container->array == NULL) {
3031 fprintf(stderr, "could not allocate memory\n");
3032 }
3033 assert(container->array != NULL);
3034 }
3035
3036 /* Copy one container into another. We assume that they are distinct. */
3037 void array_container_copy(const array_container_t *src,
3038 array_container_t *dst) {
3039 const int32_t cardinality = src->cardinality;
3040 if (cardinality > dst->capacity) {
3041 array_container_grow(dst, cardinality, false);
3042 }
3043
3044 dst->cardinality = cardinality;
3045 memcpy(dst->array, src->array, cardinality * sizeof(uint16_t));
3046 }
3047
3048 void array_container_add_from_range(array_container_t *arr, uint32_t min,
3049 uint32_t max, uint16_t step) {
3050 for (uint32_t value = min; value < max; value += step) {
3051 array_container_append(arr, value);
3052 }
3053 }
3054
3055 /* Computes the union of array1 and array2 and write the result to arrayout.
3056 * It is assumed that arrayout is distinct from both array1 and array2.
3057 */
3058 void array_container_union(const array_container_t *array_1,
3059 const array_container_t *array_2,
3060 array_container_t *out) {
3061 const int32_t card_1 = array_1->cardinality, card_2 = array_2->cardinality;
3062 const int32_t max_cardinality = card_1 + card_2;
3063
3064 if (out->capacity < max_cardinality) {
3065 array_container_grow(out, max_cardinality, false);
3066 }
3067 out->cardinality = (int32_t)fast_union_uint16(array_1->array, card_1,
3068 array_2->array, card_2, out->array);
3069
3070 }
3071
3072 /* Computes the difference of array1 and array2 and write the result
3073 * to array out.
3074 * Array out does not need to be distinct from array_1
3075 */
3076 void array_container_andnot(const array_container_t *array_1,
3077 const array_container_t *array_2,
3078 array_container_t *out) {
3079 if (out->capacity < array_1->cardinality)
3080 array_container_grow(out, array_1->cardinality, false);
3081 #ifdef ROARING_VECTOR_OPERATIONS_ENABLED
3082 if((out != array_1) && (out != array_2)) {
3083 out->cardinality =
3084 difference_vector16(array_1->array, array_1->cardinality,
3085 array_2->array, array_2->cardinality, out->array);
3086 } else {
3087 out->cardinality =
3088 difference_uint16(array_1->array, array_1->cardinality, array_2->array,
3089 array_2->cardinality, out->array);
3090 }
3091 #else
3092 out->cardinality =
3093 difference_uint16(array_1->array, array_1->cardinality, array_2->array,
3094 array_2->cardinality, out->array);
3095 #endif
3096 }
3097
3098 /* Computes the symmetric difference of array1 and array2 and write the
3099 * result
3100 * to arrayout.
3101 * It is assumed that arrayout is distinct from both array1 and array2.
3102 */
3103 void array_container_xor(const array_container_t *array_1,
3104 const array_container_t *array_2,
3105 array_container_t *out) {
3106 const int32_t card_1 = array_1->cardinality, card_2 = array_2->cardinality;
3107 const int32_t max_cardinality = card_1 + card_2;
3108 if (out->capacity < max_cardinality) {
3109 array_container_grow(out, max_cardinality, false);
3110 }
3111
3112 #ifdef ROARING_VECTOR_OPERATIONS_ENABLED
3113 out->cardinality =
3114 xor_vector16(array_1->array, array_1->cardinality, array_2->array,
3115 array_2->cardinality, out->array);
3116 #else
3117 out->cardinality =
3118 xor_uint16(array_1->array, array_1->cardinality, array_2->array,
3119 array_2->cardinality, out->array);
3120 #endif
3121 }
3122
3123 static inline int32_t minimum_int32(int32_t a, int32_t b) {
3124 return (a < b) ? a : b;
3125 }
3126
3127 /* computes the intersection of array1 and array2 and write the result to
3128 * arrayout.
3129 * It is assumed that arrayout is distinct from both array1 and array2.
3130 * */
3131 void array_container_intersection(const array_container_t *array1,
3132 const array_container_t *array2,
3133 array_container_t *out) {
3134 int32_t card_1 = array1->cardinality, card_2 = array2->cardinality,
3135 min_card = minimum_int32(card_1, card_2);
3136 const int threshold = 64; // subject to tuning
3137 #ifdef USEAVX
3138 if (out->capacity < min_card) {
3139 array_container_grow(out, min_card + sizeof(__m128i) / sizeof(uint16_t),
3140 false);
3141 }
3142 #else
3143 if (out->capacity < min_card) {
3144 array_container_grow(out, min_card, false);
3145 }
3146 #endif
3147
3148 if (card_1 * threshold < card_2) {
3149 out->cardinality = intersect_skewed_uint16(
3150 array1->array, card_1, array2->array, card_2, out->array);
3151 } else if (card_2 * threshold < card_1) {
3152 out->cardinality = intersect_skewed_uint16(
3153 array2->array, card_2, array1->array, card_1, out->array);
3154 } else {
3155 #ifdef USEAVX
3156 out->cardinality = intersect_vector16(
3157 array1->array, card_1, array2->array, card_2, out->array);
3158 #else
3159 out->cardinality = intersect_uint16(array1->array, card_1,
3160 array2->array, card_2, out->array);
3161 #endif
3162 }
3163 }
3164
3165 /* computes the size of the intersection of array1 and array2
3166 * */
3167 int array_container_intersection_cardinality(const array_container_t *array1,
3168 const array_container_t *array2) {
3169 int32_t card_1 = array1->cardinality, card_2 = array2->cardinality;
3170 const int threshold = 64; // subject to tuning
3171 if (card_1 * threshold < card_2) {
3172 return intersect_skewed_uint16_cardinality(array1->array, card_1,
3173 array2->array, card_2);
3174 } else if (card_2 * threshold < card_1) {
3175 return intersect_skewed_uint16_cardinality(array2->array, card_2,
3176 array1->array, card_1);
3177 } else {
3178 #ifdef USEAVX
3179 return intersect_vector16_cardinality(array1->array, card_1,
3180 array2->array, card_2);
3181 #else
3182 return intersect_uint16_cardinality(array1->array, card_1,
3183 array2->array, card_2);
3184 #endif
3185 }
3186 }
3187
3188 bool array_container_intersect(const array_container_t *array1,
3189 const array_container_t *array2) {
3190 int32_t card_1 = array1->cardinality, card_2 = array2->cardinality;
3191 const int threshold = 64; // subject to tuning
3192 if (card_1 * threshold < card_2) {
3193 return intersect_skewed_uint16_nonempty(
3194 array1->array, card_1, array2->array, card_2);
3195 } else if (card_2 * threshold < card_1) {
3196 return intersect_skewed_uint16_nonempty(
3197 array2->array, card_2, array1->array, card_1);
3198 } else {
3199 // we do not bother vectorizing
3200 return intersect_uint16_nonempty(array1->array, card_1,
3201 array2->array, card_2);
3202 }
3203 }
3204
3205 /* computes the intersection of array1 and array2 and write the result to
3206 * array1.
3207 * */
3208 void array_container_intersection_inplace(array_container_t *src_1,
3209 const array_container_t *src_2) {
3210 // todo: can any of this be vectorized?
3211 int32_t card_1 = src_1->cardinality, card_2 = src_2->cardinality;
3212 const int threshold = 64; // subject to tuning
3213 if (card_1 * threshold < card_2) {
3214 src_1->cardinality = intersect_skewed_uint16(
3215 src_1->array, card_1, src_2->array, card_2, src_1->array);
3216 } else if (card_2 * threshold < card_1) {
3217 src_1->cardinality = intersect_skewed_uint16(
3218 src_2->array, card_2, src_1->array, card_1, src_1->array);
3219 } else {
3220 src_1->cardinality = intersect_uint16(
3221 src_1->array, card_1, src_2->array, card_2, src_1->array);
3222 }
3223 }
3224
3225 int array_container_to_uint32_array(void *vout, const array_container_t *cont,
3226 uint32_t base) {
3227 int outpos = 0;
3228 uint32_t *out = (uint32_t *)vout;
3229 for (int i = 0; i < cont->cardinality; ++i) {
3230 const uint32_t val = base + cont->array[i];
3231 memcpy(out + outpos, &val,
3232 sizeof(uint32_t)); // should be compiled as a MOV on x64
3233 outpos++;
3234 }
3235 return outpos;
3236 }
3237
3238 void array_container_printf(const array_container_t *v) {
3239 if (v->cardinality == 0) {
3240 printf("{}");
3241 return;
3242 }
3243 printf("{");
3244 printf("%d", v->array[0]);
3245 for (int i = 1; i < v->cardinality; ++i) {
3246 printf(",%d", v->array[i]);
3247 }
3248 printf("}");
3249 }
3250
3251 void array_container_printf_as_uint32_array(const array_container_t *v,
3252 uint32_t base) {
3253 if (v->cardinality == 0) {
3254 return;
3255 }
3256 printf("%u", v->array[0] + base);
3257 for (int i = 1; i < v->cardinality; ++i) {
3258 printf(",%u", v->array[i] + base);
3259 }
3260 }
3261
3262 /* Compute the number of runs */
3263 int32_t array_container_number_of_runs(const array_container_t *a) {
3264 // Can SIMD work here?
3265 int32_t nr_runs = 0;
3266 int32_t prev = -2;
3267 for (const uint16_t *p = a->array; p != a->array + a->cardinality; ++p) {
3268 if (*p != prev + 1) nr_runs++;
3269 prev = *p;
3270 }
3271 return nr_runs;
3272 }
3273
3274 int32_t array_container_serialize(const array_container_t *container, char *buf) {
3275 int32_t l, off;
3276 uint16_t cardinality = (uint16_t)container->cardinality;
3277
3278 memcpy(buf, &cardinality, off = sizeof(cardinality));
3279 l = sizeof(uint16_t) * container->cardinality;
3280 if (l) memcpy(&buf[off], container->array, l);
3281
3282 return (off + l);
3283 }
3284
3285 /**
3286 * Writes the underlying array to buf, outputs how many bytes were written.
3287 * The number of bytes written should be
3288 * array_container_size_in_bytes(container).
3289 *
3290 */
3291 int32_t array_container_write(const array_container_t *container, char *buf) {
3292 memcpy(buf, container->array, container->cardinality * sizeof(uint16_t));
3293 return array_container_size_in_bytes(container);
3294 }
3295
3296 bool array_container_is_subset(const array_container_t *container1,
3297 const array_container_t *container2) {
3298 if (container1->cardinality > container2->cardinality) {
3299 return false;
3300 }
3301 int i1 = 0, i2 = 0;
3302 while (i1 < container1->cardinality && i2 < container2->cardinality) {
3303 if (container1->array[i1] == container2->array[i2]) {
3304 i1++;
3305 i2++;
3306 } else if (container1->array[i1] > container2->array[i2]) {
3307 i2++;
3308 } else { // container1->array[i1] < container2->array[i2]
3309 return false;
3310 }
3311 }
3312 if (i1 == container1->cardinality) {
3313 return true;
3314 } else {
3315 return false;
3316 }
3317 }
3318
3319 int32_t array_container_read(int32_t cardinality, array_container_t *container,
3320 const char *buf) {
3321 if (container->capacity < cardinality) {
3322 array_container_grow(container, cardinality, false);
3323 }
3324 container->cardinality = cardinality;
3325 memcpy(container->array, buf, container->cardinality * sizeof(uint16_t));
3326
3327 return array_container_size_in_bytes(container);
3328 }
3329
3330 uint32_t array_container_serialization_len(const array_container_t *container) {
3331 return (sizeof(uint16_t) /* container->cardinality converted to 16 bit */ +
3332 (sizeof(uint16_t) * container->cardinality));
3333 }
3334
3335 void *array_container_deserialize(const char *buf, size_t buf_len) {
3336 array_container_t *ptr;
3337
3338 if (buf_len < 2) /* capacity converted to 16 bit */
3339 return (NULL);
3340 else
3341 buf_len -= 2;
3342
3343 if ((ptr = (array_container_t *)malloc(sizeof(array_container_t))) !=
3344 NULL) {
3345 size_t len;
3346 int32_t off;
3347 uint16_t cardinality;
3348
3349 memcpy(&cardinality, buf, off = sizeof(cardinality));
3350
3351 ptr->capacity = ptr->cardinality = (uint32_t)cardinality;
3352 len = sizeof(uint16_t) * ptr->cardinality;
3353
3354 if (len != buf_len) {
3355 free(ptr);
3356 return (NULL);
3357 }
3358
3359 if ((ptr->array = (uint16_t *)malloc(sizeof(uint16_t) *
3360 ptr->capacity)) == NULL) {
3361 free(ptr);
3362 return (NULL);
3363 }
3364
3365 if (len) memcpy(ptr->array, &buf[off], len);
3366
3367 /* Check if returned values are monotonically increasing */
3368 for (int32_t i = 0, j = 0; i < ptr->cardinality; i++) {
3369 if (ptr->array[i] < j) {
3370 free(ptr->array);
3371 free(ptr);
3372 return (NULL);
3373 } else
3374 j = ptr->array[i];
3375 }
3376 }
3377
3378 return (ptr);
3379 }
3380
3381 bool array_container_iterate(const array_container_t *cont, uint32_t base,
3382 roaring_iterator iterator, void *ptr) {
3383 for (int i = 0; i < cont->cardinality; i++)
3384 if (!iterator(cont->array[i] + base, ptr)) return false;
3385 return true;
3386 }
3387
3388 bool array_container_iterate64(const array_container_t *cont, uint32_t base,
3389 roaring_iterator64 iterator, uint64_t high_bits,
3390 void *ptr) {
3391 for (int i = 0; i < cont->cardinality; i++)
3392 if (!iterator(high_bits | (uint64_t)(cont->array[i] + base), ptr))
3393 return false;
3394 return true;
3395 }
3396 /* end file src/containers/array.c */
3397 /* begin file src/containers/bitset.c */
3398 /*
3399 * bitset.c
3400 *
3401 */
3402 #ifndef _POSIX_C_SOURCE
3403 #define _POSIX_C_SOURCE 200809L
3404 #endif
3405 #include <assert.h>
3406 #include <stdio.h>
3407 #include <stdlib.h>
3408 #include <string.h>
3409
3410
3411 void bitset_container_clear(bitset_container_t *bitset) {
3412 memset(bitset->array, 0, sizeof(uint64_t) * BITSET_CONTAINER_SIZE_IN_WORDS);
3413 bitset->cardinality = 0;
3414 }
3415
3416 void bitset_container_set_all(bitset_container_t *bitset) {
3417 memset(bitset->array, INT64_C(-1),
3418 sizeof(uint64_t) * BITSET_CONTAINER_SIZE_IN_WORDS);
3419 bitset->cardinality = (1 << 16);
3420 }
3421
3422
3423
3424 /* Create a new bitset. Return NULL in case of failure. */
3425 bitset_container_t *bitset_container_create(void) {
3426 bitset_container_t *bitset =
3427 (bitset_container_t *)malloc(sizeof(bitset_container_t));
3428
3429 if (!bitset) {
3430 return NULL;
3431 }
3432 // sizeof(__m256i) == 32
3433 bitset->array = (uint64_t *)roaring_bitmap_aligned_malloc(
3434 32, sizeof(uint64_t) * BITSET_CONTAINER_SIZE_IN_WORDS);
3435 if (!bitset->array) {
3436 free(bitset);
3437 return NULL;
3438 }
3439 bitset_container_clear(bitset);
3440 return bitset;
3441 }
3442
3443 /* Copy one container into another. We assume that they are distinct. */
3444 void bitset_container_copy(const bitset_container_t *source,
3445 bitset_container_t *dest) {
3446 dest->cardinality = source->cardinality;
3447 memcpy(dest->array, source->array,
3448 sizeof(uint64_t) * BITSET_CONTAINER_SIZE_IN_WORDS);
3449 }
3450
3451 void bitset_container_add_from_range(bitset_container_t *bitset, uint32_t min,
3452 uint32_t max, uint16_t step) {
3453 if (step == 0) return; // refuse to crash
3454 if ((64 % step) == 0) { // step divides 64
3455 uint64_t mask = 0; // construct the repeated mask
3456 for (uint32_t value = (min % step); value < 64; value += step) {
3457 mask |= ((uint64_t)1 << value);
3458 }
3459 uint32_t firstword = min / 64;
3460 uint32_t endword = (max - 1) / 64;
3461 bitset->cardinality = (max - min + step - 1) / step;
3462 if (firstword == endword) {
3463 bitset->array[firstword] |=
3464 mask & (((~UINT64_C(0)) << (min % 64)) &
3465 ((~UINT64_C(0)) >> ((~max + 1) % 64)));
3466 return;
3467 }
3468 bitset->array[firstword] = mask & ((~UINT64_C(0)) << (min % 64));
3469 for (uint32_t i = firstword + 1; i < endword; i++)
3470 bitset->array[i] = mask;
3471 bitset->array[endword] = mask & ((~UINT64_C(0)) >> ((~max + 1) % 64));
3472 } else {
3473 for (uint32_t value = min; value < max; value += step) {
3474 bitset_container_add(bitset, value);
3475 }
3476 }
3477 }
3478
3479 /* Free memory. */
3480 void bitset_container_free(bitset_container_t *bitset) {
3481 if(bitset->array != NULL) {// Jon Strabala reports that some tools complain otherwise
3482 roaring_bitmap_aligned_free(bitset->array);
3483 bitset->array = NULL; // pedantic
3484 }
3485 free(bitset);
3486 }
3487
3488 /* duplicate container. */
3489 bitset_container_t *bitset_container_clone(const bitset_container_t *src) {
3490 bitset_container_t *bitset =
3491 (bitset_container_t *)malloc(sizeof(bitset_container_t));
3492 assert(bitset);
3493
3494 // sizeof(__m256i) == 32
3495 bitset->array = (uint64_t *)roaring_bitmap_aligned_malloc(
3496 32, sizeof(uint64_t) * BITSET_CONTAINER_SIZE_IN_WORDS);
3497 assert(bitset->array);
3498 bitset->cardinality = src->cardinality;
3499 memcpy(bitset->array, src->array,
3500 sizeof(uint64_t) * BITSET_CONTAINER_SIZE_IN_WORDS);
3501 return bitset;
3502 }
3503
3504 void bitset_container_set_range(bitset_container_t *bitset, uint32_t begin,
3505 uint32_t end) {
3506 bitset_set_range(bitset->array, begin, end);
3507 bitset->cardinality =
3508 bitset_container_compute_cardinality(bitset); // could be smarter
3509 }
3510
3511
3512 bool bitset_container_intersect(const bitset_container_t *src_1,
3513 const bitset_container_t *src_2) {
3514 // could vectorize, but this is probably already quite fast in practice
3515 const uint64_t * __restrict__ array_1 = src_1->array;
3516 const uint64_t * __restrict__ array_2 = src_2->array;
3517 for (int i = 0; i < BITSET_CONTAINER_SIZE_IN_WORDS; i ++) {
3518 if((array_1[i] & array_2[i]) != 0) return true;
3519 }
3520 return false;
3521 }
3522
3523
3524 #ifdef USEAVX
3525 #ifndef WORDS_IN_AVX2_REG
3526 #define WORDS_IN_AVX2_REG sizeof(__m256i) / sizeof(uint64_t)
3527 #endif
3528 /* Get the number of bits set (force computation) */
3529 int bitset_container_compute_cardinality(const bitset_container_t *bitset) {
3530 return (int) avx2_harley_seal_popcount256(
3531 (const __m256i *)bitset->array,
3532 BITSET_CONTAINER_SIZE_IN_WORDS / (WORDS_IN_AVX2_REG));
3533 }
3534
3535 #elif defined(USENEON)
3536 int bitset_container_compute_cardinality(const bitset_container_t *bitset) {
3537 uint16x8_t n0 = vdupq_n_u16(0);
3538 uint16x8_t n1 = vdupq_n_u16(0);
3539 uint16x8_t n2 = vdupq_n_u16(0);
3540 uint16x8_t n3 = vdupq_n_u16(0);
3541 for (size_t i = 0; i < BITSET_CONTAINER_SIZE_IN_WORDS; i += 8) {
3542 uint64x2_t c0 = vld1q_u64(&bitset->array[i + 0]);
3543 n0 = vaddq_u16(n0, vpaddlq_u8(vcntq_u8(vreinterpretq_u8_u64(c0))));
3544 uint64x2_t c1 = vld1q_u64(&bitset->array[i + 2]);
3545 n1 = vaddq_u16(n1, vpaddlq_u8(vcntq_u8(vreinterpretq_u8_u64(c1))));
3546 uint64x2_t c2 = vld1q_u64(&bitset->array[i + 4]);
3547 n2 = vaddq_u16(n2, vpaddlq_u8(vcntq_u8(vreinterpretq_u8_u64(c2))));
3548 uint64x2_t c3 = vld1q_u64(&bitset->array[i + 6]);
3549 n3 = vaddq_u16(n3, vpaddlq_u8(vcntq_u8(vreinterpretq_u8_u64(c3))));
3550 }
3551 uint64x2_t n = vdupq_n_u64(0);
3552 n = vaddq_u64(n, vpaddlq_u32(vpaddlq_u16(n0)));
3553 n = vaddq_u64(n, vpaddlq_u32(vpaddlq_u16(n1)));
3554 n = vaddq_u64(n, vpaddlq_u32(vpaddlq_u16(n2)));
3555 n = vaddq_u64(n, vpaddlq_u32(vpaddlq_u16(n3)));
3556 return vgetq_lane_u64(n, 0) + vgetq_lane_u64(n, 1);
3557 }
3558
3559 #else
3560
3561 /* Get the number of bits set (force computation) */
3562 int bitset_container_compute_cardinality(const bitset_container_t *bitset) {
3563 const uint64_t *array = bitset->array;
3564 int32_t sum = 0;
3565 for (int i = 0; i < BITSET_CONTAINER_SIZE_IN_WORDS; i += 4) {
3566 sum += hamming(array[i]);
3567 sum += hamming(array[i + 1]);
3568 sum += hamming(array[i + 2]);
3569 sum += hamming(array[i + 3]);
3570 }
3571 return sum;
3572 }
3573
3574 #endif
3575
3576 #ifdef USEAVX
3577
3578 #define BITSET_CONTAINER_FN_REPEAT 8
3579 #ifndef WORDS_IN_AVX2_REG
3580 #define WORDS_IN_AVX2_REG sizeof(__m256i) / sizeof(uint64_t)
3581 #endif
3582 #define LOOP_SIZE \
3583 BITSET_CONTAINER_SIZE_IN_WORDS / \
3584 ((WORDS_IN_AVX2_REG)*BITSET_CONTAINER_FN_REPEAT)
3585
3586 /* Computes a binary operation (eg union) on bitset1 and bitset2 and write the
3587 result to bitsetout */
3588 // clang-format off
3589 #define BITSET_CONTAINER_FN(opname, opsymbol, avx_intrinsic, neon_intrinsic) \
3590 int bitset_container_##opname##_nocard(const bitset_container_t *src_1, \
3591 const bitset_container_t *src_2, \
3592 bitset_container_t *dst) { \
3593 const uint8_t * __restrict__ array_1 = (const uint8_t *)src_1->array; \
3594 const uint8_t * __restrict__ array_2 = (const uint8_t *)src_2->array; \
3595 /* not using the blocking optimization for some reason*/ \
3596 uint8_t *out = (uint8_t*)dst->array; \
3597 const int innerloop = 8; \
3598 for (size_t i = 0; \
3599 i < BITSET_CONTAINER_SIZE_IN_WORDS / (WORDS_IN_AVX2_REG); \
3600 i+=innerloop) {\
3601 __m256i A1, A2, AO; \
3602 A1 = _mm256_lddqu_si256((const __m256i *)(array_1)); \
3603 A2 = _mm256_lddqu_si256((const __m256i *)(array_2)); \
3604 AO = avx_intrinsic(A2, A1); \
3605 _mm256_storeu_si256((__m256i *)out, AO); \
3606 A1 = _mm256_lddqu_si256((const __m256i *)(array_1 + 32)); \
3607 A2 = _mm256_lddqu_si256((const __m256i *)(array_2 + 32)); \
3608 AO = avx_intrinsic(A2, A1); \
3609 _mm256_storeu_si256((__m256i *)(out+32), AO); \
3610 A1 = _mm256_lddqu_si256((const __m256i *)(array_1 + 64)); \
3611 A2 = _mm256_lddqu_si256((const __m256i *)(array_2 + 64)); \
3612 AO = avx_intrinsic(A2, A1); \
3613 _mm256_storeu_si256((__m256i *)(out+64), AO); \
3614 A1 = _mm256_lddqu_si256((const __m256i *)(array_1 + 96)); \
3615 A2 = _mm256_lddqu_si256((const __m256i *)(array_2 + 96)); \
3616 AO = avx_intrinsic(A2, A1); \
3617 _mm256_storeu_si256((__m256i *)(out+96), AO); \
3618 A1 = _mm256_lddqu_si256((const __m256i *)(array_1 + 128)); \
3619 A2 = _mm256_lddqu_si256((const __m256i *)(array_2 + 128)); \
3620 AO = avx_intrinsic(A2, A1); \
3621 _mm256_storeu_si256((__m256i *)(out+128), AO); \
3622 A1 = _mm256_lddqu_si256((const __m256i *)(array_1 + 160)); \
3623 A2 = _mm256_lddqu_si256((const __m256i *)(array_2 + 160)); \
3624 AO = avx_intrinsic(A2, A1); \
3625 _mm256_storeu_si256((__m256i *)(out+160), AO); \
3626 A1 = _mm256_lddqu_si256((const __m256i *)(array_1 + 192)); \
3627 A2 = _mm256_lddqu_si256((const __m256i *)(array_2 + 192)); \
3628 AO = avx_intrinsic(A2, A1); \
3629 _mm256_storeu_si256((__m256i *)(out+192), AO); \
3630 A1 = _mm256_lddqu_si256((const __m256i *)(array_1 + 224)); \
3631 A2 = _mm256_lddqu_si256((const __m256i *)(array_2 + 224)); \
3632 AO = avx_intrinsic(A2, A1); \
3633 _mm256_storeu_si256((__m256i *)(out+224), AO); \
3634 out+=256; \
3635 array_1 += 256; \
3636 array_2 += 256; \
3637 } \
3638 dst->cardinality = BITSET_UNKNOWN_CARDINALITY; \
3639 return dst->cardinality; \
3640 } \
3641 /* next, a version that updates cardinality*/ \
3642 int bitset_container_##opname(const bitset_container_t *src_1, \
3643 const bitset_container_t *src_2, \
3644 bitset_container_t *dst) { \
3645 const __m256i * __restrict__ array_1 = (const __m256i *) src_1->array; \
3646 const __m256i * __restrict__ array_2 = (const __m256i *) src_2->array; \
3647 __m256i *out = (__m256i *) dst->array; \
3648 dst->cardinality = (int32_t)avx2_harley_seal_popcount256andstore_##opname(array_2,\
3649 array_1, out,BITSET_CONTAINER_SIZE_IN_WORDS / (WORDS_IN_AVX2_REG));\
3650 return dst->cardinality; \
3651 } \
3652 /* next, a version that just computes the cardinality*/ \
3653 int bitset_container_##opname##_justcard(const bitset_container_t *src_1, \
3654 const bitset_container_t *src_2) { \
3655 const __m256i * __restrict__ data1 = (const __m256i *) src_1->array; \
3656 const __m256i * __restrict__ data2 = (const __m256i *) src_2->array; \
3657 return (int)avx2_harley_seal_popcount256_##opname(data2, \
3658 data1, BITSET_CONTAINER_SIZE_IN_WORDS / (WORDS_IN_AVX2_REG));\
3659 }
3660
3661 #elif defined(USENEON)
3662
3663 #define BITSET_CONTAINER_FN(opname, opsymbol, avx_intrinsic, neon_intrinsic) \
3664 int bitset_container_##opname(const bitset_container_t *src_1, \
3665 const bitset_container_t *src_2, \
3666 bitset_container_t *dst) { \
3667 const uint64_t * __restrict__ array_1 = src_1->array; \
3668 const uint64_t * __restrict__ array_2 = src_2->array; \
3669 uint64_t *out = dst->array; \
3670 uint16x8_t n0 = vdupq_n_u16(0); \
3671 uint16x8_t n1 = vdupq_n_u16(0); \
3672 uint16x8_t n2 = vdupq_n_u16(0); \
3673 uint16x8_t n3 = vdupq_n_u16(0); \
3674 for (size_t i = 0; i < BITSET_CONTAINER_SIZE_IN_WORDS; i += 8) { \
3675 uint64x2_t c0 = neon_intrinsic(vld1q_u64(&array_1[i + 0]), \
3676 vld1q_u64(&array_2[i + 0])); \
3677 n0 = vaddq_u16(n0, vpaddlq_u8(vcntq_u8(vreinterpretq_u8_u64(c0)))); \
3678 vst1q_u64(&out[i + 0], c0); \
3679 uint64x2_t c1 = neon_intrinsic(vld1q_u64(&array_1[i + 2]), \
3680 vld1q_u64(&array_2[i + 2])); \
3681 n1 = vaddq_u16(n1, vpaddlq_u8(vcntq_u8(vreinterpretq_u8_u64(c1)))); \
3682 vst1q_u64(&out[i + 2], c1); \
3683 uint64x2_t c2 = neon_intrinsic(vld1q_u64(&array_1[i + 4]), \
3684 vld1q_u64(&array_2[i + 4])); \
3685 n2 = vaddq_u16(n2, vpaddlq_u8(vcntq_u8(vreinterpretq_u8_u64(c2)))); \
3686 vst1q_u64(&out[i + 4], c2); \
3687 uint64x2_t c3 = neon_intrinsic(vld1q_u64(&array_1[i + 6]), \
3688 vld1q_u64(&array_2[i + 6])); \
3689 n3 = vaddq_u16(n3, vpaddlq_u8(vcntq_u8(vreinterpretq_u8_u64(c3)))); \
3690 vst1q_u64(&out[i + 6], c3); \
3691 } \
3692 uint64x2_t n = vdupq_n_u64(0); \
3693 n = vaddq_u64(n, vpaddlq_u32(vpaddlq_u16(n0))); \
3694 n = vaddq_u64(n, vpaddlq_u32(vpaddlq_u16(n1))); \
3695 n = vaddq_u64(n, vpaddlq_u32(vpaddlq_u16(n2))); \
3696 n = vaddq_u64(n, vpaddlq_u32(vpaddlq_u16(n3))); \
3697 dst->cardinality = vgetq_lane_u64(n, 0) + vgetq_lane_u64(n, 1); \
3698 return dst->cardinality; \
3699 } \
3700 int bitset_container_##opname##_nocard(const bitset_container_t *src_1, \
3701 const bitset_container_t *src_2, \
3702 bitset_container_t *dst) { \
3703 const uint64_t * __restrict__ array_1 = src_1->array; \
3704 const uint64_t * __restrict__ array_2 = src_2->array; \
3705 uint64_t *out = dst->array; \
3706 for (size_t i = 0; i < BITSET_CONTAINER_SIZE_IN_WORDS; i += 8) { \
3707 vst1q_u64(&out[i + 0], neon_intrinsic(vld1q_u64(&array_1[i + 0]), \
3708 vld1q_u64(&array_2[i + 0]))); \
3709 vst1q_u64(&out[i + 2], neon_intrinsic(vld1q_u64(&array_1[i + 2]), \
3710 vld1q_u64(&array_2[i + 2]))); \
3711 vst1q_u64(&out[i + 4], neon_intrinsic(vld1q_u64(&array_1[i + 4]), \
3712 vld1q_u64(&array_2[i + 4]))); \
3713 vst1q_u64(&out[i + 6], neon_intrinsic(vld1q_u64(&array_1[i + 6]), \
3714 vld1q_u64(&array_2[i + 6]))); \
3715 } \
3716 dst->cardinality = BITSET_UNKNOWN_CARDINALITY; \
3717 return dst->cardinality; \
3718 } \
3719 int bitset_container_##opname##_justcard(const bitset_container_t *src_1, \
3720 const bitset_container_t *src_2) { \
3721 const uint64_t * __restrict__ array_1 = src_1->array; \
3722 const uint64_t * __restrict__ array_2 = src_2->array; \
3723 uint16x8_t n0 = vdupq_n_u16(0); \
3724 uint16x8_t n1 = vdupq_n_u16(0); \
3725 uint16x8_t n2 = vdupq_n_u16(0); \
3726 uint16x8_t n3 = vdupq_n_u16(0); \
3727 for (size_t i = 0; i < BITSET_CONTAINER_SIZE_IN_WORDS; i += 8) { \
3728 uint64x2_t c0 = neon_intrinsic(vld1q_u64(&array_1[i + 0]), \
3729 vld1q_u64(&array_2[i + 0])); \
3730 n0 = vaddq_u16(n0, vpaddlq_u8(vcntq_u8(vreinterpretq_u8_u64(c0)))); \
3731 uint64x2_t c1 = neon_intrinsic(vld1q_u64(&array_1[i + 2]), \
3732 vld1q_u64(&array_2[i + 2])); \
3733 n1 = vaddq_u16(n1, vpaddlq_u8(vcntq_u8(vreinterpretq_u8_u64(c1)))); \
3734 uint64x2_t c2 = neon_intrinsic(vld1q_u64(&array_1[i + 4]), \
3735 vld1q_u64(&array_2[i + 4])); \
3736 n2 = vaddq_u16(n2, vpaddlq_u8(vcntq_u8(vreinterpretq_u8_u64(c2)))); \
3737 uint64x2_t c3 = neon_intrinsic(vld1q_u64(&array_1[i + 6]), \
3738 vld1q_u64(&array_2[i + 6])); \
3739 n3 = vaddq_u16(n3, vpaddlq_u8(vcntq_u8(vreinterpretq_u8_u64(c3)))); \
3740 } \
3741 uint64x2_t n = vdupq_n_u64(0); \
3742 n = vaddq_u64(n, vpaddlq_u32(vpaddlq_u16(n0))); \
3743 n = vaddq_u64(n, vpaddlq_u32(vpaddlq_u16(n1))); \
3744 n = vaddq_u64(n, vpaddlq_u32(vpaddlq_u16(n2))); \
3745 n = vaddq_u64(n, vpaddlq_u32(vpaddlq_u16(n3))); \
3746 return vgetq_lane_u64(n, 0) + vgetq_lane_u64(n, 1); \
3747 }
3748
3749 #else /* not USEAVX */
3750
3751 #define BITSET_CONTAINER_FN(opname, opsymbol, avx_intrinsic, neon_intrinsic) \
3752 int bitset_container_##opname(const bitset_container_t *src_1, \
3753 const bitset_container_t *src_2, \
3754 bitset_container_t *dst) { \
3755 const uint64_t * __restrict__ array_1 = src_1->array; \
3756 const uint64_t * __restrict__ array_2 = src_2->array; \
3757 uint64_t *out = dst->array; \
3758 int32_t sum = 0; \
3759 for (size_t i = 0; i < BITSET_CONTAINER_SIZE_IN_WORDS; i += 2) { \
3760 const uint64_t word_1 = (array_1[i])opsymbol(array_2[i]), \
3761 word_2 = (array_1[i + 1])opsymbol(array_2[i + 1]); \
3762 out[i] = word_1; \
3763 out[i + 1] = word_2; \
3764 sum += hamming(word_1); \
3765 sum += hamming(word_2); \
3766 } \
3767 dst->cardinality = sum; \
3768 return dst->cardinality; \
3769 } \
3770 int bitset_container_##opname##_nocard(const bitset_container_t *src_1, \
3771 const bitset_container_t *src_2, \
3772 bitset_container_t *dst) { \
3773 const uint64_t * __restrict__ array_1 = src_1->array; \
3774 const uint64_t * __restrict__ array_2 = src_2->array; \
3775 uint64_t *out = dst->array; \
3776 for (size_t i = 0; i < BITSET_CONTAINER_SIZE_IN_WORDS; i++) { \
3777 out[i] = (array_1[i])opsymbol(array_2[i]); \
3778 } \
3779 dst->cardinality = BITSET_UNKNOWN_CARDINALITY; \
3780 return dst->cardinality; \
3781 } \
3782 int bitset_container_##opname##_justcard(const bitset_container_t *src_1, \
3783 const bitset_container_t *src_2) { \
3784 const uint64_t * __restrict__ array_1 = src_1->array; \
3785 const uint64_t * __restrict__ array_2 = src_2->array; \
3786 int32_t sum = 0; \
3787 for (size_t i = 0; i < BITSET_CONTAINER_SIZE_IN_WORDS; i += 2) { \
3788 const uint64_t word_1 = (array_1[i])opsymbol(array_2[i]), \
3789 word_2 = (array_1[i + 1])opsymbol(array_2[i + 1]); \
3790 sum += hamming(word_1); \
3791 sum += hamming(word_2); \
3792 } \
3793 return sum; \
3794 }
3795
3796 #endif
3797
3798 // we duplicate the function because other containers use the "or" term, makes API more consistent
3799 BITSET_CONTAINER_FN(or, |, _mm256_or_si256, vorrq_u64)
3800 BITSET_CONTAINER_FN(union, |, _mm256_or_si256, vorrq_u64)
3801
3802 // we duplicate the function because other containers use the "intersection" term, makes API more consistent
3803 BITSET_CONTAINER_FN(and, &, _mm256_and_si256, vandq_u64)
3804 BITSET_CONTAINER_FN(intersection, &, _mm256_and_si256, vandq_u64)
3805
3806 BITSET_CONTAINER_FN(xor, ^, _mm256_xor_si256, veorq_u64)
3807 BITSET_CONTAINER_FN(andnot, &~, _mm256_andnot_si256, vbicq_u64)
3808 // clang-format On
3809
3810
3811
3812 int bitset_container_to_uint32_array( void *vout, const bitset_container_t *cont, uint32_t base) {
3813 #ifdef USEAVX2FORDECODING
3814 if(cont->cardinality >= 8192)// heuristic
3815 return (int) bitset_extract_setbits_avx2(cont->array, BITSET_CONTAINER_SIZE_IN_WORDS, vout,cont->cardinality,base);
3816 else
3817 return (int) bitset_extract_setbits(cont->array, BITSET_CONTAINER_SIZE_IN_WORDS, vout,base);
3818 #else
3819 return (int) bitset_extract_setbits(cont->array, BITSET_CONTAINER_SIZE_IN_WORDS, vout,base);
3820 #endif
3821 }
3822
3823 /*
3824 * Print this container using printf (useful for debugging).
3825 */
3826 void bitset_container_printf(const bitset_container_t * v) {
3827 printf("{");
3828 uint32_t base = 0;
3829 bool iamfirst = true;// TODO: rework so that this is not necessary yet still readable
3830 for (int i = 0; i < BITSET_CONTAINER_SIZE_IN_WORDS; ++i) {
3831 uint64_t w = v->array[i];
3832 while (w != 0) {
3833 uint64_t t = w & (~w + 1);
3834 int r = __builtin_ctzll(w);
3835 if(iamfirst) {// predicted to be false
3836 printf("%u",base + r);
3837 iamfirst = false;
3838 } else {
3839 printf(",%u",base + r);
3840 }
3841 w ^= t;
3842 }
3843 base += 64;
3844 }
3845 printf("}");
3846 }
3847
3848
3849 /*
3850 * Print this container using printf as a comma-separated list of 32-bit integers starting at base.
3851 */
3852 void bitset_container_printf_as_uint32_array(const bitset_container_t * v, uint32_t base) {
3853 bool iamfirst = true;// TODO: rework so that this is not necessary yet still readable
3854 for (int i = 0; i < BITSET_CONTAINER_SIZE_IN_WORDS; ++i) {
3855 uint64_t w = v->array[i];
3856 while (w != 0) {
3857 uint64_t t = w & (~w + 1);
3858 int r = __builtin_ctzll(w);
3859 if(iamfirst) {// predicted to be false
3860 printf("%u", r + base);
3861 iamfirst = false;
3862 } else {
3863 printf(",%u",r + base);
3864 }
3865 w ^= t;
3866 }
3867 base += 64;
3868 }
3869 }
3870
3871
3872 // TODO: use the fast lower bound, also
3873 int bitset_container_number_of_runs(bitset_container_t *b) {
3874 int num_runs = 0;
3875 uint64_t next_word = b->array[0];
3876
3877 for (int i = 0; i < BITSET_CONTAINER_SIZE_IN_WORDS-1; ++i) {
3878 uint64_t word = next_word;
3879 next_word = b->array[i+1];
3880 num_runs += hamming((~word) & (word << 1)) + ( (word >> 63) & ~next_word);
3881 }
3882
3883 uint64_t word = next_word;
3884 num_runs += hamming((~word) & (word << 1));
3885 if((word & 0x8000000000000000ULL) != 0)
3886 num_runs++;
3887 return num_runs;
3888 }
3889
3890 int32_t bitset_container_serialize(const bitset_container_t *container, char *buf) {
3891 int32_t l = sizeof(uint64_t) * BITSET_CONTAINER_SIZE_IN_WORDS;
3892 memcpy(buf, container->array, l);
3893 return(l);
3894 }
3895
3896
3897
3898 int32_t bitset_container_write(const bitset_container_t *container,
3899 char *buf) {
3900 memcpy(buf, container->array, BITSET_CONTAINER_SIZE_IN_WORDS * sizeof(uint64_t));
3901 return bitset_container_size_in_bytes(container);
3902 }
3903
3904
3905 int32_t bitset_container_read(int32_t cardinality, bitset_container_t *container,
3906 const char *buf) {
3907 container->cardinality = cardinality;
3908 memcpy(container->array, buf, BITSET_CONTAINER_SIZE_IN_WORDS * sizeof(uint64_t));
3909 return bitset_container_size_in_bytes(container);
3910 }
3911
3912 uint32_t bitset_container_serialization_len(void) {
3913 return(sizeof(uint64_t) * BITSET_CONTAINER_SIZE_IN_WORDS);
3914 }
3915
3916 void* bitset_container_deserialize(const char *buf, size_t buf_len) {
3917 bitset_container_t *ptr;
3918 size_t l = sizeof(uint64_t) * BITSET_CONTAINER_SIZE_IN_WORDS;
3919
3920 if(l != buf_len)
3921 return(NULL);
3922
3923 if((ptr = (bitset_container_t *)malloc(sizeof(bitset_container_t))) != NULL) {
3924 memcpy(ptr, buf, sizeof(bitset_container_t));
3925 // sizeof(__m256i) == 32
3926 ptr->array = (uint64_t *) roaring_bitmap_aligned_malloc(32, l);
3927 if (! ptr->array) {
3928 free(ptr);
3929 return NULL;
3930 }
3931 memcpy(ptr->array, buf, l);
3932 ptr->cardinality = bitset_container_compute_cardinality(ptr);
3933 }
3934
3935 return((void*)ptr);
3936 }
3937
3938 bool bitset_container_iterate(const bitset_container_t *cont, uint32_t base, roaring_iterator iterator, void *ptr) {
3939 for (int32_t i = 0; i < BITSET_CONTAINER_SIZE_IN_WORDS; ++i ) {
3940 uint64_t w = cont->array[i];
3941 while (w != 0) {
3942 uint64_t t = w & (~w + 1);
3943 int r = __builtin_ctzll(w);
3944 if(!iterator(r + base, ptr)) return false;
3945 w ^= t;
3946 }
3947 base += 64;
3948 }
3949 return true;
3950 }
3951
3952 bool bitset_container_iterate64(const bitset_container_t *cont, uint32_t base, roaring_iterator64 iterator, uint64_t high_bits, void *ptr) {
3953 for (int32_t i = 0; i < BITSET_CONTAINER_SIZE_IN_WORDS; ++i ) {
3954 uint64_t w = cont->array[i];
3955 while (w != 0) {
3956 uint64_t t = w & (~w + 1);
3957 int r = __builtin_ctzll(w);
3958 if(!iterator(high_bits | (uint64_t)(r + base), ptr)) return false;
3959 w ^= t;
3960 }
3961 base += 64;
3962 }
3963 return true;
3964 }
3965
3966
3967 bool bitset_container_equals(const bitset_container_t *container1, const bitset_container_t *container2) {
3968 if((container1->cardinality != BITSET_UNKNOWN_CARDINALITY) && (container2->cardinality != BITSET_UNKNOWN_CARDINALITY)) {
3969 if(container1->cardinality != container2->cardinality) {
3970 return false;
3971 }
3972 if (container1->cardinality == INT32_C(0x10000)) {
3973 return true;
3974 }
3975 }
3976 #ifdef USEAVX
3977 const __m256i *ptr1 = (const __m256i*)container1->array;
3978 const __m256i *ptr2 = (const __m256i*)container2->array;
3979 for (size_t i = 0; i < BITSET_CONTAINER_SIZE_IN_WORDS*sizeof(uint64_t)/32; i++) {
3980 __m256i r1 = _mm256_load_si256(ptr1+i);
3981 __m256i r2 = _mm256_load_si256(ptr2+i);
3982 int mask = _mm256_movemask_epi8(_mm256_cmpeq_epi8(r1, r2));
3983 if ((uint32_t)mask != UINT32_MAX) {
3984 return false;
3985 }
3986 }
3987 #else
3988 return memcmp(container1->array,
3989 container2->array,
3990 BITSET_CONTAINER_SIZE_IN_WORDS*sizeof(uint64_t)) == 0;
3991 #endif
3992 return true;
3993 }
3994
3995 bool bitset_container_is_subset(const bitset_container_t *container1,
3996 const bitset_container_t *container2) {
3997 if((container1->cardinality != BITSET_UNKNOWN_CARDINALITY) && (container2->cardinality != BITSET_UNKNOWN_CARDINALITY)) {
3998 if(container1->cardinality > container2->cardinality) {
3999 return false;
4000 }
4001 }
4002 for(int32_t i = 0; i < BITSET_CONTAINER_SIZE_IN_WORDS; ++i ) {
4003 if((container1->array[i] & container2->array[i]) != container1->array[i]) {
4004 return false;
4005 }
4006 }
4007 return true;
4008 }
4009
4010 bool bitset_container_select(const bitset_container_t *container, uint32_t *start_rank, uint32_t rank, uint32_t *element) {
4011 int card = bitset_container_cardinality(container);
4012 if(rank >= *start_rank + card) {
4013 *start_rank += card;
4014 return false;
4015 }
4016 const uint64_t *array = container->array;
4017 int32_t size;
4018 for (int i = 0; i < BITSET_CONTAINER_SIZE_IN_WORDS; i += 1) {
4019 size = hamming(array[i]);
4020 if(rank <= *start_rank + size) {
4021 uint64_t w = container->array[i];
4022 uint16_t base = i*64;
4023 while (w != 0) {
4024 uint64_t t = w & (~w + 1);
4025 int r = __builtin_ctzll(w);
4026 if(*start_rank == rank) {
4027 *element = r+base;
4028 return true;
4029 }
4030 w ^= t;
4031 *start_rank += 1;
4032 }
4033 }
4034 else
4035 *start_rank += size;
4036 }
4037 assert(false);
4038 __builtin_unreachable();
4039 }
4040
4041
4042 /* Returns the smallest value (assumes not empty) */
4043 uint16_t bitset_container_minimum(const bitset_container_t *container) {
4044 for (int32_t i = 0; i < BITSET_CONTAINER_SIZE_IN_WORDS; ++i ) {
4045 uint64_t w = container->array[i];
4046 if (w != 0) {
4047 int r = __builtin_ctzll(w);
4048 return r + i * 64;
4049 }
4050 }
4051 return UINT16_MAX;
4052 }
4053
4054 /* Returns the largest value (assumes not empty) */
4055 uint16_t bitset_container_maximum(const bitset_container_t *container) {
4056 for (int32_t i = BITSET_CONTAINER_SIZE_IN_WORDS - 1; i > 0; --i ) {
4057 uint64_t w = container->array[i];
4058 if (w != 0) {
4059 int r = __builtin_clzll(w);
4060 return i * 64 + 63 - r;
4061 }
4062 }
4063 return 0;
4064 }
4065
4066 /* Returns the number of values equal or smaller than x */
4067 int bitset_container_rank(const bitset_container_t *container, uint16_t x) {
4068 // credit: aqrit
4069 int sum = 0;
4070 int i = 0;
4071 for (int end = x / 64; i < end; i++){
4072 sum += hamming(container->array[i]);
4073 }
4074 uint64_t lastword = container->array[i];
4075 uint64_t lastpos = UINT64_C(1) << (x % 64);
4076 uint64_t mask = lastpos + lastpos - 1; // smear right
4077 sum += hamming(lastword & mask);
4078 return sum;
4079 }
4080
4081 /* Returns the index of the first value equal or larger than x, or -1 */
4082 int bitset_container_index_equalorlarger(const bitset_container_t *container, uint16_t x) {
4083 uint32_t x32 = x;
4084 uint32_t k = x32 / 64;
4085 uint64_t word = container->array[k];
4086 const int diff = x32 - k * 64; // in [0,64)
4087 word = (word >> diff) << diff; // a mask is faster, but we don't care
4088 while(word == 0) {
4089 k++;
4090 if(k == BITSET_CONTAINER_SIZE_IN_WORDS) return -1;
4091 word = container->array[k];
4092 }
4093 return k * 64 + __builtin_ctzll(word);
4094 }
4095 /* end file src/containers/bitset.c */
4096 /* begin file src/containers/containers.c */
4097
4098
4099 void container_free(void *container, uint8_t typecode) {
4100 switch (typecode) {
4101 case BITSET_CONTAINER_TYPE_CODE:
4102 bitset_container_free((bitset_container_t *)container);
4103 break;
4104 case ARRAY_CONTAINER_TYPE_CODE:
4105 array_container_free((array_container_t *)container);
4106 break;
4107 case RUN_CONTAINER_TYPE_CODE:
4108 run_container_free((run_container_t *)container);
4109 break;
4110 case SHARED_CONTAINER_TYPE_CODE:
4111 shared_container_free((shared_container_t *)container);
4112 break;
4113 default:
4114 assert(false);
4115 __builtin_unreachable();
4116 }
4117 }
4118
4119 void container_printf(const void *container, uint8_t typecode) {
4120 container = container_unwrap_shared(container, &typecode);
4121 switch (typecode) {
4122 case BITSET_CONTAINER_TYPE_CODE:
4123 bitset_container_printf((const bitset_container_t *)container);
4124 return;
4125 case ARRAY_CONTAINER_TYPE_CODE:
4126 array_container_printf((const array_container_t *)container);
4127 return;
4128 case RUN_CONTAINER_TYPE_CODE:
4129 run_container_printf((const run_container_t *)container);
4130 return;
4131 default:
4132 __builtin_unreachable();
4133 }
4134 }
4135
4136 void container_printf_as_uint32_array(const void *container, uint8_t typecode,
4137 uint32_t base) {
4138 container = container_unwrap_shared(container, &typecode);
4139 switch (typecode) {
4140 case BITSET_CONTAINER_TYPE_CODE:
4141 bitset_container_printf_as_uint32_array(
4142 (const bitset_container_t *)container, base);
4143 return;
4144 case ARRAY_CONTAINER_TYPE_CODE:
4145 array_container_printf_as_uint32_array(
4146 (const array_container_t *)container, base);
4147 return;
4148 case RUN_CONTAINER_TYPE_CODE:
4149 run_container_printf_as_uint32_array(
4150 (const run_container_t *)container, base);
4151 return;
4152 return;
4153 default:
4154 __builtin_unreachable();
4155 }
4156 }
4157
4158 int32_t container_serialize(const void *container, uint8_t typecode,
4159 char *buf) {
4160 container = container_unwrap_shared(container, &typecode);
4161 switch (typecode) {
4162 case BITSET_CONTAINER_TYPE_CODE:
4163 return (bitset_container_serialize((const bitset_container_t *)container,
4164 buf));
4165 case ARRAY_CONTAINER_TYPE_CODE:
4166 return (
4167 array_container_serialize((const array_container_t *)container, buf));
4168 case RUN_CONTAINER_TYPE_CODE:
4169 return (run_container_serialize((const run_container_t *)container, buf));
4170 default:
4171 assert(0);
4172 __builtin_unreachable();
4173 return (-1);
4174 }
4175 }
4176
4177 uint32_t container_serialization_len(const void *container, uint8_t typecode) {
4178 container = container_unwrap_shared(container, &typecode);
4179 switch (typecode) {
4180 case BITSET_CONTAINER_TYPE_CODE:
4181 return bitset_container_serialization_len();
4182 case ARRAY_CONTAINER_TYPE_CODE:
4183 return array_container_serialization_len(
4184 (const array_container_t *)container);
4185 case RUN_CONTAINER_TYPE_CODE:
4186 return run_container_serialization_len(
4187 (const run_container_t *)container);
4188 default:
4189 assert(0);
4190 __builtin_unreachable();
4191 return (0);
4192 }
4193 }
4194
4195 void *container_deserialize(uint8_t typecode, const char *buf, size_t buf_len) {
4196 switch (typecode) {
4197 case BITSET_CONTAINER_TYPE_CODE:
4198 return (bitset_container_deserialize(buf, buf_len));
4199 case ARRAY_CONTAINER_TYPE_CODE:
4200 return (array_container_deserialize(buf, buf_len));
4201 case RUN_CONTAINER_TYPE_CODE:
4202 return (run_container_deserialize(buf, buf_len));
4203 case SHARED_CONTAINER_TYPE_CODE:
4204 printf("this should never happen.\n");
4205 assert(0);
4206 __builtin_unreachable();
4207 return (NULL);
4208 default:
4209 assert(0);
4210 __builtin_unreachable();
4211 return (NULL);
4212 }
4213 }
4214
4215 void *get_copy_of_container(void *container, uint8_t *typecode,
4216 bool copy_on_write) {
4217 if (copy_on_write) {
4218 shared_container_t *shared_container;
4219 if (*typecode == SHARED_CONTAINER_TYPE_CODE) {
4220 shared_container = (shared_container_t *)container;
4221 shared_container->counter += 1;
4222 return shared_container;
4223 }
4224 assert(*typecode != SHARED_CONTAINER_TYPE_CODE);
4225
4226 if ((shared_container = (shared_container_t *)malloc(
4227 sizeof(shared_container_t))) == NULL) {
4228 return NULL;
4229 }
4230
4231 shared_container->container = container;
4232 shared_container->typecode = *typecode;
4233
4234 shared_container->counter = 2;
4235 *typecode = SHARED_CONTAINER_TYPE_CODE;
4236
4237 return shared_container;
4238 } // copy_on_write
4239 // otherwise, no copy on write...
4240 const void *actualcontainer =
4241 container_unwrap_shared((const void *)container, typecode);
4242 assert(*typecode != SHARED_CONTAINER_TYPE_CODE);
4243 return container_clone(actualcontainer, *typecode);
4244 }
4245 /**
4246 * Copies a container, requires a typecode. This allocates new memory, caller
4247 * is responsible for deallocation.
4248 */
4249 void *container_clone(const void *container, uint8_t typecode) {
4250 container = container_unwrap_shared(container, &typecode);
4251 switch (typecode) {
4252 case BITSET_CONTAINER_TYPE_CODE:
4253 return bitset_container_clone((const bitset_container_t *)container);
4254 case ARRAY_CONTAINER_TYPE_CODE:
4255 return array_container_clone((const array_container_t *)container);
4256 case RUN_CONTAINER_TYPE_CODE:
4257 return run_container_clone((const run_container_t *)container);
4258 case SHARED_CONTAINER_TYPE_CODE:
4259 printf("shared containers are not clonable\n");
4260 assert(false);
4261 return NULL;
4262 default:
4263 assert(false);
4264 __builtin_unreachable();
4265 return NULL;
4266 }
4267 }
4268
4269 void *shared_container_extract_copy(shared_container_t *container,
4270 uint8_t *typecode) {
4271 assert(container->counter > 0);
4272 assert(container->typecode != SHARED_CONTAINER_TYPE_CODE);
4273 container->counter--;
4274 *typecode = container->typecode;
4275 void *answer;
4276 if (container->counter == 0) {
4277 answer = container->container;
4278 container->container = NULL; // paranoid
4279 free(container);
4280 } else {
4281 answer = container_clone(container->container, *typecode);
4282 }
4283 assert(*typecode != SHARED_CONTAINER_TYPE_CODE);
4284 return answer;
4285 }
4286
4287 void shared_container_free(shared_container_t *container) {
4288 assert(container->counter > 0);
4289 container->counter--;
4290 if (container->counter == 0) {
4291 assert(container->typecode != SHARED_CONTAINER_TYPE_CODE);
4292 container_free(container->container, container->typecode);
4293 container->container = NULL; // paranoid
4294 free(container);
4295 }
4296 }
4297
4298 /* end file src/containers/containers.c */
4299 /* begin file src/containers/convert.c */
4300 #include <stdio.h>
4301
4302
4303 // file contains grubby stuff that must know impl. details of all container
4304 // types.
4305 bitset_container_t *bitset_container_from_array(const array_container_t *a) {
4306 bitset_container_t *ans = bitset_container_create();
4307 int limit = array_container_cardinality(a);
4308 for (int i = 0; i < limit; ++i) bitset_container_set(ans, a->array[i]);
4309 return ans;
4310 }
4311
4312 bitset_container_t *bitset_container_from_run(const run_container_t *arr) {
4313 int card = run_container_cardinality(arr);
4314 bitset_container_t *answer = bitset_container_create();
4315 for (int rlepos = 0; rlepos < arr->n_runs; ++rlepos) {
4316 rle16_t vl = arr->runs[rlepos];
4317 bitset_set_lenrange(answer->array, vl.value, vl.length);
4318 }
4319 answer->cardinality = card;
4320 return answer;
4321 }
4322
4323 array_container_t *array_container_from_run(const run_container_t *arr) {
4324 array_container_t *answer =
4325 array_container_create_given_capacity(run_container_cardinality(arr));
4326 answer->cardinality = 0;
4327 for (int rlepos = 0; rlepos < arr->n_runs; ++rlepos) {
4328 int run_start = arr->runs[rlepos].value;
4329 int run_end = run_start + arr->runs[rlepos].length;
4330
4331 for (int run_value = run_start; run_value <= run_end; ++run_value) {
4332 answer->array[answer->cardinality++] = (uint16_t)run_value;
4333 }
4334 }
4335 return answer;
4336 }
4337
4338 array_container_t *array_container_from_bitset(const bitset_container_t *bits) {
4339 array_container_t *result =
4340 array_container_create_given_capacity(bits->cardinality);
4341 result->cardinality = bits->cardinality;
4342 // sse version ends up being slower here
4343 // (bitset_extract_setbits_sse_uint16)
4344 // because of the sparsity of the data
4345 bitset_extract_setbits_uint16(bits->array, BITSET_CONTAINER_SIZE_IN_WORDS,
4346 result->array, 0);
4347 return result;
4348 }
4349
4350 /* assumes that container has adequate space. Run from [s,e] (inclusive) */
4351 static void add_run(run_container_t *r, int s, int e) {
4352 r->runs[r->n_runs].value = s;
4353 r->runs[r->n_runs].length = e - s;
4354 r->n_runs++;
4355 }
4356
4357 run_container_t *run_container_from_array(const array_container_t *c) {
4358 int32_t n_runs = array_container_number_of_runs(c);
4359 run_container_t *answer = run_container_create_given_capacity(n_runs);
4360 int prev = -2;
4361 int run_start = -1;
4362 int32_t card = c->cardinality;
4363 if (card == 0) return answer;
4364 for (int i = 0; i < card; ++i) {
4365 const uint16_t cur_val = c->array[i];
4366 if (cur_val != prev + 1) {
4367 // new run starts; flush old one, if any
4368 if (run_start != -1) add_run(answer, run_start, prev);
4369 run_start = cur_val;
4370 }
4371 prev = c->array[i];
4372 }
4373 // now prev is the last seen value
4374 add_run(answer, run_start, prev);
4375 // assert(run_container_cardinality(answer) == c->cardinality);
4376 return answer;
4377 }
4378
4379 /**
4380 * Convert the runcontainer to either a Bitmap or an Array Container, depending
4381 * on the cardinality. Frees the container.
4382 * Allocates and returns new container, which caller is responsible for freeing.
4383 * It does not free the run container.
4384 */
4385
4386 void *convert_to_bitset_or_array_container(run_container_t *r, int32_t card,
4387 uint8_t *resulttype) {
4388 if (card <= DEFAULT_MAX_SIZE) {
4389 array_container_t *answer = array_container_create_given_capacity(card);
4390 answer->cardinality = 0;
4391 for (int rlepos = 0; rlepos < r->n_runs; ++rlepos) {
4392 uint16_t run_start = r->runs[rlepos].value;
4393 uint16_t run_end = run_start + r->runs[rlepos].length;
4394 for (uint16_t run_value = run_start; run_value <= run_end;
4395 ++run_value) {
4396 answer->array[answer->cardinality++] = run_value;
4397 }
4398 }
4399 assert(card == answer->cardinality);
4400 *resulttype = ARRAY_CONTAINER_TYPE_CODE;
4401 //run_container_free(r);
4402 return answer;
4403 }
4404 bitset_container_t *answer = bitset_container_create();
4405 for (int rlepos = 0; rlepos < r->n_runs; ++rlepos) {
4406 uint16_t run_start = r->runs[rlepos].value;
4407 bitset_set_lenrange(answer->array, run_start, r->runs[rlepos].length);
4408 }
4409 answer->cardinality = card;
4410 *resulttype = BITSET_CONTAINER_TYPE_CODE;
4411 //run_container_free(r);
4412 return answer;
4413 }
4414
4415 /* Converts a run container to either an array or a bitset, IF it saves space.
4416 */
4417 /* If a conversion occurs, the caller is responsible to free the original
4418 * container and
4419 * he becomes responsible to free the new one. */
4420 void *convert_run_to_efficient_container(run_container_t *c,
4421 uint8_t *typecode_after) {
4422 int32_t size_as_run_container =
4423 run_container_serialized_size_in_bytes(c->n_runs);
4424
4425 int32_t size_as_bitset_container =
4426 bitset_container_serialized_size_in_bytes();
4427 int32_t card = run_container_cardinality(c);
4428 int32_t size_as_array_container =
4429 array_container_serialized_size_in_bytes(card);
4430
4431 int32_t min_size_non_run =
4432 size_as_bitset_container < size_as_array_container
4433 ? size_as_bitset_container
4434 : size_as_array_container;
4435 if (size_as_run_container <= min_size_non_run) { // no conversion
4436 *typecode_after = RUN_CONTAINER_TYPE_CODE;
4437 return c;
4438 }
4439 if (card <= DEFAULT_MAX_SIZE) {
4440 // to array
4441 array_container_t *answer = array_container_create_given_capacity(card);
4442 answer->cardinality = 0;
4443 for (int rlepos = 0; rlepos < c->n_runs; ++rlepos) {
4444 int run_start = c->runs[rlepos].value;
4445 int run_end = run_start + c->runs[rlepos].length;
4446
4447 for (int run_value = run_start; run_value <= run_end; ++run_value) {
4448 answer->array[answer->cardinality++] = (uint16_t)run_value;
4449 }
4450 }
4451 *typecode_after = ARRAY_CONTAINER_TYPE_CODE;
4452 return answer;
4453 }
4454
4455 // else to bitset
4456 bitset_container_t *answer = bitset_container_create();
4457
4458 for (int rlepos = 0; rlepos < c->n_runs; ++rlepos) {
4459 int start = c->runs[rlepos].value;
4460 int end = start + c->runs[rlepos].length;
4461 bitset_set_range(answer->array, start, end + 1);
4462 }
4463 answer->cardinality = card;
4464 *typecode_after = BITSET_CONTAINER_TYPE_CODE;
4465 return answer;
4466 }
4467
4468 // like convert_run_to_efficient_container but frees the old result if needed
4469 void *convert_run_to_efficient_container_and_free(run_container_t *c,
4470 uint8_t *typecode_after) {
4471 void *answer = convert_run_to_efficient_container(c, typecode_after);
4472 if (answer != c) run_container_free(c);
4473 return answer;
4474 }
4475
4476 /* once converted, the original container is disposed here, rather than
4477 in roaring_array
4478 */
4479
4480 // TODO: split into run- array- and bitset- subfunctions for sanity;
4481 // a few function calls won't really matter.
4482
4483 void *convert_run_optimize(void *c, uint8_t typecode_original,
4484 uint8_t *typecode_after) {
4485 if (typecode_original == RUN_CONTAINER_TYPE_CODE) {
4486 void *newc = convert_run_to_efficient_container((run_container_t *)c,
4487 typecode_after);
4488 if (newc != c) {
4489 container_free(c, typecode_original);
4490 }
4491 return newc;
4492 } else if (typecode_original == ARRAY_CONTAINER_TYPE_CODE) {
4493 // it might need to be converted to a run container.
4494 array_container_t *c_qua_array = (array_container_t *)c;
4495 int32_t n_runs = array_container_number_of_runs(c_qua_array);
4496 int32_t size_as_run_container =
4497 run_container_serialized_size_in_bytes(n_runs);
4498 int32_t card = array_container_cardinality(c_qua_array);
4499 int32_t size_as_array_container =
4500 array_container_serialized_size_in_bytes(card);
4501
4502 if (size_as_run_container >= size_as_array_container) {
4503 *typecode_after = ARRAY_CONTAINER_TYPE_CODE;
4504 return c;
4505 }
4506 // else convert array to run container
4507 run_container_t *answer = run_container_create_given_capacity(n_runs);
4508 int prev = -2;
4509 int run_start = -1;
4510
4511 assert(card > 0);
4512 for (int i = 0; i < card; ++i) {
4513 uint16_t cur_val = c_qua_array->array[i];
4514 if (cur_val != prev + 1) {
4515 // new run starts; flush old one, if any
4516 if (run_start != -1) add_run(answer, run_start, prev);
4517 run_start = cur_val;
4518 }
4519 prev = c_qua_array->array[i];
4520 }
4521 assert(run_start >= 0);
4522 // now prev is the last seen value
4523 add_run(answer, run_start, prev);
4524 *typecode_after = RUN_CONTAINER_TYPE_CODE;
4525 array_container_free(c_qua_array);
4526 return answer;
4527 } else if (typecode_original ==
4528 BITSET_CONTAINER_TYPE_CODE) { // run conversions on bitset
4529 // does bitset need conversion to run?
4530 bitset_container_t *c_qua_bitset = (bitset_container_t *)c;
4531 int32_t n_runs = bitset_container_number_of_runs(c_qua_bitset);
4532 int32_t size_as_run_container =
4533 run_container_serialized_size_in_bytes(n_runs);
4534 int32_t size_as_bitset_container =
4535 bitset_container_serialized_size_in_bytes();
4536
4537 if (size_as_bitset_container <= size_as_run_container) {
4538 // no conversion needed.
4539 *typecode_after = BITSET_CONTAINER_TYPE_CODE;
4540 return c;
4541 }
4542 // bitset to runcontainer (ported from Java RunContainer(
4543 // BitmapContainer bc, int nbrRuns))
4544 assert(n_runs > 0); // no empty bitmaps
4545 run_container_t *answer = run_container_create_given_capacity(n_runs);
4546
4547 int long_ctr = 0;
4548 uint64_t cur_word = c_qua_bitset->array[0];
4549 int run_count = 0;
4550 while (true) {
4551 while (cur_word == UINT64_C(0) &&
4552 long_ctr < BITSET_CONTAINER_SIZE_IN_WORDS - 1)
4553 cur_word = c_qua_bitset->array[++long_ctr];
4554
4555 if (cur_word == UINT64_C(0)) {
4556 bitset_container_free(c_qua_bitset);
4557 *typecode_after = RUN_CONTAINER_TYPE_CODE;
4558 return answer;
4559 }
4560
4561 int local_run_start = __builtin_ctzll(cur_word);
4562 int run_start = local_run_start + 64 * long_ctr;
4563 uint64_t cur_word_with_1s = cur_word | (cur_word - 1);
4564
4565 int run_end = 0;
4566 while (cur_word_with_1s == UINT64_C(0xFFFFFFFFFFFFFFFF) &&
4567 long_ctr < BITSET_CONTAINER_SIZE_IN_WORDS - 1)
4568 cur_word_with_1s = c_qua_bitset->array[++long_ctr];
4569
4570 if (cur_word_with_1s == UINT64_C(0xFFFFFFFFFFFFFFFF)) {
4571 run_end = 64 + long_ctr * 64; // exclusive, I guess
4572 add_run(answer, run_start, run_end - 1);
4573 bitset_container_free(c_qua_bitset);
4574 *typecode_after = RUN_CONTAINER_TYPE_CODE;
4575 return answer;
4576 }
4577 int local_run_end = __builtin_ctzll(~cur_word_with_1s);
4578 run_end = local_run_end + long_ctr * 64;
4579 add_run(answer, run_start, run_end - 1);
4580 run_count++;
4581 cur_word = cur_word_with_1s & (cur_word_with_1s + 1);
4582 }
4583 return answer;
4584 } else {
4585 assert(false);
4586 __builtin_unreachable();
4587 return NULL;
4588 }
4589 }
4590
4591 bitset_container_t *bitset_container_from_run_range(const run_container_t *run,
4592 uint32_t min, uint32_t max) {
4593 bitset_container_t *bitset = bitset_container_create();
4594 int32_t union_cardinality = 0;
4595 for (int32_t i = 0; i < run->n_runs; ++i) {
4596 uint32_t rle_min = run->runs[i].value;
4597 uint32_t rle_max = rle_min + run->runs[i].length;
4598 bitset_set_lenrange(bitset->array, rle_min, rle_max - rle_min);
4599 union_cardinality += run->runs[i].length + 1;
4600 }
4601 union_cardinality += max - min + 1;
4602 union_cardinality -= bitset_lenrange_cardinality(bitset->array, min, max-min);
4603 bitset_set_lenrange(bitset->array, min, max - min);
4604 bitset->cardinality = union_cardinality;
4605 return bitset;
4606 }
4607 /* end file src/containers/convert.c */
4608 /* begin file src/containers/mixed_andnot.c */
4609 /*
4610 * mixed_andnot.c. More methods since operation is not symmetric,
4611 * except no "wide" andnot , so no lazy options motivated.
4612 */
4613
4614 #include <assert.h>
4615 #include <string.h>
4616
4617
4618 /* Compute the andnot of src_1 and src_2 and write the result to
4619 * dst, a valid array container that could be the same as dst.*/
4620 void array_bitset_container_andnot(const array_container_t *src_1,
4621 const bitset_container_t *src_2,
4622 array_container_t *dst) {
4623 // follows Java implementation as of June 2016
4624 if (dst->capacity < src_1->cardinality) {
4625 array_container_grow(dst, src_1->cardinality, false);
4626 }
4627 int32_t newcard = 0;
4628 const int32_t origcard = src_1->cardinality;
4629 for (int i = 0; i < origcard; ++i) {
4630 uint16_t key = src_1->array[i];
4631 dst->array[newcard] = key;
4632 newcard += 1 - bitset_container_contains(src_2, key);
4633 }
4634 dst->cardinality = newcard;
4635 }
4636
4637 /* Compute the andnot of src_1 and src_2 and write the result to
4638 * src_1 */
4639
4640 void array_bitset_container_iandnot(array_container_t *src_1,
4641 const bitset_container_t *src_2) {
4642 array_bitset_container_andnot(src_1, src_2, src_1);
4643 }
4644
4645 /* Compute the andnot of src_1 and src_2 and write the result to
4646 * dst, which does not initially have a valid container.
4647 * Return true for a bitset result; false for array
4648 */
4649
4650 bool bitset_array_container_andnot(const bitset_container_t *src_1,
4651 const array_container_t *src_2, void **dst) {
4652 // Java did this directly, but we have option of asm or avx
4653 bitset_container_t *result = bitset_container_create();
4654 bitset_container_copy(src_1, result);
4655 result->cardinality =
4656 (int32_t)bitset_clear_list(result->array, (uint64_t)result->cardinality,
4657 src_2->array, (uint64_t)src_2->cardinality);
4658
4659 // do required type conversions.
4660 if (result->cardinality <= DEFAULT_MAX_SIZE) {
4661 *dst = array_container_from_bitset(result);
4662 bitset_container_free(result);
4663 return false;
4664 }
4665 *dst = result;
4666 return true;
4667 }
4668
4669 /* Compute the andnot of src_1 and src_2 and write the result to
4670 * dst (which has no container initially). It will modify src_1
4671 * to be dst if the result is a bitset. Otherwise, it will
4672 * free src_1 and dst will be a new array container. In both
4673 * cases, the caller is responsible for deallocating dst.
4674 * Returns true iff dst is a bitset */
4675
4676 bool bitset_array_container_iandnot(bitset_container_t *src_1,
4677 const array_container_t *src_2,
4678 void **dst) {
4679 *dst = src_1;
4680 src_1->cardinality =
4681 (int32_t)bitset_clear_list(src_1->array, (uint64_t)src_1->cardinality,
4682 src_2->array, (uint64_t)src_2->cardinality);
4683
4684 if (src_1->cardinality <= DEFAULT_MAX_SIZE) {
4685 *dst = array_container_from_bitset(src_1);
4686 bitset_container_free(src_1);
4687 return false; // not bitset
4688 } else
4689 return true;
4690 }
4691
4692 /* Compute the andnot of src_1 and src_2 and write the result to
4693 * dst. Result may be either a bitset or an array container
4694 * (returns "result is bitset"). dst does not initially have
4695 * any container, but becomes either a bitset container (return
4696 * result true) or an array container.
4697 */
4698
4699 bool run_bitset_container_andnot(const run_container_t *src_1,
4700 const bitset_container_t *src_2, void **dst) {
4701 // follows the Java implementation as of June 2016
4702 int card = run_container_cardinality(src_1);
4703 if (card <= DEFAULT_MAX_SIZE) {
4704 // must be an array
4705 array_container_t *answer = array_container_create_given_capacity(card);
4706 answer->cardinality = 0;
4707 for (int32_t rlepos = 0; rlepos < src_1->n_runs; ++rlepos) {
4708 rle16_t rle = src_1->runs[rlepos];
4709 for (int run_value = rle.value; run_value <= rle.value + rle.length;
4710 ++run_value) {
4711 if (!bitset_container_get(src_2, (uint16_t)run_value)) {
4712 answer->array[answer->cardinality++] = (uint16_t)run_value;
4713 }
4714 }
4715 }
4716 *dst = answer;
4717 return false;
4718 } else { // we guess it will be a bitset, though have to check guess when
4719 // done
4720 bitset_container_t *answer = bitset_container_clone(src_2);
4721
4722 uint32_t last_pos = 0;
4723 for (int32_t rlepos = 0; rlepos < src_1->n_runs; ++rlepos) {
4724 rle16_t rle = src_1->runs[rlepos];
4725
4726 uint32_t start = rle.value;
4727 uint32_t end = start + rle.length + 1;
4728 bitset_reset_range(answer->array, last_pos, start);
4729 bitset_flip_range(answer->array, start, end);
4730 last_pos = end;
4731 }
4732 bitset_reset_range(answer->array, last_pos, (uint32_t)(1 << 16));
4733
4734 answer->cardinality = bitset_container_compute_cardinality(answer);
4735
4736 if (answer->cardinality <= DEFAULT_MAX_SIZE) {
4737 *dst = array_container_from_bitset(answer);
4738 bitset_container_free(answer);
4739 return false; // not bitset
4740 }
4741 *dst = answer;
4742 return true; // bitset
4743 }
4744 }
4745
4746 /* Compute the andnot of src_1 and src_2 and write the result to
4747 * dst. Result may be either a bitset or an array container
4748 * (returns "result is bitset"). dst does not initially have
4749 * any container, but becomes either a bitset container (return
4750 * result true) or an array container.
4751 */
4752
4753 bool run_bitset_container_iandnot(run_container_t *src_1,
4754 const bitset_container_t *src_2, void **dst) {
4755 // dummy implementation
4756 bool ans = run_bitset_container_andnot(src_1, src_2, dst);
4757 run_container_free(src_1);
4758 return ans;
4759 }
4760
4761 /* Compute the andnot of src_1 and src_2 and write the result to
4762 * dst. Result may be either a bitset or an array container
4763 * (returns "result is bitset"). dst does not initially have
4764 * any container, but becomes either a bitset container (return
4765 * result true) or an array container.
4766 */
4767
4768 bool bitset_run_container_andnot(const bitset_container_t *src_1,
4769 const run_container_t *src_2, void **dst) {
4770 // follows Java implementation
4771 bitset_container_t *result = bitset_container_create();
4772
4773 bitset_container_copy(src_1, result);
4774 for (int32_t rlepos = 0; rlepos < src_2->n_runs; ++rlepos) {
4775 rle16_t rle = src_2->runs[rlepos];
4776 bitset_reset_range(result->array, rle.value,
4777 rle.value + rle.length + UINT32_C(1));
4778 }
4779 result->cardinality = bitset_container_compute_cardinality(result);
4780
4781 if (result->cardinality <= DEFAULT_MAX_SIZE) {
4782 *dst = array_container_from_bitset(result);
4783 bitset_container_free(result);
4784 return false; // not bitset
4785 }
4786 *dst = result;
4787 return true; // bitset
4788 }
4789
4790 /* Compute the andnot of src_1 and src_2 and write the result to
4791 * dst (which has no container initially). It will modify src_1
4792 * to be dst if the result is a bitset. Otherwise, it will
4793 * free src_1 and dst will be a new array container. In both
4794 * cases, the caller is responsible for deallocating dst.
4795 * Returns true iff dst is a bitset */
4796
4797 bool bitset_run_container_iandnot(bitset_container_t *src_1,
4798 const run_container_t *src_2, void **dst) {
4799 *dst = src_1;
4800
4801 for (int32_t rlepos = 0; rlepos < src_2->n_runs; ++rlepos) {
4802 rle16_t rle = src_2->runs[rlepos];
4803 bitset_reset_range(src_1->array, rle.value,
4804 rle.value + rle.length + UINT32_C(1));
4805 }
4806 src_1->cardinality = bitset_container_compute_cardinality(src_1);
4807
4808 if (src_1->cardinality <= DEFAULT_MAX_SIZE) {
4809 *dst = array_container_from_bitset(src_1);
4810 bitset_container_free(src_1);
4811 return false; // not bitset
4812 } else
4813 return true;
4814 }
4815
4816 /* helper. a_out must be a valid array container with adequate capacity.
4817 * Returns the cardinality of the output container. Partly Based on Java
4818 * implementation Util.unsignedDifference.
4819 *
4820 * TODO: Util.unsignedDifference does not use advanceUntil. Is it cheaper
4821 * to avoid advanceUntil?
4822 */
4823
4824 static int run_array_array_subtract(const run_container_t *r,
4825 const array_container_t *a_in,
4826 array_container_t *a_out) {
4827 int out_card = 0;
4828 int32_t in_array_pos =
4829 -1; // since advanceUntil always assumes we start the search AFTER this
4830
4831 for (int rlepos = 0; rlepos < r->n_runs; rlepos++) {
4832 int32_t start = r->runs[rlepos].value;
4833 int32_t end = start + r->runs[rlepos].length + 1;
4834
4835 in_array_pos = advanceUntil(a_in->array, in_array_pos,
4836 a_in->cardinality, (uint16_t)start);
4837
4838 if (in_array_pos >= a_in->cardinality) { // run has no items subtracted
4839 for (int32_t i = start; i < end; ++i)
4840 a_out->array[out_card++] = (uint16_t)i;
4841 } else {
4842 uint16_t next_nonincluded = a_in->array[in_array_pos];
4843 if (next_nonincluded >= end) {
4844 // another case when run goes unaltered
4845 for (int32_t i = start; i < end; ++i)
4846 a_out->array[out_card++] = (uint16_t)i;
4847 in_array_pos--; // ensure we see this item again if necessary
4848 } else {
4849 for (int32_t i = start; i < end; ++i)
4850 if (i != next_nonincluded)
4851 a_out->array[out_card++] = (uint16_t)i;
4852 else // 0 should ensure we don't match
4853 next_nonincluded =
4854 (in_array_pos + 1 >= a_in->cardinality)
4855 ? 0
4856 : a_in->array[++in_array_pos];
4857 in_array_pos--; // see again
4858 }
4859 }
4860 }
4861 return out_card;
4862 }
4863
4864 /* dst does not indicate a valid container initially. Eventually it
4865 * can become any type of container.
4866 */
4867
4868 int run_array_container_andnot(const run_container_t *src_1,
4869 const array_container_t *src_2, void **dst) {
4870 // follows the Java impl as of June 2016
4871
4872 int card = run_container_cardinality(src_1);
4873 const int arbitrary_threshold = 32;
4874
4875 if (card <= arbitrary_threshold) {
4876 if (src_2->cardinality == 0) {
4877 *dst = run_container_clone(src_1);
4878 return RUN_CONTAINER_TYPE_CODE;
4879 }
4880 // Java's "lazyandNot.toEfficientContainer" thing
4881 run_container_t *answer = run_container_create_given_capacity(
4882 card + array_container_cardinality(src_2));
4883
4884 int rlepos = 0;
4885 int xrlepos = 0; // "x" is src_2
4886 rle16_t rle = src_1->runs[rlepos];
4887 int32_t start = rle.value;
4888 int32_t end = start + rle.length + 1;
4889 int32_t xstart = src_2->array[xrlepos];
4890
4891 while ((rlepos < src_1->n_runs) && (xrlepos < src_2->cardinality)) {
4892 if (end <= xstart) {
4893 // output the first run
4894 answer->runs[answer->n_runs++] =
4895 (rle16_t){.value = (uint16_t)start,
4896 .length = (uint16_t)(end - start - 1)};
4897 rlepos++;
4898 if (rlepos < src_1->n_runs) {
4899 start = src_1->runs[rlepos].value;
4900 end = start + src_1->runs[rlepos].length + 1;
4901 }
4902 } else if (xstart + 1 <= start) {
4903 // exit the second run
4904 xrlepos++;
4905 if (xrlepos < src_2->cardinality) {
4906 xstart = src_2->array[xrlepos];
4907 }
4908 } else {
4909 if (start < xstart) {
4910 answer->runs[answer->n_runs++] =
4911 (rle16_t){.value = (uint16_t)start,
4912 .length = (uint16_t)(xstart - start - 1)};
4913 }
4914 if (xstart + 1 < end) {
4915 start = xstart + 1;
4916 } else {
4917 rlepos++;
4918 if (rlepos < src_1->n_runs) {
4919 start = src_1->runs[rlepos].value;
4920 end = start + src_1->runs[rlepos].length + 1;
4921 }
4922 }
4923 }
4924 }
4925 if (rlepos < src_1->n_runs) {
4926 answer->runs[answer->n_runs++] =
4927 (rle16_t){.value = (uint16_t)start,
4928 .length = (uint16_t)(end - start - 1)};
4929 rlepos++;
4930 if (rlepos < src_1->n_runs) {
4931 memcpy(answer->runs + answer->n_runs, src_1->runs + rlepos,
4932 (src_1->n_runs - rlepos) * sizeof(rle16_t));
4933 answer->n_runs += (src_1->n_runs - rlepos);
4934 }
4935 }
4936 uint8_t return_type;
4937 *dst = convert_run_to_efficient_container(answer, &return_type);
4938 if (answer != *dst) run_container_free(answer);
4939 return return_type;
4940 }
4941 // else it's a bitmap or array
4942
4943 if (card <= DEFAULT_MAX_SIZE) {
4944 array_container_t *ac = array_container_create_given_capacity(card);
4945 // nb Java code used a generic iterator-based merge to compute
4946 // difference
4947 ac->cardinality = run_array_array_subtract(src_1, src_2, ac);
4948 *dst = ac;
4949 return ARRAY_CONTAINER_TYPE_CODE;
4950 }
4951 bitset_container_t *ans = bitset_container_from_run(src_1);
4952 bool result_is_bitset = bitset_array_container_iandnot(ans, src_2, dst);
4953 return (result_is_bitset ? BITSET_CONTAINER_TYPE_CODE
4954 : ARRAY_CONTAINER_TYPE_CODE);
4955 }
4956
4957 /* Compute the andnot of src_1 and src_2 and write the result to
4958 * dst (which has no container initially). It will modify src_1
4959 * to be dst if the result is a bitset. Otherwise, it will
4960 * free src_1 and dst will be a new array container. In both
4961 * cases, the caller is responsible for deallocating dst.
4962 * Returns true iff dst is a bitset */
4963
4964 int run_array_container_iandnot(run_container_t *src_1,
4965 const array_container_t *src_2, void **dst) {
4966 // dummy implementation same as June 2016 Java
4967 int ans = run_array_container_andnot(src_1, src_2, dst);
4968 run_container_free(src_1);
4969 return ans;
4970 }
4971
4972 /* dst must be a valid array container, allowed to be src_1 */
4973
4974 void array_run_container_andnot(const array_container_t *src_1,
4975 const run_container_t *src_2,
4976 array_container_t *dst) {
4977 // basically following Java impl as of June 2016
4978 if (src_1->cardinality > dst->capacity) {
4979 array_container_grow(dst, src_1->cardinality, false);
4980 }
4981
4982 if (src_2->n_runs == 0) {
4983 memmove(dst->array, src_1->array,
4984 sizeof(uint16_t) * src_1->cardinality);
4985 dst->cardinality = src_1->cardinality;
4986 return;
4987 }
4988 int32_t run_start = src_2->runs[0].value;
4989 int32_t run_end = run_start + src_2->runs[0].length;
4990 int which_run = 0;
4991
4992 uint16_t val = 0;
4993 int dest_card = 0;
4994 for (int i = 0; i < src_1->cardinality; ++i) {
4995 val = src_1->array[i];
4996 if (val < run_start)
4997 dst->array[dest_card++] = val;
4998 else if (val <= run_end) {
4999 ; // omitted item
5000 } else {
5001 do {
5002 if (which_run + 1 < src_2->n_runs) {
5003 ++which_run;
5004 run_start = src_2->runs[which_run].value;
5005 run_end = run_start + src_2->runs[which_run].length;
5006
5007 } else
5008 run_start = run_end = (1 << 16) + 1;
5009 } while (val > run_end);
5010 --i;
5011 }
5012 }
5013 dst->cardinality = dest_card;
5014 }
5015
5016 /* dst does not indicate a valid container initially. Eventually it
5017 * can become any kind of container.
5018 */
5019
5020 void array_run_container_iandnot(array_container_t *src_1,
5021 const run_container_t *src_2) {
5022 array_run_container_andnot(src_1, src_2, src_1);
5023 }
5024
5025 /* dst does not indicate a valid container initially. Eventually it
5026 * can become any kind of container.
5027 */
5028
5029 int run_run_container_andnot(const run_container_t *src_1,
5030 const run_container_t *src_2, void **dst) {
5031 run_container_t *ans = run_container_create();
5032 run_container_andnot(src_1, src_2, ans);
5033 uint8_t typecode_after;
5034 *dst = convert_run_to_efficient_container_and_free(ans, &typecode_after);
5035 return typecode_after;
5036 }
5037
5038 /* Compute the andnot of src_1 and src_2 and write the result to
5039 * dst (which has no container initially). It will modify src_1
5040 * to be dst if the result is a bitset. Otherwise, it will
5041 * free src_1 and dst will be a new array container. In both
5042 * cases, the caller is responsible for deallocating dst.
5043 * Returns true iff dst is a bitset */
5044
5045 int run_run_container_iandnot(run_container_t *src_1,
5046 const run_container_t *src_2, void **dst) {
5047 // following Java impl as of June 2016 (dummy)
5048 int ans = run_run_container_andnot(src_1, src_2, dst);
5049 run_container_free(src_1);
5050 return ans;
5051 }
5052
5053 /*
5054 * dst is a valid array container and may be the same as src_1
5055 */
5056
5057 void array_array_container_andnot(const array_container_t *src_1,
5058 const array_container_t *src_2,
5059 array_container_t *dst) {
5060 array_container_andnot(src_1, src_2, dst);
5061 }
5062
5063 /* inplace array-array andnot will always be able to reuse the space of
5064 * src_1 */
5065 void array_array_container_iandnot(array_container_t *src_1,
5066 const array_container_t *src_2) {
5067 array_container_andnot(src_1, src_2, src_1);
5068 }
5069
5070 /* Compute the andnot of src_1 and src_2 and write the result to
5071 * dst (which has no container initially). Return value is
5072 * "dst is a bitset"
5073 */
5074
5075 bool bitset_bitset_container_andnot(const bitset_container_t *src_1,
5076 const bitset_container_t *src_2,
5077 void **dst) {
5078 bitset_container_t *ans = bitset_container_create();
5079 int card = bitset_container_andnot(src_1, src_2, ans);
5080 if (card <= DEFAULT_MAX_SIZE) {
5081 *dst = array_container_from_bitset(ans);
5082 bitset_container_free(ans);
5083 return false; // not bitset
5084 } else {
5085 *dst = ans;
5086 return true;
5087 }
5088 }
5089
5090 /* Compute the andnot of src_1 and src_2 and write the result to
5091 * dst (which has no container initially). It will modify src_1
5092 * to be dst if the result is a bitset. Otherwise, it will
5093 * free src_1 and dst will be a new array container. In both
5094 * cases, the caller is responsible for deallocating dst.
5095 * Returns true iff dst is a bitset */
5096
5097 bool bitset_bitset_container_iandnot(bitset_container_t *src_1,
5098 const bitset_container_t *src_2,
5099 void **dst) {
5100 int card = bitset_container_andnot(src_1, src_2, src_1);
5101 if (card <= DEFAULT_MAX_SIZE) {
5102 *dst = array_container_from_bitset(src_1);
5103 bitset_container_free(src_1);
5104 return false; // not bitset
5105 } else {
5106 *dst = src_1;
5107 return true;
5108 }
5109 }
5110 /* end file src/containers/mixed_andnot.c */
5111 /* begin file src/containers/mixed_equal.c */
5112
5113 bool array_container_equal_bitset(const array_container_t* container1,
5114 const bitset_container_t* container2) {
5115 if (container2->cardinality != BITSET_UNKNOWN_CARDINALITY) {
5116 if (container2->cardinality != container1->cardinality) {
5117 return false;
5118 }
5119 }
5120 int32_t pos = 0;
5121 for (int32_t i = 0; i < BITSET_CONTAINER_SIZE_IN_WORDS; ++i) {
5122 uint64_t w = container2->array[i];
5123 while (w != 0) {
5124 uint64_t t = w & (~w + 1);
5125 uint16_t r = i * 64 + __builtin_ctzll(w);
5126 if (pos >= container1->cardinality) {
5127 return false;
5128 }
5129 if (container1->array[pos] != r) {
5130 return false;
5131 }
5132 ++pos;
5133 w ^= t;
5134 }
5135 }
5136 return (pos == container1->cardinality);
5137 }
5138
5139 bool run_container_equals_array(const run_container_t* container1,
5140 const array_container_t* container2) {
5141 if (run_container_cardinality(container1) != container2->cardinality)
5142 return false;
5143 int32_t pos = 0;
5144 for (int i = 0; i < container1->n_runs; ++i) {
5145 const uint32_t run_start = container1->runs[i].value;
5146 const uint32_t le = container1->runs[i].length;
5147
5148 if (container2->array[pos] != run_start) {
5149 return false;
5150 }
5151
5152 if (container2->array[pos + le] != run_start + le) {
5153 return false;
5154 }
5155
5156 pos += le + 1;
5157 }
5158 return true;
5159 }
5160
5161 bool run_container_equals_bitset(const run_container_t* container1,
5162 const bitset_container_t* container2) {
5163
5164 int run_card = run_container_cardinality(container1);
5165 int bitset_card = (container2->cardinality != BITSET_UNKNOWN_CARDINALITY) ?
5166 container2->cardinality :
5167 bitset_container_compute_cardinality(container2);
5168 if (bitset_card != run_card) {
5169 return false;
5170 }
5171
5172 for (int32_t i = 0; i < container1->n_runs; i++) {
5173 uint32_t begin = container1->runs[i].value;
5174 if (container1->runs[i].length) {
5175 uint32_t end = begin + container1->runs[i].length + 1;
5176 if (!bitset_container_contains_range(container2, begin, end)) {
5177 return false;
5178 }
5179 } else {
5180 if (!bitset_container_contains(container2, begin)) {
5181 return false;
5182 }
5183 }
5184 }
5185
5186 return true;
5187 }
5188 /* end file src/containers/mixed_equal.c */
5189 /* begin file src/containers/mixed_intersection.c */
5190 /*
5191 * mixed_intersection.c
5192 *
5193 */
5194
5195
5196 /* Compute the intersection of src_1 and src_2 and write the result to
5197 * dst. */
5198 void array_bitset_container_intersection(const array_container_t *src_1,
5199 const bitset_container_t *src_2,
5200 array_container_t *dst) {
5201 if (dst->capacity < src_1->cardinality) {
5202 array_container_grow(dst, src_1->cardinality, false);
5203 }
5204 int32_t newcard = 0; // dst could be src_1
5205 const int32_t origcard = src_1->cardinality;
5206 for (int i = 0; i < origcard; ++i) {
5207 uint16_t key = src_1->array[i];
5208 // this branchless approach is much faster...
5209 dst->array[newcard] = key;
5210 newcard += bitset_container_contains(src_2, key);
5211 /**
5212 * we could do it this way instead...
5213 * if (bitset_container_contains(src_2, key)) {
5214 * dst->array[newcard++] = key;
5215 * }
5216 * but if the result is unpredictable, the processor generates
5217 * many mispredicted branches.
5218 * Difference can be huge (from 3 cycles when predictable all the way
5219 * to 16 cycles when unpredictable.
5220 * See
5221 * https://github.com/lemire/Code-used-on-Daniel-Lemire-s-blog/blob/master/extra/bitset/c/arraybitsetintersection.c
5222 */
5223 }
5224 dst->cardinality = newcard;
5225 }
5226
5227 /* Compute the size of the intersection of src_1 and src_2. */
5228 int array_bitset_container_intersection_cardinality(
5229 const array_container_t *src_1, const bitset_container_t *src_2) {
5230 int32_t newcard = 0;
5231 const int32_t origcard = src_1->cardinality;
5232 for (int i = 0; i < origcard; ++i) {
5233 uint16_t key = src_1->array[i];
5234 newcard += bitset_container_contains(src_2, key);
5235 }
5236 return newcard;
5237 }
5238
5239
5240 bool array_bitset_container_intersect(const array_container_t *src_1,
5241 const bitset_container_t *src_2) {
5242 const int32_t origcard = src_1->cardinality;
5243 for (int i = 0; i < origcard; ++i) {
5244 uint16_t key = src_1->array[i];
5245 if(bitset_container_contains(src_2, key)) return true;
5246 }
5247 return false;
5248 }
5249
5250 /* Compute the intersection of src_1 and src_2 and write the result to
5251 * dst. It is allowed for dst to be equal to src_1. We assume that dst is a
5252 * valid container. */
5253 void array_run_container_intersection(const array_container_t *src_1,
5254 const run_container_t *src_2,
5255 array_container_t *dst) {
5256 if (run_container_is_full(src_2)) {
5257 if (dst != src_1) array_container_copy(src_1, dst);
5258 return;
5259 }
5260 if (dst->capacity < src_1->cardinality) {
5261 array_container_grow(dst, src_1->cardinality, false);
5262 }
5263 if (src_2->n_runs == 0) {
5264 return;
5265 }
5266 int32_t rlepos = 0;
5267 int32_t arraypos = 0;
5268 rle16_t rle = src_2->runs[rlepos];
5269 int32_t newcard = 0;
5270 while (arraypos < src_1->cardinality) {
5271 const uint16_t arrayval = src_1->array[arraypos];
5272 while (rle.value + rle.length <
5273 arrayval) { // this will frequently be false
5274 ++rlepos;
5275 if (rlepos == src_2->n_runs) {
5276 dst->cardinality = newcard;
5277 return; // we are done
5278 }
5279 rle = src_2->runs[rlepos];
5280 }
5281 if (rle.value > arrayval) {
5282 arraypos = advanceUntil(src_1->array, arraypos, src_1->cardinality,
5283 rle.value);
5284 } else {
5285 dst->array[newcard] = arrayval;
5286 newcard++;
5287 arraypos++;
5288 }
5289 }
5290 dst->cardinality = newcard;
5291 }
5292
5293 /* Compute the intersection of src_1 and src_2 and write the result to
5294 * *dst. If the result is true then the result is a bitset_container_t
5295 * otherwise is a array_container_t. If *dst == src_2, an in-place processing
5296 * is attempted.*/
5297 bool run_bitset_container_intersection(const run_container_t *src_1,
5298 const bitset_container_t *src_2,
5299 void **dst) {
5300 if (run_container_is_full(src_1)) {
5301 if (*dst != src_2) *dst = bitset_container_clone(src_2);
5302 return true;
5303 }
5304 int32_t card = run_container_cardinality(src_1);
5305 if (card <= DEFAULT_MAX_SIZE) {
5306 // result can only be an array (assuming that we never make a
5307 // RunContainer)
5308 if (card > src_2->cardinality) {
5309 card = src_2->cardinality;
5310 }
5311 array_container_t *answer = array_container_create_given_capacity(card);
5312 *dst = answer;
5313 if (*dst == NULL) {
5314 return false;
5315 }
5316 for (int32_t rlepos = 0; rlepos < src_1->n_runs; ++rlepos) {
5317 rle16_t rle = src_1->runs[rlepos];
5318 uint32_t endofrun = (uint32_t)rle.value + rle.length;
5319 for (uint32_t runValue = rle.value; runValue <= endofrun;
5320 ++runValue) {
5321 answer->array[answer->cardinality] = (uint16_t)runValue;
5322 answer->cardinality +=
5323 bitset_container_contains(src_2, runValue);
5324 }
5325 }
5326 return false;
5327 }
5328 if (*dst == src_2) { // we attempt in-place
5329 bitset_container_t *answer = (bitset_container_t *)*dst;
5330 uint32_t start = 0;
5331 for (int32_t rlepos = 0; rlepos < src_1->n_runs; ++rlepos) {
5332 const rle16_t rle = src_1->runs[rlepos];
5333 uint32_t end = rle.value;
5334 bitset_reset_range(src_2->array, start, end);
5335
5336 start = end + rle.length + 1;
5337 }
5338 bitset_reset_range(src_2->array, start, UINT32_C(1) << 16);
5339 answer->cardinality = bitset_container_compute_cardinality(answer);
5340 if (src_2->cardinality > DEFAULT_MAX_SIZE) {
5341 return true;
5342 } else {
5343 array_container_t *newanswer = array_container_from_bitset(src_2);
5344 if (newanswer == NULL) {
5345 *dst = NULL;
5346 return false;
5347 }
5348 *dst = newanswer;
5349 return false;
5350 }
5351 } else { // no inplace
5352 // we expect the answer to be a bitmap (if we are lucky)
5353 bitset_container_t *answer = bitset_container_clone(src_2);
5354
5355 *dst = answer;
5356 if (answer == NULL) {
5357 return true;
5358 }
5359 uint32_t start = 0;
5360 for (int32_t rlepos = 0; rlepos < src_1->n_runs; ++rlepos) {
5361 const rle16_t rle = src_1->runs[rlepos];
5362 uint32_t end = rle.value;
5363 bitset_reset_range(answer->array, start, end);
5364 start = end + rle.length + 1;
5365 }
5366 bitset_reset_range(answer->array, start, UINT32_C(1) << 16);
5367 answer->cardinality = bitset_container_compute_cardinality(answer);
5368
5369 if (answer->cardinality > DEFAULT_MAX_SIZE) {
5370 return true;
5371 } else {
5372 array_container_t *newanswer = array_container_from_bitset(answer);
5373 bitset_container_free((bitset_container_t *)*dst);
5374 if (newanswer == NULL) {
5375 *dst = NULL;
5376 return false;
5377 }
5378 *dst = newanswer;
5379 return false;
5380 }
5381 }
5382 }
5383
5384 /* Compute the size of the intersection between src_1 and src_2 . */
5385 int array_run_container_intersection_cardinality(const array_container_t *src_1,
5386 const run_container_t *src_2) {
5387 if (run_container_is_full(src_2)) {
5388 return src_1->cardinality;
5389 }
5390 if (src_2->n_runs == 0) {
5391 return 0;
5392 }
5393 int32_t rlepos = 0;
5394 int32_t arraypos = 0;
5395 rle16_t rle = src_2->runs[rlepos];
5396 int32_t newcard = 0;
5397 while (arraypos < src_1->cardinality) {
5398 const uint16_t arrayval = src_1->array[arraypos];
5399 while (rle.value + rle.length <
5400 arrayval) { // this will frequently be false
5401 ++rlepos;
5402 if (rlepos == src_2->n_runs) {
5403 return newcard; // we are done
5404 }
5405 rle = src_2->runs[rlepos];
5406 }
5407 if (rle.value > arrayval) {
5408 arraypos = advanceUntil(src_1->array, arraypos, src_1->cardinality,
5409 rle.value);
5410 } else {
5411 newcard++;
5412 arraypos++;
5413 }
5414 }
5415 return newcard;
5416 }
5417
5418 /* Compute the intersection between src_1 and src_2
5419 **/
5420 int run_bitset_container_intersection_cardinality(
5421 const run_container_t *src_1, const bitset_container_t *src_2) {
5422 if (run_container_is_full(src_1)) {
5423 return bitset_container_cardinality(src_2);
5424 }
5425 int answer = 0;
5426 for (int32_t rlepos = 0; rlepos < src_1->n_runs; ++rlepos) {
5427 rle16_t rle = src_1->runs[rlepos];
5428 answer +=
5429 bitset_lenrange_cardinality(src_2->array, rle.value, rle.length);
5430 }
5431 return answer;
5432 }
5433
5434
5435 bool array_run_container_intersect(const array_container_t *src_1,
5436 const run_container_t *src_2) {
5437 if( run_container_is_full(src_2) ) {
5438 return !array_container_empty(src_1);
5439 }
5440 if (src_2->n_runs == 0) {
5441 return false;
5442 }
5443 int32_t rlepos = 0;
5444 int32_t arraypos = 0;
5445 rle16_t rle = src_2->runs[rlepos];
5446 while (arraypos < src_1->cardinality) {
5447 const uint16_t arrayval = src_1->array[arraypos];
5448 while (rle.value + rle.length <
5449 arrayval) { // this will frequently be false
5450 ++rlepos;
5451 if (rlepos == src_2->n_runs) {
5452 return false; // we are done
5453 }
5454 rle = src_2->runs[rlepos];
5455 }
5456 if (rle.value > arrayval) {
5457 arraypos = advanceUntil(src_1->array, arraypos, src_1->cardinality,
5458 rle.value);
5459 } else {
5460 return true;
5461 }
5462 }
5463 return false;
5464 }
5465
5466 /* Compute the intersection between src_1 and src_2
5467 **/
5468 bool run_bitset_container_intersect(const run_container_t *src_1,
5469 const bitset_container_t *src_2) {
5470 if( run_container_is_full(src_1) ) {
5471 return !bitset_container_empty(src_2);
5472 }
5473 for (int32_t rlepos = 0; rlepos < src_1->n_runs; ++rlepos) {
5474 rle16_t rle = src_1->runs[rlepos];
5475 if(!bitset_lenrange_empty(src_2->array, rle.value,rle.length)) return true;
5476 }
5477 return false;
5478 }
5479
5480 /*
5481 * Compute the intersection between src_1 and src_2 and write the result
5482 * to *dst. If the return function is true, the result is a bitset_container_t
5483 * otherwise is a array_container_t.
5484 */
5485 bool bitset_bitset_container_intersection(const bitset_container_t *src_1,
5486 const bitset_container_t *src_2,
5487 void **dst) {
5488 const int newCardinality = bitset_container_and_justcard(src_1, src_2);
5489 if (newCardinality > DEFAULT_MAX_SIZE) {
5490 *dst = bitset_container_create();
5491 if (*dst != NULL) {
5492 bitset_container_and_nocard(src_1, src_2,
5493 (bitset_container_t *)*dst);
5494 ((bitset_container_t *)*dst)->cardinality = newCardinality;
5495 }
5496 return true; // it is a bitset
5497 }
5498 *dst = array_container_create_given_capacity(newCardinality);
5499 if (*dst != NULL) {
5500 ((array_container_t *)*dst)->cardinality = newCardinality;
5501 bitset_extract_intersection_setbits_uint16(
5502 ((const bitset_container_t *)src_1)->array,
5503 ((const bitset_container_t *)src_2)->array,
5504 BITSET_CONTAINER_SIZE_IN_WORDS, ((array_container_t *)*dst)->array,
5505 0);
5506 }
5507 return false; // not a bitset
5508 }
5509
5510 bool bitset_bitset_container_intersection_inplace(
5511 bitset_container_t *src_1, const bitset_container_t *src_2, void **dst) {
5512 const int newCardinality = bitset_container_and_justcard(src_1, src_2);
5513 if (newCardinality > DEFAULT_MAX_SIZE) {
5514 *dst = src_1;
5515 bitset_container_and_nocard(src_1, src_2, src_1);
5516 ((bitset_container_t *)*dst)->cardinality = newCardinality;
5517 return true; // it is a bitset
5518 }
5519 *dst = array_container_create_given_capacity(newCardinality);
5520 if (*dst != NULL) {
5521 ((array_container_t *)*dst)->cardinality = newCardinality;
5522 bitset_extract_intersection_setbits_uint16(
5523 ((const bitset_container_t *)src_1)->array,
5524 ((const bitset_container_t *)src_2)->array,
5525 BITSET_CONTAINER_SIZE_IN_WORDS, ((array_container_t *)*dst)->array,
5526 0);
5527 }
5528 return false; // not a bitset
5529 }
5530 /* end file src/containers/mixed_intersection.c */
5531 /* begin file src/containers/mixed_negation.c */
5532 /*
5533 * mixed_negation.c
5534 *
5535 */
5536
5537 #include <assert.h>
5538 #include <string.h>
5539
5540
5541 // TODO: make simplified and optimized negation code across
5542 // the full range.
5543
5544 /* Negation across the entire range of the container.
5545 * Compute the negation of src and write the result
5546 * to *dst. The complement of a
5547 * sufficiently sparse set will always be dense and a hence a bitmap
5548 ' * We assume that dst is pre-allocated and a valid bitset container
5549 * There can be no in-place version.
5550 */
5551 void array_container_negation(const array_container_t *src,
5552 bitset_container_t *dst) {
5553 uint64_t card = UINT64_C(1 << 16);
5554 bitset_container_set_all(dst);
5555
5556 dst->cardinality = (int32_t)bitset_clear_list(dst->array, card, src->array,
5557 (uint64_t)src->cardinality);
5558 }
5559
5560 /* Negation across the entire range of the container
5561 * Compute the negation of src and write the result
5562 * to *dst. A true return value indicates a bitset result,
5563 * otherwise the result is an array container.
5564 * We assume that dst is not pre-allocated. In
5565 * case of failure, *dst will be NULL.
5566 */
5567 bool bitset_container_negation(const bitset_container_t *src, void **dst) {
5568 return bitset_container_negation_range(src, 0, (1 << 16), dst);
5569 }
5570
5571 /* inplace version */
5572 /*
5573 * Same as bitset_container_negation except that if the output is to
5574 * be a
5575 * bitset_container_t, then src is modified and no allocation is made.
5576 * If the output is to be an array_container_t, then caller is responsible
5577 * to free the container.
5578 * In all cases, the result is in *dst.
5579 */
5580 bool bitset_container_negation_inplace(bitset_container_t *src, void **dst) {
5581 return bitset_container_negation_range_inplace(src, 0, (1 << 16), dst);
5582 }
5583
5584 /* Negation across the entire range of container
5585 * Compute the negation of src and write the result
5586 * to *dst. Return values are the *_TYPECODES as defined * in containers.h
5587 * We assume that dst is not pre-allocated. In
5588 * case of failure, *dst will be NULL.
5589 */
5590 int run_container_negation(const run_container_t *src, void **dst) {
5591 return run_container_negation_range(src, 0, (1 << 16), dst);
5592 }
5593
5594 /*
5595 * Same as run_container_negation except that if the output is to
5596 * be a
5597 * run_container_t, and has the capacity to hold the result,
5598 * then src is modified and no allocation is made.
5599 * In all cases, the result is in *dst.
5600 */
5601 int run_container_negation_inplace(run_container_t *src, void **dst) {
5602 return run_container_negation_range_inplace(src, 0, (1 << 16), dst);
5603 }
5604
5605 /* Negation across a range of the container.
5606 * Compute the negation of src and write the result
5607 * to *dst. Returns true if the result is a bitset container
5608 * and false for an array container. *dst is not preallocated.
5609 */
5610 bool array_container_negation_range(const array_container_t *src,
5611 const int range_start, const int range_end,
5612 void **dst) {
5613 /* close port of the Java implementation */
5614 if (range_start >= range_end) {
5615 *dst = array_container_clone(src);
5616 return false;
5617 }
5618
5619 int32_t start_index =
5620 binarySearch(src->array, src->cardinality, (uint16_t)range_start);
5621 if (start_index < 0) start_index = -start_index - 1;
5622
5623 int32_t last_index =
5624 binarySearch(src->array, src->cardinality, (uint16_t)(range_end - 1));
5625 if (last_index < 0) last_index = -last_index - 2;
5626
5627 const int32_t current_values_in_range = last_index - start_index + 1;
5628 const int32_t span_to_be_flipped = range_end - range_start;
5629 const int32_t new_values_in_range =
5630 span_to_be_flipped - current_values_in_range;
5631 const int32_t cardinality_change =
5632 new_values_in_range - current_values_in_range;
5633 const int32_t new_cardinality = src->cardinality + cardinality_change;
5634
5635 if (new_cardinality > DEFAULT_MAX_SIZE) {
5636 bitset_container_t *temp = bitset_container_from_array(src);
5637 bitset_flip_range(temp->array, (uint32_t)range_start,
5638 (uint32_t)range_end);
5639 temp->cardinality = new_cardinality;
5640 *dst = temp;
5641 return true;
5642 }
5643
5644 array_container_t *arr =
5645 array_container_create_given_capacity(new_cardinality);
5646 *dst = (void *)arr;
5647 if(new_cardinality == 0) {
5648 arr->cardinality = new_cardinality;
5649 return false; // we are done.
5650 }
5651 // copy stuff before the active area
5652 memcpy(arr->array, src->array, start_index * sizeof(uint16_t));
5653
5654 // work on the range
5655 int32_t out_pos = start_index, in_pos = start_index;
5656 int32_t val_in_range = range_start;
5657 for (; val_in_range < range_end && in_pos <= last_index; ++val_in_range) {
5658 if ((uint16_t)val_in_range != src->array[in_pos]) {
5659 arr->array[out_pos++] = (uint16_t)val_in_range;
5660 } else {
5661 ++in_pos;
5662 }
5663 }
5664 for (; val_in_range < range_end; ++val_in_range)
5665 arr->array[out_pos++] = (uint16_t)val_in_range;
5666
5667 // content after the active range
5668 memcpy(arr->array + out_pos, src->array + (last_index + 1),
5669 (src->cardinality - (last_index + 1)) * sizeof(uint16_t));
5670 arr->cardinality = new_cardinality;
5671 return false;
5672 }
5673
5674 /* Even when the result would fit, it is unclear how to make an
5675 * inplace version without inefficient copying.
5676 */
5677
5678 bool array_container_negation_range_inplace(array_container_t *src,
5679 const int range_start,
5680 const int range_end, void **dst) {
5681 bool ans = array_container_negation_range(src, range_start, range_end, dst);
5682 // TODO : try a real inplace version
5683 array_container_free(src);
5684 return ans;
5685 }
5686
5687 /* Negation across a range of the container
5688 * Compute the negation of src and write the result
5689 * to *dst. A true return value indicates a bitset result,
5690 * otherwise the result is an array container.
5691 * We assume that dst is not pre-allocated. In
5692 * case of failure, *dst will be NULL.
5693 */
5694 bool bitset_container_negation_range(const bitset_container_t *src,
5695 const int range_start, const int range_end,
5696 void **dst) {
5697 // TODO maybe consider density-based estimate
5698 // and sometimes build result directly as array, with
5699 // conversion back to bitset if wrong. Or determine
5700 // actual result cardinality, then go directly for the known final cont.
5701
5702 // keep computation using bitsets as long as possible.
5703 bitset_container_t *t = bitset_container_clone(src);
5704 bitset_flip_range(t->array, (uint32_t)range_start, (uint32_t)range_end);
5705 t->cardinality = bitset_container_compute_cardinality(t);
5706
5707 if (t->cardinality > DEFAULT_MAX_SIZE) {
5708 *dst = t;
5709 return true;
5710 } else {
5711 *dst = array_container_from_bitset(t);
5712 bitset_container_free(t);
5713 return false;
5714 }
5715 }
5716
5717 /* inplace version */
5718 /*
5719 * Same as bitset_container_negation except that if the output is to
5720 * be a
5721 * bitset_container_t, then src is modified and no allocation is made.
5722 * If the output is to be an array_container_t, then caller is responsible
5723 * to free the container.
5724 * In all cases, the result is in *dst.
5725 */
5726 bool bitset_container_negation_range_inplace(bitset_container_t *src,
5727 const int range_start,
5728 const int range_end, void **dst) {
5729 bitset_flip_range(src->array, (uint32_t)range_start, (uint32_t)range_end);
5730 src->cardinality = bitset_container_compute_cardinality(src);
5731 if (src->cardinality > DEFAULT_MAX_SIZE) {
5732 *dst = src;
5733 return true;
5734 }
5735 *dst = array_container_from_bitset(src);
5736 bitset_container_free(src);
5737 return false;
5738 }
5739
5740 /* Negation across a range of container
5741 * Compute the negation of src and write the result
5742 * to *dst. Return values are the *_TYPECODES as defined * in containers.h
5743 * We assume that dst is not pre-allocated. In
5744 * case of failure, *dst will be NULL.
5745 */
5746 int run_container_negation_range(const run_container_t *src,
5747 const int range_start, const int range_end,
5748 void **dst) {
5749 uint8_t return_typecode;
5750
5751 // follows the Java implementation
5752 if (range_end <= range_start) {
5753 *dst = run_container_clone(src);
5754 return RUN_CONTAINER_TYPE_CODE;
5755 }
5756
5757 run_container_t *ans = run_container_create_given_capacity(
5758 src->n_runs + 1); // src->n_runs + 1);
5759 int k = 0;
5760 for (; k < src->n_runs && src->runs[k].value < range_start; ++k) {
5761 ans->runs[k] = src->runs[k];
5762 ans->n_runs++;
5763 }
5764
5765 run_container_smart_append_exclusive(
5766 ans, (uint16_t)range_start, (uint16_t)(range_end - range_start - 1));
5767
5768 for (; k < src->n_runs; ++k) {
5769 run_container_smart_append_exclusive(ans, src->runs[k].value,
5770 src->runs[k].length);
5771 }
5772
5773 *dst = convert_run_to_efficient_container(ans, &return_typecode);
5774 if (return_typecode != RUN_CONTAINER_TYPE_CODE) run_container_free(ans);
5775
5776 return return_typecode;
5777 }
5778
5779 /*
5780 * Same as run_container_negation except that if the output is to
5781 * be a
5782 * run_container_t, and has the capacity to hold the result,
5783 * then src is modified and no allocation is made.
5784 * In all cases, the result is in *dst.
5785 */
5786 int run_container_negation_range_inplace(run_container_t *src,
5787 const int range_start,
5788 const int range_end, void **dst) {
5789 uint8_t return_typecode;
5790
5791 if (range_end <= range_start) {
5792 *dst = src;
5793 return RUN_CONTAINER_TYPE_CODE;
5794 }
5795
5796 // TODO: efficient special case when range is 0 to 65535 inclusive
5797
5798 if (src->capacity == src->n_runs) {
5799 // no excess room. More checking to see if result can fit
5800 bool last_val_before_range = false;
5801 bool first_val_in_range = false;
5802 bool last_val_in_range = false;
5803 bool first_val_past_range = false;
5804
5805 if (range_start > 0)
5806 last_val_before_range =
5807 run_container_contains(src, (uint16_t)(range_start - 1));
5808 first_val_in_range = run_container_contains(src, (uint16_t)range_start);
5809
5810 if (last_val_before_range == first_val_in_range) {
5811 last_val_in_range =
5812 run_container_contains(src, (uint16_t)(range_end - 1));
5813 if (range_end != 0x10000)
5814 first_val_past_range =
5815 run_container_contains(src, (uint16_t)range_end);
5816
5817 if (last_val_in_range ==
5818 first_val_past_range) { // no space for inplace
5819 int ans = run_container_negation_range(src, range_start,
5820 range_end, dst);
5821 run_container_free(src);
5822 return ans;
5823 }
5824 }
5825 }
5826 // all other cases: result will fit
5827
5828 run_container_t *ans = src;
5829 int my_nbr_runs = src->n_runs;
5830
5831 ans->n_runs = 0;
5832 int k = 0;
5833 for (; (k < my_nbr_runs) && (src->runs[k].value < range_start); ++k) {
5834 // ans->runs[k] = src->runs[k]; (would be self-copy)
5835 ans->n_runs++;
5836 }
5837
5838 // as with Java implementation, use locals to give self a buffer of depth 1
5839 rle16_t buffered = (rle16_t){.value = (uint16_t)0, .length = (uint16_t)0};
5840 rle16_t next = buffered;
5841 if (k < my_nbr_runs) buffered = src->runs[k];
5842
5843 run_container_smart_append_exclusive(
5844 ans, (uint16_t)range_start, (uint16_t)(range_end - range_start - 1));
5845
5846 for (; k < my_nbr_runs; ++k) {
5847 if (k + 1 < my_nbr_runs) next = src->runs[k + 1];
5848
5849 run_container_smart_append_exclusive(ans, buffered.value,
5850 buffered.length);
5851 buffered = next;
5852 }
5853
5854 *dst = convert_run_to_efficient_container(ans, &return_typecode);
5855 if (return_typecode != RUN_CONTAINER_TYPE_CODE) run_container_free(ans);
5856
5857 return return_typecode;
5858 }
5859 /* end file src/containers/mixed_negation.c */
5860 /* begin file src/containers/mixed_subset.c */
5861
5862 bool array_container_is_subset_bitset(const array_container_t* container1,
5863 const bitset_container_t* container2) {
5864 if (container2->cardinality != BITSET_UNKNOWN_CARDINALITY) {
5865 if (container2->cardinality < container1->cardinality) {
5866 return false;
5867 }
5868 }
5869 for (int i = 0; i < container1->cardinality; ++i) {
5870 if (!bitset_container_contains(container2, container1->array[i])) {
5871 return false;
5872 }
5873 }
5874 return true;
5875 }
5876
5877 bool run_container_is_subset_array(const run_container_t* container1,
5878 const array_container_t* container2) {
5879 if (run_container_cardinality(container1) > container2->cardinality)
5880 return false;
5881 int32_t start_pos = -1, stop_pos = -1;
5882 for (int i = 0; i < container1->n_runs; ++i) {
5883 int32_t start = container1->runs[i].value;
5884 int32_t stop = start + container1->runs[i].length;
5885 start_pos = advanceUntil(container2->array, stop_pos,
5886 container2->cardinality, start);
5887 stop_pos = advanceUntil(container2->array, stop_pos,
5888 container2->cardinality, stop);
5889 if (start_pos == container2->cardinality) {
5890 return false;
5891 } else if (stop_pos - start_pos != stop - start ||
5892 container2->array[start_pos] != start ||
5893 container2->array[stop_pos] != stop) {
5894 return false;
5895 }
5896 }
5897 return true;
5898 }
5899
5900 bool array_container_is_subset_run(const array_container_t* container1,
5901 const run_container_t* container2) {
5902 if (container1->cardinality > run_container_cardinality(container2))
5903 return false;
5904 int i_array = 0, i_run = 0;
5905 while (i_array < container1->cardinality && i_run < container2->n_runs) {
5906 uint32_t start = container2->runs[i_run].value;
5907 uint32_t stop = start + container2->runs[i_run].length;
5908 if (container1->array[i_array] < start) {
5909 return false;
5910 } else if (container1->array[i_array] > stop) {
5911 i_run++;
5912 } else { // the value of the array is in the run
5913 i_array++;
5914 }
5915 }
5916 if (i_array == container1->cardinality) {
5917 return true;
5918 } else {
5919 return false;
5920 }
5921 }
5922
5923 bool run_container_is_subset_bitset(const run_container_t* container1,
5924 const bitset_container_t* container2) {
5925 // todo: this code could be much faster
5926 if (container2->cardinality != BITSET_UNKNOWN_CARDINALITY) {
5927 if (container2->cardinality < run_container_cardinality(container1)) {
5928 return false;
5929 }
5930 } else {
5931 int32_t card = bitset_container_compute_cardinality(
5932 container2); // modify container2?
5933 if (card < run_container_cardinality(container1)) {
5934 return false;
5935 }
5936 }
5937 for (int i = 0; i < container1->n_runs; ++i) {
5938 uint32_t run_start = container1->runs[i].value;
5939 uint32_t le = container1->runs[i].length;
5940 for (uint32_t j = run_start; j <= run_start + le; ++j) {
5941 if (!bitset_container_contains(container2, j)) {
5942 return false;
5943 }
5944 }
5945 }
5946 return true;
5947 }
5948
5949 bool bitset_container_is_subset_run(const bitset_container_t* container1,
5950 const run_container_t* container2) {
5951 // todo: this code could be much faster
5952 if (container1->cardinality != BITSET_UNKNOWN_CARDINALITY) {
5953 if (container1->cardinality > run_container_cardinality(container2)) {
5954 return false;
5955 }
5956 }
5957 int32_t i_bitset = 0, i_run = 0;
5958 while (i_bitset < BITSET_CONTAINER_SIZE_IN_WORDS &&
5959 i_run < container2->n_runs) {
5960 uint64_t w = container1->array[i_bitset];
5961 while (w != 0 && i_run < container2->n_runs) {
5962 uint32_t start = container2->runs[i_run].value;
5963 uint32_t stop = start + container2->runs[i_run].length;
5964 uint64_t t = w & (~w + 1);
5965 uint16_t r = i_bitset * 64 + __builtin_ctzll(w);
5966 if (r < start) {
5967 return false;
5968 } else if (r > stop) {
5969 i_run++;
5970 continue;
5971 } else {
5972 w ^= t;
5973 }
5974 }
5975 if (w == 0) {
5976 i_bitset++;
5977 } else {
5978 return false;
5979 }
5980 }
5981 if (i_bitset < BITSET_CONTAINER_SIZE_IN_WORDS) {
5982 // terminated iterating on the run containers, check that rest of bitset
5983 // is empty
5984 for (; i_bitset < BITSET_CONTAINER_SIZE_IN_WORDS; i_bitset++) {
5985 if (container1->array[i_bitset] != 0) {
5986 return false;
5987 }
5988 }
5989 }
5990 return true;
5991 }
5992 /* end file src/containers/mixed_subset.c */
5993 /* begin file src/containers/mixed_union.c */
5994 /*
5995 * mixed_union.c
5996 *
5997 */
5998
5999 #include <assert.h>
6000 #include <string.h>
6001
6002
6003 /* Compute the union of src_1 and src_2 and write the result to
6004 * dst. */
6005 void array_bitset_container_union(const array_container_t *src_1,
6006 const bitset_container_t *src_2,
6007 bitset_container_t *dst) {
6008 if (src_2 != dst) bitset_container_copy(src_2, dst);
6009 dst->cardinality = (int32_t)bitset_set_list_withcard(
6010 dst->array, dst->cardinality, src_1->array, src_1->cardinality);
6011 }
6012
6013 /* Compute the union of src_1 and src_2 and write the result to
6014 * dst. It is allowed for src_2 to be dst. This version does not
6015 * update the cardinality of dst (it is set to BITSET_UNKNOWN_CARDINALITY). */
6016 void array_bitset_container_lazy_union(const array_container_t *src_1,
6017 const bitset_container_t *src_2,
6018 bitset_container_t *dst) {
6019 if (src_2 != dst) bitset_container_copy(src_2, dst);
6020 bitset_set_list(dst->array, src_1->array, src_1->cardinality);
6021 dst->cardinality = BITSET_UNKNOWN_CARDINALITY;
6022 }
6023
6024 void run_bitset_container_union(const run_container_t *src_1,
6025 const bitset_container_t *src_2,
6026 bitset_container_t *dst) {
6027 assert(!run_container_is_full(src_1)); // catch this case upstream
6028 if (src_2 != dst) bitset_container_copy(src_2, dst);
6029 for (int32_t rlepos = 0; rlepos < src_1->n_runs; ++rlepos) {
6030 rle16_t rle = src_1->runs[rlepos];
6031 bitset_set_lenrange(dst->array, rle.value, rle.length);
6032 }
6033 dst->cardinality = bitset_container_compute_cardinality(dst);
6034 }
6035
6036 void run_bitset_container_lazy_union(const run_container_t *src_1,
6037 const bitset_container_t *src_2,
6038 bitset_container_t *dst) {
6039 assert(!run_container_is_full(src_1)); // catch this case upstream
6040 if (src_2 != dst) bitset_container_copy(src_2, dst);
6041 for (int32_t rlepos = 0; rlepos < src_1->n_runs; ++rlepos) {
6042 rle16_t rle = src_1->runs[rlepos];
6043 bitset_set_lenrange(dst->array, rle.value, rle.length);
6044 }
6045 dst->cardinality = BITSET_UNKNOWN_CARDINALITY;
6046 }
6047
6048 // why do we leave the result as a run container??
6049 void array_run_container_union(const array_container_t *src_1,
6050 const run_container_t *src_2,
6051 run_container_t *dst) {
6052 if (run_container_is_full(src_2)) {
6053 run_container_copy(src_2, dst);
6054 return;
6055 }
6056 // TODO: see whether the "2*" is spurious
6057 run_container_grow(dst, 2 * (src_1->cardinality + src_2->n_runs), false);
6058 int32_t rlepos = 0;
6059 int32_t arraypos = 0;
6060 rle16_t previousrle;
6061 if (src_2->runs[rlepos].value <= src_1->array[arraypos]) {
6062 previousrle = run_container_append_first(dst, src_2->runs[rlepos]);
6063 rlepos++;
6064 } else {
6065 previousrle =
6066 run_container_append_value_first(dst, src_1->array[arraypos]);
6067 arraypos++;
6068 }
6069 while ((rlepos < src_2->n_runs) && (arraypos < src_1->cardinality)) {
6070 if (src_2->runs[rlepos].value <= src_1->array[arraypos]) {
6071 run_container_append(dst, src_2->runs[rlepos], &previousrle);
6072 rlepos++;
6073 } else {
6074 run_container_append_value(dst, src_1->array[arraypos],
6075 &previousrle);
6076 arraypos++;
6077 }
6078 }
6079 if (arraypos < src_1->cardinality) {
6080 while (arraypos < src_1->cardinality) {
6081 run_container_append_value(dst, src_1->array[arraypos],
6082 &previousrle);
6083 arraypos++;
6084 }
6085 } else {
6086 while (rlepos < src_2->n_runs) {
6087 run_container_append(dst, src_2->runs[rlepos], &previousrle);
6088 rlepos++;
6089 }
6090 }
6091 }
6092
6093 void array_run_container_inplace_union(const array_container_t *src_1,
6094 run_container_t *src_2) {
6095 if (run_container_is_full(src_2)) {
6096 return;
6097 }
6098 const int32_t maxoutput = src_1->cardinality + src_2->n_runs;
6099 const int32_t neededcapacity = maxoutput + src_2->n_runs;
6100 if (src_2->capacity < neededcapacity)
6101 run_container_grow(src_2, neededcapacity, true);
6102 memmove(src_2->runs + maxoutput, src_2->runs,
6103 src_2->n_runs * sizeof(rle16_t));
6104 rle16_t *inputsrc2 = src_2->runs + maxoutput;
6105 int32_t rlepos = 0;
6106 int32_t arraypos = 0;
6107 int src2nruns = src_2->n_runs;
6108 src_2->n_runs = 0;
6109
6110 rle16_t previousrle;
6111
6112 if (inputsrc2[rlepos].value <= src_1->array[arraypos]) {
6113 previousrle = run_container_append_first(src_2, inputsrc2[rlepos]);
6114 rlepos++;
6115 } else {
6116 previousrle =
6117 run_container_append_value_first(src_2, src_1->array[arraypos]);
6118 arraypos++;
6119 }
6120
6121 while ((rlepos < src2nruns) && (arraypos < src_1->cardinality)) {
6122 if (inputsrc2[rlepos].value <= src_1->array[arraypos]) {
6123 run_container_append(src_2, inputsrc2[rlepos], &previousrle);
6124 rlepos++;
6125 } else {
6126 run_container_append_value(src_2, src_1->array[arraypos],
6127 &previousrle);
6128 arraypos++;
6129 }
6130 }
6131 if (arraypos < src_1->cardinality) {
6132 while (arraypos < src_1->cardinality) {
6133 run_container_append_value(src_2, src_1->array[arraypos],
6134 &previousrle);
6135 arraypos++;
6136 }
6137 } else {
6138 while (rlepos < src2nruns) {
6139 run_container_append(src_2, inputsrc2[rlepos], &previousrle);
6140 rlepos++;
6141 }
6142 }
6143 }
6144
6145 bool array_array_container_union(const array_container_t *src_1,
6146 const array_container_t *src_2, void **dst) {
6147 int totalCardinality = src_1->cardinality + src_2->cardinality;
6148 if (totalCardinality <= DEFAULT_MAX_SIZE) {
6149 *dst = array_container_create_given_capacity(totalCardinality);
6150 if (*dst != NULL) {
6151 array_container_union(src_1, src_2, (array_container_t *)*dst);
6152 } else {
6153 return true; // otherwise failure won't be caught
6154 }
6155 return false; // not a bitset
6156 }
6157 *dst = bitset_container_create();
6158 bool returnval = true; // expect a bitset
6159 if (*dst != NULL) {
6160 bitset_container_t *ourbitset = (bitset_container_t *)*dst;
6161 bitset_set_list(ourbitset->array, src_1->array, src_1->cardinality);
6162 ourbitset->cardinality = (int32_t)bitset_set_list_withcard(
6163 ourbitset->array, src_1->cardinality, src_2->array,
6164 src_2->cardinality);
6165 if (ourbitset->cardinality <= DEFAULT_MAX_SIZE) {
6166 // need to convert!
6167 *dst = array_container_from_bitset(ourbitset);
6168 bitset_container_free(ourbitset);
6169 returnval = false; // not going to be a bitset
6170 }
6171 }
6172 return returnval;
6173 }
6174
6175 bool array_array_container_inplace_union(array_container_t *src_1,
6176 const array_container_t *src_2, void **dst) {
6177 int totalCardinality = src_1->cardinality + src_2->cardinality;
6178 *dst = NULL;
6179 if (totalCardinality <= DEFAULT_MAX_SIZE) {
6180 if(src_1->capacity < totalCardinality) {
6181 *dst = array_container_create_given_capacity(2 * totalCardinality); // be purposefully generous
6182 if (*dst != NULL) {
6183 array_container_union(src_1, src_2, (array_container_t *)*dst);
6184 } else {
6185 return true; // otherwise failure won't be caught
6186 }
6187 return false; // not a bitset
6188 } else {
6189 memmove(src_1->array + src_2->cardinality, src_1->array, src_1->cardinality * sizeof(uint16_t));
6190 src_1->cardinality = (int32_t)union_uint16(src_1->array + src_2->cardinality, src_1->cardinality,
6191 src_2->array, src_2->cardinality, src_1->array);
6192 return false; // not a bitset
6193 }
6194 }
6195 *dst = bitset_container_create();
6196 bool returnval = true; // expect a bitset
6197 if (*dst != NULL) {
6198 bitset_container_t *ourbitset = (bitset_container_t *)*dst;
6199 bitset_set_list(ourbitset->array, src_1->array, src_1->cardinality);
6200 ourbitset->cardinality = (int32_t)bitset_set_list_withcard(
6201 ourbitset->array, src_1->cardinality, src_2->array,
6202 src_2->cardinality);
6203 if (ourbitset->cardinality <= DEFAULT_MAX_SIZE) {
6204 // need to convert!
6205 if(src_1->capacity < ourbitset->cardinality) {
6206 array_container_grow(src_1, ourbitset->cardinality, false);
6207 }
6208
6209 bitset_extract_setbits_uint16(ourbitset->array, BITSET_CONTAINER_SIZE_IN_WORDS,
6210 src_1->array, 0);
6211 src_1->cardinality = ourbitset->cardinality;
6212 *dst = src_1;
6213 bitset_container_free(ourbitset);
6214 returnval = false; // not going to be a bitset
6215 }
6216 }
6217 return returnval;
6218 }
6219
6220
6221 bool array_array_container_lazy_union(const array_container_t *src_1,
6222 const array_container_t *src_2,
6223 void **dst) {
6224 int totalCardinality = src_1->cardinality + src_2->cardinality;
6225 if (totalCardinality <= ARRAY_LAZY_LOWERBOUND) {
6226 *dst = array_container_create_given_capacity(totalCardinality);
6227 if (*dst != NULL) {
6228 array_container_union(src_1, src_2, (array_container_t *)*dst);
6229 } else {
6230 return true; // otherwise failure won't be caught
6231 }
6232 return false; // not a bitset
6233 }
6234 *dst = bitset_container_create();
6235 bool returnval = true; // expect a bitset
6236 if (*dst != NULL) {
6237 bitset_container_t *ourbitset = (bitset_container_t *)*dst;
6238 bitset_set_list(ourbitset->array, src_1->array, src_1->cardinality);
6239 bitset_set_list(ourbitset->array, src_2->array, src_2->cardinality);
6240 ourbitset->cardinality = BITSET_UNKNOWN_CARDINALITY;
6241 }
6242 return returnval;
6243 }
6244
6245
6246 bool array_array_container_lazy_inplace_union(array_container_t *src_1,
6247 const array_container_t *src_2,
6248 void **dst) {
6249 int totalCardinality = src_1->cardinality + src_2->cardinality;
6250 *dst = NULL;
6251 if (totalCardinality <= ARRAY_LAZY_LOWERBOUND) {
6252 if(src_1->capacity < totalCardinality) {
6253 *dst = array_container_create_given_capacity(2 * totalCardinality); // be purposefully generous
6254 if (*dst != NULL) {
6255 array_container_union(src_1, src_2, (array_container_t *)*dst);
6256 } else {
6257 return true; // otherwise failure won't be caught
6258 }
6259 return false; // not a bitset
6260 } else {
6261 memmove(src_1->array + src_2->cardinality, src_1->array, src_1->cardinality * sizeof(uint16_t));
6262 src_1->cardinality = (int32_t)union_uint16(src_1->array + src_2->cardinality, src_1->cardinality,
6263 src_2->array, src_2->cardinality, src_1->array);
6264 return false; // not a bitset
6265 }
6266 }
6267 *dst = bitset_container_create();
6268 bool returnval = true; // expect a bitset
6269 if (*dst != NULL) {
6270 bitset_container_t *ourbitset = (bitset_container_t *)*dst;
6271 bitset_set_list(ourbitset->array, src_1->array, src_1->cardinality);
6272 bitset_set_list(ourbitset->array, src_2->array, src_2->cardinality);
6273 ourbitset->cardinality = BITSET_UNKNOWN_CARDINALITY;
6274 }
6275 return returnval;
6276 }
6277 /* end file src/containers/mixed_union.c */
6278 /* begin file src/containers/mixed_xor.c */
6279 /*
6280 * mixed_xor.c
6281 */
6282
6283 #include <assert.h>
6284 #include <string.h>
6285
6286
6287 /* Compute the xor of src_1 and src_2 and write the result to
6288 * dst (which has no container initially).
6289 * Result is true iff dst is a bitset */
6290 bool array_bitset_container_xor(const array_container_t *src_1,
6291 const bitset_container_t *src_2, void **dst) {
6292 bitset_container_t *result = bitset_container_create();
6293 bitset_container_copy(src_2, result);
6294 result->cardinality = (int32_t)bitset_flip_list_withcard(
6295 result->array, result->cardinality, src_1->array, src_1->cardinality);
6296
6297 // do required type conversions.
6298 if (result->cardinality <= DEFAULT_MAX_SIZE) {
6299 *dst = array_container_from_bitset(result);
6300 bitset_container_free(result);
6301 return false; // not bitset
6302 }
6303 *dst = result;
6304 return true; // bitset
6305 }
6306
6307 /* Compute the xor of src_1 and src_2 and write the result to
6308 * dst. It is allowed for src_2 to be dst. This version does not
6309 * update the cardinality of dst (it is set to BITSET_UNKNOWN_CARDINALITY).
6310 */
6311
6312 void array_bitset_container_lazy_xor(const array_container_t *src_1,
6313 const bitset_container_t *src_2,
6314 bitset_container_t *dst) {
6315 if (src_2 != dst) bitset_container_copy(src_2, dst);
6316 bitset_flip_list(dst->array, src_1->array, src_1->cardinality);
6317 dst->cardinality = BITSET_UNKNOWN_CARDINALITY;
6318 }
6319
6320 /* Compute the xor of src_1 and src_2 and write the result to
6321 * dst. Result may be either a bitset or an array container
6322 * (returns "result is bitset"). dst does not initially have
6323 * any container, but becomes either a bitset container (return
6324 * result true) or an array container.
6325 */
6326
6327 bool run_bitset_container_xor(const run_container_t *src_1,
6328 const bitset_container_t *src_2, void **dst) {
6329 bitset_container_t *result = bitset_container_create();
6330
6331 bitset_container_copy(src_2, result);
6332 for (int32_t rlepos = 0; rlepos < src_1->n_runs; ++rlepos) {
6333 rle16_t rle = src_1->runs[rlepos];
6334 bitset_flip_range(result->array, rle.value,
6335 rle.value + rle.length + UINT32_C(1));
6336 }
6337 result->cardinality = bitset_container_compute_cardinality(result);
6338
6339 if (result->cardinality <= DEFAULT_MAX_SIZE) {
6340 *dst = array_container_from_bitset(result);
6341 bitset_container_free(result);
6342 return false; // not bitset
6343 }
6344 *dst = result;
6345 return true; // bitset
6346 }
6347
6348 /* lazy xor. Dst is initialized and may be equal to src_2.
6349 * Result is left as a bitset container, even if actual
6350 * cardinality would dictate an array container.
6351 */
6352
6353 void run_bitset_container_lazy_xor(const run_container_t *src_1,
6354 const bitset_container_t *src_2,
6355 bitset_container_t *dst) {
6356 if (src_2 != dst) bitset_container_copy(src_2, dst);
6357 for (int32_t rlepos = 0; rlepos < src_1->n_runs; ++rlepos) {
6358 rle16_t rle = src_1->runs[rlepos];
6359 bitset_flip_range(dst->array, rle.value,
6360 rle.value + rle.length + UINT32_C(1));
6361 }
6362 dst->cardinality = BITSET_UNKNOWN_CARDINALITY;
6363 }
6364
6365 /* dst does not indicate a valid container initially. Eventually it
6366 * can become any kind of container.
6367 */
6368
6369 int array_run_container_xor(const array_container_t *src_1,
6370 const run_container_t *src_2, void **dst) {
6371 // semi following Java XOR implementation as of May 2016
6372 // the C OR implementation works quite differently and can return a run
6373 // container
6374 // TODO could optimize for full run containers.
6375
6376 // use of lazy following Java impl.
6377 const int arbitrary_threshold = 32;
6378 if (src_1->cardinality < arbitrary_threshold) {
6379 run_container_t *ans = run_container_create();
6380 array_run_container_lazy_xor(src_1, src_2, ans); // keeps runs.
6381 uint8_t typecode_after;
6382 *dst =
6383 convert_run_to_efficient_container_and_free(ans, &typecode_after);
6384 return typecode_after;
6385 }
6386
6387 int card = run_container_cardinality(src_2);
6388 if (card <= DEFAULT_MAX_SIZE) {
6389 // Java implementation works with the array, xoring the run elements via
6390 // iterator
6391 array_container_t *temp = array_container_from_run(src_2);
6392 bool ret_is_bitset = array_array_container_xor(temp, src_1, dst);
6393 array_container_free(temp);
6394 return ret_is_bitset ? BITSET_CONTAINER_TYPE_CODE
6395 : ARRAY_CONTAINER_TYPE_CODE;
6396
6397 } else { // guess that it will end up as a bitset
6398 bitset_container_t *result = bitset_container_from_run(src_2);
6399 bool is_bitset = bitset_array_container_ixor(result, src_1, dst);
6400 // any necessary type conversion has been done by the ixor
6401 int retval = (is_bitset ? BITSET_CONTAINER_TYPE_CODE
6402 : ARRAY_CONTAINER_TYPE_CODE);
6403 return retval;
6404 }
6405 }
6406
6407 /* Dst is a valid run container. (Can it be src_2? Let's say not.)
6408 * Leaves result as run container, even if other options are
6409 * smaller.
6410 */
6411
6412 void array_run_container_lazy_xor(const array_container_t *src_1,
6413 const run_container_t *src_2,
6414 run_container_t *dst) {
6415 run_container_grow(dst, src_1->cardinality + src_2->n_runs, false);
6416 int32_t rlepos = 0;
6417 int32_t arraypos = 0;
6418 dst->n_runs = 0;
6419
6420 while ((rlepos < src_2->n_runs) && (arraypos < src_1->cardinality)) {
6421 if (src_2->runs[rlepos].value <= src_1->array[arraypos]) {
6422 run_container_smart_append_exclusive(dst, src_2->runs[rlepos].value,
6423 src_2->runs[rlepos].length);
6424 rlepos++;
6425 } else {
6426 run_container_smart_append_exclusive(dst, src_1->array[arraypos],
6427 0);
6428 arraypos++;
6429 }
6430 }
6431 while (arraypos < src_1->cardinality) {
6432 run_container_smart_append_exclusive(dst, src_1->array[arraypos], 0);
6433 arraypos++;
6434 }
6435 while (rlepos < src_2->n_runs) {
6436 run_container_smart_append_exclusive(dst, src_2->runs[rlepos].value,
6437 src_2->runs[rlepos].length);
6438 rlepos++;
6439 }
6440 }
6441
6442 /* dst does not indicate a valid container initially. Eventually it
6443 * can become any kind of container.
6444 */
6445
6446 int run_run_container_xor(const run_container_t *src_1,
6447 const run_container_t *src_2, void **dst) {
6448 run_container_t *ans = run_container_create();
6449 run_container_xor(src_1, src_2, ans);
6450 uint8_t typecode_after;
6451 *dst = convert_run_to_efficient_container_and_free(ans, &typecode_after);
6452 return typecode_after;
6453 }
6454
6455 /*
6456 * Java implementation (as of May 2016) for array_run, run_run
6457 * and bitset_run don't do anything different for inplace.
6458 * Could adopt the mixed_union.c approach instead (ie, using
6459 * smart_append_exclusive)
6460 *
6461 */
6462
6463 bool array_array_container_xor(const array_container_t *src_1,
6464 const array_container_t *src_2, void **dst) {
6465 int totalCardinality =
6466 src_1->cardinality + src_2->cardinality; // upper bound
6467 if (totalCardinality <= DEFAULT_MAX_SIZE) {
6468 *dst = array_container_create_given_capacity(totalCardinality);
6469 array_container_xor(src_1, src_2, (array_container_t *)*dst);
6470 return false; // not a bitset
6471 }
6472 *dst = bitset_container_from_array(src_1);
6473 bool returnval = true; // expect a bitset
6474 bitset_container_t *ourbitset = (bitset_container_t *)*dst;
6475 ourbitset->cardinality = (uint32_t)bitset_flip_list_withcard(
6476 ourbitset->array, src_1->cardinality, src_2->array, src_2->cardinality);
6477 if (ourbitset->cardinality <= DEFAULT_MAX_SIZE) {
6478 // need to convert!
6479 *dst = array_container_from_bitset(ourbitset);
6480 bitset_container_free(ourbitset);
6481 returnval = false; // not going to be a bitset
6482 }
6483
6484 return returnval;
6485 }
6486
6487 bool array_array_container_lazy_xor(const array_container_t *src_1,
6488 const array_container_t *src_2,
6489 void **dst) {
6490 int totalCardinality = src_1->cardinality + src_2->cardinality;
6491 // upper bound, but probably poor estimate for xor
6492 if (totalCardinality <= ARRAY_LAZY_LOWERBOUND) {
6493 *dst = array_container_create_given_capacity(totalCardinality);
6494 if (*dst != NULL)
6495 array_container_xor(src_1, src_2, (array_container_t *)*dst);
6496 return false; // not a bitset
6497 }
6498 *dst = bitset_container_from_array(src_1);
6499 bool returnval = true; // expect a bitset (maybe, for XOR??)
6500 if (*dst != NULL) {
6501 bitset_container_t *ourbitset = (bitset_container_t *)*dst;
6502 bitset_flip_list(ourbitset->array, src_2->array, src_2->cardinality);
6503 ourbitset->cardinality = BITSET_UNKNOWN_CARDINALITY;
6504 }
6505 return returnval;
6506 }
6507
6508 /* Compute the xor of src_1 and src_2 and write the result to
6509 * dst (which has no container initially). Return value is
6510 * "dst is a bitset"
6511 */
6512
6513 bool bitset_bitset_container_xor(const bitset_container_t *src_1,
6514 const bitset_container_t *src_2, void **dst) {
6515 bitset_container_t *ans = bitset_container_create();
6516 int card = bitset_container_xor(src_1, src_2, ans);
6517 if (card <= DEFAULT_MAX_SIZE) {
6518 *dst = array_container_from_bitset(ans);
6519 bitset_container_free(ans);
6520 return false; // not bitset
6521 } else {
6522 *dst = ans;
6523 return true;
6524 }
6525 }
6526
6527 /* Compute the xor of src_1 and src_2 and write the result to
6528 * dst (which has no container initially). It will modify src_1
6529 * to be dst if the result is a bitset. Otherwise, it will
6530 * free src_1 and dst will be a new array container. In both
6531 * cases, the caller is responsible for deallocating dst.
6532 * Returns true iff dst is a bitset */
6533
6534 bool bitset_array_container_ixor(bitset_container_t *src_1,
6535 const array_container_t *src_2, void **dst) {
6536 *dst = src_1;
6537 src_1->cardinality = (uint32_t)bitset_flip_list_withcard(
6538 src_1->array, src_1->cardinality, src_2->array, src_2->cardinality);
6539
6540 if (src_1->cardinality <= DEFAULT_MAX_SIZE) {
6541 *dst = array_container_from_bitset(src_1);
6542 bitset_container_free(src_1);
6543 return false; // not bitset
6544 } else
6545 return true;
6546 }
6547
6548 /* a bunch of in-place, some of which may not *really* be inplace.
6549 * TODO: write actual inplace routine if efficiency warrants it
6550 * Anything inplace with a bitset is a good candidate
6551 */
6552
6553 bool bitset_bitset_container_ixor(bitset_container_t *src_1,
6554 const bitset_container_t *src_2, void **dst) {
6555 bool ans = bitset_bitset_container_xor(src_1, src_2, dst);
6556 bitset_container_free(src_1);
6557 return ans;
6558 }
6559
6560 bool array_bitset_container_ixor(array_container_t *src_1,
6561 const bitset_container_t *src_2, void **dst) {
6562 bool ans = array_bitset_container_xor(src_1, src_2, dst);
6563 array_container_free(src_1);
6564 return ans;
6565 }
6566
6567 /* Compute the xor of src_1 and src_2 and write the result to
6568 * dst. Result may be either a bitset or an array container
6569 * (returns "result is bitset"). dst does not initially have
6570 * any container, but becomes either a bitset container (return
6571 * result true) or an array container.
6572 */
6573
6574 bool run_bitset_container_ixor(run_container_t *src_1,
6575 const bitset_container_t *src_2, void **dst) {
6576 bool ans = run_bitset_container_xor(src_1, src_2, dst);
6577 run_container_free(src_1);
6578 return ans;
6579 }
6580
6581 bool bitset_run_container_ixor(bitset_container_t *src_1,
6582 const run_container_t *src_2, void **dst) {
6583 bool ans = run_bitset_container_xor(src_2, src_1, dst);
6584 bitset_container_free(src_1);
6585 return ans;
6586 }
6587
6588 /* dst does not indicate a valid container initially. Eventually it
6589 * can become any kind of container.
6590 */
6591
6592 int array_run_container_ixor(array_container_t *src_1,
6593 const run_container_t *src_2, void **dst) {
6594 int ans = array_run_container_xor(src_1, src_2, dst);
6595 array_container_free(src_1);
6596 return ans;
6597 }
6598
6599 int run_array_container_ixor(run_container_t *src_1,
6600 const array_container_t *src_2, void **dst) {
6601 int ans = array_run_container_xor(src_2, src_1, dst);
6602 run_container_free(src_1);
6603 return ans;
6604 }
6605
6606 bool array_array_container_ixor(array_container_t *src_1,
6607 const array_container_t *src_2, void **dst) {
6608 bool ans = array_array_container_xor(src_1, src_2, dst);
6609 array_container_free(src_1);
6610 return ans;
6611 }
6612
6613 int run_run_container_ixor(run_container_t *src_1, const run_container_t *src_2,
6614 void **dst) {
6615 int ans = run_run_container_xor(src_1, src_2, dst);
6616 run_container_free(src_1);
6617 return ans;
6618 }
6619 /* end file src/containers/mixed_xor.c */
6620 /* begin file src/containers/run.c */
6621 #include <stdio.h>
6622 #include <stdlib.h>
6623
6624
6625 bool run_container_add(run_container_t *run, uint16_t pos) {
6626 int32_t index = interleavedBinarySearch(run->runs, run->n_runs, pos);
6627 if (index >= 0) return false; // already there
6628 index = -index - 2; // points to preceding value, possibly -1
6629 if (index >= 0) { // possible match
6630 int32_t offset = pos - run->runs[index].value;
6631 int32_t le = run->runs[index].length;
6632 if (offset <= le) return false; // already there
6633 if (offset == le + 1) {
6634 // we may need to fuse
6635 if (index + 1 < run->n_runs) {
6636 if (run->runs[index + 1].value == pos + 1) {
6637 // indeed fusion is needed
6638 run->runs[index].length = run->runs[index + 1].value +
6639 run->runs[index + 1].length -
6640 run->runs[index].value;
6641 recoverRoomAtIndex(run, (uint16_t)(index + 1));
6642 return true;
6643 }
6644 }
6645 run->runs[index].length++;
6646 return true;
6647 }
6648 if (index + 1 < run->n_runs) {
6649 // we may need to fuse
6650 if (run->runs[index + 1].value == pos + 1) {
6651 // indeed fusion is needed
6652 run->runs[index + 1].value = pos;
6653 run->runs[index + 1].length = run->runs[index + 1].length + 1;
6654 return true;
6655 }
6656 }
6657 }
6658 if (index == -1) {
6659 // we may need to extend the first run
6660 if (0 < run->n_runs) {
6661 if (run->runs[0].value == pos + 1) {
6662 run->runs[0].length++;
6663 run->runs[0].value--;
6664 return true;
6665 }
6666 }
6667 }
6668 makeRoomAtIndex(run, (uint16_t)(index + 1));
6669 run->runs[index + 1].value = pos;
6670 run->runs[index + 1].length = 0;
6671 return true;
6672 }
6673
6674 /* Create a new run container. Return NULL in case of failure. */
6675 run_container_t *run_container_create_given_capacity(int32_t size) {
6676 run_container_t *run;
6677 /* Allocate the run container itself. */
6678 run = (run_container_t *)malloc(sizeof(run_container_t));
6679 assert (run);
6680 if (size <= 0) // we don't want to rely on malloc(0)
6681 run->runs = NULL;
6682 run->runs = (rle16_t *)malloc(sizeof(rle16_t) * size);
6683 assert (run->runs);
6684 run->capacity = size;
6685 run->n_runs = 0;
6686 return run;
6687 }
6688
6689 int run_container_shrink_to_fit(run_container_t *src) {
6690 if (src->n_runs == src->capacity) return 0; // nothing to do
6691 int savings = src->capacity - src->n_runs;
6692 src->capacity = src->n_runs;
6693 rle16_t *oldruns = src->runs;
6694 src->runs = (rle16_t *)realloc(oldruns, src->capacity * sizeof(rle16_t));
6695 return savings;
6696 }
6697 /* Create a new run container. Return NULL in case of failure. */
6698 run_container_t *run_container_create(void) {
6699 return run_container_create_given_capacity(RUN_DEFAULT_INIT_SIZE);
6700 }
6701
6702 run_container_t *run_container_clone(const run_container_t *src) {
6703 run_container_t *run = run_container_create_given_capacity(src->capacity);
6704 if (run == NULL) return NULL;
6705 run->capacity = src->capacity;
6706 run->n_runs = src->n_runs;
6707 memcpy(run->runs, src->runs, src->n_runs * sizeof(rle16_t));
6708 return run;
6709 }
6710
6711 /* Free memory. */
6712 void run_container_free(run_container_t *run) {
6713 if(run->runs != NULL) {// Jon Strabala reports that some tools complain otherwise
6714 free(run->runs);
6715 run->runs = NULL; // pedantic
6716 }
6717 free(run);
6718 }
6719
6720 void run_container_grow(run_container_t *run, int32_t min, bool copy) {
6721 int32_t newCapacity =
6722 (run->capacity == 0)
6723 ? RUN_DEFAULT_INIT_SIZE
6724 : run->capacity < 64 ? run->capacity * 2
6725 : run->capacity < 1024 ? run->capacity * 3 / 2
6726 : run->capacity * 5 / 4;
6727 if (newCapacity < min) newCapacity = min;
6728 run->capacity = newCapacity;
6729 assert(run->capacity >= min);
6730 if (copy) {
6731 rle16_t *oldruns = run->runs;
6732 run->runs =
6733 (rle16_t *)realloc(oldruns, run->capacity * sizeof(rle16_t));
6734 } else {
6735 // Jon Strabala reports that some tools complain otherwise
6736 if (run->runs != NULL) {
6737 free(run->runs);
6738 }
6739 run->runs = (rle16_t *)malloc(run->capacity * sizeof(rle16_t));
6740 }
6741 // handle the case where realloc fails
6742 if (run->runs == NULL) {
6743 fprintf(stderr, "could not allocate memory\n");
6744 }
6745 assert(run->runs != NULL);
6746 }
6747
6748 /* copy one container into another */
6749 void run_container_copy(const run_container_t *src, run_container_t *dst) {
6750 const int32_t n_runs = src->n_runs;
6751 if (src->n_runs > dst->capacity) {
6752 run_container_grow(dst, n_runs, false);
6753 }
6754 dst->n_runs = n_runs;
6755 memcpy(dst->runs, src->runs, sizeof(rle16_t) * n_runs);
6756 }
6757
6758 /* Compute the union of `src_1' and `src_2' and write the result to `dst'
6759 * It is assumed that `dst' is distinct from both `src_1' and `src_2'. */
6760 void run_container_union(const run_container_t *src_1,
6761 const run_container_t *src_2, run_container_t *dst) {
6762 // TODO: this could be a lot more efficient
6763
6764 // we start out with inexpensive checks
6765 const bool if1 = run_container_is_full(src_1);
6766 const bool if2 = run_container_is_full(src_2);
6767 if (if1 || if2) {
6768 if (if1) {
6769 run_container_copy(src_1, dst);
6770 return;
6771 }
6772 if (if2) {
6773 run_container_copy(src_2, dst);
6774 return;
6775 }
6776 }
6777 const int32_t neededcapacity = src_1->n_runs + src_2->n_runs;
6778 if (dst->capacity < neededcapacity)
6779 run_container_grow(dst, neededcapacity, false);
6780 dst->n_runs = 0;
6781 int32_t rlepos = 0;
6782 int32_t xrlepos = 0;
6783
6784 rle16_t previousrle;
6785 if (src_1->runs[rlepos].value <= src_2->runs[xrlepos].value) {
6786 previousrle = run_container_append_first(dst, src_1->runs[rlepos]);
6787 rlepos++;
6788 } else {
6789 previousrle = run_container_append_first(dst, src_2->runs[xrlepos]);
6790 xrlepos++;
6791 }
6792
6793 while ((xrlepos < src_2->n_runs) && (rlepos < src_1->n_runs)) {
6794 rle16_t newrl;
6795 if (src_1->runs[rlepos].value <= src_2->runs[xrlepos].value) {
6796 newrl = src_1->runs[rlepos];
6797 rlepos++;
6798 } else {
6799 newrl = src_2->runs[xrlepos];
6800 xrlepos++;
6801 }
6802 run_container_append(dst, newrl, &previousrle);
6803 }
6804 while (xrlepos < src_2->n_runs) {
6805 run_container_append(dst, src_2->runs[xrlepos], &previousrle);
6806 xrlepos++;
6807 }
6808 while (rlepos < src_1->n_runs) {
6809 run_container_append(dst, src_1->runs[rlepos], &previousrle);
6810 rlepos++;
6811 }
6812 }
6813
6814 /* Compute the union of `src_1' and `src_2' and write the result to `src_1'
6815 */
6816 void run_container_union_inplace(run_container_t *src_1,
6817 const run_container_t *src_2) {
6818 // TODO: this could be a lot more efficient
6819
6820 // we start out with inexpensive checks
6821 const bool if1 = run_container_is_full(src_1);
6822 const bool if2 = run_container_is_full(src_2);
6823 if (if1 || if2) {
6824 if (if1) {
6825 return;
6826 }
6827 if (if2) {
6828 run_container_copy(src_2, src_1);
6829 return;
6830 }
6831 }
6832 // we move the data to the end of the current array
6833 const int32_t maxoutput = src_1->n_runs + src_2->n_runs;
6834 const int32_t neededcapacity = maxoutput + src_1->n_runs;
6835 if (src_1->capacity < neededcapacity)
6836 run_container_grow(src_1, neededcapacity, true);
6837 memmove(src_1->runs + maxoutput, src_1->runs,
6838 src_1->n_runs * sizeof(rle16_t));
6839 rle16_t *inputsrc1 = src_1->runs + maxoutput;
6840 const int32_t input1nruns = src_1->n_runs;
6841 src_1->n_runs = 0;
6842 int32_t rlepos = 0;
6843 int32_t xrlepos = 0;
6844
6845 rle16_t previousrle;
6846 if (inputsrc1[rlepos].value <= src_2->runs[xrlepos].value) {
6847 previousrle = run_container_append_first(src_1, inputsrc1[rlepos]);
6848 rlepos++;
6849 } else {
6850 previousrle = run_container_append_first(src_1, src_2->runs[xrlepos]);
6851 xrlepos++;
6852 }
6853 while ((xrlepos < src_2->n_runs) && (rlepos < input1nruns)) {
6854 rle16_t newrl;
6855 if (inputsrc1[rlepos].value <= src_2->runs[xrlepos].value) {
6856 newrl = inputsrc1[rlepos];
6857 rlepos++;
6858 } else {
6859 newrl = src_2->runs[xrlepos];
6860 xrlepos++;
6861 }
6862 run_container_append(src_1, newrl, &previousrle);
6863 }
6864 while (xrlepos < src_2->n_runs) {
6865 run_container_append(src_1, src_2->runs[xrlepos], &previousrle);
6866 xrlepos++;
6867 }
6868 while (rlepos < input1nruns) {
6869 run_container_append(src_1, inputsrc1[rlepos], &previousrle);
6870 rlepos++;
6871 }
6872 }
6873
6874 /* Compute the symmetric difference of `src_1' and `src_2' and write the result
6875 * to `dst'
6876 * It is assumed that `dst' is distinct from both `src_1' and `src_2'. */
6877 void run_container_xor(const run_container_t *src_1,
6878 const run_container_t *src_2, run_container_t *dst) {
6879 // don't bother to convert xor with full range into negation
6880 // since negation is implemented similarly
6881
6882 const int32_t neededcapacity = src_1->n_runs + src_2->n_runs;
6883 if (dst->capacity < neededcapacity)
6884 run_container_grow(dst, neededcapacity, false);
6885
6886 int32_t pos1 = 0;
6887 int32_t pos2 = 0;
6888 dst->n_runs = 0;
6889
6890 while ((pos1 < src_1->n_runs) && (pos2 < src_2->n_runs)) {
6891 if (src_1->runs[pos1].value <= src_2->runs[pos2].value) {
6892 run_container_smart_append_exclusive(dst, src_1->runs[pos1].value,
6893 src_1->runs[pos1].length);
6894 pos1++;
6895 } else {
6896 run_container_smart_append_exclusive(dst, src_2->runs[pos2].value,
6897 src_2->runs[pos2].length);
6898 pos2++;
6899 }
6900 }
6901 while (pos1 < src_1->n_runs) {
6902 run_container_smart_append_exclusive(dst, src_1->runs[pos1].value,
6903 src_1->runs[pos1].length);
6904 pos1++;
6905 }
6906
6907 while (pos2 < src_2->n_runs) {
6908 run_container_smart_append_exclusive(dst, src_2->runs[pos2].value,
6909 src_2->runs[pos2].length);
6910 pos2++;
6911 }
6912 }
6913
6914 /* Compute the intersection of src_1 and src_2 and write the result to
6915 * dst. It is assumed that dst is distinct from both src_1 and src_2. */
6916 void run_container_intersection(const run_container_t *src_1,
6917 const run_container_t *src_2,
6918 run_container_t *dst) {
6919 const bool if1 = run_container_is_full(src_1);
6920 const bool if2 = run_container_is_full(src_2);
6921 if (if1 || if2) {
6922 if (if1) {
6923 run_container_copy(src_2, dst);
6924 return;
6925 }
6926 if (if2) {
6927 run_container_copy(src_1, dst);
6928 return;
6929 }
6930 }
6931 // TODO: this could be a lot more efficient, could use SIMD optimizations
6932 const int32_t neededcapacity = src_1->n_runs + src_2->n_runs;
6933 if (dst->capacity < neededcapacity)
6934 run_container_grow(dst, neededcapacity, false);
6935 dst->n_runs = 0;
6936 int32_t rlepos = 0;
6937 int32_t xrlepos = 0;
6938 int32_t start = src_1->runs[rlepos].value;
6939 int32_t end = start + src_1->runs[rlepos].length + 1;
6940 int32_t xstart = src_2->runs[xrlepos].value;
6941 int32_t xend = xstart + src_2->runs[xrlepos].length + 1;
6942 while ((rlepos < src_1->n_runs) && (xrlepos < src_2->n_runs)) {
6943 if (end <= xstart) {
6944 ++rlepos;
6945 if (rlepos < src_1->n_runs) {
6946 start = src_1->runs[rlepos].value;
6947 end = start + src_1->runs[rlepos].length + 1;
6948 }
6949 } else if (xend <= start) {
6950 ++xrlepos;
6951 if (xrlepos < src_2->n_runs) {
6952 xstart = src_2->runs[xrlepos].value;
6953 xend = xstart + src_2->runs[xrlepos].length + 1;
6954 }
6955 } else { // they overlap
6956 const int32_t lateststart = start > xstart ? start : xstart;
6957 int32_t earliestend;
6958 if (end == xend) { // improbable
6959 earliestend = end;
6960 rlepos++;
6961 xrlepos++;
6962 if (rlepos < src_1->n_runs) {
6963 start = src_1->runs[rlepos].value;
6964 end = start + src_1->runs[rlepos].length + 1;
6965 }
6966 if (xrlepos < src_2->n_runs) {
6967 xstart = src_2->runs[xrlepos].value;
6968 xend = xstart + src_2->runs[xrlepos].length + 1;
6969 }
6970 } else if (end < xend) {
6971 earliestend = end;
6972 rlepos++;
6973 if (rlepos < src_1->n_runs) {
6974 start = src_1->runs[rlepos].value;
6975 end = start + src_1->runs[rlepos].length + 1;
6976 }
6977
6978 } else { // end > xend
6979 earliestend = xend;
6980 xrlepos++;
6981 if (xrlepos < src_2->n_runs) {
6982 xstart = src_2->runs[xrlepos].value;
6983 xend = xstart + src_2->runs[xrlepos].length + 1;
6984 }
6985 }
6986 dst->runs[dst->n_runs].value = (uint16_t)lateststart;
6987 dst->runs[dst->n_runs].length =
6988 (uint16_t)(earliestend - lateststart - 1);
6989 dst->n_runs++;
6990 }
6991 }
6992 }
6993
6994 /* Compute the size of the intersection of src_1 and src_2 . */
6995 int run_container_intersection_cardinality(const run_container_t *src_1,
6996 const run_container_t *src_2) {
6997 const bool if1 = run_container_is_full(src_1);
6998 const bool if2 = run_container_is_full(src_2);
6999 if (if1 || if2) {
7000 if (if1) {
7001 return run_container_cardinality(src_2);
7002 }
7003 if (if2) {
7004 return run_container_cardinality(src_1);
7005 }
7006 }
7007 int answer = 0;
7008 int32_t rlepos = 0;
7009 int32_t xrlepos = 0;
7010 int32_t start = src_1->runs[rlepos].value;
7011 int32_t end = start + src_1->runs[rlepos].length + 1;
7012 int32_t xstart = src_2->runs[xrlepos].value;
7013 int32_t xend = xstart + src_2->runs[xrlepos].length + 1;
7014 while ((rlepos < src_1->n_runs) && (xrlepos < src_2->n_runs)) {
7015 if (end <= xstart) {
7016 ++rlepos;
7017 if (rlepos < src_1->n_runs) {
7018 start = src_1->runs[rlepos].value;
7019 end = start + src_1->runs[rlepos].length + 1;
7020 }
7021 } else if (xend <= start) {
7022 ++xrlepos;
7023 if (xrlepos < src_2->n_runs) {
7024 xstart = src_2->runs[xrlepos].value;
7025 xend = xstart + src_2->runs[xrlepos].length + 1;
7026 }
7027 } else { // they overlap
7028 const int32_t lateststart = start > xstart ? start : xstart;
7029 int32_t earliestend;
7030 if (end == xend) { // improbable
7031 earliestend = end;
7032 rlepos++;
7033 xrlepos++;
7034 if (rlepos < src_1->n_runs) {
7035 start = src_1->runs[rlepos].value;
7036 end = start + src_1->runs[rlepos].length + 1;
7037 }
7038 if (xrlepos < src_2->n_runs) {
7039 xstart = src_2->runs[xrlepos].value;
7040 xend = xstart + src_2->runs[xrlepos].length + 1;
7041 }
7042 } else if (end < xend) {
7043 earliestend = end;
7044 rlepos++;
7045 if (rlepos < src_1->n_runs) {
7046 start = src_1->runs[rlepos].value;
7047 end = start + src_1->runs[rlepos].length + 1;
7048 }
7049
7050 } else { // end > xend
7051 earliestend = xend;
7052 xrlepos++;
7053 if (xrlepos < src_2->n_runs) {
7054 xstart = src_2->runs[xrlepos].value;
7055 xend = xstart + src_2->runs[xrlepos].length + 1;
7056 }
7057 }
7058 answer += earliestend - lateststart;
7059 }
7060 }
7061 return answer;
7062 }
7063
7064 bool run_container_intersect(const run_container_t *src_1,
7065 const run_container_t *src_2) {
7066 const bool if1 = run_container_is_full(src_1);
7067 const bool if2 = run_container_is_full(src_2);
7068 if (if1 || if2) {
7069 if (if1) {
7070 return !run_container_empty(src_2);
7071 }
7072 if (if2) {
7073 return !run_container_empty(src_1);
7074 }
7075 }
7076 int32_t rlepos = 0;
7077 int32_t xrlepos = 0;
7078 int32_t start = src_1->runs[rlepos].value;
7079 int32_t end = start + src_1->runs[rlepos].length + 1;
7080 int32_t xstart = src_2->runs[xrlepos].value;
7081 int32_t xend = xstart + src_2->runs[xrlepos].length + 1;
7082 while ((rlepos < src_1->n_runs) && (xrlepos < src_2->n_runs)) {
7083 if (end <= xstart) {
7084 ++rlepos;
7085 if (rlepos < src_1->n_runs) {
7086 start = src_1->runs[rlepos].value;
7087 end = start + src_1->runs[rlepos].length + 1;
7088 }
7089 } else if (xend <= start) {
7090 ++xrlepos;
7091 if (xrlepos < src_2->n_runs) {
7092 xstart = src_2->runs[xrlepos].value;
7093 xend = xstart + src_2->runs[xrlepos].length + 1;
7094 }
7095 } else { // they overlap
7096 return true;
7097 }
7098 }
7099 return false;
7100 }
7101
7102
7103 /* Compute the difference of src_1 and src_2 and write the result to
7104 * dst. It is assumed that dst is distinct from both src_1 and src_2. */
7105 void run_container_andnot(const run_container_t *src_1,
7106 const run_container_t *src_2, run_container_t *dst) {
7107 // following Java implementation as of June 2016
7108
7109 if (dst->capacity < src_1->n_runs + src_2->n_runs)
7110 run_container_grow(dst, src_1->n_runs + src_2->n_runs, false);
7111
7112 dst->n_runs = 0;
7113
7114 int rlepos1 = 0;
7115 int rlepos2 = 0;
7116 int32_t start = src_1->runs[rlepos1].value;
7117 int32_t end = start + src_1->runs[rlepos1].length + 1;
7118 int32_t start2 = src_2->runs[rlepos2].value;
7119 int32_t end2 = start2 + src_2->runs[rlepos2].length + 1;
7120
7121 while ((rlepos1 < src_1->n_runs) && (rlepos2 < src_2->n_runs)) {
7122 if (end <= start2) {
7123 // output the first run
7124 dst->runs[dst->n_runs++] =
7125 (rle16_t){.value = (uint16_t)start,
7126 .length = (uint16_t)(end - start - 1)};
7127 rlepos1++;
7128 if (rlepos1 < src_1->n_runs) {
7129 start = src_1->runs[rlepos1].value;
7130 end = start + src_1->runs[rlepos1].length + 1;
7131 }
7132 } else if (end2 <= start) {
7133 // exit the second run
7134 rlepos2++;
7135 if (rlepos2 < src_2->n_runs) {
7136 start2 = src_2->runs[rlepos2].value;
7137 end2 = start2 + src_2->runs[rlepos2].length + 1;
7138 }
7139 } else {
7140 if (start < start2) {
7141 dst->runs[dst->n_runs++] =
7142 (rle16_t){.value = (uint16_t)start,
7143 .length = (uint16_t)(start2 - start - 1)};
7144 }
7145 if (end2 < end) {
7146 start = end2;
7147 } else {
7148 rlepos1++;
7149 if (rlepos1 < src_1->n_runs) {
7150 start = src_1->runs[rlepos1].value;
7151 end = start + src_1->runs[rlepos1].length + 1;
7152 }
7153 }
7154 }
7155 }
7156 if (rlepos1 < src_1->n_runs) {
7157 dst->runs[dst->n_runs++] = (rle16_t){
7158 .value = (uint16_t)start, .length = (uint16_t)(end - start - 1)};
7159 rlepos1++;
7160 if (rlepos1 < src_1->n_runs) {
7161 memcpy(dst->runs + dst->n_runs, src_1->runs + rlepos1,
7162 sizeof(rle16_t) * (src_1->n_runs - rlepos1));
7163 dst->n_runs += src_1->n_runs - rlepos1;
7164 }
7165 }
7166 }
7167
7168 int run_container_to_uint32_array(void *vout, const run_container_t *cont,
7169 uint32_t base) {
7170 int outpos = 0;
7171 uint32_t *out = (uint32_t *)vout;
7172 for (int i = 0; i < cont->n_runs; ++i) {
7173 uint32_t run_start = base + cont->runs[i].value;
7174 uint16_t le = cont->runs[i].length;
7175 for (int j = 0; j <= le; ++j) {
7176 uint32_t val = run_start + j;
7177 memcpy(out + outpos, &val,
7178 sizeof(uint32_t)); // should be compiled as a MOV on x64
7179 outpos++;
7180 }
7181 }
7182 return outpos;
7183 }
7184
7185 /*
7186 * Print this container using printf (useful for debugging).
7187 */
7188 void run_container_printf(const run_container_t *cont) {
7189 for (int i = 0; i < cont->n_runs; ++i) {
7190 uint16_t run_start = cont->runs[i].value;
7191 uint16_t le = cont->runs[i].length;
7192 printf("[%d,%d]", run_start, run_start + le);
7193 }
7194 }
7195
7196 /*
7197 * Print this container using printf as a comma-separated list of 32-bit
7198 * integers starting at base.
7199 */
7200 void run_container_printf_as_uint32_array(const run_container_t *cont,
7201 uint32_t base) {
7202 if (cont->n_runs == 0) return;
7203 {
7204 uint32_t run_start = base + cont->runs[0].value;
7205 uint16_t le = cont->runs[0].length;
7206 printf("%u", run_start);
7207 for (uint32_t j = 1; j <= le; ++j) printf(",%u", run_start + j);
7208 }
7209 for (int32_t i = 1; i < cont->n_runs; ++i) {
7210 uint32_t run_start = base + cont->runs[i].value;
7211 uint16_t le = cont->runs[i].length;
7212 for (uint32_t j = 0; j <= le; ++j) printf(",%u", run_start + j);
7213 }
7214 }
7215
7216 int32_t run_container_serialize(const run_container_t *container, char *buf) {
7217 int32_t l, off;
7218
7219 memcpy(buf, &container->n_runs, off = sizeof(container->n_runs));
7220 memcpy(&buf[off], &container->capacity, sizeof(container->capacity));
7221 off += sizeof(container->capacity);
7222
7223 l = sizeof(rle16_t) * container->n_runs;
7224 memcpy(&buf[off], container->runs, l);
7225 return (off + l);
7226 }
7227
7228 int32_t run_container_write(const run_container_t *container, char *buf) {
7229 memcpy(buf, &container->n_runs, sizeof(uint16_t));
7230 memcpy(buf + sizeof(uint16_t), container->runs,
7231 container->n_runs * sizeof(rle16_t));
7232 return run_container_size_in_bytes(container);
7233 }
7234
7235 int32_t run_container_read(int32_t cardinality, run_container_t *container,
7236 const char *buf) {
7237 (void)cardinality;
7238 memcpy(&container->n_runs, buf, sizeof(uint16_t));
7239 if (container->n_runs > container->capacity)
7240 run_container_grow(container, container->n_runs, false);
7241 if(container->n_runs > 0) {
7242 memcpy(container->runs, buf + sizeof(uint16_t),
7243 container->n_runs * sizeof(rle16_t));
7244 }
7245 return run_container_size_in_bytes(container);
7246 }
7247
7248 uint32_t run_container_serialization_len(const run_container_t *container) {
7249 return (sizeof(container->n_runs) + sizeof(container->capacity) +
7250 sizeof(rle16_t) * container->n_runs);
7251 }
7252
7253 void *run_container_deserialize(const char *buf, size_t buf_len) {
7254 run_container_t *ptr;
7255
7256 if (buf_len < 8 /* n_runs + capacity */)
7257 return (NULL);
7258 else
7259 buf_len -= 8;
7260
7261 if ((ptr = (run_container_t *)malloc(sizeof(run_container_t))) != NULL) {
7262 size_t len;
7263 int32_t off;
7264
7265 memcpy(&ptr->n_runs, buf, off = 4);
7266 memcpy(&ptr->capacity, &buf[off], 4);
7267 off += 4;
7268
7269 len = sizeof(rle16_t) * ptr->n_runs;
7270
7271 if (len != buf_len) {
7272 free(ptr);
7273 return (NULL);
7274 }
7275
7276 if ((ptr->runs = (rle16_t *)malloc(len)) == NULL) {
7277 free(ptr);
7278 return (NULL);
7279 }
7280
7281 memcpy(ptr->runs, &buf[off], len);
7282
7283 /* Check if returned values are monotonically increasing */
7284 for (int32_t i = 0, j = 0; i < ptr->n_runs; i++) {
7285 if (ptr->runs[i].value < j) {
7286 free(ptr->runs);
7287 free(ptr);
7288 return (NULL);
7289 } else
7290 j = ptr->runs[i].value;
7291 }
7292 }
7293
7294 return (ptr);
7295 }
7296
7297 bool run_container_iterate(const run_container_t *cont, uint32_t base,
7298 roaring_iterator iterator, void *ptr) {
7299 for (int i = 0; i < cont->n_runs; ++i) {
7300 uint32_t run_start = base + cont->runs[i].value;
7301 uint16_t le = cont->runs[i].length;
7302
7303 for (int j = 0; j <= le; ++j)
7304 if (!iterator(run_start + j, ptr)) return false;
7305 }
7306 return true;
7307 }
7308
7309 bool run_container_iterate64(const run_container_t *cont, uint32_t base,
7310 roaring_iterator64 iterator, uint64_t high_bits,
7311 void *ptr) {
7312 for (int i = 0; i < cont->n_runs; ++i) {
7313 uint32_t run_start = base + cont->runs[i].value;
7314 uint16_t le = cont->runs[i].length;
7315
7316 for (int j = 0; j <= le; ++j)
7317 if (!iterator(high_bits | (uint64_t)(run_start + j), ptr))
7318 return false;
7319 }
7320 return true;
7321 }
7322
7323 bool run_container_is_subset(const run_container_t *container1,
7324 const run_container_t *container2) {
7325 int i1 = 0, i2 = 0;
7326 while (i1 < container1->n_runs && i2 < container2->n_runs) {
7327 int start1 = container1->runs[i1].value;
7328 int stop1 = start1 + container1->runs[i1].length;
7329 int start2 = container2->runs[i2].value;
7330 int stop2 = start2 + container2->runs[i2].length;
7331 if (start1 < start2) {
7332 return false;
7333 } else { // start1 >= start2
7334 if (stop1 < stop2) {
7335 i1++;
7336 } else if (stop1 == stop2) {
7337 i1++;
7338 i2++;
7339 } else { // stop1 > stop2
7340 i2++;
7341 }
7342 }
7343 }
7344 if (i1 == container1->n_runs) {
7345 return true;
7346 } else {
7347 return false;
7348 }
7349 }
7350
7351 // TODO: write smart_append_exclusive version to match the overloaded 1 param
7352 // Java version (or is it even used?)
7353
7354 // follows the Java implementation closely
7355 // length is the rle-value. Ie, run [10,12) uses a length value 1.
7356 void run_container_smart_append_exclusive(run_container_t *src,
7357 const uint16_t start,
7358 const uint16_t length) {
7359 int old_end;
7360 rle16_t *last_run = src->n_runs ? src->runs + (src->n_runs - 1) : NULL;
7361 rle16_t *appended_last_run = src->runs + src->n_runs;
7362
7363 if (!src->n_runs ||
7364 (start > (old_end = last_run->value + last_run->length + 1))) {
7365 *appended_last_run = (rle16_t){.value = start, .length = length};
7366 src->n_runs++;
7367 return;
7368 }
7369 if (old_end == start) {
7370 // we merge
7371 last_run->length += (length + 1);
7372 return;
7373 }
7374 int new_end = start + length + 1;
7375
7376 if (start == last_run->value) {
7377 // wipe out previous
7378 if (new_end < old_end) {
7379 *last_run = (rle16_t){.value = (uint16_t)new_end,
7380 .length = (uint16_t)(old_end - new_end - 1)};
7381 return;
7382 } else if (new_end > old_end) {
7383 *last_run = (rle16_t){.value = (uint16_t)old_end,
7384 .length = (uint16_t)(new_end - old_end - 1)};
7385 return;
7386 } else {
7387 src->n_runs--;
7388 return;
7389 }
7390 }
7391 last_run->length = start - last_run->value - 1;
7392 if (new_end < old_end) {
7393 *appended_last_run =
7394 (rle16_t){.value = (uint16_t)new_end,
7395 .length = (uint16_t)(old_end - new_end - 1)};
7396 src->n_runs++;
7397 } else if (new_end > old_end) {
7398 *appended_last_run =
7399 (rle16_t){.value = (uint16_t)old_end,
7400 .length = (uint16_t)(new_end - old_end - 1)};
7401 src->n_runs++;
7402 }
7403 }
7404
7405 bool run_container_select(const run_container_t *container,
7406 uint32_t *start_rank, uint32_t rank,
7407 uint32_t *element) {
7408 for (int i = 0; i < container->n_runs; i++) {
7409 uint16_t length = container->runs[i].length;
7410 if (rank <= *start_rank + length) {
7411 uint16_t value = container->runs[i].value;
7412 *element = value + rank - (*start_rank);
7413 return true;
7414 } else
7415 *start_rank += length + 1;
7416 }
7417 return false;
7418 }
7419
7420 int run_container_rank(const run_container_t *container, uint16_t x) {
7421 int sum = 0;
7422 uint32_t x32 = x;
7423 for (int i = 0; i < container->n_runs; i++) {
7424 uint32_t startpoint = container->runs[i].value;
7425 uint32_t length = container->runs[i].length;
7426 uint32_t endpoint = length + startpoint;
7427 if (x <= endpoint) {
7428 if (x < startpoint) break;
7429 return sum + (x32 - startpoint) + 1;
7430 } else {
7431 sum += length + 1;
7432 }
7433 }
7434 return sum;
7435 }
7436 /* end file src/containers/run.c */
7437 /* begin file src/roaring.c */
7438 #include <assert.h>
7439 #include <stdarg.h>
7440 #include <stdint.h>
7441 #include <stdio.h>
7442 #include <string.h>
7443 #include <inttypes.h>
7444
7445 static inline bool is_cow(const roaring_bitmap_t *r) {
7446 return r->high_low_container.flags & ROARING_FLAG_COW;
7447 }
7448 static inline bool is_frozen(const roaring_bitmap_t *r) {
7449 return r->high_low_container.flags & ROARING_FLAG_FROZEN;
7450 }
7451
7452 // this is like roaring_bitmap_add, but it populates pointer arguments in such a
7453 // way
7454 // that we can recover the container touched, which, in turn can be used to
7455 // accelerate some functions (when you repeatedly need to add to the same
7456 // container)
7457 static inline void *containerptr_roaring_bitmap_add(roaring_bitmap_t *r,
7458 uint32_t val,
7459 uint8_t *typecode,
7460 int *index) {
7461 uint16_t hb = val >> 16;
7462 const int i = ra_get_index(&r->high_low_container, hb);
7463 if (i >= 0) {
7464 ra_unshare_container_at_index(&r->high_low_container, i);
7465 void *container =
7466 ra_get_container_at_index(&r->high_low_container, i, typecode);
7467 uint8_t newtypecode = *typecode;
7468 void *container2 =
7469 container_add(container, val & 0xFFFF, *typecode, &newtypecode);
7470 *index = i;
7471 if (container2 != container) {
7472 container_free(container, *typecode);
7473 ra_set_container_at_index(&r->high_low_container, i, container2,
7474 newtypecode);
7475 *typecode = newtypecode;
7476 return container2;
7477 } else {
7478 return container;
7479 }
7480 } else {
7481 array_container_t *newac = array_container_create();
7482 void *container = container_add(newac, val & 0xFFFF,
7483 ARRAY_CONTAINER_TYPE_CODE, typecode);
7484 // we could just assume that it stays an array container
7485 ra_insert_new_key_value_at(&r->high_low_container, -i - 1, hb,
7486 container, *typecode);
7487 *index = -i - 1;
7488 return container;
7489 }
7490 }
7491
7492 roaring_bitmap_t *roaring_bitmap_create(void) {
7493 roaring_bitmap_t *ans =
7494 (roaring_bitmap_t *)malloc(sizeof(roaring_bitmap_t));
7495 if (!ans) {
7496 return NULL;
7497 }
7498 ra_init(&ans->high_low_container);
7499 return ans;
7500 }
7501
7502 roaring_bitmap_t *roaring_bitmap_create_with_capacity(uint32_t cap) {
7503 roaring_bitmap_t *ans =
7504 (roaring_bitmap_t *)malloc(sizeof(roaring_bitmap_t));
7505 if (!ans) {
7506 return NULL;
7507 }
7508 bool is_ok = ra_init_with_capacity(&ans->high_low_container, cap);
7509 if (!is_ok) {
7510 free(ans);
7511 return NULL;
7512 }
7513 return ans;
7514 }
7515
7516 void roaring_bitmap_add_many(roaring_bitmap_t *r, size_t n_args,
7517 const uint32_t *vals) {
7518 void *container = NULL; // hold value of last container touched
7519 uint8_t typecode = 0; // typecode of last container touched
7520 uint32_t prev = 0; // previous valued inserted
7521 size_t i = 0; // index of value
7522 int containerindex = 0;
7523 if (n_args == 0) return;
7524 uint32_t val;
7525 memcpy(&val, vals + i, sizeof(val));
7526 container =
7527 containerptr_roaring_bitmap_add(r, val, &typecode, &containerindex);
7528 prev = val;
7529 i++;
7530 for (; i < n_args; i++) {
7531 memcpy(&val, vals + i, sizeof(val));
7532 if (((prev ^ val) >> 16) ==
7533 0) { // no need to seek the container, it is at hand
7534 // because we already have the container at hand, we can do the
7535 // insertion
7536 // automatically, bypassing the roaring_bitmap_add call
7537 uint8_t newtypecode = typecode;
7538 void *container2 =
7539 container_add(container, val & 0xFFFF, typecode, &newtypecode);
7540 if (container2 != container) { // rare instance when we need to
7541 // change the container type
7542 container_free(container, typecode);
7543 ra_set_container_at_index(&r->high_low_container,
7544 containerindex, container2,
7545 newtypecode);
7546 typecode = newtypecode;
7547 container = container2;
7548 }
7549 } else {
7550 container = containerptr_roaring_bitmap_add(r, val, &typecode,
7551 &containerindex);
7552 }
7553 prev = val;
7554 }
7555 }
7556
7557 roaring_bitmap_t *roaring_bitmap_of_ptr(size_t n_args, const uint32_t *vals) {
7558 roaring_bitmap_t *answer = roaring_bitmap_create();
7559 roaring_bitmap_add_many(answer, n_args, vals);
7560 return answer;
7561 }
7562
7563 roaring_bitmap_t *roaring_bitmap_of(size_t n_args, ...) {
7564 // todo: could be greatly optimized but we do not expect this call to ever
7565 // include long lists
7566 roaring_bitmap_t *answer = roaring_bitmap_create();
7567 va_list ap;
7568 va_start(ap, n_args);
7569 for (size_t i = 1; i <= n_args; i++) {
7570 uint32_t val = va_arg(ap, uint32_t);
7571 roaring_bitmap_add(answer, val);
7572 }
7573 va_end(ap);
7574 return answer;
7575 }
7576
7577 static inline uint32_t minimum_uint32(uint32_t a, uint32_t b) {
7578 return (a < b) ? a : b;
7579 }
7580
7581 static inline uint64_t minimum_uint64(uint64_t a, uint64_t b) {
7582 return (a < b) ? a : b;
7583 }
7584
7585 roaring_bitmap_t *roaring_bitmap_from_range(uint64_t min, uint64_t max,
7586 uint32_t step) {
7587 if(max >= UINT64_C(0x100000000)) {
7588 max = UINT64_C(0x100000000);
7589 }
7590 if (step == 0) return NULL;
7591 if (max <= min) return NULL;
7592 roaring_bitmap_t *answer = roaring_bitmap_create();
7593 if (step >= (1 << 16)) {
7594 for (uint32_t value = (uint32_t)min; value < max; value += step) {
7595 roaring_bitmap_add(answer, value);
7596 }
7597 return answer;
7598 }
7599 uint64_t min_tmp = min;
7600 do {
7601 uint32_t key = (uint32_t)min_tmp >> 16;
7602 uint32_t container_min = min_tmp & 0xFFFF;
7603 uint32_t container_max = (uint32_t)minimum_uint64(max - (key << 16), 1 << 16);
7604 uint8_t type;
7605 void *container = container_from_range(&type, container_min,
7606 container_max, (uint16_t)step);
7607 ra_append(&answer->high_low_container, key, container, type);
7608 uint32_t gap = container_max - container_min + step - 1;
7609 min_tmp += gap - (gap % step);
7610 } while (min_tmp < max);
7611 // cardinality of bitmap will be ((uint64_t) max - min + step - 1 ) / step
7612 return answer;
7613 }
7614
7615 void roaring_bitmap_add_range_closed(roaring_bitmap_t *ra, uint32_t min, uint32_t max) {
7616 if (min > max) {
7617 return;
7618 }
7619
7620 uint32_t min_key = min >> 16;
7621 uint32_t max_key = max >> 16;
7622
7623 int32_t num_required_containers = max_key - min_key + 1;
7624 int32_t suffix_length = count_greater(ra->high_low_container.keys,
7625 ra->high_low_container.size,
7626 max_key);
7627 int32_t prefix_length = count_less(ra->high_low_container.keys,
7628 ra->high_low_container.size - suffix_length,
7629 min_key);
7630 int32_t common_length = ra->high_low_container.size - prefix_length - suffix_length;
7631
7632 if (num_required_containers > common_length) {
7633 ra_shift_tail(&ra->high_low_container, suffix_length,
7634 num_required_containers - common_length);
7635 }
7636
7637 int32_t src = prefix_length + common_length - 1;
7638 int32_t dst = ra->high_low_container.size - suffix_length - 1;
7639 for (uint32_t key = max_key; key != min_key-1; key--) { // beware of min_key==0
7640 uint32_t container_min = (min_key == key) ? (min & 0xffff) : 0;
7641 uint32_t container_max = (max_key == key) ? (max & 0xffff) : 0xffff;
7642 void* new_container;
7643 uint8_t new_type;
7644
7645 if (src >= 0 && ra->high_low_container.keys[src] == key) {
7646 ra_unshare_container_at_index(&ra->high_low_container, src);
7647 new_container = container_add_range(ra->high_low_container.containers[src],
7648 ra->high_low_container.typecodes[src],
7649 container_min, container_max, &new_type);
7650 if (new_container != ra->high_low_container.containers[src]) {
7651 container_free(ra->high_low_container.containers[src],
7652 ra->high_low_container.typecodes[src]);
7653 }
7654 src--;
7655 } else {
7656 new_container = container_from_range(&new_type, container_min,
7657 container_max+1, 1);
7658 }
7659 ra_replace_key_and_container_at_index(&ra->high_low_container, dst,
7660 key, new_container, new_type);
7661 dst--;
7662 }
7663 }
7664
7665 void roaring_bitmap_remove_range_closed(roaring_bitmap_t *ra, uint32_t min, uint32_t max) {
7666 if (min > max) {
7667 return;
7668 }
7669
7670 uint32_t min_key = min >> 16;
7671 uint32_t max_key = max >> 16;
7672
7673 int32_t src = count_less(ra->high_low_container.keys, ra->high_low_container.size, min_key);
7674 int32_t dst = src;
7675 while (src < ra->high_low_container.size && ra->high_low_container.keys[src] <= max_key) {
7676 uint32_t container_min = (min_key == ra->high_low_container.keys[src]) ? (min & 0xffff) : 0;
7677 uint32_t container_max = (max_key == ra->high_low_container.keys[src]) ? (max & 0xffff) : 0xffff;
7678 ra_unshare_container_at_index(&ra->high_low_container, src);
7679 void *new_container;
7680 uint8_t new_type;
7681 new_container = container_remove_range(ra->high_low_container.containers[src],
7682 ra->high_low_container.typecodes[src],
7683 container_min, container_max,
7684 &new_type);
7685 if (new_container != ra->high_low_container.containers[src]) {
7686 container_free(ra->high_low_container.containers[src],
7687 ra->high_low_container.typecodes[src]);
7688 }
7689 if (new_container) {
7690 ra_replace_key_and_container_at_index(&ra->high_low_container, dst,
7691 ra->high_low_container.keys[src],
7692 new_container, new_type);
7693 dst++;
7694 }
7695 src++;
7696 }
7697 if (src > dst) {
7698 ra_shift_tail(&ra->high_low_container, ra->high_low_container.size - src, dst - src);
7699 }
7700 }
7701
7702 void roaring_bitmap_printf(const roaring_bitmap_t *ra) {
7703 printf("{");
7704 for (int i = 0; i < ra->high_low_container.size; ++i) {
7705 container_printf_as_uint32_array(
7706 ra->high_low_container.containers[i],
7707 ra->high_low_container.typecodes[i],
7708 ((uint32_t)ra->high_low_container.keys[i]) << 16);
7709 if (i + 1 < ra->high_low_container.size) printf(",");
7710 }
7711 printf("}");
7712 }
7713
7714 void roaring_bitmap_printf_describe(const roaring_bitmap_t *ra) {
7715 printf("{");
7716 for (int i = 0; i < ra->high_low_container.size; ++i) {
7717 printf("%d: %s (%d)", ra->high_low_container.keys[i],
7718 get_full_container_name(ra->high_low_container.containers[i],
7719 ra->high_low_container.typecodes[i]),
7720 container_get_cardinality(ra->high_low_container.containers[i],
7721 ra->high_low_container.typecodes[i]));
7722 if (ra->high_low_container.typecodes[i] == SHARED_CONTAINER_TYPE_CODE) {
7723 printf(
7724 "(shared count = %" PRIu32 " )",
7725 ((shared_container_t *)(ra->high_low_container.containers[i]))
7726 ->counter);
7727 }
7728
7729 if (i + 1 < ra->high_low_container.size) printf(", ");
7730 }
7731 printf("}");
7732 }
7733
7734 typedef struct min_max_sum_s {
7735 uint32_t min;
7736 uint32_t max;
7737 uint64_t sum;
7738 } min_max_sum_t;
7739
7740 static bool min_max_sum_fnc(uint32_t value, void *param) {
7741 min_max_sum_t *mms = (min_max_sum_t *)param;
7742 if (value > mms->max) mms->max = value;
7743 if (value < mms->min) mms->min = value;
7744 mms->sum += value;
7745 return true; // we always process all data points
7746 }
7747
7748 /**
7749 * (For advanced users.)
7750 * Collect statistics about the bitmap
7751 */
7752 void roaring_bitmap_statistics(const roaring_bitmap_t *ra,
7753 roaring_statistics_t *stat) {
7754 memset(stat, 0, sizeof(*stat));
7755 stat->n_containers = ra->high_low_container.size;
7756 stat->cardinality = roaring_bitmap_get_cardinality(ra);
7757 min_max_sum_t mms;
7758 mms.min = UINT32_C(0xFFFFFFFF);
7759 mms.max = UINT32_C(0);
7760 mms.sum = 0;
7761 roaring_iterate(ra, &min_max_sum_fnc, &mms);
7762 stat->min_value = mms.min;
7763 stat->max_value = mms.max;
7764 stat->sum_value = mms.sum;
7765
7766 for (int i = 0; i < ra->high_low_container.size; ++i) {
7767 uint8_t truetype =
7768 get_container_type(ra->high_low_container.containers[i],
7769 ra->high_low_container.typecodes[i]);
7770 uint32_t card =
7771 container_get_cardinality(ra->high_low_container.containers[i],
7772 ra->high_low_container.typecodes[i]);
7773 uint32_t sbytes =
7774 container_size_in_bytes(ra->high_low_container.containers[i],
7775 ra->high_low_container.typecodes[i]);
7776 switch (truetype) {
7777 case BITSET_CONTAINER_TYPE_CODE:
7778 stat->n_bitset_containers++;
7779 stat->n_values_bitset_containers += card;
7780 stat->n_bytes_bitset_containers += sbytes;
7781 break;
7782 case ARRAY_CONTAINER_TYPE_CODE:
7783 stat->n_array_containers++;
7784 stat->n_values_array_containers += card;
7785 stat->n_bytes_array_containers += sbytes;
7786 break;
7787 case RUN_CONTAINER_TYPE_CODE:
7788 stat->n_run_containers++;
7789 stat->n_values_run_containers += card;
7790 stat->n_bytes_run_containers += sbytes;
7791 break;
7792 default:
7793 assert(false);
7794 __builtin_unreachable();
7795 }
7796 }
7797 }
7798
7799 roaring_bitmap_t *roaring_bitmap_copy(const roaring_bitmap_t *r) {
7800 roaring_bitmap_t *ans =
7801 (roaring_bitmap_t *)malloc(sizeof(roaring_bitmap_t));
7802 if (!ans) {
7803 return NULL;
7804 }
7805 bool is_ok = ra_copy(&r->high_low_container, &ans->high_low_container,
7806 is_cow(r));
7807 if (!is_ok) {
7808 free(ans);
7809 return NULL;
7810 }
7811 roaring_bitmap_set_copy_on_write(ans, is_cow(r));
7812 return ans;
7813 }
7814
7815 bool roaring_bitmap_overwrite(roaring_bitmap_t *dest,
7816 const roaring_bitmap_t *src) {
7817 return ra_overwrite(&src->high_low_container, &dest->high_low_container,
7818 is_cow(src));
7819 }
7820
7821 void roaring_bitmap_free(const roaring_bitmap_t *r) {
7822 if (!is_frozen(r)) {
7823 ra_clear((roaring_array_t*)&r->high_low_container);
7824 }
7825 free((roaring_bitmap_t*)r);
7826 }
7827
7828 void roaring_bitmap_clear(roaring_bitmap_t *r) {
7829 ra_reset(&r->high_low_container);
7830 }
7831
7832 void roaring_bitmap_add(roaring_bitmap_t *r, uint32_t val) {
7833 const uint16_t hb = val >> 16;
7834 const int i = ra_get_index(&r->high_low_container, hb);
7835 uint8_t typecode;
7836 if (i >= 0) {
7837 ra_unshare_container_at_index(&r->high_low_container, i);
7838 void *container =
7839 ra_get_container_at_index(&r->high_low_container, i, &typecode);
7840 uint8_t newtypecode = typecode;
7841 void *container2 =
7842 container_add(container, val & 0xFFFF, typecode, &newtypecode);
7843 if (container2 != container) {
7844 container_free(container, typecode);
7845 ra_set_container_at_index(&r->high_low_container, i, container2,
7846 newtypecode);
7847 }
7848 } else {
7849 array_container_t *newac = array_container_create();
7850 void *container = container_add(newac, val & 0xFFFF,
7851 ARRAY_CONTAINER_TYPE_CODE, &typecode);
7852 // we could just assume that it stays an array container
7853 ra_insert_new_key_value_at(&r->high_low_container, -i - 1, hb,
7854 container, typecode);
7855 }
7856 }
7857
7858 bool roaring_bitmap_add_checked(roaring_bitmap_t *r, uint32_t val) {
7859 const uint16_t hb = val >> 16;
7860 const int i = ra_get_index(&r->high_low_container, hb);
7861 uint8_t typecode;
7862 bool result = false;
7863 if (i >= 0) {
7864 ra_unshare_container_at_index(&r->high_low_container, i);
7865 void *container =
7866 ra_get_container_at_index(&r->high_low_container, i, &typecode);
7867
7868 const int oldCardinality =
7869 container_get_cardinality(container, typecode);
7870
7871 uint8_t newtypecode = typecode;
7872 void *container2 =
7873 container_add(container, val & 0xFFFF, typecode, &newtypecode);
7874 if (container2 != container) {
7875 container_free(container, typecode);
7876 ra_set_container_at_index(&r->high_low_container, i, container2,
7877 newtypecode);
7878 result = true;
7879 } else {
7880 const int newCardinality =
7881 container_get_cardinality(container, newtypecode);
7882
7883 result = oldCardinality != newCardinality;
7884 }
7885 } else {
7886 array_container_t *newac = array_container_create();
7887 void *container = container_add(newac, val & 0xFFFF,
7888 ARRAY_CONTAINER_TYPE_CODE, &typecode);
7889 // we could just assume that it stays an array container
7890 ra_insert_new_key_value_at(&r->high_low_container, -i - 1, hb,
7891 container, typecode);
7892 result = true;
7893 }
7894
7895 return result;
7896 }
7897
7898 void roaring_bitmap_remove(roaring_bitmap_t *r, uint32_t val) {
7899 const uint16_t hb = val >> 16;
7900 const int i = ra_get_index(&r->high_low_container, hb);
7901 uint8_t typecode;
7902 if (i >= 0) {
7903 ra_unshare_container_at_index(&r->high_low_container, i);
7904 void *container =
7905 ra_get_container_at_index(&r->high_low_container, i, &typecode);
7906 uint8_t newtypecode = typecode;
7907 void *container2 =
7908 container_remove(container, val & 0xFFFF, typecode, &newtypecode);
7909 if (container2 != container) {
7910 container_free(container, typecode);
7911 ra_set_container_at_index(&r->high_low_container, i, container2,
7912 newtypecode);
7913 }
7914 if (container_get_cardinality(container2, newtypecode) != 0) {
7915 ra_set_container_at_index(&r->high_low_container, i, container2,
7916 newtypecode);
7917 } else {
7918 ra_remove_at_index_and_free(&r->high_low_container, i);
7919 }
7920 }
7921 }
7922
7923 bool roaring_bitmap_remove_checked(roaring_bitmap_t *r, uint32_t val) {
7924 const uint16_t hb = val >> 16;
7925 const int i = ra_get_index(&r->high_low_container, hb);
7926 uint8_t typecode;
7927 bool result = false;
7928 if (i >= 0) {
7929 ra_unshare_container_at_index(&r->high_low_container, i);
7930 void *container =
7931 ra_get_container_at_index(&r->high_low_container, i, &typecode);
7932
7933 const int oldCardinality =
7934 container_get_cardinality(container, typecode);
7935
7936 uint8_t newtypecode = typecode;
7937 void *container2 =
7938 container_remove(container, val & 0xFFFF, typecode, &newtypecode);
7939 if (container2 != container) {
7940 container_free(container, typecode);
7941 ra_set_container_at_index(&r->high_low_container, i, container2,
7942 newtypecode);
7943 }
7944
7945 const int newCardinality =
7946 container_get_cardinality(container2, newtypecode);
7947
7948 if (newCardinality != 0) {
7949 ra_set_container_at_index(&r->high_low_container, i, container2,
7950 newtypecode);
7951 } else {
7952 ra_remove_at_index_and_free(&r->high_low_container, i);
7953 }
7954
7955 result = oldCardinality != newCardinality;
7956 }
7957 return result;
7958 }
7959
7960 void roaring_bitmap_remove_many(roaring_bitmap_t *r, size_t n_args,
7961 const uint32_t *vals) {
7962 if (n_args == 0 || r->high_low_container.size == 0) {
7963 return;
7964 }
7965 int32_t pos = -1; // position of the container used in the previous iteration
7966 for (size_t i = 0; i < n_args; i++) {
7967 uint16_t key = (uint16_t)(vals[i] >> 16);
7968 if (pos < 0 || key != r->high_low_container.keys[pos]) {
7969 pos = ra_get_index(&r->high_low_container, key);
7970 }
7971 if (pos >= 0) {
7972 uint8_t new_typecode;
7973 void *new_container;
7974 new_container = container_remove(r->high_low_container.containers[pos],
7975 vals[i] & 0xffff,
7976 r->high_low_container.typecodes[pos],
7977 &new_typecode);
7978 if (new_container != r->high_low_container.containers[pos]) {
7979 container_free(r->high_low_container.containers[pos],
7980 r->high_low_container.typecodes[pos]);
7981 ra_replace_key_and_container_at_index(&r->high_low_container,
7982 pos, key, new_container,
7983 new_typecode);
7984 }
7985 if (!container_nonzero_cardinality(new_container, new_typecode)) {
7986 container_free(new_container, new_typecode);
7987 ra_remove_at_index(&r->high_low_container, pos);
7988 pos = -1;
7989 }
7990 }
7991 }
7992 }
7993
7994 // there should be some SIMD optimizations possible here
7995 roaring_bitmap_t *roaring_bitmap_and(const roaring_bitmap_t *x1,
7996 const roaring_bitmap_t *x2) {
7997 uint8_t container_result_type = 0;
7998 const int length1 = x1->high_low_container.size,
7999 length2 = x2->high_low_container.size;
8000 uint32_t neededcap = length1 > length2 ? length2 : length1;
8001 roaring_bitmap_t *answer = roaring_bitmap_create_with_capacity(neededcap);
8002 roaring_bitmap_set_copy_on_write(answer, is_cow(x1) && is_cow(x2));
8003
8004 int pos1 = 0, pos2 = 0;
8005
8006 while (pos1 < length1 && pos2 < length2) {
8007 const uint16_t s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
8008 const uint16_t s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
8009
8010 if (s1 == s2) {
8011 uint8_t container_type_1, container_type_2;
8012 void *c1 = ra_get_container_at_index(&x1->high_low_container, pos1,
8013 &container_type_1);
8014 void *c2 = ra_get_container_at_index(&x2->high_low_container, pos2,
8015 &container_type_2);
8016 void *c = container_and(c1, container_type_1, c2, container_type_2,
8017 &container_result_type);
8018 if (container_nonzero_cardinality(c, container_result_type)) {
8019 ra_append(&answer->high_low_container, s1, c,
8020 container_result_type);
8021 } else {
8022 container_free(
8023 c, container_result_type); // otherwise:memory leak!
8024 }
8025 ++pos1;
8026 ++pos2;
8027 } else if (s1 < s2) { // s1 < s2
8028 pos1 = ra_advance_until(&x1->high_low_container, s2, pos1);
8029 } else { // s1 > s2
8030 pos2 = ra_advance_until(&x2->high_low_container, s1, pos2);
8031 }
8032 }
8033 return answer;
8034 }
8035
8036 /**
8037 * Compute the union of 'number' bitmaps.
8038 */
8039 roaring_bitmap_t *roaring_bitmap_or_many(size_t number,
8040 const roaring_bitmap_t **x) {
8041 if (number == 0) {
8042 return roaring_bitmap_create();
8043 }
8044 if (number == 1) {
8045 return roaring_bitmap_copy(x[0]);
8046 }
8047 roaring_bitmap_t *answer =
8048 roaring_bitmap_lazy_or(x[0], x[1], LAZY_OR_BITSET_CONVERSION);
8049 for (size_t i = 2; i < number; i++) {
8050 roaring_bitmap_lazy_or_inplace(answer, x[i], LAZY_OR_BITSET_CONVERSION);
8051 }
8052 roaring_bitmap_repair_after_lazy(answer);
8053 return answer;
8054 }
8055
8056 /**
8057 * Compute the xor of 'number' bitmaps.
8058 */
8059 roaring_bitmap_t *roaring_bitmap_xor_many(size_t number,
8060 const roaring_bitmap_t **x) {
8061 if (number == 0) {
8062 return roaring_bitmap_create();
8063 }
8064 if (number == 1) {
8065 return roaring_bitmap_copy(x[0]);
8066 }
8067 roaring_bitmap_t *answer = roaring_bitmap_lazy_xor(x[0], x[1]);
8068 for (size_t i = 2; i < number; i++) {
8069 roaring_bitmap_lazy_xor_inplace(answer, x[i]);
8070 }
8071 roaring_bitmap_repair_after_lazy(answer);
8072 return answer;
8073 }
8074
8075 // inplace and (modifies its first argument).
8076 void roaring_bitmap_and_inplace(roaring_bitmap_t *x1,
8077 const roaring_bitmap_t *x2) {
8078 if (x1 == x2) return;
8079 int pos1 = 0, pos2 = 0, intersection_size = 0;
8080 const int length1 = ra_get_size(&x1->high_low_container);
8081 const int length2 = ra_get_size(&x2->high_low_container);
8082
8083 // any skipped-over or newly emptied containers in x1
8084 // have to be freed.
8085 while (pos1 < length1 && pos2 < length2) {
8086 const uint16_t s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
8087 const uint16_t s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
8088
8089 if (s1 == s2) {
8090 uint8_t typecode1, typecode2, typecode_result;
8091 void *c1 = ra_get_container_at_index(&x1->high_low_container, pos1,
8092 &typecode1);
8093 c1 = get_writable_copy_if_shared(c1, &typecode1);
8094 void *c2 = ra_get_container_at_index(&x2->high_low_container, pos2,
8095 &typecode2);
8096 void *c =
8097 container_iand(c1, typecode1, c2, typecode2, &typecode_result);
8098 if (c != c1) { // in this instance a new container was created, and
8099 // we need to free the old one
8100 container_free(c1, typecode1);
8101 }
8102 if (container_nonzero_cardinality(c, typecode_result)) {
8103 ra_replace_key_and_container_at_index(&x1->high_low_container,
8104 intersection_size, s1, c,
8105 typecode_result);
8106 intersection_size++;
8107 } else {
8108 container_free(c, typecode_result);
8109 }
8110 ++pos1;
8111 ++pos2;
8112 } else if (s1 < s2) {
8113 pos1 = ra_advance_until_freeing(&x1->high_low_container, s2, pos1);
8114 } else { // s1 > s2
8115 pos2 = ra_advance_until(&x2->high_low_container, s1, pos2);
8116 }
8117 }
8118
8119 // if we ended early because x2 ran out, then all remaining in x1 should be
8120 // freed
8121 while (pos1 < length1) {
8122 container_free(x1->high_low_container.containers[pos1],
8123 x1->high_low_container.typecodes[pos1]);
8124 ++pos1;
8125 }
8126
8127 // all containers after this have either been copied or freed
8128 ra_downsize(&x1->high_low_container, intersection_size);
8129 }
8130
8131 roaring_bitmap_t *roaring_bitmap_or(const roaring_bitmap_t *x1,
8132 const roaring_bitmap_t *x2) {
8133 uint8_t container_result_type = 0;
8134 const int length1 = x1->high_low_container.size,
8135 length2 = x2->high_low_container.size;
8136 if (0 == length1) {
8137 return roaring_bitmap_copy(x2);
8138 }
8139 if (0 == length2) {
8140 return roaring_bitmap_copy(x1);
8141 }
8142 roaring_bitmap_t *answer =
8143 roaring_bitmap_create_with_capacity(length1 + length2);
8144 roaring_bitmap_set_copy_on_write(answer, is_cow(x1) && is_cow(x2));
8145 int pos1 = 0, pos2 = 0;
8146 uint8_t container_type_1, container_type_2;
8147 uint16_t s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
8148 uint16_t s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
8149 while (true) {
8150 if (s1 == s2) {
8151 void *c1 = ra_get_container_at_index(&x1->high_low_container, pos1,
8152 &container_type_1);
8153 void *c2 = ra_get_container_at_index(&x2->high_low_container, pos2,
8154 &container_type_2);
8155 void *c = container_or(c1, container_type_1, c2, container_type_2,
8156 &container_result_type);
8157 // since we assume that the initial containers are non-empty, the
8158 // result here
8159 // can only be non-empty
8160 ra_append(&answer->high_low_container, s1, c,
8161 container_result_type);
8162 ++pos1;
8163 ++pos2;
8164 if (pos1 == length1) break;
8165 if (pos2 == length2) break;
8166 s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
8167 s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
8168
8169 } else if (s1 < s2) { // s1 < s2
8170 void *c1 = ra_get_container_at_index(&x1->high_low_container, pos1,
8171 &container_type_1);
8172 // c1 = container_clone(c1, container_type_1);
8173 c1 =
8174 get_copy_of_container(c1, &container_type_1, is_cow(x1));
8175 if (is_cow(x1)) {
8176 ra_set_container_at_index(&x1->high_low_container, pos1, c1,
8177 container_type_1);
8178 }
8179 ra_append(&answer->high_low_container, s1, c1, container_type_1);
8180 pos1++;
8181 if (pos1 == length1) break;
8182 s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
8183
8184 } else { // s1 > s2
8185 void *c2 = ra_get_container_at_index(&x2->high_low_container, pos2,
8186 &container_type_2);
8187 // c2 = container_clone(c2, container_type_2);
8188 c2 =
8189 get_copy_of_container(c2, &container_type_2, is_cow(x2));
8190 if (is_cow(x2)) {
8191 ra_set_container_at_index(&x2->high_low_container, pos2, c2,
8192 container_type_2);
8193 }
8194 ra_append(&answer->high_low_container, s2, c2, container_type_2);
8195 pos2++;
8196 if (pos2 == length2) break;
8197 s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
8198 }
8199 }
8200 if (pos1 == length1) {
8201 ra_append_copy_range(&answer->high_low_container,
8202 &x2->high_low_container, pos2, length2,
8203 is_cow(x2));
8204 } else if (pos2 == length2) {
8205 ra_append_copy_range(&answer->high_low_container,
8206 &x1->high_low_container, pos1, length1,
8207 is_cow(x1));
8208 }
8209 return answer;
8210 }
8211
8212 // inplace or (modifies its first argument).
8213 void roaring_bitmap_or_inplace(roaring_bitmap_t *x1,
8214 const roaring_bitmap_t *x2) {
8215 uint8_t container_result_type = 0;
8216 int length1 = x1->high_low_container.size;
8217 const int length2 = x2->high_low_container.size;
8218
8219 if (0 == length2) return;
8220
8221 if (0 == length1) {
8222 roaring_bitmap_overwrite(x1, x2);
8223 return;
8224 }
8225 int pos1 = 0, pos2 = 0;
8226 uint8_t container_type_1, container_type_2;
8227 uint16_t s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
8228 uint16_t s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
8229 while (true) {
8230 if (s1 == s2) {
8231 void *c1 = ra_get_container_at_index(&x1->high_low_container, pos1,
8232 &container_type_1);
8233 if (!container_is_full(c1, container_type_1)) {
8234 c1 = get_writable_copy_if_shared(c1, &container_type_1);
8235
8236 void *c2 = ra_get_container_at_index(&x2->high_low_container,
8237 pos2, &container_type_2);
8238 void *c =
8239 container_ior(c1, container_type_1, c2, container_type_2,
8240 &container_result_type);
8241 if (c !=
8242 c1) { // in this instance a new container was created, and
8243 // we need to free the old one
8244 container_free(c1, container_type_1);
8245 }
8246
8247 ra_set_container_at_index(&x1->high_low_container, pos1, c,
8248 container_result_type);
8249 }
8250 ++pos1;
8251 ++pos2;
8252 if (pos1 == length1) break;
8253 if (pos2 == length2) break;
8254 s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
8255 s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
8256
8257 } else if (s1 < s2) { // s1 < s2
8258 pos1++;
8259 if (pos1 == length1) break;
8260 s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
8261
8262 } else { // s1 > s2
8263 void *c2 = ra_get_container_at_index(&x2->high_low_container, pos2,
8264 &container_type_2);
8265 c2 =
8266 get_copy_of_container(c2, &container_type_2, is_cow(x2));
8267 if (is_cow(x2)) {
8268 ra_set_container_at_index(&x2->high_low_container, pos2, c2,
8269 container_type_2);
8270 }
8271
8272 // void *c2_clone = container_clone(c2, container_type_2);
8273 ra_insert_new_key_value_at(&x1->high_low_container, pos1, s2, c2,
8274 container_type_2);
8275 pos1++;
8276 length1++;
8277 pos2++;
8278 if (pos2 == length2) break;
8279 s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
8280 }
8281 }
8282 if (pos1 == length1) {
8283 ra_append_copy_range(&x1->high_low_container, &x2->high_low_container,
8284 pos2, length2, is_cow(x2));
8285 }
8286 }
8287
8288 roaring_bitmap_t *roaring_bitmap_xor(const roaring_bitmap_t *x1,
8289 const roaring_bitmap_t *x2) {
8290 uint8_t container_result_type = 0;
8291 const int length1 = x1->high_low_container.size,
8292 length2 = x2->high_low_container.size;
8293 if (0 == length1) {
8294 return roaring_bitmap_copy(x2);
8295 }
8296 if (0 == length2) {
8297 return roaring_bitmap_copy(x1);
8298 }
8299 roaring_bitmap_t *answer =
8300 roaring_bitmap_create_with_capacity(length1 + length2);
8301 roaring_bitmap_set_copy_on_write(answer, is_cow(x1) && is_cow(x2));
8302 int pos1 = 0, pos2 = 0;
8303 uint8_t container_type_1, container_type_2;
8304 uint16_t s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
8305 uint16_t s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
8306 while (true) {
8307 if (s1 == s2) {
8308 void *c1 = ra_get_container_at_index(&x1->high_low_container, pos1,
8309 &container_type_1);
8310 void *c2 = ra_get_container_at_index(&x2->high_low_container, pos2,
8311 &container_type_2);
8312 void *c = container_xor(c1, container_type_1, c2, container_type_2,
8313 &container_result_type);
8314
8315 if (container_nonzero_cardinality(c, container_result_type)) {
8316 ra_append(&answer->high_low_container, s1, c,
8317 container_result_type);
8318 } else {
8319 container_free(c, container_result_type);
8320 }
8321 ++pos1;
8322 ++pos2;
8323 if (pos1 == length1) break;
8324 if (pos2 == length2) break;
8325 s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
8326 s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
8327
8328 } else if (s1 < s2) { // s1 < s2
8329 void *c1 = ra_get_container_at_index(&x1->high_low_container, pos1,
8330 &container_type_1);
8331 c1 =
8332 get_copy_of_container(c1, &container_type_1, is_cow(x1));
8333 if (is_cow(x1)) {
8334 ra_set_container_at_index(&x1->high_low_container, pos1, c1,
8335 container_type_1);
8336 }
8337 ra_append(&answer->high_low_container, s1, c1, container_type_1);
8338 pos1++;
8339 if (pos1 == length1) break;
8340 s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
8341
8342 } else { // s1 > s2
8343 void *c2 = ra_get_container_at_index(&x2->high_low_container, pos2,
8344 &container_type_2);
8345 c2 =
8346 get_copy_of_container(c2, &container_type_2, is_cow(x2));
8347 if (is_cow(x2)) {
8348 ra_set_container_at_index(&x2->high_low_container, pos2, c2,
8349 container_type_2);
8350 }
8351 ra_append(&answer->high_low_container, s2, c2, container_type_2);
8352 pos2++;
8353 if (pos2 == length2) break;
8354 s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
8355 }
8356 }
8357 if (pos1 == length1) {
8358 ra_append_copy_range(&answer->high_low_container,
8359 &x2->high_low_container, pos2, length2,
8360 is_cow(x2));
8361 } else if (pos2 == length2) {
8362 ra_append_copy_range(&answer->high_low_container,
8363 &x1->high_low_container, pos1, length1,
8364 is_cow(x1));
8365 }
8366 return answer;
8367 }
8368
8369 // inplace xor (modifies its first argument).
8370
8371 void roaring_bitmap_xor_inplace(roaring_bitmap_t *x1,
8372 const roaring_bitmap_t *x2) {
8373 assert(x1 != x2);
8374 uint8_t container_result_type = 0;
8375 int length1 = x1->high_low_container.size;
8376 const int length2 = x2->high_low_container.size;
8377
8378 if (0 == length2) return;
8379
8380 if (0 == length1) {
8381 roaring_bitmap_overwrite(x1, x2);
8382 return;
8383 }
8384
8385 // XOR can have new containers inserted from x2, but can also
8386 // lose containers when x1 and x2 are nonempty and identical.
8387
8388 int pos1 = 0, pos2 = 0;
8389 uint8_t container_type_1, container_type_2;
8390 uint16_t s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
8391 uint16_t s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
8392 while (true) {
8393 if (s1 == s2) {
8394 void *c1 = ra_get_container_at_index(&x1->high_low_container, pos1,
8395 &container_type_1);
8396 c1 = get_writable_copy_if_shared(c1, &container_type_1);
8397
8398 void *c2 = ra_get_container_at_index(&x2->high_low_container, pos2,
8399 &container_type_2);
8400 void *c = container_ixor(c1, container_type_1, c2, container_type_2,
8401 &container_result_type);
8402
8403 if (container_nonzero_cardinality(c, container_result_type)) {
8404 ra_set_container_at_index(&x1->high_low_container, pos1, c,
8405 container_result_type);
8406 ++pos1;
8407 } else {
8408 container_free(c, container_result_type);
8409 ra_remove_at_index(&x1->high_low_container, pos1);
8410 --length1;
8411 }
8412
8413 ++pos2;
8414 if (pos1 == length1) break;
8415 if (pos2 == length2) break;
8416 s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
8417 s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
8418
8419 } else if (s1 < s2) { // s1 < s2
8420 pos1++;
8421 if (pos1 == length1) break;
8422 s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
8423
8424 } else { // s1 > s2
8425 void *c2 = ra_get_container_at_index(&x2->high_low_container, pos2,
8426 &container_type_2);
8427 c2 =
8428 get_copy_of_container(c2, &container_type_2, is_cow(x2));
8429 if (is_cow(x2)) {
8430 ra_set_container_at_index(&x2->high_low_container, pos2, c2,
8431 container_type_2);
8432 }
8433
8434 ra_insert_new_key_value_at(&x1->high_low_container, pos1, s2, c2,
8435 container_type_2);
8436 pos1++;
8437 length1++;
8438 pos2++;
8439 if (pos2 == length2) break;
8440 s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
8441 }
8442 }
8443 if (pos1 == length1) {
8444 ra_append_copy_range(&x1->high_low_container, &x2->high_low_container,
8445 pos2, length2, is_cow(x2));
8446 }
8447 }
8448
8449 roaring_bitmap_t *roaring_bitmap_andnot(const roaring_bitmap_t *x1,
8450 const roaring_bitmap_t *x2) {
8451 uint8_t container_result_type = 0;
8452 const int length1 = x1->high_low_container.size,
8453 length2 = x2->high_low_container.size;
8454 if (0 == length1) {
8455 roaring_bitmap_t *empty_bitmap = roaring_bitmap_create();
8456 roaring_bitmap_set_copy_on_write(empty_bitmap, is_cow(x1) && is_cow(x2));
8457 return empty_bitmap;
8458 }
8459 if (0 == length2) {
8460 return roaring_bitmap_copy(x1);
8461 }
8462 roaring_bitmap_t *answer = roaring_bitmap_create_with_capacity(length1);
8463 roaring_bitmap_set_copy_on_write(answer, is_cow(x1) && is_cow(x2));
8464
8465 int pos1 = 0, pos2 = 0;
8466 uint8_t container_type_1, container_type_2;
8467 uint16_t s1 = 0;
8468 uint16_t s2 = 0;
8469 while (true) {
8470 s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
8471 s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
8472
8473 if (s1 == s2) {
8474 void *c1 = ra_get_container_at_index(&x1->high_low_container, pos1,
8475 &container_type_1);
8476 void *c2 = ra_get_container_at_index(&x2->high_low_container, pos2,
8477 &container_type_2);
8478 void *c =
8479 container_andnot(c1, container_type_1, c2, container_type_2,
8480 &container_result_type);
8481
8482 if (container_nonzero_cardinality(c, container_result_type)) {
8483 ra_append(&answer->high_low_container, s1, c,
8484 container_result_type);
8485 } else {
8486 container_free(c, container_result_type);
8487 }
8488 ++pos1;
8489 ++pos2;
8490 if (pos1 == length1) break;
8491 if (pos2 == length2) break;
8492 } else if (s1 < s2) { // s1 < s2
8493 const int next_pos1 =
8494 ra_advance_until(&x1->high_low_container, s2, pos1);
8495 ra_append_copy_range(&answer->high_low_container,
8496 &x1->high_low_container, pos1, next_pos1,
8497 is_cow(x1));
8498 // TODO : perhaps some of the copy_on_write should be based on
8499 // answer rather than x1 (more stringent?). Many similar cases
8500 pos1 = next_pos1;
8501 if (pos1 == length1) break;
8502 } else { // s1 > s2
8503 pos2 = ra_advance_until(&x2->high_low_container, s1, pos2);
8504 if (pos2 == length2) break;
8505 }
8506 }
8507 if (pos2 == length2) {
8508 ra_append_copy_range(&answer->high_low_container,
8509 &x1->high_low_container, pos1, length1,
8510 is_cow(x1));
8511 }
8512 return answer;
8513 }
8514
8515 // inplace andnot (modifies its first argument).
8516
8517 void roaring_bitmap_andnot_inplace(roaring_bitmap_t *x1,
8518 const roaring_bitmap_t *x2) {
8519 assert(x1 != x2);
8520
8521 uint8_t container_result_type = 0;
8522 int length1 = x1->high_low_container.size;
8523 const int length2 = x2->high_low_container.size;
8524 int intersection_size = 0;
8525
8526 if (0 == length2) return;
8527
8528 if (0 == length1) {
8529 roaring_bitmap_clear(x1);
8530 return;
8531 }
8532
8533 int pos1 = 0, pos2 = 0;
8534 uint8_t container_type_1, container_type_2;
8535 uint16_t s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
8536 uint16_t s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
8537 while (true) {
8538 if (s1 == s2) {
8539 void *c1 = ra_get_container_at_index(&x1->high_low_container, pos1,
8540 &container_type_1);
8541 c1 = get_writable_copy_if_shared(c1, &container_type_1);
8542
8543 void *c2 = ra_get_container_at_index(&x2->high_low_container, pos2,
8544 &container_type_2);
8545 void *c =
8546 container_iandnot(c1, container_type_1, c2, container_type_2,
8547 &container_result_type);
8548
8549 if (container_nonzero_cardinality(c, container_result_type)) {
8550 ra_replace_key_and_container_at_index(&x1->high_low_container,
8551 intersection_size++, s1,
8552 c, container_result_type);
8553 } else {
8554 container_free(c, container_result_type);
8555 }
8556
8557 ++pos1;
8558 ++pos2;
8559 if (pos1 == length1) break;
8560 if (pos2 == length2) break;
8561 s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
8562 s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
8563
8564 } else if (s1 < s2) { // s1 < s2
8565 if (pos1 != intersection_size) {
8566 void *c1 = ra_get_container_at_index(&x1->high_low_container,
8567 pos1, &container_type_1);
8568
8569 ra_replace_key_and_container_at_index(&x1->high_low_container,
8570 intersection_size, s1, c1,
8571 container_type_1);
8572 }
8573 intersection_size++;
8574 pos1++;
8575 if (pos1 == length1) break;
8576 s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
8577
8578 } else { // s1 > s2
8579 pos2 = ra_advance_until(&x2->high_low_container, s1, pos2);
8580 if (pos2 == length2) break;
8581 s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
8582 }
8583 }
8584
8585 if (pos1 < length1) {
8586 // all containers between intersection_size and
8587 // pos1 are junk. However, they have either been moved
8588 // (thus still referenced) or involved in an iandnot
8589 // that will clean up all containers that could not be reused.
8590 // Thus we should not free the junk containers between
8591 // intersection_size and pos1.
8592 if (pos1 > intersection_size) {
8593 // left slide of remaining items
8594 ra_copy_range(&x1->high_low_container, pos1, length1,
8595 intersection_size);
8596 }
8597 // else current placement is fine
8598 intersection_size += (length1 - pos1);
8599 }
8600 ra_downsize(&x1->high_low_container, intersection_size);
8601 }
8602
8603 uint64_t roaring_bitmap_get_cardinality(const roaring_bitmap_t *ra) {
8604 uint64_t card = 0;
8605 for (int i = 0; i < ra->high_low_container.size; ++i)
8606 card += container_get_cardinality(ra->high_low_container.containers[i],
8607 ra->high_low_container.typecodes[i]);
8608 return card;
8609 }
8610
8611 uint64_t roaring_bitmap_range_cardinality(const roaring_bitmap_t *ra,
8612 uint64_t range_start,
8613 uint64_t range_end) {
8614 if (range_end > UINT32_MAX) {
8615 range_end = UINT32_MAX + UINT64_C(1);
8616 }
8617 if (range_start >= range_end) {
8618 return 0;
8619 }
8620 range_end--; // make range_end inclusive
8621 // now we have: 0 <= range_start <= range_end <= UINT32_MAX
8622
8623 uint16_t minhb = range_start >> 16;
8624 uint16_t maxhb = range_end >> 16;
8625
8626 uint64_t card = 0;
8627
8628 int i = ra_get_index(&ra->high_low_container, minhb);
8629 if (i >= 0) {
8630 if (minhb == maxhb) {
8631 card += container_rank(ra->high_low_container.containers[i],
8632 ra->high_low_container.typecodes[i],
8633 range_end & 0xffff);
8634 } else {
8635 card += container_get_cardinality(ra->high_low_container.containers[i],
8636 ra->high_low_container.typecodes[i]);
8637 }
8638 if ((range_start & 0xffff) != 0) {
8639 card -= container_rank(ra->high_low_container.containers[i],
8640 ra->high_low_container.typecodes[i],
8641 (range_start & 0xffff) - 1);
8642 }
8643 i++;
8644 } else {
8645 i = -i - 1;
8646 }
8647
8648 for (; i < ra->high_low_container.size; i++) {
8649 uint16_t key = ra->high_low_container.keys[i];
8650 if (key < maxhb) {
8651 card += container_get_cardinality(ra->high_low_container.containers[i],
8652 ra->high_low_container.typecodes[i]);
8653 } else if (key == maxhb) {
8654 card += container_rank(ra->high_low_container.containers[i],
8655 ra->high_low_container.typecodes[i],
8656 range_end & 0xffff);
8657 break;
8658 } else {
8659 break;
8660 }
8661 }
8662
8663 return card;
8664 }
8665
8666
8667 bool roaring_bitmap_is_empty(const roaring_bitmap_t *ra) {
8668 return ra->high_low_container.size == 0;
8669 }
8670
8671 void roaring_bitmap_to_uint32_array(const roaring_bitmap_t *ra, uint32_t *ans) {
8672 ra_to_uint32_array(&ra->high_low_container, ans);
8673 }
8674
8675 bool roaring_bitmap_range_uint32_array(const roaring_bitmap_t *ra, size_t offset, size_t limit, uint32_t *ans) {
8676 return ra_range_uint32_array(&ra->high_low_container, offset, limit, ans);
8677 }
8678
8679 /** convert array and bitmap containers to run containers when it is more
8680 * efficient;
8681 * also convert from run containers when more space efficient. Returns
8682 * true if the result has at least one run container.
8683 */
8684 bool roaring_bitmap_run_optimize(roaring_bitmap_t *r) {
8685 bool answer = false;
8686 for (int i = 0; i < r->high_low_container.size; i++) {
8687 uint8_t typecode_original, typecode_after;
8688 ra_unshare_container_at_index(
8689 &r->high_low_container, i); // TODO: this introduces extra cloning!
8690 void *c = ra_get_container_at_index(&r->high_low_container, i,
8691 &typecode_original);
8692 void *c1 = convert_run_optimize(c, typecode_original, &typecode_after);
8693 if (typecode_after == RUN_CONTAINER_TYPE_CODE) answer = true;
8694 ra_set_container_at_index(&r->high_low_container, i, c1,
8695 typecode_after);
8696 }
8697 return answer;
8698 }
8699
8700 size_t roaring_bitmap_shrink_to_fit(roaring_bitmap_t *r) {
8701 size_t answer = 0;
8702 for (int i = 0; i < r->high_low_container.size; i++) {
8703 uint8_t typecode_original;
8704 void *c = ra_get_container_at_index(&r->high_low_container, i,
8705 &typecode_original);
8706 answer += container_shrink_to_fit(c, typecode_original);
8707 }
8708 answer += ra_shrink_to_fit(&r->high_low_container);
8709 return answer;
8710 }
8711
8712 /**
8713 * Remove run-length encoding even when it is more space efficient
8714 * return whether a change was applied
8715 */
8716 bool roaring_bitmap_remove_run_compression(roaring_bitmap_t *r) {
8717 bool answer = false;
8718 for (int i = 0; i < r->high_low_container.size; i++) {
8719 uint8_t typecode_original, typecode_after;
8720 void *c = ra_get_container_at_index(&r->high_low_container, i,
8721 &typecode_original);
8722 if (get_container_type(c, typecode_original) ==
8723 RUN_CONTAINER_TYPE_CODE) {
8724 answer = true;
8725 if (typecode_original == SHARED_CONTAINER_TYPE_CODE) {
8726 run_container_t *truec =
8727 (run_container_t *)((shared_container_t *)c)->container;
8728 int32_t card = run_container_cardinality(truec);
8729 void *c1 = convert_to_bitset_or_array_container(
8730 truec, card, &typecode_after);
8731 shared_container_free((shared_container_t *)c);// will free the run container as needed
8732 ra_set_container_at_index(&r->high_low_container, i, c1,
8733 typecode_after);
8734
8735 } else {
8736 int32_t card = run_container_cardinality((run_container_t *)c);
8737 void *c1 = convert_to_bitset_or_array_container(
8738 (run_container_t *)c, card, &typecode_after);
8739 run_container_free((run_container_t *)c);
8740 ra_set_container_at_index(&r->high_low_container, i, c1,
8741 typecode_after);
8742 }
8743 }
8744 }
8745 return answer;
8746 }
8747
8748 size_t roaring_bitmap_serialize(const roaring_bitmap_t *ra, char *buf) {
8749 size_t portablesize = roaring_bitmap_portable_size_in_bytes(ra);
8750 uint64_t cardinality = roaring_bitmap_get_cardinality(ra);
8751 uint64_t sizeasarray = cardinality * sizeof(uint32_t) + sizeof(uint32_t);
8752 if (portablesize < sizeasarray) {
8753 buf[0] = SERIALIZATION_CONTAINER;
8754 return roaring_bitmap_portable_serialize(ra, buf + 1) + 1;
8755 } else {
8756 buf[0] = SERIALIZATION_ARRAY_UINT32;
8757 memcpy(buf + 1, &cardinality, sizeof(uint32_t));
8758 roaring_bitmap_to_uint32_array(
8759 ra, (uint32_t *)(buf + 1 + sizeof(uint32_t)));
8760 return 1 + (size_t)sizeasarray;
8761 }
8762 }
8763
8764 size_t roaring_bitmap_size_in_bytes(const roaring_bitmap_t *ra) {
8765 size_t portablesize = roaring_bitmap_portable_size_in_bytes(ra);
8766 uint64_t sizeasarray = roaring_bitmap_get_cardinality(ra) * sizeof(uint32_t) +
8767 sizeof(uint32_t);
8768 return portablesize < sizeasarray ? portablesize + 1 : (size_t)sizeasarray + 1;
8769 }
8770
8771 size_t roaring_bitmap_portable_size_in_bytes(const roaring_bitmap_t *ra) {
8772 return ra_portable_size_in_bytes(&ra->high_low_container);
8773 }
8774
8775
8776 roaring_bitmap_t *roaring_bitmap_portable_deserialize_safe(const char *buf, size_t maxbytes) {
8777 roaring_bitmap_t *ans =
8778 (roaring_bitmap_t *)malloc(sizeof(roaring_bitmap_t));
8779 if (ans == NULL) {
8780 return NULL;
8781 }
8782 size_t bytesread;
8783 bool is_ok = ra_portable_deserialize(&ans->high_low_container, buf, maxbytes, &bytesread);
8784 if(is_ok) assert(bytesread <= maxbytes);
8785 roaring_bitmap_set_copy_on_write(ans, false);
8786 if (!is_ok) {
8787 free(ans);
8788 return NULL;
8789 }
8790 return ans;
8791 }
8792
8793 roaring_bitmap_t *roaring_bitmap_portable_deserialize(const char *buf) {
8794 return roaring_bitmap_portable_deserialize_safe(buf, SIZE_MAX);
8795 }
8796
8797
8798 size_t roaring_bitmap_portable_deserialize_size(const char *buf, size_t maxbytes) {
8799 return ra_portable_deserialize_size(buf, maxbytes);
8800 }
8801
8802
8803 size_t roaring_bitmap_portable_serialize(const roaring_bitmap_t *ra,
8804 char *buf) {
8805 return ra_portable_serialize(&ra->high_low_container, buf);
8806 }
8807
8808 roaring_bitmap_t *roaring_bitmap_deserialize(const void *buf) {
8809 const char *bufaschar = (const char *)buf;
8810 if (*(const unsigned char *)buf == SERIALIZATION_ARRAY_UINT32) {
8811 /* This looks like a compressed set of uint32_t elements */
8812 uint32_t card;
8813 memcpy(&card, bufaschar + 1, sizeof(uint32_t));
8814 const uint32_t *elems =
8815 (const uint32_t *)(bufaschar + 1 + sizeof(uint32_t));
8816
8817 return roaring_bitmap_of_ptr(card, elems);
8818 } else if (bufaschar[0] == SERIALIZATION_CONTAINER) {
8819 return roaring_bitmap_portable_deserialize(bufaschar + 1);
8820 } else
8821 return (NULL);
8822 }
8823
8824 bool roaring_iterate(const roaring_bitmap_t *ra, roaring_iterator iterator,
8825 void *ptr) {
8826 for (int i = 0; i < ra->high_low_container.size; ++i)
8827 if (!container_iterate(ra->high_low_container.containers[i],
8828 ra->high_low_container.typecodes[i],
8829 ((uint32_t)ra->high_low_container.keys[i]) << 16,
8830 iterator, ptr)) {
8831 return false;
8832 }
8833 return true;
8834 }
8835
8836 bool roaring_iterate64(const roaring_bitmap_t *ra, roaring_iterator64 iterator,
8837 uint64_t high_bits, void *ptr) {
8838 for (int i = 0; i < ra->high_low_container.size; ++i)
8839 if (!container_iterate64(
8840 ra->high_low_container.containers[i],
8841 ra->high_low_container.typecodes[i],
8842 ((uint32_t)ra->high_low_container.keys[i]) << 16, iterator,
8843 high_bits, ptr)) {
8844 return false;
8845 }
8846 return true;
8847 }
8848
8849 /****
8850 * begin roaring_uint32_iterator_t
8851 *****/
8852
8853 // Partially initializes the roaring iterator when it begins looking at
8854 // a new container.
8855 static bool iter_new_container_partial_init(roaring_uint32_iterator_t *newit) {
8856 newit->in_container_index = 0;
8857 newit->run_index = 0;
8858 newit->current_value = 0;
8859 if (newit->container_index >= newit->parent->high_low_container.size ||
8860 newit->container_index < 0) {
8861 newit->current_value = UINT32_MAX;
8862 return (newit->has_value = false);
8863 }
8864 // assume not empty
8865 newit->has_value = true;
8866 // we precompute container, typecode and highbits so that successive
8867 // iterators do not have to grab them from odd memory locations
8868 // and have to worry about the (easily predicted) container_unwrap_shared
8869 // call.
8870 newit->container =
8871 newit->parent->high_low_container.containers[newit->container_index];
8872 newit->typecode =
8873 newit->parent->high_low_container.typecodes[newit->container_index];
8874 newit->highbits =
8875 ((uint32_t)
8876 newit->parent->high_low_container.keys[newit->container_index])
8877 << 16;
8878 newit->container =
8879 container_unwrap_shared(newit->container, &(newit->typecode));
8880 return newit->has_value;
8881 }
8882
8883 static bool loadfirstvalue(roaring_uint32_iterator_t *newit) {
8884 if (!iter_new_container_partial_init(newit))
8885 return newit->has_value;
8886
8887 uint32_t wordindex;
8888 uint64_t word; // used for bitsets
8889 switch (newit->typecode) {
8890 case BITSET_CONTAINER_TYPE_CODE:
8891 wordindex = 0;
8892 while ((word = ((const bitset_container_t *)(newit->container))
8893 ->array[wordindex]) == 0)
8894 wordindex++; // advance
8895 // here "word" is non-zero
8896 newit->in_container_index = wordindex * 64 + __builtin_ctzll(word);
8897 newit->current_value = newit->highbits | newit->in_container_index;
8898 break;
8899 case ARRAY_CONTAINER_TYPE_CODE:
8900 newit->current_value =
8901 newit->highbits |
8902 ((const array_container_t *)(newit->container))->array[0];
8903 break;
8904 case RUN_CONTAINER_TYPE_CODE:
8905 newit->current_value =
8906 newit->highbits |
8907 (((const run_container_t *)(newit->container))->runs[0].value);
8908 break;
8909 default:
8910 // if this ever happens, bug!
8911 assert(false);
8912 } // switch (typecode)
8913 return true;
8914 }
8915
8916 static bool loadlastvalue(roaring_uint32_iterator_t* newit) {
8917 if (!iter_new_container_partial_init(newit))
8918 return newit->has_value;
8919
8920 switch(newit->typecode) {
8921 case BITSET_CONTAINER_TYPE_CODE: {
8922 uint32_t wordindex = BITSET_CONTAINER_SIZE_IN_WORDS - 1;
8923 uint64_t word;
8924 const bitset_container_t* bitset_container = (const bitset_container_t*)newit->container;
8925 while ((word = bitset_container->array[wordindex]) == 0)
8926 --wordindex;
8927
8928 int num_leading_zeros = __builtin_clzll(word);
8929 newit->in_container_index = (wordindex * 64) + (63 - num_leading_zeros);
8930 newit->current_value = newit->highbits | newit->in_container_index;
8931 break;
8932 }
8933 case ARRAY_CONTAINER_TYPE_CODE: {
8934 const array_container_t* array_container = (const array_container_t*)newit->container;
8935 newit->in_container_index = array_container->cardinality - 1;
8936 newit->current_value = newit->highbits | array_container->array[newit->in_container_index];
8937 break;
8938 }
8939 case RUN_CONTAINER_TYPE_CODE: {
8940 const run_container_t* run_container = (const run_container_t*)newit->container;
8941 newit->run_index = run_container->n_runs - 1;
8942 const rle16_t* last_run = &run_container->runs[newit->run_index];
8943 newit->current_value = newit->highbits | (last_run->value + last_run->length);
8944 break;
8945 }
8946 default:
8947 // if this ever happens, bug!
8948 assert(false);
8949 }
8950 return true;
8951 }
8952
8953 // prerequesite: the value should be in range of the container
8954 static bool loadfirstvalue_largeorequal(roaring_uint32_iterator_t *newit, uint32_t val) {
8955 // Don't have to check return value because of prerequisite
8956 iter_new_container_partial_init(newit);
8957 uint16_t lb = val & 0xFFFF;
8958
8959 switch (newit->typecode) {
8960 case BITSET_CONTAINER_TYPE_CODE:
8961 newit->in_container_index = bitset_container_index_equalorlarger((const bitset_container_t *)(newit->container), lb);
8962 newit->current_value = newit->highbits | newit->in_container_index;
8963 break;
8964 case ARRAY_CONTAINER_TYPE_CODE:
8965 newit->in_container_index = array_container_index_equalorlarger((const array_container_t *)(newit->container), lb);
8966 newit->current_value =
8967 newit->highbits |
8968 ((const array_container_t *)(newit->container))->array[newit->in_container_index];
8969 break;
8970 case RUN_CONTAINER_TYPE_CODE:
8971 newit->run_index = run_container_index_equalorlarger((const run_container_t *)(newit->container), lb);
8972 if(((const run_container_t *)(newit->container))->runs[newit->run_index].value <= lb) {
8973 newit->current_value = val;
8974 } else {
8975 newit->current_value =
8976 newit->highbits |
8977 (((const run_container_t *)(newit->container))->runs[newit->run_index].value);
8978 }
8979 break;
8980 default:
8981 // if this ever happens, bug!
8982 assert(false);
8983 } // switch (typecode)
8984 return true;
8985 }
8986
8987 void roaring_init_iterator(const roaring_bitmap_t *ra,
8988 roaring_uint32_iterator_t *newit) {
8989 newit->parent = ra;
8990 newit->container_index = 0;
8991 newit->has_value = loadfirstvalue(newit);
8992 }
8993
8994 void roaring_init_iterator_last(const roaring_bitmap_t *ra,
8995 roaring_uint32_iterator_t *newit) {
8996 newit->parent = ra;
8997 newit->container_index = newit->parent->high_low_container.size - 1;
8998 newit->has_value = loadlastvalue(newit);
8999 }
9000
9001 roaring_uint32_iterator_t *roaring_create_iterator(const roaring_bitmap_t *ra) {
9002 roaring_uint32_iterator_t *newit =
9003 (roaring_uint32_iterator_t *)malloc(sizeof(roaring_uint32_iterator_t));
9004 if (newit == NULL) return NULL;
9005 roaring_init_iterator(ra, newit);
9006 return newit;
9007 }
9008
9009 roaring_uint32_iterator_t *roaring_copy_uint32_iterator(
9010 const roaring_uint32_iterator_t *it) {
9011 roaring_uint32_iterator_t *newit =
9012 (roaring_uint32_iterator_t *)malloc(sizeof(roaring_uint32_iterator_t));
9013 memcpy(newit, it, sizeof(roaring_uint32_iterator_t));
9014 return newit;
9015 }
9016
9017 bool roaring_move_uint32_iterator_equalorlarger(roaring_uint32_iterator_t *it, uint32_t val) {
9018 uint16_t hb = val >> 16;
9019 const int i = ra_get_index(& it->parent->high_low_container, hb);
9020 if (i >= 0) {
9021 uint32_t lowvalue = container_maximum(it->parent->high_low_container.containers[i], it->parent->high_low_container.typecodes[i]);
9022 uint16_t lb = val & 0xFFFF;
9023 if(lowvalue < lb ) {
9024 it->container_index = i+1; // will have to load first value of next container
9025 } else {// the value is necessarily within the range of the container
9026 it->container_index = i;
9027 it->has_value = loadfirstvalue_largeorequal(it, val);
9028 return it->has_value;
9029 }
9030 } else {
9031 // there is no matching, so we are going for the next container
9032 it->container_index = -i-1;
9033 }
9034 it->has_value = loadfirstvalue(it);
9035 return it->has_value;
9036 }
9037
9038
9039 bool roaring_advance_uint32_iterator(roaring_uint32_iterator_t *it) {
9040 if (it->container_index >= it->parent->high_low_container.size) {
9041 return (it->has_value = false);
9042 }
9043 if (it->container_index < 0) {
9044 it->container_index = 0;
9045 return (it->has_value = loadfirstvalue(it));
9046 }
9047
9048 uint32_t wordindex; // used for bitsets
9049 uint64_t word; // used for bitsets
9050 switch (it->typecode) {
9051 case BITSET_CONTAINER_TYPE_CODE:
9052 it->in_container_index++;
9053 wordindex = it->in_container_index / 64;
9054 if (wordindex >= BITSET_CONTAINER_SIZE_IN_WORDS) break;
9055 word = ((const bitset_container_t *)(it->container))
9056 ->array[wordindex] &
9057 (UINT64_MAX << (it->in_container_index % 64));
9058 // next part could be optimized/simplified
9059 while ((word == 0) &&
9060 (wordindex + 1 < BITSET_CONTAINER_SIZE_IN_WORDS)) {
9061 wordindex++;
9062 word = ((const bitset_container_t *)(it->container))
9063 ->array[wordindex];
9064 }
9065 if (word != 0) {
9066 it->in_container_index = wordindex * 64 + __builtin_ctzll(word);
9067 it->current_value = it->highbits | it->in_container_index;
9068 return (it->has_value = true);
9069 }
9070 break;
9071 case ARRAY_CONTAINER_TYPE_CODE:
9072 it->in_container_index++;
9073 if (it->in_container_index <
9074 ((const array_container_t *)(it->container))->cardinality) {
9075 it->current_value = it->highbits |
9076 ((const array_container_t *)(it->container))
9077 ->array[it->in_container_index];
9078 return (it->has_value = true);
9079 }
9080 break;
9081 case RUN_CONTAINER_TYPE_CODE: {
9082 if(it->current_value == UINT32_MAX) {
9083 return (it->has_value = false); // without this, we risk an overflow to zero
9084 }
9085
9086 const run_container_t* run_container = (const run_container_t*)it->container;
9087 if (++it->current_value <= (it->highbits | (run_container->runs[it->run_index].value +
9088 run_container->runs[it->run_index].length))) {
9089 return (it->has_value = true);
9090 }
9091
9092 if (++it->run_index < run_container->n_runs) {
9093 // Assume the run has a value
9094 it->current_value = it->highbits | run_container->runs[it->run_index].value;
9095 return (it->has_value = true);
9096 }
9097 break;
9098 }
9099 default:
9100 // if this ever happens, bug!
9101 assert(false);
9102 } // switch (typecode)
9103 // moving to next container
9104 it->container_index++;
9105 return (it->has_value = loadfirstvalue(it));
9106 }
9107
9108 bool roaring_previous_uint32_iterator(roaring_uint32_iterator_t *it) {
9109 if (it->container_index < 0) {
9110 return (it->has_value = false);
9111 }
9112 if (it->container_index >= it->parent->high_low_container.size) {
9113 it->container_index = it->parent->high_low_container.size - 1;
9114 return (it->has_value = loadlastvalue(it));
9115 }
9116
9117 switch (it->typecode) {
9118 case BITSET_CONTAINER_TYPE_CODE: {
9119 if (--it->in_container_index < 0)
9120 break;
9121
9122 const bitset_container_t* bitset_container = (const bitset_container_t*)it->container;
9123 int32_t wordindex = it->in_container_index / 64;
9124 uint64_t word = bitset_container->array[wordindex] & (UINT64_MAX >> (63 - (it->in_container_index % 64)));
9125
9126 while (word == 0 && --wordindex >= 0) {
9127 word = bitset_container->array[wordindex];
9128 }
9129 if (word == 0)
9130 break;
9131
9132 int num_leading_zeros = __builtin_clzll(word);
9133 it->in_container_index = (wordindex * 64) + (63 - num_leading_zeros);
9134 it->current_value = it->highbits | it->in_container_index;
9135 return (it->has_value = true);
9136 }
9137 case ARRAY_CONTAINER_TYPE_CODE: {
9138 if (--it->in_container_index < 0)
9139 break;
9140
9141 const array_container_t* array_container = (const array_container_t*)it->container;
9142 it->current_value = it->highbits | array_container->array[it->in_container_index];
9143 return (it->has_value = true);
9144 }
9145 case RUN_CONTAINER_TYPE_CODE: {
9146 if(it->current_value == 0)
9147 return (it->has_value = false);
9148
9149 const run_container_t* run_container = (const run_container_t*)it->container;
9150 if (--it->current_value >= (it->highbits | run_container->runs[it->run_index].value)) {
9151 return (it->has_value = true);
9152 }
9153
9154 if (--it->run_index < 0)
9155 break;
9156
9157 it->current_value = it->highbits | (run_container->runs[it->run_index].value +
9158 run_container->runs[it->run_index].length);
9159 return (it->has_value = true);
9160 }
9161 default:
9162 // if this ever happens, bug!
9163 assert(false);
9164 } // switch (typecode)
9165
9166 // moving to previous container
9167 it->container_index--;
9168 return (it->has_value = loadlastvalue(it));
9169 }
9170
9171 uint32_t roaring_read_uint32_iterator(roaring_uint32_iterator_t *it, uint32_t* buf, uint32_t count) {
9172 uint32_t ret = 0;
9173 uint32_t num_values;
9174 uint32_t wordindex; // used for bitsets
9175 uint64_t word; // used for bitsets
9176 const array_container_t* acont; //TODO remove
9177 const run_container_t* rcont; //TODO remove
9178 const bitset_container_t* bcont; //TODO remove
9179
9180 while (it->has_value && ret < count) {
9181 switch (it->typecode) {
9182 case BITSET_CONTAINER_TYPE_CODE:
9183 bcont = (const bitset_container_t*)(it->container);
9184 wordindex = it->in_container_index / 64;
9185 word = bcont->array[wordindex] & (UINT64_MAX << (it->in_container_index % 64));
9186 do {
9187 while (word != 0 && ret < count) {
9188 buf[0] = it->highbits | (wordindex * 64 + __builtin_ctzll(word));
9189 word = word & (word - 1);
9190 buf++;
9191 ret++;
9192 }
9193 while (word == 0 && wordindex+1 < BITSET_CONTAINER_SIZE_IN_WORDS) {
9194 wordindex++;
9195 word = bcont->array[wordindex];
9196 }
9197 } while (word != 0 && ret < count);
9198 it->has_value = (word != 0);
9199 if (it->has_value) {
9200 it->in_container_index = wordindex * 64 + __builtin_ctzll(word);
9201 it->current_value = it->highbits | it->in_container_index;
9202 }
9203 break;
9204 case ARRAY_CONTAINER_TYPE_CODE:
9205 acont = (const array_container_t *)(it->container);
9206 num_values = minimum_uint32(acont->cardinality - it->in_container_index, count - ret);
9207 for (uint32_t i = 0; i < num_values; i++) {
9208 buf[i] = it->highbits | acont->array[it->in_container_index + i];
9209 }
9210 buf += num_values;
9211 ret += num_values;
9212 it->in_container_index += num_values;
9213 it->has_value = (it->in_container_index < acont->cardinality);
9214 if (it->has_value) {
9215 it->current_value = it->highbits | acont->array[it->in_container_index];
9216 }
9217 break;
9218 case RUN_CONTAINER_TYPE_CODE:
9219 rcont = (const run_container_t*)(it->container);
9220 //"in_run_index" name is misleading, read it as "max_value_in_current_run"
9221 do {
9222 uint32_t largest_run_value = it->highbits | (rcont->runs[it->run_index].value + rcont->runs[it->run_index].length);
9223 num_values = minimum_uint32(largest_run_value - it->current_value + 1, count - ret);
9224 for (uint32_t i = 0; i < num_values; i++) {
9225 buf[i] = it->current_value + i;
9226 }
9227 it->current_value += num_values; // this can overflow to zero: UINT32_MAX+1=0
9228 buf += num_values;
9229 ret += num_values;
9230
9231 if (it->current_value > largest_run_value || it->current_value == 0) {
9232 it->run_index++;
9233 if (it->run_index < rcont->n_runs) {
9234 it->current_value = it->highbits | rcont->runs[it->run_index].value;
9235 } else {
9236 it->has_value = false;
9237 }
9238 }
9239 } while ((ret < count) && it->has_value);
9240 break;
9241 default:
9242 assert(false);
9243 }
9244 if (it->has_value) {
9245 assert(ret == count);
9246 return ret;
9247 }
9248 it->container_index++;
9249 it->has_value = loadfirstvalue(it);
9250 }
9251 return ret;
9252 }
9253
9254
9255
9256 void roaring_free_uint32_iterator(roaring_uint32_iterator_t *it) { free(it); }
9257
9258 /****
9259 * end of roaring_uint32_iterator_t
9260 *****/
9261
9262 bool roaring_bitmap_equals(const roaring_bitmap_t *ra1,
9263 const roaring_bitmap_t *ra2) {
9264 if (ra1->high_low_container.size != ra2->high_low_container.size) {
9265 return false;
9266 }
9267 for (int i = 0; i < ra1->high_low_container.size; ++i) {
9268 if (ra1->high_low_container.keys[i] !=
9269 ra2->high_low_container.keys[i]) {
9270 return false;
9271 }
9272 }
9273 for (int i = 0; i < ra1->high_low_container.size; ++i) {
9274 bool areequal = container_equals(ra1->high_low_container.containers[i],
9275 ra1->high_low_container.typecodes[i],
9276 ra2->high_low_container.containers[i],
9277 ra2->high_low_container.typecodes[i]);
9278 if (!areequal) {
9279 return false;
9280 }
9281 }
9282 return true;
9283 }
9284
9285 bool roaring_bitmap_is_subset(const roaring_bitmap_t *ra1,
9286 const roaring_bitmap_t *ra2) {
9287 const int length1 = ra1->high_low_container.size,
9288 length2 = ra2->high_low_container.size;
9289
9290 int pos1 = 0, pos2 = 0;
9291
9292 while (pos1 < length1 && pos2 < length2) {
9293 const uint16_t s1 = ra_get_key_at_index(&ra1->high_low_container, pos1);
9294 const uint16_t s2 = ra_get_key_at_index(&ra2->high_low_container, pos2);
9295
9296 if (s1 == s2) {
9297 uint8_t container_type_1, container_type_2;
9298 void *c1 = ra_get_container_at_index(&ra1->high_low_container, pos1,
9299 &container_type_1);
9300 void *c2 = ra_get_container_at_index(&ra2->high_low_container, pos2,
9301 &container_type_2);
9302 bool subset =
9303 container_is_subset(c1, container_type_1, c2, container_type_2);
9304 if (!subset) return false;
9305 ++pos1;
9306 ++pos2;
9307 } else if (s1 < s2) { // s1 < s2
9308 return false;
9309 } else { // s1 > s2
9310 pos2 = ra_advance_until(&ra2->high_low_container, s1, pos2);
9311 }
9312 }
9313 if (pos1 == length1)
9314 return true;
9315 else
9316 return false;
9317 }
9318
9319 static void insert_flipped_container(roaring_array_t *ans_arr,
9320 const roaring_array_t *x1_arr, uint16_t hb,
9321 uint16_t lb_start, uint16_t lb_end) {
9322 const int i = ra_get_index(x1_arr, hb);
9323 const int j = ra_get_index(ans_arr, hb);
9324 uint8_t ctype_in, ctype_out;
9325 void *flipped_container = NULL;
9326 if (i >= 0) {
9327 void *container_to_flip =
9328 ra_get_container_at_index(x1_arr, i, &ctype_in);
9329 flipped_container =
9330 container_not_range(container_to_flip, ctype_in, (uint32_t)lb_start,
9331 (uint32_t)(lb_end + 1), &ctype_out);
9332
9333 if (container_get_cardinality(flipped_container, ctype_out))
9334 ra_insert_new_key_value_at(ans_arr, -j - 1, hb, flipped_container,
9335 ctype_out);
9336 else {
9337 container_free(flipped_container, ctype_out);
9338 }
9339 } else {
9340 flipped_container = container_range_of_ones(
9341 (uint32_t)lb_start, (uint32_t)(lb_end + 1), &ctype_out);
9342 ra_insert_new_key_value_at(ans_arr, -j - 1, hb, flipped_container,
9343 ctype_out);
9344 }
9345 }
9346
9347 static void inplace_flip_container(roaring_array_t *x1_arr, uint16_t hb,
9348 uint16_t lb_start, uint16_t lb_end) {
9349 const int i = ra_get_index(x1_arr, hb);
9350 uint8_t ctype_in, ctype_out;
9351 void *flipped_container = NULL;
9352 if (i >= 0) {
9353 void *container_to_flip =
9354 ra_get_container_at_index(x1_arr, i, &ctype_in);
9355 flipped_container = container_inot_range(
9356 container_to_flip, ctype_in, (uint32_t)lb_start,
9357 (uint32_t)(lb_end + 1), &ctype_out);
9358 // if a new container was created, the old one was already freed
9359 if (container_get_cardinality(flipped_container, ctype_out)) {
9360 ra_set_container_at_index(x1_arr, i, flipped_container, ctype_out);
9361 } else {
9362 container_free(flipped_container, ctype_out);
9363 ra_remove_at_index(x1_arr, i);
9364 }
9365
9366 } else {
9367 flipped_container = container_range_of_ones(
9368 (uint32_t)lb_start, (uint32_t)(lb_end + 1), &ctype_out);
9369 ra_insert_new_key_value_at(x1_arr, -i - 1, hb, flipped_container,
9370 ctype_out);
9371 }
9372 }
9373
9374 static void insert_fully_flipped_container(roaring_array_t *ans_arr,
9375 const roaring_array_t *x1_arr,
9376 uint16_t hb) {
9377 const int i = ra_get_index(x1_arr, hb);
9378 const int j = ra_get_index(ans_arr, hb);
9379 uint8_t ctype_in, ctype_out;
9380 void *flipped_container = NULL;
9381 if (i >= 0) {
9382 void *container_to_flip =
9383 ra_get_container_at_index(x1_arr, i, &ctype_in);
9384 flipped_container =
9385 container_not(container_to_flip, ctype_in, &ctype_out);
9386 if (container_get_cardinality(flipped_container, ctype_out))
9387 ra_insert_new_key_value_at(ans_arr, -j - 1, hb, flipped_container,
9388 ctype_out);
9389 else {
9390 container_free(flipped_container, ctype_out);
9391 }
9392 } else {
9393 flipped_container = container_range_of_ones(0U, 0x10000U, &ctype_out);
9394 ra_insert_new_key_value_at(ans_arr, -j - 1, hb, flipped_container,
9395 ctype_out);
9396 }
9397 }
9398
9399 static void inplace_fully_flip_container(roaring_array_t *x1_arr, uint16_t hb) {
9400 const int i = ra_get_index(x1_arr, hb);
9401 uint8_t ctype_in, ctype_out;
9402 void *flipped_container = NULL;
9403 if (i >= 0) {
9404 void *container_to_flip =
9405 ra_get_container_at_index(x1_arr, i, &ctype_in);
9406 flipped_container =
9407 container_inot(container_to_flip, ctype_in, &ctype_out);
9408
9409 if (container_get_cardinality(flipped_container, ctype_out)) {
9410 ra_set_container_at_index(x1_arr, i, flipped_container, ctype_out);
9411 } else {
9412 container_free(flipped_container, ctype_out);
9413 ra_remove_at_index(x1_arr, i);
9414 }
9415
9416 } else {
9417 flipped_container = container_range_of_ones(0U, 0x10000U, &ctype_out);
9418 ra_insert_new_key_value_at(x1_arr, -i - 1, hb, flipped_container,
9419 ctype_out);
9420 }
9421 }
9422
9423 roaring_bitmap_t *roaring_bitmap_flip(const roaring_bitmap_t *x1,
9424 uint64_t range_start,
9425 uint64_t range_end) {
9426 if (range_start >= range_end) {
9427 return roaring_bitmap_copy(x1);
9428 }
9429 if(range_end >= UINT64_C(0x100000000)) {
9430 range_end = UINT64_C(0x100000000);
9431 }
9432
9433 roaring_bitmap_t *ans = roaring_bitmap_create();
9434 roaring_bitmap_set_copy_on_write(ans, is_cow(x1));
9435
9436 uint16_t hb_start = (uint16_t)(range_start >> 16);
9437 const uint16_t lb_start = (uint16_t)range_start; // & 0xFFFF;
9438 uint16_t hb_end = (uint16_t)((range_end - 1) >> 16);
9439 const uint16_t lb_end = (uint16_t)(range_end - 1); // & 0xFFFF;
9440
9441 ra_append_copies_until(&ans->high_low_container, &x1->high_low_container,
9442 hb_start, is_cow(x1));
9443 if (hb_start == hb_end) {
9444 insert_flipped_container(&ans->high_low_container,
9445 &x1->high_low_container, hb_start, lb_start,
9446 lb_end);
9447 } else {
9448 // start and end containers are distinct
9449 if (lb_start > 0) {
9450 // handle first (partial) container
9451 insert_flipped_container(&ans->high_low_container,
9452 &x1->high_low_container, hb_start,
9453 lb_start, 0xFFFF);
9454 ++hb_start; // for the full containers. Can't wrap.
9455 }
9456
9457 if (lb_end != 0xFFFF) --hb_end; // later we'll handle the partial block
9458
9459 for (uint32_t hb = hb_start; hb <= hb_end; ++hb) {
9460 insert_fully_flipped_container(&ans->high_low_container,
9461 &x1->high_low_container, hb);
9462 }
9463
9464 // handle a partial final container
9465 if (lb_end != 0xFFFF) {
9466 insert_flipped_container(&ans->high_low_container,
9467 &x1->high_low_container, hb_end + 1, 0,
9468 lb_end);
9469 ++hb_end;
9470 }
9471 }
9472 ra_append_copies_after(&ans->high_low_container, &x1->high_low_container,
9473 hb_end, is_cow(x1));
9474 return ans;
9475 }
9476
9477 void roaring_bitmap_flip_inplace(roaring_bitmap_t *x1, uint64_t range_start,
9478 uint64_t range_end) {
9479 if (range_start >= range_end) {
9480 return; // empty range
9481 }
9482 if(range_end >= UINT64_C(0x100000000)) {
9483 range_end = UINT64_C(0x100000000);
9484 }
9485
9486 uint16_t hb_start = (uint16_t)(range_start >> 16);
9487 const uint16_t lb_start = (uint16_t)range_start;
9488 uint16_t hb_end = (uint16_t)((range_end - 1) >> 16);
9489 const uint16_t lb_end = (uint16_t)(range_end - 1);
9490
9491 if (hb_start == hb_end) {
9492 inplace_flip_container(&x1->high_low_container, hb_start, lb_start,
9493 lb_end);
9494 } else {
9495 // start and end containers are distinct
9496 if (lb_start > 0) {
9497 // handle first (partial) container
9498 inplace_flip_container(&x1->high_low_container, hb_start, lb_start,
9499 0xFFFF);
9500 ++hb_start; // for the full containers. Can't wrap.
9501 }
9502
9503 if (lb_end != 0xFFFF) --hb_end;
9504
9505 for (uint32_t hb = hb_start; hb <= hb_end; ++hb) {
9506 inplace_fully_flip_container(&x1->high_low_container, hb);
9507 }
9508 // handle a partial final container
9509 if (lb_end != 0xFFFF) {
9510 inplace_flip_container(&x1->high_low_container, hb_end + 1, 0,
9511 lb_end);
9512 ++hb_end;
9513 }
9514 }
9515 }
9516
9517 roaring_bitmap_t *roaring_bitmap_lazy_or(const roaring_bitmap_t *x1,
9518 const roaring_bitmap_t *x2,
9519 const bool bitsetconversion) {
9520 uint8_t container_result_type = 0;
9521 const int length1 = x1->high_low_container.size,
9522 length2 = x2->high_low_container.size;
9523 if (0 == length1) {
9524 return roaring_bitmap_copy(x2);
9525 }
9526 if (0 == length2) {
9527 return roaring_bitmap_copy(x1);
9528 }
9529 roaring_bitmap_t *answer =
9530 roaring_bitmap_create_with_capacity(length1 + length2);
9531 roaring_bitmap_set_copy_on_write(answer, is_cow(x1) && is_cow(x2));
9532 int pos1 = 0, pos2 = 0;
9533 uint8_t container_type_1, container_type_2;
9534 uint16_t s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
9535 uint16_t s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
9536 while (true) {
9537 if (s1 == s2) {
9538 void *c1 = ra_get_container_at_index(&x1->high_low_container, pos1,
9539 &container_type_1);
9540 void *c2 = ra_get_container_at_index(&x2->high_low_container, pos2,
9541 &container_type_2);
9542 void *c;
9543 if (bitsetconversion && (get_container_type(c1, container_type_1) !=
9544 BITSET_CONTAINER_TYPE_CODE) &&
9545 (get_container_type(c2, container_type_2) !=
9546 BITSET_CONTAINER_TYPE_CODE)) {
9547 void *newc1 =
9548 container_mutable_unwrap_shared(c1, &container_type_1);
9549 newc1 = container_to_bitset(newc1, container_type_1);
9550 container_type_1 = BITSET_CONTAINER_TYPE_CODE;
9551 c = container_lazy_ior(newc1, container_type_1, c2,
9552 container_type_2,
9553 &container_result_type);
9554 if (c != newc1) { // should not happen
9555 container_free(newc1, container_type_1);
9556 }
9557 } else {
9558 c = container_lazy_or(c1, container_type_1, c2,
9559 container_type_2, &container_result_type);
9560 }
9561 // since we assume that the initial containers are non-empty,
9562 // the
9563 // result here
9564 // can only be non-empty
9565 ra_append(&answer->high_low_container, s1, c,
9566 container_result_type);
9567 ++pos1;
9568 ++pos2;
9569 if (pos1 == length1) break;
9570 if (pos2 == length2) break;
9571 s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
9572 s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
9573
9574 } else if (s1 < s2) { // s1 < s2
9575 void *c1 = ra_get_container_at_index(&x1->high_low_container, pos1,
9576 &container_type_1);
9577 c1 =
9578 get_copy_of_container(c1, &container_type_1, is_cow(x1));
9579 if (is_cow(x1)) {
9580 ra_set_container_at_index(&x1->high_low_container, pos1, c1,
9581 container_type_1);
9582 }
9583 ra_append(&answer->high_low_container, s1, c1, container_type_1);
9584 pos1++;
9585 if (pos1 == length1) break;
9586 s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
9587
9588 } else { // s1 > s2
9589 void *c2 = ra_get_container_at_index(&x2->high_low_container, pos2,
9590 &container_type_2);
9591 c2 =
9592 get_copy_of_container(c2, &container_type_2, is_cow(x2));
9593 if (is_cow(x2)) {
9594 ra_set_container_at_index(&x2->high_low_container, pos2, c2,
9595 container_type_2);
9596 }
9597 ra_append(&answer->high_low_container, s2, c2, container_type_2);
9598 pos2++;
9599 if (pos2 == length2) break;
9600 s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
9601 }
9602 }
9603 if (pos1 == length1) {
9604 ra_append_copy_range(&answer->high_low_container,
9605 &x2->high_low_container, pos2, length2,
9606 is_cow(x2));
9607 } else if (pos2 == length2) {
9608 ra_append_copy_range(&answer->high_low_container,
9609 &x1->high_low_container, pos1, length1,
9610 is_cow(x1));
9611 }
9612 return answer;
9613 }
9614
9615 void roaring_bitmap_lazy_or_inplace(roaring_bitmap_t *x1,
9616 const roaring_bitmap_t *x2,
9617 const bool bitsetconversion) {
9618 uint8_t container_result_type = 0;
9619 int length1 = x1->high_low_container.size;
9620 const int length2 = x2->high_low_container.size;
9621
9622 if (0 == length2) return;
9623
9624 if (0 == length1) {
9625 roaring_bitmap_overwrite(x1, x2);
9626 return;
9627 }
9628 int pos1 = 0, pos2 = 0;
9629 uint8_t container_type_1, container_type_2;
9630 uint16_t s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
9631 uint16_t s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
9632 while (true) {
9633 if (s1 == s2) {
9634 void *c1 = ra_get_container_at_index(&x1->high_low_container, pos1,
9635 &container_type_1);
9636 if (!container_is_full(c1, container_type_1)) {
9637 if ((bitsetconversion == false) ||
9638 (get_container_type(c1, container_type_1) ==
9639 BITSET_CONTAINER_TYPE_CODE)) {
9640 c1 = get_writable_copy_if_shared(c1, &container_type_1);
9641 } else {
9642 // convert to bitset
9643 void *oldc1 = c1;
9644 uint8_t oldt1 = container_type_1;
9645 c1 = container_mutable_unwrap_shared(c1, &container_type_1);
9646 c1 = container_to_bitset(c1, container_type_1);
9647 container_free(oldc1, oldt1);
9648 container_type_1 = BITSET_CONTAINER_TYPE_CODE;
9649 }
9650
9651 void *c2 = ra_get_container_at_index(&x2->high_low_container,
9652 pos2, &container_type_2);
9653 void *c = container_lazy_ior(c1, container_type_1, c2,
9654 container_type_2,
9655 &container_result_type);
9656 if (c !=
9657 c1) { // in this instance a new container was created, and
9658 // we need to free the old one
9659 container_free(c1, container_type_1);
9660 }
9661
9662 ra_set_container_at_index(&x1->high_low_container, pos1, c,
9663 container_result_type);
9664 }
9665 ++pos1;
9666 ++pos2;
9667 if (pos1 == length1) break;
9668 if (pos2 == length2) break;
9669 s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
9670 s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
9671
9672 } else if (s1 < s2) { // s1 < s2
9673 pos1++;
9674 if (pos1 == length1) break;
9675 s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
9676
9677 } else { // s1 > s2
9678 void *c2 = ra_get_container_at_index(&x2->high_low_container, pos2,
9679 &container_type_2);
9680 // void *c2_clone = container_clone(c2, container_type_2);
9681 c2 =
9682 get_copy_of_container(c2, &container_type_2, is_cow(x2));
9683 if (is_cow(x2)) {
9684 ra_set_container_at_index(&x2->high_low_container, pos2, c2,
9685 container_type_2);
9686 }
9687 ra_insert_new_key_value_at(&x1->high_low_container, pos1, s2, c2,
9688 container_type_2);
9689 pos1++;
9690 length1++;
9691 pos2++;
9692 if (pos2 == length2) break;
9693 s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
9694 }
9695 }
9696 if (pos1 == length1) {
9697 ra_append_copy_range(&x1->high_low_container, &x2->high_low_container,
9698 pos2, length2, is_cow(x2));
9699 }
9700 }
9701
9702 roaring_bitmap_t *roaring_bitmap_lazy_xor(const roaring_bitmap_t *x1,
9703 const roaring_bitmap_t *x2) {
9704 uint8_t container_result_type = 0;
9705 const int length1 = x1->high_low_container.size,
9706 length2 = x2->high_low_container.size;
9707 if (0 == length1) {
9708 return roaring_bitmap_copy(x2);
9709 }
9710 if (0 == length2) {
9711 return roaring_bitmap_copy(x1);
9712 }
9713 roaring_bitmap_t *answer =
9714 roaring_bitmap_create_with_capacity(length1 + length2);
9715 roaring_bitmap_set_copy_on_write(answer, is_cow(x1) && is_cow(x2));
9716 int pos1 = 0, pos2 = 0;
9717 uint8_t container_type_1, container_type_2;
9718 uint16_t s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
9719 uint16_t s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
9720 while (true) {
9721 if (s1 == s2) {
9722 void *c1 = ra_get_container_at_index(&x1->high_low_container, pos1,
9723 &container_type_1);
9724 void *c2 = ra_get_container_at_index(&x2->high_low_container, pos2,
9725 &container_type_2);
9726 void *c =
9727 container_lazy_xor(c1, container_type_1, c2, container_type_2,
9728 &container_result_type);
9729
9730 if (container_nonzero_cardinality(c, container_result_type)) {
9731 ra_append(&answer->high_low_container, s1, c,
9732 container_result_type);
9733 } else {
9734 container_free(c, container_result_type);
9735 }
9736
9737 ++pos1;
9738 ++pos2;
9739 if (pos1 == length1) break;
9740 if (pos2 == length2) break;
9741 s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
9742 s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
9743
9744 } else if (s1 < s2) { // s1 < s2
9745 void *c1 = ra_get_container_at_index(&x1->high_low_container, pos1,
9746 &container_type_1);
9747 c1 =
9748 get_copy_of_container(c1, &container_type_1, is_cow(x1));
9749 if (is_cow(x1)) {
9750 ra_set_container_at_index(&x1->high_low_container, pos1, c1,
9751 container_type_1);
9752 }
9753 ra_append(&answer->high_low_container, s1, c1, container_type_1);
9754 pos1++;
9755 if (pos1 == length1) break;
9756 s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
9757
9758 } else { // s1 > s2
9759 void *c2 = ra_get_container_at_index(&x2->high_low_container, pos2,
9760 &container_type_2);
9761 c2 =
9762 get_copy_of_container(c2, &container_type_2, is_cow(x2));
9763 if (is_cow(x2)) {
9764 ra_set_container_at_index(&x2->high_low_container, pos2, c2,
9765 container_type_2);
9766 }
9767 ra_append(&answer->high_low_container, s2, c2, container_type_2);
9768 pos2++;
9769 if (pos2 == length2) break;
9770 s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
9771 }
9772 }
9773 if (pos1 == length1) {
9774 ra_append_copy_range(&answer->high_low_container,
9775 &x2->high_low_container, pos2, length2,
9776 is_cow(x2));
9777 } else if (pos2 == length2) {
9778 ra_append_copy_range(&answer->high_low_container,
9779 &x1->high_low_container, pos1, length1,
9780 is_cow(x1));
9781 }
9782 return answer;
9783 }
9784
9785 void roaring_bitmap_lazy_xor_inplace(roaring_bitmap_t *x1,
9786 const roaring_bitmap_t *x2) {
9787 assert(x1 != x2);
9788 uint8_t container_result_type = 0;
9789 int length1 = x1->high_low_container.size;
9790 const int length2 = x2->high_low_container.size;
9791
9792 if (0 == length2) return;
9793
9794 if (0 == length1) {
9795 roaring_bitmap_overwrite(x1, x2);
9796 return;
9797 }
9798 int pos1 = 0, pos2 = 0;
9799 uint8_t container_type_1, container_type_2;
9800 uint16_t s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
9801 uint16_t s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
9802 while (true) {
9803 if (s1 == s2) {
9804 void *c1 = ra_get_container_at_index(&x1->high_low_container, pos1,
9805 &container_type_1);
9806 c1 = get_writable_copy_if_shared(c1, &container_type_1);
9807 void *c2 = ra_get_container_at_index(&x2->high_low_container, pos2,
9808 &container_type_2);
9809 void *c =
9810 container_lazy_ixor(c1, container_type_1, c2, container_type_2,
9811 &container_result_type);
9812 if (container_nonzero_cardinality(c, container_result_type)) {
9813 ra_set_container_at_index(&x1->high_low_container, pos1, c,
9814 container_result_type);
9815 ++pos1;
9816 } else {
9817 container_free(c, container_result_type);
9818 ra_remove_at_index(&x1->high_low_container, pos1);
9819 --length1;
9820 }
9821 ++pos2;
9822 if (pos1 == length1) break;
9823 if (pos2 == length2) break;
9824 s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
9825 s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
9826
9827 } else if (s1 < s2) { // s1 < s2
9828 pos1++;
9829 if (pos1 == length1) break;
9830 s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
9831
9832 } else { // s1 > s2
9833 void *c2 = ra_get_container_at_index(&x2->high_low_container, pos2,
9834 &container_type_2);
9835 // void *c2_clone = container_clone(c2, container_type_2);
9836 c2 =
9837 get_copy_of_container(c2, &container_type_2, is_cow(x2));
9838 if (is_cow(x2)) {
9839 ra_set_container_at_index(&x2->high_low_container, pos2, c2,
9840 container_type_2);
9841 }
9842 ra_insert_new_key_value_at(&x1->high_low_container, pos1, s2, c2,
9843 container_type_2);
9844 pos1++;
9845 length1++;
9846 pos2++;
9847 if (pos2 == length2) break;
9848 s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
9849 }
9850 }
9851 if (pos1 == length1) {
9852 ra_append_copy_range(&x1->high_low_container, &x2->high_low_container,
9853 pos2, length2, is_cow(x2));
9854 }
9855 }
9856
9857 void roaring_bitmap_repair_after_lazy(roaring_bitmap_t *ra) {
9858 for (int i = 0; i < ra->high_low_container.size; ++i) {
9859 const uint8_t original_typecode = ra->high_low_container.typecodes[i];
9860 void *container = ra->high_low_container.containers[i];
9861 uint8_t new_typecode = original_typecode;
9862 void *newcontainer =
9863 container_repair_after_lazy(container, &new_typecode);
9864 ra->high_low_container.containers[i] = newcontainer;
9865 ra->high_low_container.typecodes[i] = new_typecode;
9866 }
9867 }
9868
9869
9870
9871 /**
9872 * roaring_bitmap_rank returns the number of integers that are smaller or equal
9873 * to x.
9874 */
9875 uint64_t roaring_bitmap_rank(const roaring_bitmap_t *bm, uint32_t x) {
9876 uint64_t size = 0;
9877 uint32_t xhigh = x >> 16;
9878 for (int i = 0; i < bm->high_low_container.size; i++) {
9879 uint32_t key = bm->high_low_container.keys[i];
9880 if (xhigh > key) {
9881 size +=
9882 container_get_cardinality(bm->high_low_container.containers[i],
9883 bm->high_low_container.typecodes[i]);
9884 } else if (xhigh == key) {
9885 return size + container_rank(bm->high_low_container.containers[i],
9886 bm->high_low_container.typecodes[i],
9887 x & 0xFFFF);
9888 } else {
9889 return size;
9890 }
9891 }
9892 return size;
9893 }
9894
9895 /**
9896 * roaring_bitmap_smallest returns the smallest value in the set.
9897 * Returns UINT32_MAX if the set is empty.
9898 */
9899 uint32_t roaring_bitmap_minimum(const roaring_bitmap_t *bm) {
9900 if (bm->high_low_container.size > 0) {
9901 void *container = bm->high_low_container.containers[0];
9902 uint8_t typecode = bm->high_low_container.typecodes[0];
9903 uint32_t key = bm->high_low_container.keys[0];
9904 uint32_t lowvalue = container_minimum(container, typecode);
9905 return lowvalue | (key << 16);
9906 }
9907 return UINT32_MAX;
9908 }
9909
9910 /**
9911 * roaring_bitmap_smallest returns the greatest value in the set.
9912 * Returns 0 if the set is empty.
9913 */
9914 uint32_t roaring_bitmap_maximum(const roaring_bitmap_t *bm) {
9915 if (bm->high_low_container.size > 0) {
9916 void *container =
9917 bm->high_low_container.containers[bm->high_low_container.size - 1];
9918 uint8_t typecode =
9919 bm->high_low_container.typecodes[bm->high_low_container.size - 1];
9920 uint32_t key =
9921 bm->high_low_container.keys[bm->high_low_container.size - 1];
9922 uint32_t lowvalue = container_maximum(container, typecode);
9923 return lowvalue | (key << 16);
9924 }
9925 return 0;
9926 }
9927
9928 bool roaring_bitmap_select(const roaring_bitmap_t *bm, uint32_t rank,
9929 uint32_t *element) {
9930 void *container;
9931 uint8_t typecode;
9932 uint16_t key;
9933 uint32_t start_rank = 0;
9934 int i = 0;
9935 bool valid = false;
9936 while (!valid && i < bm->high_low_container.size) {
9937 container = bm->high_low_container.containers[i];
9938 typecode = bm->high_low_container.typecodes[i];
9939 valid =
9940 container_select(container, typecode, &start_rank, rank, element);
9941 i++;
9942 }
9943
9944 if (valid) {
9945 key = bm->high_low_container.keys[i - 1];
9946 *element |= (key << 16);
9947 return true;
9948 } else
9949 return false;
9950 }
9951
9952 bool roaring_bitmap_intersect(const roaring_bitmap_t *x1,
9953 const roaring_bitmap_t *x2) {
9954 const int length1 = x1->high_low_container.size,
9955 length2 = x2->high_low_container.size;
9956 uint64_t answer = 0;
9957 int pos1 = 0, pos2 = 0;
9958
9959 while (pos1 < length1 && pos2 < length2) {
9960 const uint16_t s1 = ra_get_key_at_index(& x1->high_low_container, pos1);
9961 const uint16_t s2 = ra_get_key_at_index(& x2->high_low_container, pos2);
9962
9963 if (s1 == s2) {
9964 uint8_t container_type_1, container_type_2;
9965 void *c1 = ra_get_container_at_index(& x1->high_low_container, pos1,
9966 &container_type_1);
9967 void *c2 = ra_get_container_at_index(& x2->high_low_container, pos2,
9968 &container_type_2);
9969 if( container_intersect(c1, container_type_1, c2, container_type_2) ) return true;
9970 ++pos1;
9971 ++pos2;
9972 } else if (s1 < s2) { // s1 < s2
9973 pos1 = ra_advance_until(& x1->high_low_container, s2, pos1);
9974 } else { // s1 > s2
9975 pos2 = ra_advance_until(& x2->high_low_container, s1, pos2);
9976 }
9977 }
9978 return answer;
9979 }
9980
9981
9982 uint64_t roaring_bitmap_and_cardinality(const roaring_bitmap_t *x1,
9983 const roaring_bitmap_t *x2) {
9984 const int length1 = x1->high_low_container.size,
9985 length2 = x2->high_low_container.size;
9986 uint64_t answer = 0;
9987 int pos1 = 0, pos2 = 0;
9988
9989 while (pos1 < length1 && pos2 < length2) {
9990 const uint16_t s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
9991 const uint16_t s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
9992
9993 if (s1 == s2) {
9994 uint8_t container_type_1, container_type_2;
9995 void *c1 = ra_get_container_at_index(&x1->high_low_container, pos1,
9996 &container_type_1);
9997 void *c2 = ra_get_container_at_index(&x2->high_low_container, pos2,
9998 &container_type_2);
9999 answer += container_and_cardinality(c1, container_type_1, c2,
10000 container_type_2);
10001 ++pos1;
10002 ++pos2;
10003 } else if (s1 < s2) { // s1 < s2
10004 pos1 = ra_advance_until(&x1->high_low_container, s2, pos1);
10005 } else { // s1 > s2
10006 pos2 = ra_advance_until(&x2->high_low_container, s1, pos2);
10007 }
10008 }
10009 return answer;
10010 }
10011
10012 double roaring_bitmap_jaccard_index(const roaring_bitmap_t *x1,
10013 const roaring_bitmap_t *x2) {
10014 const uint64_t c1 = roaring_bitmap_get_cardinality(x1);
10015 const uint64_t c2 = roaring_bitmap_get_cardinality(x2);
10016 const uint64_t inter = roaring_bitmap_and_cardinality(x1, x2);
10017 return (double)inter / (double)(c1 + c2 - inter);
10018 }
10019
10020 uint64_t roaring_bitmap_or_cardinality(const roaring_bitmap_t *x1,
10021 const roaring_bitmap_t *x2) {
10022 const uint64_t c1 = roaring_bitmap_get_cardinality(x1);
10023 const uint64_t c2 = roaring_bitmap_get_cardinality(x2);
10024 const uint64_t inter = roaring_bitmap_and_cardinality(x1, x2);
10025 return c1 + c2 - inter;
10026 }
10027
10028 uint64_t roaring_bitmap_andnot_cardinality(const roaring_bitmap_t *x1,
10029 const roaring_bitmap_t *x2) {
10030 const uint64_t c1 = roaring_bitmap_get_cardinality(x1);
10031 const uint64_t inter = roaring_bitmap_and_cardinality(x1, x2);
10032 return c1 - inter;
10033 }
10034
10035 uint64_t roaring_bitmap_xor_cardinality(const roaring_bitmap_t *x1,
10036 const roaring_bitmap_t *x2) {
10037 const uint64_t c1 = roaring_bitmap_get_cardinality(x1);
10038 const uint64_t c2 = roaring_bitmap_get_cardinality(x2);
10039 const uint64_t inter = roaring_bitmap_and_cardinality(x1, x2);
10040 return c1 + c2 - 2 * inter;
10041 }
10042
10043
10044 /**
10045 * Check whether a range of values from range_start (included) to range_end (excluded) is present
10046 */
10047 bool roaring_bitmap_contains_range(const roaring_bitmap_t *r, uint64_t range_start, uint64_t range_end) {
10048 if(range_end >= UINT64_C(0x100000000)) {
10049 range_end = UINT64_C(0x100000000);
10050 }
10051 if (range_start >= range_end) return true; // empty range are always contained!
10052 if (range_end - range_start == 1) return roaring_bitmap_contains(r, (uint32_t)range_start);
10053 uint16_t hb_rs = (uint16_t)(range_start >> 16);
10054 uint16_t hb_re = (uint16_t)((range_end - 1) >> 16);
10055 const int32_t span = hb_re - hb_rs;
10056 const int32_t hlc_sz = ra_get_size(&r->high_low_container);
10057 if (hlc_sz < span + 1) {
10058 return false;
10059 }
10060 int32_t is = ra_get_index(&r->high_low_container, hb_rs);
10061 int32_t ie = ra_get_index(&r->high_low_container, hb_re);
10062 ie = (ie < 0 ? -ie - 1 : ie);
10063 if ((is < 0) || ((ie - is) != span)) {
10064 return false;
10065 }
10066 const uint32_t lb_rs = range_start & 0xFFFF;
10067 const uint32_t lb_re = ((range_end - 1) & 0xFFFF) + 1;
10068 uint8_t typecode;
10069 void *container = ra_get_container_at_index(&r->high_low_container, is, &typecode);
10070 if (hb_rs == hb_re) {
10071 return container_contains_range(container, lb_rs, lb_re, typecode);
10072 }
10073 if (!container_contains_range(container, lb_rs, 1 << 16, typecode)) {
10074 return false;
10075 }
10076 assert(ie < hlc_sz); // would indicate an algorithmic bug
10077 container = ra_get_container_at_index(&r->high_low_container, ie, &typecode);
10078 if (!container_contains_range(container, 0, lb_re, typecode)) {
10079 return false;
10080 }
10081 for (int32_t i = is + 1; i < ie; ++i) {
10082 container = ra_get_container_at_index(&r->high_low_container, i, &typecode);
10083 if (!container_is_full(container, typecode) ) {
10084 return false;
10085 }
10086 }
10087 return true;
10088 }
10089
10090
10091 bool roaring_bitmap_is_strict_subset(const roaring_bitmap_t *ra1,
10092 const roaring_bitmap_t *ra2) {
10093 return (roaring_bitmap_get_cardinality(ra2) >
10094 roaring_bitmap_get_cardinality(ra1) &&
10095 roaring_bitmap_is_subset(ra1, ra2));
10096 }
10097
10098
10099 /*
10100 * FROZEN SERIALIZATION FORMAT DESCRIPTION
10101 *
10102 * -- (beginning must be aligned by 32 bytes) --
10103 * <bitset_data> uint64_t[BITSET_CONTAINER_SIZE_IN_WORDS * num_bitset_containers]
10104 * <run_data> rle16_t[total number of rle elements in all run containers]
10105 * <array_data> uint16_t[total number of array elements in all array containers]
10106 * <keys> uint16_t[num_containers]
10107 * <counts> uint16_t[num_containers]
10108 * <typecodes> uint8_t[num_containers]
10109 * <header> uint32_t
10110 *
10111 * <header> is a 4-byte value which is a bit union of FROZEN_COOKIE (15 bits)
10112 * and the number of containers (17 bits).
10113 *
10114 * <counts> stores number of elements for every container.
10115 * Its meaning depends on container type.
10116 * For array and bitset containers, this value is the container cardinality minus one.
10117 * For run container, it is the number of rle_t elements (n_runs).
10118 *
10119 * <bitset_data>,<array_data>,<run_data> are flat arrays of elements of
10120 * all containers of respective type.
10121 *
10122 * <*_data> and <keys> are kept close together because they are not accessed
10123 * during deserilization. This may reduce IO in case of large mapped bitmaps.
10124 * All members have their native alignments during deserilization except <header>,
10125 * which is not guaranteed to be aligned by 4 bytes.
10126 */
10127
10128 size_t roaring_bitmap_frozen_size_in_bytes(const roaring_bitmap_t *rb) {
10129 const roaring_array_t *ra = &rb->high_low_container;
10130 size_t num_bytes = 0;
10131 for (int32_t i = 0; i < ra->size; i++) {
10132 switch (ra->typecodes[i]) {
10133 case BITSET_CONTAINER_TYPE_CODE: {
10134 num_bytes += BITSET_CONTAINER_SIZE_IN_WORDS * sizeof(uint64_t);
10135 break;
10136 }
10137 case RUN_CONTAINER_TYPE_CODE: {
10138 const run_container_t *run =
10139 (const run_container_t *) ra->containers[i];
10140 num_bytes += run->n_runs * sizeof(rle16_t);
10141 break;
10142 }
10143 case ARRAY_CONTAINER_TYPE_CODE: {
10144 const array_container_t *array =
10145 (const array_container_t *) ra->containers[i];
10146 num_bytes += array->cardinality * sizeof(uint16_t);
10147 break;
10148 }
10149 default:
10150 __builtin_unreachable();
10151 }
10152 }
10153 num_bytes += (2 + 2 + 1) * ra->size; // keys, counts, typecodes
10154 num_bytes += 4; // header
10155 return num_bytes;
10156 }
10157
10158 inline static void *arena_alloc(char **arena, size_t num_bytes) {
10159 char *res = *arena;
10160 *arena += num_bytes;
10161 return res;
10162 }
10163
10164 void roaring_bitmap_frozen_serialize(const roaring_bitmap_t *rb, char *buf) {
10165 /*
10166 * Note: we do not require user to supply spicificly aligned buffer.
10167 * Thus we have to use memcpy() everywhere.
10168 */
10169
10170 const roaring_array_t *ra = &rb->high_low_container;
10171
10172 size_t bitset_zone_size = 0;
10173 size_t run_zone_size = 0;
10174 size_t array_zone_size = 0;
10175 for (int32_t i = 0; i < ra->size; i++) {
10176 switch (ra->typecodes[i]) {
10177 case BITSET_CONTAINER_TYPE_CODE: {
10178 bitset_zone_size +=
10179 BITSET_CONTAINER_SIZE_IN_WORDS * sizeof(uint64_t);
10180 break;
10181 }
10182 case RUN_CONTAINER_TYPE_CODE: {
10183 const run_container_t *run =
10184 (const run_container_t *) ra->containers[i];
10185 run_zone_size += run->n_runs * sizeof(rle16_t);
10186 break;
10187 }
10188 case ARRAY_CONTAINER_TYPE_CODE: {
10189 const array_container_t *array =
10190 (const array_container_t *) ra->containers[i];
10191 array_zone_size += array->cardinality * sizeof(uint16_t);
10192 break;
10193 }
10194 default:
10195 __builtin_unreachable();
10196 }
10197 }
10198
10199 uint64_t *bitset_zone = (uint64_t *)arena_alloc(&buf, bitset_zone_size);
10200 rle16_t *run_zone = (rle16_t *)arena_alloc(&buf, run_zone_size);
10201 uint16_t *array_zone = (uint16_t *)arena_alloc(&buf, array_zone_size);
10202 uint16_t *key_zone = (uint16_t *)arena_alloc(&buf, 2*ra->size);
10203 uint16_t *count_zone = (uint16_t *)arena_alloc(&buf, 2*ra->size);
10204 uint8_t *typecode_zone = (uint8_t *)arena_alloc(&buf, ra->size);
10205 uint32_t *header_zone = (uint32_t *)arena_alloc(&buf, 4);
10206
10207 for (int32_t i = 0; i < ra->size; i++) {
10208 uint16_t count;
10209 switch (ra->typecodes[i]) {
10210 case BITSET_CONTAINER_TYPE_CODE: {
10211 const bitset_container_t *bitset =
10212 (const bitset_container_t *) ra->containers[i];
10213 memcpy(bitset_zone, bitset->array,
10214 BITSET_CONTAINER_SIZE_IN_WORDS * sizeof(uint64_t));
10215 bitset_zone += BITSET_CONTAINER_SIZE_IN_WORDS;
10216 if (bitset->cardinality != BITSET_UNKNOWN_CARDINALITY) {
10217 count = bitset->cardinality - 1;
10218 } else {
10219 count = bitset_container_compute_cardinality(bitset) - 1;
10220 }
10221 break;
10222 }
10223 case RUN_CONTAINER_TYPE_CODE: {
10224 const run_container_t *run =
10225 (const run_container_t *) ra->containers[i];
10226 size_t num_bytes = run->n_runs * sizeof(rle16_t);
10227 memcpy(run_zone, run->runs, num_bytes);
10228 run_zone += run->n_runs;
10229 count = run->n_runs;
10230 break;
10231 }
10232 case ARRAY_CONTAINER_TYPE_CODE: {
10233 const array_container_t *array =
10234 (const array_container_t *) ra->containers[i];
10235 size_t num_bytes = array->cardinality * sizeof(uint16_t);
10236 memcpy(array_zone, array->array, num_bytes);
10237 array_zone += array->cardinality;
10238 count = array->cardinality - 1;
10239 break;
10240 }
10241 default:
10242 __builtin_unreachable();
10243 }
10244 memcpy(&count_zone[i], &count, 2);
10245 }
10246 memcpy(key_zone, ra->keys, ra->size * sizeof(uint16_t));
10247 memcpy(typecode_zone, ra->typecodes, ra->size * sizeof(uint8_t));
10248 uint32_t header = ((uint32_t)ra->size << 15) | FROZEN_COOKIE;
10249 memcpy(header_zone, &header, 4);
10250 }
10251
10252 const roaring_bitmap_t *
10253 roaring_bitmap_frozen_view(const char *buf, size_t length) {
10254 if ((uintptr_t)buf % 32 != 0) {
10255 return NULL;
10256 }
10257
10258 // cookie and num_containers
10259 if (length < 4) {
10260 return NULL;
10261 }
10262 uint32_t header;
10263 memcpy(&header, buf + length - 4, 4); // header may be misaligned
10264 if ((header & 0x7FFF) != FROZEN_COOKIE) {
10265 return NULL;
10266 }
10267 int32_t num_containers = (header >> 15);
10268
10269 // typecodes, counts and keys
10270 if (length < 4 + (size_t)num_containers * (1 + 2 + 2)) {
10271 return NULL;
10272 }
10273 uint16_t *keys = (uint16_t *)(buf + length - 4 - num_containers * 5);
10274 uint16_t *counts = (uint16_t *)(buf + length - 4 - num_containers * 3);
10275 uint8_t *typecodes = (uint8_t *)(buf + length - 4 - num_containers * 1);
10276
10277 // {bitset,array,run}_zone
10278 int32_t num_bitset_containers = 0;
10279 int32_t num_run_containers = 0;
10280 int32_t num_array_containers = 0;
10281 size_t bitset_zone_size = 0;
10282 size_t run_zone_size = 0;
10283 size_t array_zone_size = 0;
10284 for (int32_t i = 0; i < num_containers; i++) {
10285 switch (typecodes[i]) {
10286 case BITSET_CONTAINER_TYPE_CODE:
10287 num_bitset_containers++;
10288 bitset_zone_size += BITSET_CONTAINER_SIZE_IN_WORDS * sizeof(uint64_t);
10289 break;
10290 case RUN_CONTAINER_TYPE_CODE:
10291 num_run_containers++;
10292 run_zone_size += counts[i] * sizeof(rle16_t);
10293 break;
10294 case ARRAY_CONTAINER_TYPE_CODE:
10295 num_array_containers++;
10296 array_zone_size += (counts[i] + UINT32_C(1)) * sizeof(uint16_t);
10297 break;
10298 default:
10299 return NULL;
10300 }
10301 }
10302 if (length != bitset_zone_size + run_zone_size + array_zone_size +
10303 5 * num_containers + 4) {
10304 return NULL;
10305 }
10306 uint64_t *bitset_zone = (uint64_t*) (buf);
10307 rle16_t *run_zone = (rle16_t*) (buf + bitset_zone_size);
10308 uint16_t *array_zone = (uint16_t*) (buf + bitset_zone_size + run_zone_size);
10309
10310 size_t alloc_size = 0;
10311 alloc_size += sizeof(roaring_bitmap_t);
10312 alloc_size += num_containers * sizeof(void *);
10313 alloc_size += num_bitset_containers * sizeof(bitset_container_t);
10314 alloc_size += num_run_containers * sizeof(run_container_t);
10315 alloc_size += num_array_containers * sizeof(array_container_t);
10316
10317 char *arena = (char *)malloc(alloc_size);
10318 if (arena == NULL) {
10319 return NULL;
10320 }
10321
10322 roaring_bitmap_t *rb = (roaring_bitmap_t *)
10323 arena_alloc(&arena, sizeof(roaring_bitmap_t));
10324 rb->high_low_container.flags = ROARING_FLAG_FROZEN;
10325 rb->high_low_container.allocation_size = num_containers;
10326 rb->high_low_container.size = num_containers;
10327 rb->high_low_container.keys = (uint16_t *)keys;
10328 rb->high_low_container.typecodes = (uint8_t *)typecodes;
10329 rb->high_low_container.containers =
10330 (void **)arena_alloc(&arena, sizeof(void*) * num_containers);
10331 for (int32_t i = 0; i < num_containers; i++) {
10332 switch (typecodes[i]) {
10333 case BITSET_CONTAINER_TYPE_CODE: {
10334 bitset_container_t *bitset = (bitset_container_t *)
10335 arena_alloc(&arena, sizeof(bitset_container_t));
10336 bitset->array = bitset_zone;
10337 bitset->cardinality = counts[i] + UINT32_C(1);
10338 rb->high_low_container.containers[i] = bitset;
10339 bitset_zone += BITSET_CONTAINER_SIZE_IN_WORDS;
10340 break;
10341 }
10342 case RUN_CONTAINER_TYPE_CODE: {
10343 run_container_t *run = (run_container_t *)
10344 arena_alloc(&arena, sizeof(run_container_t));
10345 run->capacity = counts[i];
10346 run->n_runs = counts[i];
10347 run->runs = run_zone;
10348 rb->high_low_container.containers[i] = run;
10349 run_zone += run->n_runs;
10350 break;
10351 }
10352 case ARRAY_CONTAINER_TYPE_CODE: {
10353 array_container_t *array = (array_container_t *)
10354 arena_alloc(&arena, sizeof(array_container_t));
10355 array->capacity = counts[i] + UINT32_C(1);
10356 array->cardinality = counts[i] + UINT32_C(1);
10357 array->array = array_zone;
10358 rb->high_low_container.containers[i] = array;
10359 array_zone += counts[i] + UINT32_C(1);
10360 break;
10361 }
10362 default:
10363 free(arena);
10364 return NULL;
10365 }
10366 }
10367
10368 return rb;
10369 }
10370 /* end file src/roaring.c */
10371 /* begin file src/roaring_array.c */
10372 #include <assert.h>
10373 #include <stdbool.h>
10374 #include <stdio.h>
10375 #include <stdlib.h>
10376 #include <string.h>
10377 #include <inttypes.h>
10378
10379
10380 // Convention: [0,ra->size) all elements are initialized
10381 // [ra->size, ra->allocation_size) is junk and contains nothing needing freeing
10382
10383 static bool realloc_array(roaring_array_t *ra, int32_t new_capacity) {
10384 // because we combine the allocations, it is not possible to use realloc
10385 /*ra->keys =
10386 (uint16_t *)realloc(ra->keys, sizeof(uint16_t) * new_capacity);
10387 ra->containers =
10388 (void **)realloc(ra->containers, sizeof(void *) * new_capacity);
10389 ra->typecodes =
10390 (uint8_t *)realloc(ra->typecodes, sizeof(uint8_t) * new_capacity);
10391 if (!ra->keys || !ra->containers || !ra->typecodes) {
10392 free(ra->keys);
10393 free(ra->containers);
10394 free(ra->typecodes);
10395 return false;
10396 }*/
10397
10398 if ( new_capacity == 0 ) {
10399 free(ra->containers);
10400 ra->containers = NULL;
10401 ra->keys = NULL;
10402 ra->typecodes = NULL;
10403 ra->allocation_size = 0;
10404 return true;
10405 }
10406 const size_t memoryneeded =
10407 new_capacity * (sizeof(uint16_t) + sizeof(void *) + sizeof(uint8_t));
10408 void *bigalloc = malloc(memoryneeded);
10409 if (!bigalloc) return false;
10410 void *oldbigalloc = ra->containers;
10411 void **newcontainers = (void **)bigalloc;
10412 uint16_t *newkeys = (uint16_t *)(newcontainers + new_capacity);
10413 uint8_t *newtypecodes = (uint8_t *)(newkeys + new_capacity);
10414 assert((char *)(newtypecodes + new_capacity) ==
10415 (char *)bigalloc + memoryneeded);
10416 if(ra->size > 0) {
10417 memcpy(newcontainers, ra->containers, sizeof(void *) * ra->size);
10418 memcpy(newkeys, ra->keys, sizeof(uint16_t) * ra->size);
10419 memcpy(newtypecodes, ra->typecodes, sizeof(uint8_t) * ra->size);
10420 }
10421 ra->containers = newcontainers;
10422 ra->keys = newkeys;
10423 ra->typecodes = newtypecodes;
10424 ra->allocation_size = new_capacity;
10425 free(oldbigalloc);
10426 return true;
10427 }
10428
10429 bool ra_init_with_capacity(roaring_array_t *new_ra, uint32_t cap) {
10430 if (!new_ra) return false;
10431 ra_init(new_ra);
10432
10433 if (cap > INT32_MAX) { return false; }
10434
10435 if(cap > 0) {
10436 void *bigalloc =
10437 malloc(cap * (sizeof(uint16_t) + sizeof(void *) + sizeof(uint8_t)));
10438 if( bigalloc == NULL ) return false;
10439 new_ra->containers = (void **)bigalloc;
10440 new_ra->keys = (uint16_t *)(new_ra->containers + cap);
10441 new_ra->typecodes = (uint8_t *)(new_ra->keys + cap);
10442 // Narrowing is safe because of above check
10443 new_ra->allocation_size = (int32_t)cap;
10444 }
10445 return true;
10446 }
10447
10448 int ra_shrink_to_fit(roaring_array_t *ra) {
10449 int savings = (ra->allocation_size - ra->size) *
10450 (sizeof(uint16_t) + sizeof(void *) + sizeof(uint8_t));
10451 if (!realloc_array(ra, ra->size)) {
10452 return 0;
10453 }
10454 ra->allocation_size = ra->size;
10455 return savings;
10456 }
10457
10458 void ra_init(roaring_array_t *new_ra) {
10459 if (!new_ra) { return; }
10460 new_ra->keys = NULL;
10461 new_ra->containers = NULL;
10462 new_ra->typecodes = NULL;
10463
10464 new_ra->allocation_size = 0;
10465 new_ra->size = 0;
10466 new_ra->flags = 0;
10467 }
10468
10469 bool ra_copy(const roaring_array_t *source, roaring_array_t *dest,
10470 bool copy_on_write) {
10471 if (!ra_init_with_capacity(dest, source->size)) return false;
10472 dest->size = source->size;
10473 dest->allocation_size = source->size;
10474 if(dest->size > 0) {
10475 memcpy(dest->keys, source->keys, dest->size * sizeof(uint16_t));
10476 }
10477 // we go through the containers, turning them into shared containers...
10478 if (copy_on_write) {
10479 for (int32_t i = 0; i < dest->size; ++i) {
10480 source->containers[i] = get_copy_of_container(
10481 source->containers[i], &source->typecodes[i], copy_on_write);
10482 }
10483 // we do a shallow copy to the other bitmap
10484 if(dest->size > 0) {
10485 memcpy(dest->containers, source->containers,
10486 dest->size * sizeof(void *));
10487 memcpy(dest->typecodes, source->typecodes,
10488 dest->size * sizeof(uint8_t));
10489 }
10490 } else {
10491 if(dest->size > 0) {
10492 memcpy(dest->typecodes, source->typecodes,
10493 dest->size * sizeof(uint8_t));
10494 }
10495 for (int32_t i = 0; i < dest->size; i++) {
10496 dest->containers[i] =
10497 container_clone(source->containers[i], source->typecodes[i]);
10498 if (dest->containers[i] == NULL) {
10499 for (int32_t j = 0; j < i; j++) {
10500 container_free(dest->containers[j], dest->typecodes[j]);
10501 }
10502 ra_clear_without_containers(dest);
10503 return false;
10504 }
10505 }
10506 }
10507 return true;
10508 }
10509
10510 bool ra_overwrite(const roaring_array_t *source, roaring_array_t *dest,
10511 bool copy_on_write) {
10512 ra_clear_containers(dest); // we are going to overwrite them
10513 if (dest->allocation_size < source->size) {
10514 if (!realloc_array(dest, source->size)) {
10515 return false;
10516 }
10517 }
10518 dest->size = source->size;
10519 memcpy(dest->keys, source->keys, dest->size * sizeof(uint16_t));
10520 // we go through the containers, turning them into shared containers...
10521 if (copy_on_write) {
10522 for (int32_t i = 0; i < dest->size; ++i) {
10523 source->containers[i] = get_copy_of_container(
10524 source->containers[i], &source->typecodes[i], copy_on_write);
10525 }
10526 // we do a shallow copy to the other bitmap
10527 memcpy(dest->containers, source->containers,
10528 dest->size * sizeof(void *));
10529 memcpy(dest->typecodes, source->typecodes,
10530 dest->size * sizeof(uint8_t));
10531 } else {
10532 memcpy(dest->typecodes, source->typecodes,
10533 dest->size * sizeof(uint8_t));
10534 for (int32_t i = 0; i < dest->size; i++) {
10535 dest->containers[i] =
10536 container_clone(source->containers[i], source->typecodes[i]);
10537 if (dest->containers[i] == NULL) {
10538 for (int32_t j = 0; j < i; j++) {
10539 container_free(dest->containers[j], dest->typecodes[j]);
10540 }
10541 ra_clear_without_containers(dest);
10542 return false;
10543 }
10544 }
10545 }
10546 return true;
10547 }
10548
10549 void ra_clear_containers(roaring_array_t *ra) {
10550 for (int32_t i = 0; i < ra->size; ++i) {
10551 container_free(ra->containers[i], ra->typecodes[i]);
10552 }
10553 }
10554
10555 void ra_reset(roaring_array_t *ra) {
10556 ra_clear_containers(ra);
10557 ra->size = 0;
10558 ra_shrink_to_fit(ra);
10559 }
10560
10561 void ra_clear_without_containers(roaring_array_t *ra) {
10562 free(ra->containers); // keys and typecodes are allocated with containers
10563 ra->size = 0;
10564 ra->allocation_size = 0;
10565 ra->containers = NULL;
10566 ra->keys = NULL;
10567 ra->typecodes = NULL;
10568 }
10569
10570 void ra_clear(roaring_array_t *ra) {
10571 ra_clear_containers(ra);
10572 ra_clear_without_containers(ra);
10573 }
10574
10575 bool extend_array(roaring_array_t *ra, int32_t k) {
10576 int32_t desired_size = ra->size + k;
10577 assert(desired_size <= MAX_CONTAINERS);
10578 if (desired_size > ra->allocation_size) {
10579 int32_t new_capacity =
10580 (ra->size < 1024) ? 2 * desired_size : 5 * desired_size / 4;
10581 if (new_capacity > MAX_CONTAINERS) {
10582 new_capacity = MAX_CONTAINERS;
10583 }
10584
10585 return realloc_array(ra, new_capacity);
10586 }
10587 return true;
10588 }
10589
10590 void ra_append(roaring_array_t *ra, uint16_t key, void *container,
10591 uint8_t typecode) {
10592 extend_array(ra, 1);
10593 const int32_t pos = ra->size;
10594
10595 ra->keys[pos] = key;
10596 ra->containers[pos] = container;
10597 ra->typecodes[pos] = typecode;
10598 ra->size++;
10599 }
10600
10601 void ra_append_copy(roaring_array_t *ra, const roaring_array_t *sa,
10602 uint16_t index, bool copy_on_write) {
10603 extend_array(ra, 1);
10604 const int32_t pos = ra->size;
10605
10606 // old contents is junk not needing freeing
10607 ra->keys[pos] = sa->keys[index];
10608 // the shared container will be in two bitmaps
10609 if (copy_on_write) {
10610 sa->containers[index] = get_copy_of_container(
10611 sa->containers[index], &sa->typecodes[index], copy_on_write);
10612 ra->containers[pos] = sa->containers[index];
10613 ra->typecodes[pos] = sa->typecodes[index];
10614 } else {
10615 ra->containers[pos] =
10616 container_clone(sa->containers[index], sa->typecodes[index]);
10617 ra->typecodes[pos] = sa->typecodes[index];
10618 }
10619 ra->size++;
10620 }
10621
10622 void ra_append_copies_until(roaring_array_t *ra, const roaring_array_t *sa,
10623 uint16_t stopping_key, bool copy_on_write) {
10624 for (int32_t i = 0; i < sa->size; ++i) {
10625 if (sa->keys[i] >= stopping_key) break;
10626 ra_append_copy(ra, sa, i, copy_on_write);
10627 }
10628 }
10629
10630 void ra_append_copy_range(roaring_array_t *ra, const roaring_array_t *sa,
10631 int32_t start_index, int32_t end_index,
10632 bool copy_on_write) {
10633 extend_array(ra, end_index - start_index);
10634 for (int32_t i = start_index; i < end_index; ++i) {
10635 const int32_t pos = ra->size;
10636 ra->keys[pos] = sa->keys[i];
10637 if (copy_on_write) {
10638 sa->containers[i] = get_copy_of_container(
10639 sa->containers[i], &sa->typecodes[i], copy_on_write);
10640 ra->containers[pos] = sa->containers[i];
10641 ra->typecodes[pos] = sa->typecodes[i];
10642 } else {
10643 ra->containers[pos] =
10644 container_clone(sa->containers[i], sa->typecodes[i]);
10645 ra->typecodes[pos] = sa->typecodes[i];
10646 }
10647 ra->size++;
10648 }
10649 }
10650
10651 void ra_append_copies_after(roaring_array_t *ra, const roaring_array_t *sa,
10652 uint16_t before_start, bool copy_on_write) {
10653 int start_location = ra_get_index(sa, before_start);
10654 if (start_location >= 0)
10655 ++start_location;
10656 else
10657 start_location = -start_location - 1;
10658 ra_append_copy_range(ra, sa, start_location, sa->size, copy_on_write);
10659 }
10660
10661 void ra_append_move_range(roaring_array_t *ra, roaring_array_t *sa,
10662 int32_t start_index, int32_t end_index) {
10663 extend_array(ra, end_index - start_index);
10664
10665 for (int32_t i = start_index; i < end_index; ++i) {
10666 const int32_t pos = ra->size;
10667
10668 ra->keys[pos] = sa->keys[i];
10669 ra->containers[pos] = sa->containers[i];
10670 ra->typecodes[pos] = sa->typecodes[i];
10671 ra->size++;
10672 }
10673 }
10674
10675 void ra_append_range(roaring_array_t *ra, roaring_array_t *sa,
10676 int32_t start_index, int32_t end_index,
10677 bool copy_on_write) {
10678 extend_array(ra, end_index - start_index);
10679
10680 for (int32_t i = start_index; i < end_index; ++i) {
10681 const int32_t pos = ra->size;
10682 ra->keys[pos] = sa->keys[i];
10683 if (copy_on_write) {
10684 sa->containers[i] = get_copy_of_container(
10685 sa->containers[i], &sa->typecodes[i], copy_on_write);
10686 ra->containers[pos] = sa->containers[i];
10687 ra->typecodes[pos] = sa->typecodes[i];
10688 } else {
10689 ra->containers[pos] =
10690 container_clone(sa->containers[i], sa->typecodes[i]);
10691 ra->typecodes[pos] = sa->typecodes[i];
10692 }
10693 ra->size++;
10694 }
10695 }
10696
10697 uint16_t ra_get_key_at_index(const roaring_array_t *ra, uint16_t i) {
10698 return ra->keys[i];
10699 }
10700
10701 // everything skipped over is freed
10702 int32_t ra_advance_until_freeing(roaring_array_t *ra, uint16_t x, int32_t pos) {
10703 while (pos < ra->size && ra->keys[pos] < x) {
10704 container_free(ra->containers[pos], ra->typecodes[pos]);
10705 ++pos;
10706 }
10707 return pos;
10708 }
10709
10710 void ra_insert_new_key_value_at(roaring_array_t *ra, int32_t i, uint16_t key,
10711 void *container, uint8_t typecode) {
10712 extend_array(ra, 1);
10713 // May be an optimization opportunity with DIY memmove
10714 memmove(&(ra->keys[i + 1]), &(ra->keys[i]),
10715 sizeof(uint16_t) * (ra->size - i));
10716 memmove(&(ra->containers[i + 1]), &(ra->containers[i]),
10717 sizeof(void *) * (ra->size - i));
10718 memmove(&(ra->typecodes[i + 1]), &(ra->typecodes[i]),
10719 sizeof(uint8_t) * (ra->size - i));
10720 ra->keys[i] = key;
10721 ra->containers[i] = container;
10722 ra->typecodes[i] = typecode;
10723 ra->size++;
10724 }
10725
10726 // note: Java routine set things to 0, enabling GC.
10727 // Java called it "resize" but it was always used to downsize.
10728 // Allowing upsize would break the conventions about
10729 // valid containers below ra->size.
10730
10731 void ra_downsize(roaring_array_t *ra, int32_t new_length) {
10732 assert(new_length <= ra->size);
10733 ra->size = new_length;
10734 }
10735
10736 void ra_remove_at_index(roaring_array_t *ra, int32_t i) {
10737 memmove(&(ra->containers[i]), &(ra->containers[i + 1]),
10738 sizeof(void *) * (ra->size - i - 1));
10739 memmove(&(ra->keys[i]), &(ra->keys[i + 1]),
10740 sizeof(uint16_t) * (ra->size - i - 1));
10741 memmove(&(ra->typecodes[i]), &(ra->typecodes[i + 1]),
10742 sizeof(uint8_t) * (ra->size - i - 1));
10743 ra->size--;
10744 }
10745
10746 void ra_remove_at_index_and_free(roaring_array_t *ra, int32_t i) {
10747 container_free(ra->containers[i], ra->typecodes[i]);
10748 ra_remove_at_index(ra, i);
10749 }
10750
10751 // used in inplace andNot only, to slide left the containers from
10752 // the mutated RoaringBitmap that are after the largest container of
10753 // the argument RoaringBitmap. In use it should be followed by a call to
10754 // downsize.
10755 //
10756 void ra_copy_range(roaring_array_t *ra, uint32_t begin, uint32_t end,
10757 uint32_t new_begin) {
10758 assert(begin <= end);
10759 assert(new_begin < begin);
10760
10761 const int range = end - begin;
10762
10763 // We ensure to previously have freed overwritten containers
10764 // that are not copied elsewhere
10765
10766 memmove(&(ra->containers[new_begin]), &(ra->containers[begin]),
10767 sizeof(void *) * range);
10768 memmove(&(ra->keys[new_begin]), &(ra->keys[begin]),
10769 sizeof(uint16_t) * range);
10770 memmove(&(ra->typecodes[new_begin]), &(ra->typecodes[begin]),
10771 sizeof(uint8_t) * range);
10772 }
10773
10774 void ra_shift_tail(roaring_array_t *ra, int32_t count, int32_t distance) {
10775 if (distance > 0) {
10776 extend_array(ra, distance);
10777 }
10778 int32_t srcpos = ra->size - count;
10779 int32_t dstpos = srcpos + distance;
10780 memmove(&(ra->keys[dstpos]), &(ra->keys[srcpos]),
10781 sizeof(uint16_t) * count);
10782 memmove(&(ra->containers[dstpos]), &(ra->containers[srcpos]),
10783 sizeof(void *) * count);
10784 memmove(&(ra->typecodes[dstpos]), &(ra->typecodes[srcpos]),
10785 sizeof(uint8_t) * count);
10786 ra->size += distance;
10787 }
10788
10789
10790 void ra_to_uint32_array(const roaring_array_t *ra, uint32_t *ans) {
10791 size_t ctr = 0;
10792 for (int32_t i = 0; i < ra->size; ++i) {
10793 int num_added = container_to_uint32_array(
10794 ans + ctr, ra->containers[i], ra->typecodes[i],
10795 ((uint32_t)ra->keys[i]) << 16);
10796 ctr += num_added;
10797 }
10798 }
10799
10800 bool ra_range_uint32_array(const roaring_array_t *ra, size_t offset, size_t limit, uint32_t *ans) {
10801 size_t ctr = 0;
10802 size_t dtr = 0;
10803
10804 size_t t_limit = 0;
10805
10806 bool first = false;
10807 size_t first_skip = 0;
10808
10809 uint32_t *t_ans = NULL;
10810 size_t cur_len = 0;
10811
10812 for (int i = 0; i < ra->size; ++i) {
10813
10814 const void *container = container_unwrap_shared(ra->containers[i], &ra->typecodes[i]);
10815 switch (ra->typecodes[i]) {
10816 case BITSET_CONTAINER_TYPE_CODE:
10817 t_limit = ((const bitset_container_t *)container)->cardinality;
10818 break;
10819 case ARRAY_CONTAINER_TYPE_CODE:
10820 t_limit = ((const array_container_t *)container)->cardinality;
10821 break;
10822 case RUN_CONTAINER_TYPE_CODE:
10823 t_limit = run_container_cardinality((const run_container_t *)container);
10824 break;
10825 case SHARED_CONTAINER_TYPE_CODE:
10826 default:
10827 __builtin_unreachable();
10828 }
10829 if (ctr + t_limit - 1 >= offset && ctr < offset + limit){
10830 if (!first){
10831 //first_skip = t_limit - (ctr + t_limit - offset);
10832 first_skip = offset - ctr;
10833 first = true;
10834 t_ans = (uint32_t *)malloc(sizeof(*t_ans) * (first_skip + limit));
10835 if(t_ans == NULL) {
10836 return false;
10837 }
10838 memset(t_ans, 0, sizeof(*t_ans) * (first_skip + limit)) ;
10839 cur_len = first_skip + limit;
10840 }
10841 if (dtr + t_limit > cur_len){
10842 uint32_t * append_ans = (uint32_t *)malloc(sizeof(*append_ans) * (cur_len + t_limit));
10843 if(append_ans == NULL) {
10844 if(t_ans != NULL) free(t_ans);
10845 return false;
10846 }
10847 memset(append_ans, 0, sizeof(*append_ans) * (cur_len + t_limit));
10848 cur_len = cur_len + t_limit;
10849 memcpy(append_ans, t_ans, dtr * sizeof(uint32_t));
10850 free(t_ans);
10851 t_ans = append_ans;
10852 }
10853 switch (ra->typecodes[i]) {
10854 case BITSET_CONTAINER_TYPE_CODE:
10855 container_to_uint32_array(
10856 t_ans + dtr, (const bitset_container_t *)container, ra->typecodes[i],
10857 ((uint32_t)ra->keys[i]) << 16);
10858 break;
10859 case ARRAY_CONTAINER_TYPE_CODE:
10860 container_to_uint32_array(
10861 t_ans + dtr, (const array_container_t *)container, ra->typecodes[i],
10862 ((uint32_t)ra->keys[i]) << 16);
10863 break;
10864 case RUN_CONTAINER_TYPE_CODE:
10865 container_to_uint32_array(
10866 t_ans + dtr, (const run_container_t *)container, ra->typecodes[i],
10867 ((uint32_t)ra->keys[i]) << 16);
10868 break;
10869 case SHARED_CONTAINER_TYPE_CODE:
10870 default:
10871 __builtin_unreachable();
10872 }
10873 dtr += t_limit;
10874 }
10875 ctr += t_limit;
10876 if (dtr-first_skip >= limit) break;
10877 }
10878 if(t_ans != NULL) {
10879 memcpy(ans, t_ans+first_skip, limit * sizeof(uint32_t));
10880 free(t_ans);
10881 }
10882 return true;
10883 }
10884
10885 bool ra_has_run_container(const roaring_array_t *ra) {
10886 for (int32_t k = 0; k < ra->size; ++k) {
10887 if (get_container_type(ra->containers[k], ra->typecodes[k]) ==
10888 RUN_CONTAINER_TYPE_CODE)
10889 return true;
10890 }
10891 return false;
10892 }
10893
10894 uint32_t ra_portable_header_size(const roaring_array_t *ra) {
10895 if (ra_has_run_container(ra)) {
10896 if (ra->size <
10897 NO_OFFSET_THRESHOLD) { // for small bitmaps, we omit the offsets
10898 return 4 + (ra->size + 7) / 8 + 4 * ra->size;
10899 }
10900 return 4 + (ra->size + 7) / 8 +
10901 8 * ra->size; // - 4 because we pack the size with the cookie
10902 } else {
10903 return 4 + 4 + 8 * ra->size;
10904 }
10905 }
10906
10907 size_t ra_portable_size_in_bytes(const roaring_array_t *ra) {
10908 size_t count = ra_portable_header_size(ra);
10909
10910 for (int32_t k = 0; k < ra->size; ++k) {
10911 count += container_size_in_bytes(ra->containers[k], ra->typecodes[k]);
10912 }
10913 return count;
10914 }
10915
10916 size_t ra_portable_serialize(const roaring_array_t *ra, char *buf) {
10917 char *initbuf = buf;
10918 uint32_t startOffset = 0;
10919 bool hasrun = ra_has_run_container(ra);
10920 if (hasrun) {
10921 uint32_t cookie = SERIAL_COOKIE | ((ra->size - 1) << 16);
10922 memcpy(buf, &cookie, sizeof(cookie));
10923 buf += sizeof(cookie);
10924 uint32_t s = (ra->size + 7) / 8;
10925 uint8_t *bitmapOfRunContainers = (uint8_t *)calloc(s, 1);
10926 assert(bitmapOfRunContainers != NULL); // todo: handle
10927 for (int32_t i = 0; i < ra->size; ++i) {
10928 if (get_container_type(ra->containers[i], ra->typecodes[i]) ==
10929 RUN_CONTAINER_TYPE_CODE) {
10930 bitmapOfRunContainers[i / 8] |= (1 << (i % 8));
10931 }
10932 }
10933 memcpy(buf, bitmapOfRunContainers, s);
10934 buf += s;
10935 free(bitmapOfRunContainers);
10936 if (ra->size < NO_OFFSET_THRESHOLD) {
10937 startOffset = 4 + 4 * ra->size + s;
10938 } else {
10939 startOffset = 4 + 8 * ra->size + s;
10940 }
10941 } else { // backwards compatibility
10942 uint32_t cookie = SERIAL_COOKIE_NO_RUNCONTAINER;
10943
10944 memcpy(buf, &cookie, sizeof(cookie));
10945 buf += sizeof(cookie);
10946 memcpy(buf, &ra->size, sizeof(ra->size));
10947 buf += sizeof(ra->size);
10948
10949 startOffset = 4 + 4 + 4 * ra->size + 4 * ra->size;
10950 }
10951 for (int32_t k = 0; k < ra->size; ++k) {
10952 memcpy(buf, &ra->keys[k], sizeof(ra->keys[k]));
10953 buf += sizeof(ra->keys[k]);
10954 // get_cardinality returns a value in [1,1<<16], subtracting one
10955 // we get [0,1<<16 - 1] which fits in 16 bits
10956 uint16_t card = (uint16_t)(
10957 container_get_cardinality(ra->containers[k], ra->typecodes[k]) - 1);
10958 memcpy(buf, &card, sizeof(card));
10959 buf += sizeof(card);
10960 }
10961 if ((!hasrun) || (ra->size >= NO_OFFSET_THRESHOLD)) {
10962 // writing the containers offsets
10963 for (int32_t k = 0; k < ra->size; k++) {
10964 memcpy(buf, &startOffset, sizeof(startOffset));
10965 buf += sizeof(startOffset);
10966 startOffset =
10967 startOffset +
10968 container_size_in_bytes(ra->containers[k], ra->typecodes[k]);
10969 }
10970 }
10971 for (int32_t k = 0; k < ra->size; ++k) {
10972 buf += container_write(ra->containers[k], ra->typecodes[k], buf);
10973 }
10974 return buf - initbuf;
10975 }
10976
10977 // Quickly checks whether there is a serialized bitmap at the pointer,
10978 // not exceeding size "maxbytes" in bytes. This function does not allocate
10979 // memory dynamically.
10980 //
10981 // This function returns 0 if and only if no valid bitmap is found.
10982 // Otherwise, it returns how many bytes are occupied.
10983 //
10984 size_t ra_portable_deserialize_size(const char *buf, const size_t maxbytes) {
10985 size_t bytestotal = sizeof(int32_t);// for cookie
10986 if(bytestotal > maxbytes) return 0;
10987 uint32_t cookie;
10988 memcpy(&cookie, buf, sizeof(int32_t));
10989 buf += sizeof(uint32_t);
10990 if ((cookie & 0xFFFF) != SERIAL_COOKIE &&
10991 cookie != SERIAL_COOKIE_NO_RUNCONTAINER) {
10992 return 0;
10993 }
10994 int32_t size;
10995
10996 if ((cookie & 0xFFFF) == SERIAL_COOKIE)
10997 size = (cookie >> 16) + 1;
10998 else {
10999 bytestotal += sizeof(int32_t);
11000 if(bytestotal > maxbytes) return 0;
11001 memcpy(&size, buf, sizeof(int32_t));
11002 buf += sizeof(uint32_t);
11003 }
11004 if (size > (1<<16)) {
11005 return 0; // logically impossible
11006 }
11007 char *bitmapOfRunContainers = NULL;
11008 bool hasrun = (cookie & 0xFFFF) == SERIAL_COOKIE;
11009 if (hasrun) {
11010 int32_t s = (size + 7) / 8;
11011 bytestotal += s;
11012 if(bytestotal > maxbytes) return 0;
11013 bitmapOfRunContainers = (char *)buf;
11014 buf += s;
11015 }
11016 bytestotal += size * 2 * sizeof(uint16_t);
11017 if(bytestotal > maxbytes) return 0;
11018 uint16_t *keyscards = (uint16_t *)buf;
11019 buf += size * 2 * sizeof(uint16_t);
11020 if ((!hasrun) || (size >= NO_OFFSET_THRESHOLD)) {
11021 // skipping the offsets
11022 bytestotal += size * 4;
11023 if(bytestotal > maxbytes) return 0;
11024 buf += size * 4;
11025 }
11026 // Reading the containers
11027 for (int32_t k = 0; k < size; ++k) {
11028 uint16_t tmp;
11029 memcpy(&tmp, keyscards + 2*k+1, sizeof(tmp));
11030 uint32_t thiscard = tmp + 1;
11031 bool isbitmap = (thiscard > DEFAULT_MAX_SIZE);
11032 bool isrun = false;
11033 if(hasrun) {
11034 if((bitmapOfRunContainers[k / 8] & (1 << (k % 8))) != 0) {
11035 isbitmap = false;
11036 isrun = true;
11037 }
11038 }
11039 if (isbitmap) {
11040 size_t containersize = BITSET_CONTAINER_SIZE_IN_WORDS * sizeof(uint64_t);
11041 bytestotal += containersize;
11042 if(bytestotal > maxbytes) return 0;
11043 buf += containersize;
11044 } else if (isrun) {
11045 bytestotal += sizeof(uint16_t);
11046 if(bytestotal > maxbytes) return 0;
11047 uint16_t n_runs;
11048 memcpy(&n_runs, buf, sizeof(uint16_t));
11049 buf += sizeof(uint16_t);
11050 size_t containersize = n_runs * sizeof(rle16_t);
11051 bytestotal += containersize;
11052 if(bytestotal > maxbytes) return 0;
11053 buf += containersize;
11054 } else {
11055 size_t containersize = thiscard * sizeof(uint16_t);
11056 bytestotal += containersize;
11057 if(bytestotal > maxbytes) return 0;
11058 buf += containersize;
11059 }
11060 }
11061 return bytestotal;
11062 }
11063
11064
11065 // this function populates answer from the content of buf (reading up to maxbytes bytes).
11066 // The function returns false if a properly serialized bitmap cannot be found.
11067 // if it returns true, readbytes is populated by how many bytes were read, we have that *readbytes <= maxbytes.
11068 bool ra_portable_deserialize(roaring_array_t *answer, const char *buf, const size_t maxbytes, size_t * readbytes) {
11069 *readbytes = sizeof(int32_t);// for cookie
11070 if(*readbytes > maxbytes) {
11071 fprintf(stderr, "Ran out of bytes while reading first 4 bytes.\n");
11072 return false;
11073 }
11074 uint32_t cookie;
11075 memcpy(&cookie, buf, sizeof(int32_t));
11076 buf += sizeof(uint32_t);
11077 if ((cookie & 0xFFFF) != SERIAL_COOKIE &&
11078 cookie != SERIAL_COOKIE_NO_RUNCONTAINER) {
11079 fprintf(stderr, "I failed to find one of the right cookies. Found %" PRIu32 "\n",
11080 cookie);
11081 return false;
11082 }
11083 int32_t size;
11084
11085 if ((cookie & 0xFFFF) == SERIAL_COOKIE)
11086 size = (cookie >> 16) + 1;
11087 else {
11088 *readbytes += sizeof(int32_t);
11089 if(*readbytes > maxbytes) {
11090 fprintf(stderr, "Ran out of bytes while reading second part of the cookie.\n");
11091 return false;
11092 }
11093 memcpy(&size, buf, sizeof(int32_t));
11094 buf += sizeof(uint32_t);
11095 }
11096 if (size > (1<<16)) {
11097 fprintf(stderr, "You cannot have so many containers, the data must be corrupted: %" PRId32 "\n",
11098 size);
11099 return false; // logically impossible
11100 }
11101 const char *bitmapOfRunContainers = NULL;
11102 bool hasrun = (cookie & 0xFFFF) == SERIAL_COOKIE;
11103 if (hasrun) {
11104 int32_t s = (size + 7) / 8;
11105 *readbytes += s;
11106 if(*readbytes > maxbytes) {// data is corrupted?
11107 fprintf(stderr, "Ran out of bytes while reading run bitmap.\n");
11108 return false;
11109 }
11110 bitmapOfRunContainers = buf;
11111 buf += s;
11112 }
11113 uint16_t *keyscards = (uint16_t *)buf;
11114
11115 *readbytes += size * 2 * sizeof(uint16_t);
11116 if(*readbytes > maxbytes) {
11117 fprintf(stderr, "Ran out of bytes while reading key-cardinality array.\n");
11118 return false;
11119 }
11120 buf += size * 2 * sizeof(uint16_t);
11121
11122 bool is_ok = ra_init_with_capacity(answer, size);
11123 if (!is_ok) {
11124 fprintf(stderr, "Failed to allocate memory for roaring array. Bailing out.\n");
11125 return false;
11126 }
11127
11128 for (int32_t k = 0; k < size; ++k) {
11129 uint16_t tmp;
11130 memcpy(&tmp, keyscards + 2*k, sizeof(tmp));
11131 answer->keys[k] = tmp;
11132 }
11133 if ((!hasrun) || (size >= NO_OFFSET_THRESHOLD)) {
11134 *readbytes += size * 4;
11135 if(*readbytes > maxbytes) {// data is corrupted?
11136 fprintf(stderr, "Ran out of bytes while reading offsets.\n");
11137 ra_clear(answer);// we need to clear the containers already allocated, and the roaring array
11138 return false;
11139 }
11140
11141 // skipping the offsets
11142 buf += size * 4;
11143 }
11144 // Reading the containers
11145 for (int32_t k = 0; k < size; ++k) {
11146 uint16_t tmp;
11147 memcpy(&tmp, keyscards + 2*k+1, sizeof(tmp));
11148 uint32_t thiscard = tmp + 1;
11149 bool isbitmap = (thiscard > DEFAULT_MAX_SIZE);
11150 bool isrun = false;
11151 if(hasrun) {
11152 if((bitmapOfRunContainers[k / 8] & (1 << (k % 8))) != 0) {
11153 isbitmap = false;
11154 isrun = true;
11155 }
11156 }
11157 if (isbitmap) {
11158 // we check that the read is allowed
11159 size_t containersize = BITSET_CONTAINER_SIZE_IN_WORDS * sizeof(uint64_t);
11160 *readbytes += containersize;
11161 if(*readbytes > maxbytes) {
11162 fprintf(stderr, "Running out of bytes while reading a bitset container.\n");
11163 ra_clear(answer);// we need to clear the containers already allocated, and the roaring array
11164 return false;
11165 }
11166 // it is now safe to read
11167 bitset_container_t *c = bitset_container_create();
11168 if(c == NULL) {// memory allocation failure
11169 fprintf(stderr, "Failed to allocate memory for a bitset container.\n");
11170 ra_clear(answer);// we need to clear the containers already allocated, and the roaring array
11171 return false;
11172 }
11173 answer->size++;
11174 buf += bitset_container_read(thiscard, c, buf);
11175 answer->containers[k] = c;
11176 answer->typecodes[k] = BITSET_CONTAINER_TYPE_CODE;
11177 } else if (isrun) {
11178 // we check that the read is allowed
11179 *readbytes += sizeof(uint16_t);
11180 if(*readbytes > maxbytes) {
11181 fprintf(stderr, "Running out of bytes while reading a run container (header).\n");
11182 ra_clear(answer);// we need to clear the containers already allocated, and the roaring array
11183 return false;
11184 }
11185 uint16_t n_runs;
11186 memcpy(&n_runs, buf, sizeof(uint16_t));
11187 size_t containersize = n_runs * sizeof(rle16_t);
11188 *readbytes += containersize;
11189 if(*readbytes > maxbytes) {// data is corrupted?
11190 fprintf(stderr, "Running out of bytes while reading a run container.\n");
11191 ra_clear(answer);// we need to clear the containers already allocated, and the roaring array
11192 return false;
11193 }
11194 // it is now safe to read
11195
11196 run_container_t *c = run_container_create();
11197 if(c == NULL) {// memory allocation failure
11198 fprintf(stderr, "Failed to allocate memory for a run container.\n");
11199 ra_clear(answer);// we need to clear the containers already allocated, and the roaring array
11200 return false;
11201 }
11202 answer->size++;
11203 buf += run_container_read(thiscard, c, buf);
11204 answer->containers[k] = c;
11205 answer->typecodes[k] = RUN_CONTAINER_TYPE_CODE;
11206 } else {
11207 // we check that the read is allowed
11208 size_t containersize = thiscard * sizeof(uint16_t);
11209 *readbytes += containersize;
11210 if(*readbytes > maxbytes) {// data is corrupted?
11211 fprintf(stderr, "Running out of bytes while reading an array container.\n");
11212 ra_clear(answer);// we need to clear the containers already allocated, and the roaring array
11213 return false;
11214 }
11215 // it is now safe to read
11216 array_container_t *c =
11217 array_container_create_given_capacity(thiscard);
11218 if(c == NULL) {// memory allocation failure
11219 fprintf(stderr, "Failed to allocate memory for an array container.\n");
11220 ra_clear(answer);// we need to clear the containers already allocated, and the roaring array
11221 return false;
11222 }
11223 answer->size++;
11224 buf += array_container_read(thiscard, c, buf);
11225 answer->containers[k] = c;
11226 answer->typecodes[k] = ARRAY_CONTAINER_TYPE_CODE;
11227 }
11228 }
11229 return true;
11230 }
11231 /* end file src/roaring_array.c */
11232 /* begin file src/roaring_priority_queue.c */
11233
11234 struct roaring_pq_element_s {
11235 uint64_t size;
11236 bool is_temporary;
11237 roaring_bitmap_t *bitmap;
11238 };
11239
11240 typedef struct roaring_pq_element_s roaring_pq_element_t;
11241
11242 struct roaring_pq_s {
11243 roaring_pq_element_t *elements;
11244 uint64_t size;
11245 };
11246
11247 typedef struct roaring_pq_s roaring_pq_t;
11248
11249 static inline bool compare(roaring_pq_element_t *t1, roaring_pq_element_t *t2) {
11250 return t1->size < t2->size;
11251 }
11252
11253 static void pq_add(roaring_pq_t *pq, roaring_pq_element_t *t) {
11254 uint64_t i = pq->size;
11255 pq->elements[pq->size++] = *t;
11256 while (i > 0) {
11257 uint64_t p = (i - 1) >> 1;
11258 roaring_pq_element_t ap = pq->elements[p];
11259 if (!compare(t, &ap)) break;
11260 pq->elements[i] = ap;
11261 i = p;
11262 }
11263 pq->elements[i] = *t;
11264 }
11265
11266 static void pq_free(roaring_pq_t *pq) {
11267 free(pq->elements);
11268 pq->elements = NULL; // paranoid
11269 free(pq);
11270 }
11271
11272 static void percolate_down(roaring_pq_t *pq, uint32_t i) {
11273 uint32_t size = (uint32_t)pq->size;
11274 uint32_t hsize = size >> 1;
11275 roaring_pq_element_t ai = pq->elements[i];
11276 while (i < hsize) {
11277 uint32_t l = (i << 1) + 1;
11278 uint32_t r = l + 1;
11279 roaring_pq_element_t bestc = pq->elements[l];
11280 if (r < size) {
11281 if (compare(pq->elements + r, &bestc)) {
11282 l = r;
11283 bestc = pq->elements[r];
11284 }
11285 }
11286 if (!compare(&bestc, &ai)) {
11287 break;
11288 }
11289 pq->elements[i] = bestc;
11290 i = l;
11291 }
11292 pq->elements[i] = ai;
11293 }
11294
11295 static roaring_pq_t *create_pq(const roaring_bitmap_t **arr, uint32_t length) {
11296 roaring_pq_t *answer = (roaring_pq_t *)malloc(sizeof(roaring_pq_t));
11297 answer->elements =
11298 (roaring_pq_element_t *)malloc(sizeof(roaring_pq_element_t) * length);
11299 answer->size = length;
11300 for (uint32_t i = 0; i < length; i++) {
11301 answer->elements[i].bitmap = (roaring_bitmap_t *)arr[i];
11302 answer->elements[i].is_temporary = false;
11303 answer->elements[i].size =
11304 roaring_bitmap_portable_size_in_bytes(arr[i]);
11305 }
11306 for (int32_t i = (length >> 1); i >= 0; i--) {
11307 percolate_down(answer, i);
11308 }
11309 return answer;
11310 }
11311
11312 static roaring_pq_element_t pq_poll(roaring_pq_t *pq) {
11313 roaring_pq_element_t ans = *pq->elements;
11314 if (pq->size > 1) {
11315 pq->elements[0] = pq->elements[--pq->size];
11316 percolate_down(pq, 0);
11317 } else
11318 --pq->size;
11319 // memmove(pq->elements,pq->elements+1,(pq->size-1)*sizeof(roaring_pq_element_t));--pq->size;
11320 return ans;
11321 }
11322
11323 // this function consumes and frees the inputs
11324 static roaring_bitmap_t *lazy_or_from_lazy_inputs(roaring_bitmap_t *x1,
11325 roaring_bitmap_t *x2) {
11326 uint8_t container_result_type = 0;
11327 const int length1 = ra_get_size(&x1->high_low_container),
11328 length2 = ra_get_size(&x2->high_low_container);
11329 if (0 == length1) {
11330 roaring_bitmap_free(x1);
11331 return x2;
11332 }
11333 if (0 == length2) {
11334 roaring_bitmap_free(x2);
11335 return x1;
11336 }
11337 uint32_t neededcap = length1 > length2 ? length2 : length1;
11338 roaring_bitmap_t *answer = roaring_bitmap_create_with_capacity(neededcap);
11339 int pos1 = 0, pos2 = 0;
11340 uint8_t container_type_1, container_type_2;
11341 uint16_t s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
11342 uint16_t s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
11343 while (true) {
11344 if (s1 == s2) {
11345 // todo: unsharing can be inefficient as it may create a clone where
11346 // none
11347 // is needed, but it has the benefit of being easy to reason about.
11348 ra_unshare_container_at_index(&x1->high_low_container, pos1);
11349 void *c1 = ra_get_container_at_index(&x1->high_low_container, pos1,
11350 &container_type_1);
11351 assert(container_type_1 != SHARED_CONTAINER_TYPE_CODE);
11352 ra_unshare_container_at_index(&x2->high_low_container, pos2);
11353 void *c2 = ra_get_container_at_index(&x2->high_low_container, pos2,
11354 &container_type_2);
11355 assert(container_type_2 != SHARED_CONTAINER_TYPE_CODE);
11356 void *c;
11357
11358 if ((container_type_2 == BITSET_CONTAINER_TYPE_CODE) &&
11359 (container_type_1 != BITSET_CONTAINER_TYPE_CODE)) {
11360 c = container_lazy_ior(c2, container_type_2, c1,
11361 container_type_1,
11362 &container_result_type);
11363 container_free(c1, container_type_1);
11364 if (c != c2) {
11365 container_free(c2, container_type_2);
11366 }
11367 } else {
11368 c = container_lazy_ior(c1, container_type_1, c2,
11369 container_type_2,
11370 &container_result_type);
11371 container_free(c2, container_type_2);
11372 if (c != c1) {
11373 container_free(c1, container_type_1);
11374 }
11375 }
11376 // since we assume that the initial containers are non-empty, the
11377 // result here
11378 // can only be non-empty
11379 ra_append(&answer->high_low_container, s1, c,
11380 container_result_type);
11381 ++pos1;
11382 ++pos2;
11383 if (pos1 == length1) break;
11384 if (pos2 == length2) break;
11385 s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
11386 s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
11387
11388 } else if (s1 < s2) { // s1 < s2
11389 void *c1 = ra_get_container_at_index(&x1->high_low_container, pos1,
11390 &container_type_1);
11391 ra_append(&answer->high_low_container, s1, c1, container_type_1);
11392 pos1++;
11393 if (pos1 == length1) break;
11394 s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
11395
11396 } else { // s1 > s2
11397 void *c2 = ra_get_container_at_index(&x2->high_low_container, pos2,
11398 &container_type_2);
11399 ra_append(&answer->high_low_container, s2, c2, container_type_2);
11400 pos2++;
11401 if (pos2 == length2) break;
11402 s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
11403 }
11404 }
11405 if (pos1 == length1) {
11406 ra_append_move_range(&answer->high_low_container,
11407 &x2->high_low_container, pos2, length2);
11408 } else if (pos2 == length2) {
11409 ra_append_move_range(&answer->high_low_container,
11410 &x1->high_low_container, pos1, length1);
11411 }
11412 ra_clear_without_containers(&x1->high_low_container);
11413 ra_clear_without_containers(&x2->high_low_container);
11414 free(x1);
11415 free(x2);
11416 return answer;
11417 }
11418
11419 /**
11420 * Compute the union of 'number' bitmaps using a heap. This can
11421 * sometimes be faster than roaring_bitmap_or_many which uses
11422 * a naive algorithm. Caller is responsible for freeing the
11423 * result.
11424 */
11425 roaring_bitmap_t *roaring_bitmap_or_many_heap(uint32_t number,
11426 const roaring_bitmap_t **x) {
11427 if (number == 0) {
11428 return roaring_bitmap_create();
11429 }
11430 if (number == 1) {
11431 return roaring_bitmap_copy(x[0]);
11432 }
11433 roaring_pq_t *pq = create_pq(x, number);
11434 while (pq->size > 1) {
11435 roaring_pq_element_t x1 = pq_poll(pq);
11436 roaring_pq_element_t x2 = pq_poll(pq);
11437
11438 if (x1.is_temporary && x2.is_temporary) {
11439 roaring_bitmap_t *newb =
11440 lazy_or_from_lazy_inputs(x1.bitmap, x2.bitmap);
11441 // should normally return a fresh new bitmap *except* that
11442 // it can return x1.bitmap or x2.bitmap in degenerate cases
11443 bool temporary = !((newb == x1.bitmap) && (newb == x2.bitmap));
11444 uint64_t bsize = roaring_bitmap_portable_size_in_bytes(newb);
11445 roaring_pq_element_t newelement = {
11446 .size = bsize, .is_temporary = temporary, .bitmap = newb};
11447 pq_add(pq, &newelement);
11448 } else if (x2.is_temporary) {
11449 roaring_bitmap_lazy_or_inplace(x2.bitmap, x1.bitmap, false);
11450 x2.size = roaring_bitmap_portable_size_in_bytes(x2.bitmap);
11451 pq_add(pq, &x2);
11452 } else if (x1.is_temporary) {
11453 roaring_bitmap_lazy_or_inplace(x1.bitmap, x2.bitmap, false);
11454 x1.size = roaring_bitmap_portable_size_in_bytes(x1.bitmap);
11455
11456 pq_add(pq, &x1);
11457 } else {
11458 roaring_bitmap_t *newb =
11459 roaring_bitmap_lazy_or(x1.bitmap, x2.bitmap, false);
11460 uint64_t bsize = roaring_bitmap_portable_size_in_bytes(newb);
11461 roaring_pq_element_t newelement = {
11462 .size = bsize, .is_temporary = true, .bitmap = newb};
11463
11464 pq_add(pq, &newelement);
11465 }
11466 }
11467 roaring_pq_element_t X = pq_poll(pq);
11468 roaring_bitmap_t *answer = X.bitmap;
11469 roaring_bitmap_repair_after_lazy(answer);
11470 pq_free(pq);
11471 return answer;
11472 }
11473 /* end file src/roaring_priority_queue.c */
11474
11475 #pragma GCC diagnostic pop