Debian Codegraph @

ext/bifrost/src/roaring.c

raw symbols SCIP
1 #include "roaring.h"
2
3 /* used for http://dmalloc.com/ Dmalloc - Debug Malloc Library */
4 #ifdef DMALLOC
5 #include "dmalloc.h"
6 #endif
7
8 /* begin file src/array_util.c */
9 #include <assert.h>
10 #include <stdbool.h>
11 #include <stdint.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15
16 extern inline int32_t binarySearch(const uint16_t *array, int32_t lenarray,
17 uint16_t ikey);
18
19 #ifdef USESSE4
20 // used by intersect_vector16
21 ALIGNED(0x1000)
22 static const uint8_t shuffle_mask16[] = {
23 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
24 0xFF, 0xFF, 0xFF, 0xFF, 0, 1, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
25 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 2, 3, 0xFF, 0xFF,
26 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
27 0, 1, 2, 3, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
28 0xFF, 0xFF, 0xFF, 0xFF, 4, 5, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
29 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0, 1, 4, 5,
30 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
31 2, 3, 4, 5, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
32 0xFF, 0xFF, 0xFF, 0xFF, 0, 1, 2, 3, 4, 5, 0xFF, 0xFF,
33 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x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, 0xFF,
323 0, 1, 10, 11, 12, 13, 14, 15, 0xFF, 0xFF, 0xFF, 0xFF,
324 0xFF, 0xFF, 0xFF, 0xFF, 2, 3, 10, 11, 12, 13, 14, 15,
325 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0, 1, 2, 3,
326 10, 11, 12, 13, 14, 15, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
327 4, 5, 10, 11, 12, 13, 14, 15, 0xFF, 0xFF, 0xFF, 0xFF,
328 0xFF, 0xFF, 0xFF, 0xFF, 0, 1, 4, 5, 10, 11, 12, 13,
329 14, 15, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 2, 3, 4, 5,
330 10, 11, 12, 13, 14, 15, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
331 0, 1, 2, 3, 4, 5, 10, 11, 12, 13, 14, 15,
332 0xFF, 0xFF, 0xFF, 0xFF, 6, 7, 10, 11, 12, 13, 14, 15,
333 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0, 1, 6, 7,
334 10, 11, 12, 13, 14, 15, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
335 2, 3, 6, 7, 10, 11, 12, 13, 14, 15, 0xFF, 0xFF,
336 0xFF, 0xFF, 0xFF, 0xFF, 0, 1, 2, 3, 6, 7, 10, 11,
337 12, 13, 14, 15, 0xFF, 0xFF, 0xFF, 0xFF, 4, 5, 6, 7,
338 10, 11, 12, 13, 14, 15, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
339 0, 1, 4, 5, 6, 7, 10, 11, 12, 13, 14, 15,
340 0xFF, 0xFF, 0xFF, 0xFF, 2, 3, 4, 5, 6, 7, 10, 11,
341 12, 13, 14, 15, 0xFF, 0xFF, 0xFF, 0xFF, 0, 1, 2, 3,
342 4, 5, 6, 7, 10, 11, 12, 13, 14, 15, 0xFF, 0xFF,
343 8, 9, 10, 11, 12, 13, 14, 15, 0xFF, 0xFF, 0xFF, 0xFF,
344 0xFF, 0xFF, 0xFF, 0xFF, 0, 1, 8, 9, 10, 11, 12, 13,
345 14, 15, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 2, 3, 8, 9,
346 10, 11, 12, 13, 14, 15, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
347 0, 1, 2, 3, 8, 9, 10, 11, 12, 13, 14, 15,
348 0xFF, 0xFF, 0xFF, 0xFF, 4, 5, 8, 9, 10, 11, 12, 13,
349 14, 15, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0, 1, 4, 5,
350 8, 9, 10, 11, 12, 13, 14, 15, 0xFF, 0xFF, 0xFF, 0xFF,
351 2, 3, 4, 5, 8, 9, 10, 11, 12, 13, 14, 15,
352 0xFF, 0xFF, 0xFF, 0xFF, 0, 1, 2, 3, 4, 5, 8, 9,
353 10, 11, 12, 13, 14, 15, 0xFF, 0xFF, 6, 7, 8, 9,
354 10, 11, 12, 13, 14, 15, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
355 0, 1, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
356 0xFF, 0xFF, 0xFF, 0xFF, 2, 3, 6, 7, 8, 9, 10, 11,
357 12, 13, 14, 15, 0xFF, 0xFF, 0xFF, 0xFF, 0, 1, 2, 3,
358 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0xFF, 0xFF,
359 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
360 0xFF, 0xFF, 0xFF, 0xFF, 0, 1, 4, 5, 6, 7, 8, 9,
361 10, 11, 12, 13, 14, 15, 0xFF, 0xFF, 2, 3, 4, 5,
362 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0xFF, 0xFF,
363 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
364 12, 13, 14, 15};
365
366 /**
367 * From Schlegel et al., Fast Sorted-Set Intersection using SIMD Instructions
368 * Optimized by D. Lemire on May 3rd 2013
369 */
370 int32_t intersect_vector16(const uint16_t *__restrict__ A, size_t s_a,
371 const uint16_t *__restrict__ B, size_t s_b,
372 uint16_t *C) {
373 size_t count = 0;
374 size_t i_a = 0, i_b = 0;
375 const int vectorlength = sizeof(__m128i) / sizeof(uint16_t);
376 const size_t st_a = (s_a / vectorlength) * vectorlength;
377 const size_t st_b = (s_b / vectorlength) * vectorlength;
378 __m128i v_a, v_b;
379 if ((i_a < st_a) && (i_b < st_b)) {
380 v_a = _mm_lddqu_si128((__m128i *)&A[i_a]);
381 v_b = _mm_lddqu_si128((__m128i *)&B[i_b]);
382 while ((A[i_a] == 0) || (B[i_b] == 0)) {
383 const __m128i res_v = _mm_cmpestrm(
384 v_b, vectorlength, v_a, vectorlength,
385 _SIDD_UWORD_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_BIT_MASK);
386 const int r = _mm_extract_epi32(res_v, 0);
387 __m128i sm16 = _mm_load_si128((const __m128i *)shuffle_mask16 + r);
388 __m128i p = _mm_shuffle_epi8(v_a, sm16);
389 _mm_storeu_si128((__m128i *)&C[count], p); // can overflow
390 count += _mm_popcnt_u32(r);
391 const uint16_t a_max = A[i_a + vectorlength - 1];
392 const uint16_t b_max = B[i_b + vectorlength - 1];
393 if (a_max <= b_max) {
394 i_a += vectorlength;
395 if (i_a == st_a) break;
396 v_a = _mm_lddqu_si128((__m128i *)&A[i_a]);
397 }
398 if (b_max <= a_max) {
399 i_b += vectorlength;
400 if (i_b == st_b) break;
401 v_b = _mm_lddqu_si128((__m128i *)&B[i_b]);
402 }
403 }
404 if ((i_a < st_a) && (i_b < st_b))
405 while (true) {
406 const __m128i res_v = _mm_cmpistrm(
407 v_b, v_a,
408 _SIDD_UWORD_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_BIT_MASK);
409 const int r = _mm_extract_epi32(res_v, 0);
410 __m128i sm16 =
411 _mm_load_si128((const __m128i *)shuffle_mask16 + r);
412 __m128i p = _mm_shuffle_epi8(v_a, sm16);
413 _mm_storeu_si128((__m128i *)&C[count], p); // can overflow
414 count += _mm_popcnt_u32(r);
415 const uint16_t a_max = A[i_a + vectorlength - 1];
416 const uint16_t b_max = B[i_b + vectorlength - 1];
417 if (a_max <= b_max) {
418 i_a += vectorlength;
419 if (i_a == st_a) break;
420 v_a = _mm_lddqu_si128((__m128i *)&A[i_a]);
421 }
422 if (b_max <= a_max) {
423 i_b += vectorlength;
424 if (i_b == st_b) break;
425 v_b = _mm_lddqu_si128((__m128i *)&B[i_b]);
426 }
427 }
428 }
429 // intersect the tail using scalar intersection
430 while (i_a < s_a && i_b < s_b) {
431 uint16_t a = A[i_a];
432 uint16_t b = B[i_b];
433 if (a < b) {
434 i_a++;
435 } else if (b < a) {
436 i_b++;
437 } else {
438 C[count] = a; //==b;
439 count++;
440 i_a++;
441 i_b++;
442 }
443 }
444 return (int32_t)count;
445 }
446
447 int32_t intersect_vector16_cardinality(const uint16_t *__restrict__ A,
448 size_t s_a,
449 const uint16_t *__restrict__ B,
450 size_t s_b) {
451 size_t count = 0;
452 size_t i_a = 0, i_b = 0;
453 const int vectorlength = sizeof(__m128i) / sizeof(uint16_t);
454 const size_t st_a = (s_a / vectorlength) * vectorlength;
455 const size_t st_b = (s_b / vectorlength) * vectorlength;
456 __m128i v_a, v_b;
457 if ((i_a < st_a) && (i_b < st_b)) {
458 v_a = _mm_lddqu_si128((__m128i *)&A[i_a]);
459 v_b = _mm_lddqu_si128((__m128i *)&B[i_b]);
460 while ((A[i_a] == 0) || (B[i_b] == 0)) {
461 const __m128i res_v = _mm_cmpestrm(
462 v_b, vectorlength, v_a, vectorlength,
463 _SIDD_UWORD_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_BIT_MASK);
464 const int r = _mm_extract_epi32(res_v, 0);
465 count += _mm_popcnt_u32(r);
466 const uint16_t a_max = A[i_a + vectorlength - 1];
467 const uint16_t b_max = B[i_b + vectorlength - 1];
468 if (a_max <= b_max) {
469 i_a += vectorlength;
470 if (i_a == st_a) break;
471 v_a = _mm_lddqu_si128((__m128i *)&A[i_a]);
472 }
473 if (b_max <= a_max) {
474 i_b += vectorlength;
475 if (i_b == st_b) break;
476 v_b = _mm_lddqu_si128((__m128i *)&B[i_b]);
477 }
478 }
479 if ((i_a < st_a) && (i_b < st_b))
480 while (true) {
481 const __m128i res_v = _mm_cmpistrm(
482 v_b, v_a,
483 _SIDD_UWORD_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_BIT_MASK);
484 const int r = _mm_extract_epi32(res_v, 0);
485 count += _mm_popcnt_u32(r);
486 const uint16_t a_max = A[i_a + vectorlength - 1];
487 const uint16_t b_max = B[i_b + vectorlength - 1];
488 if (a_max <= b_max) {
489 i_a += vectorlength;
490 if (i_a == st_a) break;
491 v_a = _mm_lddqu_si128((__m128i *)&A[i_a]);
492 }
493 if (b_max <= a_max) {
494 i_b += vectorlength;
495 if (i_b == st_b) break;
496 v_b = _mm_lddqu_si128((__m128i *)&B[i_b]);
497 }
498 }
499 }
500 // intersect the tail using scalar intersection
501 while (i_a < s_a && i_b < s_b) {
502 uint16_t a = A[i_a];
503 uint16_t b = B[i_b];
504 if (a < b) {
505 i_a++;
506 } else if (b < a) {
507 i_b++;
508 } else {
509 count++;
510 i_a++;
511 i_b++;
512 }
513 }
514 return (int32_t)count;
515 }
516
517 int32_t difference_vector16(const uint16_t *__restrict__ A, size_t s_a,
518 const uint16_t *__restrict__ B, size_t s_b,
519 uint16_t *C) {
520 // we handle the degenerate case
521 if (s_a == 0) return 0;
522 if (s_b == 0) {
523 if (A != C) memcpy(C, A, sizeof(uint16_t) * s_a);
524 return (int32_t)s_a;
525 }
526 // handle the leading zeroes, it is messy but it allows us to use the fast
527 // _mm_cmpistrm instrinsic safely
528 int32_t count = 0;
529 if ((A[0] == 0) || (B[0] == 0)) {
530 if ((A[0] == 0) && (B[0] == 0)) {
531 A++;
532 s_a--;
533 B++;
534 s_b--;
535 } else if (A[0] == 0) {
536 C[count++] = 0;
537 A++;
538 s_a--;
539 } else {
540 B++;
541 s_b--;
542 }
543 }
544 // at this point, we have two non-empty arrays, made of non-zero
545 // increasing values.
546 size_t i_a = 0, i_b = 0;
547 const size_t vectorlength = sizeof(__m128i) / sizeof(uint16_t);
548 const size_t st_a = (s_a / vectorlength) * vectorlength;
549 const size_t st_b = (s_b / vectorlength) * vectorlength;
550 if ((i_a < st_a) && (i_b < st_b)) { // this is the vectorized code path
551 __m128i v_a, v_b; //, v_bmax;
552 // we load a vector from A and a vector from B
553 v_a = _mm_lddqu_si128((__m128i *)&A[i_a]);
554 v_b = _mm_lddqu_si128((__m128i *)&B[i_b]);
555 // we have a runningmask which indicates which values from A have been
556 // spotted in B, these don't get written out.
557 __m128i runningmask_a_found_in_b = _mm_setzero_si128();
558 /****
559 * start of the main vectorized loop
560 *****/
561 while (true) {
562 // afoundinb will contain a mask indicate for each entry in A
563 // whether it is seen
564 // in B
565 const __m128i a_found_in_b =
566 _mm_cmpistrm(v_b, v_a, _SIDD_UWORD_OPS | _SIDD_CMP_EQUAL_ANY |
567 _SIDD_BIT_MASK);
568 runningmask_a_found_in_b =
569 _mm_or_si128(runningmask_a_found_in_b, a_found_in_b);
570 // we always compare the last values of A and B
571 const uint16_t a_max = A[i_a + vectorlength - 1];
572 const uint16_t b_max = B[i_b + vectorlength - 1];
573 if (a_max <= b_max) {
574 // Ok. In this code path, we are ready to write our v_a
575 // because there is no need to read more from B, they will
576 // all be large values.
577 const int bitmask_belongs_to_difference =
578 _mm_extract_epi32(runningmask_a_found_in_b, 0) ^ 0xFF;
579 /*** next few lines are probably expensive *****/
580 __m128i sm16 = _mm_load_si128((const __m128i *)shuffle_mask16 +
581 bitmask_belongs_to_difference);
582 __m128i p = _mm_shuffle_epi8(v_a, sm16);
583 _mm_storeu_si128((__m128i *)&C[count], p); // can overflow
584 count += _mm_popcnt_u32(bitmask_belongs_to_difference);
585 // we advance a
586 i_a += vectorlength;
587 if (i_a == st_a) // no more
588 break;
589 runningmask_a_found_in_b = _mm_setzero_si128();
590 v_a = _mm_lddqu_si128((__m128i *)&A[i_a]);
591 }
592 if (b_max <= a_max) {
593 // in this code path, the current v_b has become useless
594 i_b += vectorlength;
595 if (i_b == st_b) break;
596 v_b = _mm_lddqu_si128((__m128i *)&B[i_b]);
597 }
598 }
599 // at this point, either we have i_a == st_a, which is the end of the
600 // vectorized processing,
601 // or we have i_b == st_b, and we are not done processing the vector...
602 // so we need to finish it off.
603 if (i_a < st_a) { // we have unfinished business...
604 uint16_t buffer[8]; // buffer to do a masked load
605 memset(buffer, 0, 8 * sizeof(uint16_t));
606 memcpy(buffer, B + i_b, (s_b - i_b) * sizeof(uint16_t));
607 v_b = _mm_lddqu_si128((__m128i *)buffer);
608 const __m128i a_found_in_b =
609 _mm_cmpistrm(v_b, v_a, _SIDD_UWORD_OPS | _SIDD_CMP_EQUAL_ANY |
610 _SIDD_BIT_MASK);
611 runningmask_a_found_in_b =
612 _mm_or_si128(runningmask_a_found_in_b, a_found_in_b);
613 const int bitmask_belongs_to_difference =
614 _mm_extract_epi32(runningmask_a_found_in_b, 0) ^ 0xFF;
615 __m128i sm16 = _mm_load_si128((const __m128i *)shuffle_mask16 +
616 bitmask_belongs_to_difference);
617 __m128i p = _mm_shuffle_epi8(v_a, sm16);
618 _mm_storeu_si128((__m128i *)&C[count], p); // can overflow
619 count += _mm_popcnt_u32(bitmask_belongs_to_difference);
620 i_a += vectorlength;
621 }
622 // at this point we should have i_a == st_a and i_b == st_b
623 }
624 // do the tail using scalar code
625 while (i_a < s_a && i_b < s_b) {
626 uint16_t a = A[i_a];
627 uint16_t b = B[i_b];
628 if (b < a) {
629 i_b++;
630 } else if (a < b) {
631 C[count] = a;
632 count++;
633 i_a++;
634 } else { //==
635 i_a++;
636 i_b++;
637 }
638 }
639 if (i_a < s_a) {
640 memmove(C + count, A + i_a, sizeof(uint16_t) * (s_a - i_a));
641 count += (int32_t)(s_a - i_a);
642 }
643 return count;
644 }
645
646 #endif // USESSE4
647
648
649
650 #ifdef USE_OLD_SKEW_INTERSECT
651 // TODO: given enough experience with the new skew intersect, drop the old one from the code base.
652
653
654 /* Computes the intersection between one small and one large set of uint16_t.
655 * Stores the result into buffer and return the number of elements. */
656 int32_t intersect_skewed_uint16(const uint16_t *small, size_t size_s,
657 const uint16_t *large, size_t size_l,
658 uint16_t *buffer) {
659 size_t pos = 0, idx_l = 0, idx_s = 0;
660
661 if (0 == size_s) {
662 return 0;
663 }
664
665 uint16_t val_l = large[idx_l], val_s = small[idx_s];
666
667 while (true) {
668 if (val_l < val_s) {
669 idx_l = advanceUntil(large, (int32_t)idx_l, (int32_t)size_l, val_s);
670 if (idx_l == size_l) break;
671 val_l = large[idx_l];
672 } else if (val_s < val_l) {
673 idx_s++;
674 if (idx_s == size_s) break;
675 val_s = small[idx_s];
676 } else {
677 buffer[pos++] = val_s;
678 idx_s++;
679 if (idx_s == size_s) break;
680 val_s = small[idx_s];
681 idx_l = advanceUntil(large, (int32_t)idx_l, (int32_t)size_l, val_s);
682 if (idx_l == size_l) break;
683 val_l = large[idx_l];
684 }
685 }
686
687 return (int32_t)pos;
688 }
689 #else // USE_OLD_SKEW_INTERSECT
690
691
692 /**
693 * Branchless binary search going after 4 values at once.
694 * Assumes that array is sorted.
695 * You have that array[*index1] >= target1, array[*index12] >= target2, ...
696 * except when *index1 = n, in which case you know that all values in array are
697 * smaller than target1, and so forth.
698 * It has logarithmic complexity.
699 */
700 static void binarySearch4(const uint16_t *array, int32_t n, uint16_t target1,
701 uint16_t target2, uint16_t target3, uint16_t target4,
702 int32_t *index1, int32_t *index2, int32_t *index3,
703 int32_t *index4) {
704 const uint16_t *base1 = array;
705 const uint16_t *base2 = array;
706 const uint16_t *base3 = array;
707 const uint16_t *base4 = array;
708 if (n == 0)
709 return;
710 while (n > 1) {
711 int32_t half = n >> 1;
712 base1 = (base1[half] < target1) ? &base1[half] : base1;
713 base2 = (base2[half] < target2) ? &base2[half] : base2;
714 base3 = (base3[half] < target3) ? &base3[half] : base3;
715 base4 = (base4[half] < target4) ? &base4[half] : base4;
716 n -= half;
717 }
718 *index1 = (int32_t)((*base1 < target1) + base1 - array);
719 *index2 = (int32_t)((*base2 < target2) + base2 - array);
720 *index3 = (int32_t)((*base3 < target3) + base3 - array);
721 *index4 = (int32_t)((*base4 < target4) + base4 - array);
722 }
723
724 /**
725 * Branchless binary search going after 2 values at once.
726 * Assumes that array is sorted.
727 * You have that array[*index1] >= target1, array[*index12] >= target2.
728 * except when *index1 = n, in which case you know that all values in array are
729 * smaller than target1, and so forth.
730 * It has logarithmic complexity.
731 */
732 static void binarySearch2(const uint16_t *array, int32_t n, uint16_t target1,
733 uint16_t target2, int32_t *index1, int32_t *index2) {
734 const uint16_t *base1 = array;
735 const uint16_t *base2 = array;
736 if (n == 0)
737 return;
738 while (n > 1) {
739 int32_t half = n >> 1;
740 base1 = (base1[half] < target1) ? &base1[half] : base1;
741 base2 = (base2[half] < target2) ? &base2[half] : base2;
742 n -= half;
743 }
744 *index1 = (int32_t)((*base1 < target1) + base1 - array);
745 *index2 = (int32_t)((*base2 < target2) + base2 - array);
746 }
747
748 /* Computes the intersection between one small and one large set of uint16_t.
749 * Stores the result into buffer and return the number of elements.
750 * Processes the small set in blocks of 4 values calling binarySearch4
751 * and binarySearch2. This approach can be slightly superior to a conventional
752 * galloping search in some instances.
753 */
754 int32_t intersect_skewed_uint16(const uint16_t *small, size_t size_s,
755 const uint16_t *large, size_t size_l,
756 uint16_t *buffer) {
757 size_t pos = 0, idx_l = 0, idx_s = 0;
758
759 if (0 == size_s) {
760 return 0;
761 }
762 int32_t index1 = 0, index2 = 0, index3 = 0, index4 = 0;
763 while ((idx_s + 4 <= size_s) && (idx_l < size_l)) {
764 uint16_t target1 = small[idx_s];
765 uint16_t target2 = small[idx_s + 1];
766 uint16_t target3 = small[idx_s + 2];
767 uint16_t target4 = small[idx_s + 3];
768 binarySearch4(large + idx_l, (int32_t)(size_l - idx_l), target1, target2, target3,
769 target4, &index1, &index2, &index3, &index4);
770 if ((index1 + idx_l < size_l) && (large[idx_l + index1] == target1)) {
771 buffer[pos++] = target1;
772 }
773 if ((index2 + idx_l < size_l) && (large[idx_l + index2] == target2)) {
774 buffer[pos++] = target2;
775 }
776 if ((index3 + idx_l < size_l) && (large[idx_l + index3] == target3)) {
777 buffer[pos++] = target3;
778 }
779 if ((index4 + idx_l < size_l) && (large[idx_l + index4] == target4)) {
780 buffer[pos++] = target4;
781 }
782 idx_s += 4;
783 idx_l += index1;
784 }
785 if ((idx_s + 2 <= size_s) && (idx_l < size_l)) {
786 uint16_t target1 = small[idx_s];
787 uint16_t target2 = small[idx_s + 1];
788 binarySearch2(large + idx_l, (int32_t)(size_l - idx_l), target1, target2, &index1,
789 &index2);
790 if ((index1 + idx_l < size_l) && (large[idx_l + index1] == target1)) {
791 buffer[pos++] = target1;
792 }
793 if ((index2 + idx_l < size_l) && (large[idx_l + index2] == target2)) {
794 buffer[pos++] = target2;
795 }
796 idx_s += 2;
797 idx_l += index1;
798 }
799 if ((idx_s < size_s) && (idx_l < size_l)) {
800 uint16_t val_s = small[idx_s];
801 int32_t index = binarySearch(large + idx_l, (int32_t)(size_l - idx_l), val_s);
802 if (index >= 0)
803 buffer[pos++] = val_s;
804 }
805 return (int32_t)pos;
806 }
807
808
809 #endif //USE_OLD_SKEW_INTERSECT
810
811
812 // TODO: this could be accelerated, possibly, by using binarySearch4 as above.
813 int32_t intersect_skewed_uint16_cardinality(const uint16_t *small,
814 size_t size_s,
815 const uint16_t *large,
816 size_t size_l) {
817 size_t pos = 0, idx_l = 0, idx_s = 0;
818
819 if (0 == size_s) {
820 return 0;
821 }
822
823 uint16_t val_l = large[idx_l], val_s = small[idx_s];
824
825 while (true) {
826 if (val_l < val_s) {
827 idx_l = advanceUntil(large, (int32_t)idx_l, (int32_t)size_l, val_s);
828 if (idx_l == size_l) break;
829 val_l = large[idx_l];
830 } else if (val_s < val_l) {
831 idx_s++;
832 if (idx_s == size_s) break;
833 val_s = small[idx_s];
834 } else {
835 pos++;
836 idx_s++;
837 if (idx_s == size_s) break;
838 val_s = small[idx_s];
839 idx_l = advanceUntil(large, (int32_t)idx_l, (int32_t)size_l, val_s);
840 if (idx_l == size_l) break;
841 val_l = large[idx_l];
842 }
843 }
844
845 return (int32_t)pos;
846 }
847
848 bool intersect_skewed_uint16_nonempty(const uint16_t *small, size_t size_s,
849 const uint16_t *large, size_t size_l) {
850 size_t idx_l = 0, idx_s = 0;
851
852 if (0 == size_s) {
853 return false;
854 }
855
856 uint16_t val_l = large[idx_l], val_s = small[idx_s];
857
858 while (true) {
859 if (val_l < val_s) {
860 idx_l = advanceUntil(large, (int32_t)idx_l, (int32_t)size_l, val_s);
861 if (idx_l == size_l) break;
862 val_l = large[idx_l];
863 } else if (val_s < val_l) {
864 idx_s++;
865 if (idx_s == size_s) break;
866 val_s = small[idx_s];
867 } else {
868 return true;
869 }
870 }
871
872 return false;
873 }
874
875 /**
876 * Generic intersection function.
877 */
878 int32_t intersect_uint16(const uint16_t *A, const size_t lenA,
879 const uint16_t *B, const size_t lenB, uint16_t *out) {
880 const uint16_t *initout = out;
881 if (lenA == 0 || lenB == 0) return 0;
882 const uint16_t *endA = A + lenA;
883 const uint16_t *endB = B + lenB;
884
885 while (1) {
886 while (*A < *B) {
887 SKIP_FIRST_COMPARE:
888 if (++A == endA) return (int32_t)(out - initout);
889 }
890 while (*A > *B) {
891 if (++B == endB) return (int32_t)(out - initout);
892 }
893 if (*A == *B) {
894 *out++ = *A;
895 if (++A == endA || ++B == endB) return (int32_t)(out - initout);
896 } else {
897 goto SKIP_FIRST_COMPARE;
898 }
899 }
900 return (int32_t)(out - initout); // NOTREACHED
901 }
902
903 int32_t intersect_uint16_cardinality(const uint16_t *A, const size_t lenA,
904 const uint16_t *B, const size_t lenB) {
905 int32_t answer = 0;
906 if (lenA == 0 || lenB == 0) return 0;
907 const uint16_t *endA = A + lenA;
908 const uint16_t *endB = B + lenB;
909
910 while (1) {
911 while (*A < *B) {
912 SKIP_FIRST_COMPARE:
913 if (++A == endA) return answer;
914 }
915 while (*A > *B) {
916 if (++B == endB) return answer;
917 }
918 if (*A == *B) {
919 ++answer;
920 if (++A == endA || ++B == endB) return answer;
921 } else {
922 goto SKIP_FIRST_COMPARE;
923 }
924 }
925 return answer; // NOTREACHED
926 }
927
928
929 bool intersect_uint16_nonempty(const uint16_t *A, const size_t lenA,
930 const uint16_t *B, const size_t lenB) {
931 if (lenA == 0 || lenB == 0) return 0;
932 const uint16_t *endA = A + lenA;
933 const uint16_t *endB = B + lenB;
934
935 while (1) {
936 while (*A < *B) {
937 SKIP_FIRST_COMPARE:
938 if (++A == endA) return false;
939 }
940 while (*A > *B) {
941 if (++B == endB) return false;
942 }
943 if (*A == *B) {
944 return true;
945 } else {
946 goto SKIP_FIRST_COMPARE;
947 }
948 }
949 return false; // NOTREACHED
950 }
951
952
953
954 /**
955 * Generic intersection function.
956 */
957 size_t intersection_uint32(const uint32_t *A, const size_t lenA,
958 const uint32_t *B, const size_t lenB,
959 uint32_t *out) {
960 const uint32_t *initout = out;
961 if (lenA == 0 || lenB == 0) return 0;
962 const uint32_t *endA = A + lenA;
963 const uint32_t *endB = B + lenB;
964
965 while (1) {
966 while (*A < *B) {
967 SKIP_FIRST_COMPARE:
968 if (++A == endA) return (out - initout);
969 }
970 while (*A > *B) {
971 if (++B == endB) return (out - initout);
972 }
973 if (*A == *B) {
974 *out++ = *A;
975 if (++A == endA || ++B == endB) return (out - initout);
976 } else {
977 goto SKIP_FIRST_COMPARE;
978 }
979 }
980 return (out - initout); // NOTREACHED
981 }
982
983 size_t intersection_uint32_card(const uint32_t *A, const size_t lenA,
984 const uint32_t *B, const size_t lenB) {
985 if (lenA == 0 || lenB == 0) return 0;
986 size_t card = 0;
987 const uint32_t *endA = A + lenA;
988 const uint32_t *endB = B + lenB;
989
990 while (1) {
991 while (*A < *B) {
992 SKIP_FIRST_COMPARE:
993 if (++A == endA) return card;
994 }
995 while (*A > *B) {
996 if (++B == endB) return card;
997 }
998 if (*A == *B) {
999 card++;
1000 if (++A == endA || ++B == endB) return card;
1001 } else {
1002 goto SKIP_FIRST_COMPARE;
1003 }
1004 }
1005 return card; // NOTREACHED
1006 }
1007
1008 // can one vectorize the computation of the union? (Update: Yes! See
1009 // union_vector16).
1010
1011 size_t union_uint16(const uint16_t *set_1, size_t size_1, const uint16_t *set_2,
1012 size_t size_2, uint16_t *buffer) {
1013 size_t pos = 0, idx_1 = 0, idx_2 = 0;
1014
1015 if (0 == size_2) {
1016 memmove(buffer, set_1, size_1 * sizeof(uint16_t));
1017 return size_1;
1018 }
1019 if (0 == size_1) {
1020 memmove(buffer, set_2, size_2 * sizeof(uint16_t));
1021 return size_2;
1022 }
1023
1024 uint16_t val_1 = set_1[idx_1], val_2 = set_2[idx_2];
1025
1026 while (true) {
1027 if (val_1 < val_2) {
1028 buffer[pos++] = val_1;
1029 ++idx_1;
1030 if (idx_1 >= size_1) break;
1031 val_1 = set_1[idx_1];
1032 } else if (val_2 < val_1) {
1033 buffer[pos++] = val_2;
1034 ++idx_2;
1035 if (idx_2 >= size_2) break;
1036 val_2 = set_2[idx_2];
1037 } else {
1038 buffer[pos++] = val_1;
1039 ++idx_1;
1040 ++idx_2;
1041 if (idx_1 >= size_1 || idx_2 >= size_2) break;
1042 val_1 = set_1[idx_1];
1043 val_2 = set_2[idx_2];
1044 }
1045 }
1046
1047 if (idx_1 < size_1) {
1048 const size_t n_elems = size_1 - idx_1;
1049 memmove(buffer + pos, set_1 + idx_1, n_elems * sizeof(uint16_t));
1050 pos += n_elems;
1051 } else if (idx_2 < size_2) {
1052 const size_t n_elems = size_2 - idx_2;
1053 memmove(buffer + pos, set_2 + idx_2, n_elems * sizeof(uint16_t));
1054 pos += n_elems;
1055 }
1056
1057 return pos;
1058 }
1059
1060 int difference_uint16(const uint16_t *a1, int length1, const uint16_t *a2,
1061 int length2, uint16_t *a_out) {
1062 int out_card = 0;
1063 int k1 = 0, k2 = 0;
1064 if (length1 == 0) return 0;
1065 if (length2 == 0) {
1066 if (a1 != a_out) memcpy(a_out, a1, sizeof(uint16_t) * length1);
1067 return length1;
1068 }
1069 uint16_t s1 = a1[k1];
1070 uint16_t s2 = a2[k2];
1071 while (true) {
1072 if (s1 < s2) {
1073 a_out[out_card++] = s1;
1074 ++k1;
1075 if (k1 >= length1) {
1076 break;
1077 }
1078 s1 = a1[k1];
1079 } else if (s1 == s2) {
1080 ++k1;
1081 ++k2;
1082 if (k1 >= length1) {
1083 break;
1084 }
1085 if (k2 >= length2) {
1086 memmove(a_out + out_card, a1 + k1,
1087 sizeof(uint16_t) * (length1 - k1));
1088 return out_card + length1 - k1;
1089 }
1090 s1 = a1[k1];
1091 s2 = a2[k2];
1092 } else { // if (val1>val2)
1093 ++k2;
1094 if (k2 >= length2) {
1095 memmove(a_out + out_card, a1 + k1,
1096 sizeof(uint16_t) * (length1 - k1));
1097 return out_card + length1 - k1;
1098 }
1099 s2 = a2[k2];
1100 }
1101 }
1102 return out_card;
1103 }
1104
1105 int32_t xor_uint16(const uint16_t *array_1, int32_t card_1,
1106 const uint16_t *array_2, int32_t card_2, uint16_t *out) {
1107 int32_t pos1 = 0, pos2 = 0, pos_out = 0;
1108 while (pos1 < card_1 && pos2 < card_2) {
1109 const uint16_t v1 = array_1[pos1];
1110 const uint16_t v2 = array_2[pos2];
1111 if (v1 == v2) {
1112 ++pos1;
1113 ++pos2;
1114 continue;
1115 }
1116 if (v1 < v2) {
1117 out[pos_out++] = v1;
1118 ++pos1;
1119 } else {
1120 out[pos_out++] = v2;
1121 ++pos2;
1122 }
1123 }
1124 if (pos1 < card_1) {
1125 const size_t n_elems = card_1 - pos1;
1126 memcpy(out + pos_out, array_1 + pos1, n_elems * sizeof(uint16_t));
1127 pos_out += (int32_t)n_elems;
1128 } else if (pos2 < card_2) {
1129 const size_t n_elems = card_2 - pos2;
1130 memcpy(out + pos_out, array_2 + pos2, n_elems * sizeof(uint16_t));
1131 pos_out += (int32_t)n_elems;
1132 }
1133 return pos_out;
1134 }
1135
1136 #ifdef USESSE4
1137
1138 /***
1139 * start of the SIMD 16-bit union code
1140 *
1141 */
1142
1143 // Assuming that vInput1 and vInput2 are sorted, produces a sorted output going
1144 // from vecMin all the way to vecMax
1145 // developed originally for merge sort using SIMD instructions.
1146 // Standard merge. See, e.g., Inoue and Taura, SIMD- and Cache-Friendly
1147 // Algorithm for Sorting an Array of Structures
1148 static inline void sse_merge(const __m128i *vInput1,
1149 const __m128i *vInput2, // input 1 & 2
1150 __m128i *vecMin, __m128i *vecMax) { // output
1151 __m128i vecTmp;
1152 vecTmp = _mm_min_epu16(*vInput1, *vInput2);
1153 *vecMax = _mm_max_epu16(*vInput1, *vInput2);
1154 vecTmp = _mm_alignr_epi8(vecTmp, vecTmp, 2);
1155 *vecMin = _mm_min_epu16(vecTmp, *vecMax);
1156 *vecMax = _mm_max_epu16(vecTmp, *vecMax);
1157 vecTmp = _mm_alignr_epi8(*vecMin, *vecMin, 2);
1158 *vecMin = _mm_min_epu16(vecTmp, *vecMax);
1159 *vecMax = _mm_max_epu16(vecTmp, *vecMax);
1160 vecTmp = _mm_alignr_epi8(*vecMin, *vecMin, 2);
1161 *vecMin = _mm_min_epu16(vecTmp, *vecMax);
1162 *vecMax = _mm_max_epu16(vecTmp, *vecMax);
1163 vecTmp = _mm_alignr_epi8(*vecMin, *vecMin, 2);
1164 *vecMin = _mm_min_epu16(vecTmp, *vecMax);
1165 *vecMax = _mm_max_epu16(vecTmp, *vecMax);
1166 vecTmp = _mm_alignr_epi8(*vecMin, *vecMin, 2);
1167 *vecMin = _mm_min_epu16(vecTmp, *vecMax);
1168 *vecMax = _mm_max_epu16(vecTmp, *vecMax);
1169 vecTmp = _mm_alignr_epi8(*vecMin, *vecMin, 2);
1170 *vecMin = _mm_min_epu16(vecTmp, *vecMax);
1171 *vecMax = _mm_max_epu16(vecTmp, *vecMax);
1172 vecTmp = _mm_alignr_epi8(*vecMin, *vecMin, 2);
1173 *vecMin = _mm_min_epu16(vecTmp, *vecMax);
1174 *vecMax = _mm_max_epu16(vecTmp, *vecMax);
1175 *vecMin = _mm_alignr_epi8(*vecMin, *vecMin, 2);
1176 }
1177
1178 // used by store_unique, generated by simdunion.py
1179 static uint8_t uniqshuf[] = {
1180 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb,
1181 0xc, 0xd, 0xe, 0xf, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9,
1182 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0xFF, 0xFF, 0x0, 0x1, 0x4, 0x5,
1183 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0xFF, 0xFF,
1184 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
1185 0xFF, 0xFF, 0xFF, 0xFF, 0x0, 0x1, 0x2, 0x3, 0x6, 0x7, 0x8, 0x9,
1186 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0xFF, 0xFF, 0x2, 0x3, 0x6, 0x7,
1187 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0xFF, 0xFF, 0xFF, 0xFF,
1188 0x0, 0x1, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
1189 0xFF, 0xFF, 0xFF, 0xFF, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd,
1190 0xe, 0xf, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0, 0x1, 0x2, 0x3,
1191 0x4, 0x5, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0xFF, 0xFF,
1192 0x2, 0x3, 0x4, 0x5, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
1193 0xFF, 0xFF, 0xFF, 0xFF, 0x0, 0x1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, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1520 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1521 0xFF, 0xFF, 0xFF, 0xFF};
1522
1523 // write vector new, while omitting repeated values assuming that previously
1524 // written vector was "old"
1525 static inline int store_unique(__m128i old, __m128i newval, uint16_t *output) {
1526 __m128i vecTmp = _mm_alignr_epi8(newval, old, 16 - 2);
1527 // lots of high latency instructions follow (optimize?)
1528 int M = _mm_movemask_epi8(
1529 _mm_packs_epi16(_mm_cmpeq_epi16(vecTmp, newval), _mm_setzero_si128()));
1530 int numberofnewvalues = 8 - _mm_popcnt_u32(M);
1531 __m128i key = _mm_lddqu_si128((const __m128i *)uniqshuf + M);
1532 __m128i val = _mm_shuffle_epi8(newval, key);
1533 _mm_storeu_si128((__m128i *)output, val);
1534 return numberofnewvalues;
1535 }
1536
1537 // working in-place, this function overwrites the repeated values
1538 // could be avoided?
1539 static inline uint32_t unique(uint16_t *out, uint32_t len) {
1540 uint32_t pos = 1;
1541 for (uint32_t i = 1; i < len; ++i) {
1542 if (out[i] != out[i - 1]) {
1543 out[pos++] = out[i];
1544 }
1545 }
1546 return pos;
1547 }
1548
1549 // use with qsort, could be avoided
1550 static int uint16_compare(const void *a, const void *b) {
1551 return (*(uint16_t *)a - *(uint16_t *)b);
1552 }
1553
1554 // a one-pass SSE union algorithm
1555 uint32_t union_vector16(const uint16_t *__restrict__ array1, uint32_t length1,
1556 const uint16_t *__restrict__ array2, uint32_t length2,
1557 uint16_t *__restrict__ output) {
1558 if ((length1 < 8) || (length2 < 8)) {
1559 return (uint32_t)union_uint16(array1, length1, array2, length2, output);
1560 }
1561 __m128i vA, vB, V, vecMin, vecMax;
1562 __m128i laststore;
1563 uint16_t *initoutput = output;
1564 uint32_t len1 = length1 / 8;
1565 uint32_t len2 = length2 / 8;
1566 uint32_t pos1 = 0;
1567 uint32_t pos2 = 0;
1568 // we start the machine
1569 vA = _mm_lddqu_si128((const __m128i *)array1 + pos1);
1570 pos1++;
1571 vB = _mm_lddqu_si128((const __m128i *)array2 + pos2);
1572 pos2++;
1573 sse_merge(&vA, &vB, &vecMin, &vecMax);
1574 laststore = _mm_set1_epi16(-1);
1575 output += store_unique(laststore, vecMin, output);
1576 laststore = vecMin;
1577 if ((pos1 < len1) && (pos2 < len2)) {
1578 uint16_t curA, curB;
1579 curA = array1[8 * pos1];
1580 curB = array2[8 * pos2];
1581 while (true) {
1582 if (curA <= curB) {
1583 V = _mm_lddqu_si128((const __m128i *)array1 + pos1);
1584 pos1++;
1585 if (pos1 < len1) {
1586 curA = array1[8 * pos1];
1587 } else {
1588 break;
1589 }
1590 } else {
1591 V = _mm_lddqu_si128((const __m128i *)array2 + pos2);
1592 pos2++;
1593 if (pos2 < len2) {
1594 curB = array2[8 * pos2];
1595 } else {
1596 break;
1597 }
1598 }
1599 sse_merge(&V, &vecMax, &vecMin, &vecMax);
1600 output += store_unique(laststore, vecMin, output);
1601 laststore = vecMin;
1602 }
1603 sse_merge(&V, &vecMax, &vecMin, &vecMax);
1604 output += store_unique(laststore, vecMin, output);
1605 laststore = vecMin;
1606 }
1607 // we finish the rest off using a scalar algorithm
1608 // could be improved?
1609 //
1610 // copy the small end on a tmp buffer
1611 uint32_t len = (uint32_t)(output - initoutput);
1612 uint16_t buffer[16];
1613 uint32_t leftoversize = store_unique(laststore, vecMax, buffer);
1614 if (pos1 == len1) {
1615 memcpy(buffer + leftoversize, array1 + 8 * pos1,
1616 (length1 - 8 * len1) * sizeof(uint16_t));
1617 leftoversize += length1 - 8 * len1;
1618 qsort(buffer, leftoversize, sizeof(uint16_t), uint16_compare);
1619
1620 leftoversize = unique(buffer, leftoversize);
1621 len += (uint32_t)union_uint16(buffer, leftoversize, array2 + 8 * pos2,
1622 length2 - 8 * pos2, output);
1623 } else {
1624 memcpy(buffer + leftoversize, array2 + 8 * pos2,
1625 (length2 - 8 * len2) * sizeof(uint16_t));
1626 leftoversize += length2 - 8 * len2;
1627 qsort(buffer, leftoversize, sizeof(uint16_t), uint16_compare);
1628 leftoversize = unique(buffer, leftoversize);
1629 len += (uint32_t)union_uint16(buffer, leftoversize, array1 + 8 * pos1,
1630 length1 - 8 * pos1, output);
1631 }
1632 return len;
1633 }
1634
1635 /**
1636 * End of the SIMD 16-bit union code
1637 *
1638 */
1639
1640 /**
1641 * Start of SIMD 16-bit XOR code
1642 */
1643
1644 // write vector new, while omitting repeated values assuming that previously
1645 // written vector was "old"
1646 static inline int store_unique_xor(__m128i old, __m128i newval,
1647 uint16_t *output) {
1648 __m128i vecTmp1 = _mm_alignr_epi8(newval, old, 16 - 4);
1649 __m128i vecTmp2 = _mm_alignr_epi8(newval, old, 16 - 2);
1650 __m128i equalleft = _mm_cmpeq_epi16(vecTmp2, vecTmp1);
1651 __m128i equalright = _mm_cmpeq_epi16(vecTmp2, newval);
1652 __m128i equalleftoright = _mm_or_si128(equalleft, equalright);
1653 int M = _mm_movemask_epi8(
1654 _mm_packs_epi16(equalleftoright, _mm_setzero_si128()));
1655 int numberofnewvalues = 8 - _mm_popcnt_u32(M);
1656 __m128i key = _mm_lddqu_si128((const __m128i *)uniqshuf + M);
1657 __m128i val = _mm_shuffle_epi8(vecTmp2, key);
1658 _mm_storeu_si128((__m128i *)output, val);
1659 return numberofnewvalues;
1660 }
1661
1662 // working in-place, this function overwrites the repeated values
1663 // could be avoided? Warning: assumes len > 0
1664 static inline uint32_t unique_xor(uint16_t *out, uint32_t len) {
1665 uint32_t pos = 1;
1666 for (uint32_t i = 1; i < len; ++i) {
1667 if (out[i] != out[i - 1]) {
1668 out[pos++] = out[i];
1669 } else
1670 pos--; // if it is identical to previous, delete it
1671 }
1672 return pos;
1673 }
1674
1675 // a one-pass SSE xor algorithm
1676 uint32_t xor_vector16(const uint16_t *__restrict__ array1, uint32_t length1,
1677 const uint16_t *__restrict__ array2, uint32_t length2,
1678 uint16_t *__restrict__ output) {
1679 if ((length1 < 8) || (length2 < 8)) {
1680 return xor_uint16(array1, length1, array2, length2, output);
1681 }
1682 __m128i vA, vB, V, vecMin, vecMax;
1683 __m128i laststore;
1684 uint16_t *initoutput = output;
1685 uint32_t len1 = length1 / 8;
1686 uint32_t len2 = length2 / 8;
1687 uint32_t pos1 = 0;
1688 uint32_t pos2 = 0;
1689 // we start the machine
1690 vA = _mm_lddqu_si128((const __m128i *)array1 + pos1);
1691 pos1++;
1692 vB = _mm_lddqu_si128((const __m128i *)array2 + pos2);
1693 pos2++;
1694 sse_merge(&vA, &vB, &vecMin, &vecMax);
1695 laststore = _mm_set1_epi16(-1);
1696 uint16_t buffer[17];
1697 output += store_unique_xor(laststore, vecMin, output);
1698
1699 laststore = vecMin;
1700 if ((pos1 < len1) && (pos2 < len2)) {
1701 uint16_t curA, curB;
1702 curA = array1[8 * pos1];
1703 curB = array2[8 * pos2];
1704 while (true) {
1705 if (curA <= curB) {
1706 V = _mm_lddqu_si128((const __m128i *)array1 + pos1);
1707 pos1++;
1708 if (pos1 < len1) {
1709 curA = array1[8 * pos1];
1710 } else {
1711 break;
1712 }
1713 } else {
1714 V = _mm_lddqu_si128((const __m128i *)array2 + pos2);
1715 pos2++;
1716 if (pos2 < len2) {
1717 curB = array2[8 * pos2];
1718 } else {
1719 break;
1720 }
1721 }
1722 sse_merge(&V, &vecMax, &vecMin, &vecMax);
1723 // conditionally stores the last value of laststore as well as all
1724 // but the
1725 // last value of vecMin
1726 output += store_unique_xor(laststore, vecMin, output);
1727 laststore = vecMin;
1728 }
1729 sse_merge(&V, &vecMax, &vecMin, &vecMax);
1730 // conditionally stores the last value of laststore as well as all but
1731 // the
1732 // last value of vecMin
1733 output += store_unique_xor(laststore, vecMin, output);
1734 laststore = vecMin;
1735 }
1736 uint32_t len = (uint32_t)(output - initoutput);
1737
1738 // we finish the rest off using a scalar algorithm
1739 // could be improved?
1740 // conditionally stores the last value of laststore as well as all but the
1741 // last value of vecMax,
1742 // we store to "buffer"
1743 int leftoversize = store_unique_xor(laststore, vecMax, buffer);
1744 uint16_t vec7 = _mm_extract_epi16(vecMax, 7);
1745 uint16_t vec6 = _mm_extract_epi16(vecMax, 6);
1746 if (vec7 != vec6) buffer[leftoversize++] = vec7;
1747 if (pos1 == len1) {
1748 memcpy(buffer + leftoversize, array1 + 8 * pos1,
1749 (length1 - 8 * len1) * sizeof(uint16_t));
1750 leftoversize += length1 - 8 * len1;
1751 if (leftoversize == 0) { // trivial case
1752 memcpy(output, array2 + 8 * pos2,
1753 (length2 - 8 * pos2) * sizeof(uint16_t));
1754 len += (length2 - 8 * pos2);
1755 } else {
1756 qsort(buffer, leftoversize, sizeof(uint16_t), uint16_compare);
1757 leftoversize = unique_xor(buffer, leftoversize);
1758 len += xor_uint16(buffer, leftoversize, array2 + 8 * pos2,
1759 length2 - 8 * pos2, output);
1760 }
1761 } else {
1762 memcpy(buffer + leftoversize, array2 + 8 * pos2,
1763 (length2 - 8 * len2) * sizeof(uint16_t));
1764 leftoversize += length2 - 8 * len2;
1765 if (leftoversize == 0) { // trivial case
1766 memcpy(output, array1 + 8 * pos1,
1767 (length1 - 8 * pos1) * sizeof(uint16_t));
1768 len += (length1 - 8 * pos1);
1769 } else {
1770 qsort(buffer, leftoversize, sizeof(uint16_t), uint16_compare);
1771 leftoversize = unique_xor(buffer, leftoversize);
1772 len += xor_uint16(buffer, leftoversize, array1 + 8 * pos1,
1773 length1 - 8 * pos1, output);
1774 }
1775 }
1776 return len;
1777 }
1778
1779 /**
1780 * End of SIMD 16-bit XOR code
1781 */
1782
1783 #endif // USESSE4
1784
1785 size_t union_uint32(const uint32_t *set_1, size_t size_1, const uint32_t *set_2,
1786 size_t size_2, uint32_t *buffer) {
1787 size_t pos = 0, idx_1 = 0, idx_2 = 0;
1788
1789 if (0 == size_2) {
1790 memmove(buffer, set_1, size_1 * sizeof(uint32_t));
1791 return size_1;
1792 }
1793 if (0 == size_1) {
1794 memmove(buffer, set_2, size_2 * sizeof(uint32_t));
1795 return size_2;
1796 }
1797
1798 uint32_t val_1 = set_1[idx_1], val_2 = set_2[idx_2];
1799
1800 while (true) {
1801 if (val_1 < val_2) {
1802 buffer[pos++] = val_1;
1803 ++idx_1;
1804 if (idx_1 >= size_1) break;
1805 val_1 = set_1[idx_1];
1806 } else if (val_2 < val_1) {
1807 buffer[pos++] = val_2;
1808 ++idx_2;
1809 if (idx_2 >= size_2) break;
1810 val_2 = set_2[idx_2];
1811 } else {
1812 buffer[pos++] = val_1;
1813 ++idx_1;
1814 ++idx_2;
1815 if (idx_1 >= size_1 || idx_2 >= size_2) break;
1816 val_1 = set_1[idx_1];
1817 val_2 = set_2[idx_2];
1818 }
1819 }
1820
1821 if (idx_1 < size_1) {
1822 const size_t n_elems = size_1 - idx_1;
1823 memmove(buffer + pos, set_1 + idx_1, n_elems * sizeof(uint32_t));
1824 pos += n_elems;
1825 } else if (idx_2 < size_2) {
1826 const size_t n_elems = size_2 - idx_2;
1827 memmove(buffer + pos, set_2 + idx_2, n_elems * sizeof(uint32_t));
1828 pos += n_elems;
1829 }
1830
1831 return pos;
1832 }
1833
1834 size_t union_uint32_card(const uint32_t *set_1, size_t size_1,
1835 const uint32_t *set_2, size_t size_2) {
1836 size_t pos = 0, idx_1 = 0, idx_2 = 0;
1837
1838 if (0 == size_2) {
1839 return size_1;
1840 }
1841 if (0 == size_1) {
1842 return size_2;
1843 }
1844
1845 uint32_t val_1 = set_1[idx_1], val_2 = set_2[idx_2];
1846
1847 while (true) {
1848 if (val_1 < val_2) {
1849 ++idx_1;
1850 ++pos;
1851 if (idx_1 >= size_1) break;
1852 val_1 = set_1[idx_1];
1853 } else if (val_2 < val_1) {
1854 ++idx_2;
1855 ++pos;
1856 if (idx_2 >= size_2) break;
1857 val_2 = set_2[idx_2];
1858 } else {
1859 ++idx_1;
1860 ++idx_2;
1861 ++pos;
1862 if (idx_1 >= size_1 || idx_2 >= size_2) break;
1863 val_1 = set_1[idx_1];
1864 val_2 = set_2[idx_2];
1865 }
1866 }
1867
1868 if (idx_1 < size_1) {
1869 const size_t n_elems = size_1 - idx_1;
1870 pos += n_elems;
1871 } else if (idx_2 < size_2) {
1872 const size_t n_elems = size_2 - idx_2;
1873 pos += n_elems;
1874 }
1875 return pos;
1876 }
1877
1878
1879
1880 size_t fast_union_uint16(const uint16_t *set_1, size_t size_1, const uint16_t *set_2,
1881 size_t size_2, uint16_t *buffer) {
1882 #ifdef ROARING_VECTOR_OPERATIONS_ENABLED
1883 // compute union with smallest array first
1884 if (size_1 < size_2) {
1885 return union_vector16(set_1, (uint32_t)size_1,
1886 set_2, (uint32_t)size_2, buffer);
1887 } else {
1888 return union_vector16(set_2, (uint32_t)size_2,
1889 set_1, (uint32_t)size_1, buffer);
1890 }
1891 #else
1892 // compute union with smallest array first
1893 if (size_1 < size_2) {
1894 return union_uint16(
1895 set_1, size_1, set_2, size_2, buffer);
1896 } else {
1897 return union_uint16(
1898 set_2, size_2, set_1, size_1, buffer);
1899 }
1900 #endif
1901 }
1902 /* end file src/array_util.c */
1903 /* begin file src/bitset_util.c */
1904 #include <assert.h>
1905 #include <stdint.h>
1906 #include <stdio.h>
1907 #include <stdlib.h>
1908 #include <string.h>
1909
1910
1911 #ifdef IS_X64
1912 static uint8_t lengthTable[256] = {
1913 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4,
1914 2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
1915 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 1, 2, 2, 3, 2, 3, 3, 4,
1916 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
1917 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6,
1918 4, 5, 5, 6, 5, 6, 6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
1919 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5,
1920 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
1921 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6,
1922 4, 5, 5, 6, 5, 6, 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
1923 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8};
1924 #endif
1925
1926 #ifdef USEAVX
1927 ALIGNED(32)
1928 static uint32_t vecDecodeTable[256][8] = {
1929 {0, 0, 0, 0, 0, 0, 0, 0}, /* 0x00 (00000000) */
1930 {1, 0, 0, 0, 0, 0, 0, 0}, /* 0x01 (00000001) */
1931 {2, 0, 0, 0, 0, 0, 0, 0}, /* 0x02 (00000010) */
1932 {1, 2, 0, 0, 0, 0, 0, 0}, /* 0x03 (00000011) */
1933 {3, 0, 0, 0, 0, 0, 0, 0}, /* 0x04 (00000100) */
1934 {1, 3, 0, 0, 0, 0, 0, 0}, /* 0x05 (00000101) */
1935 {2, 3, 0, 0, 0, 0, 0, 0}, /* 0x06 (00000110) */
1936 {1, 2, 3, 0, 0, 0, 0, 0}, /* 0x07 (00000111) */
1937 {4, 0, 0, 0, 0, 0, 0, 0}, /* 0x08 (00001000) */
1938 {1, 4, 0, 0, 0, 0, 0, 0}, /* 0x09 (00001001) */
1939 {2, 4, 0, 0, 0, 0, 0, 0}, /* 0x0A (00001010) */
1940 {1, 2, 4, 0, 0, 0, 0, 0}, /* 0x0B (00001011) */
1941 {3, 4, 0, 0, 0, 0, 0, 0}, /* 0x0C (00001100) */
1942 {1, 3, 4, 0, 0, 0, 0, 0}, /* 0x0D (00001101) */
1943 {2, 3, 4, 0, 0, 0, 0, 0}, /* 0x0E (00001110) */
1944 {1, 2, 3, 4, 0, 0, 0, 0}, /* 0x0F (00001111) */
1945 {5, 0, 0, 0, 0, 0, 0, 0}, /* 0x10 (00010000) */
1946 {1, 5, 0, 0, 0, 0, 0, 0}, /* 0x11 (00010001) */
1947 {2, 5, 0, 0, 0, 0, 0, 0}, /* 0x12 (00010010) */
1948 {1, 2, 5, 0, 0, 0, 0, 0}, /* 0x13 (00010011) */
1949 {3, 5, 0, 0, 0, 0, 0, 0}, /* 0x14 (00010100) */
1950 {1, 3, 5, 0, 0, 0, 0, 0}, /* 0x15 (00010101) */
1951 {2, 3, 5, 0, 0, 0, 0, 0}, /* 0x16 (00010110) */
1952 {1, 2, 3, 5, 0, 0, 0, 0}, /* 0x17 (00010111) */
1953 {4, 5, 0, 0, 0, 0, 0, 0}, /* 0x18 (00011000) */
1954 {1, 4, 5, 0, 0, 0, 0, 0}, /* 0x19 (00011001) */
1955 {2, 4, 5, 0, 0, 0, 0, 0}, /* 0x1A (00011010) */
1956 {1, 2, 4, 5, 0, 0, 0, 0}, /* 0x1B (00011011) */
1957 {3, 4, 5, 0, 0, 0, 0, 0}, /* 0x1C (00011100) */
1958 {1, 3, 4, 5, 0, 0, 0, 0}, /* 0x1D (00011101) */
1959 {2, 3, 4, 5, 0, 0, 0, 0}, /* 0x1E (00011110) */
1960 {1, 2, 3, 4, 5, 0, 0, 0}, /* 0x1F (00011111) */
1961 {6, 0, 0, 0, 0, 0, 0, 0}, /* 0x20 (00100000) */
1962 {1, 6, 0, 0, 0, 0, 0, 0}, /* 0x21 (00100001) */
1963 {2, 6, 0, 0, 0, 0, 0, 0}, /* 0x22 (00100010) */
1964 {1, 2, 6, 0, 0, 0, 0, 0}, /* 0x23 (00100011) */
1965 {3, 6, 0, 0, 0, 0, 0, 0}, /* 0x24 (00100100) */
1966 {1, 3, 6, 0, 0, 0, 0, 0}, /* 0x25 (00100101) */
1967 {2, 3, 6, 0, 0, 0, 0, 0}, /* 0x26 (00100110) */
1968 {1, 2, 3, 6, 0, 0, 0, 0}, /* 0x27 (00100111) */
1969 {4, 6, 0, 0, 0, 0, 0, 0}, /* 0x28 (00101000) */
1970 {1, 4, 6, 0, 0, 0, 0, 0}, /* 0x29 (00101001) */
1971 {2, 4, 6, 0, 0, 0, 0, 0}, /* 0x2A (00101010) */
1972 {1, 2, 4, 6, 0, 0, 0, 0}, /* 0x2B (00101011) */
1973 {3, 4, 6, 0, 0, 0, 0, 0}, /* 0x2C (00101100) */
1974 {1, 3, 4, 6, 0, 0, 0, 0}, /* 0x2D (00101101) */
1975 {2, 3, 4, 6, 0, 0, 0, 0}, /* 0x2E (00101110) */
1976 {1, 2, 3, 4, 6, 0, 0, 0}, /* 0x2F (00101111) */
1977 {5, 6, 0, 0, 0, 0, 0, 0}, /* 0x30 (00110000) */
1978 {1, 5, 6, 0, 0, 0, 0, 0}, /* 0x31 (00110001) */
1979 {2, 5, 6, 0, 0, 0, 0, 0}, /* 0x32 (00110010) */
1980 {1, 2, 5, 6, 0, 0, 0, 0}, /* 0x33 (00110011) */
1981 {3, 5, 6, 0, 0, 0, 0, 0}, /* 0x34 (00110100) */
1982 {1, 3, 5, 6, 0, 0, 0, 0}, /* 0x35 (00110101) */
1983 {2, 3, 5, 6, 0, 0, 0, 0}, /* 0x36 (00110110) */
1984 {1, 2, 3, 5, 6, 0, 0, 0}, /* 0x37 (00110111) */
1985 {4, 5, 6, 0, 0, 0, 0, 0}, /* 0x38 (00111000) */
1986 {1, 4, 5, 6, 0, 0, 0, 0}, /* 0x39 (00111001) */
1987 {2, 4, 5, 6, 0, 0, 0, 0}, /* 0x3A (00111010) */
1988 {1, 2, 4, 5, 6, 0, 0, 0}, /* 0x3B (00111011) */
1989 {3, 4, 5, 6, 0, 0, 0, 0}, /* 0x3C (00111100) */
1990 {1, 3, 4, 5, 6, 0, 0, 0}, /* 0x3D (00111101) */
1991 {2, 3, 4, 5, 6, 0, 0, 0}, /* 0x3E (00111110) */
1992 {1, 2, 3, 4, 5, 6, 0, 0}, /* 0x3F (00111111) */
1993 {7, 0, 0, 0, 0, 0, 0, 0}, /* 0x40 (01000000) */
1994 {1, 7, 0, 0, 0, 0, 0, 0}, /* 0x41 (01000001) */
1995 {2, 7, 0, 0, 0, 0, 0, 0}, /* 0x42 (01000010) */
1996 {1, 2, 7, 0, 0, 0, 0, 0}, /* 0x43 (01000011) */
1997 {3, 7, 0, 0, 0, 0, 0, 0}, /* 0x44 (01000100) */
1998 {1, 3, 7, 0, 0, 0, 0, 0}, /* 0x45 (01000101) */
1999 {2, 3, 7, 0, 0, 0, 0, 0}, /* 0x46 (01000110) */
2000 {1, 2, 3, 7, 0, 0, 0, 0}, /* 0x47 (01000111) */
2001 {4, 7, 0, 0, 0, 0, 0, 0}, /* 0x48 (01001000) */
2002 {1, 4, 7, 0, 0, 0, 0, 0}, /* 0x49 (01001001) */
2003 {2, 4, 7, 0, 0, 0, 0, 0}, /* 0x4A (01001010) */
2004 {1, 2, 4, 7, 0, 0, 0, 0}, /* 0x4B (01001011) */
2005 {3, 4, 7, 0, 0, 0, 0, 0}, /* 0x4C (01001100) */
2006 {1, 3, 4, 7, 0, 0, 0, 0}, /* 0x4D (01001101) */
2007 {2, 3, 4, 7, 0, 0, 0, 0}, /* 0x4E (01001110) */
2008 {1, 2, 3, 4, 7, 0, 0, 0}, /* 0x4F (01001111) */
2009 {5, 7, 0, 0, 0, 0, 0, 0}, /* 0x50 (01010000) */
2010 {1, 5, 7, 0, 0, 0, 0, 0}, /* 0x51 (01010001) */
2011 {2, 5, 7, 0, 0, 0, 0, 0}, /* 0x52 (01010010) */
2012 {1, 2, 5, 7, 0, 0, 0, 0}, /* 0x53 (01010011) */
2013 {3, 5, 7, 0, 0, 0, 0, 0}, /* 0x54 (01010100) */
2014 {1, 3, 5, 7, 0, 0, 0, 0}, /* 0x55 (01010101) */
2015 {2, 3, 5, 7, 0, 0, 0, 0}, /* 0x56 (01010110) */
2016 {1, 2, 3, 5, 7, 0, 0, 0}, /* 0x57 (01010111) */
2017 {4, 5, 7, 0, 0, 0, 0, 0}, /* 0x58 (01011000) */
2018 {1, 4, 5, 7, 0, 0, 0, 0}, /* 0x59 (01011001) */
2019 {2, 4, 5, 7, 0, 0, 0, 0}, /* 0x5A (01011010) */
2020 {1, 2, 4, 5, 7, 0, 0, 0}, /* 0x5B (01011011) */
2021 {3, 4, 5, 7, 0, 0, 0, 0}, /* 0x5C (01011100) */
2022 {1, 3, 4, 5, 7, 0, 0, 0}, /* 0x5D (01011101) */
2023 {2, 3, 4, 5, 7, 0, 0, 0}, /* 0x5E (01011110) */
2024 {1, 2, 3, 4, 5, 7, 0, 0}, /* 0x5F (01011111) */
2025 {6, 7, 0, 0, 0, 0, 0, 0}, /* 0x60 (01100000) */
2026 {1, 6, 7, 0, 0, 0, 0, 0}, /* 0x61 (01100001) */
2027 {2, 6, 7, 0, 0, 0, 0, 0}, /* 0x62 (01100010) */
2028 {1, 2, 6, 7, 0, 0, 0, 0}, /* 0x63 (01100011) */
2029 {3, 6, 7, 0, 0, 0, 0, 0}, /* 0x64 (01100100) */
2030 {1, 3, 6, 7, 0, 0, 0, 0}, /* 0x65 (01100101) */
2031 {2, 3, 6, 7, 0, 0, 0, 0}, /* 0x66 (01100110) */
2032 {1, 2, 3, 6, 7, 0, 0, 0}, /* 0x67 (01100111) */
2033 {4, 6, 7, 0, 0, 0, 0, 0}, /* 0x68 (01101000) */
2034 {1, 4, 6, 7, 0, 0, 0, 0}, /* 0x69 (01101001) */
2035 {2, 4, 6, 7, 0, 0, 0, 0}, /* 0x6A (01101010) */
2036 {1, 2, 4, 6, 7, 0, 0, 0}, /* 0x6B (01101011) */
2037 {3, 4, 6, 7, 0, 0, 0, 0}, /* 0x6C (01101100) */
2038 {1, 3, 4, 6, 7, 0, 0, 0}, /* 0x6D (01101101) */
2039 {2, 3, 4, 6, 7, 0, 0, 0}, /* 0x6E (01101110) */
2040 {1, 2, 3, 4, 6, 7, 0, 0}, /* 0x6F (01101111) */
2041 {5, 6, 7, 0, 0, 0, 0, 0}, /* 0x70 (01110000) */
2042 {1, 5, 6, 7, 0, 0, 0, 0}, /* 0x71 (01110001) */
2043 {2, 5, 6, 7, 0, 0, 0, 0}, /* 0x72 (01110010) */
2044 {1, 2, 5, 6, 7, 0, 0, 0}, /* 0x73 (01110011) */
2045 {3, 5, 6, 7, 0, 0, 0, 0}, /* 0x74 (01110100) */
2046 {1, 3, 5, 6, 7, 0, 0, 0}, /* 0x75 (01110101) */
2047 {2, 3, 5, 6, 7, 0, 0, 0}, /* 0x76 (01110110) */
2048 {1, 2, 3, 5, 6, 7, 0, 0}, /* 0x77 (01110111) */
2049 {4, 5, 6, 7, 0, 0, 0, 0}, /* 0x78 (01111000) */
2050 {1, 4, 5, 6, 7, 0, 0, 0}, /* 0x79 (01111001) */
2051 {2, 4, 5, 6, 7, 0, 0, 0}, /* 0x7A (01111010) */
2052 {1, 2, 4, 5, 6, 7, 0, 0}, /* 0x7B (01111011) */
2053 {3, 4, 5, 6, 7, 0, 0, 0}, /* 0x7C (01111100) */
2054 {1, 3, 4, 5, 6, 7, 0, 0}, /* 0x7D (01111101) */
2055 {2, 3, 4, 5, 6, 7, 0, 0}, /* 0x7E (01111110) */
2056 {1, 2, 3, 4, 5, 6, 7, 0}, /* 0x7F (01111111) */
2057 {8, 0, 0, 0, 0, 0, 0, 0}, /* 0x80 (10000000) */
2058 {1, 8, 0, 0, 0, 0, 0, 0}, /* 0x81 (10000001) */
2059 {2, 8, 0, 0, 0, 0, 0, 0}, /* 0x82 (10000010) */
2060 {1, 2, 8, 0, 0, 0, 0, 0}, /* 0x83 (10000011) */
2061 {3, 8, 0, 0, 0, 0, 0, 0}, /* 0x84 (10000100) */
2062 {1, 3, 8, 0, 0, 0, 0, 0}, /* 0x85 (10000101) */
2063 {2, 3, 8, 0, 0, 0, 0, 0}, /* 0x86 (10000110) */
2064 {1, 2, 3, 8, 0, 0, 0, 0}, /* 0x87 (10000111) */
2065 {4, 8, 0, 0, 0, 0, 0, 0}, /* 0x88 (10001000) */
2066 {1, 4, 8, 0, 0, 0, 0, 0}, /* 0x89 (10001001) */
2067 {2, 4, 8, 0, 0, 0, 0, 0}, /* 0x8A (10001010) */
2068 {1, 2, 4, 8, 0, 0, 0, 0}, /* 0x8B (10001011) */
2069 {3, 4, 8, 0, 0, 0, 0, 0}, /* 0x8C (10001100) */
2070 {1, 3, 4, 8, 0, 0, 0, 0}, /* 0x8D (10001101) */
2071 {2, 3, 4, 8, 0, 0, 0, 0}, /* 0x8E (10001110) */
2072 {1, 2, 3, 4, 8, 0, 0, 0}, /* 0x8F (10001111) */
2073 {5, 8, 0, 0, 0, 0, 0, 0}, /* 0x90 (10010000) */
2074 {1, 5, 8, 0, 0, 0, 0, 0}, /* 0x91 (10010001) */
2075 {2, 5, 8, 0, 0, 0, 0, 0}, /* 0x92 (10010010) */
2076 {1, 2, 5, 8, 0, 0, 0, 0}, /* 0x93 (10010011) */
2077 {3, 5, 8, 0, 0, 0, 0, 0}, /* 0x94 (10010100) */
2078 {1, 3, 5, 8, 0, 0, 0, 0}, /* 0x95 (10010101) */
2079 {2, 3, 5, 8, 0, 0, 0, 0}, /* 0x96 (10010110) */
2080 {1, 2, 3, 5, 8, 0, 0, 0}, /* 0x97 (10010111) */
2081 {4, 5, 8, 0, 0, 0, 0, 0}, /* 0x98 (10011000) */
2082 {1, 4, 5, 8, 0, 0, 0, 0}, /* 0x99 (10011001) */
2083 {2, 4, 5, 8, 0, 0, 0, 0}, /* 0x9A (10011010) */
2084 {1, 2, 4, 5, 8, 0, 0, 0}, /* 0x9B (10011011) */
2085 {3, 4, 5, 8, 0, 0, 0, 0}, /* 0x9C (10011100) */
2086 {1, 3, 4, 5, 8, 0, 0, 0}, /* 0x9D (10011101) */
2087 {2, 3, 4, 5, 8, 0, 0, 0}, /* 0x9E (10011110) */
2088 {1, 2, 3, 4, 5, 8, 0, 0}, /* 0x9F (10011111) */
2089 {6, 8, 0, 0, 0, 0, 0, 0}, /* 0xA0 (10100000) */
2090 {1, 6, 8, 0, 0, 0, 0, 0}, /* 0xA1 (10100001) */
2091 {2, 6, 8, 0, 0, 0, 0, 0}, /* 0xA2 (10100010) */
2092 {1, 2, 6, 8, 0, 0, 0, 0}, /* 0xA3 (10100011) */
2093 {3, 6, 8, 0, 0, 0, 0, 0}, /* 0xA4 (10100100) */
2094 {1, 3, 6, 8, 0, 0, 0, 0}, /* 0xA5 (10100101) */
2095 {2, 3, 6, 8, 0, 0, 0, 0}, /* 0xA6 (10100110) */
2096 {1, 2, 3, 6, 8, 0, 0, 0}, /* 0xA7 (10100111) */
2097 {4, 6, 8, 0, 0, 0, 0, 0}, /* 0xA8 (10101000) */
2098 {1, 4, 6, 8, 0, 0, 0, 0}, /* 0xA9 (10101001) */
2099 {2, 4, 6, 8, 0, 0, 0, 0}, /* 0xAA (10101010) */
2100 {1, 2, 4, 6, 8, 0, 0, 0}, /* 0xAB (10101011) */
2101 {3, 4, 6, 8, 0, 0, 0, 0}, /* 0xAC (10101100) */
2102 {1, 3, 4, 6, 8, 0, 0, 0}, /* 0xAD (10101101) */
2103 {2, 3, 4, 6, 8, 0, 0, 0}, /* 0xAE (10101110) */
2104 {1, 2, 3, 4, 6, 8, 0, 0}, /* 0xAF (10101111) */
2105 {5, 6, 8, 0, 0, 0, 0, 0}, /* 0xB0 (10110000) */
2106 {1, 5, 6, 8, 0, 0, 0, 0}, /* 0xB1 (10110001) */
2107 {2, 5, 6, 8, 0, 0, 0, 0}, /* 0xB2 (10110010) */
2108 {1, 2, 5, 6, 8, 0, 0, 0}, /* 0xB3 (10110011) */
2109 {3, 5, 6, 8, 0, 0, 0, 0}, /* 0xB4 (10110100) */
2110 {1, 3, 5, 6, 8, 0, 0, 0}, /* 0xB5 (10110101) */
2111 {2, 3, 5, 6, 8, 0, 0, 0}, /* 0xB6 (10110110) */
2112 {1, 2, 3, 5, 6, 8, 0, 0}, /* 0xB7 (10110111) */
2113 {4, 5, 6, 8, 0, 0, 0, 0}, /* 0xB8 (10111000) */
2114 {1, 4, 5, 6, 8, 0, 0, 0}, /* 0xB9 (10111001) */
2115 {2, 4, 5, 6, 8, 0, 0, 0}, /* 0xBA (10111010) */
2116 {1, 2, 4, 5, 6, 8, 0, 0}, /* 0xBB (10111011) */
2117 {3, 4, 5, 6, 8, 0, 0, 0}, /* 0xBC (10111100) */
2118 {1, 3, 4, 5, 6, 8, 0, 0}, /* 0xBD (10111101) */
2119 {2, 3, 4, 5, 6, 8, 0, 0}, /* 0xBE (10111110) */
2120 {1, 2, 3, 4, 5, 6, 8, 0}, /* 0xBF (10111111) */
2121 {7, 8, 0, 0, 0, 0, 0, 0}, /* 0xC0 (11000000) */
2122 {1, 7, 8, 0, 0, 0, 0, 0}, /* 0xC1 (11000001) */
2123 {2, 7, 8, 0, 0, 0, 0, 0}, /* 0xC2 (11000010) */
2124 {1, 2, 7, 8, 0, 0, 0, 0}, /* 0xC3 (11000011) */
2125 {3, 7, 8, 0, 0, 0, 0, 0}, /* 0xC4 (11000100) */
2126 {1, 3, 7, 8, 0, 0, 0, 0}, /* 0xC5 (11000101) */
2127 {2, 3, 7, 8, 0, 0, 0, 0}, /* 0xC6 (11000110) */
2128 {1, 2, 3, 7, 8, 0, 0, 0}, /* 0xC7 (11000111) */
2129 {4, 7, 8, 0, 0, 0, 0, 0}, /* 0xC8 (11001000) */
2130 {1, 4, 7, 8, 0, 0, 0, 0}, /* 0xC9 (11001001) */
2131 {2, 4, 7, 8, 0, 0, 0, 0}, /* 0xCA (11001010) */
2132 {1, 2, 4, 7, 8, 0, 0, 0}, /* 0xCB (11001011) */
2133 {3, 4, 7, 8, 0, 0, 0, 0}, /* 0xCC (11001100) */
2134 {1, 3, 4, 7, 8, 0, 0, 0}, /* 0xCD (11001101) */
2135 {2, 3, 4, 7, 8, 0, 0, 0}, /* 0xCE (11001110) */
2136 {1, 2, 3, 4, 7, 8, 0, 0}, /* 0xCF (11001111) */
2137 {5, 7, 8, 0, 0, 0, 0, 0}, /* 0xD0 (11010000) */
2138 {1, 5, 7, 8, 0, 0, 0, 0}, /* 0xD1 (11010001) */
2139 {2, 5, 7, 8, 0, 0, 0, 0}, /* 0xD2 (11010010) */
2140 {1, 2, 5, 7, 8, 0, 0, 0}, /* 0xD3 (11010011) */
2141 {3, 5, 7, 8, 0, 0, 0, 0}, /* 0xD4 (11010100) */
2142 {1, 3, 5, 7, 8, 0, 0, 0}, /* 0xD5 (11010101) */
2143 {2, 3, 5, 7, 8, 0, 0, 0}, /* 0xD6 (11010110) */
2144 {1, 2, 3, 5, 7, 8, 0, 0}, /* 0xD7 (11010111) */
2145 {4, 5, 7, 8, 0, 0, 0, 0}, /* 0xD8 (11011000) */
2146 {1, 4, 5, 7, 8, 0, 0, 0}, /* 0xD9 (11011001) */
2147 {2, 4, 5, 7, 8, 0, 0, 0}, /* 0xDA (11011010) */
2148 {1, 2, 4, 5, 7, 8, 0, 0}, /* 0xDB (11011011) */
2149 {3, 4, 5, 7, 8, 0, 0, 0}, /* 0xDC (11011100) */
2150 {1, 3, 4, 5, 7, 8, 0, 0}, /* 0xDD (11011101) */
2151 {2, 3, 4, 5, 7, 8, 0, 0}, /* 0xDE (11011110) */
2152 {1, 2, 3, 4, 5, 7, 8, 0}, /* 0xDF (11011111) */
2153 {6, 7, 8, 0, 0, 0, 0, 0}, /* 0xE0 (11100000) */
2154 {1, 6, 7, 8, 0, 0, 0, 0}, /* 0xE1 (11100001) */
2155 {2, 6, 7, 8, 0, 0, 0, 0}, /* 0xE2 (11100010) */
2156 {1, 2, 6, 7, 8, 0, 0, 0}, /* 0xE3 (11100011) */
2157 {3, 6, 7, 8, 0, 0, 0, 0}, /* 0xE4 (11100100) */
2158 {1, 3, 6, 7, 8, 0, 0, 0}, /* 0xE5 (11100101) */
2159 {2, 3, 6, 7, 8, 0, 0, 0}, /* 0xE6 (11100110) */
2160 {1, 2, 3, 6, 7, 8, 0, 0}, /* 0xE7 (11100111) */
2161 {4, 6, 7, 8, 0, 0, 0, 0}, /* 0xE8 (11101000) */
2162 {1, 4, 6, 7, 8, 0, 0, 0}, /* 0xE9 (11101001) */
2163 {2, 4, 6, 7, 8, 0, 0, 0}, /* 0xEA (11101010) */
2164 {1, 2, 4, 6, 7, 8, 0, 0}, /* 0xEB (11101011) */
2165 {3, 4, 6, 7, 8, 0, 0, 0}, /* 0xEC (11101100) */
2166 {1, 3, 4, 6, 7, 8, 0, 0}, /* 0xED (11101101) */
2167 {2, 3, 4, 6, 7, 8, 0, 0}, /* 0xEE (11101110) */
2168 {1, 2, 3, 4, 6, 7, 8, 0}, /* 0xEF (11101111) */
2169 {5, 6, 7, 8, 0, 0, 0, 0}, /* 0xF0 (11110000) */
2170 {1, 5, 6, 7, 8, 0, 0, 0}, /* 0xF1 (11110001) */
2171 {2, 5, 6, 7, 8, 0, 0, 0}, /* 0xF2 (11110010) */
2172 {1, 2, 5, 6, 7, 8, 0, 0}, /* 0xF3 (11110011) */
2173 {3, 5, 6, 7, 8, 0, 0, 0}, /* 0xF4 (11110100) */
2174 {1, 3, 5, 6, 7, 8, 0, 0}, /* 0xF5 (11110101) */
2175 {2, 3, 5, 6, 7, 8, 0, 0}, /* 0xF6 (11110110) */
2176 {1, 2, 3, 5, 6, 7, 8, 0}, /* 0xF7 (11110111) */
2177 {4, 5, 6, 7, 8, 0, 0, 0}, /* 0xF8 (11111000) */
2178 {1, 4, 5, 6, 7, 8, 0, 0}, /* 0xF9 (11111001) */
2179 {2, 4, 5, 6, 7, 8, 0, 0}, /* 0xFA (11111010) */
2180 {1, 2, 4, 5, 6, 7, 8, 0}, /* 0xFB (11111011) */
2181 {3, 4, 5, 6, 7, 8, 0, 0}, /* 0xFC (11111100) */
2182 {1, 3, 4, 5, 6, 7, 8, 0}, /* 0xFD (11111101) */
2183 {2, 3, 4, 5, 6, 7, 8, 0}, /* 0xFE (11111110) */
2184 {1, 2, 3, 4, 5, 6, 7, 8} /* 0xFF (11111111) */
2185 };
2186
2187 #endif // #ifdef USEAVX
2188
2189 #ifdef IS_X64
2190 // same as vecDecodeTable but in 16 bits
2191 ALIGNED(32)
2192 static uint16_t vecDecodeTable_uint16[256][8] = {
2193 {0, 0, 0, 0, 0, 0, 0, 0}, /* 0x00 (00000000) */
2194 {1, 0, 0, 0, 0, 0, 0, 0}, /* 0x01 (00000001) */
2195 {2, 0, 0, 0, 0, 0, 0, 0}, /* 0x02 (00000010) */
2196 {1, 2, 0, 0, 0, 0, 0, 0}, /* 0x03 (00000011) */
2197 {3, 0, 0, 0, 0, 0, 0, 0}, /* 0x04 (00000100) */
2198 {1, 3, 0, 0, 0, 0, 0, 0}, /* 0x05 (00000101) */
2199 {2, 3, 0, 0, 0, 0, 0, 0}, /* 0x06 (00000110) */
2200 {1, 2, 3, 0, 0, 0, 0, 0}, /* 0x07 (00000111) */
2201 {4, 0, 0, 0, 0, 0, 0, 0}, /* 0x08 (00001000) */
2202 {1, 4, 0, 0, 0, 0, 0, 0}, /* 0x09 (00001001) */
2203 {2, 4, 0, 0, 0, 0, 0, 0}, /* 0x0A (00001010) */
2204 {1, 2, 4, 0, 0, 0, 0, 0}, /* 0x0B (00001011) */
2205 {3, 4, 0, 0, 0, 0, 0, 0}, /* 0x0C (00001100) */
2206 {1, 3, 4, 0, 0, 0, 0, 0}, /* 0x0D (00001101) */
2207 {2, 3, 4, 0, 0, 0, 0, 0}, /* 0x0E (00001110) */
2208 {1, 2, 3, 4, 0, 0, 0, 0}, /* 0x0F (00001111) */
2209 {5, 0, 0, 0, 0, 0, 0, 0}, /* 0x10 (00010000) */
2210 {1, 5, 0, 0, 0, 0, 0, 0}, /* 0x11 (00010001) */
2211 {2, 5, 0, 0, 0, 0, 0, 0}, /* 0x12 (00010010) */
2212 {1, 2, 5, 0, 0, 0, 0, 0}, /* 0x13 (00010011) */
2213 {3, 5, 0, 0, 0, 0, 0, 0}, /* 0x14 (00010100) */
2214 {1, 3, 5, 0, 0, 0, 0, 0}, /* 0x15 (00010101) */
2215 {2, 3, 5, 0, 0, 0, 0, 0}, /* 0x16 (00010110) */
2216 {1, 2, 3, 5, 0, 0, 0, 0}, /* 0x17 (00010111) */
2217 {4, 5, 0, 0, 0, 0, 0, 0}, /* 0x18 (00011000) */
2218 {1, 4, 5, 0, 0, 0, 0, 0}, /* 0x19 (00011001) */
2219 {2, 4, 5, 0, 0, 0, 0, 0}, /* 0x1A (00011010) */
2220 {1, 2, 4, 5, 0, 0, 0, 0}, /* 0x1B (00011011) */
2221 {3, 4, 5, 0, 0, 0, 0, 0}, /* 0x1C (00011100) */
2222 {1, 3, 4, 5, 0, 0, 0, 0}, /* 0x1D (00011101) */
2223 {2, 3, 4, 5, 0, 0, 0, 0}, /* 0x1E (00011110) */
2224 {1, 2, 3, 4, 5, 0, 0, 0}, /* 0x1F (00011111) */
2225 {6, 0, 0, 0, 0, 0, 0, 0}, /* 0x20 (00100000) */
2226 {1, 6, 0, 0, 0, 0, 0, 0}, /* 0x21 (00100001) */
2227 {2, 6, 0, 0, 0, 0, 0, 0}, /* 0x22 (00100010) */
2228 {1, 2, 6, 0, 0, 0, 0, 0}, /* 0x23 (00100011) */
2229 {3, 6, 0, 0, 0, 0, 0, 0}, /* 0x24 (00100100) */
2230 {1, 3, 6, 0, 0, 0, 0, 0}, /* 0x25 (00100101) */
2231 {2, 3, 6, 0, 0, 0, 0, 0}, /* 0x26 (00100110) */
2232 {1, 2, 3, 6, 0, 0, 0, 0}, /* 0x27 (00100111) */
2233 {4, 6, 0, 0, 0, 0, 0, 0}, /* 0x28 (00101000) */
2234 {1, 4, 6, 0, 0, 0, 0, 0}, /* 0x29 (00101001) */
2235 {2, 4, 6, 0, 0, 0, 0, 0}, /* 0x2A (00101010) */
2236 {1, 2, 4, 6, 0, 0, 0, 0}, /* 0x2B (00101011) */
2237 {3, 4, 6, 0, 0, 0, 0, 0}, /* 0x2C (00101100) */
2238 {1, 3, 4, 6, 0, 0, 0, 0}, /* 0x2D (00101101) */
2239 {2, 3, 4, 6, 0, 0, 0, 0}, /* 0x2E (00101110) */
2240 {1, 2, 3, 4, 6, 0, 0, 0}, /* 0x2F (00101111) */
2241 {5, 6, 0, 0, 0, 0, 0, 0}, /* 0x30 (00110000) */
2242 {1, 5, 6, 0, 0, 0, 0, 0}, /* 0x31 (00110001) */
2243 {2, 5, 6, 0, 0, 0, 0, 0}, /* 0x32 (00110010) */
2244 {1, 2, 5, 6, 0, 0, 0, 0}, /* 0x33 (00110011) */
2245 {3, 5, 6, 0, 0, 0, 0, 0}, /* 0x34 (00110100) */
2246 {1, 3, 5, 6, 0, 0, 0, 0}, /* 0x35 (00110101) */
2247 {2, 3, 5, 6, 0, 0, 0, 0}, /* 0x36 (00110110) */
2248 {1, 2, 3, 5, 6, 0, 0, 0}, /* 0x37 (00110111) */
2249 {4, 5, 6, 0, 0, 0, 0, 0}, /* 0x38 (00111000) */
2250 {1, 4, 5, 6, 0, 0, 0, 0}, /* 0x39 (00111001) */
2251 {2, 4, 5, 6, 0, 0, 0, 0}, /* 0x3A (00111010) */
2252 {1, 2, 4, 5, 6, 0, 0, 0}, /* 0x3B (00111011) */
2253 {3, 4, 5, 6, 0, 0, 0, 0}, /* 0x3C (00111100) */
2254 {1, 3, 4, 5, 6, 0, 0, 0}, /* 0x3D (00111101) */
2255 {2, 3, 4, 5, 6, 0, 0, 0}, /* 0x3E (00111110) */
2256 {1, 2, 3, 4, 5, 6, 0, 0}, /* 0x3F (00111111) */
2257 {7, 0, 0, 0, 0, 0, 0, 0}, /* 0x40 (01000000) */
2258 {1, 7, 0, 0, 0, 0, 0, 0}, /* 0x41 (01000001) */
2259 {2, 7, 0, 0, 0, 0, 0, 0}, /* 0x42 (01000010) */
2260 {1, 2, 7, 0, 0, 0, 0, 0}, /* 0x43 (01000011) */
2261 {3, 7, 0, 0, 0, 0, 0, 0}, /* 0x44 (01000100) */
2262 {1, 3, 7, 0, 0, 0, 0, 0}, /* 0x45 (01000101) */
2263 {2, 3, 7, 0, 0, 0, 0, 0}, /* 0x46 (01000110) */
2264 {1, 2, 3, 7, 0, 0, 0, 0}, /* 0x47 (01000111) */
2265 {4, 7, 0, 0, 0, 0, 0, 0}, /* 0x48 (01001000) */
2266 {1, 4, 7, 0, 0, 0, 0, 0}, /* 0x49 (01001001) */
2267 {2, 4, 7, 0, 0, 0, 0, 0}, /* 0x4A (01001010) */
2268 {1, 2, 4, 7, 0, 0, 0, 0}, /* 0x4B (01001011) */
2269 {3, 4, 7, 0, 0, 0, 0, 0}, /* 0x4C (01001100) */
2270 {1, 3, 4, 7, 0, 0, 0, 0}, /* 0x4D (01001101) */
2271 {2, 3, 4, 7, 0, 0, 0, 0}, /* 0x4E (01001110) */
2272 {1, 2, 3, 4, 7, 0, 0, 0}, /* 0x4F (01001111) */
2273 {5, 7, 0, 0, 0, 0, 0, 0}, /* 0x50 (01010000) */
2274 {1, 5, 7, 0, 0, 0, 0, 0}, /* 0x51 (01010001) */
2275 {2, 5, 7, 0, 0, 0, 0, 0}, /* 0x52 (01010010) */
2276 {1, 2, 5, 7, 0, 0, 0, 0}, /* 0x53 (01010011) */
2277 {3, 5, 7, 0, 0, 0, 0, 0}, /* 0x54 (01010100) */
2278 {1, 3, 5, 7, 0, 0, 0, 0}, /* 0x55 (01010101) */
2279 {2, 3, 5, 7, 0, 0, 0, 0}, /* 0x56 (01010110) */
2280 {1, 2, 3, 5, 7, 0, 0, 0}, /* 0x57 (01010111) */
2281 {4, 5, 7, 0, 0, 0, 0, 0}, /* 0x58 (01011000) */
2282 {1, 4, 5, 7, 0, 0, 0, 0}, /* 0x59 (01011001) */
2283 {2, 4, 5, 7, 0, 0, 0, 0}, /* 0x5A (01011010) */
2284 {1, 2, 4, 5, 7, 0, 0, 0}, /* 0x5B (01011011) */
2285 {3, 4, 5, 7, 0, 0, 0, 0}, /* 0x5C (01011100) */
2286 {1, 3, 4, 5, 7, 0, 0, 0}, /* 0x5D (01011101) */
2287 {2, 3, 4, 5, 7, 0, 0, 0}, /* 0x5E (01011110) */
2288 {1, 2, 3, 4, 5, 7, 0, 0}, /* 0x5F (01011111) */
2289 {6, 7, 0, 0, 0, 0, 0, 0}, /* 0x60 (01100000) */
2290 {1, 6, 7, 0, 0, 0, 0, 0}, /* 0x61 (01100001) */
2291 {2, 6, 7, 0, 0, 0, 0, 0}, /* 0x62 (01100010) */
2292 {1, 2, 6, 7, 0, 0, 0, 0}, /* 0x63 (01100011) */
2293 {3, 6, 7, 0, 0, 0, 0, 0}, /* 0x64 (01100100) */
2294 {1, 3, 6, 7, 0, 0, 0, 0}, /* 0x65 (01100101) */
2295 {2, 3, 6, 7, 0, 0, 0, 0}, /* 0x66 (01100110) */
2296 {1, 2, 3, 6, 7, 0, 0, 0}, /* 0x67 (01100111) */
2297 {4, 6, 7, 0, 0, 0, 0, 0}, /* 0x68 (01101000) */
2298 {1, 4, 6, 7, 0, 0, 0, 0}, /* 0x69 (01101001) */
2299 {2, 4, 6, 7, 0, 0, 0, 0}, /* 0x6A (01101010) */
2300 {1, 2, 4, 6, 7, 0, 0, 0}, /* 0x6B (01101011) */
2301 {3, 4, 6, 7, 0, 0, 0, 0}, /* 0x6C (01101100) */
2302 {1, 3, 4, 6, 7, 0, 0, 0}, /* 0x6D (01101101) */
2303 {2, 3, 4, 6, 7, 0, 0, 0}, /* 0x6E (01101110) */
2304 {1, 2, 3, 4, 6, 7, 0, 0}, /* 0x6F (01101111) */
2305 {5, 6, 7, 0, 0, 0, 0, 0}, /* 0x70 (01110000) */
2306 {1, 5, 6, 7, 0, 0, 0, 0}, /* 0x71 (01110001) */
2307 {2, 5, 6, 7, 0, 0, 0, 0}, /* 0x72 (01110010) */
2308 {1, 2, 5, 6, 7, 0, 0, 0}, /* 0x73 (01110011) */
2309 {3, 5, 6, 7, 0, 0, 0, 0}, /* 0x74 (01110100) */
2310 {1, 3, 5, 6, 7, 0, 0, 0}, /* 0x75 (01110101) */
2311 {2, 3, 5, 6, 7, 0, 0, 0}, /* 0x76 (01110110) */
2312 {1, 2, 3, 5, 6, 7, 0, 0}, /* 0x77 (01110111) */
2313 {4, 5, 6, 7, 0, 0, 0, 0}, /* 0x78 (01111000) */
2314 {1, 4, 5, 6, 7, 0, 0, 0}, /* 0x79 (01111001) */
2315 {2, 4, 5, 6, 7, 0, 0, 0}, /* 0x7A (01111010) */
2316 {1, 2, 4, 5, 6, 7, 0, 0}, /* 0x7B (01111011) */
2317 {3, 4, 5, 6, 7, 0, 0, 0}, /* 0x7C (01111100) */
2318 {1, 3, 4, 5, 6, 7, 0, 0}, /* 0x7D (01111101) */
2319 {2, 3, 4, 5, 6, 7, 0, 0}, /* 0x7E (01111110) */
2320 {1, 2, 3, 4, 5, 6, 7, 0}, /* 0x7F (01111111) */
2321 {8, 0, 0, 0, 0, 0, 0, 0}, /* 0x80 (10000000) */
2322 {1, 8, 0, 0, 0, 0, 0, 0}, /* 0x81 (10000001) */
2323 {2, 8, 0, 0, 0, 0, 0, 0}, /* 0x82 (10000010) */
2324 {1, 2, 8, 0, 0, 0, 0, 0}, /* 0x83 (10000011) */
2325 {3, 8, 0, 0, 0, 0, 0, 0}, /* 0x84 (10000100) */
2326 {1, 3, 8, 0, 0, 0, 0, 0}, /* 0x85 (10000101) */
2327 {2, 3, 8, 0, 0, 0, 0, 0}, /* 0x86 (10000110) */
2328 {1, 2, 3, 8, 0, 0, 0, 0}, /* 0x87 (10000111) */
2329 {4, 8, 0, 0, 0, 0, 0, 0}, /* 0x88 (10001000) */
2330 {1, 4, 8, 0, 0, 0, 0, 0}, /* 0x89 (10001001) */
2331 {2, 4, 8, 0, 0, 0, 0, 0}, /* 0x8A (10001010) */
2332 {1, 2, 4, 8, 0, 0, 0, 0}, /* 0x8B (10001011) */
2333 {3, 4, 8, 0, 0, 0, 0, 0}, /* 0x8C (10001100) */
2334 {1, 3, 4, 8, 0, 0, 0, 0}, /* 0x8D (10001101) */
2335 {2, 3, 4, 8, 0, 0, 0, 0}, /* 0x8E (10001110) */
2336 {1, 2, 3, 4, 8, 0, 0, 0}, /* 0x8F (10001111) */
2337 {5, 8, 0, 0, 0, 0, 0, 0}, /* 0x90 (10010000) */
2338 {1, 5, 8, 0, 0, 0, 0, 0}, /* 0x91 (10010001) */
2339 {2, 5, 8, 0, 0, 0, 0, 0}, /* 0x92 (10010010) */
2340 {1, 2, 5, 8, 0, 0, 0, 0}, /* 0x93 (10010011) */
2341 {3, 5, 8, 0, 0, 0, 0, 0}, /* 0x94 (10010100) */
2342 {1, 3, 5, 8, 0, 0, 0, 0}, /* 0x95 (10010101) */
2343 {2, 3, 5, 8, 0, 0, 0, 0}, /* 0x96 (10010110) */
2344 {1, 2, 3, 5, 8, 0, 0, 0}, /* 0x97 (10010111) */
2345 {4, 5, 8, 0, 0, 0, 0, 0}, /* 0x98 (10011000) */
2346 {1, 4, 5, 8, 0, 0, 0, 0}, /* 0x99 (10011001) */
2347 {2, 4, 5, 8, 0, 0, 0, 0}, /* 0x9A (10011010) */
2348 {1, 2, 4, 5, 8, 0, 0, 0}, /* 0x9B (10011011) */
2349 {3, 4, 5, 8, 0, 0, 0, 0}, /* 0x9C (10011100) */
2350 {1, 3, 4, 5, 8, 0, 0, 0}, /* 0x9D (10011101) */
2351 {2, 3, 4, 5, 8, 0, 0, 0}, /* 0x9E (10011110) */
2352 {1, 2, 3, 4, 5, 8, 0, 0}, /* 0x9F (10011111) */
2353 {6, 8, 0, 0, 0, 0, 0, 0}, /* 0xA0 (10100000) */
2354 {1, 6, 8, 0, 0, 0, 0, 0}, /* 0xA1 (10100001) */
2355 {2, 6, 8, 0, 0, 0, 0, 0}, /* 0xA2 (10100010) */
2356 {1, 2, 6, 8, 0, 0, 0, 0}, /* 0xA3 (10100011) */
2357 {3, 6, 8, 0, 0, 0, 0, 0}, /* 0xA4 (10100100) */
2358 {1, 3, 6, 8, 0, 0, 0, 0}, /* 0xA5 (10100101) */
2359 {2, 3, 6, 8, 0, 0, 0, 0}, /* 0xA6 (10100110) */
2360 {1, 2, 3, 6, 8, 0, 0, 0}, /* 0xA7 (10100111) */
2361 {4, 6, 8, 0, 0, 0, 0, 0}, /* 0xA8 (10101000) */
2362 {1, 4, 6, 8, 0, 0, 0, 0}, /* 0xA9 (10101001) */
2363 {2, 4, 6, 8, 0, 0, 0, 0}, /* 0xAA (10101010) */
2364 {1, 2, 4, 6, 8, 0, 0, 0}, /* 0xAB (10101011) */
2365 {3, 4, 6, 8, 0, 0, 0, 0}, /* 0xAC (10101100) */
2366 {1, 3, 4, 6, 8, 0, 0, 0}, /* 0xAD (10101101) */
2367 {2, 3, 4, 6, 8, 0, 0, 0}, /* 0xAE (10101110) */
2368 {1, 2, 3, 4, 6, 8, 0, 0}, /* 0xAF (10101111) */
2369 {5, 6, 8, 0, 0, 0, 0, 0}, /* 0xB0 (10110000) */
2370 {1, 5, 6, 8, 0, 0, 0, 0}, /* 0xB1 (10110001) */
2371 {2, 5, 6, 8, 0, 0, 0, 0}, /* 0xB2 (10110010) */
2372 {1, 2, 5, 6, 8, 0, 0, 0}, /* 0xB3 (10110011) */
2373 {3, 5, 6, 8, 0, 0, 0, 0}, /* 0xB4 (10110100) */
2374 {1, 3, 5, 6, 8, 0, 0, 0}, /* 0xB5 (10110101) */
2375 {2, 3, 5, 6, 8, 0, 0, 0}, /* 0xB6 (10110110) */
2376 {1, 2, 3, 5, 6, 8, 0, 0}, /* 0xB7 (10110111) */
2377 {4, 5, 6, 8, 0, 0, 0, 0}, /* 0xB8 (10111000) */
2378 {1, 4, 5, 6, 8, 0, 0, 0}, /* 0xB9 (10111001) */
2379 {2, 4, 5, 6, 8, 0, 0, 0}, /* 0xBA (10111010) */
2380 {1, 2, 4, 5, 6, 8, 0, 0}, /* 0xBB (10111011) */
2381 {3, 4, 5, 6, 8, 0, 0, 0}, /* 0xBC (10111100) */
2382 {1, 3, 4, 5, 6, 8, 0, 0}, /* 0xBD (10111101) */
2383 {2, 3, 4, 5, 6, 8, 0, 0}, /* 0xBE (10111110) */
2384 {1, 2, 3, 4, 5, 6, 8, 0}, /* 0xBF (10111111) */
2385 {7, 8, 0, 0, 0, 0, 0, 0}, /* 0xC0 (11000000) */
2386 {1, 7, 8, 0, 0, 0, 0, 0}, /* 0xC1 (11000001) */
2387 {2, 7, 8, 0, 0, 0, 0, 0}, /* 0xC2 (11000010) */
2388 {1, 2, 7, 8, 0, 0, 0, 0}, /* 0xC3 (11000011) */
2389 {3, 7, 8, 0, 0, 0, 0, 0}, /* 0xC4 (11000100) */
2390 {1, 3, 7, 8, 0, 0, 0, 0}, /* 0xC5 (11000101) */
2391 {2, 3, 7, 8, 0, 0, 0, 0}, /* 0xC6 (11000110) */
2392 {1, 2, 3, 7, 8, 0, 0, 0}, /* 0xC7 (11000111) */
2393 {4, 7, 8, 0, 0, 0, 0, 0}, /* 0xC8 (11001000) */
2394 {1, 4, 7, 8, 0, 0, 0, 0}, /* 0xC9 (11001001) */
2395 {2, 4, 7, 8, 0, 0, 0, 0}, /* 0xCA (11001010) */
2396 {1, 2, 4, 7, 8, 0, 0, 0}, /* 0xCB (11001011) */
2397 {3, 4, 7, 8, 0, 0, 0, 0}, /* 0xCC (11001100) */
2398 {1, 3, 4, 7, 8, 0, 0, 0}, /* 0xCD (11001101) */
2399 {2, 3, 4, 7, 8, 0, 0, 0}, /* 0xCE (11001110) */
2400 {1, 2, 3, 4, 7, 8, 0, 0}, /* 0xCF (11001111) */
2401 {5, 7, 8, 0, 0, 0, 0, 0}, /* 0xD0 (11010000) */
2402 {1, 5, 7, 8, 0, 0, 0, 0}, /* 0xD1 (11010001) */
2403 {2, 5, 7, 8, 0, 0, 0, 0}, /* 0xD2 (11010010) */
2404 {1, 2, 5, 7, 8, 0, 0, 0}, /* 0xD3 (11010011) */
2405 {3, 5, 7, 8, 0, 0, 0, 0}, /* 0xD4 (11010100) */
2406 {1, 3, 5, 7, 8, 0, 0, 0}, /* 0xD5 (11010101) */
2407 {2, 3, 5, 7, 8, 0, 0, 0}, /* 0xD6 (11010110) */
2408 {1, 2, 3, 5, 7, 8, 0, 0}, /* 0xD7 (11010111) */
2409 {4, 5, 7, 8, 0, 0, 0, 0}, /* 0xD8 (11011000) */
2410 {1, 4, 5, 7, 8, 0, 0, 0}, /* 0xD9 (11011001) */
2411 {2, 4, 5, 7, 8, 0, 0, 0}, /* 0xDA (11011010) */
2412 {1, 2, 4, 5, 7, 8, 0, 0}, /* 0xDB (11011011) */
2413 {3, 4, 5, 7, 8, 0, 0, 0}, /* 0xDC (11011100) */
2414 {1, 3, 4, 5, 7, 8, 0, 0}, /* 0xDD (11011101) */
2415 {2, 3, 4, 5, 7, 8, 0, 0}, /* 0xDE (11011110) */
2416 {1, 2, 3, 4, 5, 7, 8, 0}, /* 0xDF (11011111) */
2417 {6, 7, 8, 0, 0, 0, 0, 0}, /* 0xE0 (11100000) */
2418 {1, 6, 7, 8, 0, 0, 0, 0}, /* 0xE1 (11100001) */
2419 {2, 6, 7, 8, 0, 0, 0, 0}, /* 0xE2 (11100010) */
2420 {1, 2, 6, 7, 8, 0, 0, 0}, /* 0xE3 (11100011) */
2421 {3, 6, 7, 8, 0, 0, 0, 0}, /* 0xE4 (11100100) */
2422 {1, 3, 6, 7, 8, 0, 0, 0}, /* 0xE5 (11100101) */
2423 {2, 3, 6, 7, 8, 0, 0, 0}, /* 0xE6 (11100110) */
2424 {1, 2, 3, 6, 7, 8, 0, 0}, /* 0xE7 (11100111) */
2425 {4, 6, 7, 8, 0, 0, 0, 0}, /* 0xE8 (11101000) */
2426 {1, 4, 6, 7, 8, 0, 0, 0}, /* 0xE9 (11101001) */
2427 {2, 4, 6, 7, 8, 0, 0, 0}, /* 0xEA (11101010) */
2428 {1, 2, 4, 6, 7, 8, 0, 0}, /* 0xEB (11101011) */
2429 {3, 4, 6, 7, 8, 0, 0, 0}, /* 0xEC (11101100) */
2430 {1, 3, 4, 6, 7, 8, 0, 0}, /* 0xED (11101101) */
2431 {2, 3, 4, 6, 7, 8, 0, 0}, /* 0xEE (11101110) */
2432 {1, 2, 3, 4, 6, 7, 8, 0}, /* 0xEF (11101111) */
2433 {5, 6, 7, 8, 0, 0, 0, 0}, /* 0xF0 (11110000) */
2434 {1, 5, 6, 7, 8, 0, 0, 0}, /* 0xF1 (11110001) */
2435 {2, 5, 6, 7, 8, 0, 0, 0}, /* 0xF2 (11110010) */
2436 {1, 2, 5, 6, 7, 8, 0, 0}, /* 0xF3 (11110011) */
2437 {3, 5, 6, 7, 8, 0, 0, 0}, /* 0xF4 (11110100) */
2438 {1, 3, 5, 6, 7, 8, 0, 0}, /* 0xF5 (11110101) */
2439 {2, 3, 5, 6, 7, 8, 0, 0}, /* 0xF6 (11110110) */
2440 {1, 2, 3, 5, 6, 7, 8, 0}, /* 0xF7 (11110111) */
2441 {4, 5, 6, 7, 8, 0, 0, 0}, /* 0xF8 (11111000) */
2442 {1, 4, 5, 6, 7, 8, 0, 0}, /* 0xF9 (11111001) */
2443 {2, 4, 5, 6, 7, 8, 0, 0}, /* 0xFA (11111010) */
2444 {1, 2, 4, 5, 6, 7, 8, 0}, /* 0xFB (11111011) */
2445 {3, 4, 5, 6, 7, 8, 0, 0}, /* 0xFC (11111100) */
2446 {1, 3, 4, 5, 6, 7, 8, 0}, /* 0xFD (11111101) */
2447 {2, 3, 4, 5, 6, 7, 8, 0}, /* 0xFE (11111110) */
2448 {1, 2, 3, 4, 5, 6, 7, 8} /* 0xFF (11111111) */
2449 };
2450
2451 #endif
2452
2453 #ifdef USEAVX
2454
2455 size_t bitset_extract_setbits_avx2(uint64_t *array, size_t length, void *vout,
2456 size_t outcapacity, uint32_t base) {
2457 uint32_t *out = (uint32_t *)vout;
2458 uint32_t *initout = out;
2459 __m256i baseVec = _mm256_set1_epi32(base - 1);
2460 __m256i incVec = _mm256_set1_epi32(64);
2461 __m256i add8 = _mm256_set1_epi32(8);
2462 uint32_t *safeout = out + outcapacity;
2463 size_t i = 0;
2464 for (; (i < length) && (out + 64 <= safeout); ++i) {
2465 uint64_t w = array[i];
2466 if (w == 0) {
2467 baseVec = _mm256_add_epi32(baseVec, incVec);
2468 } else {
2469 for (int k = 0; k < 4; ++k) {
2470 uint8_t byteA = (uint8_t)w;
2471 uint8_t byteB = (uint8_t)(w >> 8);
2472 w >>= 16;
2473 __m256i vecA =
2474 _mm256_load_si256((const __m256i *)vecDecodeTable[byteA]);
2475 __m256i vecB =
2476 _mm256_load_si256((const __m256i *)vecDecodeTable[byteB]);
2477 uint8_t advanceA = lengthTable[byteA];
2478 uint8_t advanceB = lengthTable[byteB];
2479 vecA = _mm256_add_epi32(baseVec, vecA);
2480 baseVec = _mm256_add_epi32(baseVec, add8);
2481 vecB = _mm256_add_epi32(baseVec, vecB);
2482 baseVec = _mm256_add_epi32(baseVec, add8);
2483 _mm256_storeu_si256((__m256i *)out, vecA);
2484 out += advanceA;
2485 _mm256_storeu_si256((__m256i *)out, vecB);
2486 out += advanceB;
2487 }
2488 }
2489 }
2490 base += i * 64;
2491 for (; (i < length) && (out < safeout); ++i) {
2492 uint64_t w = array[i];
2493 while ((w != 0) && (out < safeout)) {
2494 uint64_t t = w & (~w + 1); // on x64, should compile to BLSI (careful: the Intel compiler seems to fail)
2495 int r = __builtin_ctzll(w); // on x64, should compile to TZCNT
2496 uint32_t val = r + base;
2497 memcpy(out, &val,
2498 sizeof(uint32_t)); // should be compiled as a MOV on x64
2499 out++;
2500 w ^= t;
2501 }
2502 base += 64;
2503 }
2504 return out - initout;
2505 }
2506 #endif // USEAVX
2507
2508 size_t bitset_extract_setbits(uint64_t *bitset, size_t length, void *vout,
2509 uint32_t base) {
2510 int outpos = 0;
2511 uint32_t *out = (uint32_t *)vout;
2512 for (size_t i = 0; i < length; ++i) {
2513 uint64_t w = bitset[i];
2514 while (w != 0) {
2515 uint64_t t = w & (~w + 1); // on x64, should compile to BLSI (careful: the Intel compiler seems to fail)
2516 int r = __builtin_ctzll(w); // on x64, should compile to TZCNT
2517 uint32_t val = r + base;
2518 memcpy(out + outpos, &val,
2519 sizeof(uint32_t)); // should be compiled as a MOV on x64
2520 outpos++;
2521 w ^= t;
2522 }
2523 base += 64;
2524 }
2525 return outpos;
2526 }
2527
2528 size_t bitset_extract_intersection_setbits_uint16(const uint64_t * __restrict__ bitset1,
2529 const uint64_t * __restrict__ bitset2,
2530 size_t length, uint16_t *out,
2531 uint16_t base) {
2532 int outpos = 0;
2533 for (size_t i = 0; i < length; ++i) {
2534 uint64_t w = bitset1[i] & bitset2[i];
2535 while (w != 0) {
2536 uint64_t t = w & (~w + 1);
2537 int r = __builtin_ctzll(w);
2538 out[outpos++] = r + base;
2539 w ^= t;
2540 }
2541 base += 64;
2542 }
2543 return outpos;
2544 }
2545
2546 #ifdef IS_X64
2547 /*
2548 * Given a bitset containing "length" 64-bit words, write out the position
2549 * of all the set bits to "out" as 16-bit integers, values start at "base" (can
2550 *be set to zero).
2551 *
2552 * The "out" pointer should be sufficient to store the actual number of bits
2553 *set.
2554 *
2555 * Returns how many values were actually decoded.
2556 *
2557 * This function uses SSE decoding.
2558 */
2559 size_t bitset_extract_setbits_sse_uint16(const uint64_t *bitset, size_t length,
2560 uint16_t *out, size_t outcapacity,
2561 uint16_t base) {
2562 uint16_t *initout = out;
2563 __m128i baseVec = _mm_set1_epi16(base - 1);
2564 __m128i incVec = _mm_set1_epi16(64);
2565 __m128i add8 = _mm_set1_epi16(8);
2566 uint16_t *safeout = out + outcapacity;
2567 const int numberofbytes = 2; // process two bytes at a time
2568 size_t i = 0;
2569 for (; (i < length) && (out + numberofbytes * 8 <= safeout); ++i) {
2570 uint64_t w = bitset[i];
2571 if (w == 0) {
2572 baseVec = _mm_add_epi16(baseVec, incVec);
2573 } else {
2574 for (int k = 0; k < 4; ++k) {
2575 uint8_t byteA = (uint8_t)w;
2576 uint8_t byteB = (uint8_t)(w >> 8);
2577 w >>= 16;
2578 __m128i vecA = _mm_load_si128(
2579 (const __m128i *)vecDecodeTable_uint16[byteA]);
2580 __m128i vecB = _mm_load_si128(
2581 (const __m128i *)vecDecodeTable_uint16[byteB]);
2582 uint8_t advanceA = lengthTable[byteA];
2583 uint8_t advanceB = lengthTable[byteB];
2584 vecA = _mm_add_epi16(baseVec, vecA);
2585 baseVec = _mm_add_epi16(baseVec, add8);
2586 vecB = _mm_add_epi16(baseVec, vecB);
2587 baseVec = _mm_add_epi16(baseVec, add8);
2588 _mm_storeu_si128((__m128i *)out, vecA);
2589 out += advanceA;
2590 _mm_storeu_si128((__m128i *)out, vecB);
2591 out += advanceB;
2592 }
2593 }
2594 }
2595 base += (uint16_t)(i * 64);
2596 for (; (i < length) && (out < safeout); ++i) {
2597 uint64_t w = bitset[i];
2598 while ((w != 0) && (out < safeout)) {
2599 uint64_t t = w & (~w + 1);
2600 int r = __builtin_ctzll(w);
2601 *out = r + base;
2602 out++;
2603 w ^= t;
2604 }
2605 base += 64;
2606 }
2607 return out - initout;
2608 }
2609 #endif
2610
2611 /*
2612 * Given a bitset containing "length" 64-bit words, write out the position
2613 * of all the set bits to "out", values start at "base" (can be set to zero).
2614 *
2615 * The "out" pointer should be sufficient to store the actual number of bits
2616 *set.
2617 *
2618 * Returns how many values were actually decoded.
2619 */
2620 size_t bitset_extract_setbits_uint16(const uint64_t *bitset, size_t length,
2621 uint16_t *out, uint16_t base) {
2622 int outpos = 0;
2623 for (size_t i = 0; i < length; ++i) {
2624 uint64_t w = bitset[i];
2625 while (w != 0) {
2626 uint64_t t = w & (~w + 1);
2627 int r = __builtin_ctzll(w);
2628 out[outpos++] = r + base;
2629 w ^= t;
2630 }
2631 base += 64;
2632 }
2633 return outpos;
2634 }
2635
2636 #if defined(ASMBITMANIPOPTIMIZATION)
2637
2638 uint64_t bitset_set_list_withcard(void *bitset, uint64_t card,
2639 const uint16_t *list, uint64_t length) {
2640 uint64_t offset, load, pos;
2641 uint64_t shift = 6;
2642 const uint16_t *end = list + length;
2643 if (!length) return card;
2644 // TODO: could unroll for performance, see bitset_set_list
2645 // bts is not available as an intrinsic in GCC
2646 __asm volatile(
2647 "1:\n"
2648 "movzwq (%[list]), %[pos]\n"
2649 "shrx %[shift], %[pos], %[offset]\n"
2650 "mov (%[bitset],%[offset],8), %[load]\n"
2651 "bts %[pos], %[load]\n"
2652 "mov %[load], (%[bitset],%[offset],8)\n"
2653 "sbb $-1, %[card]\n"
2654 "add $2, %[list]\n"
2655 "cmp %[list], %[end]\n"
2656 "jnz 1b"
2657 : [card] "+&r"(card), [list] "+&r"(list), [load] "=&r"(load),
2658 [pos] "=&r"(pos), [offset] "=&r"(offset)
2659 : [end] "r"(end), [bitset] "r"(bitset), [shift] "r"(shift));
2660 return card;
2661 }
2662
2663 void bitset_set_list(void *bitset, const uint16_t *list, uint64_t length) {
2664 uint64_t pos;
2665 const uint16_t *end = list + length;
2666
2667 uint64_t shift = 6;
2668 uint64_t offset;
2669 uint64_t load;
2670 for (; list + 3 < end; list += 4) {
2671 pos = list[0];
2672 __asm volatile(
2673 "shrx %[shift], %[pos], %[offset]\n"
2674 "mov (%[bitset],%[offset],8), %[load]\n"
2675 "bts %[pos], %[load]\n"
2676 "mov %[load], (%[bitset],%[offset],8)"
2677 : [load] "=&r"(load), [offset] "=&r"(offset)
2678 : [bitset] "r"(bitset), [shift] "r"(shift), [pos] "r"(pos));
2679 pos = list[1];
2680 __asm volatile(
2681 "shrx %[shift], %[pos], %[offset]\n"
2682 "mov (%[bitset],%[offset],8), %[load]\n"
2683 "bts %[pos], %[load]\n"
2684 "mov %[load], (%[bitset],%[offset],8)"
2685 : [load] "=&r"(load), [offset] "=&r"(offset)
2686 : [bitset] "r"(bitset), [shift] "r"(shift), [pos] "r"(pos));
2687 pos = list[2];
2688 __asm volatile(
2689 "shrx %[shift], %[pos], %[offset]\n"
2690 "mov (%[bitset],%[offset],8), %[load]\n"
2691 "bts %[pos], %[load]\n"
2692 "mov %[load], (%[bitset],%[offset],8)"
2693 : [load] "=&r"(load), [offset] "=&r"(offset)
2694 : [bitset] "r"(bitset), [shift] "r"(shift), [pos] "r"(pos));
2695 pos = list[3];
2696 __asm volatile(
2697 "shrx %[shift], %[pos], %[offset]\n"
2698 "mov (%[bitset],%[offset],8), %[load]\n"
2699 "bts %[pos], %[load]\n"
2700 "mov %[load], (%[bitset],%[offset],8)"
2701 : [load] "=&r"(load), [offset] "=&r"(offset)
2702 : [bitset] "r"(bitset), [shift] "r"(shift), [pos] "r"(pos));
2703 }
2704
2705 while (list != end) {
2706 pos = list[0];
2707 __asm volatile(
2708 "shrx %[shift], %[pos], %[offset]\n"
2709 "mov (%[bitset],%[offset],8), %[load]\n"
2710 "bts %[pos], %[load]\n"
2711 "mov %[load], (%[bitset],%[offset],8)"
2712 : [load] "=&r"(load), [offset] "=&r"(offset)
2713 : [bitset] "r"(bitset), [shift] "r"(shift), [pos] "r"(pos));
2714 list++;
2715 }
2716 }
2717
2718 uint64_t bitset_clear_list(void *bitset, uint64_t card, const uint16_t *list,
2719 uint64_t length) {
2720 uint64_t offset, load, pos;
2721 uint64_t shift = 6;
2722 const uint16_t *end = list + length;
2723 if (!length) return card;
2724 // btr is not available as an intrinsic in GCC
2725 __asm volatile(
2726 "1:\n"
2727 "movzwq (%[list]), %[pos]\n"
2728 "shrx %[shift], %[pos], %[offset]\n"
2729 "mov (%[bitset],%[offset],8), %[load]\n"
2730 "btr %[pos], %[load]\n"
2731 "mov %[load], (%[bitset],%[offset],8)\n"
2732 "sbb $0, %[card]\n"
2733 "add $2, %[list]\n"
2734 "cmp %[list], %[end]\n"
2735 "jnz 1b"
2736 : [card] "+&r"(card), [list] "+&r"(list), [load] "=&r"(load),
2737 [pos] "=&r"(pos), [offset] "=&r"(offset)
2738 : [end] "r"(end), [bitset] "r"(bitset), [shift] "r"(shift)
2739 :
2740 /* clobbers */ "memory");
2741 return card;
2742 }
2743
2744 #else
2745 uint64_t bitset_clear_list(void *bitset, uint64_t card, const uint16_t *list,
2746 uint64_t length) {
2747 uint64_t offset, load, newload, pos, index;
2748 const uint16_t *end = list + length;
2749 while (list != end) {
2750 pos = *(const uint16_t *)list;
2751 offset = pos >> 6;
2752 index = pos % 64;
2753 load = ((uint64_t *)bitset)[offset];
2754 newload = load & ~(UINT64_C(1) << index);
2755 card -= (load ^ newload) >> index;
2756 ((uint64_t *)bitset)[offset] = newload;
2757 list++;
2758 }
2759 return card;
2760 }
2761
2762 uint64_t bitset_set_list_withcard(void *bitset, uint64_t card,
2763 const uint16_t *list, uint64_t length) {
2764 uint64_t offset, load, newload, pos, index;
2765 const uint16_t *end = list + length;
2766 while (list != end) {
2767 pos = *(const uint16_t *)list;
2768 offset = pos >> 6;
2769 index = pos % 64;
2770 load = ((uint64_t *)bitset)[offset];
2771 newload = load | (UINT64_C(1) << index);
2772 card += (load ^ newload) >> index;
2773 ((uint64_t *)bitset)[offset] = newload;
2774 list++;
2775 }
2776 return card;
2777 }
2778
2779 void bitset_set_list(void *bitset, const uint16_t *list, uint64_t length) {
2780 uint64_t offset, load, newload, pos, index;
2781 const uint16_t *end = list + length;
2782 while (list != end) {
2783 pos = *(const uint16_t *)list;
2784 offset = pos >> 6;
2785 index = pos % 64;
2786 load = ((uint64_t *)bitset)[offset];
2787 newload = load | (UINT64_C(1) << index);
2788 ((uint64_t *)bitset)[offset] = newload;
2789 list++;
2790 }
2791 }
2792
2793 #endif
2794
2795 /* flip specified bits */
2796 /* TODO: consider whether worthwhile to make an asm version */
2797
2798 uint64_t bitset_flip_list_withcard(void *bitset, uint64_t card,
2799 const uint16_t *list, uint64_t length) {
2800 uint64_t offset, load, newload, pos, index;
2801 const uint16_t *end = list + length;
2802 while (list != end) {
2803 pos = *(const uint16_t *)list;
2804 offset = pos >> 6;
2805 index = pos % 64;
2806 load = ((uint64_t *)bitset)[offset];
2807 newload = load ^ (UINT64_C(1) << index);
2808 // todo: is a branch here all that bad?
2809 card +=
2810 (1 - 2 * (((UINT64_C(1) << index) & load) >> index)); // +1 or -1
2811 ((uint64_t *)bitset)[offset] = newload;
2812 list++;
2813 }
2814 return card;
2815 }
2816
2817 void bitset_flip_list(void *bitset, const uint16_t *list, uint64_t length) {
2818 uint64_t offset, load, newload, pos, index;
2819 const uint16_t *end = list + length;
2820 while (list != end) {
2821 pos = *(const uint16_t *)list;
2822 offset = pos >> 6;
2823 index = pos % 64;
2824 load = ((uint64_t *)bitset)[offset];
2825 newload = load ^ (UINT64_C(1) << index);
2826 ((uint64_t *)bitset)[offset] = newload;
2827 list++;
2828 }
2829 }
2830 /* end file src/bitset_util.c */
2831 /* begin file src/containers/array.c */
2832 /*
2833 * array.c
2834 *
2835 */
2836
2837 #include <assert.h>
2838 #include <stdio.h>
2839 #include <stdlib.h>
2840
2841 extern inline uint16_t array_container_minimum(const array_container_t *arr);
2842 extern inline uint16_t array_container_maximum(const array_container_t *arr);
2843 extern inline int array_container_index_equalorlarger(const array_container_t *arr, uint16_t x);
2844
2845 extern inline int array_container_rank(const array_container_t *arr,
2846 uint16_t x);
2847 extern inline bool array_container_contains(const array_container_t *arr,
2848 uint16_t pos);
2849 extern int array_container_cardinality(const array_container_t *array);
2850 extern bool array_container_nonzero_cardinality(const array_container_t *array);
2851 extern void array_container_clear(array_container_t *array);
2852 extern int32_t array_container_serialized_size_in_bytes(int32_t card);
2853 extern bool array_container_empty(const array_container_t *array);
2854 extern bool array_container_full(const array_container_t *array);
2855
2856 /* Create a new array with capacity size. Return NULL in case of failure. */
2857 array_container_t *array_container_create_given_capacity(int32_t size) {
2858 array_container_t *container;
2859
2860 if ((container = (array_container_t *)malloc(sizeof(array_container_t))) ==
2861 NULL) {
2862 return NULL;
2863 }
2864
2865 if( size <= 0 ) { // we don't want to rely on malloc(0)
2866 container->array = NULL;
2867 } else if ((container->array = (uint16_t *)malloc(sizeof(uint16_t) * size)) ==
2868 NULL) {
2869 free(container);
2870 return NULL;
2871 }
2872
2873 container->capacity = size;
2874 container->cardinality = 0;
2875
2876 return container;
2877 }
2878
2879 /* Create a new array. Return NULL in case of failure. */
2880 array_container_t *array_container_create() {
2881 return array_container_create_given_capacity(ARRAY_DEFAULT_INIT_SIZE);
2882 }
2883
2884 /* Create a new array containing all values in [min,max). */
2885 array_container_t * array_container_create_range(uint32_t min, uint32_t max) {
2886 array_container_t * answer = array_container_create_given_capacity(max - min + 1);
2887 if(answer == NULL) return answer;
2888 answer->cardinality = 0;
2889 for(uint32_t k = min; k < max; k++) {
2890 answer->array[answer->cardinality++] = k;
2891 }
2892 return answer;
2893 }
2894
2895 /* Duplicate container */
2896 array_container_t *array_container_clone(const array_container_t *src) {
2897 array_container_t *newcontainer =
2898 array_container_create_given_capacity(src->capacity);
2899 if (newcontainer == NULL) return NULL;
2900
2901 newcontainer->cardinality = src->cardinality;
2902
2903 memcpy(newcontainer->array, src->array,
2904 src->cardinality * sizeof(uint16_t));
2905
2906 return newcontainer;
2907 }
2908
2909 int array_container_shrink_to_fit(array_container_t *src) {
2910 if (src->cardinality == src->capacity) return 0; // nothing to do
2911 int savings = src->capacity - src->cardinality;
2912 src->capacity = src->cardinality;
2913 if( src->capacity == 0) { // we do not want to rely on realloc for zero allocs
2914 free(src->array);
2915 src->array = NULL;
2916 } else {
2917 uint16_t *oldarray = src->array;
2918 src->array =
2919 (uint16_t *)realloc(oldarray, src->capacity * sizeof(uint16_t));
2920 if (src->array == NULL) free(oldarray); // should never happen?
2921 }
2922 return savings;
2923 }
2924
2925 /* Free memory. */
2926 void array_container_free(array_container_t *arr) {
2927 if(arr->array != NULL) {// Jon Strabala reports that some tools complain otherwise
2928 free(arr->array);
2929 arr->array = NULL; // pedantic
2930 }
2931 free(arr);
2932 }
2933
2934 static inline int32_t grow_capacity(int32_t capacity) {
2935 return (capacity <= 0) ? ARRAY_DEFAULT_INIT_SIZE
2936 : capacity < 64 ? capacity * 2
2937 : capacity < 1024 ? capacity * 3 / 2
2938 : capacity * 5 / 4;
2939 }
2940
2941 static inline int32_t clamp(int32_t val, int32_t min, int32_t max) {
2942 return ((val < min) ? min : (val > max) ? max : val);
2943 }
2944
2945 /**
2946 * increase capacity to at least min, and to no more than max. Whether the
2947 * existing data needs to be copied over depends on the "preserve" parameter. If
2948 * preserve is false,
2949 * then the new content will be uninitialized, otherwise the old content is
2950 * copie.
2951 */
2952 void array_container_grow(array_container_t *container, int32_t min,
2953 int32_t max, bool preserve) {
2954 int32_t new_capacity = clamp(grow_capacity(container->capacity), min, max);
2955 container->capacity = new_capacity;
2956 uint16_t *array = container->array;
2957
2958 if (preserve) {
2959 container->array =
2960 (uint16_t *)realloc(array, new_capacity * sizeof(uint16_t));
2961 if (container->array == NULL) free(array);
2962 } else {
2963 // Jon Strabala reports that some tools complain otherwise
2964 if (array != NULL) {
2965 free(array);
2966 }
2967 container->array = (uint16_t *)malloc(new_capacity * sizeof(uint16_t));
2968 }
2969
2970 // handle the case where realloc fails
2971 if (container->array == NULL) {
2972 fprintf(stderr, "could not allocate memory\n");
2973 }
2974 assert(container->array != NULL);
2975 }
2976
2977 /* Copy one container into another. We assume that they are distinct. */
2978 void array_container_copy(const array_container_t *src,
2979 array_container_t *dst) {
2980 const int32_t cardinality = src->cardinality;
2981 if (cardinality > dst->capacity) {
2982 array_container_grow(dst, cardinality, INT32_MAX, false);
2983 }
2984
2985 dst->cardinality = cardinality;
2986 memcpy(dst->array, src->array, cardinality * sizeof(uint16_t));
2987 }
2988
2989 void array_container_add_from_range(array_container_t *arr, uint32_t min,
2990 uint32_t max, uint16_t step) {
2991 for (uint32_t value = min; value < max; value += step) {
2992 array_container_append(arr, value);
2993 }
2994 }
2995
2996 /* Computes the union of array1 and array2 and write the result to arrayout.
2997 * It is assumed that arrayout is distinct from both array1 and array2.
2998 */
2999 void array_container_union(const array_container_t *array_1,
3000 const array_container_t *array_2,
3001 array_container_t *out) {
3002 const int32_t card_1 = array_1->cardinality, card_2 = array_2->cardinality;
3003 const int32_t max_cardinality = card_1 + card_2;
3004
3005 if (out->capacity < max_cardinality) {
3006 array_container_grow(out, max_cardinality, 2 * DEFAULT_MAX_SIZE, false);
3007 }
3008 out->cardinality = (int32_t)fast_union_uint16(array_1->array, card_1,
3009 array_2->array, card_2, out->array);
3010
3011 }
3012
3013 /* Computes the difference of array1 and array2 and write the result
3014 * to array out.
3015 * Array out does not need to be distinct from array_1
3016 */
3017 void array_container_andnot(const array_container_t *array_1,
3018 const array_container_t *array_2,
3019 array_container_t *out) {
3020 if (out->capacity < array_1->cardinality)
3021 array_container_grow(out, array_1->cardinality, DEFAULT_MAX_SIZE, false);
3022 #ifdef ROARING_VECTOR_OPERATIONS_ENABLED
3023 out->cardinality =
3024 difference_vector16(array_1->array, array_1->cardinality,
3025 array_2->array, array_2->cardinality, out->array);
3026 #else
3027 out->cardinality =
3028 difference_uint16(array_1->array, array_1->cardinality, array_2->array,
3029 array_2->cardinality, out->array);
3030 #endif
3031 }
3032
3033 /* Computes the symmetric difference of array1 and array2 and write the
3034 * result
3035 * to arrayout.
3036 * It is assumed that arrayout is distinct from both array1 and array2.
3037 */
3038 void array_container_xor(const array_container_t *array_1,
3039 const array_container_t *array_2,
3040 array_container_t *out) {
3041 const int32_t card_1 = array_1->cardinality, card_2 = array_2->cardinality;
3042 const int32_t max_cardinality = card_1 + card_2;
3043 if (out->capacity < max_cardinality) {
3044 array_container_grow(out, max_cardinality, 2 * DEFAULT_MAX_SIZE, false);
3045 }
3046
3047 #ifdef ROARING_VECTOR_OPERATIONS_ENABLED
3048 out->cardinality =
3049 xor_vector16(array_1->array, array_1->cardinality, array_2->array,
3050 array_2->cardinality, out->array);
3051 #else
3052 out->cardinality =
3053 xor_uint16(array_1->array, array_1->cardinality, array_2->array,
3054 array_2->cardinality, out->array);
3055 #endif
3056 }
3057
3058 static inline int32_t minimum_int32(int32_t a, int32_t b) {
3059 return (a < b) ? a : b;
3060 }
3061
3062 /* computes the intersection of array1 and array2 and write the result to
3063 * arrayout.
3064 * It is assumed that arrayout is distinct from both array1 and array2.
3065 * */
3066 void array_container_intersection(const array_container_t *array1,
3067 const array_container_t *array2,
3068 array_container_t *out) {
3069 int32_t card_1 = array1->cardinality, card_2 = array2->cardinality,
3070 min_card = minimum_int32(card_1, card_2);
3071 const int threshold = 64; // subject to tuning
3072 #ifdef USEAVX
3073 if (out->capacity < min_card) {
3074 array_container_grow(out, min_card + sizeof(__m128i) / sizeof(uint16_t),
3075 DEFAULT_MAX_SIZE + sizeof(__m128i) / sizeof(uint16_t), false);
3076 }
3077 #else
3078 if (out->capacity < min_card) {
3079 array_container_grow(out, min_card, DEFAULT_MAX_SIZE, false);
3080 }
3081 #endif
3082
3083 if (card_1 * threshold < card_2) {
3084 out->cardinality = intersect_skewed_uint16(
3085 array1->array, card_1, array2->array, card_2, out->array);
3086 } else if (card_2 * threshold < card_1) {
3087 out->cardinality = intersect_skewed_uint16(
3088 array2->array, card_2, array1->array, card_1, out->array);
3089 } else {
3090 #ifdef USEAVX
3091 out->cardinality = intersect_vector16(
3092 array1->array, card_1, array2->array, card_2, out->array);
3093 #else
3094 out->cardinality = intersect_uint16(array1->array, card_1,
3095 array2->array, card_2, out->array);
3096 #endif
3097 }
3098 }
3099
3100 /* computes the size of the intersection of array1 and array2
3101 * */
3102 int array_container_intersection_cardinality(const array_container_t *array1,
3103 const array_container_t *array2) {
3104 int32_t card_1 = array1->cardinality, card_2 = array2->cardinality;
3105 const int threshold = 64; // subject to tuning
3106 if (card_1 * threshold < card_2) {
3107 return intersect_skewed_uint16_cardinality(array1->array, card_1,
3108 array2->array, card_2);
3109 } else if (card_2 * threshold < card_1) {
3110 return intersect_skewed_uint16_cardinality(array2->array, card_2,
3111 array1->array, card_1);
3112 } else {
3113 #ifdef USEAVX
3114 return intersect_vector16_cardinality(array1->array, card_1,
3115 array2->array, card_2);
3116 #else
3117 return intersect_uint16_cardinality(array1->array, card_1,
3118 array2->array, card_2);
3119 #endif
3120 }
3121 }
3122
3123 bool array_container_intersect(const array_container_t *array1,
3124 const array_container_t *array2) {
3125 int32_t card_1 = array1->cardinality, card_2 = array2->cardinality;
3126 const int threshold = 64; // subject to tuning
3127 if (card_1 * threshold < card_2) {
3128 return intersect_skewed_uint16_nonempty(
3129 array1->array, card_1, array2->array, card_2);
3130 } else if (card_2 * threshold < card_1) {
3131 return intersect_skewed_uint16_nonempty(
3132 array2->array, card_2, array1->array, card_1);
3133 } else {
3134 // we do not bother vectorizing
3135 return intersect_uint16_nonempty(array1->array, card_1,
3136 array2->array, card_2);
3137 }
3138 }
3139
3140 /* computes the intersection of array1 and array2 and write the result to
3141 * array1.
3142 * */
3143 void array_container_intersection_inplace(array_container_t *src_1,
3144 const array_container_t *src_2) {
3145 // todo: can any of this be vectorized?
3146 int32_t card_1 = src_1->cardinality, card_2 = src_2->cardinality;
3147 const int threshold = 64; // subject to tuning
3148 if (card_1 * threshold < card_2) {
3149 src_1->cardinality = intersect_skewed_uint16(
3150 src_1->array, card_1, src_2->array, card_2, src_1->array);
3151 } else if (card_2 * threshold < card_1) {
3152 src_1->cardinality = intersect_skewed_uint16(
3153 src_2->array, card_2, src_1->array, card_1, src_1->array);
3154 } else {
3155 src_1->cardinality = intersect_uint16(
3156 src_1->array, card_1, src_2->array, card_2, src_1->array);
3157 }
3158 }
3159
3160 int array_container_to_uint32_array(void *vout, const array_container_t *cont,
3161 uint32_t base) {
3162 int outpos = 0;
3163 uint32_t *out = (uint32_t *)vout;
3164 for (int i = 0; i < cont->cardinality; ++i) {
3165 const uint32_t val = base + cont->array[i];
3166 memcpy(out + outpos, &val,
3167 sizeof(uint32_t)); // should be compiled as a MOV on x64
3168 outpos++;
3169 }
3170 return outpos;
3171 }
3172
3173 void array_container_printf(const array_container_t *v) {
3174 if (v->cardinality == 0) {
3175 printf("{}");
3176 return;
3177 }
3178 printf("{");
3179 printf("%d", v->array[0]);
3180 for (int i = 1; i < v->cardinality; ++i) {
3181 printf(",%d", v->array[i]);
3182 }
3183 printf("}");
3184 }
3185
3186 void array_container_printf_as_uint32_array(const array_container_t *v,
3187 uint32_t base) {
3188 if (v->cardinality == 0) {
3189 return;
3190 }
3191 printf("%u", v->array[0] + base);
3192 for (int i = 1; i < v->cardinality; ++i) {
3193 printf(",%u", v->array[i] + base);
3194 }
3195 }
3196
3197 /* Compute the number of runs */
3198 int32_t array_container_number_of_runs(const array_container_t *a) {
3199 // Can SIMD work here?
3200 int32_t nr_runs = 0;
3201 int32_t prev = -2;
3202 for (const uint16_t *p = a->array; p != a->array + a->cardinality; ++p) {
3203 if (*p != prev + 1) nr_runs++;
3204 prev = *p;
3205 }
3206 return nr_runs;
3207 }
3208
3209 int32_t array_container_serialize(const array_container_t *container, char *buf) {
3210 int32_t l, off;
3211 uint16_t cardinality = (uint16_t)container->cardinality;
3212
3213 memcpy(buf, &cardinality, off = sizeof(cardinality));
3214 l = sizeof(uint16_t) * container->cardinality;
3215 if (l) memcpy(&buf[off], container->array, l);
3216
3217 return (off + l);
3218 }
3219
3220 /**
3221 * Writes the underlying array to buf, outputs how many bytes were written.
3222 * The number of bytes written should be
3223 * array_container_size_in_bytes(container).
3224 *
3225 */
3226 int32_t array_container_write(const array_container_t *container, char *buf) {
3227 memcpy(buf, container->array, container->cardinality * sizeof(uint16_t));
3228 return array_container_size_in_bytes(container);
3229 }
3230
3231 bool array_container_equals(const array_container_t *container1,
3232 const array_container_t *container2) {
3233 if (container1->cardinality != container2->cardinality) {
3234 return false;
3235 }
3236 // could be vectorized:
3237 for (int32_t i = 0; i < container1->cardinality; ++i) {
3238 if (container1->array[i] != container2->array[i]) return false;
3239 }
3240 return true;
3241 }
3242
3243 bool array_container_is_subset(const array_container_t *container1,
3244 const array_container_t *container2) {
3245 if (container1->cardinality > container2->cardinality) {
3246 return false;
3247 }
3248 int i1 = 0, i2 = 0;
3249 while (i1 < container1->cardinality && i2 < container2->cardinality) {
3250 if (container1->array[i1] == container2->array[i2]) {
3251 i1++;
3252 i2++;
3253 } else if (container1->array[i1] > container2->array[i2]) {
3254 i2++;
3255 } else { // container1->array[i1] < container2->array[i2]
3256 return false;
3257 }
3258 }
3259 if (i1 == container1->cardinality) {
3260 return true;
3261 } else {
3262 return false;
3263 }
3264 }
3265
3266 int32_t array_container_read(int32_t cardinality, array_container_t *container,
3267 const char *buf) {
3268 if (container->capacity < cardinality) {
3269 array_container_grow(container, cardinality, DEFAULT_MAX_SIZE, false);
3270 }
3271 container->cardinality = cardinality;
3272 memcpy(container->array, buf, container->cardinality * sizeof(uint16_t));
3273
3274 return array_container_size_in_bytes(container);
3275 }
3276
3277 uint32_t array_container_serialization_len(const array_container_t *container) {
3278 return (sizeof(uint16_t) /* container->cardinality converted to 16 bit */ +
3279 (sizeof(uint16_t) * container->cardinality));
3280 }
3281
3282 void *array_container_deserialize(const char *buf, size_t buf_len) {
3283 array_container_t *ptr;
3284
3285 if (buf_len < 2) /* capacity converted to 16 bit */
3286 return (NULL);
3287 else
3288 buf_len -= 2;
3289
3290 if ((ptr = (array_container_t *)malloc(sizeof(array_container_t))) !=
3291 NULL) {
3292 size_t len;
3293 int32_t off;
3294 uint16_t cardinality;
3295
3296 memcpy(&cardinality, buf, off = sizeof(cardinality));
3297
3298 ptr->capacity = ptr->cardinality = (uint32_t)cardinality;
3299 len = sizeof(uint16_t) * ptr->cardinality;
3300
3301 if (len != buf_len) {
3302 free(ptr);
3303 return (NULL);
3304 }
3305
3306 if ((ptr->array = (uint16_t *)malloc(sizeof(uint16_t) *
3307 ptr->capacity)) == NULL) {
3308 free(ptr);
3309 return (NULL);
3310 }
3311
3312 if (len) memcpy(ptr->array, &buf[off], len);
3313
3314 /* Check if returned values are monotonically increasing */
3315 for (int32_t i = 0, j = 0; i < ptr->cardinality; i++) {
3316 if (ptr->array[i] < j) {
3317 free(ptr->array);
3318 free(ptr);
3319 return (NULL);
3320 } else
3321 j = ptr->array[i];
3322 }
3323 }
3324
3325 return (ptr);
3326 }
3327
3328 bool array_container_iterate(const array_container_t *cont, uint32_t base,
3329 roaring_iterator iterator, void *ptr) {
3330 for (int i = 0; i < cont->cardinality; i++)
3331 if (!iterator(cont->array[i] + base, ptr)) return false;
3332 return true;
3333 }
3334
3335 bool array_container_iterate64(const array_container_t *cont, uint32_t base,
3336 roaring_iterator64 iterator, uint64_t high_bits,
3337 void *ptr) {
3338 for (int i = 0; i < cont->cardinality; i++)
3339 if (!iterator(high_bits | (uint64_t)(cont->array[i] + base), ptr))
3340 return false;
3341 return true;
3342 }
3343 /* end file src/containers/array.c */
3344 /* begin file src/containers/bitset.c */
3345 /*
3346 * bitset.c
3347 *
3348 */
3349 #ifndef _POSIX_C_SOURCE
3350 #define _POSIX_C_SOURCE 200809L
3351 #endif
3352 #include <assert.h>
3353 #include <stdio.h>
3354 #include <stdlib.h>
3355 #include <string.h>
3356
3357
3358 extern int bitset_container_cardinality(const bitset_container_t *bitset);
3359 extern bool bitset_container_nonzero_cardinality(bitset_container_t *bitset);
3360 extern void bitset_container_set(bitset_container_t *bitset, uint16_t pos);
3361 extern void bitset_container_unset(bitset_container_t *bitset, uint16_t pos);
3362 extern inline bool bitset_container_get(const bitset_container_t *bitset,
3363 uint16_t pos);
3364 extern int32_t bitset_container_serialized_size_in_bytes();
3365 extern bool bitset_container_add(bitset_container_t *bitset, uint16_t pos);
3366 extern bool bitset_container_remove(bitset_container_t *bitset, uint16_t pos);
3367 extern inline bool bitset_container_contains(const bitset_container_t *bitset,
3368 uint16_t pos);
3369
3370 void bitset_container_clear(bitset_container_t *bitset) {
3371 memset(bitset->array, 0, sizeof(uint64_t) * BITSET_CONTAINER_SIZE_IN_WORDS);
3372 bitset->cardinality = 0;
3373 }
3374
3375 void bitset_container_set_all(bitset_container_t *bitset) {
3376 memset(bitset->array, INT64_C(-1),
3377 sizeof(uint64_t) * BITSET_CONTAINER_SIZE_IN_WORDS);
3378 bitset->cardinality = (1 << 16);
3379 }
3380
3381
3382
3383 /* Create a new bitset. Return NULL in case of failure. */
3384 bitset_container_t *bitset_container_create(void) {
3385 bitset_container_t *bitset =
3386 (bitset_container_t *)malloc(sizeof(bitset_container_t));
3387
3388 if (!bitset) {
3389 return NULL;
3390 }
3391 // sizeof(__m256i) == 32
3392 bitset->array = (uint64_t *)aligned_malloc(
3393 32, sizeof(uint64_t) * BITSET_CONTAINER_SIZE_IN_WORDS);
3394 if (!bitset->array) {
3395 free(bitset);
3396 return NULL;
3397 }
3398 bitset_container_clear(bitset);
3399 return bitset;
3400 }
3401
3402 /* Copy one container into another. We assume that they are distinct. */
3403 void bitset_container_copy(const bitset_container_t *source,
3404 bitset_container_t *dest) {
3405 dest->cardinality = source->cardinality;
3406 memcpy(dest->array, source->array,
3407 sizeof(uint64_t) * BITSET_CONTAINER_SIZE_IN_WORDS);
3408 }
3409
3410 void bitset_container_add_from_range(bitset_container_t *bitset, uint32_t min,
3411 uint32_t max, uint16_t step) {
3412 if (step == 0) return; // refuse to crash
3413 if ((64 % step) == 0) { // step divides 64
3414 uint64_t mask = 0; // construct the repeated mask
3415 for (uint32_t value = (min % step); value < 64; value += step) {
3416 mask |= ((uint64_t)1 << value);
3417 }
3418 uint32_t firstword = min / 64;
3419 uint32_t endword = (max - 1) / 64;
3420 bitset->cardinality = (max - min + step - 1) / step;
3421 if (firstword == endword) {
3422 bitset->array[firstword] |=
3423 mask & (((~UINT64_C(0)) << (min % 64)) &
3424 ((~UINT64_C(0)) >> ((~max + 1) % 64)));
3425 return;
3426 }
3427 bitset->array[firstword] = mask & ((~UINT64_C(0)) << (min % 64));
3428 for (uint32_t i = firstword + 1; i < endword; i++)
3429 bitset->array[i] = mask;
3430 bitset->array[endword] = mask & ((~UINT64_C(0)) >> ((~max + 1) % 64));
3431 } else {
3432 for (uint32_t value = min; value < max; value += step) {
3433 bitset_container_add(bitset, value);
3434 }
3435 }
3436 }
3437
3438 /* Free memory. */
3439 void bitset_container_free(bitset_container_t *bitset) {
3440 if(bitset->array != NULL) {// Jon Strabala reports that some tools complain otherwise
3441 aligned_free(bitset->array);
3442 bitset->array = NULL; // pedantic
3443 }
3444 free(bitset);
3445 }
3446
3447 /* duplicate container. */
3448 bitset_container_t *bitset_container_clone(const bitset_container_t *src) {
3449 bitset_container_t *bitset =
3450 (bitset_container_t *)malloc(sizeof(bitset_container_t));
3451
3452 if (!bitset) {
3453 return NULL;
3454 }
3455 // sizeof(__m256i) == 32
3456 bitset->array = (uint64_t *)aligned_malloc(
3457 32, sizeof(uint64_t) * BITSET_CONTAINER_SIZE_IN_WORDS);
3458 if (!bitset->array) {
3459 free(bitset);
3460 return NULL;
3461 }
3462 bitset->cardinality = src->cardinality;
3463 memcpy(bitset->array, src->array,
3464 sizeof(uint64_t) * BITSET_CONTAINER_SIZE_IN_WORDS);
3465 return bitset;
3466 }
3467
3468 void bitset_container_set_range(bitset_container_t *bitset, uint32_t begin,
3469 uint32_t end) {
3470 bitset_set_range(bitset->array, begin, end);
3471 bitset->cardinality =
3472 bitset_container_compute_cardinality(bitset); // could be smarter
3473 }
3474
3475
3476 bool bitset_container_intersect(const bitset_container_t *src_1,
3477 const bitset_container_t *src_2) {
3478 // could vectorize, but this is probably already quite fast in practice
3479 const uint64_t * __restrict__ array_1 = src_1->array;
3480 const uint64_t * __restrict__ array_2 = src_2->array;
3481 for (int i = 0; i < BITSET_CONTAINER_SIZE_IN_WORDS; i ++) {
3482 if((array_1[i] & array_2[i]) != 0) return true;
3483 }
3484 return false;
3485 }
3486
3487
3488 #ifdef USEAVX
3489 #ifndef WORDS_IN_AVX2_REG
3490 #define WORDS_IN_AVX2_REG sizeof(__m256i) / sizeof(uint64_t)
3491 #endif
3492 /* Get the number of bits set (force computation) */
3493 int bitset_container_compute_cardinality(const bitset_container_t *bitset) {
3494 return (int) avx2_harley_seal_popcount256(
3495 (const __m256i *)bitset->array,
3496 BITSET_CONTAINER_SIZE_IN_WORDS / (WORDS_IN_AVX2_REG));
3497 }
3498 #else
3499
3500 /* Get the number of bits set (force computation) */
3501 int bitset_container_compute_cardinality(const bitset_container_t *bitset) {
3502 const uint64_t *array = bitset->array;
3503 int32_t sum = 0;
3504 for (int i = 0; i < BITSET_CONTAINER_SIZE_IN_WORDS; i += 4) {
3505 sum += hamming(array[i]);
3506 sum += hamming(array[i + 1]);
3507 sum += hamming(array[i + 2]);
3508 sum += hamming(array[i + 3]);
3509 }
3510 return sum;
3511 }
3512
3513 #endif
3514
3515 #ifdef USEAVX
3516
3517 #define BITSET_CONTAINER_FN_REPEAT 8
3518 #ifndef WORDS_IN_AVX2_REG
3519 #define WORDS_IN_AVX2_REG sizeof(__m256i) / sizeof(uint64_t)
3520 #endif
3521 #define LOOP_SIZE \
3522 BITSET_CONTAINER_SIZE_IN_WORDS / \
3523 ((WORDS_IN_AVX2_REG)*BITSET_CONTAINER_FN_REPEAT)
3524
3525 /* Computes a binary operation (eg union) on bitset1 and bitset2 and write the
3526 result to bitsetout */
3527 // clang-format off
3528 #define BITSET_CONTAINER_FN(opname, opsymbol, avx_intrinsic) \
3529 int bitset_container_##opname##_nocard(const bitset_container_t *src_1, \
3530 const bitset_container_t *src_2, \
3531 bitset_container_t *dst) { \
3532 const uint8_t * __restrict__ array_1 = (const uint8_t *)src_1->array; \
3533 const uint8_t * __restrict__ array_2 = (const uint8_t *)src_2->array; \
3534 /* not using the blocking optimization for some reason*/ \
3535 uint8_t *out = (uint8_t*)dst->array; \
3536 const int innerloop = 8; \
3537 for (size_t i = 0; \
3538 i < BITSET_CONTAINER_SIZE_IN_WORDS / (WORDS_IN_AVX2_REG); \
3539 i+=innerloop) {\
3540 __m256i A1, A2, AO; \
3541 A1 = _mm256_lddqu_si256((const __m256i *)(array_1)); \
3542 A2 = _mm256_lddqu_si256((const __m256i *)(array_2)); \
3543 AO = avx_intrinsic(A2, A1); \
3544 _mm256_storeu_si256((__m256i *)out, AO); \
3545 A1 = _mm256_lddqu_si256((const __m256i *)(array_1 + 32)); \
3546 A2 = _mm256_lddqu_si256((const __m256i *)(array_2 + 32)); \
3547 AO = avx_intrinsic(A2, A1); \
3548 _mm256_storeu_si256((__m256i *)(out+32), AO); \
3549 A1 = _mm256_lddqu_si256((const __m256i *)(array_1 + 64)); \
3550 A2 = _mm256_lddqu_si256((const __m256i *)(array_2 + 64)); \
3551 AO = avx_intrinsic(A2, A1); \
3552 _mm256_storeu_si256((__m256i *)(out+64), AO); \
3553 A1 = _mm256_lddqu_si256((const __m256i *)(array_1 + 96)); \
3554 A2 = _mm256_lddqu_si256((const __m256i *)(array_2 + 96)); \
3555 AO = avx_intrinsic(A2, A1); \
3556 _mm256_storeu_si256((__m256i *)(out+96), AO); \
3557 A1 = _mm256_lddqu_si256((const __m256i *)(array_1 + 128)); \
3558 A2 = _mm256_lddqu_si256((const __m256i *)(array_2 + 128)); \
3559 AO = avx_intrinsic(A2, A1); \
3560 _mm256_storeu_si256((__m256i *)(out+128), AO); \
3561 A1 = _mm256_lddqu_si256((const __m256i *)(array_1 + 160)); \
3562 A2 = _mm256_lddqu_si256((const __m256i *)(array_2 + 160)); \
3563 AO = avx_intrinsic(A2, A1); \
3564 _mm256_storeu_si256((__m256i *)(out+160), AO); \
3565 A1 = _mm256_lddqu_si256((const __m256i *)(array_1 + 192)); \
3566 A2 = _mm256_lddqu_si256((const __m256i *)(array_2 + 192)); \
3567 AO = avx_intrinsic(A2, A1); \
3568 _mm256_storeu_si256((__m256i *)(out+192), AO); \
3569 A1 = _mm256_lddqu_si256((const __m256i *)(array_1 + 224)); \
3570 A2 = _mm256_lddqu_si256((const __m256i *)(array_2 + 224)); \
3571 AO = avx_intrinsic(A2, A1); \
3572 _mm256_storeu_si256((__m256i *)(out+224), AO); \
3573 out+=256; \
3574 array_1 += 256; \
3575 array_2 += 256; \
3576 } \
3577 dst->cardinality = BITSET_UNKNOWN_CARDINALITY; \
3578 return dst->cardinality; \
3579 } \
3580 /* next, a version that updates cardinality*/ \
3581 int bitset_container_##opname(const bitset_container_t *src_1, \
3582 const bitset_container_t *src_2, \
3583 bitset_container_t *dst) { \
3584 const __m256i * __restrict__ array_1 = (const __m256i *) src_1->array; \
3585 const __m256i * __restrict__ array_2 = (const __m256i *) src_2->array; \
3586 __m256i *out = (__m256i *) dst->array; \
3587 dst->cardinality = (int32_t)avx2_harley_seal_popcount256andstore_##opname(array_2,\
3588 array_1, out,BITSET_CONTAINER_SIZE_IN_WORDS / (WORDS_IN_AVX2_REG));\
3589 return dst->cardinality; \
3590 } \
3591 /* next, a version that just computes the cardinality*/ \
3592 int bitset_container_##opname##_justcard(const bitset_container_t *src_1, \
3593 const bitset_container_t *src_2) { \
3594 const __m256i * __restrict__ data1 = (const __m256i *) src_1->array; \
3595 const __m256i * __restrict__ data2 = (const __m256i *) src_2->array; \
3596 return (int)avx2_harley_seal_popcount256_##opname(data2, \
3597 data1, BITSET_CONTAINER_SIZE_IN_WORDS / (WORDS_IN_AVX2_REG));\
3598 }
3599
3600
3601
3602 #else /* not USEAVX */
3603
3604 #define BITSET_CONTAINER_FN(opname, opsymbol, avxintrinsic) \
3605 int bitset_container_##opname(const bitset_container_t *src_1, \
3606 const bitset_container_t *src_2, \
3607 bitset_container_t *dst) { \
3608 const uint64_t * __restrict__ array_1 = src_1->array; \
3609 const uint64_t * __restrict__ array_2 = src_2->array; \
3610 uint64_t *out = dst->array; \
3611 int32_t sum = 0; \
3612 for (size_t i = 0; i < BITSET_CONTAINER_SIZE_IN_WORDS; i += 2) { \
3613 const uint64_t word_1 = (array_1[i])opsymbol(array_2[i]), \
3614 word_2 = (array_1[i + 1])opsymbol(array_2[i + 1]); \
3615 out[i] = word_1; \
3616 out[i + 1] = word_2; \
3617 sum += hamming(word_1); \
3618 sum += hamming(word_2); \
3619 } \
3620 dst->cardinality = sum; \
3621 return dst->cardinality; \
3622 } \
3623 int bitset_container_##opname##_nocard(const bitset_container_t *src_1, \
3624 const bitset_container_t *src_2, \
3625 bitset_container_t *dst) { \
3626 const uint64_t * __restrict__ array_1 = src_1->array; \
3627 const uint64_t * __restrict__ array_2 = src_2->array; \
3628 uint64_t *out = dst->array; \
3629 for (size_t i = 0; i < BITSET_CONTAINER_SIZE_IN_WORDS; i++) { \
3630 out[i] = (array_1[i])opsymbol(array_2[i]); \
3631 } \
3632 dst->cardinality = BITSET_UNKNOWN_CARDINALITY; \
3633 return dst->cardinality; \
3634 } \
3635 int bitset_container_##opname##_justcard(const bitset_container_t *src_1, \
3636 const bitset_container_t *src_2) { \
3637 const uint64_t * __restrict__ array_1 = src_1->array; \
3638 const uint64_t * __restrict__ array_2 = src_2->array; \
3639 int32_t sum = 0; \
3640 for (size_t i = 0; i < BITSET_CONTAINER_SIZE_IN_WORDS; i += 2) { \
3641 const uint64_t word_1 = (array_1[i])opsymbol(array_2[i]), \
3642 word_2 = (array_1[i + 1])opsymbol(array_2[i + 1]); \
3643 sum += hamming(word_1); \
3644 sum += hamming(word_2); \
3645 } \
3646 return sum; \
3647 }
3648
3649 #endif
3650
3651 // we duplicate the function because other containers use the "or" term, makes API more consistent
3652 BITSET_CONTAINER_FN(or, |, _mm256_or_si256)
3653 BITSET_CONTAINER_FN(union, |, _mm256_or_si256)
3654
3655 // we duplicate the function because other containers use the "intersection" term, makes API more consistent
3656 BITSET_CONTAINER_FN(and, &, _mm256_and_si256)
3657 BITSET_CONTAINER_FN(intersection, &, _mm256_and_si256)
3658
3659 BITSET_CONTAINER_FN(xor, ^, _mm256_xor_si256)
3660 BITSET_CONTAINER_FN(andnot, &~, _mm256_andnot_si256)
3661 // clang-format On
3662
3663
3664
3665 int bitset_container_to_uint32_array( void *vout, const bitset_container_t *cont, uint32_t base) {
3666 #ifdef USEAVX2FORDECODING
3667 if(cont->cardinality >= 8192)// heuristic
3668 return (int) bitset_extract_setbits_avx2(cont->array, BITSET_CONTAINER_SIZE_IN_WORDS, vout,cont->cardinality,base);
3669 else
3670 return (int) bitset_extract_setbits(cont->array, BITSET_CONTAINER_SIZE_IN_WORDS, vout,base);
3671 #else
3672 return (int) bitset_extract_setbits(cont->array, BITSET_CONTAINER_SIZE_IN_WORDS, vout,base);
3673 #endif
3674 }
3675
3676 /*
3677 * Print this container using printf (useful for debugging).
3678 */
3679 void bitset_container_printf(const bitset_container_t * v) {
3680 printf("{");
3681 uint32_t base = 0;
3682 bool iamfirst = true;// TODO: rework so that this is not necessary yet still readable
3683 for (int i = 0; i < BITSET_CONTAINER_SIZE_IN_WORDS; ++i) {
3684 uint64_t w = v->array[i];
3685 while (w != 0) {
3686 uint64_t t = w & (~w + 1);
3687 int r = __builtin_ctzll(w);
3688 if(iamfirst) {// predicted to be false
3689 printf("%u",base + r);
3690 iamfirst = false;
3691 } else {
3692 printf(",%u",base + r);
3693 }
3694 w ^= t;
3695 }
3696 base += 64;
3697 }
3698 printf("}");
3699 }
3700
3701
3702 /*
3703 * Print this container using printf as a comma-separated list of 32-bit integers starting at base.
3704 */
3705 void bitset_container_printf_as_uint32_array(const bitset_container_t * v, uint32_t base) {
3706 bool iamfirst = true;// TODO: rework so that this is not necessary yet still readable
3707 for (int i = 0; i < BITSET_CONTAINER_SIZE_IN_WORDS; ++i) {
3708 uint64_t w = v->array[i];
3709 while (w != 0) {
3710 uint64_t t = w & (~w + 1);
3711 int r = __builtin_ctzll(w);
3712 if(iamfirst) {// predicted to be false
3713 printf("%u", r + base);
3714 iamfirst = false;
3715 } else {
3716 printf(",%u",r + base);
3717 }
3718 w ^= t;
3719 }
3720 base += 64;
3721 }
3722 }
3723
3724
3725 // TODO: use the fast lower bound, also
3726 int bitset_container_number_of_runs(bitset_container_t *b) {
3727 int num_runs = 0;
3728 uint64_t next_word = b->array[0];
3729
3730 for (int i = 0; i < BITSET_CONTAINER_SIZE_IN_WORDS-1; ++i) {
3731 uint64_t word = next_word;
3732 next_word = b->array[i+1];
3733 num_runs += hamming((~word) & (word << 1)) + ( (word >> 63) & ~next_word);
3734 }
3735
3736 uint64_t word = next_word;
3737 num_runs += hamming((~word) & (word << 1));
3738 if((word & 0x8000000000000000ULL) != 0)
3739 num_runs++;
3740 return num_runs;
3741 }
3742
3743 int32_t bitset_container_serialize(const bitset_container_t *container, char *buf) {
3744 int32_t l = sizeof(uint64_t) * BITSET_CONTAINER_SIZE_IN_WORDS;
3745 memcpy(buf, container->array, l);
3746 return(l);
3747 }
3748
3749
3750
3751 int32_t bitset_container_write(const bitset_container_t *container,
3752 char *buf) {
3753 memcpy(buf, container->array, BITSET_CONTAINER_SIZE_IN_WORDS * sizeof(uint64_t));
3754 return bitset_container_size_in_bytes(container);
3755 }
3756
3757
3758 int32_t bitset_container_read(int32_t cardinality, bitset_container_t *container,
3759 const char *buf) {
3760 container->cardinality = cardinality;
3761 memcpy(container->array, buf, BITSET_CONTAINER_SIZE_IN_WORDS * sizeof(uint64_t));
3762 return bitset_container_size_in_bytes(container);
3763 }
3764
3765 uint32_t bitset_container_serialization_len() {
3766 return(sizeof(uint64_t) * BITSET_CONTAINER_SIZE_IN_WORDS);
3767 }
3768
3769 void* bitset_container_deserialize(const char *buf, size_t buf_len) {
3770 bitset_container_t *ptr;
3771 size_t l = sizeof(uint64_t) * BITSET_CONTAINER_SIZE_IN_WORDS;
3772
3773 if(l != buf_len)
3774 return(NULL);
3775
3776 if((ptr = (bitset_container_t *)malloc(sizeof(bitset_container_t))) != NULL) {
3777 memcpy(ptr, buf, sizeof(bitset_container_t));
3778 // sizeof(__m256i) == 32
3779 ptr->array = (uint64_t *) aligned_malloc(32, l);
3780 if (! ptr->array) {
3781 free(ptr);
3782 return NULL;
3783 }
3784 memcpy(ptr->array, buf, l);
3785 ptr->cardinality = bitset_container_compute_cardinality(ptr);
3786 }
3787
3788 return((void*)ptr);
3789 }
3790
3791 bool bitset_container_iterate(const bitset_container_t *cont, uint32_t base, roaring_iterator iterator, void *ptr) {
3792 for (int32_t i = 0; i < BITSET_CONTAINER_SIZE_IN_WORDS; ++i ) {
3793 uint64_t w = cont->array[i];
3794 while (w != 0) {
3795 uint64_t t = w & (~w + 1);
3796 int r = __builtin_ctzll(w);
3797 if(!iterator(r + base, ptr)) return false;
3798 w ^= t;
3799 }
3800 base += 64;
3801 }
3802 return true;
3803 }
3804
3805 bool bitset_container_iterate64(const bitset_container_t *cont, uint32_t base, roaring_iterator64 iterator, uint64_t high_bits, void *ptr) {
3806 for (int32_t i = 0; i < BITSET_CONTAINER_SIZE_IN_WORDS; ++i ) {
3807 uint64_t w = cont->array[i];
3808 while (w != 0) {
3809 uint64_t t = w & (~w + 1);
3810 int r = __builtin_ctzll(w);
3811 if(!iterator(high_bits | (uint64_t)(r + base), ptr)) return false;
3812 w ^= t;
3813 }
3814 base += 64;
3815 }
3816 return true;
3817 }
3818
3819
3820 bool bitset_container_equals(const bitset_container_t *container1, const bitset_container_t *container2) {
3821 if((container1->cardinality != BITSET_UNKNOWN_CARDINALITY) && (container2->cardinality != BITSET_UNKNOWN_CARDINALITY)) {
3822 if(container1->cardinality != container2->cardinality) {
3823 return false;
3824 }
3825 }
3826 for(int32_t i = 0; i < BITSET_CONTAINER_SIZE_IN_WORDS; ++i ) {
3827 if(container1->array[i] != container2->array[i]) {
3828 return false;
3829 }
3830 }
3831 return true;
3832 }
3833
3834 bool bitset_container_is_subset(const bitset_container_t *container1,
3835 const bitset_container_t *container2) {
3836 if((container1->cardinality != BITSET_UNKNOWN_CARDINALITY) && (container2->cardinality != BITSET_UNKNOWN_CARDINALITY)) {
3837 if(container1->cardinality > container2->cardinality) {
3838 return false;
3839 }
3840 }
3841 for(int32_t i = 0; i < BITSET_CONTAINER_SIZE_IN_WORDS; ++i ) {
3842 if((container1->array[i] & container2->array[i]) != container1->array[i]) {
3843 return false;
3844 }
3845 }
3846 return true;
3847 }
3848
3849 bool bitset_container_select(const bitset_container_t *container, uint32_t *start_rank, uint32_t rank, uint32_t *element) {
3850 int card = bitset_container_cardinality(container);
3851 if(rank >= *start_rank + card) {
3852 *start_rank += card;
3853 return false;
3854 }
3855 const uint64_t *array = container->array;
3856 int32_t size;
3857 for (int i = 0; i < BITSET_CONTAINER_SIZE_IN_WORDS; i += 1) {
3858 size = hamming(array[i]);
3859 if(rank <= *start_rank + size) {
3860 uint64_t w = container->array[i];
3861 uint16_t base = i*64;
3862 while (w != 0) {
3863 uint64_t t = w & (~w + 1);
3864 int r = __builtin_ctzll(w);
3865 if(*start_rank == rank) {
3866 *element = r+base;
3867 return true;
3868 }
3869 w ^= t;
3870 *start_rank += 1;
3871 }
3872 }
3873 else
3874 *start_rank += size;
3875 }
3876 assert(false);
3877 __builtin_unreachable();
3878 }
3879
3880
3881 /* Returns the smallest value (assumes not empty) */
3882 uint16_t bitset_container_minimum(const bitset_container_t *container) {
3883 for (int32_t i = 0; i < BITSET_CONTAINER_SIZE_IN_WORDS; ++i ) {
3884 uint64_t w = container->array[i];
3885 if (w != 0) {
3886 int r = __builtin_ctzll(w);
3887 return r + i * 64;
3888 }
3889 }
3890 return UINT16_MAX;
3891 }
3892
3893 /* Returns the largest value (assumes not empty) */
3894 uint16_t bitset_container_maximum(const bitset_container_t *container) {
3895 for (int32_t i = BITSET_CONTAINER_SIZE_IN_WORDS - 1; i > 0; --i ) {
3896 uint64_t w = container->array[i];
3897 if (w != 0) {
3898 int r = __builtin_clzll(w);
3899 return i * 64 + 63 - r;
3900 }
3901 }
3902 return 0;
3903 }
3904
3905 /* Returns the number of values equal or smaller than x */
3906 int bitset_container_rank(const bitset_container_t *container, uint16_t x) {
3907 uint32_t x32 = x;
3908 int sum = 0;
3909 uint32_t k = 0;
3910 for (; k + 63 <= x32; k += 64) {
3911 sum += hamming(container->array[k / 64]);
3912 }
3913 // at this point, we have covered everything up to k, k not included.
3914 // we have that k < x, but not so large that k+63<=x
3915 // k is a power of 64
3916 int bitsleft = x32 - k + 1;// will be in [0,64)
3917 uint64_t leftoverword = container->array[k / 64];// k / 64 should be within scope
3918 leftoverword = leftoverword & ((UINT64_C(1) << bitsleft) - 1);
3919 sum += hamming(leftoverword);
3920 return sum;
3921 }
3922
3923 /* Returns the index of the first value equal or larger than x, or -1 */
3924 int bitset_container_index_equalorlarger(const bitset_container_t *container, uint16_t x) {
3925 uint32_t x32 = x;
3926 uint32_t k = x32 / 64;
3927 uint64_t word = container->array[k];
3928 const int diff = x32 - k * 64; // in [0,64)
3929 word = (word >> diff) << diff; // a mask is faster, but we don't care
3930 while(word == 0) {
3931 k++;
3932 if(k == BITSET_CONTAINER_SIZE_IN_WORDS) return -1;
3933 word = container->array[k];
3934 }
3935 return k * 64 + __builtin_ctzll(word);
3936 }
3937 /* end file src/containers/bitset.c */
3938 /* begin file src/containers/containers.c */
3939
3940
3941 extern inline const void *container_unwrap_shared(
3942 const void *candidate_shared_container, uint8_t *type);
3943 extern inline void *container_mutable_unwrap_shared(
3944 void *candidate_shared_container, uint8_t *type);
3945
3946 extern const char *get_container_name(uint8_t typecode);
3947
3948 extern int container_get_cardinality(const void *container, uint8_t typecode);
3949
3950 extern void *container_iand(void *c1, uint8_t type1, const void *c2,
3951 uint8_t type2, uint8_t *result_type);
3952
3953 extern void *container_ior(void *c1, uint8_t type1, const void *c2,
3954 uint8_t type2, uint8_t *result_type);
3955
3956 extern void *container_ixor(void *c1, uint8_t type1, const void *c2,
3957 uint8_t type2, uint8_t *result_type);
3958
3959 extern void *container_iandnot(void *c1, uint8_t type1, const void *c2,
3960 uint8_t type2, uint8_t *result_type);
3961
3962 void container_free(void *container, uint8_t typecode) {
3963 switch (typecode) {
3964 case BITSET_CONTAINER_TYPE_CODE:
3965 bitset_container_free((bitset_container_t *)container);
3966 break;
3967 case ARRAY_CONTAINER_TYPE_CODE:
3968 array_container_free((array_container_t *)container);
3969 break;
3970 case RUN_CONTAINER_TYPE_CODE:
3971 run_container_free((run_container_t *)container);
3972 break;
3973 case SHARED_CONTAINER_TYPE_CODE:
3974 shared_container_free((shared_container_t *)container);
3975 break;
3976 default:
3977 assert(false);
3978 __builtin_unreachable();
3979 }
3980 }
3981
3982 void container_printf(const void *container, uint8_t typecode) {
3983 container = container_unwrap_shared(container, &typecode);
3984 switch (typecode) {
3985 case BITSET_CONTAINER_TYPE_CODE:
3986 bitset_container_printf((const bitset_container_t *)container);
3987 return;
3988 case ARRAY_CONTAINER_TYPE_CODE:
3989 array_container_printf((const array_container_t *)container);
3990 return;
3991 case RUN_CONTAINER_TYPE_CODE:
3992 run_container_printf((const run_container_t *)container);
3993 return;
3994 default:
3995 __builtin_unreachable();
3996 }
3997 }
3998
3999 void container_printf_as_uint32_array(const void *container, uint8_t typecode,
4000 uint32_t base) {
4001 container = container_unwrap_shared(container, &typecode);
4002 switch (typecode) {
4003 case BITSET_CONTAINER_TYPE_CODE:
4004 bitset_container_printf_as_uint32_array(
4005 (const bitset_container_t *)container, base);
4006 return;
4007 case ARRAY_CONTAINER_TYPE_CODE:
4008 array_container_printf_as_uint32_array(
4009 (const array_container_t *)container, base);
4010 return;
4011 case RUN_CONTAINER_TYPE_CODE:
4012 run_container_printf_as_uint32_array(
4013 (const run_container_t *)container, base);
4014 return;
4015 return;
4016 default:
4017 __builtin_unreachable();
4018 }
4019 }
4020
4021 int32_t container_serialize(const void *container, uint8_t typecode,
4022 char *buf) {
4023 container = container_unwrap_shared(container, &typecode);
4024 switch (typecode) {
4025 case BITSET_CONTAINER_TYPE_CODE:
4026 return (bitset_container_serialize((const bitset_container_t *)container,
4027 buf));
4028 case ARRAY_CONTAINER_TYPE_CODE:
4029 return (
4030 array_container_serialize((const array_container_t *)container, buf));
4031 case RUN_CONTAINER_TYPE_CODE:
4032 return (run_container_serialize((const run_container_t *)container, buf));
4033 default:
4034 assert(0);
4035 __builtin_unreachable();
4036 return (-1);
4037 }
4038 }
4039
4040 uint32_t container_serialization_len(const void *container, uint8_t typecode) {
4041 container = container_unwrap_shared(container, &typecode);
4042 switch (typecode) {
4043 case BITSET_CONTAINER_TYPE_CODE:
4044 return bitset_container_serialization_len();
4045 case ARRAY_CONTAINER_TYPE_CODE:
4046 return array_container_serialization_len(
4047 (const array_container_t *)container);
4048 case RUN_CONTAINER_TYPE_CODE:
4049 return run_container_serialization_len(
4050 (const run_container_t *)container);
4051 default:
4052 assert(0);
4053 __builtin_unreachable();
4054 return (0);
4055 }
4056 }
4057
4058 void *container_deserialize(uint8_t typecode, const char *buf, size_t buf_len) {
4059 switch (typecode) {
4060 case BITSET_CONTAINER_TYPE_CODE:
4061 return (bitset_container_deserialize(buf, buf_len));
4062 case ARRAY_CONTAINER_TYPE_CODE:
4063 return (array_container_deserialize(buf, buf_len));
4064 case RUN_CONTAINER_TYPE_CODE:
4065 return (run_container_deserialize(buf, buf_len));
4066 case SHARED_CONTAINER_TYPE_CODE:
4067 printf("this should never happen.\n");
4068 assert(0);
4069 __builtin_unreachable();
4070 return (NULL);
4071 default:
4072 assert(0);
4073 __builtin_unreachable();
4074 return (NULL);
4075 }
4076 }
4077
4078 extern bool container_nonzero_cardinality(const void *container,
4079 uint8_t typecode);
4080
4081 extern void container_free(void *container, uint8_t typecode);
4082
4083 extern int container_to_uint32_array(uint32_t *output, const void *container,
4084 uint8_t typecode, uint32_t base);
4085
4086 extern void *container_add(void *container, uint16_t val, uint8_t typecode,
4087 uint8_t *new_typecode);
4088
4089 extern inline bool container_contains(const void *container, uint16_t val,
4090 uint8_t typecode);
4091
4092 extern void *container_clone(const void *container, uint8_t typecode);
4093
4094 extern void *container_and(const void *c1, uint8_t type1, const void *c2,
4095 uint8_t type2, uint8_t *result_type);
4096
4097 extern void *container_or(const void *c1, uint8_t type1, const void *c2,
4098 uint8_t type2, uint8_t *result_type);
4099
4100 extern void *container_xor(const void *c1, uint8_t type1, const void *c2,
4101 uint8_t type2, uint8_t *result_type);
4102
4103 void *get_copy_of_container(void *container, uint8_t *typecode,
4104 bool copy_on_write) {
4105 if (copy_on_write) {
4106 shared_container_t *shared_container;
4107 if (*typecode == SHARED_CONTAINER_TYPE_CODE) {
4108 shared_container = (shared_container_t *)container;
4109 shared_container->counter += 1;
4110 return shared_container;
4111 }
4112 assert(*typecode != SHARED_CONTAINER_TYPE_CODE);
4113
4114 if ((shared_container = (shared_container_t *)malloc(
4115 sizeof(shared_container_t))) == NULL) {
4116 return NULL;
4117 }
4118
4119 shared_container->container = container;
4120 shared_container->typecode = *typecode;
4121
4122 shared_container->counter = 2;
4123 *typecode = SHARED_CONTAINER_TYPE_CODE;
4124
4125 return shared_container;
4126 } // copy_on_write
4127 // otherwise, no copy on write...
4128 const void *actualcontainer =
4129 container_unwrap_shared((const void *)container, typecode);
4130 assert(*typecode != SHARED_CONTAINER_TYPE_CODE);
4131 return container_clone(actualcontainer, *typecode);
4132 }
4133 /**
4134 * Copies a container, requires a typecode. This allocates new memory, caller
4135 * is responsible for deallocation.
4136 */
4137 void *container_clone(const void *container, uint8_t typecode) {
4138 container = container_unwrap_shared(container, &typecode);
4139 switch (typecode) {
4140 case BITSET_CONTAINER_TYPE_CODE:
4141 return bitset_container_clone((const bitset_container_t *)container);
4142 case ARRAY_CONTAINER_TYPE_CODE:
4143 return array_container_clone((const array_container_t *)container);
4144 case RUN_CONTAINER_TYPE_CODE:
4145 return run_container_clone((const run_container_t *)container);
4146 case SHARED_CONTAINER_TYPE_CODE:
4147 printf("shared containers are not cloneable\n");
4148 assert(false);
4149 return NULL;
4150 default:
4151 assert(false);
4152 __builtin_unreachable();
4153 return NULL;
4154 }
4155 }
4156
4157 void *shared_container_extract_copy(shared_container_t *container,
4158 uint8_t *typecode) {
4159 assert(container->counter > 0);
4160 assert(container->typecode != SHARED_CONTAINER_TYPE_CODE);
4161 container->counter--;
4162 *typecode = container->typecode;
4163 void *answer;
4164 if (container->counter == 0) {
4165 answer = container->container;
4166 container->container = NULL; // paranoid
4167 free(container);
4168 } else {
4169 answer = container_clone(container->container, *typecode);
4170 }
4171 assert(*typecode != SHARED_CONTAINER_TYPE_CODE);
4172 return answer;
4173 }
4174
4175 void shared_container_free(shared_container_t *container) {
4176 assert(container->counter > 0);
4177 container->counter--;
4178 if (container->counter == 0) {
4179 assert(container->typecode != SHARED_CONTAINER_TYPE_CODE);
4180 container_free(container->container, container->typecode);
4181 container->container = NULL; // paranoid
4182 free(container);
4183 }
4184 }
4185
4186 extern void *container_not(const void *c1, uint8_t type1, uint8_t *result_type);
4187
4188 extern void *container_not_range(const void *c1, uint8_t type1,
4189 uint32_t range_start, uint32_t range_end,
4190 uint8_t *result_type);
4191
4192 extern void *container_inot(void *c1, uint8_t type1, uint8_t *result_type);
4193
4194 extern void *container_inot_range(void *c1, uint8_t type1, uint32_t range_start,
4195 uint32_t range_end, uint8_t *result_type);
4196
4197 extern void *container_range_of_ones(uint32_t range_start, uint32_t range_end,
4198 uint8_t *result_type);
4199
4200 // where are the correponding things for union and intersection??
4201 extern void *container_lazy_xor(const void *c1, uint8_t type1, const void *c2,
4202 uint8_t type2, uint8_t *result_type);
4203
4204 extern void *container_lazy_ixor(void *c1, uint8_t type1, const void *c2,
4205 uint8_t type2, uint8_t *result_type);
4206
4207 extern void *container_andnot(const void *c1, uint8_t type1, const void *c2,
4208 uint8_t type2, uint8_t *result_type);
4209 /* end file src/containers/containers.c */
4210 /* begin file src/containers/convert.c */
4211 #include <stdio.h>
4212
4213
4214 // file contains grubby stuff that must know impl. details of all container
4215 // types.
4216 bitset_container_t *bitset_container_from_array(const array_container_t *a) {
4217 bitset_container_t *ans = bitset_container_create();
4218 int limit = array_container_cardinality(a);
4219 for (int i = 0; i < limit; ++i) bitset_container_set(ans, a->array[i]);
4220 return ans;
4221 }
4222
4223 bitset_container_t *bitset_container_from_run(const run_container_t *arr) {
4224 int card = run_container_cardinality(arr);
4225 bitset_container_t *answer = bitset_container_create();
4226 for (int rlepos = 0; rlepos < arr->n_runs; ++rlepos) {
4227 rle16_t vl = arr->runs[rlepos];
4228 bitset_set_lenrange(answer->array, vl.value, vl.length);
4229 }
4230 answer->cardinality = card;
4231 return answer;
4232 }
4233
4234 array_container_t *array_container_from_run(const run_container_t *arr) {
4235 array_container_t *answer =
4236 array_container_create_given_capacity(run_container_cardinality(arr));
4237 answer->cardinality = 0;
4238 for (int rlepos = 0; rlepos < arr->n_runs; ++rlepos) {
4239 int run_start = arr->runs[rlepos].value;
4240 int run_end = run_start + arr->runs[rlepos].length;
4241
4242 for (int run_value = run_start; run_value <= run_end; ++run_value) {
4243 answer->array[answer->cardinality++] = (uint16_t)run_value;
4244 }
4245 }
4246 return answer;
4247 }
4248
4249 array_container_t *array_container_from_bitset(const bitset_container_t *bits) {
4250 array_container_t *result =
4251 array_container_create_given_capacity(bits->cardinality);
4252 result->cardinality = bits->cardinality;
4253 // sse version ends up being slower here
4254 // (bitset_extract_setbits_sse_uint16)
4255 // because of the sparsity of the data
4256 bitset_extract_setbits_uint16(bits->array, BITSET_CONTAINER_SIZE_IN_WORDS,
4257 result->array, 0);
4258 return result;
4259 }
4260
4261 /* assumes that container has adequate space. Run from [s,e] (inclusive) */
4262 static void add_run(run_container_t *r, int s, int e) {
4263 r->runs[r->n_runs].value = s;
4264 r->runs[r->n_runs].length = e - s;
4265 r->n_runs++;
4266 }
4267
4268 run_container_t *run_container_from_array(const array_container_t *c) {
4269 int32_t n_runs = array_container_number_of_runs(c);
4270 run_container_t *answer = run_container_create_given_capacity(n_runs);
4271 int prev = -2;
4272 int run_start = -1;
4273 int32_t card = c->cardinality;
4274 if (card == 0) return answer;
4275 for (int i = 0; i < card; ++i) {
4276 const uint16_t cur_val = c->array[i];
4277 if (cur_val != prev + 1) {
4278 // new run starts; flush old one, if any
4279 if (run_start != -1) add_run(answer, run_start, prev);
4280 run_start = cur_val;
4281 }
4282 prev = c->array[i];
4283 }
4284 // now prev is the last seen value
4285 add_run(answer, run_start, prev);
4286 // assert(run_container_cardinality(answer) == c->cardinality);
4287 return answer;
4288 }
4289
4290 /**
4291 * Convert the runcontainer to either a Bitmap or an Array Container, depending
4292 * on the cardinality. Frees the container.
4293 * Allocates and returns new container, which caller is responsible for freeing
4294 */
4295
4296 void *convert_to_bitset_or_array_container(run_container_t *r, int32_t card,
4297 uint8_t *resulttype) {
4298 if (card <= DEFAULT_MAX_SIZE) {
4299 array_container_t *answer = array_container_create_given_capacity(card);
4300 answer->cardinality = 0;
4301 for (int rlepos = 0; rlepos < r->n_runs; ++rlepos) {
4302 uint16_t run_start = r->runs[rlepos].value;
4303 uint16_t run_end = run_start + r->runs[rlepos].length;
4304 for (uint16_t run_value = run_start; run_value <= run_end;
4305 ++run_value) {
4306 answer->array[answer->cardinality++] = run_value;
4307 }
4308 }
4309 assert(card == answer->cardinality);
4310 *resulttype = ARRAY_CONTAINER_TYPE_CODE;
4311 run_container_free(r);
4312 return answer;
4313 }
4314 bitset_container_t *answer = bitset_container_create();
4315 for (int rlepos = 0; rlepos < r->n_runs; ++rlepos) {
4316 uint16_t run_start = r->runs[rlepos].value;
4317 bitset_set_lenrange(answer->array, run_start, r->runs[rlepos].length);
4318 }
4319 answer->cardinality = card;
4320 *resulttype = BITSET_CONTAINER_TYPE_CODE;
4321 run_container_free(r);
4322 return answer;
4323 }
4324
4325 /* Converts a run container to either an array or a bitset, IF it saves space.
4326 */
4327 /* If a conversion occurs, the caller is responsible to free the original
4328 * container and
4329 * he becomes responsible to free the new one. */
4330 void *convert_run_to_efficient_container(run_container_t *c,
4331 uint8_t *typecode_after) {
4332 int32_t size_as_run_container =
4333 run_container_serialized_size_in_bytes(c->n_runs);
4334
4335 int32_t size_as_bitset_container =
4336 bitset_container_serialized_size_in_bytes();
4337 int32_t card = run_container_cardinality(c);
4338 int32_t size_as_array_container =
4339 array_container_serialized_size_in_bytes(card);
4340
4341 int32_t min_size_non_run =
4342 size_as_bitset_container < size_as_array_container
4343 ? size_as_bitset_container
4344 : size_as_array_container;
4345 if (size_as_run_container <= min_size_non_run) { // no conversion
4346 *typecode_after = RUN_CONTAINER_TYPE_CODE;
4347 return c;
4348 }
4349 if (card <= DEFAULT_MAX_SIZE) {
4350 // to array
4351 array_container_t *answer = array_container_create_given_capacity(card);
4352 answer->cardinality = 0;
4353 for (int rlepos = 0; rlepos < c->n_runs; ++rlepos) {
4354 int run_start = c->runs[rlepos].value;
4355 int run_end = run_start + c->runs[rlepos].length;
4356
4357 for (int run_value = run_start; run_value <= run_end; ++run_value) {
4358 answer->array[answer->cardinality++] = (uint16_t)run_value;
4359 }
4360 }
4361 *typecode_after = ARRAY_CONTAINER_TYPE_CODE;
4362 return answer;
4363 }
4364
4365 // else to bitset
4366 bitset_container_t *answer = bitset_container_create();
4367
4368 for (int rlepos = 0; rlepos < c->n_runs; ++rlepos) {
4369 int start = c->runs[rlepos].value;
4370 int end = start + c->runs[rlepos].length;
4371 bitset_set_range(answer->array, start, end + 1);
4372 }
4373 answer->cardinality = card;
4374 *typecode_after = BITSET_CONTAINER_TYPE_CODE;
4375 return answer;
4376 }
4377
4378 // like convert_run_to_efficient_container but frees the old result if needed
4379 void *convert_run_to_efficient_container_and_free(run_container_t *c,
4380 uint8_t *typecode_after) {
4381 void *answer = convert_run_to_efficient_container(c, typecode_after);
4382 if (answer != c) run_container_free(c);
4383 return answer;
4384 }
4385
4386 /* once converted, the original container is disposed here, rather than
4387 in roaring_array
4388 */
4389
4390 // TODO: split into run- array- and bitset- subfunctions for sanity;
4391 // a few function calls won't really matter.
4392
4393 void *convert_run_optimize(void *c, uint8_t typecode_original,
4394 uint8_t *typecode_after) {
4395 if (typecode_original == RUN_CONTAINER_TYPE_CODE) {
4396 void *newc = convert_run_to_efficient_container((run_container_t *)c,
4397 typecode_after);
4398 if (newc != c) {
4399 container_free(c, typecode_original);
4400 }
4401 return newc;
4402 } else if (typecode_original == ARRAY_CONTAINER_TYPE_CODE) {
4403 // it might need to be converted to a run container.
4404 array_container_t *c_qua_array = (array_container_t *)c;
4405 int32_t n_runs = array_container_number_of_runs(c_qua_array);
4406 int32_t size_as_run_container =
4407 run_container_serialized_size_in_bytes(n_runs);
4408 int32_t card = array_container_cardinality(c_qua_array);
4409 int32_t size_as_array_container =
4410 array_container_serialized_size_in_bytes(card);
4411
4412 if (size_as_run_container >= size_as_array_container) {
4413 *typecode_after = ARRAY_CONTAINER_TYPE_CODE;
4414 return c;
4415 }
4416 // else convert array to run container
4417 run_container_t *answer = run_container_create_given_capacity(n_runs);
4418 int prev = -2;
4419 int run_start = -1;
4420
4421 assert(card > 0);
4422 for (int i = 0; i < card; ++i) {
4423 uint16_t cur_val = c_qua_array->array[i];
4424 if (cur_val != prev + 1) {
4425 // new run starts; flush old one, if any
4426 if (run_start != -1) add_run(answer, run_start, prev);
4427 run_start = cur_val;
4428 }
4429 prev = c_qua_array->array[i];
4430 }
4431 assert(run_start >= 0);
4432 // now prev is the last seen value
4433 add_run(answer, run_start, prev);
4434 *typecode_after = RUN_CONTAINER_TYPE_CODE;
4435 array_container_free(c_qua_array);
4436 return answer;
4437 } else if (typecode_original ==
4438 BITSET_CONTAINER_TYPE_CODE) { // run conversions on bitset
4439 // does bitset need conversion to run?
4440 bitset_container_t *c_qua_bitset = (bitset_container_t *)c;
4441 int32_t n_runs = bitset_container_number_of_runs(c_qua_bitset);
4442 int32_t size_as_run_container =
4443 run_container_serialized_size_in_bytes(n_runs);
4444 int32_t size_as_bitset_container =
4445 bitset_container_serialized_size_in_bytes();
4446
4447 if (size_as_bitset_container <= size_as_run_container) {
4448 // no conversion needed.
4449 *typecode_after = BITSET_CONTAINER_TYPE_CODE;
4450 return c;
4451 }
4452 // bitset to runcontainer (ported from Java RunContainer(
4453 // BitmapContainer bc, int nbrRuns))
4454 assert(n_runs > 0); // no empty bitmaps
4455 run_container_t *answer = run_container_create_given_capacity(n_runs);
4456
4457 int long_ctr = 0;
4458 uint64_t cur_word = c_qua_bitset->array[0];
4459 int run_count = 0;
4460 while (true) {
4461 while (cur_word == UINT64_C(0) &&
4462 long_ctr < BITSET_CONTAINER_SIZE_IN_WORDS - 1)
4463 cur_word = c_qua_bitset->array[++long_ctr];
4464
4465 if (cur_word == UINT64_C(0)) {
4466 bitset_container_free(c_qua_bitset);
4467 *typecode_after = RUN_CONTAINER_TYPE_CODE;
4468 return answer;
4469 }
4470
4471 int local_run_start = __builtin_ctzll(cur_word);
4472 int run_start = local_run_start + 64 * long_ctr;
4473 uint64_t cur_word_with_1s = cur_word | (cur_word - 1);
4474
4475 int run_end = 0;
4476 while (cur_word_with_1s == UINT64_C(0xFFFFFFFFFFFFFFFF) &&
4477 long_ctr < BITSET_CONTAINER_SIZE_IN_WORDS - 1)
4478 cur_word_with_1s = c_qua_bitset->array[++long_ctr];
4479
4480 if (cur_word_with_1s == UINT64_C(0xFFFFFFFFFFFFFFFF)) {
4481 run_end = 64 + long_ctr * 64; // exclusive, I guess
4482 add_run(answer, run_start, run_end - 1);
4483 bitset_container_free(c_qua_bitset);
4484 *typecode_after = RUN_CONTAINER_TYPE_CODE;
4485 return answer;
4486 }
4487 int local_run_end = __builtin_ctzll(~cur_word_with_1s);
4488 run_end = local_run_end + long_ctr * 64;
4489 add_run(answer, run_start, run_end - 1);
4490 run_count++;
4491 cur_word = cur_word_with_1s & (cur_word_with_1s + 1);
4492 }
4493 return answer;
4494 } else {
4495 assert(false);
4496 __builtin_unreachable();
4497 return NULL;
4498 }
4499 }
4500
4501 bitset_container_t *bitset_container_from_run_range(const run_container_t *run,
4502 uint32_t min, uint32_t max) {
4503 bitset_container_t *bitset = bitset_container_create();
4504 int32_t union_cardinality = 0;
4505 for (int32_t i = 0; i < run->n_runs; ++i) {
4506 uint32_t rle_min = run->runs[i].value;
4507 uint32_t rle_max = rle_min + run->runs[i].length;
4508 bitset_set_lenrange(bitset->array, rle_min, rle_max - rle_min);
4509 union_cardinality += run->runs[i].length + 1;
4510 }
4511 union_cardinality += max - min + 1;
4512 union_cardinality -= bitset_lenrange_cardinality(bitset->array, min, max-min);
4513 bitset_set_lenrange(bitset->array, min, max - min);
4514 bitset->cardinality = union_cardinality;
4515 return bitset;
4516 }
4517 /* end file src/containers/convert.c */
4518 /* begin file src/containers/mixed_andnot.c */
4519 /*
4520 * mixed_andnot.c. More methods since operation is not symmetric,
4521 * except no "wide" andnot , so no lazy options motivated.
4522 */
4523
4524 #include <assert.h>
4525 #include <string.h>
4526
4527
4528 /* Compute the andnot of src_1 and src_2 and write the result to
4529 * dst, a valid array container that could be the same as dst.*/
4530 void array_bitset_container_andnot(const array_container_t *src_1,
4531 const bitset_container_t *src_2,
4532 array_container_t *dst) {
4533 // follows Java implementation as of June 2016
4534 if (dst->capacity < src_1->cardinality)
4535 array_container_grow(dst, src_1->cardinality, INT32_MAX, false);
4536 int32_t newcard = 0;
4537 const int32_t origcard = src_1->cardinality;
4538 for (int i = 0; i < origcard; ++i) {
4539 uint16_t key = src_1->array[i];
4540 dst->array[newcard] = key;
4541 newcard += 1 - bitset_container_contains(src_2, key);
4542 }
4543 dst->cardinality = newcard;
4544 }
4545
4546 /* Compute the andnot of src_1 and src_2 and write the result to
4547 * src_1 */
4548
4549 void array_bitset_container_iandnot(array_container_t *src_1,
4550 const bitset_container_t *src_2) {
4551 array_bitset_container_andnot(src_1, src_2, src_1);
4552 }
4553
4554 /* Compute the andnot of src_1 and src_2 and write the result to
4555 * dst, which does not initially have a valid container.
4556 * Return true for a bitset result; false for array
4557 */
4558
4559 bool bitset_array_container_andnot(const bitset_container_t *src_1,
4560 const array_container_t *src_2, void **dst) {
4561 // Java did this directly, but we have option of asm or avx
4562 bitset_container_t *result = bitset_container_create();
4563 bitset_container_copy(src_1, result);
4564 result->cardinality =
4565 (int32_t)bitset_clear_list(result->array, (uint64_t)result->cardinality,
4566 src_2->array, (uint64_t)src_2->cardinality);
4567
4568 // do required type conversions.
4569 if (result->cardinality <= DEFAULT_MAX_SIZE) {
4570 *dst = array_container_from_bitset(result);
4571 bitset_container_free(result);
4572 return false;
4573 }
4574 *dst = result;
4575 return true;
4576 }
4577
4578 /* Compute the andnot of src_1 and src_2 and write the result to
4579 * dst (which has no container initially). It will modify src_1
4580 * to be dst if the result is a bitset. Otherwise, it will
4581 * free src_1 and dst will be a new array container. In both
4582 * cases, the caller is responsible for deallocating dst.
4583 * Returns true iff dst is a bitset */
4584
4585 bool bitset_array_container_iandnot(bitset_container_t *src_1,
4586 const array_container_t *src_2,
4587 void **dst) {
4588 *dst = src_1;
4589 src_1->cardinality =
4590 (int32_t)bitset_clear_list(src_1->array, (uint64_t)src_1->cardinality,
4591 src_2->array, (uint64_t)src_2->cardinality);
4592
4593 if (src_1->cardinality <= DEFAULT_MAX_SIZE) {
4594 *dst = array_container_from_bitset(src_1);
4595 bitset_container_free(src_1);
4596 return false; // not bitset
4597 } else
4598 return true;
4599 }
4600
4601 /* Compute the andnot of src_1 and src_2 and write the result to
4602 * dst. Result may be either a bitset or an array container
4603 * (returns "result is bitset"). dst does not initially have
4604 * any container, but becomes either a bitset container (return
4605 * result true) or an array container.
4606 */
4607
4608 bool run_bitset_container_andnot(const run_container_t *src_1,
4609 const bitset_container_t *src_2, void **dst) {
4610 // follows the Java implementation as of June 2016
4611 int card = run_container_cardinality(src_1);
4612 if (card <= DEFAULT_MAX_SIZE) {
4613 // must be an array
4614 array_container_t *answer = array_container_create_given_capacity(card);
4615 answer->cardinality = 0;
4616 for (int32_t rlepos = 0; rlepos < src_1->n_runs; ++rlepos) {
4617 rle16_t rle = src_1->runs[rlepos];
4618 for (int run_value = rle.value; run_value <= rle.value + rle.length;
4619 ++run_value) {
4620 if (!bitset_container_get(src_2, (uint16_t)run_value)) {
4621 answer->array[answer->cardinality++] = (uint16_t)run_value;
4622 }
4623 }
4624 }
4625 *dst = answer;
4626 return false;
4627 } else { // we guess it will be a bitset, though have to check guess when
4628 // done
4629 bitset_container_t *answer = bitset_container_clone(src_2);
4630
4631 uint32_t last_pos = 0;
4632 for (int32_t rlepos = 0; rlepos < src_1->n_runs; ++rlepos) {
4633 rle16_t rle = src_1->runs[rlepos];
4634
4635 uint32_t start = rle.value;
4636 uint32_t end = start + rle.length + 1;
4637 bitset_reset_range(answer->array, last_pos, start);
4638 bitset_flip_range(answer->array, start, end);
4639 last_pos = end;
4640 }
4641 bitset_reset_range(answer->array, last_pos, (uint32_t)(1 << 16));
4642
4643 answer->cardinality = bitset_container_compute_cardinality(answer);
4644
4645 if (answer->cardinality <= DEFAULT_MAX_SIZE) {
4646 *dst = array_container_from_bitset(answer);
4647 bitset_container_free(answer);
4648 return false; // not bitset
4649 }
4650 *dst = answer;
4651 return true; // bitset
4652 }
4653 }
4654
4655 /* Compute the andnot of src_1 and src_2 and write the result to
4656 * dst. Result may be either a bitset or an array container
4657 * (returns "result is bitset"). dst does not initially have
4658 * any container, but becomes either a bitset container (return
4659 * result true) or an array container.
4660 */
4661
4662 bool run_bitset_container_iandnot(run_container_t *src_1,
4663 const bitset_container_t *src_2, void **dst) {
4664 // dummy implementation
4665 bool ans = run_bitset_container_andnot(src_1, src_2, dst);
4666 run_container_free(src_1);
4667 return ans;
4668 }
4669
4670 /* Compute the andnot of src_1 and src_2 and write the result to
4671 * dst. Result may be either a bitset or an array container
4672 * (returns "result is bitset"). dst does not initially have
4673 * any container, but becomes either a bitset container (return
4674 * result true) or an array container.
4675 */
4676
4677 bool bitset_run_container_andnot(const bitset_container_t *src_1,
4678 const run_container_t *src_2, void **dst) {
4679 // follows Java implementation
4680 bitset_container_t *result = bitset_container_create();
4681
4682 bitset_container_copy(src_1, result);
4683 for (int32_t rlepos = 0; rlepos < src_2->n_runs; ++rlepos) {
4684 rle16_t rle = src_2->runs[rlepos];
4685 bitset_reset_range(result->array, rle.value,
4686 rle.value + rle.length + UINT32_C(1));
4687 }
4688 result->cardinality = bitset_container_compute_cardinality(result);
4689
4690 if (result->cardinality <= DEFAULT_MAX_SIZE) {
4691 *dst = array_container_from_bitset(result);
4692 bitset_container_free(result);
4693 return false; // not bitset
4694 }
4695 *dst = result;
4696 return true; // bitset
4697 }
4698
4699 /* Compute the andnot of src_1 and src_2 and write the result to
4700 * dst (which has no container initially). It will modify src_1
4701 * to be dst if the result is a bitset. Otherwise, it will
4702 * free src_1 and dst will be a new array container. In both
4703 * cases, the caller is responsible for deallocating dst.
4704 * Returns true iff dst is a bitset */
4705
4706 bool bitset_run_container_iandnot(bitset_container_t *src_1,
4707 const run_container_t *src_2, void **dst) {
4708 *dst = src_1;
4709
4710 for (int32_t rlepos = 0; rlepos < src_2->n_runs; ++rlepos) {
4711 rle16_t rle = src_2->runs[rlepos];
4712 bitset_reset_range(src_1->array, rle.value,
4713 rle.value + rle.length + UINT32_C(1));
4714 }
4715 src_1->cardinality = bitset_container_compute_cardinality(src_1);
4716
4717 if (src_1->cardinality <= DEFAULT_MAX_SIZE) {
4718 *dst = array_container_from_bitset(src_1);
4719 bitset_container_free(src_1);
4720 return false; // not bitset
4721 } else
4722 return true;
4723 }
4724
4725 /* helper. a_out must be a valid array container with adequate capacity.
4726 * Returns the cardinality of the output container. Partly Based on Java
4727 * implementation Util.unsignedDifference.
4728 *
4729 * TODO: Util.unsignedDifference does not use advanceUntil. Is it cheaper
4730 * to avoid advanceUntil?
4731 */
4732
4733 static int run_array_array_subtract(const run_container_t *r,
4734 const array_container_t *a_in,
4735 array_container_t *a_out) {
4736 int out_card = 0;
4737 int32_t in_array_pos =
4738 -1; // since advanceUntil always assumes we start the search AFTER this
4739
4740 for (int rlepos = 0; rlepos < r->n_runs; rlepos++) {
4741 int32_t start = r->runs[rlepos].value;
4742 int32_t end = start + r->runs[rlepos].length + 1;
4743
4744 in_array_pos = advanceUntil(a_in->array, in_array_pos,
4745 a_in->cardinality, (uint16_t)start);
4746
4747 if (in_array_pos >= a_in->cardinality) { // run has no items subtracted
4748 for (int32_t i = start; i < end; ++i)
4749 a_out->array[out_card++] = (uint16_t)i;
4750 } else {
4751 uint16_t next_nonincluded = a_in->array[in_array_pos];
4752 if (next_nonincluded >= end) {
4753 // another case when run goes unaltered
4754 for (int32_t i = start; i < end; ++i)
4755 a_out->array[out_card++] = (uint16_t)i;
4756 in_array_pos--; // ensure we see this item again if necessary
4757 } else {
4758 for (int32_t i = start; i < end; ++i)
4759 if (i != next_nonincluded)
4760 a_out->array[out_card++] = (uint16_t)i;
4761 else // 0 should ensure we don't match
4762 next_nonincluded =
4763 (in_array_pos + 1 >= a_in->cardinality)
4764 ? 0
4765 : a_in->array[++in_array_pos];
4766 in_array_pos--; // see again
4767 }
4768 }
4769 }
4770 return out_card;
4771 }
4772
4773 /* dst does not indicate a valid container initially. Eventually it
4774 * can become any type of container.
4775 */
4776
4777 int run_array_container_andnot(const run_container_t *src_1,
4778 const array_container_t *src_2, void **dst) {
4779 // follows the Java impl as of June 2016
4780
4781 int card = run_container_cardinality(src_1);
4782 const int arbitrary_threshold = 32;
4783
4784 if (card <= arbitrary_threshold) {
4785 if (src_2->cardinality == 0) {
4786 *dst = run_container_clone(src_1);
4787 return RUN_CONTAINER_TYPE_CODE;
4788 }
4789 // Java's "lazyandNot.toEfficientContainer" thing
4790 run_container_t *answer = run_container_create_given_capacity(
4791 card + array_container_cardinality(src_2));
4792
4793 int rlepos = 0;
4794 int xrlepos = 0; // "x" is src_2
4795 rle16_t rle = src_1->runs[rlepos];
4796 int32_t start = rle.value;
4797 int32_t end = start + rle.length + 1;
4798 int32_t xstart = src_2->array[xrlepos];
4799
4800 while ((rlepos < src_1->n_runs) && (xrlepos < src_2->cardinality)) {
4801 if (end <= xstart) {
4802 // output the first run
4803 answer->runs[answer->n_runs++] =
4804 (rle16_t){.value = (uint16_t)start,
4805 .length = (uint16_t)(end - start - 1)};
4806 rlepos++;
4807 if (rlepos < src_1->n_runs) {
4808 start = src_1->runs[rlepos].value;
4809 end = start + src_1->runs[rlepos].length + 1;
4810 }
4811 } else if (xstart + 1 <= start) {
4812 // exit the second run
4813 xrlepos++;
4814 if (xrlepos < src_2->cardinality) {
4815 xstart = src_2->array[xrlepos];
4816 }
4817 } else {
4818 if (start < xstart) {
4819 answer->runs[answer->n_runs++] =
4820 (rle16_t){.value = (uint16_t)start,
4821 .length = (uint16_t)(xstart - start - 1)};
4822 }
4823 if (xstart + 1 < end) {
4824 start = xstart + 1;
4825 } else {
4826 rlepos++;
4827 if (rlepos < src_1->n_runs) {
4828 start = src_1->runs[rlepos].value;
4829 end = start + src_1->runs[rlepos].length + 1;
4830 }
4831 }
4832 }
4833 }
4834 if (rlepos < src_1->n_runs) {
4835 answer->runs[answer->n_runs++] =
4836 (rle16_t){.value = (uint16_t)start,
4837 .length = (uint16_t)(end - start - 1)};
4838 rlepos++;
4839 if (rlepos < src_1->n_runs) {
4840 memcpy(answer->runs + answer->n_runs, src_1->runs + rlepos,
4841 (src_1->n_runs - rlepos) * sizeof(rle16_t));
4842 answer->n_runs += (src_1->n_runs - rlepos);
4843 }
4844 }
4845 uint8_t return_type;
4846 *dst = convert_run_to_efficient_container(answer, &return_type);
4847 if (answer != *dst) run_container_free(answer);
4848 return return_type;
4849 }
4850 // else it's a bitmap or array
4851
4852 if (card <= DEFAULT_MAX_SIZE) {
4853 array_container_t *ac = array_container_create_given_capacity(card);
4854 // nb Java code used a generic iterator-based merge to compute
4855 // difference
4856 ac->cardinality = run_array_array_subtract(src_1, src_2, ac);
4857 *dst = ac;
4858 return ARRAY_CONTAINER_TYPE_CODE;
4859 }
4860 bitset_container_t *ans = bitset_container_from_run(src_1);
4861 bool result_is_bitset = bitset_array_container_iandnot(ans, src_2, dst);
4862 return (result_is_bitset ? BITSET_CONTAINER_TYPE_CODE
4863 : ARRAY_CONTAINER_TYPE_CODE);
4864 }
4865
4866 /* Compute the andnot of src_1 and src_2 and write the result to
4867 * dst (which has no container initially). It will modify src_1
4868 * to be dst if the result is a bitset. Otherwise, it will
4869 * free src_1 and dst will be a new array container. In both
4870 * cases, the caller is responsible for deallocating dst.
4871 * Returns true iff dst is a bitset */
4872
4873 int run_array_container_iandnot(run_container_t *src_1,
4874 const array_container_t *src_2, void **dst) {
4875 // dummy implementation same as June 2016 Java
4876 int ans = run_array_container_andnot(src_1, src_2, dst);
4877 run_container_free(src_1);
4878 return ans;
4879 }
4880
4881 /* dst must be a valid array container, allowed to be src_1 */
4882
4883 void array_run_container_andnot(const array_container_t *src_1,
4884 const run_container_t *src_2,
4885 array_container_t *dst) {
4886 // basically following Java impl as of June 2016
4887 if (src_1->cardinality > dst->capacity)
4888 array_container_grow(dst, src_1->cardinality, INT32_MAX, false);
4889
4890 if (src_2->n_runs == 0) {
4891 memmove(dst->array, src_1->array,
4892 sizeof(uint16_t) * src_1->cardinality);
4893 dst->cardinality = src_1->cardinality;
4894 return;
4895 }
4896 int32_t run_start = src_2->runs[0].value;
4897 int32_t run_end = run_start + src_2->runs[0].length;
4898 int which_run = 0;
4899
4900 uint16_t val = 0;
4901 int dest_card = 0;
4902 for (int i = 0; i < src_1->cardinality; ++i) {
4903 val = src_1->array[i];
4904 if (val < run_start)
4905 dst->array[dest_card++] = val;
4906 else if (val <= run_end) {
4907 ; // omitted item
4908 } else {
4909 do {
4910 if (which_run + 1 < src_2->n_runs) {
4911 ++which_run;
4912 run_start = src_2->runs[which_run].value;
4913 run_end = run_start + src_2->runs[which_run].length;
4914
4915 } else
4916 run_start = run_end = (1 << 16) + 1;
4917 } while (val > run_end);
4918 --i;
4919 }
4920 }
4921 dst->cardinality = dest_card;
4922 }
4923
4924 /* dst does not indicate a valid container initially. Eventually it
4925 * can become any kind of container.
4926 */
4927
4928 void array_run_container_iandnot(array_container_t *src_1,
4929 const run_container_t *src_2) {
4930 array_run_container_andnot(src_1, src_2, src_1);
4931 }
4932
4933 /* dst does not indicate a valid container initially. Eventually it
4934 * can become any kind of container.
4935 */
4936
4937 int run_run_container_andnot(const run_container_t *src_1,
4938 const run_container_t *src_2, void **dst) {
4939 run_container_t *ans = run_container_create();
4940 run_container_andnot(src_1, src_2, ans);
4941 uint8_t typecode_after;
4942 *dst = convert_run_to_efficient_container_and_free(ans, &typecode_after);
4943 return typecode_after;
4944 }
4945
4946 /* Compute the andnot of src_1 and src_2 and write the result to
4947 * dst (which has no container initially). It will modify src_1
4948 * to be dst if the result is a bitset. Otherwise, it will
4949 * free src_1 and dst will be a new array container. In both
4950 * cases, the caller is responsible for deallocating dst.
4951 * Returns true iff dst is a bitset */
4952
4953 int run_run_container_iandnot(run_container_t *src_1,
4954 const run_container_t *src_2, void **dst) {
4955 // following Java impl as of June 2016 (dummy)
4956 int ans = run_run_container_andnot(src_1, src_2, dst);
4957 run_container_free(src_1);
4958 return ans;
4959 }
4960
4961 /*
4962 * dst is a valid array container and may be the same as src_1
4963 */
4964
4965 void array_array_container_andnot(const array_container_t *src_1,
4966 const array_container_t *src_2,
4967 array_container_t *dst) {
4968 array_container_andnot(src_1, src_2, dst);
4969 }
4970
4971 /* inplace array-array andnot will always be able to reuse the space of
4972 * src_1 */
4973 void array_array_container_iandnot(array_container_t *src_1,
4974 const array_container_t *src_2) {
4975 array_container_andnot(src_1, src_2, src_1);
4976 }
4977
4978 /* Compute the andnot of src_1 and src_2 and write the result to
4979 * dst (which has no container initially). Return value is
4980 * "dst is a bitset"
4981 */
4982
4983 bool bitset_bitset_container_andnot(const bitset_container_t *src_1,
4984 const bitset_container_t *src_2,
4985 void **dst) {
4986 bitset_container_t *ans = bitset_container_create();
4987 int card = bitset_container_andnot(src_1, src_2, ans);
4988 if (card <= DEFAULT_MAX_SIZE) {
4989 *dst = array_container_from_bitset(ans);
4990 bitset_container_free(ans);
4991 return false; // not bitset
4992 } else {
4993 *dst = ans;
4994 return true;
4995 }
4996 }
4997
4998 /* Compute the andnot of src_1 and src_2 and write the result to
4999 * dst (which has no container initially). It will modify src_1
5000 * to be dst if the result is a bitset. Otherwise, it will
5001 * free src_1 and dst will be a new array container. In both
5002 * cases, the caller is responsible for deallocating dst.
5003 * Returns true iff dst is a bitset */
5004
5005 bool bitset_bitset_container_iandnot(bitset_container_t *src_1,
5006 const bitset_container_t *src_2,
5007 void **dst) {
5008 int card = bitset_container_andnot(src_1, src_2, src_1);
5009 if (card <= DEFAULT_MAX_SIZE) {
5010 *dst = array_container_from_bitset(src_1);
5011 bitset_container_free(src_1);
5012 return false; // not bitset
5013 } else {
5014 *dst = src_1;
5015 return true;
5016 }
5017 }
5018 /* end file src/containers/mixed_andnot.c */
5019 /* begin file src/containers/mixed_equal.c */
5020
5021 bool array_container_equal_bitset(const array_container_t* container1,
5022 const bitset_container_t* container2) {
5023 if (container2->cardinality != BITSET_UNKNOWN_CARDINALITY) {
5024 if (container2->cardinality != container1->cardinality) {
5025 return false;
5026 }
5027 }
5028 int32_t pos = 0;
5029 for (int32_t i = 0; i < BITSET_CONTAINER_SIZE_IN_WORDS; ++i) {
5030 uint64_t w = container2->array[i];
5031 while (w != 0) {
5032 uint64_t t = w & (~w + 1);
5033 uint16_t r = i * 64 + __builtin_ctzll(w);
5034 if (pos >= container1->cardinality) {
5035 return false;
5036 }
5037 if (container1->array[pos] != r) {
5038 return false;
5039 }
5040 ++pos;
5041 w ^= t;
5042 }
5043 }
5044 return (pos == container1->cardinality);
5045 }
5046
5047 bool run_container_equals_array(const run_container_t* container1,
5048 const array_container_t* container2) {
5049 if (run_container_cardinality(container1) != container2->cardinality)
5050 return false;
5051 int32_t pos = 0;
5052 for (int i = 0; i < container1->n_runs; ++i) {
5053 const uint32_t run_start = container1->runs[i].value;
5054 const uint32_t le = container1->runs[i].length;
5055
5056 if (container2->array[pos] != run_start) {
5057 return false;
5058 }
5059
5060 if (container2->array[pos + le] != run_start + le) {
5061 return false;
5062 }
5063
5064 pos += le + 1;
5065 }
5066 return true;
5067 }
5068
5069 bool run_container_equals_bitset(const run_container_t* container1,
5070 const bitset_container_t* container2) {
5071 if (container2->cardinality != BITSET_UNKNOWN_CARDINALITY) {
5072 if (container2->cardinality != run_container_cardinality(container1)) {
5073 return false;
5074 }
5075 } else {
5076 int32_t card = bitset_container_compute_cardinality(
5077 container2); // modify container2?
5078 if (card != run_container_cardinality(container1)) {
5079 return false;
5080 }
5081 }
5082 for (int i = 0; i < container1->n_runs; ++i) {
5083 uint32_t run_start = container1->runs[i].value;
5084 uint32_t le = container1->runs[i].length;
5085 for (uint32_t j = run_start; j <= run_start + le; ++j) {
5086 // todo: this code could be much faster
5087 if (!bitset_container_contains(container2, j)) {
5088 return false;
5089 }
5090 }
5091 }
5092 return true;
5093 }
5094 /* end file src/containers/mixed_equal.c */
5095 /* begin file src/containers/mixed_intersection.c */
5096 /*
5097 * mixed_intersection.c
5098 *
5099 */
5100
5101
5102 /* Compute the intersection of src_1 and src_2 and write the result to
5103 * dst. */
5104 void array_bitset_container_intersection(const array_container_t *src_1,
5105 const bitset_container_t *src_2,
5106 array_container_t *dst) {
5107 if (dst->capacity < src_1->cardinality)
5108 array_container_grow(dst, src_1->cardinality, INT32_MAX, false);
5109 int32_t newcard = 0; // dst could be src_1
5110 const int32_t origcard = src_1->cardinality;
5111 for (int i = 0; i < origcard; ++i) {
5112 uint16_t key = src_1->array[i];
5113 // this branchless approach is much faster...
5114 dst->array[newcard] = key;
5115 newcard += bitset_container_contains(src_2, key);
5116 /**
5117 * we could do it this way instead...
5118 * if (bitset_container_contains(src_2, key)) {
5119 * dst->array[newcard++] = key;
5120 * }
5121 * but if the result is unpredictible, the processor generates
5122 * many mispredicted branches.
5123 * Difference can be huge (from 3 cycles when predictible all the way
5124 * to 16 cycles when unpredictible.
5125 * See
5126 * https://github.com/lemire/Code-used-on-Daniel-Lemire-s-blog/blob/master/extra/bitset/c/arraybitsetintersection.c
5127 */
5128 }
5129 dst->cardinality = newcard;
5130 }
5131
5132 /* Compute the size of the intersection of src_1 and src_2. */
5133 int array_bitset_container_intersection_cardinality(
5134 const array_container_t *src_1, const bitset_container_t *src_2) {
5135 int32_t newcard = 0;
5136 const int32_t origcard = src_1->cardinality;
5137 for (int i = 0; i < origcard; ++i) {
5138 uint16_t key = src_1->array[i];
5139 newcard += bitset_container_contains(src_2, key);
5140 }
5141 return newcard;
5142 }
5143
5144
5145 bool array_bitset_container_intersect(const array_container_t *src_1,
5146 const bitset_container_t *src_2) {
5147 const int32_t origcard = src_1->cardinality;
5148 for (int i = 0; i < origcard; ++i) {
5149 uint16_t key = src_1->array[i];
5150 if(bitset_container_contains(src_2, key)) return true;
5151 }
5152 return false;
5153 }
5154
5155 /* Compute the intersection of src_1 and src_2 and write the result to
5156 * dst. It is allowed for dst to be equal to src_1. We assume that dst is a
5157 * valid container. */
5158 void array_run_container_intersection(const array_container_t *src_1,
5159 const run_container_t *src_2,
5160 array_container_t *dst) {
5161 if (run_container_is_full(src_2)) {
5162 if (dst != src_1) array_container_copy(src_1, dst);
5163 return;
5164 }
5165 if (dst->capacity < src_1->cardinality)
5166 array_container_grow(dst, src_1->cardinality, INT32_MAX, false);
5167 if (src_2->n_runs == 0) {
5168 return;
5169 }
5170 int32_t rlepos = 0;
5171 int32_t arraypos = 0;
5172 rle16_t rle = src_2->runs[rlepos];
5173 int32_t newcard = 0;
5174 while (arraypos < src_1->cardinality) {
5175 const uint16_t arrayval = src_1->array[arraypos];
5176 while (rle.value + rle.length <
5177 arrayval) { // this will frequently be false
5178 ++rlepos;
5179 if (rlepos == src_2->n_runs) {
5180 dst->cardinality = newcard;
5181 return; // we are done
5182 }
5183 rle = src_2->runs[rlepos];
5184 }
5185 if (rle.value > arrayval) {
5186 arraypos = advanceUntil(src_1->array, arraypos, src_1->cardinality,
5187 rle.value);
5188 } else {
5189 dst->array[newcard] = arrayval;
5190 newcard++;
5191 arraypos++;
5192 }
5193 }
5194 dst->cardinality = newcard;
5195 }
5196
5197 /* Compute the intersection of src_1 and src_2 and write the result to
5198 * *dst. If the result is true then the result is a bitset_container_t
5199 * otherwise is a array_container_t. If *dst == src_2, an in-place processing
5200 * is attempted.*/
5201 bool run_bitset_container_intersection(const run_container_t *src_1,
5202 const bitset_container_t *src_2,
5203 void **dst) {
5204 if (run_container_is_full(src_1)) {
5205 if (*dst != src_2) *dst = bitset_container_clone(src_2);
5206 return true;
5207 }
5208 int32_t card = run_container_cardinality(src_1);
5209 if (card <= DEFAULT_MAX_SIZE) {
5210 // result can only be an array (assuming that we never make a
5211 // RunContainer)
5212 if (card > src_2->cardinality) {
5213 card = src_2->cardinality;
5214 }
5215 array_container_t *answer = array_container_create_given_capacity(card);
5216 *dst = answer;
5217 if (*dst == NULL) {
5218 return false;
5219 }
5220 for (int32_t rlepos = 0; rlepos < src_1->n_runs; ++rlepos) {
5221 rle16_t rle = src_1->runs[rlepos];
5222 uint32_t endofrun = (uint32_t)rle.value + rle.length;
5223 for (uint32_t runValue = rle.value; runValue <= endofrun;
5224 ++runValue) {
5225 answer->array[answer->cardinality] = (uint16_t)runValue;
5226 answer->cardinality +=
5227 bitset_container_contains(src_2, runValue);
5228 }
5229 }
5230 return false;
5231 }
5232 if (*dst == src_2) { // we attempt in-place
5233 bitset_container_t *answer = (bitset_container_t *)*dst;
5234 uint32_t start = 0;
5235 for (int32_t rlepos = 0; rlepos < src_1->n_runs; ++rlepos) {
5236 const rle16_t rle = src_1->runs[rlepos];
5237 uint32_t end = rle.value;
5238 bitset_reset_range(src_2->array, start, end);
5239
5240 start = end + rle.length + 1;
5241 }
5242 bitset_reset_range(src_2->array, start, UINT32_C(1) << 16);
5243 answer->cardinality = bitset_container_compute_cardinality(answer);
5244 if (src_2->cardinality > DEFAULT_MAX_SIZE) {
5245 return true;
5246 } else {
5247 array_container_t *newanswer = array_container_from_bitset(src_2);
5248 if (newanswer == NULL) {
5249 *dst = NULL;
5250 return false;
5251 }
5252 *dst = newanswer;
5253 return false;
5254 }
5255 } else { // no inplace
5256 // we expect the answer to be a bitmap (if we are lucky)
5257 bitset_container_t *answer = bitset_container_clone(src_2);
5258
5259 *dst = answer;
5260 if (answer == NULL) {
5261 return true;
5262 }
5263 uint32_t start = 0;
5264 for (int32_t rlepos = 0; rlepos < src_1->n_runs; ++rlepos) {
5265 const rle16_t rle = src_1->runs[rlepos];
5266 uint32_t end = rle.value;
5267 bitset_reset_range(answer->array, start, end);
5268 start = end + rle.length + 1;
5269 }
5270 bitset_reset_range(answer->array, start, UINT32_C(1) << 16);
5271 answer->cardinality = bitset_container_compute_cardinality(answer);
5272
5273 if (answer->cardinality > DEFAULT_MAX_SIZE) {
5274 return true;
5275 } else {
5276 array_container_t *newanswer = array_container_from_bitset(answer);
5277 bitset_container_free((bitset_container_t *)*dst);
5278 if (newanswer == NULL) {
5279 *dst = NULL;
5280 return false;
5281 }
5282 *dst = newanswer;
5283 return false;
5284 }
5285 }
5286 }
5287
5288 /* Compute the size of the intersection between src_1 and src_2 . */
5289 int array_run_container_intersection_cardinality(const array_container_t *src_1,
5290 const run_container_t *src_2) {
5291 if (run_container_is_full(src_2)) {
5292 return src_1->cardinality;
5293 }
5294 if (src_2->n_runs == 0) {
5295 return 0;
5296 }
5297 int32_t rlepos = 0;
5298 int32_t arraypos = 0;
5299 rle16_t rle = src_2->runs[rlepos];
5300 int32_t newcard = 0;
5301 while (arraypos < src_1->cardinality) {
5302 const uint16_t arrayval = src_1->array[arraypos];
5303 while (rle.value + rle.length <
5304 arrayval) { // this will frequently be false
5305 ++rlepos;
5306 if (rlepos == src_2->n_runs) {
5307 return newcard; // we are done
5308 }
5309 rle = src_2->runs[rlepos];
5310 }
5311 if (rle.value > arrayval) {
5312 arraypos = advanceUntil(src_1->array, arraypos, src_1->cardinality,
5313 rle.value);
5314 } else {
5315 newcard++;
5316 arraypos++;
5317 }
5318 }
5319 return newcard;
5320 }
5321
5322 /* Compute the intersection between src_1 and src_2
5323 **/
5324 int run_bitset_container_intersection_cardinality(
5325 const run_container_t *src_1, const bitset_container_t *src_2) {
5326 if (run_container_is_full(src_1)) {
5327 return bitset_container_cardinality(src_2);
5328 }
5329 int answer = 0;
5330 for (int32_t rlepos = 0; rlepos < src_1->n_runs; ++rlepos) {
5331 rle16_t rle = src_1->runs[rlepos];
5332 answer +=
5333 bitset_lenrange_cardinality(src_2->array, rle.value, rle.length);
5334 }
5335 return answer;
5336 }
5337
5338
5339 bool array_run_container_intersect(const array_container_t *src_1,
5340 const run_container_t *src_2) {
5341 if( run_container_is_full(src_2) ) {
5342 return !array_container_empty(src_1);
5343 }
5344 if (src_2->n_runs == 0) {
5345 return false;
5346 }
5347 int32_t rlepos = 0;
5348 int32_t arraypos = 0;
5349 rle16_t rle = src_2->runs[rlepos];
5350 while (arraypos < src_1->cardinality) {
5351 const uint16_t arrayval = src_1->array[arraypos];
5352 while (rle.value + rle.length <
5353 arrayval) { // this will frequently be false
5354 ++rlepos;
5355 if (rlepos == src_2->n_runs) {
5356 return false; // we are done
5357 }
5358 rle = src_2->runs[rlepos];
5359 }
5360 if (rle.value > arrayval) {
5361 arraypos = advanceUntil(src_1->array, arraypos, src_1->cardinality,
5362 rle.value);
5363 } else {
5364 return true;
5365 }
5366 }
5367 return false;
5368 }
5369
5370 /* Compute the intersection between src_1 and src_2
5371 **/
5372 bool run_bitset_container_intersect(const run_container_t *src_1,
5373 const bitset_container_t *src_2) {
5374 if( run_container_is_full(src_1) ) {
5375 return !bitset_container_empty(src_2);
5376 }
5377 for (int32_t rlepos = 0; rlepos < src_1->n_runs; ++rlepos) {
5378 rle16_t rle = src_1->runs[rlepos];
5379 if(!bitset_lenrange_empty(src_2->array, rle.value,rle.length)) return true;
5380 }
5381 return false;
5382 }
5383
5384 /*
5385 * Compute the intersection between src_1 and src_2 and write the result
5386 * to *dst. If the return function is true, the result is a bitset_container_t
5387 * otherwise is a array_container_t.
5388 */
5389 bool bitset_bitset_container_intersection(const bitset_container_t *src_1,
5390 const bitset_container_t *src_2,
5391 void **dst) {
5392 const int newCardinality = bitset_container_and_justcard(src_1, src_2);
5393 if (newCardinality > DEFAULT_MAX_SIZE) {
5394 *dst = bitset_container_create();
5395 if (*dst != NULL) {
5396 bitset_container_and_nocard(src_1, src_2,
5397 (bitset_container_t *)*dst);
5398 ((bitset_container_t *)*dst)->cardinality = newCardinality;
5399 }
5400 return true; // it is a bitset
5401 }
5402 *dst = array_container_create_given_capacity(newCardinality);
5403 if (*dst != NULL) {
5404 ((array_container_t *)*dst)->cardinality = newCardinality;
5405 bitset_extract_intersection_setbits_uint16(
5406 ((const bitset_container_t *)src_1)->array,
5407 ((const bitset_container_t *)src_2)->array,
5408 BITSET_CONTAINER_SIZE_IN_WORDS, ((array_container_t *)*dst)->array,
5409 0);
5410 }
5411 return false; // not a bitset
5412 }
5413
5414 bool bitset_bitset_container_intersection_inplace(
5415 bitset_container_t *src_1, const bitset_container_t *src_2, void **dst) {
5416 const int newCardinality = bitset_container_and_justcard(src_1, src_2);
5417 if (newCardinality > DEFAULT_MAX_SIZE) {
5418 *dst = src_1;
5419 bitset_container_and_nocard(src_1, src_2, src_1);
5420 ((bitset_container_t *)*dst)->cardinality = newCardinality;
5421 return true; // it is a bitset
5422 }
5423 *dst = array_container_create_given_capacity(newCardinality);
5424 if (*dst != NULL) {
5425 ((array_container_t *)*dst)->cardinality = newCardinality;
5426 bitset_extract_intersection_setbits_uint16(
5427 ((const bitset_container_t *)src_1)->array,
5428 ((const bitset_container_t *)src_2)->array,
5429 BITSET_CONTAINER_SIZE_IN_WORDS, ((array_container_t *)*dst)->array,
5430 0);
5431 }
5432 return false; // not a bitset
5433 }
5434 /* end file src/containers/mixed_intersection.c */
5435 /* begin file src/containers/mixed_negation.c */
5436 /*
5437 * mixed_negation.c
5438 *
5439 */
5440
5441 #include <assert.h>
5442 #include <string.h>
5443
5444
5445 // TODO: make simplified and optimized negation code across
5446 // the full range.
5447
5448 /* Negation across the entire range of the container.
5449 * Compute the negation of src and write the result
5450 * to *dst. The complement of a
5451 * sufficiently sparse set will always be dense and a hence a bitmap
5452 ' * We assume that dst is pre-allocated and a valid bitset container
5453 * There can be no in-place version.
5454 */
5455 void array_container_negation(const array_container_t *src,
5456 bitset_container_t *dst) {
5457 uint64_t card = UINT64_C(1 << 16);
5458 bitset_container_set_all(dst);
5459
5460 dst->cardinality = (int32_t)bitset_clear_list(dst->array, card, src->array,
5461 (uint64_t)src->cardinality);
5462 }
5463
5464 /* Negation across the entire range of the container
5465 * Compute the negation of src and write the result
5466 * to *dst. A true return value indicates a bitset result,
5467 * otherwise the result is an array container.
5468 * We assume that dst is not pre-allocated. In
5469 * case of failure, *dst will be NULL.
5470 */
5471 bool bitset_container_negation(const bitset_container_t *src, void **dst) {
5472 return bitset_container_negation_range(src, 0, (1 << 16), dst);
5473 }
5474
5475 /* inplace version */
5476 /*
5477 * Same as bitset_container_negation except that if the output is to
5478 * be a
5479 * bitset_container_t, then src is modified and no allocation is made.
5480 * If the output is to be an array_container_t, then caller is responsible
5481 * to free the container.
5482 * In all cases, the result is in *dst.
5483 */
5484 bool bitset_container_negation_inplace(bitset_container_t *src, void **dst) {
5485 return bitset_container_negation_range_inplace(src, 0, (1 << 16), dst);
5486 }
5487
5488 /* Negation across the entire range of container
5489 * Compute the negation of src and write the result
5490 * to *dst. Return values are the *_TYPECODES as defined * in containers.h
5491 * We assume that dst is not pre-allocated. In
5492 * case of failure, *dst will be NULL.
5493 */
5494 int run_container_negation(const run_container_t *src, void **dst) {
5495 return run_container_negation_range(src, 0, (1 << 16), dst);
5496 }
5497
5498 /*
5499 * Same as run_container_negation except that if the output is to
5500 * be a
5501 * run_container_t, and has the capacity to hold the result,
5502 * then src is modified and no allocation is made.
5503 * In all cases, the result is in *dst.
5504 */
5505 int run_container_negation_inplace(run_container_t *src, void **dst) {
5506 return run_container_negation_range_inplace(src, 0, (1 << 16), dst);
5507 }
5508
5509 /* Negation across a range of the container.
5510 * Compute the negation of src and write the result
5511 * to *dst. Returns true if the result is a bitset container
5512 * and false for an array container. *dst is not preallocated.
5513 */
5514 bool array_container_negation_range(const array_container_t *src,
5515 const int range_start, const int range_end,
5516 void **dst) {
5517 /* close port of the Java implementation */
5518 if (range_start >= range_end) {
5519 *dst = array_container_clone(src);
5520 return false;
5521 }
5522
5523 int32_t start_index =
5524 binarySearch(src->array, src->cardinality, (uint16_t)range_start);
5525 if (start_index < 0) start_index = -start_index - 1;
5526
5527 int32_t last_index =
5528 binarySearch(src->array, src->cardinality, (uint16_t)(range_end - 1));
5529 if (last_index < 0) last_index = -last_index - 2;
5530
5531 const int32_t current_values_in_range = last_index - start_index + 1;
5532 const int32_t span_to_be_flipped = range_end - range_start;
5533 const int32_t new_values_in_range =
5534 span_to_be_flipped - current_values_in_range;
5535 const int32_t cardinality_change =
5536 new_values_in_range - current_values_in_range;
5537 const int32_t new_cardinality = src->cardinality + cardinality_change;
5538
5539 if (new_cardinality > DEFAULT_MAX_SIZE) {
5540 bitset_container_t *temp = bitset_container_from_array(src);
5541 bitset_flip_range(temp->array, (uint32_t)range_start,
5542 (uint32_t)range_end);
5543 temp->cardinality = new_cardinality;
5544 *dst = temp;
5545 return true;
5546 }
5547
5548 array_container_t *arr =
5549 array_container_create_given_capacity(new_cardinality);
5550 *dst = (void *)arr;
5551 if(new_cardinality == 0) {
5552 arr->cardinality = new_cardinality;
5553 return false; // we are done.
5554 }
5555 // copy stuff before the active area
5556 memcpy(arr->array, src->array, start_index * sizeof(uint16_t));
5557
5558 // work on the range
5559 int32_t out_pos = start_index, in_pos = start_index;
5560 int32_t val_in_range = range_start;
5561 for (; val_in_range < range_end && in_pos <= last_index; ++val_in_range) {
5562 if ((uint16_t)val_in_range != src->array[in_pos]) {
5563 arr->array[out_pos++] = (uint16_t)val_in_range;
5564 } else {
5565 ++in_pos;
5566 }
5567 }
5568 for (; val_in_range < range_end; ++val_in_range)
5569 arr->array[out_pos++] = (uint16_t)val_in_range;
5570
5571 // content after the active range
5572 memcpy(arr->array + out_pos, src->array + (last_index + 1),
5573 (src->cardinality - (last_index + 1)) * sizeof(uint16_t));
5574 arr->cardinality = new_cardinality;
5575 return false;
5576 }
5577
5578 /* Even when the result would fit, it is unclear how to make an
5579 * inplace version without inefficient copying.
5580 */
5581
5582 bool array_container_negation_range_inplace(array_container_t *src,
5583 const int range_start,
5584 const int range_end, void **dst) {
5585 bool ans = array_container_negation_range(src, range_start, range_end, dst);
5586 // TODO : try a real inplace version
5587 array_container_free(src);
5588 return ans;
5589 }
5590
5591 /* Negation across a range of the container
5592 * Compute the negation of src and write the result
5593 * to *dst. A true return value indicates a bitset result,
5594 * otherwise the result is an array container.
5595 * We assume that dst is not pre-allocated. In
5596 * case of failure, *dst will be NULL.
5597 */
5598 bool bitset_container_negation_range(const bitset_container_t *src,
5599 const int range_start, const int range_end,
5600 void **dst) {
5601 // TODO maybe consider density-based estimate
5602 // and sometimes build result directly as array, with
5603 // conversion back to bitset if wrong. Or determine
5604 // actual result cardinality, then go directly for the known final cont.
5605
5606 // keep computation using bitsets as long as possible.
5607 bitset_container_t *t = bitset_container_clone(src);
5608 bitset_flip_range(t->array, (uint32_t)range_start, (uint32_t)range_end);
5609 t->cardinality = bitset_container_compute_cardinality(t);
5610
5611 if (t->cardinality > DEFAULT_MAX_SIZE) {
5612 *dst = t;
5613 return true;
5614 } else {
5615 *dst = array_container_from_bitset(t);
5616 bitset_container_free(t);
5617 return false;
5618 }
5619 }
5620
5621 /* inplace version */
5622 /*
5623 * Same as bitset_container_negation except that if the output is to
5624 * be a
5625 * bitset_container_t, then src is modified and no allocation is made.
5626 * If the output is to be an array_container_t, then caller is responsible
5627 * to free the container.
5628 * In all cases, the result is in *dst.
5629 */
5630 bool bitset_container_negation_range_inplace(bitset_container_t *src,
5631 const int range_start,
5632 const int range_end, void **dst) {
5633 bitset_flip_range(src->array, (uint32_t)range_start, (uint32_t)range_end);
5634 src->cardinality = bitset_container_compute_cardinality(src);
5635 if (src->cardinality > DEFAULT_MAX_SIZE) {
5636 *dst = src;
5637 return true;
5638 }
5639 *dst = array_container_from_bitset(src);
5640 bitset_container_free(src);
5641 return false;
5642 }
5643
5644 /* Negation across a range of container
5645 * Compute the negation of src and write the result
5646 * to *dst. Return values are the *_TYPECODES as defined * in containers.h
5647 * We assume that dst is not pre-allocated. In
5648 * case of failure, *dst will be NULL.
5649 */
5650 int run_container_negation_range(const run_container_t *src,
5651 const int range_start, const int range_end,
5652 void **dst) {
5653 uint8_t return_typecode;
5654
5655 // follows the Java implementation
5656 if (range_end <= range_start) {
5657 *dst = run_container_clone(src);
5658 return RUN_CONTAINER_TYPE_CODE;
5659 }
5660
5661 run_container_t *ans = run_container_create_given_capacity(
5662 src->n_runs + 1); // src->n_runs + 1);
5663 int k = 0;
5664 for (; k < src->n_runs && src->runs[k].value < range_start; ++k) {
5665 ans->runs[k] = src->runs[k];
5666 ans->n_runs++;
5667 }
5668
5669 run_container_smart_append_exclusive(
5670 ans, (uint16_t)range_start, (uint16_t)(range_end - range_start - 1));
5671
5672 for (; k < src->n_runs; ++k) {
5673 run_container_smart_append_exclusive(ans, src->runs[k].value,
5674 src->runs[k].length);
5675 }
5676
5677 *dst = convert_run_to_efficient_container(ans, &return_typecode);
5678 if (return_typecode != RUN_CONTAINER_TYPE_CODE) run_container_free(ans);
5679
5680 return return_typecode;
5681 }
5682
5683 /*
5684 * Same as run_container_negation except that if the output is to
5685 * be a
5686 * run_container_t, and has the capacity to hold the result,
5687 * then src is modified and no allocation is made.
5688 * In all cases, the result is in *dst.
5689 */
5690 int run_container_negation_range_inplace(run_container_t *src,
5691 const int range_start,
5692 const int range_end, void **dst) {
5693 uint8_t return_typecode;
5694
5695 if (range_end <= range_start) {
5696 *dst = src;
5697 return RUN_CONTAINER_TYPE_CODE;
5698 }
5699
5700 // TODO: efficient special case when range is 0 to 65535 inclusive
5701
5702 if (src->capacity == src->n_runs) {
5703 // no excess room. More checking to see if result can fit
5704 bool last_val_before_range = false;
5705 bool first_val_in_range = false;
5706 bool last_val_in_range = false;
5707 bool first_val_past_range = false;
5708
5709 if (range_start > 0)
5710 last_val_before_range =
5711 run_container_contains(src, (uint16_t)(range_start - 1));
5712 first_val_in_range = run_container_contains(src, (uint16_t)range_start);
5713
5714 if (last_val_before_range == first_val_in_range) {
5715 last_val_in_range =
5716 run_container_contains(src, (uint16_t)(range_end - 1));
5717 if (range_end != 0x10000)
5718 first_val_past_range =
5719 run_container_contains(src, (uint16_t)range_end);
5720
5721 if (last_val_in_range ==
5722 first_val_past_range) { // no space for inplace
5723 int ans = run_container_negation_range(src, range_start,
5724 range_end, dst);
5725 run_container_free(src);
5726 return ans;
5727 }
5728 }
5729 }
5730 // all other cases: result will fit
5731
5732 run_container_t *ans = src;
5733 int my_nbr_runs = src->n_runs;
5734
5735 ans->n_runs = 0;
5736 int k = 0;
5737 for (; (k < my_nbr_runs) && (src->runs[k].value < range_start); ++k) {
5738 // ans->runs[k] = src->runs[k]; (would be self-copy)
5739 ans->n_runs++;
5740 }
5741
5742 // as with Java implementation, use locals to give self a buffer of depth 1
5743 rle16_t buffered = (rle16_t){.value = (uint16_t)0, .length = (uint16_t)0};
5744 rle16_t next = buffered;
5745 if (k < my_nbr_runs) buffered = src->runs[k];
5746
5747 run_container_smart_append_exclusive(
5748 ans, (uint16_t)range_start, (uint16_t)(range_end - range_start - 1));
5749
5750 for (; k < my_nbr_runs; ++k) {
5751 if (k + 1 < my_nbr_runs) next = src->runs[k + 1];
5752
5753 run_container_smart_append_exclusive(ans, buffered.value,
5754 buffered.length);
5755 buffered = next;
5756 }
5757
5758 *dst = convert_run_to_efficient_container(ans, &return_typecode);
5759 if (return_typecode != RUN_CONTAINER_TYPE_CODE) run_container_free(ans);
5760
5761 return return_typecode;
5762 }
5763 /* end file src/containers/mixed_negation.c */
5764 /* begin file src/containers/mixed_subset.c */
5765
5766 bool array_container_is_subset_bitset(const array_container_t* container1,
5767 const bitset_container_t* container2) {
5768 if (container2->cardinality != BITSET_UNKNOWN_CARDINALITY) {
5769 if (container2->cardinality < container1->cardinality) {
5770 return false;
5771 }
5772 }
5773 for (int i = 0; i < container1->cardinality; ++i) {
5774 if (!bitset_container_contains(container2, container1->array[i])) {
5775 return false;
5776 }
5777 }
5778 return true;
5779 }
5780
5781 bool run_container_is_subset_array(const run_container_t* container1,
5782 const array_container_t* container2) {
5783 if (run_container_cardinality(container1) > container2->cardinality)
5784 return false;
5785 int32_t start_pos = -1, stop_pos = -1;
5786 for (int i = 0; i < container1->n_runs; ++i) {
5787 int32_t start = container1->runs[i].value;
5788 int32_t stop = start + container1->runs[i].length;
5789 start_pos = advanceUntil(container2->array, stop_pos,
5790 container2->cardinality, start);
5791 stop_pos = advanceUntil(container2->array, stop_pos,
5792 container2->cardinality, stop);
5793 if (start_pos == container2->cardinality) {
5794 return false;
5795 } else if (stop_pos - start_pos != stop - start ||
5796 container2->array[start_pos] != start ||
5797 container2->array[stop_pos] != stop) {
5798 return false;
5799 }
5800 }
5801 return true;
5802 }
5803
5804 bool array_container_is_subset_run(const array_container_t* container1,
5805 const run_container_t* container2) {
5806 if (container1->cardinality > run_container_cardinality(container2))
5807 return false;
5808 int i_array = 0, i_run = 0;
5809 while (i_array < container1->cardinality && i_run < container2->n_runs) {
5810 uint32_t start = container2->runs[i_run].value;
5811 uint32_t stop = start + container2->runs[i_run].length;
5812 if (container1->array[i_array] < start) {
5813 return false;
5814 } else if (container1->array[i_array] > stop) {
5815 i_run++;
5816 } else { // the value of the array is in the run
5817 i_array++;
5818 }
5819 }
5820 if (i_array == container1->cardinality) {
5821 return true;
5822 } else {
5823 return false;
5824 }
5825 }
5826
5827 bool run_container_is_subset_bitset(const run_container_t* container1,
5828 const bitset_container_t* container2) {
5829 // todo: this code could be much faster
5830 if (container2->cardinality != BITSET_UNKNOWN_CARDINALITY) {
5831 if (container2->cardinality < run_container_cardinality(container1)) {
5832 return false;
5833 }
5834 } else {
5835 int32_t card = bitset_container_compute_cardinality(
5836 container2); // modify container2?
5837 if (card < run_container_cardinality(container1)) {
5838 return false;
5839 }
5840 }
5841 for (int i = 0; i < container1->n_runs; ++i) {
5842 uint32_t run_start = container1->runs[i].value;
5843 uint32_t le = container1->runs[i].length;
5844 for (uint32_t j = run_start; j <= run_start + le; ++j) {
5845 if (!bitset_container_contains(container2, j)) {
5846 return false;
5847 }
5848 }
5849 }
5850 return true;
5851 }
5852
5853 bool bitset_container_is_subset_run(const bitset_container_t* container1,
5854 const run_container_t* container2) {
5855 // todo: this code could be much faster
5856 if (container1->cardinality != BITSET_UNKNOWN_CARDINALITY) {
5857 if (container1->cardinality > run_container_cardinality(container2)) {
5858 return false;
5859 }
5860 }
5861 int32_t i_bitset = 0, i_run = 0;
5862 while (i_bitset < BITSET_CONTAINER_SIZE_IN_WORDS &&
5863 i_run < container2->n_runs) {
5864 uint64_t w = container1->array[i_bitset];
5865 while (w != 0 && i_run < container2->n_runs) {
5866 uint32_t start = container2->runs[i_run].value;
5867 uint32_t stop = start + container2->runs[i_run].length;
5868 uint64_t t = w & (~w + 1);
5869 uint16_t r = i_bitset * 64 + __builtin_ctzll(w);
5870 if (r < start) {
5871 return false;
5872 } else if (r > stop) {
5873 i_run++;
5874 continue;
5875 } else {
5876 w ^= t;
5877 }
5878 }
5879 if (w == 0) {
5880 i_bitset++;
5881 } else {
5882 return false;
5883 }
5884 }
5885 if (i_bitset < BITSET_CONTAINER_SIZE_IN_WORDS) {
5886 // terminated iterating on the run containers, check that rest of bitset
5887 // is empty
5888 for (; i_bitset < BITSET_CONTAINER_SIZE_IN_WORDS; i_bitset++) {
5889 if (container1->array[i_bitset] != 0) {
5890 return false;
5891 }
5892 }
5893 }
5894 return true;
5895 }
5896 /* end file src/containers/mixed_subset.c */
5897 /* begin file src/containers/mixed_union.c */
5898 /*
5899 * mixed_union.c
5900 *
5901 */
5902
5903 #include <assert.h>
5904 #include <string.h>
5905
5906
5907 /* Compute the union of src_1 and src_2 and write the result to
5908 * dst. */
5909 void array_bitset_container_union(const array_container_t *src_1,
5910 const bitset_container_t *src_2,
5911 bitset_container_t *dst) {
5912 if (src_2 != dst) bitset_container_copy(src_2, dst);
5913 dst->cardinality = (int32_t)bitset_set_list_withcard(
5914 dst->array, dst->cardinality, src_1->array, src_1->cardinality);
5915 }
5916
5917 /* Compute the union of src_1 and src_2 and write the result to
5918 * dst. It is allowed for src_2 to be dst. This version does not
5919 * update the cardinality of dst (it is set to BITSET_UNKNOWN_CARDINALITY). */
5920 void array_bitset_container_lazy_union(const array_container_t *src_1,
5921 const bitset_container_t *src_2,
5922 bitset_container_t *dst) {
5923 if (src_2 != dst) bitset_container_copy(src_2, dst);
5924 bitset_set_list(dst->array, src_1->array, src_1->cardinality);
5925 dst->cardinality = BITSET_UNKNOWN_CARDINALITY;
5926 }
5927
5928 void run_bitset_container_union(const run_container_t *src_1,
5929 const bitset_container_t *src_2,
5930 bitset_container_t *dst) {
5931 assert(!run_container_is_full(src_1)); // catch this case upstream
5932 if (src_2 != dst) bitset_container_copy(src_2, dst);
5933 for (int32_t rlepos = 0; rlepos < src_1->n_runs; ++rlepos) {
5934 rle16_t rle = src_1->runs[rlepos];
5935 bitset_set_lenrange(dst->array, rle.value, rle.length);
5936 }
5937 dst->cardinality = bitset_container_compute_cardinality(dst);
5938 }
5939
5940 void run_bitset_container_lazy_union(const run_container_t *src_1,
5941 const bitset_container_t *src_2,
5942 bitset_container_t *dst) {
5943 assert(!run_container_is_full(src_1)); // catch this case upstream
5944 if (src_2 != dst) bitset_container_copy(src_2, dst);
5945 for (int32_t rlepos = 0; rlepos < src_1->n_runs; ++rlepos) {
5946 rle16_t rle = src_1->runs[rlepos];
5947 bitset_set_lenrange(dst->array, rle.value, rle.length);
5948 }
5949 dst->cardinality = BITSET_UNKNOWN_CARDINALITY;
5950 }
5951
5952 // why do we leave the result as a run container??
5953 void array_run_container_union(const array_container_t *src_1,
5954 const run_container_t *src_2,
5955 run_container_t *dst) {
5956 if (run_container_is_full(src_2)) {
5957 run_container_copy(src_2, dst);
5958 return;
5959 }
5960 // TODO: see whether the "2*" is spurious
5961 run_container_grow(dst, 2 * (src_1->cardinality + src_2->n_runs), false);
5962 int32_t rlepos = 0;
5963 int32_t arraypos = 0;
5964 rle16_t previousrle;
5965 if (src_2->runs[rlepos].value <= src_1->array[arraypos]) {
5966 previousrle = run_container_append_first(dst, src_2->runs[rlepos]);
5967 rlepos++;
5968 } else {
5969 previousrle =
5970 run_container_append_value_first(dst, src_1->array[arraypos]);
5971 arraypos++;
5972 }
5973 while ((rlepos < src_2->n_runs) && (arraypos < src_1->cardinality)) {
5974 if (src_2->runs[rlepos].value <= src_1->array[arraypos]) {
5975 run_container_append(dst, src_2->runs[rlepos], &previousrle);
5976 rlepos++;
5977 } else {
5978 run_container_append_value(dst, src_1->array[arraypos],
5979 &previousrle);
5980 arraypos++;
5981 }
5982 }
5983 if (arraypos < src_1->cardinality) {
5984 while (arraypos < src_1->cardinality) {
5985 run_container_append_value(dst, src_1->array[arraypos],
5986 &previousrle);
5987 arraypos++;
5988 }
5989 } else {
5990 while (rlepos < src_2->n_runs) {
5991 run_container_append(dst, src_2->runs[rlepos], &previousrle);
5992 rlepos++;
5993 }
5994 }
5995 }
5996
5997 void array_run_container_inplace_union(const array_container_t *src_1,
5998 run_container_t *src_2) {
5999 if (run_container_is_full(src_2)) {
6000 return;
6001 }
6002 const int32_t maxoutput = src_1->cardinality + src_2->n_runs;
6003 const int32_t neededcapacity = maxoutput + src_2->n_runs;
6004 if (src_2->capacity < neededcapacity)
6005 run_container_grow(src_2, neededcapacity, true);
6006 memmove(src_2->runs + maxoutput, src_2->runs,
6007 src_2->n_runs * sizeof(rle16_t));
6008 rle16_t *inputsrc2 = src_2->runs + maxoutput;
6009 int32_t rlepos = 0;
6010 int32_t arraypos = 0;
6011 int src2nruns = src_2->n_runs;
6012 src_2->n_runs = 0;
6013
6014 rle16_t previousrle;
6015
6016 if (inputsrc2[rlepos].value <= src_1->array[arraypos]) {
6017 previousrle = run_container_append_first(src_2, inputsrc2[rlepos]);
6018 rlepos++;
6019 } else {
6020 previousrle =
6021 run_container_append_value_first(src_2, src_1->array[arraypos]);
6022 arraypos++;
6023 }
6024
6025 while ((rlepos < src2nruns) && (arraypos < src_1->cardinality)) {
6026 if (inputsrc2[rlepos].value <= src_1->array[arraypos]) {
6027 run_container_append(src_2, inputsrc2[rlepos], &previousrle);
6028 rlepos++;
6029 } else {
6030 run_container_append_value(src_2, src_1->array[arraypos],
6031 &previousrle);
6032 arraypos++;
6033 }
6034 }
6035 if (arraypos < src_1->cardinality) {
6036 while (arraypos < src_1->cardinality) {
6037 run_container_append_value(src_2, src_1->array[arraypos],
6038 &previousrle);
6039 arraypos++;
6040 }
6041 } else {
6042 while (rlepos < src2nruns) {
6043 run_container_append(src_2, inputsrc2[rlepos], &previousrle);
6044 rlepos++;
6045 }
6046 }
6047 }
6048
6049 bool array_array_container_union(const array_container_t *src_1,
6050 const array_container_t *src_2, void **dst) {
6051 int totalCardinality = src_1->cardinality + src_2->cardinality;
6052 if (totalCardinality <= DEFAULT_MAX_SIZE) {
6053 *dst = array_container_create_given_capacity(totalCardinality);
6054 if (*dst != NULL) {
6055 array_container_union(src_1, src_2, (array_container_t *)*dst);
6056 } else {
6057 return true; // otherwise failure won't be caught
6058 }
6059 return false; // not a bitset
6060 }
6061 *dst = bitset_container_create();
6062 bool returnval = true; // expect a bitset
6063 if (*dst != NULL) {
6064 bitset_container_t *ourbitset = (bitset_container_t *)*dst;
6065 bitset_set_list(ourbitset->array, src_1->array, src_1->cardinality);
6066 ourbitset->cardinality = (int32_t)bitset_set_list_withcard(
6067 ourbitset->array, src_1->cardinality, src_2->array,
6068 src_2->cardinality);
6069 if (ourbitset->cardinality <= DEFAULT_MAX_SIZE) {
6070 // need to convert!
6071 *dst = array_container_from_bitset(ourbitset);
6072 bitset_container_free(ourbitset);
6073 returnval = false; // not going to be a bitset
6074 }
6075 }
6076 return returnval;
6077 }
6078
6079 bool array_array_container_inplace_union(array_container_t *src_1,
6080 const array_container_t *src_2, void **dst) {
6081 int totalCardinality = src_1->cardinality + src_2->cardinality;
6082 *dst = NULL;
6083 if (totalCardinality <= DEFAULT_MAX_SIZE) {
6084 if(src_1->capacity < totalCardinality) {
6085 *dst = array_container_create_given_capacity(2 * totalCardinality); // be purposefully generous
6086 if (*dst != NULL) {
6087 array_container_union(src_1, src_2, (array_container_t *)*dst);
6088 } else {
6089 return true; // otherwise failure won't be caught
6090 }
6091 return false; // not a bitset
6092 } else {
6093 memmove(src_1->array + src_2->cardinality, src_1->array, src_1->cardinality * sizeof(uint16_t));
6094 src_1->cardinality = (int32_t)fast_union_uint16(src_1->array + src_2->cardinality, src_1->cardinality,
6095 src_2->array, src_2->cardinality, src_1->array);
6096 return false; // not a bitset
6097 }
6098 }
6099 *dst = bitset_container_create();
6100 bool returnval = true; // expect a bitset
6101 if (*dst != NULL) {
6102 bitset_container_t *ourbitset = (bitset_container_t *)*dst;
6103 bitset_set_list(ourbitset->array, src_1->array, src_1->cardinality);
6104 ourbitset->cardinality = (int32_t)bitset_set_list_withcard(
6105 ourbitset->array, src_1->cardinality, src_2->array,
6106 src_2->cardinality);
6107 if (ourbitset->cardinality <= DEFAULT_MAX_SIZE) {
6108 // need to convert!
6109 if(src_1->capacity < ourbitset->cardinality) {
6110 array_container_grow(src_1, ourbitset->cardinality, INT32_MAX, false);
6111 }
6112
6113 bitset_extract_setbits_uint16(ourbitset->array, BITSET_CONTAINER_SIZE_IN_WORDS,
6114 src_1->array, 0);
6115 src_1->cardinality = ourbitset->cardinality;
6116 *dst = src_1;
6117 bitset_container_free(ourbitset);
6118 returnval = false; // not going to be a bitset
6119 }
6120 }
6121 return returnval;
6122 }
6123
6124
6125 bool array_array_container_lazy_union(const array_container_t *src_1,
6126 const array_container_t *src_2,
6127 void **dst) {
6128 int totalCardinality = src_1->cardinality + src_2->cardinality;
6129 if (totalCardinality <= ARRAY_LAZY_LOWERBOUND) {
6130 *dst = array_container_create_given_capacity(totalCardinality);
6131 if (*dst != NULL) {
6132 array_container_union(src_1, src_2, (array_container_t *)*dst);
6133 } else {
6134 return true; // otherwise failure won't be caught
6135 }
6136 return false; // not a bitset
6137 }
6138 *dst = bitset_container_create();
6139 bool returnval = true; // expect a bitset
6140 if (*dst != NULL) {
6141 bitset_container_t *ourbitset = (bitset_container_t *)*dst;
6142 bitset_set_list(ourbitset->array, src_1->array, src_1->cardinality);
6143 bitset_set_list(ourbitset->array, src_2->array, src_2->cardinality);
6144 ourbitset->cardinality = BITSET_UNKNOWN_CARDINALITY;
6145 }
6146 return returnval;
6147 }
6148
6149
6150 bool array_array_container_lazy_inplace_union(array_container_t *src_1,
6151 const array_container_t *src_2,
6152 void **dst) {
6153 int totalCardinality = src_1->cardinality + src_2->cardinality;
6154 *dst = NULL;
6155 if (totalCardinality <= ARRAY_LAZY_LOWERBOUND) {
6156 if(src_1->capacity < totalCardinality) {
6157 *dst = array_container_create_given_capacity(2 * totalCardinality); // be purposefully generous
6158 if (*dst != NULL) {
6159 array_container_union(src_1, src_2, (array_container_t *)*dst);
6160 } else {
6161 return true; // otherwise failure won't be caught
6162 }
6163 return false; // not a bitset
6164 } else {
6165 memmove(src_1->array + src_2->cardinality, src_1->array, src_1->cardinality * sizeof(uint16_t));
6166 src_1->cardinality = (int32_t)fast_union_uint16(src_1->array + src_2->cardinality, src_1->cardinality,
6167 src_2->array, src_2->cardinality, src_1->array);
6168 return false; // not a bitset
6169 }
6170 }
6171 *dst = bitset_container_create();
6172 bool returnval = true; // expect a bitset
6173 if (*dst != NULL) {
6174 bitset_container_t *ourbitset = (bitset_container_t *)*dst;
6175 bitset_set_list(ourbitset->array, src_1->array, src_1->cardinality);
6176 bitset_set_list(ourbitset->array, src_2->array, src_2->cardinality);
6177 ourbitset->cardinality = BITSET_UNKNOWN_CARDINALITY;
6178 }
6179 return returnval;
6180 }
6181 /* end file src/containers/mixed_union.c */
6182 /* begin file src/containers/mixed_xor.c */
6183 /*
6184 * mixed_xor.c
6185 */
6186
6187 #include <assert.h>
6188 #include <string.h>
6189
6190
6191 /* Compute the xor of src_1 and src_2 and write the result to
6192 * dst (which has no container initially).
6193 * Result is true iff dst is a bitset */
6194 bool array_bitset_container_xor(const array_container_t *src_1,
6195 const bitset_container_t *src_2, void **dst) {
6196 bitset_container_t *result = bitset_container_create();
6197 bitset_container_copy(src_2, result);
6198 result->cardinality = (int32_t)bitset_flip_list_withcard(
6199 result->array, result->cardinality, src_1->array, src_1->cardinality);
6200
6201 // do required type conversions.
6202 if (result->cardinality <= DEFAULT_MAX_SIZE) {
6203 *dst = array_container_from_bitset(result);
6204 bitset_container_free(result);
6205 return false; // not bitset
6206 }
6207 *dst = result;
6208 return true; // bitset
6209 }
6210
6211 /* Compute the xor of src_1 and src_2 and write the result to
6212 * dst. It is allowed for src_2 to be dst. This version does not
6213 * update the cardinality of dst (it is set to BITSET_UNKNOWN_CARDINALITY).
6214 */
6215
6216 void array_bitset_container_lazy_xor(const array_container_t *src_1,
6217 const bitset_container_t *src_2,
6218 bitset_container_t *dst) {
6219 if (src_2 != dst) bitset_container_copy(src_2, dst);
6220 bitset_flip_list(dst->array, src_1->array, src_1->cardinality);
6221 dst->cardinality = BITSET_UNKNOWN_CARDINALITY;
6222 }
6223
6224 /* Compute the xor of src_1 and src_2 and write the result to
6225 * dst. Result may be either a bitset or an array container
6226 * (returns "result is bitset"). dst does not initially have
6227 * any container, but becomes either a bitset container (return
6228 * result true) or an array container.
6229 */
6230
6231 bool run_bitset_container_xor(const run_container_t *src_1,
6232 const bitset_container_t *src_2, void **dst) {
6233 bitset_container_t *result = bitset_container_create();
6234
6235 bitset_container_copy(src_2, result);
6236 for (int32_t rlepos = 0; rlepos < src_1->n_runs; ++rlepos) {
6237 rle16_t rle = src_1->runs[rlepos];
6238 bitset_flip_range(result->array, rle.value,
6239 rle.value + rle.length + UINT32_C(1));
6240 }
6241 result->cardinality = bitset_container_compute_cardinality(result);
6242
6243 if (result->cardinality <= DEFAULT_MAX_SIZE) {
6244 *dst = array_container_from_bitset(result);
6245 bitset_container_free(result);
6246 return false; // not bitset
6247 }
6248 *dst = result;
6249 return true; // bitset
6250 }
6251
6252 /* lazy xor. Dst is initialized and may be equal to src_2.
6253 * Result is left as a bitset container, even if actual
6254 * cardinality would dictate an array container.
6255 */
6256
6257 void run_bitset_container_lazy_xor(const run_container_t *src_1,
6258 const bitset_container_t *src_2,
6259 bitset_container_t *dst) {
6260 if (src_2 != dst) bitset_container_copy(src_2, dst);
6261 for (int32_t rlepos = 0; rlepos < src_1->n_runs; ++rlepos) {
6262 rle16_t rle = src_1->runs[rlepos];
6263 bitset_flip_range(dst->array, rle.value,
6264 rle.value + rle.length + UINT32_C(1));
6265 }
6266 dst->cardinality = BITSET_UNKNOWN_CARDINALITY;
6267 }
6268
6269 /* dst does not indicate a valid container initially. Eventually it
6270 * can become any kind of container.
6271 */
6272
6273 int array_run_container_xor(const array_container_t *src_1,
6274 const run_container_t *src_2, void **dst) {
6275 // semi following Java XOR implementation as of May 2016
6276 // the C OR implementation works quite differently and can return a run
6277 // container
6278 // TODO could optimize for full run containers.
6279
6280 // use of lazy following Java impl.
6281 const int arbitrary_threshold = 32;
6282 if (src_1->cardinality < arbitrary_threshold) {
6283 run_container_t *ans = run_container_create();
6284 array_run_container_lazy_xor(src_1, src_2, ans); // keeps runs.
6285 uint8_t typecode_after;
6286 *dst =
6287 convert_run_to_efficient_container_and_free(ans, &typecode_after);
6288 return typecode_after;
6289 }
6290
6291 int card = run_container_cardinality(src_2);
6292 if (card <= DEFAULT_MAX_SIZE) {
6293 // Java implementation works with the array, xoring the run elements via
6294 // iterator
6295 array_container_t *temp = array_container_from_run(src_2);
6296 bool ret_is_bitset = array_array_container_xor(temp, src_1, dst);
6297 array_container_free(temp);
6298 return ret_is_bitset ? BITSET_CONTAINER_TYPE_CODE
6299 : ARRAY_CONTAINER_TYPE_CODE;
6300
6301 } else { // guess that it will end up as a bitset
6302 bitset_container_t *result = bitset_container_from_run(src_2);
6303 bool is_bitset = bitset_array_container_ixor(result, src_1, dst);
6304 // any necessary type conversion has been done by the ixor
6305 int retval = (is_bitset ? BITSET_CONTAINER_TYPE_CODE
6306 : ARRAY_CONTAINER_TYPE_CODE);
6307 return retval;
6308 }
6309 }
6310
6311 /* Dst is a valid run container. (Can it be src_2? Let's say not.)
6312 * Leaves result as run container, even if other options are
6313 * smaller.
6314 */
6315
6316 void array_run_container_lazy_xor(const array_container_t *src_1,
6317 const run_container_t *src_2,
6318 run_container_t *dst) {
6319 run_container_grow(dst, src_1->cardinality + src_2->n_runs, false);
6320 int32_t rlepos = 0;
6321 int32_t arraypos = 0;
6322 dst->n_runs = 0;
6323
6324 while ((rlepos < src_2->n_runs) && (arraypos < src_1->cardinality)) {
6325 if (src_2->runs[rlepos].value <= src_1->array[arraypos]) {
6326 run_container_smart_append_exclusive(dst, src_2->runs[rlepos].value,
6327 src_2->runs[rlepos].length);
6328 rlepos++;
6329 } else {
6330 run_container_smart_append_exclusive(dst, src_1->array[arraypos],
6331 0);
6332 arraypos++;
6333 }
6334 }
6335 while (arraypos < src_1->cardinality) {
6336 run_container_smart_append_exclusive(dst, src_1->array[arraypos], 0);
6337 arraypos++;
6338 }
6339 while (rlepos < src_2->n_runs) {
6340 run_container_smart_append_exclusive(dst, src_2->runs[rlepos].value,
6341 src_2->runs[rlepos].length);
6342 rlepos++;
6343 }
6344 }
6345
6346 /* dst does not indicate a valid container initially. Eventually it
6347 * can become any kind of container.
6348 */
6349
6350 int run_run_container_xor(const run_container_t *src_1,
6351 const run_container_t *src_2, void **dst) {
6352 run_container_t *ans = run_container_create();
6353 run_container_xor(src_1, src_2, ans);
6354 uint8_t typecode_after;
6355 *dst = convert_run_to_efficient_container_and_free(ans, &typecode_after);
6356 return typecode_after;
6357 }
6358
6359 /*
6360 * Java implementation (as of May 2016) for array_run, run_run
6361 * and bitset_run don't do anything different for inplace.
6362 * Could adopt the mixed_union.c approach instead (ie, using
6363 * smart_append_exclusive)
6364 *
6365 */
6366
6367 bool array_array_container_xor(const array_container_t *src_1,
6368 const array_container_t *src_2, void **dst) {
6369 int totalCardinality =
6370 src_1->cardinality + src_2->cardinality; // upper bound
6371 if (totalCardinality <= DEFAULT_MAX_SIZE) {
6372 *dst = array_container_create_given_capacity(totalCardinality);
6373 array_container_xor(src_1, src_2, (array_container_t *)*dst);
6374 return false; // not a bitset
6375 }
6376 *dst = bitset_container_from_array(src_1);
6377 bool returnval = true; // expect a bitset
6378 bitset_container_t *ourbitset = (bitset_container_t *)*dst;
6379 ourbitset->cardinality = (uint32_t)bitset_flip_list_withcard(
6380 ourbitset->array, src_1->cardinality, src_2->array, src_2->cardinality);
6381 if (ourbitset->cardinality <= DEFAULT_MAX_SIZE) {
6382 // need to convert!
6383 *dst = array_container_from_bitset(ourbitset);
6384 bitset_container_free(ourbitset);
6385 returnval = false; // not going to be a bitset
6386 }
6387
6388 return returnval;
6389 }
6390
6391 bool array_array_container_lazy_xor(const array_container_t *src_1,
6392 const array_container_t *src_2,
6393 void **dst) {
6394 int totalCardinality = src_1->cardinality + src_2->cardinality;
6395 // upper bound, but probably poor estimate for xor
6396 if (totalCardinality <= ARRAY_LAZY_LOWERBOUND) {
6397 *dst = array_container_create_given_capacity(totalCardinality);
6398 if (*dst != NULL)
6399 array_container_xor(src_1, src_2, (array_container_t *)*dst);
6400 return false; // not a bitset
6401 }
6402 *dst = bitset_container_from_array(src_1);
6403 bool returnval = true; // expect a bitset (maybe, for XOR??)
6404 if (*dst != NULL) {
6405 bitset_container_t *ourbitset = (bitset_container_t *)*dst;
6406 bitset_flip_list(ourbitset->array, src_2->array, src_2->cardinality);
6407 ourbitset->cardinality = BITSET_UNKNOWN_CARDINALITY;
6408 }
6409 return returnval;
6410 }
6411
6412 /* Compute the xor of src_1 and src_2 and write the result to
6413 * dst (which has no container initially). Return value is
6414 * "dst is a bitset"
6415 */
6416
6417 bool bitset_bitset_container_xor(const bitset_container_t *src_1,
6418 const bitset_container_t *src_2, void **dst) {
6419 bitset_container_t *ans = bitset_container_create();
6420 int card = bitset_container_xor(src_1, src_2, ans);
6421 if (card <= DEFAULT_MAX_SIZE) {
6422 *dst = array_container_from_bitset(ans);
6423 bitset_container_free(ans);
6424 return false; // not bitset
6425 } else {
6426 *dst = ans;
6427 return true;
6428 }
6429 }
6430
6431 /* Compute the xor of src_1 and src_2 and write the result to
6432 * dst (which has no container initially). It will modify src_1
6433 * to be dst if the result is a bitset. Otherwise, it will
6434 * free src_1 and dst will be a new array container. In both
6435 * cases, the caller is responsible for deallocating dst.
6436 * Returns true iff dst is a bitset */
6437
6438 bool bitset_array_container_ixor(bitset_container_t *src_1,
6439 const array_container_t *src_2, void **dst) {
6440 *dst = src_1;
6441 src_1->cardinality = (uint32_t)bitset_flip_list_withcard(
6442 src_1->array, src_1->cardinality, src_2->array, src_2->cardinality);
6443
6444 if (src_1->cardinality <= DEFAULT_MAX_SIZE) {
6445 *dst = array_container_from_bitset(src_1);
6446 bitset_container_free(src_1);
6447 return false; // not bitset
6448 } else
6449 return true;
6450 }
6451
6452 /* a bunch of in-place, some of which may not *really* be inplace.
6453 * TODO: write actual inplace routine if efficiency warrants it
6454 * Anything inplace with a bitset is a good candidate
6455 */
6456
6457 bool bitset_bitset_container_ixor(bitset_container_t *src_1,
6458 const bitset_container_t *src_2, void **dst) {
6459 bool ans = bitset_bitset_container_xor(src_1, src_2, dst);
6460 bitset_container_free(src_1);
6461 return ans;
6462 }
6463
6464 bool array_bitset_container_ixor(array_container_t *src_1,
6465 const bitset_container_t *src_2, void **dst) {
6466 bool ans = array_bitset_container_xor(src_1, src_2, dst);
6467 array_container_free(src_1);
6468 return ans;
6469 }
6470
6471 /* Compute the xor of src_1 and src_2 and write the result to
6472 * dst. Result may be either a bitset or an array container
6473 * (returns "result is bitset"). dst does not initially have
6474 * any container, but becomes either a bitset container (return
6475 * result true) or an array container.
6476 */
6477
6478 bool run_bitset_container_ixor(run_container_t *src_1,
6479 const bitset_container_t *src_2, void **dst) {
6480 bool ans = run_bitset_container_xor(src_1, src_2, dst);
6481 run_container_free(src_1);
6482 return ans;
6483 }
6484
6485 bool bitset_run_container_ixor(bitset_container_t *src_1,
6486 const run_container_t *src_2, void **dst) {
6487 bool ans = run_bitset_container_xor(src_2, src_1, dst);
6488 bitset_container_free(src_1);
6489 return ans;
6490 }
6491
6492 /* dst does not indicate a valid container initially. Eventually it
6493 * can become any kind of container.
6494 */
6495
6496 int array_run_container_ixor(array_container_t *src_1,
6497 const run_container_t *src_2, void **dst) {
6498 int ans = array_run_container_xor(src_1, src_2, dst);
6499 array_container_free(src_1);
6500 return ans;
6501 }
6502
6503 int run_array_container_ixor(run_container_t *src_1,
6504 const array_container_t *src_2, void **dst) {
6505 int ans = array_run_container_xor(src_2, src_1, dst);
6506 run_container_free(src_1);
6507 return ans;
6508 }
6509
6510 bool array_array_container_ixor(array_container_t *src_1,
6511 const array_container_t *src_2, void **dst) {
6512 bool ans = array_array_container_xor(src_1, src_2, dst);
6513 array_container_free(src_1);
6514 return ans;
6515 }
6516
6517 int run_run_container_ixor(run_container_t *src_1, const run_container_t *src_2,
6518 void **dst) {
6519 int ans = run_run_container_xor(src_1, src_2, dst);
6520 run_container_free(src_1);
6521 return ans;
6522 }
6523 /* end file src/containers/mixed_xor.c */
6524 /* begin file src/containers/run.c */
6525 #include <stdio.h>
6526 #include <stdlib.h>
6527
6528
6529 extern inline uint16_t run_container_minimum(const run_container_t *run);
6530 extern inline uint16_t run_container_maximum(const run_container_t *run);
6531 extern inline int32_t interleavedBinarySearch(const rle16_t *array,
6532 int32_t lenarray, uint16_t ikey);
6533 extern inline bool run_container_contains(const run_container_t *run,
6534 uint16_t pos);
6535 extern inline int run_container_index_equalorlarger(const run_container_t *arr, uint16_t x);
6536 extern bool run_container_is_full(const run_container_t *run);
6537 extern bool run_container_nonzero_cardinality(const run_container_t *r);
6538 extern void run_container_clear(run_container_t *run);
6539 extern int32_t run_container_serialized_size_in_bytes(int32_t num_runs);
6540 extern run_container_t *run_container_create_range(uint32_t start,
6541 uint32_t stop);
6542
6543 bool run_container_add(run_container_t *run, uint16_t pos) {
6544 int32_t index = interleavedBinarySearch(run->runs, run->n_runs, pos);
6545 if (index >= 0) return false; // already there
6546 index = -index - 2; // points to preceding value, possibly -1
6547 if (index >= 0) { // possible match
6548 int32_t offset = pos - run->runs[index].value;
6549 int32_t le = run->runs[index].length;
6550 if (offset <= le) return false; // already there
6551 if (offset == le + 1) {
6552 // we may need to fuse
6553 if (index + 1 < run->n_runs) {
6554 if (run->runs[index + 1].value == pos + 1) {
6555 // indeed fusion is needed
6556 run->runs[index].length = run->runs[index + 1].value +
6557 run->runs[index + 1].length -
6558 run->runs[index].value;
6559 recoverRoomAtIndex(run, (uint16_t)(index + 1));
6560 return true;
6561 }
6562 }
6563 run->runs[index].length++;
6564 return true;
6565 }
6566 if (index + 1 < run->n_runs) {
6567 // we may need to fuse
6568 if (run->runs[index + 1].value == pos + 1) {
6569 // indeed fusion is needed
6570 run->runs[index + 1].value = pos;
6571 run->runs[index + 1].length = run->runs[index + 1].length + 1;
6572 return true;
6573 }
6574 }
6575 }
6576 if (index == -1) {
6577 // we may need to extend the first run
6578 if (0 < run->n_runs) {
6579 if (run->runs[0].value == pos + 1) {
6580 run->runs[0].length++;
6581 run->runs[0].value--;
6582 return true;
6583 }
6584 }
6585 }
6586 makeRoomAtIndex(run, (uint16_t)(index + 1));
6587 run->runs[index + 1].value = pos;
6588 run->runs[index + 1].length = 0;
6589 return true;
6590 }
6591
6592 /* Create a new run container. Return NULL in case of failure. */
6593 run_container_t *run_container_create_given_capacity(int32_t size) {
6594 run_container_t *run;
6595 /* Allocate the run container itself. */
6596 if ((run = (run_container_t *)malloc(sizeof(run_container_t))) == NULL) {
6597 return NULL;
6598 }
6599 if (size <= 0 ) { // we don't want to rely on malloc(0)
6600 run->runs = NULL;
6601 } else if ((run->runs = (rle16_t *)malloc(sizeof(rle16_t) * size)) == NULL) {
6602 free(run);
6603 return NULL;
6604 }
6605 run->capacity = size;
6606 run->n_runs = 0;
6607 return run;
6608 }
6609
6610 int run_container_shrink_to_fit(run_container_t *src) {
6611 if (src->n_runs == src->capacity) return 0; // nothing to do
6612 int savings = src->capacity - src->n_runs;
6613 src->capacity = src->n_runs;
6614 rle16_t *oldruns = src->runs;
6615 src->runs = (rle16_t *)realloc(oldruns, src->capacity * sizeof(rle16_t));
6616 if (src->runs == NULL) free(oldruns); // should never happen?
6617 return savings;
6618 }
6619 /* Create a new run container. Return NULL in case of failure. */
6620 run_container_t *run_container_create(void) {
6621 return run_container_create_given_capacity(RUN_DEFAULT_INIT_SIZE);
6622 }
6623
6624 run_container_t *run_container_clone(const run_container_t *src) {
6625 run_container_t *run = run_container_create_given_capacity(src->capacity);
6626 if (run == NULL) return NULL;
6627 run->capacity = src->capacity;
6628 run->n_runs = src->n_runs;
6629 memcpy(run->runs, src->runs, src->n_runs * sizeof(rle16_t));
6630 return run;
6631 }
6632
6633 /* Free memory. */
6634 void run_container_free(run_container_t *run) {
6635 if(run->runs != NULL) {// Jon Strabala reports that some tools complain otherwise
6636 free(run->runs);
6637 run->runs = NULL; // pedantic
6638 }
6639 free(run);
6640 }
6641
6642 void run_container_grow(run_container_t *run, int32_t min, bool copy) {
6643 int32_t newCapacity =
6644 (run->capacity == 0)
6645 ? RUN_DEFAULT_INIT_SIZE
6646 : run->capacity < 64 ? run->capacity * 2
6647 : run->capacity < 1024 ? run->capacity * 3 / 2
6648 : run->capacity * 5 / 4;
6649 if (newCapacity < min) newCapacity = min;
6650 run->capacity = newCapacity;
6651 assert(run->capacity >= min);
6652 if (copy) {
6653 rle16_t *oldruns = run->runs;
6654 run->runs =
6655 (rle16_t *)realloc(oldruns, run->capacity * sizeof(rle16_t));
6656 if (run->runs == NULL) free(oldruns);
6657 } else {
6658 // Jon Strabala reports that some tools complain otherwise
6659 if (run->runs != NULL) {
6660 free(run->runs);
6661 }
6662 run->runs = (rle16_t *)malloc(run->capacity * sizeof(rle16_t));
6663 }
6664 // handle the case where realloc fails
6665 if (run->runs == NULL) {
6666 fprintf(stderr, "could not allocate memory\n");
6667 }
6668 assert(run->runs != NULL);
6669 }
6670
6671 /* copy one container into another */
6672 void run_container_copy(const run_container_t *src, run_container_t *dst) {
6673 const int32_t n_runs = src->n_runs;
6674 if (src->n_runs > dst->capacity) {
6675 run_container_grow(dst, n_runs, false);
6676 }
6677 dst->n_runs = n_runs;
6678 memcpy(dst->runs, src->runs, sizeof(rle16_t) * n_runs);
6679 }
6680
6681 /* Compute the union of `src_1' and `src_2' and write the result to `dst'
6682 * It is assumed that `dst' is distinct from both `src_1' and `src_2'. */
6683 void run_container_union(const run_container_t *src_1,
6684 const run_container_t *src_2, run_container_t *dst) {
6685 // TODO: this could be a lot more efficient
6686
6687 // we start out with inexpensive checks
6688 const bool if1 = run_container_is_full(src_1);
6689 const bool if2 = run_container_is_full(src_2);
6690 if (if1 || if2) {
6691 if (if1) {
6692 run_container_copy(src_1, dst);
6693 return;
6694 }
6695 if (if2) {
6696 run_container_copy(src_2, dst);
6697 return;
6698 }
6699 }
6700 const int32_t neededcapacity = src_1->n_runs + src_2->n_runs;
6701 if (dst->capacity < neededcapacity)
6702 run_container_grow(dst, neededcapacity, false);
6703 dst->n_runs = 0;
6704 int32_t rlepos = 0;
6705 int32_t xrlepos = 0;
6706
6707 rle16_t previousrle;
6708 if (src_1->runs[rlepos].value <= src_2->runs[xrlepos].value) {
6709 previousrle = run_container_append_first(dst, src_1->runs[rlepos]);
6710 rlepos++;
6711 } else {
6712 previousrle = run_container_append_first(dst, src_2->runs[xrlepos]);
6713 xrlepos++;
6714 }
6715
6716 while ((xrlepos < src_2->n_runs) && (rlepos < src_1->n_runs)) {
6717 rle16_t newrl;
6718 if (src_1->runs[rlepos].value <= src_2->runs[xrlepos].value) {
6719 newrl = src_1->runs[rlepos];
6720 rlepos++;
6721 } else {
6722 newrl = src_2->runs[xrlepos];
6723 xrlepos++;
6724 }
6725 run_container_append(dst, newrl, &previousrle);
6726 }
6727 while (xrlepos < src_2->n_runs) {
6728 run_container_append(dst, src_2->runs[xrlepos], &previousrle);
6729 xrlepos++;
6730 }
6731 while (rlepos < src_1->n_runs) {
6732 run_container_append(dst, src_1->runs[rlepos], &previousrle);
6733 rlepos++;
6734 }
6735 }
6736
6737 /* Compute the union of `src_1' and `src_2' and write the result to `src_1'
6738 */
6739 void run_container_union_inplace(run_container_t *src_1,
6740 const run_container_t *src_2) {
6741 // TODO: this could be a lot more efficient
6742
6743 // we start out with inexpensive checks
6744 const bool if1 = run_container_is_full(src_1);
6745 const bool if2 = run_container_is_full(src_2);
6746 if (if1 || if2) {
6747 if (if1) {
6748 return;
6749 }
6750 if (if2) {
6751 run_container_copy(src_2, src_1);
6752 return;
6753 }
6754 }
6755 // we move the data to the end of the current array
6756 const int32_t maxoutput = src_1->n_runs + src_2->n_runs;
6757 const int32_t neededcapacity = maxoutput + src_1->n_runs;
6758 if (src_1->capacity < neededcapacity)
6759 run_container_grow(src_1, neededcapacity, true);
6760 memmove(src_1->runs + maxoutput, src_1->runs,
6761 src_1->n_runs * sizeof(rle16_t));
6762 rle16_t *inputsrc1 = src_1->runs + maxoutput;
6763 const int32_t input1nruns = src_1->n_runs;
6764 src_1->n_runs = 0;
6765 int32_t rlepos = 0;
6766 int32_t xrlepos = 0;
6767
6768 rle16_t previousrle;
6769 if (inputsrc1[rlepos].value <= src_2->runs[xrlepos].value) {
6770 previousrle = run_container_append_first(src_1, inputsrc1[rlepos]);
6771 rlepos++;
6772 } else {
6773 previousrle = run_container_append_first(src_1, src_2->runs[xrlepos]);
6774 xrlepos++;
6775 }
6776 while ((xrlepos < src_2->n_runs) && (rlepos < input1nruns)) {
6777 rle16_t newrl;
6778 if (inputsrc1[rlepos].value <= src_2->runs[xrlepos].value) {
6779 newrl = inputsrc1[rlepos];
6780 rlepos++;
6781 } else {
6782 newrl = src_2->runs[xrlepos];
6783 xrlepos++;
6784 }
6785 run_container_append(src_1, newrl, &previousrle);
6786 }
6787 while (xrlepos < src_2->n_runs) {
6788 run_container_append(src_1, src_2->runs[xrlepos], &previousrle);
6789 xrlepos++;
6790 }
6791 while (rlepos < input1nruns) {
6792 run_container_append(src_1, inputsrc1[rlepos], &previousrle);
6793 rlepos++;
6794 }
6795 }
6796
6797 /* Compute the symmetric difference of `src_1' and `src_2' and write the result
6798 * to `dst'
6799 * It is assumed that `dst' is distinct from both `src_1' and `src_2'. */
6800 void run_container_xor(const run_container_t *src_1,
6801 const run_container_t *src_2, run_container_t *dst) {
6802 // don't bother to convert xor with full range into negation
6803 // since negation is implemented similarly
6804
6805 const int32_t neededcapacity = src_1->n_runs + src_2->n_runs;
6806 if (dst->capacity < neededcapacity)
6807 run_container_grow(dst, neededcapacity, false);
6808
6809 int32_t pos1 = 0;
6810 int32_t pos2 = 0;
6811 dst->n_runs = 0;
6812
6813 while ((pos1 < src_1->n_runs) && (pos2 < src_2->n_runs)) {
6814 if (src_1->runs[pos1].value <= src_2->runs[pos2].value) {
6815 run_container_smart_append_exclusive(dst, src_1->runs[pos1].value,
6816 src_1->runs[pos1].length);
6817 pos1++;
6818 } else {
6819 run_container_smart_append_exclusive(dst, src_2->runs[pos2].value,
6820 src_2->runs[pos2].length);
6821 pos2++;
6822 }
6823 }
6824 while (pos1 < src_1->n_runs) {
6825 run_container_smart_append_exclusive(dst, src_1->runs[pos1].value,
6826 src_1->runs[pos1].length);
6827 pos1++;
6828 }
6829
6830 while (pos2 < src_2->n_runs) {
6831 run_container_smart_append_exclusive(dst, src_2->runs[pos2].value,
6832 src_2->runs[pos2].length);
6833 pos2++;
6834 }
6835 }
6836
6837 /* Compute the intersection of src_1 and src_2 and write the result to
6838 * dst. It is assumed that dst is distinct from both src_1 and src_2. */
6839 void run_container_intersection(const run_container_t *src_1,
6840 const run_container_t *src_2,
6841 run_container_t *dst) {
6842 const bool if1 = run_container_is_full(src_1);
6843 const bool if2 = run_container_is_full(src_2);
6844 if (if1 || if2) {
6845 if (if1) {
6846 run_container_copy(src_2, dst);
6847 return;
6848 }
6849 if (if2) {
6850 run_container_copy(src_1, dst);
6851 return;
6852 }
6853 }
6854 // TODO: this could be a lot more efficient, could use SIMD optimizations
6855 const int32_t neededcapacity = src_1->n_runs + src_2->n_runs;
6856 if (dst->capacity < neededcapacity)
6857 run_container_grow(dst, neededcapacity, false);
6858 dst->n_runs = 0;
6859 int32_t rlepos = 0;
6860 int32_t xrlepos = 0;
6861 int32_t start = src_1->runs[rlepos].value;
6862 int32_t end = start + src_1->runs[rlepos].length + 1;
6863 int32_t xstart = src_2->runs[xrlepos].value;
6864 int32_t xend = xstart + src_2->runs[xrlepos].length + 1;
6865 while ((rlepos < src_1->n_runs) && (xrlepos < src_2->n_runs)) {
6866 if (end <= xstart) {
6867 ++rlepos;
6868 if (rlepos < src_1->n_runs) {
6869 start = src_1->runs[rlepos].value;
6870 end = start + src_1->runs[rlepos].length + 1;
6871 }
6872 } else if (xend <= start) {
6873 ++xrlepos;
6874 if (xrlepos < src_2->n_runs) {
6875 xstart = src_2->runs[xrlepos].value;
6876 xend = xstart + src_2->runs[xrlepos].length + 1;
6877 }
6878 } else { // they overlap
6879 const int32_t lateststart = start > xstart ? start : xstart;
6880 int32_t earliestend;
6881 if (end == xend) { // improbable
6882 earliestend = end;
6883 rlepos++;
6884 xrlepos++;
6885 if (rlepos < src_1->n_runs) {
6886 start = src_1->runs[rlepos].value;
6887 end = start + src_1->runs[rlepos].length + 1;
6888 }
6889 if (xrlepos < src_2->n_runs) {
6890 xstart = src_2->runs[xrlepos].value;
6891 xend = xstart + src_2->runs[xrlepos].length + 1;
6892 }
6893 } else if (end < xend) {
6894 earliestend = end;
6895 rlepos++;
6896 if (rlepos < src_1->n_runs) {
6897 start = src_1->runs[rlepos].value;
6898 end = start + src_1->runs[rlepos].length + 1;
6899 }
6900
6901 } else { // end > xend
6902 earliestend = xend;
6903 xrlepos++;
6904 if (xrlepos < src_2->n_runs) {
6905 xstart = src_2->runs[xrlepos].value;
6906 xend = xstart + src_2->runs[xrlepos].length + 1;
6907 }
6908 }
6909 dst->runs[dst->n_runs].value = (uint16_t)lateststart;
6910 dst->runs[dst->n_runs].length =
6911 (uint16_t)(earliestend - lateststart - 1);
6912 dst->n_runs++;
6913 }
6914 }
6915 }
6916
6917 /* Compute the size of the intersection of src_1 and src_2 . */
6918 int run_container_intersection_cardinality(const run_container_t *src_1,
6919 const run_container_t *src_2) {
6920 const bool if1 = run_container_is_full(src_1);
6921 const bool if2 = run_container_is_full(src_2);
6922 if (if1 || if2) {
6923 if (if1) {
6924 return run_container_cardinality(src_2);
6925 }
6926 if (if2) {
6927 return run_container_cardinality(src_1);
6928 }
6929 }
6930 int answer = 0;
6931 int32_t rlepos = 0;
6932 int32_t xrlepos = 0;
6933 int32_t start = src_1->runs[rlepos].value;
6934 int32_t end = start + src_1->runs[rlepos].length + 1;
6935 int32_t xstart = src_2->runs[xrlepos].value;
6936 int32_t xend = xstart + src_2->runs[xrlepos].length + 1;
6937 while ((rlepos < src_1->n_runs) && (xrlepos < src_2->n_runs)) {
6938 if (end <= xstart) {
6939 ++rlepos;
6940 if (rlepos < src_1->n_runs) {
6941 start = src_1->runs[rlepos].value;
6942 end = start + src_1->runs[rlepos].length + 1;
6943 }
6944 } else if (xend <= start) {
6945 ++xrlepos;
6946 if (xrlepos < src_2->n_runs) {
6947 xstart = src_2->runs[xrlepos].value;
6948 xend = xstart + src_2->runs[xrlepos].length + 1;
6949 }
6950 } else { // they overlap
6951 const int32_t lateststart = start > xstart ? start : xstart;
6952 int32_t earliestend;
6953 if (end == xend) { // improbable
6954 earliestend = end;
6955 rlepos++;
6956 xrlepos++;
6957 if (rlepos < src_1->n_runs) {
6958 start = src_1->runs[rlepos].value;
6959 end = start + src_1->runs[rlepos].length + 1;
6960 }
6961 if (xrlepos < src_2->n_runs) {
6962 xstart = src_2->runs[xrlepos].value;
6963 xend = xstart + src_2->runs[xrlepos].length + 1;
6964 }
6965 } else if (end < xend) {
6966 earliestend = end;
6967 rlepos++;
6968 if (rlepos < src_1->n_runs) {
6969 start = src_1->runs[rlepos].value;
6970 end = start + src_1->runs[rlepos].length + 1;
6971 }
6972
6973 } else { // end > xend
6974 earliestend = xend;
6975 xrlepos++;
6976 if (xrlepos < src_2->n_runs) {
6977 xstart = src_2->runs[xrlepos].value;
6978 xend = xstart + src_2->runs[xrlepos].length + 1;
6979 }
6980 }
6981 answer += earliestend - lateststart;
6982 }
6983 }
6984 return answer;
6985 }
6986
6987 bool run_container_intersect(const run_container_t *src_1,
6988 const run_container_t *src_2) {
6989 const bool if1 = run_container_is_full(src_1);
6990 const bool if2 = run_container_is_full(src_2);
6991 if (if1 || if2) {
6992 if (if1) {
6993 return !run_container_empty(src_2);
6994 }
6995 if (if2) {
6996 return !run_container_empty(src_1);
6997 }
6998 }
6999 int32_t rlepos = 0;
7000 int32_t xrlepos = 0;
7001 int32_t start = src_1->runs[rlepos].value;
7002 int32_t end = start + src_1->runs[rlepos].length + 1;
7003 int32_t xstart = src_2->runs[xrlepos].value;
7004 int32_t xend = xstart + src_2->runs[xrlepos].length + 1;
7005 while ((rlepos < src_1->n_runs) && (xrlepos < src_2->n_runs)) {
7006 if (end <= xstart) {
7007 ++rlepos;
7008 if (rlepos < src_1->n_runs) {
7009 start = src_1->runs[rlepos].value;
7010 end = start + src_1->runs[rlepos].length + 1;
7011 }
7012 } else if (xend <= start) {
7013 ++xrlepos;
7014 if (xrlepos < src_2->n_runs) {
7015 xstart = src_2->runs[xrlepos].value;
7016 xend = xstart + src_2->runs[xrlepos].length + 1;
7017 }
7018 } else { // they overlap
7019 return true;
7020 }
7021 }
7022 return false;
7023 }
7024
7025
7026 /* Compute the difference of src_1 and src_2 and write the result to
7027 * dst. It is assumed that dst is distinct from both src_1 and src_2. */
7028 void run_container_andnot(const run_container_t *src_1,
7029 const run_container_t *src_2, run_container_t *dst) {
7030 // following Java implementation as of June 2016
7031
7032 if (dst->capacity < src_1->n_runs + src_2->n_runs)
7033 run_container_grow(dst, src_1->n_runs + src_2->n_runs, false);
7034
7035 dst->n_runs = 0;
7036
7037 int rlepos1 = 0;
7038 int rlepos2 = 0;
7039 int32_t start = src_1->runs[rlepos1].value;
7040 int32_t end = start + src_1->runs[rlepos1].length + 1;
7041 int32_t start2 = src_2->runs[rlepos2].value;
7042 int32_t end2 = start2 + src_2->runs[rlepos2].length + 1;
7043
7044 while ((rlepos1 < src_1->n_runs) && (rlepos2 < src_2->n_runs)) {
7045 if (end <= start2) {
7046 // output the first run
7047 dst->runs[dst->n_runs++] =
7048 (rle16_t){.value = (uint16_t)start,
7049 .length = (uint16_t)(end - start - 1)};
7050 rlepos1++;
7051 if (rlepos1 < src_1->n_runs) {
7052 start = src_1->runs[rlepos1].value;
7053 end = start + src_1->runs[rlepos1].length + 1;
7054 }
7055 } else if (end2 <= start) {
7056 // exit the second run
7057 rlepos2++;
7058 if (rlepos2 < src_2->n_runs) {
7059 start2 = src_2->runs[rlepos2].value;
7060 end2 = start2 + src_2->runs[rlepos2].length + 1;
7061 }
7062 } else {
7063 if (start < start2) {
7064 dst->runs[dst->n_runs++] =
7065 (rle16_t){.value = (uint16_t)start,
7066 .length = (uint16_t)(start2 - start - 1)};
7067 }
7068 if (end2 < end) {
7069 start = end2;
7070 } else {
7071 rlepos1++;
7072 if (rlepos1 < src_1->n_runs) {
7073 start = src_1->runs[rlepos1].value;
7074 end = start + src_1->runs[rlepos1].length + 1;
7075 }
7076 }
7077 }
7078 }
7079 if (rlepos1 < src_1->n_runs) {
7080 dst->runs[dst->n_runs++] = (rle16_t){
7081 .value = (uint16_t)start, .length = (uint16_t)(end - start - 1)};
7082 rlepos1++;
7083 if (rlepos1 < src_1->n_runs) {
7084 memcpy(dst->runs + dst->n_runs, src_1->runs + rlepos1,
7085 sizeof(rle16_t) * (src_1->n_runs - rlepos1));
7086 dst->n_runs += src_1->n_runs - rlepos1;
7087 }
7088 }
7089 }
7090
7091 int run_container_to_uint32_array(void *vout, const run_container_t *cont,
7092 uint32_t base) {
7093 int outpos = 0;
7094 uint32_t *out = (uint32_t *)vout;
7095 for (int i = 0; i < cont->n_runs; ++i) {
7096 uint32_t run_start = base + cont->runs[i].value;
7097 uint16_t le = cont->runs[i].length;
7098 for (int j = 0; j <= le; ++j) {
7099 uint32_t val = run_start + j;
7100 memcpy(out + outpos, &val,
7101 sizeof(uint32_t)); // should be compiled as a MOV on x64
7102 outpos++;
7103 }
7104 }
7105 return outpos;
7106 }
7107
7108 /*
7109 * Print this container using printf (useful for debugging).
7110 */
7111 void run_container_printf(const run_container_t *cont) {
7112 for (int i = 0; i < cont->n_runs; ++i) {
7113 uint16_t run_start = cont->runs[i].value;
7114 uint16_t le = cont->runs[i].length;
7115 printf("[%d,%d]", run_start, run_start + le);
7116 }
7117 }
7118
7119 /*
7120 * Print this container using printf as a comma-separated list of 32-bit
7121 * integers starting at base.
7122 */
7123 void run_container_printf_as_uint32_array(const run_container_t *cont,
7124 uint32_t base) {
7125 if (cont->n_runs == 0) return;
7126 {
7127 uint32_t run_start = base + cont->runs[0].value;
7128 uint16_t le = cont->runs[0].length;
7129 printf("%u", run_start);
7130 for (uint32_t j = 1; j <= le; ++j) printf(",%u", run_start + j);
7131 }
7132 for (int32_t i = 1; i < cont->n_runs; ++i) {
7133 uint32_t run_start = base + cont->runs[i].value;
7134 uint16_t le = cont->runs[i].length;
7135 for (uint32_t j = 0; j <= le; ++j) printf(",%u", run_start + j);
7136 }
7137 }
7138
7139 int32_t run_container_serialize(const run_container_t *container, char *buf) {
7140 int32_t l, off;
7141
7142 memcpy(buf, &container->n_runs, off = sizeof(container->n_runs));
7143 memcpy(&buf[off], &container->capacity, sizeof(container->capacity));
7144 off += sizeof(container->capacity);
7145
7146 l = sizeof(rle16_t) * container->n_runs;
7147 memcpy(&buf[off], container->runs, l);
7148 return (off + l);
7149 }
7150
7151 int32_t run_container_write(const run_container_t *container, char *buf) {
7152 memcpy(buf, &container->n_runs, sizeof(uint16_t));
7153 memcpy(buf + sizeof(uint16_t), container->runs,
7154 container->n_runs * sizeof(rle16_t));
7155 return run_container_size_in_bytes(container);
7156 }
7157
7158 int32_t run_container_read(int32_t cardinality, run_container_t *container,
7159 const char *buf) {
7160 (void)cardinality;
7161 memcpy(&container->n_runs, buf, sizeof(uint16_t));
7162 if (container->n_runs > container->capacity)
7163 run_container_grow(container, container->n_runs, false);
7164 memcpy(container->runs, buf + sizeof(uint16_t),
7165 container->n_runs * sizeof(rle16_t));
7166 return run_container_size_in_bytes(container);
7167 }
7168
7169 uint32_t run_container_serialization_len(const run_container_t *container) {
7170 return (sizeof(container->n_runs) + sizeof(container->capacity) +
7171 sizeof(rle16_t) * container->n_runs);
7172 }
7173
7174 void *run_container_deserialize(const char *buf, size_t buf_len) {
7175 run_container_t *ptr;
7176
7177 if (buf_len < 8 /* n_runs + capacity */)
7178 return (NULL);
7179 else
7180 buf_len -= 8;
7181
7182 if ((ptr = (run_container_t *)malloc(sizeof(run_container_t))) != NULL) {
7183 size_t len;
7184 int32_t off;
7185
7186 memcpy(&ptr->n_runs, buf, off = 4);
7187 memcpy(&ptr->capacity, &buf[off], 4);
7188 off += 4;
7189
7190 len = sizeof(rle16_t) * ptr->n_runs;
7191
7192 if (len != buf_len) {
7193 free(ptr);
7194 return (NULL);
7195 }
7196
7197 if ((ptr->runs = (rle16_t *)malloc(len)) == NULL) {
7198 free(ptr);
7199 return (NULL);
7200 }
7201
7202 memcpy(ptr->runs, &buf[off], len);
7203
7204 /* Check if returned values are monotonically increasing */
7205 for (int32_t i = 0, j = 0; i < ptr->n_runs; i++) {
7206 if (ptr->runs[i].value < j) {
7207 free(ptr->runs);
7208 free(ptr);
7209 return (NULL);
7210 } else
7211 j = ptr->runs[i].value;
7212 }
7213 }
7214
7215 return (ptr);
7216 }
7217
7218 bool run_container_iterate(const run_container_t *cont, uint32_t base,
7219 roaring_iterator iterator, void *ptr) {
7220 for (int i = 0; i < cont->n_runs; ++i) {
7221 uint32_t run_start = base + cont->runs[i].value;
7222 uint16_t le = cont->runs[i].length;
7223
7224 for (int j = 0; j <= le; ++j)
7225 if (!iterator(run_start + j, ptr)) return false;
7226 }
7227 return true;
7228 }
7229
7230 bool run_container_iterate64(const run_container_t *cont, uint32_t base,
7231 roaring_iterator64 iterator, uint64_t high_bits,
7232 void *ptr) {
7233 for (int i = 0; i < cont->n_runs; ++i) {
7234 uint32_t run_start = base + cont->runs[i].value;
7235 uint16_t le = cont->runs[i].length;
7236
7237 for (int j = 0; j <= le; ++j)
7238 if (!iterator(high_bits | (uint64_t)(run_start + j), ptr))
7239 return false;
7240 }
7241 return true;
7242 }
7243
7244 bool run_container_equals(const run_container_t *container1,
7245 const run_container_t *container2) {
7246 if (container1->n_runs != container2->n_runs) {
7247 return false;
7248 }
7249 for (int32_t i = 0; i < container1->n_runs; ++i) {
7250 if ((container1->runs[i].value != container2->runs[i].value) ||
7251 (container1->runs[i].length != container2->runs[i].length))
7252 return false;
7253 }
7254 return true;
7255 }
7256
7257 bool run_container_is_subset(const run_container_t *container1,
7258 const run_container_t *container2) {
7259 int i1 = 0, i2 = 0;
7260 while (i1 < container1->n_runs && i2 < container2->n_runs) {
7261 int start1 = container1->runs[i1].value;
7262 int stop1 = start1 + container1->runs[i1].length;
7263 int start2 = container2->runs[i2].value;
7264 int stop2 = start2 + container2->runs[i2].length;
7265 if (start1 < start2) {
7266 return false;
7267 } else { // start1 >= start2
7268 if (stop1 < stop2) {
7269 i1++;
7270 } else if (stop1 == stop2) {
7271 i1++;
7272 i2++;
7273 } else { // stop1 > stop2
7274 i2++;
7275 }
7276 }
7277 }
7278 if (i1 == container1->n_runs) {
7279 return true;
7280 } else {
7281 return false;
7282 }
7283 }
7284
7285 // TODO: write smart_append_exclusive version to match the overloaded 1 param
7286 // Java version (or is it even used?)
7287
7288 // follows the Java implementation closely
7289 // length is the rle-value. Ie, run [10,12) uses a length value 1.
7290 void run_container_smart_append_exclusive(run_container_t *src,
7291 const uint16_t start,
7292 const uint16_t length) {
7293 int old_end;
7294 rle16_t *last_run = src->n_runs ? src->runs + (src->n_runs - 1) : NULL;
7295 rle16_t *appended_last_run = src->runs + src->n_runs;
7296
7297 if (!src->n_runs ||
7298 (start > (old_end = last_run->value + last_run->length + 1))) {
7299 *appended_last_run = (rle16_t){.value = start, .length = length};
7300 src->n_runs++;
7301 return;
7302 }
7303 if (old_end == start) {
7304 // we merge
7305 last_run->length += (length + 1);
7306 return;
7307 }
7308 int new_end = start + length + 1;
7309
7310 if (start == last_run->value) {
7311 // wipe out previous
7312 if (new_end < old_end) {
7313 *last_run = (rle16_t){.value = (uint16_t)new_end,
7314 .length = (uint16_t)(old_end - new_end - 1)};
7315 return;
7316 } else if (new_end > old_end) {
7317 *last_run = (rle16_t){.value = (uint16_t)old_end,
7318 .length = (uint16_t)(new_end - old_end - 1)};
7319 return;
7320 } else {
7321 src->n_runs--;
7322 return;
7323 }
7324 }
7325 last_run->length = start - last_run->value - 1;
7326 if (new_end < old_end) {
7327 *appended_last_run =
7328 (rle16_t){.value = (uint16_t)new_end,
7329 .length = (uint16_t)(old_end - new_end - 1)};
7330 src->n_runs++;
7331 } else if (new_end > old_end) {
7332 *appended_last_run =
7333 (rle16_t){.value = (uint16_t)old_end,
7334 .length = (uint16_t)(new_end - old_end - 1)};
7335 src->n_runs++;
7336 }
7337 }
7338
7339 bool run_container_select(const run_container_t *container,
7340 uint32_t *start_rank, uint32_t rank,
7341 uint32_t *element) {
7342 for (int i = 0; i < container->n_runs; i++) {
7343 uint16_t length = container->runs[i].length;
7344 if (rank <= *start_rank + length) {
7345 uint16_t value = container->runs[i].value;
7346 *element = value + rank - (*start_rank);
7347 return true;
7348 } else
7349 *start_rank += length + 1;
7350 }
7351 return false;
7352 }
7353
7354 int run_container_rank(const run_container_t *container, uint16_t x) {
7355 int sum = 0;
7356 uint32_t x32 = x;
7357 for (int i = 0; i < container->n_runs; i++) {
7358 uint32_t startpoint = container->runs[i].value;
7359 uint32_t length = container->runs[i].length;
7360 uint32_t endpoint = length + startpoint;
7361 if (x <= endpoint) {
7362 if (x < startpoint) break;
7363 return sum + (x32 - startpoint) + 1;
7364 } else {
7365 sum += length + 1;
7366 }
7367 }
7368 return sum;
7369 }
7370 /* end file src/containers/run.c */
7371 /* begin file src/roaring.c */
7372 #include <assert.h>
7373 #include <stdarg.h>
7374 #include <stdint.h>
7375 #include <stdio.h>
7376 #include <string.h>
7377
7378 extern inline bool roaring_bitmap_contains(const roaring_bitmap_t *r,
7379 uint32_t val);
7380
7381 // this is like roaring_bitmap_add, but it populates pointer arguments in such a
7382 // way
7383 // that we can recover the container touched, which, in turn can be used to
7384 // accelerate some functions (when you repeatedly need to add to the same
7385 // container)
7386 static inline void *containerptr_roaring_bitmap_add(roaring_bitmap_t *r,
7387 uint32_t val,
7388 uint8_t *typecode,
7389 int *index) {
7390 uint16_t hb = val >> 16;
7391 const int i = ra_get_index(&r->high_low_container, hb);
7392 if (i >= 0) {
7393 ra_unshare_container_at_index(&r->high_low_container, i);
7394 void *container =
7395 ra_get_container_at_index(&r->high_low_container, i, typecode);
7396 uint8_t newtypecode = *typecode;
7397 void *container2 =
7398 container_add(container, val & 0xFFFF, *typecode, &newtypecode);
7399 *index = i;
7400 if (container2 != container) {
7401 container_free(container, *typecode);
7402 ra_set_container_at_index(&r->high_low_container, i, container2,
7403 newtypecode);
7404 *typecode = newtypecode;
7405 return container2;
7406 } else {
7407 return container;
7408 }
7409 } else {
7410 array_container_t *newac = array_container_create();
7411 void *container = container_add(newac, val & 0xFFFF,
7412 ARRAY_CONTAINER_TYPE_CODE, typecode);
7413 // we could just assume that it stays an array container
7414 ra_insert_new_key_value_at(&r->high_low_container, -i - 1, hb,
7415 container, *typecode);
7416 *index = -i - 1;
7417 return container;
7418 }
7419 }
7420
7421 roaring_bitmap_t *roaring_bitmap_create() {
7422 roaring_bitmap_t *ans =
7423 (roaring_bitmap_t *)malloc(sizeof(roaring_bitmap_t));
7424 if (!ans) {
7425 return NULL;
7426 }
7427 bool is_ok = ra_init(&ans->high_low_container);
7428 if (!is_ok) {
7429 free(ans);
7430 return NULL;
7431 }
7432 ans->copy_on_write = false;
7433 return ans;
7434 }
7435
7436 roaring_bitmap_t *roaring_bitmap_create_with_capacity(uint32_t cap) {
7437 roaring_bitmap_t *ans =
7438 (roaring_bitmap_t *)malloc(sizeof(roaring_bitmap_t));
7439 if (!ans) {
7440 return NULL;
7441 }
7442 bool is_ok = ra_init_with_capacity(&ans->high_low_container, cap);
7443 if (!is_ok) {
7444 free(ans);
7445 return NULL;
7446 }
7447 ans->copy_on_write = false;
7448 return ans;
7449 }
7450
7451 void roaring_bitmap_add_many(roaring_bitmap_t *r, size_t n_args,
7452 const uint32_t *vals) {
7453 void *container = NULL; // hold value of last container touched
7454 uint8_t typecode = 0; // typecode of last container touched
7455 uint32_t prev = 0; // previous valued inserted
7456 size_t i = 0; // index of value
7457 int containerindex = 0;
7458 if (n_args == 0) return;
7459 uint32_t val;
7460 memcpy(&val, vals + i, sizeof(val));
7461 container =
7462 containerptr_roaring_bitmap_add(r, val, &typecode, &containerindex);
7463 prev = val;
7464 i++;
7465 for (; i < n_args; i++) {
7466 memcpy(&val, vals + i, sizeof(val));
7467 if (((prev ^ val) >> 16) ==
7468 0) { // no need to seek the container, it is at hand
7469 // because we already have the container at hand, we can do the
7470 // insertion
7471 // automatically, bypassing the roaring_bitmap_add call
7472 uint8_t newtypecode = typecode;
7473 void *container2 =
7474 container_add(container, val & 0xFFFF, typecode, &newtypecode);
7475 if (container2 != container) { // rare instance when we need to
7476 // change the container type
7477 container_free(container, typecode);
7478 ra_set_container_at_index(&r->high_low_container,
7479 containerindex, container2,
7480 newtypecode);
7481 typecode = newtypecode;
7482 container = container2;
7483 }
7484 } else {
7485 container = containerptr_roaring_bitmap_add(r, val, &typecode,
7486 &containerindex);
7487 }
7488 prev = val;
7489 }
7490 }
7491
7492 roaring_bitmap_t *roaring_bitmap_of_ptr(size_t n_args, const uint32_t *vals) {
7493 roaring_bitmap_t *answer = roaring_bitmap_create();
7494 roaring_bitmap_add_many(answer, n_args, vals);
7495 return answer;
7496 }
7497
7498 roaring_bitmap_t *roaring_bitmap_of(size_t n_args, ...) {
7499 // todo: could be greatly optimized but we do not expect this call to ever
7500 // include long lists
7501 roaring_bitmap_t *answer = roaring_bitmap_create();
7502 va_list ap;
7503 va_start(ap, n_args);
7504 for (size_t i = 1; i <= n_args; i++) {
7505 uint32_t val = va_arg(ap, uint32_t);
7506 roaring_bitmap_add(answer, val);
7507 }
7508 va_end(ap);
7509 return answer;
7510 }
7511
7512 static inline uint32_t minimum_uint32(uint32_t a, uint32_t b) {
7513 return (a < b) ? a : b;
7514 }
7515
7516 static inline uint64_t minimum_uint64(uint64_t a, uint64_t b) {
7517 return (a < b) ? a : b;
7518 }
7519
7520 roaring_bitmap_t *roaring_bitmap_from_range(uint64_t min, uint64_t max,
7521 uint32_t step) {
7522 if(max >= UINT64_C(0x100000000)) {
7523 max = UINT64_C(0x100000000);
7524 }
7525 if (step == 0) return NULL;
7526 if (max <= min) return NULL;
7527 roaring_bitmap_t *answer = roaring_bitmap_create();
7528 if (step >= (1 << 16)) {
7529 for (uint32_t value = (uint32_t)min; value < max; value += step) {
7530 roaring_bitmap_add(answer, value);
7531 }
7532 return answer;
7533 }
7534 uint64_t min_tmp = min;
7535 do {
7536 uint32_t key = (uint32_t)min_tmp >> 16;
7537 uint32_t container_min = min_tmp & 0xFFFF;
7538 uint32_t container_max = (uint32_t)minimum_uint64(max - (key << 16), 1 << 16);
7539 uint8_t type;
7540 void *container = container_from_range(&type, container_min,
7541 container_max, (uint16_t)step);
7542 ra_append(&answer->high_low_container, key, container, type);
7543 uint32_t gap = container_max - container_min + step - 1;
7544 min_tmp += gap - (gap % step);
7545 } while (min_tmp < max);
7546 // cardinality of bitmap will be ((uint64_t) max - min + step - 1 ) / step
7547 return answer;
7548 }
7549
7550 void roaring_bitmap_add_range_closed(roaring_bitmap_t *ra, uint32_t min, uint32_t max) {
7551 if (min > max) {
7552 return;
7553 }
7554
7555 uint32_t min_key = min >> 16;
7556 uint32_t max_key = max >> 16;
7557
7558 int32_t num_required_containers = max_key - min_key + 1;
7559 int32_t suffix_length = count_greater(ra->high_low_container.keys,
7560 ra->high_low_container.size,
7561 max_key);
7562 int32_t prefix_length = count_less(ra->high_low_container.keys,
7563 ra->high_low_container.size - suffix_length,
7564 min_key);
7565 int32_t common_length = ra->high_low_container.size - prefix_length - suffix_length;
7566
7567 if (num_required_containers > common_length) {
7568 ra_shift_right(&ra->high_low_container, suffix_length,
7569 num_required_containers - common_length);
7570 }
7571
7572 int32_t src = prefix_length + common_length - 1;
7573 int32_t dst = ra->high_low_container.size - suffix_length - 1;
7574 for (uint32_t key = max_key; key != min_key-1; key--) { // beware of min_key==0
7575 uint32_t container_min = (min_key == key) ? (min & 0xffff) : 0;
7576 uint32_t container_max = (max_key == key) ? (max & 0xffff) : 0xffff;
7577 void* new_container;
7578 uint8_t new_type;
7579
7580 if (src >= 0 && ra->high_low_container.keys[src] == key) {
7581 ra_unshare_container_at_index(&ra->high_low_container, src);
7582 new_container = container_add_range(ra->high_low_container.containers[src],
7583 ra->high_low_container.typecodes[src],
7584 container_min, container_max, &new_type);
7585 if (new_container != ra->high_low_container.containers[src]) {
7586 container_free(ra->high_low_container.containers[src],
7587 ra->high_low_container.typecodes[src]);
7588 }
7589 src--;
7590 } else {
7591 new_container = container_from_range(&new_type, container_min,
7592 container_max+1, 1);
7593 }
7594 ra_replace_key_and_container_at_index(&ra->high_low_container, dst,
7595 key, new_container, new_type);
7596 dst--;
7597 }
7598 }
7599
7600 void roaring_bitmap_add_range(roaring_bitmap_t *ra, uint64_t min, uint64_t max) {
7601 if(max == min) return;
7602 roaring_bitmap_add_range_closed(ra, min, max - 1);
7603 }
7604
7605 void roaring_bitmap_printf(const roaring_bitmap_t *ra) {
7606 printf("{");
7607 for (int i = 0; i < ra->high_low_container.size; ++i) {
7608 container_printf_as_uint32_array(
7609 ra->high_low_container.containers[i],
7610 ra->high_low_container.typecodes[i],
7611 ((uint32_t)ra->high_low_container.keys[i]) << 16);
7612 if (i + 1 < ra->high_low_container.size) printf(",");
7613 }
7614 printf("}");
7615 }
7616
7617 void roaring_bitmap_printf_describe(const roaring_bitmap_t *ra) {
7618 printf("{");
7619 for (int i = 0; i < ra->high_low_container.size; ++i) {
7620 printf("%d: %s (%d)", ra->high_low_container.keys[i],
7621 get_full_container_name(ra->high_low_container.containers[i],
7622 ra->high_low_container.typecodes[i]),
7623 container_get_cardinality(ra->high_low_container.containers[i],
7624 ra->high_low_container.typecodes[i]));
7625 if (ra->high_low_container.typecodes[i] == SHARED_CONTAINER_TYPE_CODE) {
7626 printf(
7627 "(shared count = %u )",
7628 ((shared_container_t *)(ra->high_low_container.containers[i]))
7629 ->counter);
7630 }
7631
7632 if (i + 1 < ra->high_low_container.size) printf(", ");
7633 }
7634 printf("}");
7635 }
7636
7637 typedef struct min_max_sum_s {
7638 uint32_t min;
7639 uint32_t max;
7640 uint64_t sum;
7641 } min_max_sum_t;
7642
7643 static bool min_max_sum_fnc(uint32_t value, void *param) {
7644 min_max_sum_t *mms = (min_max_sum_t *)param;
7645 if (value > mms->max) mms->max = value;
7646 if (value < mms->min) mms->min = value;
7647 mms->sum += value;
7648 return true; // we always process all data points
7649 }
7650
7651 /**
7652 * (For advanced users.)
7653 * Collect statistics about the bitmap
7654 */
7655 void roaring_bitmap_statistics(const roaring_bitmap_t *ra,
7656 roaring_statistics_t *stat) {
7657 memset(stat, 0, sizeof(*stat));
7658 stat->n_containers = ra->high_low_container.size;
7659 stat->cardinality = roaring_bitmap_get_cardinality(ra);
7660 min_max_sum_t mms;
7661 mms.min = UINT32_C(0xFFFFFFFF);
7662 mms.max = UINT32_C(0);
7663 mms.sum = 0;
7664 roaring_iterate(ra, &min_max_sum_fnc, &mms);
7665 stat->min_value = mms.min;
7666 stat->max_value = mms.max;
7667 stat->sum_value = mms.sum;
7668
7669 for (int i = 0; i < ra->high_low_container.size; ++i) {
7670 uint8_t truetype =
7671 get_container_type(ra->high_low_container.containers[i],
7672 ra->high_low_container.typecodes[i]);
7673 uint32_t card =
7674 container_get_cardinality(ra->high_low_container.containers[i],
7675 ra->high_low_container.typecodes[i]);
7676 uint32_t sbytes =
7677 container_size_in_bytes(ra->high_low_container.containers[i],
7678 ra->high_low_container.typecodes[i]);
7679 switch (truetype) {
7680 case BITSET_CONTAINER_TYPE_CODE:
7681 stat->n_bitset_containers++;
7682 stat->n_values_bitset_containers += card;
7683 stat->n_bytes_bitset_containers += sbytes;
7684 break;
7685 case ARRAY_CONTAINER_TYPE_CODE:
7686 stat->n_array_containers++;
7687 stat->n_values_array_containers += card;
7688 stat->n_bytes_array_containers += sbytes;
7689 break;
7690 case RUN_CONTAINER_TYPE_CODE:
7691 stat->n_run_containers++;
7692 stat->n_values_run_containers += card;
7693 stat->n_bytes_run_containers += sbytes;
7694 break;
7695 default:
7696 assert(false);
7697 __builtin_unreachable();
7698 }
7699 }
7700 }
7701
7702 roaring_bitmap_t *roaring_bitmap_copy(const roaring_bitmap_t *r) {
7703 roaring_bitmap_t *ans =
7704 (roaring_bitmap_t *)malloc(sizeof(roaring_bitmap_t));
7705 if (!ans) {
7706 return NULL;
7707 }
7708 bool is_ok = ra_copy(&r->high_low_container, &ans->high_low_container,
7709 r->copy_on_write);
7710 if (!is_ok) {
7711 free(ans);
7712 return NULL;
7713 }
7714 ans->copy_on_write = r->copy_on_write;
7715 return ans;
7716 }
7717
7718 static bool roaring_bitmap_overwrite(roaring_bitmap_t *dest,
7719 const roaring_bitmap_t *src) {
7720 return ra_overwrite(&src->high_low_container, &dest->high_low_container,
7721 src->copy_on_write);
7722 }
7723
7724 void roaring_bitmap_free(roaring_bitmap_t *r) {
7725 ra_clear(&r->high_low_container);
7726 free(r);
7727 }
7728
7729 void roaring_bitmap_clear(roaring_bitmap_t *r) {
7730 ra_reset(&r->high_low_container);
7731 }
7732
7733 void roaring_bitmap_add(roaring_bitmap_t *r, uint32_t val) {
7734 const uint16_t hb = val >> 16;
7735 const int i = ra_get_index(&r->high_low_container, hb);
7736 uint8_t typecode;
7737 if (i >= 0) {
7738 ra_unshare_container_at_index(&r->high_low_container, i);
7739 void *container =
7740 ra_get_container_at_index(&r->high_low_container, i, &typecode);
7741 uint8_t newtypecode = typecode;
7742 void *container2 =
7743 container_add(container, val & 0xFFFF, typecode, &newtypecode);
7744 if (container2 != container) {
7745 container_free(container, typecode);
7746 ra_set_container_at_index(&r->high_low_container, i, container2,
7747 newtypecode);
7748 }
7749 } else {
7750 array_container_t *newac = array_container_create();
7751 void *container = container_add(newac, val & 0xFFFF,
7752 ARRAY_CONTAINER_TYPE_CODE, &typecode);
7753 // we could just assume that it stays an array container
7754 ra_insert_new_key_value_at(&r->high_low_container, -i - 1, hb,
7755 container, typecode);
7756 }
7757 }
7758
7759 bool roaring_bitmap_add_checked(roaring_bitmap_t *r, uint32_t val) {
7760 const uint16_t hb = val >> 16;
7761 const int i = ra_get_index(&r->high_low_container, hb);
7762 uint8_t typecode;
7763 bool result = false;
7764 if (i >= 0) {
7765 ra_unshare_container_at_index(&r->high_low_container, i);
7766 void *container =
7767 ra_get_container_at_index(&r->high_low_container, i, &typecode);
7768
7769 const int oldCardinality =
7770 container_get_cardinality(container, typecode);
7771
7772 uint8_t newtypecode = typecode;
7773 void *container2 =
7774 container_add(container, val & 0xFFFF, typecode, &newtypecode);
7775 if (container2 != container) {
7776 container_free(container, typecode);
7777 ra_set_container_at_index(&r->high_low_container, i, container2,
7778 newtypecode);
7779 result = true;
7780 } else {
7781 const int newCardinality =
7782 container_get_cardinality(container, newtypecode);
7783
7784 result = oldCardinality != newCardinality;
7785 }
7786 } else {
7787 array_container_t *newac = array_container_create();
7788 void *container = container_add(newac, val & 0xFFFF,
7789 ARRAY_CONTAINER_TYPE_CODE, &typecode);
7790 // we could just assume that it stays an array container
7791 ra_insert_new_key_value_at(&r->high_low_container, -i - 1, hb,
7792 container, typecode);
7793 result = true;
7794 }
7795
7796 return result;
7797 }
7798
7799 void roaring_bitmap_remove(roaring_bitmap_t *r, uint32_t val) {
7800 const uint16_t hb = val >> 16;
7801 const int i = ra_get_index(&r->high_low_container, hb);
7802 uint8_t typecode;
7803 if (i >= 0) {
7804 ra_unshare_container_at_index(&r->high_low_container, i);
7805 void *container =
7806 ra_get_container_at_index(&r->high_low_container, i, &typecode);
7807 uint8_t newtypecode = typecode;
7808 void *container2 =
7809 container_remove(container, val & 0xFFFF, typecode, &newtypecode);
7810 if (container2 != container) {
7811 container_free(container, typecode);
7812 ra_set_container_at_index(&r->high_low_container, i, container2,
7813 newtypecode);
7814 }
7815 if (container_get_cardinality(container2, newtypecode) != 0) {
7816 ra_set_container_at_index(&r->high_low_container, i, container2,
7817 newtypecode);
7818 } else {
7819 ra_remove_at_index_and_free(&r->high_low_container, i);
7820 }
7821 }
7822 }
7823
7824 bool roaring_bitmap_remove_checked(roaring_bitmap_t *r, uint32_t val) {
7825 const uint16_t hb = val >> 16;
7826 const int i = ra_get_index(&r->high_low_container, hb);
7827 uint8_t typecode;
7828 bool result = false;
7829 if (i >= 0) {
7830 ra_unshare_container_at_index(&r->high_low_container, i);
7831 void *container =
7832 ra_get_container_at_index(&r->high_low_container, i, &typecode);
7833
7834 const int oldCardinality =
7835 container_get_cardinality(container, typecode);
7836
7837 uint8_t newtypecode = typecode;
7838 void *container2 =
7839 container_remove(container, val & 0xFFFF, typecode, &newtypecode);
7840 if (container2 != container) {
7841 container_free(container, typecode);
7842 ra_set_container_at_index(&r->high_low_container, i, container2,
7843 newtypecode);
7844 }
7845
7846 const int newCardinality =
7847 container_get_cardinality(container2, newtypecode);
7848
7849 if (newCardinality != 0) {
7850 ra_set_container_at_index(&r->high_low_container, i, container2,
7851 newtypecode);
7852 } else {
7853 ra_remove_at_index_and_free(&r->high_low_container, i);
7854 }
7855
7856 result = oldCardinality != newCardinality;
7857 }
7858 return result;
7859 }
7860
7861 // there should be some SIMD optimizations possible here
7862 roaring_bitmap_t *roaring_bitmap_and(const roaring_bitmap_t *x1,
7863 const roaring_bitmap_t *x2) {
7864 uint8_t container_result_type = 0;
7865 const int length1 = x1->high_low_container.size,
7866 length2 = x2->high_low_container.size;
7867 uint32_t neededcap = length1 > length2 ? length2 : length1;
7868 roaring_bitmap_t *answer = roaring_bitmap_create_with_capacity(neededcap);
7869 answer->copy_on_write = x1->copy_on_write && x2->copy_on_write;
7870
7871 int pos1 = 0, pos2 = 0;
7872
7873 while (pos1 < length1 && pos2 < length2) {
7874 const uint16_t s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
7875 const uint16_t s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
7876
7877 if (s1 == s2) {
7878 uint8_t container_type_1, container_type_2;
7879 void *c1 = ra_get_container_at_index(&x1->high_low_container, pos1,
7880 &container_type_1);
7881 void *c2 = ra_get_container_at_index(&x2->high_low_container, pos2,
7882 &container_type_2);
7883 void *c = container_and(c1, container_type_1, c2, container_type_2,
7884 &container_result_type);
7885 if (container_nonzero_cardinality(c, container_result_type)) {
7886 ra_append(&answer->high_low_container, s1, c,
7887 container_result_type);
7888 } else {
7889 container_free(
7890 c, container_result_type); // otherwise:memory leak!
7891 }
7892 ++pos1;
7893 ++pos2;
7894 } else if (s1 < s2) { // s1 < s2
7895 pos1 = ra_advance_until(&x1->high_low_container, s2, pos1);
7896 } else { // s1 > s2
7897 pos2 = ra_advance_until(&x2->high_low_container, s1, pos2);
7898 }
7899 }
7900 return answer;
7901 }
7902
7903 /**
7904 * Compute the union of 'number' bitmaps.
7905 */
7906 roaring_bitmap_t *roaring_bitmap_or_many(size_t number,
7907 const roaring_bitmap_t **x) {
7908 if (number == 0) {
7909 return roaring_bitmap_create();
7910 }
7911 if (number == 1) {
7912 return roaring_bitmap_copy(x[0]);
7913 }
7914 roaring_bitmap_t *answer =
7915 roaring_bitmap_lazy_or(x[0], x[1], LAZY_OR_BITSET_CONVERSION);
7916 for (size_t i = 2; i < number; i++) {
7917 roaring_bitmap_lazy_or_inplace(answer, x[i], LAZY_OR_BITSET_CONVERSION);
7918 }
7919 roaring_bitmap_repair_after_lazy(answer);
7920 return answer;
7921 }
7922
7923 /**
7924 * Compute the xor of 'number' bitmaps.
7925 */
7926 roaring_bitmap_t *roaring_bitmap_xor_many(size_t number,
7927 const roaring_bitmap_t **x) {
7928 if (number == 0) {
7929 return roaring_bitmap_create();
7930 }
7931 if (number == 1) {
7932 return roaring_bitmap_copy(x[0]);
7933 }
7934 roaring_bitmap_t *answer = roaring_bitmap_lazy_xor(x[0], x[1]);
7935 for (size_t i = 2; i < number; i++) {
7936 roaring_bitmap_lazy_xor_inplace(answer, x[i]);
7937 }
7938 roaring_bitmap_repair_after_lazy(answer);
7939 return answer;
7940 }
7941
7942 // inplace and (modifies its first argument).
7943 void roaring_bitmap_and_inplace(roaring_bitmap_t *x1,
7944 const roaring_bitmap_t *x2) {
7945 if (x1 == x2) return;
7946 int pos1 = 0, pos2 = 0, intersection_size = 0;
7947 const int length1 = ra_get_size(&x1->high_low_container);
7948 const int length2 = ra_get_size(&x2->high_low_container);
7949
7950 // any skipped-over or newly emptied containers in x1
7951 // have to be freed.
7952 while (pos1 < length1 && pos2 < length2) {
7953 const uint16_t s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
7954 const uint16_t s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
7955
7956 if (s1 == s2) {
7957 uint8_t typecode1, typecode2, typecode_result;
7958 void *c1 = ra_get_container_at_index(&x1->high_low_container, pos1,
7959 &typecode1);
7960 c1 = get_writable_copy_if_shared(c1, &typecode1);
7961 void *c2 = ra_get_container_at_index(&x2->high_low_container, pos2,
7962 &typecode2);
7963 void *c =
7964 container_iand(c1, typecode1, c2, typecode2, &typecode_result);
7965 if (c != c1) { // in this instance a new container was created, and
7966 // we need to free the old one
7967 container_free(c1, typecode1);
7968 }
7969 if (container_nonzero_cardinality(c, typecode_result)) {
7970 ra_replace_key_and_container_at_index(&x1->high_low_container,
7971 intersection_size, s1, c,
7972 typecode_result);
7973 intersection_size++;
7974 } else {
7975 container_free(c, typecode_result);
7976 }
7977 ++pos1;
7978 ++pos2;
7979 } else if (s1 < s2) {
7980 pos1 = ra_advance_until_freeing(&x1->high_low_container, s2, pos1);
7981 } else { // s1 > s2
7982 pos2 = ra_advance_until(&x2->high_low_container, s1, pos2);
7983 }
7984 }
7985
7986 // if we ended early because x2 ran out, then all remaining in x1 should be
7987 // freed
7988 while (pos1 < length1) {
7989 container_free(x1->high_low_container.containers[pos1],
7990 x1->high_low_container.typecodes[pos1]);
7991 ++pos1;
7992 }
7993
7994 // all containers after this have either been copied or freed
7995 ra_downsize(&x1->high_low_container, intersection_size);
7996 }
7997
7998 roaring_bitmap_t *roaring_bitmap_or(const roaring_bitmap_t *x1,
7999 const roaring_bitmap_t *x2) {
8000 uint8_t container_result_type = 0;
8001 const int length1 = x1->high_low_container.size,
8002 length2 = x2->high_low_container.size;
8003 if (0 == length1) {
8004 return roaring_bitmap_copy(x2);
8005 }
8006 if (0 == length2) {
8007 return roaring_bitmap_copy(x1);
8008 }
8009 roaring_bitmap_t *answer =
8010 roaring_bitmap_create_with_capacity(length1 + length2);
8011 answer->copy_on_write = x1->copy_on_write && x2->copy_on_write;
8012 int pos1 = 0, pos2 = 0;
8013 uint8_t container_type_1, container_type_2;
8014 uint16_t s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
8015 uint16_t s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
8016 while (true) {
8017 if (s1 == s2) {
8018 void *c1 = ra_get_container_at_index(&x1->high_low_container, pos1,
8019 &container_type_1);
8020 void *c2 = ra_get_container_at_index(&x2->high_low_container, pos2,
8021 &container_type_2);
8022 void *c = container_or(c1, container_type_1, c2, container_type_2,
8023 &container_result_type);
8024 // since we assume that the initial containers are non-empty, the
8025 // result here
8026 // can only be non-empty
8027 ra_append(&answer->high_low_container, s1, c,
8028 container_result_type);
8029 ++pos1;
8030 ++pos2;
8031 if (pos1 == length1) break;
8032 if (pos2 == length2) break;
8033 s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
8034 s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
8035
8036 } else if (s1 < s2) { // s1 < s2
8037 void *c1 = ra_get_container_at_index(&x1->high_low_container, pos1,
8038 &container_type_1);
8039 // c1 = container_clone(c1, container_type_1);
8040 c1 =
8041 get_copy_of_container(c1, &container_type_1, x1->copy_on_write);
8042 if (x1->copy_on_write) {
8043 ra_set_container_at_index(&x1->high_low_container, pos1, c1,
8044 container_type_1);
8045 }
8046 ra_append(&answer->high_low_container, s1, c1, container_type_1);
8047 pos1++;
8048 if (pos1 == length1) break;
8049 s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
8050
8051 } else { // s1 > s2
8052 void *c2 = ra_get_container_at_index(&x2->high_low_container, pos2,
8053 &container_type_2);
8054 // c2 = container_clone(c2, container_type_2);
8055 c2 =
8056 get_copy_of_container(c2, &container_type_2, x2->copy_on_write);
8057 if (x2->copy_on_write) {
8058 ra_set_container_at_index(&x2->high_low_container, pos2, c2,
8059 container_type_2);
8060 }
8061 ra_append(&answer->high_low_container, s2, c2, container_type_2);
8062 pos2++;
8063 if (pos2 == length2) break;
8064 s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
8065 }
8066 }
8067 if (pos1 == length1) {
8068 ra_append_copy_range(&answer->high_low_container,
8069 &x2->high_low_container, pos2, length2,
8070 x2->copy_on_write);
8071 } else if (pos2 == length2) {
8072 ra_append_copy_range(&answer->high_low_container,
8073 &x1->high_low_container, pos1, length1,
8074 x1->copy_on_write);
8075 }
8076 return answer;
8077 }
8078
8079 // inplace or (modifies its first argument).
8080 void roaring_bitmap_or_inplace(roaring_bitmap_t *x1,
8081 const roaring_bitmap_t *x2) {
8082 uint8_t container_result_type = 0;
8083 int length1 = x1->high_low_container.size;
8084 const int length2 = x2->high_low_container.size;
8085
8086 if (0 == length2) return;
8087
8088 if (0 == length1) {
8089 roaring_bitmap_overwrite(x1, x2);
8090 return;
8091 }
8092 int pos1 = 0, pos2 = 0;
8093 uint8_t container_type_1, container_type_2;
8094 uint16_t s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
8095 uint16_t s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
8096 while (true) {
8097 if (s1 == s2) {
8098 void *c1 = ra_get_container_at_index(&x1->high_low_container, pos1,
8099 &container_type_1);
8100 if (!container_is_full(c1, container_type_1)) {
8101 c1 = get_writable_copy_if_shared(c1, &container_type_1);
8102
8103 void *c2 = ra_get_container_at_index(&x2->high_low_container,
8104 pos2, &container_type_2);
8105 void *c =
8106 container_ior(c1, container_type_1, c2, container_type_2,
8107 &container_result_type);
8108 if (c !=
8109 c1) { // in this instance a new container was created, and
8110 // we need to free the old one
8111 container_free(c1, container_type_1);
8112 }
8113
8114 ra_set_container_at_index(&x1->high_low_container, pos1, c,
8115 container_result_type);
8116 }
8117 ++pos1;
8118 ++pos2;
8119 if (pos1 == length1) break;
8120 if (pos2 == length2) break;
8121 s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
8122 s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
8123
8124 } else if (s1 < s2) { // s1 < s2
8125 pos1++;
8126 if (pos1 == length1) break;
8127 s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
8128
8129 } else { // s1 > s2
8130 void *c2 = ra_get_container_at_index(&x2->high_low_container, pos2,
8131 &container_type_2);
8132 c2 =
8133 get_copy_of_container(c2, &container_type_2, x2->copy_on_write);
8134 if (x2->copy_on_write) {
8135 ra_set_container_at_index(&x2->high_low_container, pos2, c2,
8136 container_type_2);
8137 }
8138
8139 // void *c2_clone = container_clone(c2, container_type_2);
8140 ra_insert_new_key_value_at(&x1->high_low_container, pos1, s2, c2,
8141 container_type_2);
8142 pos1++;
8143 length1++;
8144 pos2++;
8145 if (pos2 == length2) break;
8146 s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
8147 }
8148 }
8149 if (pos1 == length1) {
8150 ra_append_copy_range(&x1->high_low_container, &x2->high_low_container,
8151 pos2, length2, x2->copy_on_write);
8152 }
8153 }
8154
8155 roaring_bitmap_t *roaring_bitmap_xor(const roaring_bitmap_t *x1,
8156 const roaring_bitmap_t *x2) {
8157 uint8_t container_result_type = 0;
8158 const int length1 = x1->high_low_container.size,
8159 length2 = x2->high_low_container.size;
8160 if (0 == length1) {
8161 return roaring_bitmap_copy(x2);
8162 }
8163 if (0 == length2) {
8164 return roaring_bitmap_copy(x1);
8165 }
8166 roaring_bitmap_t *answer =
8167 roaring_bitmap_create_with_capacity(length1 + length2);
8168 answer->copy_on_write = x1->copy_on_write && x2->copy_on_write;
8169 int pos1 = 0, pos2 = 0;
8170 uint8_t container_type_1, container_type_2;
8171 uint16_t s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
8172 uint16_t s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
8173 while (true) {
8174 if (s1 == s2) {
8175 void *c1 = ra_get_container_at_index(&x1->high_low_container, pos1,
8176 &container_type_1);
8177 void *c2 = ra_get_container_at_index(&x2->high_low_container, pos2,
8178 &container_type_2);
8179 void *c = container_xor(c1, container_type_1, c2, container_type_2,
8180 &container_result_type);
8181
8182 if (container_nonzero_cardinality(c, container_result_type)) {
8183 ra_append(&answer->high_low_container, s1, c,
8184 container_result_type);
8185 } else {
8186 container_free(c, container_result_type);
8187 }
8188 ++pos1;
8189 ++pos2;
8190 if (pos1 == length1) break;
8191 if (pos2 == length2) break;
8192 s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
8193 s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
8194
8195 } else if (s1 < s2) { // s1 < s2
8196 void *c1 = ra_get_container_at_index(&x1->high_low_container, pos1,
8197 &container_type_1);
8198 c1 =
8199 get_copy_of_container(c1, &container_type_1, x1->copy_on_write);
8200 if (x1->copy_on_write) {
8201 ra_set_container_at_index(&x1->high_low_container, pos1, c1,
8202 container_type_1);
8203 }
8204 ra_append(&answer->high_low_container, s1, c1, container_type_1);
8205 pos1++;
8206 if (pos1 == length1) break;
8207 s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
8208
8209 } else { // s1 > s2
8210 void *c2 = ra_get_container_at_index(&x2->high_low_container, pos2,
8211 &container_type_2);
8212 c2 =
8213 get_copy_of_container(c2, &container_type_2, x2->copy_on_write);
8214 if (x2->copy_on_write) {
8215 ra_set_container_at_index(&x2->high_low_container, pos2, c2,
8216 container_type_2);
8217 }
8218 ra_append(&answer->high_low_container, s2, c2, container_type_2);
8219 pos2++;
8220 if (pos2 == length2) break;
8221 s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
8222 }
8223 }
8224 if (pos1 == length1) {
8225 ra_append_copy_range(&answer->high_low_container,
8226 &x2->high_low_container, pos2, length2,
8227 x2->copy_on_write);
8228 } else if (pos2 == length2) {
8229 ra_append_copy_range(&answer->high_low_container,
8230 &x1->high_low_container, pos1, length1,
8231 x1->copy_on_write);
8232 }
8233 return answer;
8234 }
8235
8236 // inplace xor (modifies its first argument).
8237
8238 void roaring_bitmap_xor_inplace(roaring_bitmap_t *x1,
8239 const roaring_bitmap_t *x2) {
8240 assert(x1 != x2);
8241 uint8_t container_result_type = 0;
8242 int length1 = x1->high_low_container.size;
8243 const int length2 = x2->high_low_container.size;
8244
8245 if (0 == length2) return;
8246
8247 if (0 == length1) {
8248 roaring_bitmap_overwrite(x1, x2);
8249 return;
8250 }
8251
8252 // XOR can have new containers inserted from x2, but can also
8253 // lose containers when x1 and x2 are nonempty and identical.
8254
8255 int pos1 = 0, pos2 = 0;
8256 uint8_t container_type_1, container_type_2;
8257 uint16_t s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
8258 uint16_t s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
8259 while (true) {
8260 if (s1 == s2) {
8261 void *c1 = ra_get_container_at_index(&x1->high_low_container, pos1,
8262 &container_type_1);
8263 c1 = get_writable_copy_if_shared(c1, &container_type_1);
8264
8265 void *c2 = ra_get_container_at_index(&x2->high_low_container, pos2,
8266 &container_type_2);
8267 void *c = container_ixor(c1, container_type_1, c2, container_type_2,
8268 &container_result_type);
8269
8270 if (container_nonzero_cardinality(c, container_result_type)) {
8271 ra_set_container_at_index(&x1->high_low_container, pos1, c,
8272 container_result_type);
8273 ++pos1;
8274 } else {
8275 container_free(c, container_result_type);
8276 ra_remove_at_index(&x1->high_low_container, pos1);
8277 --length1;
8278 }
8279
8280 ++pos2;
8281 if (pos1 == length1) break;
8282 if (pos2 == length2) break;
8283 s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
8284 s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
8285
8286 } else if (s1 < s2) { // s1 < s2
8287 pos1++;
8288 if (pos1 == length1) break;
8289 s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
8290
8291 } else { // s1 > s2
8292 void *c2 = ra_get_container_at_index(&x2->high_low_container, pos2,
8293 &container_type_2);
8294 c2 =
8295 get_copy_of_container(c2, &container_type_2, x2->copy_on_write);
8296 if (x2->copy_on_write) {
8297 ra_set_container_at_index(&x2->high_low_container, pos2, c2,
8298 container_type_2);
8299 }
8300
8301 ra_insert_new_key_value_at(&x1->high_low_container, pos1, s2, c2,
8302 container_type_2);
8303 pos1++;
8304 length1++;
8305 pos2++;
8306 if (pos2 == length2) break;
8307 s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
8308 }
8309 }
8310 if (pos1 == length1) {
8311 ra_append_copy_range(&x1->high_low_container, &x2->high_low_container,
8312 pos2, length2, x2->copy_on_write);
8313 }
8314 }
8315
8316 roaring_bitmap_t *roaring_bitmap_andnot(const roaring_bitmap_t *x1,
8317 const roaring_bitmap_t *x2) {
8318 uint8_t container_result_type = 0;
8319 const int length1 = x1->high_low_container.size,
8320 length2 = x2->high_low_container.size;
8321 if (0 == length1) {
8322 roaring_bitmap_t *empty_bitmap = roaring_bitmap_create();
8323 empty_bitmap->copy_on_write = x1->copy_on_write && x2->copy_on_write;
8324 return empty_bitmap;
8325 }
8326 if (0 == length2) {
8327 return roaring_bitmap_copy(x1);
8328 }
8329 roaring_bitmap_t *answer = roaring_bitmap_create_with_capacity(length1);
8330 answer->copy_on_write = x1->copy_on_write && x2->copy_on_write;
8331
8332 int pos1 = 0, pos2 = 0;
8333 uint8_t container_type_1, container_type_2;
8334 uint16_t s1 = 0;
8335 uint16_t s2 = 0;
8336 while (true) {
8337 s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
8338 s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
8339
8340 if (s1 == s2) {
8341 void *c1 = ra_get_container_at_index(&x1->high_low_container, pos1,
8342 &container_type_1);
8343 void *c2 = ra_get_container_at_index(&x2->high_low_container, pos2,
8344 &container_type_2);
8345 void *c =
8346 container_andnot(c1, container_type_1, c2, container_type_2,
8347 &container_result_type);
8348
8349 if (container_nonzero_cardinality(c, container_result_type)) {
8350 ra_append(&answer->high_low_container, s1, c,
8351 container_result_type);
8352 } else {
8353 container_free(c, container_result_type);
8354 }
8355 ++pos1;
8356 ++pos2;
8357 if (pos1 == length1) break;
8358 if (pos2 == length2) break;
8359 } else if (s1 < s2) { // s1 < s2
8360 const int next_pos1 =
8361 ra_advance_until(&x1->high_low_container, s2, pos1);
8362 ra_append_copy_range(&answer->high_low_container,
8363 &x1->high_low_container, pos1, next_pos1,
8364 x1->copy_on_write);
8365 // TODO : perhaps some of the copy_on_write should be based on
8366 // answer rather than x1 (more stringent?). Many similar cases
8367 pos1 = next_pos1;
8368 if (pos1 == length1) break;
8369 } else { // s1 > s2
8370 pos2 = ra_advance_until(&x2->high_low_container, s1, pos2);
8371 if (pos2 == length2) break;
8372 }
8373 }
8374 if (pos2 == length2) {
8375 ra_append_copy_range(&answer->high_low_container,
8376 &x1->high_low_container, pos1, length1,
8377 x1->copy_on_write);
8378 }
8379 return answer;
8380 }
8381
8382 // inplace andnot (modifies its first argument).
8383
8384 void roaring_bitmap_andnot_inplace(roaring_bitmap_t *x1,
8385 const roaring_bitmap_t *x2) {
8386 assert(x1 != x2);
8387
8388 uint8_t container_result_type = 0;
8389 int length1 = x1->high_low_container.size;
8390 const int length2 = x2->high_low_container.size;
8391 int intersection_size = 0;
8392
8393 if (0 == length2) return;
8394
8395 if (0 == length1) {
8396 roaring_bitmap_clear(x1);
8397 return;
8398 }
8399
8400 int pos1 = 0, pos2 = 0;
8401 uint8_t container_type_1, container_type_2;
8402 uint16_t s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
8403 uint16_t s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
8404 while (true) {
8405 if (s1 == s2) {
8406 void *c1 = ra_get_container_at_index(&x1->high_low_container, pos1,
8407 &container_type_1);
8408 c1 = get_writable_copy_if_shared(c1, &container_type_1);
8409
8410 void *c2 = ra_get_container_at_index(&x2->high_low_container, pos2,
8411 &container_type_2);
8412 void *c =
8413 container_iandnot(c1, container_type_1, c2, container_type_2,
8414 &container_result_type);
8415
8416 if (container_nonzero_cardinality(c, container_result_type)) {
8417 ra_replace_key_and_container_at_index(&x1->high_low_container,
8418 intersection_size++, s1,
8419 c, container_result_type);
8420 } else {
8421 container_free(c, container_result_type);
8422 }
8423
8424 ++pos1;
8425 ++pos2;
8426 if (pos1 == length1) break;
8427 if (pos2 == length2) break;
8428 s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
8429 s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
8430
8431 } else if (s1 < s2) { // s1 < s2
8432 if (pos1 != intersection_size) {
8433 void *c1 = ra_get_container_at_index(&x1->high_low_container,
8434 pos1, &container_type_1);
8435
8436 ra_replace_key_and_container_at_index(&x1->high_low_container,
8437 intersection_size, s1, c1,
8438 container_type_1);
8439 }
8440 intersection_size++;
8441 pos1++;
8442 if (pos1 == length1) break;
8443 s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
8444
8445 } else { // s1 > s2
8446 pos2 = ra_advance_until(&x2->high_low_container, s1, pos2);
8447 if (pos2 == length2) break;
8448 s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
8449 }
8450 }
8451
8452 if (pos1 < length1) {
8453 // all containers between intersection_size and
8454 // pos1 are junk. However, they have either been moved
8455 // (thus still referenced) or involved in an iandnot
8456 // that will clean up all containers that could not be reused.
8457 // Thus we should not free the junk containers between
8458 // intersection_size and pos1.
8459 if (pos1 > intersection_size) {
8460 // left slide of remaining items
8461 ra_copy_range(&x1->high_low_container, pos1, length1,
8462 intersection_size);
8463 }
8464 // else current placement is fine
8465 intersection_size += (length1 - pos1);
8466 }
8467 ra_downsize(&x1->high_low_container, intersection_size);
8468 }
8469
8470 uint64_t roaring_bitmap_get_cardinality(const roaring_bitmap_t *ra) {
8471 uint64_t card = 0;
8472 for (int i = 0; i < ra->high_low_container.size; ++i)
8473 card += container_get_cardinality(ra->high_low_container.containers[i],
8474 ra->high_low_container.typecodes[i]);
8475 return card;
8476 }
8477
8478 bool roaring_bitmap_is_empty(const roaring_bitmap_t *ra) {
8479 return ra->high_low_container.size == 0;
8480 }
8481
8482 void roaring_bitmap_to_uint32_array(const roaring_bitmap_t *ra, uint32_t *ans) {
8483 ra_to_uint32_array(&ra->high_low_container, ans);
8484 }
8485
8486 /** convert array and bitmap containers to run containers when it is more
8487 * efficient;
8488 * also convert from run containers when more space efficient. Returns
8489 * true if the result has at least one run container.
8490 */
8491 bool roaring_bitmap_run_optimize(roaring_bitmap_t *r) {
8492 bool answer = false;
8493 for (int i = 0; i < r->high_low_container.size; i++) {
8494 uint8_t typecode_original, typecode_after;
8495 ra_unshare_container_at_index(
8496 &r->high_low_container, i); // TODO: this introduces extra cloning!
8497 void *c = ra_get_container_at_index(&r->high_low_container, i,
8498 &typecode_original);
8499 void *c1 = convert_run_optimize(c, typecode_original, &typecode_after);
8500 if (typecode_after == RUN_CONTAINER_TYPE_CODE) answer = true;
8501 ra_set_container_at_index(&r->high_low_container, i, c1,
8502 typecode_after);
8503 }
8504 return answer;
8505 }
8506
8507 size_t roaring_bitmap_shrink_to_fit(roaring_bitmap_t *r) {
8508 size_t answer = 0;
8509 for (int i = 0; i < r->high_low_container.size; i++) {
8510 uint8_t typecode_original;
8511 void *c = ra_get_container_at_index(&r->high_low_container, i,
8512 &typecode_original);
8513 answer += container_shrink_to_fit(c, typecode_original);
8514 }
8515 answer += ra_shrink_to_fit(&r->high_low_container);
8516 return answer;
8517 }
8518
8519 /**
8520 * Remove run-length encoding even when it is more space efficient
8521 * return whether a change was applied
8522 */
8523 bool roaring_bitmap_remove_run_compression(roaring_bitmap_t *r) {
8524 bool answer = false;
8525 for (int i = 0; i < r->high_low_container.size; i++) {
8526 uint8_t typecode_original, typecode_after;
8527 void *c = ra_get_container_at_index(&r->high_low_container, i,
8528 &typecode_original);
8529 if (get_container_type(c, typecode_original) ==
8530 RUN_CONTAINER_TYPE_CODE) {
8531 answer = true;
8532 if (typecode_original == SHARED_CONTAINER_TYPE_CODE) {
8533 run_container_t *truec =
8534 (run_container_t *)((shared_container_t *)c)->container;
8535 int32_t card = run_container_cardinality(truec);
8536 void *c1 = convert_to_bitset_or_array_container(
8537 truec, card, &typecode_after);
8538 shared_container_free((shared_container_t *)c);
8539 ra_set_container_at_index(&r->high_low_container, i, c1,
8540 typecode_after);
8541
8542 } else {
8543 int32_t card = run_container_cardinality((run_container_t *)c);
8544 void *c1 = convert_to_bitset_or_array_container(
8545 (run_container_t *)c, card, &typecode_after);
8546 ra_set_container_at_index(&r->high_low_container, i, c1,
8547 typecode_after);
8548 }
8549 }
8550 }
8551 return answer;
8552 }
8553
8554 size_t roaring_bitmap_serialize(const roaring_bitmap_t *ra, char *buf) {
8555 size_t portablesize = roaring_bitmap_portable_size_in_bytes(ra);
8556 uint64_t cardinality = roaring_bitmap_get_cardinality(ra);
8557 size_t sizeasarray = cardinality * sizeof(uint32_t) + sizeof(uint32_t);
8558 if (portablesize < sizeasarray) {
8559 buf[0] = SERIALIZATION_CONTAINER;
8560 return roaring_bitmap_portable_serialize(ra, buf + 1) + 1;
8561 } else {
8562 buf[0] = SERIALIZATION_ARRAY_UINT32;
8563 memcpy(buf + 1, &cardinality, sizeof(uint32_t));
8564 roaring_bitmap_to_uint32_array(
8565 ra, (uint32_t *)(buf + 1 + sizeof(uint32_t)));
8566 return 1 + sizeasarray;
8567 }
8568 }
8569
8570 size_t roaring_bitmap_size_in_bytes(const roaring_bitmap_t *ra) {
8571 size_t portablesize = roaring_bitmap_portable_size_in_bytes(ra);
8572 size_t sizeasarray = roaring_bitmap_get_cardinality(ra) * sizeof(uint32_t) +
8573 sizeof(uint32_t);
8574 return portablesize < sizeasarray ? portablesize + 1 : sizeasarray + 1;
8575 }
8576
8577 size_t roaring_bitmap_portable_size_in_bytes(const roaring_bitmap_t *ra) {
8578 return ra_portable_size_in_bytes(&ra->high_low_container);
8579 }
8580
8581
8582 roaring_bitmap_t *roaring_bitmap_portable_deserialize_safe(const char *buf, size_t maxbytes) {
8583 roaring_bitmap_t *ans =
8584 (roaring_bitmap_t *)malloc(sizeof(roaring_bitmap_t));
8585 if (ans == NULL) {
8586 return NULL;
8587 }
8588 size_t bytesread;
8589 bool is_ok = ra_portable_deserialize(&ans->high_low_container, buf, maxbytes, &bytesread);
8590 if(is_ok) assert(bytesread <= maxbytes);
8591 ans->copy_on_write = false;
8592 if (!is_ok) {
8593 free(ans);
8594 return NULL;
8595 }
8596 return ans;
8597 }
8598
8599 roaring_bitmap_t *roaring_bitmap_portable_deserialize(const char *buf) {
8600 return roaring_bitmap_portable_deserialize_safe(buf, SIZE_MAX);
8601 }
8602
8603
8604 size_t roaring_bitmap_portable_deserialize_size(const char *buf, size_t maxbytes) {
8605 return ra_portable_deserialize_size(buf, maxbytes);
8606 }
8607
8608
8609 size_t roaring_bitmap_portable_serialize(const roaring_bitmap_t *ra,
8610 char *buf) {
8611 return ra_portable_serialize(&ra->high_low_container, buf);
8612 }
8613
8614 roaring_bitmap_t *roaring_bitmap_deserialize(const void *buf) {
8615 const char *bufaschar = (const char *)buf;
8616 if (*(const unsigned char *)buf == SERIALIZATION_ARRAY_UINT32) {
8617 /* This looks like a compressed set of uint32_t elements */
8618 uint32_t card;
8619 memcpy(&card, bufaschar + 1, sizeof(uint32_t));
8620 const uint32_t *elems =
8621 (const uint32_t *)(bufaschar + 1 + sizeof(uint32_t));
8622
8623 return roaring_bitmap_of_ptr(card, elems);
8624 } else if (bufaschar[0] == SERIALIZATION_CONTAINER) {
8625 return roaring_bitmap_portable_deserialize(bufaschar + 1);
8626 } else
8627 return (NULL);
8628 }
8629
8630 bool roaring_iterate(const roaring_bitmap_t *ra, roaring_iterator iterator,
8631 void *ptr) {
8632 for (int i = 0; i < ra->high_low_container.size; ++i)
8633 if (!container_iterate(ra->high_low_container.containers[i],
8634 ra->high_low_container.typecodes[i],
8635 ((uint32_t)ra->high_low_container.keys[i]) << 16,
8636 iterator, ptr)) {
8637 return false;
8638 }
8639 return true;
8640 }
8641
8642 bool roaring_iterate64(const roaring_bitmap_t *ra, roaring_iterator64 iterator,
8643 uint64_t high_bits, void *ptr) {
8644 for (int i = 0; i < ra->high_low_container.size; ++i)
8645 if (!container_iterate64(
8646 ra->high_low_container.containers[i],
8647 ra->high_low_container.typecodes[i],
8648 ((uint32_t)ra->high_low_container.keys[i]) << 16, iterator,
8649 high_bits, ptr)) {
8650 return false;
8651 }
8652 return true;
8653 }
8654
8655 /****
8656 * begin roaring_uint32_iterator_t
8657 *****/
8658
8659 static bool loadfirstvalue(roaring_uint32_iterator_t *newit) {
8660 newit->in_container_index = 0;
8661 newit->run_index = 0;
8662 newit->current_value = 0;
8663 if (newit->container_index >=
8664 newit->parent->high_low_container.size) { // otherwise nothing
8665 newit->current_value = UINT32_MAX;
8666 return (newit->has_value = false);
8667 }
8668 // assume not empty
8669 newit->has_value = true;
8670 // we precompute container, typecode and highbits so that successive
8671 // iterators do not have to grab them from odd memory locations
8672 // and have to worry about the (easily predicted) container_unwrap_shared
8673 // call.
8674 newit->container =
8675 newit->parent->high_low_container.containers[newit->container_index];
8676 newit->typecode =
8677 newit->parent->high_low_container.typecodes[newit->container_index];
8678 newit->highbits =
8679 ((uint32_t)
8680 newit->parent->high_low_container.keys[newit->container_index])
8681 << 16;
8682 newit->container =
8683 container_unwrap_shared(newit->container, &(newit->typecode));
8684 uint32_t wordindex;
8685 uint64_t word; // used for bitsets
8686 switch (newit->typecode) {
8687 case BITSET_CONTAINER_TYPE_CODE:
8688 wordindex = 0;
8689 while ((word = ((const bitset_container_t *)(newit->container))
8690 ->array[wordindex]) == 0)
8691 wordindex++; // advance
8692 // here "word" is non-zero
8693 newit->in_container_index = wordindex * 64 + __builtin_ctzll(word);
8694 newit->current_value = newit->highbits | newit->in_container_index;
8695 break;
8696 case ARRAY_CONTAINER_TYPE_CODE:
8697 newit->current_value =
8698 newit->highbits |
8699 ((const array_container_t *)(newit->container))->array[0];
8700 break;
8701 case RUN_CONTAINER_TYPE_CODE:
8702 newit->current_value =
8703 newit->highbits |
8704 (((const run_container_t *)(newit->container))->runs[0].value);
8705 newit->in_run_index =
8706 newit->current_value +
8707 (((const run_container_t *)(newit->container))->runs[0].length);
8708 break;
8709 default:
8710 // if this ever happens, bug!
8711 assert(false);
8712 } // switch (typecode)
8713 return true;
8714 }
8715
8716 // prerequesite: the value should be in range of the container
8717 static bool loadfirstvalue_largeorequal(roaring_uint32_iterator_t *newit, uint32_t val) {
8718 uint16_t lb = val & 0xFFFF;
8719 newit->in_container_index = 0;
8720 newit->run_index = 0;
8721 newit->current_value = 0;
8722 // assume it is found
8723 newit->has_value = true;
8724 newit->container =
8725 newit->parent->high_low_container.containers[newit->container_index];
8726 newit->typecode =
8727 newit->parent->high_low_container.typecodes[newit->container_index];
8728 newit->highbits =
8729 ((uint32_t)
8730 newit->parent->high_low_container.keys[newit->container_index])
8731 << 16;
8732 newit->container =
8733 container_unwrap_shared(newit->container, &(newit->typecode));
8734 switch (newit->typecode) {
8735 case BITSET_CONTAINER_TYPE_CODE:
8736 newit->in_container_index = bitset_container_index_equalorlarger((const bitset_container_t *)(newit->container), lb);
8737 newit->current_value = newit->highbits | newit->in_container_index;
8738 break;
8739 case ARRAY_CONTAINER_TYPE_CODE:
8740 newit->in_container_index = array_container_index_equalorlarger((const array_container_t *)(newit->container), lb);
8741 newit->current_value =
8742 newit->highbits |
8743 ((const array_container_t *)(newit->container))->array[newit->in_container_index];
8744 break;
8745 case RUN_CONTAINER_TYPE_CODE:
8746 newit->run_index = run_container_index_equalorlarger((const run_container_t *)(newit->container), lb);
8747 if(((const run_container_t *)(newit->container))->runs[newit->run_index].value <= lb) {
8748 newit->current_value = val;
8749 } else {
8750 newit->current_value =
8751 newit->highbits |
8752 (((const run_container_t *)(newit->container))->runs[newit->run_index].value);
8753 }
8754 newit->in_run_index =
8755 (newit->highbits | (((const run_container_t *)(newit->container))->runs[newit->run_index].value)) +
8756 (((const run_container_t *)(newit->container))->runs[newit->run_index].length);
8757
8758 break;
8759 default:
8760 // if this ever happens, bug!
8761 assert(false);
8762 } // switch (typecode)
8763 return true;
8764 }
8765
8766 void roaring_init_iterator(const roaring_bitmap_t *ra,
8767 roaring_uint32_iterator_t *newit) {
8768 newit->parent = ra;
8769 newit->container_index = 0;
8770 newit->has_value = loadfirstvalue(newit);
8771 }
8772
8773 roaring_uint32_iterator_t *roaring_create_iterator(const roaring_bitmap_t *ra) {
8774 roaring_uint32_iterator_t *newit =
8775 (roaring_uint32_iterator_t *)malloc(sizeof(roaring_uint32_iterator_t));
8776 if (newit == NULL) return NULL;
8777 roaring_init_iterator(ra, newit);
8778 return newit;
8779 }
8780
8781 roaring_uint32_iterator_t *roaring_copy_uint32_iterator(
8782 const roaring_uint32_iterator_t *it) {
8783 roaring_uint32_iterator_t *newit =
8784 (roaring_uint32_iterator_t *)malloc(sizeof(roaring_uint32_iterator_t));
8785 memcpy(newit, it, sizeof(roaring_uint32_iterator_t));
8786 return newit;
8787 }
8788
8789 bool roaring_move_uint32_iterator_equalorlarger(roaring_uint32_iterator_t *it, uint32_t val) {
8790 uint16_t hb = val >> 16;
8791 const int i = ra_get_index(& it->parent->high_low_container, hb);
8792 if (i >= 0) {
8793 uint32_t lowvalue = container_maximum(it->parent->high_low_container.containers[i], it->parent->high_low_container.typecodes[i]);
8794 uint16_t lb = val & 0xFFFF;
8795 if(lowvalue < lb ) {
8796 it->container_index = i+1; // will have to load first value of next container
8797 } else {// the value is necessarily within the range of the container
8798 it->container_index = i;
8799 it->has_value = loadfirstvalue_largeorequal(it, val);
8800 return it->has_value;
8801 }
8802 } else {
8803 // there is no matching, so we are going for the next container
8804 it->container_index = -i-1;
8805 }
8806 it->has_value = loadfirstvalue(it);
8807 return it->has_value;
8808 }
8809
8810
8811 bool roaring_advance_uint32_iterator(roaring_uint32_iterator_t *it) {
8812 if (it->container_index >= it->parent->high_low_container.size) {
8813 return (it->has_value = false);
8814 }
8815 uint32_t wordindex; // used for bitsets
8816 uint64_t word; // used for bitsets
8817 switch (it->typecode) {
8818 case BITSET_CONTAINER_TYPE_CODE:
8819 it->in_container_index++;
8820 wordindex = it->in_container_index / 64;
8821 if (wordindex >= BITSET_CONTAINER_SIZE_IN_WORDS) break;
8822 word = ((const bitset_container_t *)(it->container))
8823 ->array[wordindex] &
8824 (UINT64_MAX << (it->in_container_index % 64));
8825 // next part could be optimized/simplified
8826 while ((word == 0) &&
8827 (wordindex + 1 < BITSET_CONTAINER_SIZE_IN_WORDS)) {
8828 wordindex++;
8829 word = ((const bitset_container_t *)(it->container))
8830 ->array[wordindex];
8831 }
8832 if (word != 0) {
8833 it->in_container_index = wordindex * 64 + __builtin_ctzll(word);
8834 it->current_value = it->highbits | it->in_container_index;
8835 return (it->has_value = true);
8836 }
8837 break;
8838 case ARRAY_CONTAINER_TYPE_CODE:
8839 it->in_container_index++;
8840 if (it->in_container_index <
8841 ((const array_container_t *)(it->container))->cardinality) {
8842 it->current_value = it->highbits |
8843 ((const array_container_t *)(it->container))
8844 ->array[it->in_container_index];
8845 return true;
8846 }
8847 break;
8848 case RUN_CONTAINER_TYPE_CODE:
8849 if(it->current_value == UINT32_MAX) {
8850 return (it->has_value = false); // without this, we risk an overflow to zero
8851 }
8852 it->current_value++;
8853 if (it->current_value <= it->in_run_index) {
8854 return (it->has_value = true);
8855 }
8856 it->run_index++;
8857 if (it->run_index <
8858 ((const run_container_t *)(it->container))->n_runs) {
8859 it->current_value =
8860 it->highbits | (((const run_container_t *)(it->container))
8861 ->runs[it->run_index]
8862 .value);
8863 it->in_run_index = it->current_value +
8864 ((const run_container_t *)(it->container))
8865 ->runs[it->run_index]
8866 .length;
8867 return (it->has_value = true);
8868 }
8869 break;
8870 default:
8871 // if this ever happens, bug!
8872 assert(false);
8873 } // switch (typecode)
8874 // moving to next container
8875 it->container_index++;
8876 return (it->has_value = loadfirstvalue(it));
8877 }
8878
8879 uint32_t roaring_read_uint32_iterator(roaring_uint32_iterator_t *it, uint32_t* buf, uint32_t count) {
8880 uint32_t ret = 0;
8881 uint32_t num_values;
8882 uint32_t wordindex; // used for bitsets
8883 uint64_t word; // used for bitsets
8884 const array_container_t* acont; //TODO remove
8885 const run_container_t* rcont; //TODO remove
8886 const bitset_container_t* bcont; //TODO remove
8887
8888 while (it->has_value && ret < count) {
8889 switch (it->typecode) {
8890 case BITSET_CONTAINER_TYPE_CODE:
8891 bcont = (const bitset_container_t*)(it->container);
8892 wordindex = it->in_container_index / 64;
8893 word = bcont->array[wordindex] & (UINT64_MAX << (it->in_container_index % 64));
8894 do {
8895 while (word != 0 && ret < count) {
8896 buf[0] = it->highbits | (wordindex * 64 + __builtin_ctzll(word));
8897 word = word & (word - 1);
8898 buf++;
8899 ret++;
8900 }
8901 while (word == 0 && wordindex+1 < BITSET_CONTAINER_SIZE_IN_WORDS) {
8902 wordindex++;
8903 word = bcont->array[wordindex];
8904 }
8905 } while (word != 0 && ret < count);
8906 it->has_value = (word != 0);
8907 if (it->has_value) {
8908 it->in_container_index = wordindex * 64 + __builtin_ctzll(word);
8909 it->current_value = it->highbits | it->in_container_index;
8910 }
8911 break;
8912 case ARRAY_CONTAINER_TYPE_CODE:
8913 acont = (const array_container_t *)(it->container);
8914 num_values = minimum_uint32(acont->cardinality - it->in_container_index, count - ret);
8915 for (uint32_t i = 0; i < num_values; i++) {
8916 buf[i] = it->highbits | acont->array[it->in_container_index + i];
8917 }
8918 buf += num_values;
8919 ret += num_values;
8920 it->in_container_index += num_values;
8921 it->has_value = (it->in_container_index < acont->cardinality);
8922 if (it->has_value) {
8923 it->current_value = it->highbits | acont->array[it->in_container_index];
8924 }
8925 break;
8926 case RUN_CONTAINER_TYPE_CODE:
8927 rcont = (const run_container_t*)(it->container);
8928 //"in_run_index" name is misleading, read it as "max_value_in_current_run"
8929 do {
8930 num_values = minimum_uint32(it->in_run_index - it->current_value + 1, count - ret);
8931 for (uint32_t i = 0; i < num_values; i++) {
8932 buf[i] = it->current_value + i;
8933 }
8934 it->current_value += num_values; // this can overflow to zero: UINT32_MAX+1=0
8935 buf += num_values;
8936 ret += num_values;
8937
8938 if (it->current_value > it->in_run_index || it->current_value == 0) {
8939 it->run_index++;
8940 if (it->run_index < rcont->n_runs) {
8941 it->current_value = it->highbits | rcont->runs[it->run_index].value;
8942 it->in_run_index = it->current_value + rcont->runs[it->run_index].length;
8943 } else {
8944 it->has_value = false;
8945 }
8946 }
8947 } while ((ret < count) && it->has_value);
8948 break;
8949 default:
8950 assert(false);
8951 }
8952 if (it->has_value) {
8953 assert(ret == count);
8954 return ret;
8955 }
8956 it->container_index++;
8957 it->has_value = loadfirstvalue(it);
8958 }
8959 return ret;
8960 }
8961
8962
8963
8964 void roaring_free_uint32_iterator(roaring_uint32_iterator_t *it) { free(it); }
8965
8966 /****
8967 * end of roaring_uint32_iterator_t
8968 *****/
8969
8970 bool roaring_bitmap_equals(const roaring_bitmap_t *ra1,
8971 const roaring_bitmap_t *ra2) {
8972 if (ra1->high_low_container.size != ra2->high_low_container.size) {
8973 return false;
8974 }
8975 for (int i = 0; i < ra1->high_low_container.size; ++i) {
8976 if (ra1->high_low_container.keys[i] !=
8977 ra2->high_low_container.keys[i]) {
8978 return false;
8979 }
8980 }
8981 for (int i = 0; i < ra1->high_low_container.size; ++i) {
8982 bool areequal = container_equals(ra1->high_low_container.containers[i],
8983 ra1->high_low_container.typecodes[i],
8984 ra2->high_low_container.containers[i],
8985 ra2->high_low_container.typecodes[i]);
8986 if (!areequal) {
8987 return false;
8988 }
8989 }
8990 return true;
8991 }
8992
8993 bool roaring_bitmap_is_subset(const roaring_bitmap_t *ra1,
8994 const roaring_bitmap_t *ra2) {
8995 const int length1 = ra1->high_low_container.size,
8996 length2 = ra2->high_low_container.size;
8997
8998 int pos1 = 0, pos2 = 0;
8999
9000 while (pos1 < length1 && pos2 < length2) {
9001 const uint16_t s1 = ra_get_key_at_index(&ra1->high_low_container, pos1);
9002 const uint16_t s2 = ra_get_key_at_index(&ra2->high_low_container, pos2);
9003
9004 if (s1 == s2) {
9005 uint8_t container_type_1, container_type_2;
9006 void *c1 = ra_get_container_at_index(&ra1->high_low_container, pos1,
9007 &container_type_1);
9008 void *c2 = ra_get_container_at_index(&ra2->high_low_container, pos2,
9009 &container_type_2);
9010 bool subset =
9011 container_is_subset(c1, container_type_1, c2, container_type_2);
9012 if (!subset) return false;
9013 ++pos1;
9014 ++pos2;
9015 } else if (s1 < s2) { // s1 < s2
9016 return false;
9017 } else { // s1 > s2
9018 pos2 = ra_advance_until(&ra2->high_low_container, s1, pos2);
9019 }
9020 }
9021 if (pos1 == length1)
9022 return true;
9023 else
9024 return false;
9025 }
9026
9027 static void insert_flipped_container(roaring_array_t *ans_arr,
9028 const roaring_array_t *x1_arr, uint16_t hb,
9029 uint16_t lb_start, uint16_t lb_end) {
9030 const int i = ra_get_index(x1_arr, hb);
9031 const int j = ra_get_index(ans_arr, hb);
9032 uint8_t ctype_in, ctype_out;
9033 void *flipped_container = NULL;
9034 if (i >= 0) {
9035 void *container_to_flip =
9036 ra_get_container_at_index(x1_arr, i, &ctype_in);
9037 flipped_container =
9038 container_not_range(container_to_flip, ctype_in, (uint32_t)lb_start,
9039 (uint32_t)(lb_end + 1), &ctype_out);
9040
9041 if (container_get_cardinality(flipped_container, ctype_out))
9042 ra_insert_new_key_value_at(ans_arr, -j - 1, hb, flipped_container,
9043 ctype_out);
9044 else {
9045 container_free(flipped_container, ctype_out);
9046 }
9047 } else {
9048 flipped_container = container_range_of_ones(
9049 (uint32_t)lb_start, (uint32_t)(lb_end + 1), &ctype_out);
9050 ra_insert_new_key_value_at(ans_arr, -j - 1, hb, flipped_container,
9051 ctype_out);
9052 }
9053 }
9054
9055 static void inplace_flip_container(roaring_array_t *x1_arr, uint16_t hb,
9056 uint16_t lb_start, uint16_t lb_end) {
9057 const int i = ra_get_index(x1_arr, hb);
9058 uint8_t ctype_in, ctype_out;
9059 void *flipped_container = NULL;
9060 if (i >= 0) {
9061 void *container_to_flip =
9062 ra_get_container_at_index(x1_arr, i, &ctype_in);
9063 flipped_container = container_inot_range(
9064 container_to_flip, ctype_in, (uint32_t)lb_start,
9065 (uint32_t)(lb_end + 1), &ctype_out);
9066 // if a new container was created, the old one was already freed
9067 if (container_get_cardinality(flipped_container, ctype_out)) {
9068 ra_set_container_at_index(x1_arr, i, flipped_container, ctype_out);
9069 } else {
9070 container_free(flipped_container, ctype_out);
9071 ra_remove_at_index(x1_arr, i);
9072 }
9073
9074 } else {
9075 flipped_container = container_range_of_ones(
9076 (uint32_t)lb_start, (uint32_t)(lb_end + 1), &ctype_out);
9077 ra_insert_new_key_value_at(x1_arr, -i - 1, hb, flipped_container,
9078 ctype_out);
9079 }
9080 }
9081
9082 static void insert_fully_flipped_container(roaring_array_t *ans_arr,
9083 const roaring_array_t *x1_arr,
9084 uint16_t hb) {
9085 const int i = ra_get_index(x1_arr, hb);
9086 const int j = ra_get_index(ans_arr, hb);
9087 uint8_t ctype_in, ctype_out;
9088 void *flipped_container = NULL;
9089 if (i >= 0) {
9090 void *container_to_flip =
9091 ra_get_container_at_index(x1_arr, i, &ctype_in);
9092 flipped_container =
9093 container_not(container_to_flip, ctype_in, &ctype_out);
9094 if (container_get_cardinality(flipped_container, ctype_out))
9095 ra_insert_new_key_value_at(ans_arr, -j - 1, hb, flipped_container,
9096 ctype_out);
9097 else {
9098 container_free(flipped_container, ctype_out);
9099 }
9100 } else {
9101 flipped_container = container_range_of_ones(0U, 0x10000U, &ctype_out);
9102 ra_insert_new_key_value_at(ans_arr, -j - 1, hb, flipped_container,
9103 ctype_out);
9104 }
9105 }
9106
9107 static void inplace_fully_flip_container(roaring_array_t *x1_arr, uint16_t hb) {
9108 const int i = ra_get_index(x1_arr, hb);
9109 uint8_t ctype_in, ctype_out;
9110 void *flipped_container = NULL;
9111 if (i >= 0) {
9112 void *container_to_flip =
9113 ra_get_container_at_index(x1_arr, i, &ctype_in);
9114 flipped_container =
9115 container_inot(container_to_flip, ctype_in, &ctype_out);
9116
9117 if (container_get_cardinality(flipped_container, ctype_out)) {
9118 ra_set_container_at_index(x1_arr, i, flipped_container, ctype_out);
9119 } else {
9120 container_free(flipped_container, ctype_out);
9121 ra_remove_at_index(x1_arr, i);
9122 }
9123
9124 } else {
9125 flipped_container = container_range_of_ones(0U, 0x10000U, &ctype_out);
9126 ra_insert_new_key_value_at(x1_arr, -i - 1, hb, flipped_container,
9127 ctype_out);
9128 }
9129 }
9130
9131 roaring_bitmap_t *roaring_bitmap_flip(const roaring_bitmap_t *x1,
9132 uint64_t range_start,
9133 uint64_t range_end) {
9134 if (range_start >= range_end) {
9135 return roaring_bitmap_copy(x1);
9136 }
9137 if(range_end >= UINT64_C(0x100000000)) {
9138 range_end = UINT64_C(0x100000000);
9139 }
9140
9141 roaring_bitmap_t *ans = roaring_bitmap_create();
9142 ans->copy_on_write = x1->copy_on_write;
9143
9144 uint16_t hb_start = (uint16_t)(range_start >> 16);
9145 const uint16_t lb_start = (uint16_t)range_start; // & 0xFFFF;
9146 uint16_t hb_end = (uint16_t)((range_end - 1) >> 16);
9147 const uint16_t lb_end = (uint16_t)(range_end - 1); // & 0xFFFF;
9148
9149 ra_append_copies_until(&ans->high_low_container, &x1->high_low_container,
9150 hb_start, x1->copy_on_write);
9151 if (hb_start == hb_end) {
9152 insert_flipped_container(&ans->high_low_container,
9153 &x1->high_low_container, hb_start, lb_start,
9154 lb_end);
9155 } else {
9156 // start and end containers are distinct
9157 if (lb_start > 0) {
9158 // handle first (partial) container
9159 insert_flipped_container(&ans->high_low_container,
9160 &x1->high_low_container, hb_start,
9161 lb_start, 0xFFFF);
9162 ++hb_start; // for the full containers. Can't wrap.
9163 }
9164
9165 if (lb_end != 0xFFFF) --hb_end; // later we'll handle the partial block
9166
9167 for (uint32_t hb = hb_start; hb <= hb_end; ++hb) {
9168 insert_fully_flipped_container(&ans->high_low_container,
9169 &x1->high_low_container, hb);
9170 }
9171
9172 // handle a partial final container
9173 if (lb_end != 0xFFFF) {
9174 insert_flipped_container(&ans->high_low_container,
9175 &x1->high_low_container, hb_end + 1, 0,
9176 lb_end);
9177 ++hb_end;
9178 }
9179 }
9180 ra_append_copies_after(&ans->high_low_container, &x1->high_low_container,
9181 hb_end, x1->copy_on_write);
9182 return ans;
9183 }
9184
9185 void roaring_bitmap_flip_inplace(roaring_bitmap_t *x1, uint64_t range_start,
9186 uint64_t range_end) {
9187 if (range_start >= range_end) {
9188 return; // empty range
9189 }
9190 if(range_end >= UINT64_C(0x100000000)) {
9191 range_end = UINT64_C(0x100000000);
9192 }
9193
9194 uint16_t hb_start = (uint16_t)(range_start >> 16);
9195 const uint16_t lb_start = (uint16_t)range_start;
9196 uint16_t hb_end = (uint16_t)((range_end - 1) >> 16);
9197 const uint16_t lb_end = (uint16_t)(range_end - 1);
9198
9199 if (hb_start == hb_end) {
9200 inplace_flip_container(&x1->high_low_container, hb_start, lb_start,
9201 lb_end);
9202 } else {
9203 // start and end containers are distinct
9204 if (lb_start > 0) {
9205 // handle first (partial) container
9206 inplace_flip_container(&x1->high_low_container, hb_start, lb_start,
9207 0xFFFF);
9208 ++hb_start; // for the full containers. Can't wrap.
9209 }
9210
9211 if (lb_end != 0xFFFF) --hb_end;
9212
9213 for (uint32_t hb = hb_start; hb <= hb_end; ++hb) {
9214 inplace_fully_flip_container(&x1->high_low_container, hb);
9215 }
9216 // handle a partial final container
9217 if (lb_end != 0xFFFF) {
9218 inplace_flip_container(&x1->high_low_container, hb_end + 1, 0,
9219 lb_end);
9220 ++hb_end;
9221 }
9222 }
9223 }
9224
9225 roaring_bitmap_t *roaring_bitmap_lazy_or(const roaring_bitmap_t *x1,
9226 const roaring_bitmap_t *x2,
9227 const bool bitsetconversion) {
9228 uint8_t container_result_type = 0;
9229 const int length1 = x1->high_low_container.size,
9230 length2 = x2->high_low_container.size;
9231 if (0 == length1) {
9232 return roaring_bitmap_copy(x2);
9233 }
9234 if (0 == length2) {
9235 return roaring_bitmap_copy(x1);
9236 }
9237 roaring_bitmap_t *answer =
9238 roaring_bitmap_create_with_capacity(length1 + length2);
9239 answer->copy_on_write = x1->copy_on_write && x2->copy_on_write;
9240 int pos1 = 0, pos2 = 0;
9241 uint8_t container_type_1, container_type_2;
9242 uint16_t s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
9243 uint16_t s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
9244 while (true) {
9245 if (s1 == s2) {
9246 void *c1 = ra_get_container_at_index(&x1->high_low_container, pos1,
9247 &container_type_1);
9248 void *c2 = ra_get_container_at_index(&x2->high_low_container, pos2,
9249 &container_type_2);
9250 void *c;
9251 if (bitsetconversion && (get_container_type(c1, container_type_1) !=
9252 BITSET_CONTAINER_TYPE_CODE) &&
9253 (get_container_type(c2, container_type_2) !=
9254 BITSET_CONTAINER_TYPE_CODE)) {
9255 void *newc1 =
9256 container_mutable_unwrap_shared(c1, &container_type_1);
9257 newc1 = container_to_bitset(newc1, container_type_1);
9258 container_type_1 = BITSET_CONTAINER_TYPE_CODE;
9259 c = container_lazy_ior(newc1, container_type_1, c2,
9260 container_type_2,
9261 &container_result_type);
9262 if (c != newc1) { // should not happen
9263 container_free(newc1, container_type_1);
9264 }
9265 } else {
9266 c = container_lazy_or(c1, container_type_1, c2,
9267 container_type_2, &container_result_type);
9268 }
9269 // since we assume that the initial containers are non-empty,
9270 // the
9271 // result here
9272 // can only be non-empty
9273 ra_append(&answer->high_low_container, s1, c,
9274 container_result_type);
9275 ++pos1;
9276 ++pos2;
9277 if (pos1 == length1) break;
9278 if (pos2 == length2) break;
9279 s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
9280 s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
9281
9282 } else if (s1 < s2) { // s1 < s2
9283 void *c1 = ra_get_container_at_index(&x1->high_low_container, pos1,
9284 &container_type_1);
9285 c1 =
9286 get_copy_of_container(c1, &container_type_1, x1->copy_on_write);
9287 if (x1->copy_on_write) {
9288 ra_set_container_at_index(&x1->high_low_container, pos1, c1,
9289 container_type_1);
9290 }
9291 ra_append(&answer->high_low_container, s1, c1, container_type_1);
9292 pos1++;
9293 if (pos1 == length1) break;
9294 s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
9295
9296 } else { // s1 > s2
9297 void *c2 = ra_get_container_at_index(&x2->high_low_container, pos2,
9298 &container_type_2);
9299 c2 =
9300 get_copy_of_container(c2, &container_type_2, x2->copy_on_write);
9301 if (x2->copy_on_write) {
9302 ra_set_container_at_index(&x2->high_low_container, pos2, c2,
9303 container_type_2);
9304 }
9305 ra_append(&answer->high_low_container, s2, c2, container_type_2);
9306 pos2++;
9307 if (pos2 == length2) break;
9308 s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
9309 }
9310 }
9311 if (pos1 == length1) {
9312 ra_append_copy_range(&answer->high_low_container,
9313 &x2->high_low_container, pos2, length2,
9314 x2->copy_on_write);
9315 } else if (pos2 == length2) {
9316 ra_append_copy_range(&answer->high_low_container,
9317 &x1->high_low_container, pos1, length1,
9318 x1->copy_on_write);
9319 }
9320 return answer;
9321 }
9322
9323 void roaring_bitmap_lazy_or_inplace(roaring_bitmap_t *x1,
9324 const roaring_bitmap_t *x2,
9325 const bool bitsetconversion) {
9326 uint8_t container_result_type = 0;
9327 int length1 = x1->high_low_container.size;
9328 const int length2 = x2->high_low_container.size;
9329
9330 if (0 == length2) return;
9331
9332 if (0 == length1) {
9333 roaring_bitmap_overwrite(x1, x2);
9334 return;
9335 }
9336 int pos1 = 0, pos2 = 0;
9337 uint8_t container_type_1, container_type_2;
9338 uint16_t s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
9339 uint16_t s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
9340 while (true) {
9341 if (s1 == s2) {
9342 void *c1 = ra_get_container_at_index(&x1->high_low_container, pos1,
9343 &container_type_1);
9344 if (!container_is_full(c1, container_type_1)) {
9345 if ((bitsetconversion == false) ||
9346 (get_container_type(c1, container_type_1) ==
9347 BITSET_CONTAINER_TYPE_CODE)) {
9348 c1 = get_writable_copy_if_shared(c1, &container_type_1);
9349 } else {
9350 // convert to bitset
9351 void *oldc1 = c1;
9352 uint8_t oldt1 = container_type_1;
9353 c1 = container_mutable_unwrap_shared(c1, &container_type_1);
9354 c1 = container_to_bitset(c1, container_type_1);
9355 container_free(oldc1, oldt1);
9356 container_type_1 = BITSET_CONTAINER_TYPE_CODE;
9357 }
9358
9359 void *c2 = ra_get_container_at_index(&x2->high_low_container,
9360 pos2, &container_type_2);
9361 void *c = container_lazy_ior(c1, container_type_1, c2,
9362 container_type_2,
9363 &container_result_type);
9364 if (c !=
9365 c1) { // in this instance a new container was created, and
9366 // we need to free the old one
9367 container_free(c1, container_type_1);
9368 }
9369
9370 ra_set_container_at_index(&x1->high_low_container, pos1, c,
9371 container_result_type);
9372 }
9373 ++pos1;
9374 ++pos2;
9375 if (pos1 == length1) break;
9376 if (pos2 == length2) break;
9377 s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
9378 s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
9379
9380 } else if (s1 < s2) { // s1 < s2
9381 pos1++;
9382 if (pos1 == length1) break;
9383 s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
9384
9385 } else { // s1 > s2
9386 void *c2 = ra_get_container_at_index(&x2->high_low_container, pos2,
9387 &container_type_2);
9388 // void *c2_clone = container_clone(c2, container_type_2);
9389 c2 =
9390 get_copy_of_container(c2, &container_type_2, x2->copy_on_write);
9391 if (x2->copy_on_write) {
9392 ra_set_container_at_index(&x2->high_low_container, pos2, c2,
9393 container_type_2);
9394 }
9395 ra_insert_new_key_value_at(&x1->high_low_container, pos1, s2, c2,
9396 container_type_2);
9397 pos1++;
9398 length1++;
9399 pos2++;
9400 if (pos2 == length2) break;
9401 s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
9402 }
9403 }
9404 if (pos1 == length1) {
9405 ra_append_copy_range(&x1->high_low_container, &x2->high_low_container,
9406 pos2, length2, x2->copy_on_write);
9407 }
9408 }
9409
9410 roaring_bitmap_t *roaring_bitmap_lazy_xor(const roaring_bitmap_t *x1,
9411 const roaring_bitmap_t *x2) {
9412 uint8_t container_result_type = 0;
9413 const int length1 = x1->high_low_container.size,
9414 length2 = x2->high_low_container.size;
9415 if (0 == length1) {
9416 return roaring_bitmap_copy(x2);
9417 }
9418 if (0 == length2) {
9419 return roaring_bitmap_copy(x1);
9420 }
9421 roaring_bitmap_t *answer =
9422 roaring_bitmap_create_with_capacity(length1 + length2);
9423 answer->copy_on_write = x1->copy_on_write && x2->copy_on_write;
9424 int pos1 = 0, pos2 = 0;
9425 uint8_t container_type_1, container_type_2;
9426 uint16_t s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
9427 uint16_t s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
9428 while (true) {
9429 if (s1 == s2) {
9430 void *c1 = ra_get_container_at_index(&x1->high_low_container, pos1,
9431 &container_type_1);
9432 void *c2 = ra_get_container_at_index(&x2->high_low_container, pos2,
9433 &container_type_2);
9434 void *c =
9435 container_lazy_xor(c1, container_type_1, c2, container_type_2,
9436 &container_result_type);
9437
9438 if (container_nonzero_cardinality(c, container_result_type)) {
9439 ra_append(&answer->high_low_container, s1, c,
9440 container_result_type);
9441 } else {
9442 container_free(c, container_result_type);
9443 }
9444
9445 ++pos1;
9446 ++pos2;
9447 if (pos1 == length1) break;
9448 if (pos2 == length2) break;
9449 s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
9450 s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
9451
9452 } else if (s1 < s2) { // s1 < s2
9453 void *c1 = ra_get_container_at_index(&x1->high_low_container, pos1,
9454 &container_type_1);
9455 c1 =
9456 get_copy_of_container(c1, &container_type_1, x1->copy_on_write);
9457 if (x1->copy_on_write) {
9458 ra_set_container_at_index(&x1->high_low_container, pos1, c1,
9459 container_type_1);
9460 }
9461 ra_append(&answer->high_low_container, s1, c1, container_type_1);
9462 pos1++;
9463 if (pos1 == length1) break;
9464 s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
9465
9466 } else { // s1 > s2
9467 void *c2 = ra_get_container_at_index(&x2->high_low_container, pos2,
9468 &container_type_2);
9469 c2 =
9470 get_copy_of_container(c2, &container_type_2, x2->copy_on_write);
9471 if (x2->copy_on_write) {
9472 ra_set_container_at_index(&x2->high_low_container, pos2, c2,
9473 container_type_2);
9474 }
9475 ra_append(&answer->high_low_container, s2, c2, container_type_2);
9476 pos2++;
9477 if (pos2 == length2) break;
9478 s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
9479 }
9480 }
9481 if (pos1 == length1) {
9482 ra_append_copy_range(&answer->high_low_container,
9483 &x2->high_low_container, pos2, length2,
9484 x2->copy_on_write);
9485 } else if (pos2 == length2) {
9486 ra_append_copy_range(&answer->high_low_container,
9487 &x1->high_low_container, pos1, length1,
9488 x1->copy_on_write);
9489 }
9490 return answer;
9491 }
9492
9493 void roaring_bitmap_lazy_xor_inplace(roaring_bitmap_t *x1,
9494 const roaring_bitmap_t *x2) {
9495 assert(x1 != x2);
9496 uint8_t container_result_type = 0;
9497 int length1 = x1->high_low_container.size;
9498 const int length2 = x2->high_low_container.size;
9499
9500 if (0 == length2) return;
9501
9502 if (0 == length1) {
9503 roaring_bitmap_overwrite(x1, x2);
9504 return;
9505 }
9506 int pos1 = 0, pos2 = 0;
9507 uint8_t container_type_1, container_type_2;
9508 uint16_t s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
9509 uint16_t s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
9510 while (true) {
9511 if (s1 == s2) {
9512 void *c1 = ra_get_container_at_index(&x1->high_low_container, pos1,
9513 &container_type_1);
9514 c1 = get_writable_copy_if_shared(c1, &container_type_1);
9515 void *c2 = ra_get_container_at_index(&x2->high_low_container, pos2,
9516 &container_type_2);
9517 void *c =
9518 container_lazy_ixor(c1, container_type_1, c2, container_type_2,
9519 &container_result_type);
9520 if (container_nonzero_cardinality(c, container_result_type)) {
9521 ra_set_container_at_index(&x1->high_low_container, pos1, c,
9522 container_result_type);
9523 ++pos1;
9524 } else {
9525 container_free(c, container_result_type);
9526 ra_remove_at_index(&x1->high_low_container, pos1);
9527 --length1;
9528 }
9529 ++pos2;
9530 if (pos1 == length1) break;
9531 if (pos2 == length2) break;
9532 s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
9533 s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
9534
9535 } else if (s1 < s2) { // s1 < s2
9536 pos1++;
9537 if (pos1 == length1) break;
9538 s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
9539
9540 } else { // s1 > s2
9541 void *c2 = ra_get_container_at_index(&x2->high_low_container, pos2,
9542 &container_type_2);
9543 // void *c2_clone = container_clone(c2, container_type_2);
9544 c2 =
9545 get_copy_of_container(c2, &container_type_2, x2->copy_on_write);
9546 if (x2->copy_on_write) {
9547 ra_set_container_at_index(&x2->high_low_container, pos2, c2,
9548 container_type_2);
9549 }
9550 ra_insert_new_key_value_at(&x1->high_low_container, pos1, s2, c2,
9551 container_type_2);
9552 pos1++;
9553 length1++;
9554 pos2++;
9555 if (pos2 == length2) break;
9556 s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
9557 }
9558 }
9559 if (pos1 == length1) {
9560 ra_append_copy_range(&x1->high_low_container, &x2->high_low_container,
9561 pos2, length2, x2->copy_on_write);
9562 }
9563 }
9564
9565 void roaring_bitmap_repair_after_lazy(roaring_bitmap_t *ra) {
9566 for (int i = 0; i < ra->high_low_container.size; ++i) {
9567 const uint8_t original_typecode = ra->high_low_container.typecodes[i];
9568 void *container = ra->high_low_container.containers[i];
9569 uint8_t new_typecode = original_typecode;
9570 void *newcontainer =
9571 container_repair_after_lazy(container, &new_typecode);
9572 ra->high_low_container.containers[i] = newcontainer;
9573 ra->high_low_container.typecodes[i] = new_typecode;
9574 }
9575 }
9576
9577
9578
9579 /**
9580 * roaring_bitmap_rank returns the number of integers that are smaller or equal
9581 * to x.
9582 */
9583 uint64_t roaring_bitmap_rank(const roaring_bitmap_t *bm, uint32_t x) {
9584 uint64_t size = 0;
9585 uint32_t xhigh = x >> 16;
9586 for (int i = 0; i < bm->high_low_container.size; i++) {
9587 uint32_t key = bm->high_low_container.keys[i];
9588 if (xhigh > key) {
9589 size +=
9590 container_get_cardinality(bm->high_low_container.containers[i],
9591 bm->high_low_container.typecodes[i]);
9592 } else if (xhigh == key) {
9593 return size + container_rank(bm->high_low_container.containers[i],
9594 bm->high_low_container.typecodes[i],
9595 x & 0xFFFF);
9596 } else {
9597 return size;
9598 }
9599 }
9600 return size;
9601 }
9602
9603 /**
9604 * roaring_bitmap_smallest returns the smallest value in the set.
9605 * Returns UINT32_MAX if the set is empty.
9606 */
9607 uint32_t roaring_bitmap_minimum(const roaring_bitmap_t *bm) {
9608 if (bm->high_low_container.size > 0) {
9609 void *container = bm->high_low_container.containers[0];
9610 uint8_t typecode = bm->high_low_container.typecodes[0];
9611 uint32_t key = bm->high_low_container.keys[0];
9612 uint32_t lowvalue = container_minimum(container, typecode);
9613 return lowvalue | (key << 16);
9614 }
9615 return UINT32_MAX;
9616 }
9617
9618 /**
9619 * roaring_bitmap_smallest returns the greatest value in the set.
9620 * Returns 0 if the set is empty.
9621 */
9622 uint32_t roaring_bitmap_maximum(const roaring_bitmap_t *bm) {
9623 if (bm->high_low_container.size > 0) {
9624 void *container =
9625 bm->high_low_container.containers[bm->high_low_container.size - 1];
9626 uint8_t typecode =
9627 bm->high_low_container.typecodes[bm->high_low_container.size - 1];
9628 uint32_t key =
9629 bm->high_low_container.keys[bm->high_low_container.size - 1];
9630 uint32_t lowvalue = container_maximum(container, typecode);
9631 return lowvalue | (key << 16);
9632 }
9633 return 0;
9634 }
9635
9636 bool roaring_bitmap_select(const roaring_bitmap_t *bm, uint32_t rank,
9637 uint32_t *element) {
9638 void *container;
9639 uint8_t typecode;
9640 uint16_t key;
9641 uint32_t start_rank = 0;
9642 int i = 0;
9643 bool valid = false;
9644 while (!valid && i < bm->high_low_container.size) {
9645 container = bm->high_low_container.containers[i];
9646 typecode = bm->high_low_container.typecodes[i];
9647 valid =
9648 container_select(container, typecode, &start_rank, rank, element);
9649 i++;
9650 }
9651
9652 if (valid) {
9653 key = bm->high_low_container.keys[i - 1];
9654 *element |= (key << 16);
9655 return true;
9656 } else
9657 return false;
9658 }
9659
9660 roaring_bitmap_t *roaring_bitmap_subsample(const roaring_bitmap_t *x, const uint64_t nb) {
9661
9662 void *container;
9663 uint8_t typecode;
9664 uint16_t key;
9665 uint32_t start_rank = 0, prev_start_rank = 0;
9666 uint32_t element;
9667
9668 roaring_bitmap_t *sampled_ids = roaring_bitmap_create();
9669
9670 if (nb == 0) return sampled_ids;
9671
9672 roaring_bitmap_t *sampled_pos = roaring_bitmap_create();
9673
9674 uint64_t card = roaring_bitmap_get_cardinality(x);
9675
9676 const uint64_t seed = rand();
9677
9678 for (uint64_t i = 0; i < nb; ++i) {
9679
9680 // wyhash pseudo-random number generator
9681 // from https://github.com/lemire/testingRNG/blob/master/source/wyhash.h
9682 // because just calling rand() is so f****** slow
9683 uint64_t l_seed = seed + UINT64_C(0x60bee2bee120fc15);
9684 __uint128_t tmp;
9685 tmp = (__uint128_t)l_seed * UINT64_C(0xa3b195354a39b70d);
9686 uint64_t m1 = (tmp >> 64) ^ tmp;
9687 tmp = (__uint128_t)m1 * UINT64_C(0x1b03738712fad5c9);
9688 uint64_t m2 = (tmp >> 64) ^ tmp;
9689
9690 roaring_bitmap_add(sampled_pos, (uint32_t)(m2 % card));
9691 }
9692
9693 roaring_uint32_iterator_t *it = roaring_create_iterator(sampled_pos);
9694
9695 int i = 0;
9696
9697 while (it->has_value && (i < x->high_low_container.size)) {
9698
9699 container = x->high_low_container.containers[i];
9700 typecode = x->high_low_container.typecodes[i];
9701
9702 if (container_select(container, typecode, &start_rank, it->current_value, &element)) {
9703
9704 element |= (x->high_low_container.keys[i] << 16);
9705 start_rank = prev_start_rank;
9706
9707 roaring_bitmap_add(sampled_ids, element);
9708 roaring_advance_uint32_iterator(it);
9709 }
9710 else {
9711
9712 i++;
9713 prev_start_rank = start_rank;
9714 }
9715 }
9716
9717 roaring_free_uint32_iterator(it);
9718 roaring_bitmap_free(sampled_pos);
9719
9720 return sampled_ids;
9721 }
9722
9723 bool roaring_bitmap_intersect(const roaring_bitmap_t *x1,
9724 const roaring_bitmap_t *x2) {
9725 const int length1 = x1->high_low_container.size,
9726 length2 = x2->high_low_container.size;
9727 uint64_t answer = 0;
9728 int pos1 = 0, pos2 = 0;
9729
9730 while (pos1 < length1 && pos2 < length2) {
9731 const uint16_t s1 = ra_get_key_at_index(& x1->high_low_container, pos1);
9732 const uint16_t s2 = ra_get_key_at_index(& x2->high_low_container, pos2);
9733
9734 if (s1 == s2) {
9735 uint8_t container_type_1, container_type_2;
9736 void *c1 = ra_get_container_at_index(& x1->high_low_container, pos1,
9737 &container_type_1);
9738 void *c2 = ra_get_container_at_index(& x2->high_low_container, pos2,
9739 &container_type_2);
9740 if( container_intersect(c1, container_type_1, c2, container_type_2) ) return true;
9741 ++pos1;
9742 ++pos2;
9743 } else if (s1 < s2) { // s1 < s2
9744 pos1 = ra_advance_until(& x1->high_low_container, s2, pos1);
9745 } else { // s1 > s2
9746 pos2 = ra_advance_until(& x2->high_low_container, s1, pos2);
9747 }
9748 }
9749 return answer;
9750 }
9751
9752 uint64_t roaring_bitmap_and_cardinality(const roaring_bitmap_t *x1,
9753 const roaring_bitmap_t *x2) {
9754 const int length1 = x1->high_low_container.size,
9755 length2 = x2->high_low_container.size;
9756 uint64_t answer = 0;
9757 int pos1 = 0, pos2 = 0;
9758
9759 while (pos1 < length1 && pos2 < length2) {
9760 const uint16_t s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
9761 const uint16_t s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
9762
9763 if (s1 == s2) {
9764 uint8_t container_type_1, container_type_2;
9765 void *c1 = ra_get_container_at_index(&x1->high_low_container, pos1,
9766 &container_type_1);
9767 void *c2 = ra_get_container_at_index(&x2->high_low_container, pos2,
9768 &container_type_2);
9769 answer += container_and_cardinality(c1, container_type_1, c2,
9770 container_type_2);
9771 ++pos1;
9772 ++pos2;
9773 } else if (s1 < s2) { // s1 < s2
9774 pos1 = ra_advance_until(&x1->high_low_container, s2, pos1);
9775 } else { // s1 > s2
9776 pos2 = ra_advance_until(&x2->high_low_container, s1, pos2);
9777 }
9778 }
9779 return answer;
9780 }
9781
9782 uint64_t roaring_bitmap_and_min_cardinality(const roaring_bitmap_t *x1,
9783 const roaring_bitmap_t *x2,
9784 const uint64_t min_shared) {
9785 const int length1 = x1->high_low_container.size,
9786 length2 = x2->high_low_container.size;
9787 uint64_t answer = 0;
9788 int pos1 = 0, pos2 = 0;
9789
9790 while (pos1 < length1 && pos2 < length2 && answer < min_shared) {
9791 const uint16_t s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
9792 const uint16_t s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
9793
9794 if (s1 == s2) {
9795 uint8_t container_type_1, container_type_2;
9796 void *c1 = ra_get_container_at_index(&x1->high_low_container, pos1,
9797 &container_type_1);
9798 void *c2 = ra_get_container_at_index(&x2->high_low_container, pos2,
9799 &container_type_2);
9800 answer += container_and_cardinality(c1, container_type_1, c2,
9801 container_type_2);
9802 ++pos1;
9803 ++pos2;
9804 } else if (s1 < s2) { // s1 < s2
9805 pos1 = ra_advance_until(&x1->high_low_container, s2, pos1);
9806 } else { // s1 > s2
9807 pos2 = ra_advance_until(&x2->high_low_container, s1, pos2);
9808 }
9809 }
9810 return answer;
9811 }
9812
9813 double roaring_bitmap_jaccard_index(const roaring_bitmap_t *x1,
9814 const roaring_bitmap_t *x2) {
9815 const uint64_t c1 = roaring_bitmap_get_cardinality(x1);
9816 const uint64_t c2 = roaring_bitmap_get_cardinality(x2);
9817 const uint64_t inter = roaring_bitmap_and_cardinality(x1, x2);
9818 return (double)inter / (double)(c1 + c2 - inter);
9819 }
9820
9821 uint64_t roaring_bitmap_or_cardinality(const roaring_bitmap_t *x1,
9822 const roaring_bitmap_t *x2) {
9823 const uint64_t c1 = roaring_bitmap_get_cardinality(x1);
9824 const uint64_t c2 = roaring_bitmap_get_cardinality(x2);
9825 const uint64_t inter = roaring_bitmap_and_cardinality(x1, x2);
9826 return c1 + c2 - inter;
9827 }
9828
9829 uint64_t roaring_bitmap_andnot_cardinality(const roaring_bitmap_t *x1,
9830 const roaring_bitmap_t *x2) {
9831 const uint64_t c1 = roaring_bitmap_get_cardinality(x1);
9832 const uint64_t inter = roaring_bitmap_and_cardinality(x1, x2);
9833 return c1 - inter;
9834 }
9835
9836 uint64_t roaring_bitmap_xor_cardinality(const roaring_bitmap_t *x1,
9837 const roaring_bitmap_t *x2) {
9838 const uint64_t c1 = roaring_bitmap_get_cardinality(x1);
9839 const uint64_t c2 = roaring_bitmap_get_cardinality(x2);
9840 const uint64_t inter = roaring_bitmap_and_cardinality(x1, x2);
9841 return c1 + c2 - 2 * inter;
9842 }
9843
9844
9845 /**
9846 * Check whether a range of values from range_start (included) to range_end (excluded) is present
9847 */
9848 bool roaring_bitmap_contains_range(const roaring_bitmap_t *r, uint64_t range_start, uint64_t range_end) {
9849 if(range_end >= UINT64_C(0x100000000)) {
9850 range_end = UINT64_C(0x100000000);
9851 }
9852 if (range_start >= range_end) return true; // empty range are always contained!
9853 if (range_end - range_start == 1) return roaring_bitmap_contains(r, range_start);
9854 uint16_t hb_rs = (uint16_t)(range_start >> 16);
9855 uint16_t hb_re = (uint16_t)((range_end - 1) >> 16);
9856 const int32_t span = hb_re - hb_rs;
9857 const int32_t hlc_sz = ra_get_size(&r->high_low_container);
9858 if (hlc_sz < span + 1) {
9859 return false;
9860 }
9861 int32_t is = ra_get_index(&r->high_low_container, hb_rs);
9862 int32_t ie = ra_get_index(&r->high_low_container, hb_re);
9863 ie = (ie < 0 ? -ie - 1 : ie);
9864 if ((is < 0) || ((ie - is) != span)) {
9865 return false;
9866 }
9867 const uint32_t lb_rs = range_start & 0xFFFF;
9868 const uint32_t lb_re = ((range_end - 1) & 0xFFFF) + 1;
9869 uint8_t typecode;
9870 void *container = ra_get_container_at_index(&r->high_low_container, is, &typecode);
9871 if (hb_rs == hb_re) {
9872 return container_contains_range(container, lb_rs, lb_re, typecode);
9873 }
9874 if (!container_contains_range(container, lb_rs, 1 << 16, typecode)) {
9875 return false;
9876 }
9877 assert(ie < hlc_sz); // would indicate an algorithmic bug
9878 container = ra_get_container_at_index(&r->high_low_container, ie, &typecode);
9879 if (!container_contains_range(container, 0, lb_re, typecode)) {
9880 return false;
9881 }
9882 for (int32_t i = is + 1; i < ie; ++i) {
9883 container = ra_get_container_at_index(&r->high_low_container, i, &typecode);
9884 if (!container_is_full(container, typecode) ) {
9885 return false;
9886 }
9887 }
9888 return true;
9889 }
9890
9891
9892 bool roaring_bitmap_is_strict_subset(const roaring_bitmap_t *ra1,
9893 const roaring_bitmap_t *ra2) {
9894 return (roaring_bitmap_get_cardinality(ra2) >
9895 roaring_bitmap_get_cardinality(ra1) &&
9896 roaring_bitmap_is_subset(ra1, ra2));
9897 }
9898 /* end file src/roaring.c */
9899 /* begin file src/roaring_array.c */
9900 #include <assert.h>
9901 #include <stdbool.h>
9902 #include <stdio.h>
9903 #include <stdlib.h>
9904 #include <string.h>
9905
9906
9907 // Convention: [0,ra->size) all elements are initialized
9908 // [ra->size, ra->allocation_size) is junk and contains nothing needing freeing
9909
9910 extern inline int32_t ra_get_size(const roaring_array_t *ra);
9911 extern inline int32_t ra_get_index(const roaring_array_t *ra, uint16_t x);
9912 extern inline void *ra_get_container_at_index(const roaring_array_t *ra,
9913 uint16_t i, uint8_t *typecode);
9914 extern inline void ra_unshare_container_at_index(roaring_array_t *ra,
9915 uint16_t i);
9916 extern inline void ra_replace_key_and_container_at_index(roaring_array_t *ra,
9917 int32_t i,
9918 uint16_t key, void *c,
9919 uint8_t typecode);
9920 extern inline void ra_set_container_at_index(const roaring_array_t *ra,
9921 int32_t i, void *c,
9922 uint8_t typecode);
9923
9924 #define INITIAL_CAPACITY 4
9925
9926 static bool realloc_array(roaring_array_t *ra, size_t new_capacity) {
9927 // because we combine the allocations, it is not possible to use realloc
9928 /*ra->keys =
9929 (uint16_t *)realloc(ra->keys, sizeof(uint16_t) * new_capacity);
9930 ra->containers =
9931 (void **)realloc(ra->containers, sizeof(void *) * new_capacity);
9932 ra->typecodes =
9933 (uint8_t *)realloc(ra->typecodes, sizeof(uint8_t) * new_capacity);
9934 if (!ra->keys || !ra->containers || !ra->typecodes) {
9935 free(ra->keys);
9936 free(ra->containers);
9937 free(ra->typecodes);
9938 return false;
9939 }*/
9940
9941 if ( new_capacity == 0 ) {
9942 free(ra->containers);
9943 ra->containers = NULL;
9944 ra->keys = NULL;
9945 ra->typecodes = NULL;
9946 ra->allocation_size = 0;
9947 return true;
9948 }
9949 const size_t memoryneeded =
9950 new_capacity * (sizeof(uint16_t) + sizeof(void *) + sizeof(uint8_t));
9951 void *bigalloc = malloc(memoryneeded);
9952 if (!bigalloc) return false;
9953 void *oldbigalloc = ra->containers;
9954 void **newcontainers = (void **)bigalloc;
9955 uint16_t *newkeys = (uint16_t *)(newcontainers + new_capacity);
9956 uint8_t *newtypecodes = (uint8_t *)(newkeys + new_capacity);
9957 assert((char *)(newtypecodes + new_capacity) ==
9958 (char *)bigalloc + memoryneeded);
9959 memcpy(newcontainers, ra->containers, sizeof(void *) * ra->size);
9960 memcpy(newkeys, ra->keys, sizeof(uint16_t) * ra->size);
9961 memcpy(newtypecodes, ra->typecodes, sizeof(uint8_t) * ra->size);
9962 ra->containers = newcontainers;
9963 ra->keys = newkeys;
9964 ra->typecodes = newtypecodes;
9965 ra->allocation_size = new_capacity;
9966 free(oldbigalloc);
9967 return true;
9968 }
9969
9970 bool ra_init_with_capacity(roaring_array_t *new_ra, uint32_t cap) {
9971 if (!new_ra) return false;
9972 new_ra->keys = NULL;
9973 new_ra->containers = NULL;
9974 new_ra->typecodes = NULL;
9975
9976 new_ra->allocation_size = cap;
9977 new_ra->size = 0;
9978 if(cap > 0) {
9979 void *bigalloc =
9980 malloc(cap * (sizeof(uint16_t) + sizeof(void *) + sizeof(uint8_t)));
9981 if( bigalloc == NULL ) return false;
9982 new_ra->containers = (void **)bigalloc;
9983 new_ra->keys = (uint16_t *)(new_ra->containers + cap);
9984 new_ra->typecodes = (uint8_t *)(new_ra->keys + cap);
9985 }
9986 return true;
9987 }
9988
9989 int ra_shrink_to_fit(roaring_array_t *ra) {
9990 int savings = (ra->allocation_size - ra->size) *
9991 (sizeof(uint16_t) + sizeof(void *) + sizeof(uint8_t));
9992 if (!realloc_array(ra, ra->size)) {
9993 return 0;
9994 }
9995 ra->allocation_size = ra->size;
9996 return savings;
9997 }
9998
9999 bool ra_init(roaring_array_t *t) {
10000 return ra_init_with_capacity(t, INITIAL_CAPACITY);
10001 }
10002
10003 bool ra_copy(const roaring_array_t *source, roaring_array_t *dest,
10004 bool copy_on_write) {
10005 if (!ra_init_with_capacity(dest, source->size)) return false;
10006 dest->size = source->size;
10007 dest->allocation_size = source->size;
10008 memcpy(dest->keys, source->keys, dest->size * sizeof(uint16_t));
10009 // we go through the containers, turning them into shared containers...
10010 if (copy_on_write) {
10011 for (int32_t i = 0; i < dest->size; ++i) {
10012 source->containers[i] = get_copy_of_container(
10013 source->containers[i], &source->typecodes[i], copy_on_write);
10014 }
10015 // we do a shallow copy to the other bitmap
10016 memcpy(dest->containers, source->containers,
10017 dest->size * sizeof(void *));
10018 memcpy(dest->typecodes, source->typecodes,
10019 dest->size * sizeof(uint8_t));
10020 } else {
10021 memcpy(dest->typecodes, source->typecodes,
10022 dest->size * sizeof(uint8_t));
10023 for (int32_t i = 0; i < dest->size; i++) {
10024 dest->containers[i] =
10025 container_clone(source->containers[i], source->typecodes[i]);
10026 if (dest->containers[i] == NULL) {
10027 for (int32_t j = 0; j < i; j++) {
10028 container_free(dest->containers[j], dest->typecodes[j]);
10029 }
10030 ra_clear_without_containers(dest);
10031 return false;
10032 }
10033 }
10034 }
10035 return true;
10036 }
10037
10038 bool ra_overwrite(const roaring_array_t *source, roaring_array_t *dest,
10039 bool copy_on_write) {
10040 ra_clear_containers(dest); // we are going to overwrite them
10041 if (dest->allocation_size < source->size) {
10042 if (!realloc_array(dest, source->size)) {
10043 return false;
10044 }
10045 }
10046 dest->size = source->size;
10047 memcpy(dest->keys, source->keys, dest->size * sizeof(uint16_t));
10048 // we go through the containers, turning them into shared containers...
10049 if (copy_on_write) {
10050 for (int32_t i = 0; i < dest->size; ++i) {
10051 source->containers[i] = get_copy_of_container(
10052 source->containers[i], &source->typecodes[i], copy_on_write);
10053 }
10054 // we do a shallow copy to the other bitmap
10055 memcpy(dest->containers, source->containers,
10056 dest->size * sizeof(void *));
10057 memcpy(dest->typecodes, source->typecodes,
10058 dest->size * sizeof(uint8_t));
10059 } else {
10060 memcpy(dest->typecodes, source->typecodes,
10061 dest->size * sizeof(uint8_t));
10062 for (int32_t i = 0; i < dest->size; i++) {
10063 dest->containers[i] =
10064 container_clone(source->containers[i], source->typecodes[i]);
10065 if (dest->containers[i] == NULL) {
10066 for (int32_t j = 0; j < i; j++) {
10067 container_free(dest->containers[j], dest->typecodes[j]);
10068 }
10069 ra_clear_without_containers(dest);
10070 return false;
10071 }
10072 }
10073 }
10074 return true;
10075 }
10076
10077 void ra_clear_containers(roaring_array_t *ra) {
10078 for (int32_t i = 0; i < ra->size; ++i) {
10079 container_free(ra->containers[i], ra->typecodes[i]);
10080 }
10081 }
10082
10083 void ra_reset(roaring_array_t *ra) {
10084 ra_clear_containers(ra);
10085 ra->size = 0;
10086 ra_shrink_to_fit(ra);
10087 }
10088
10089 void ra_clear_without_containers(roaring_array_t *ra) {
10090 free(ra->containers); // keys and typecodes are allocated with containers
10091 ra->size = 0;
10092 ra->allocation_size = 0;
10093 ra->containers = NULL;
10094 ra->keys = NULL;
10095 ra->typecodes = NULL;
10096 }
10097
10098 void ra_clear(roaring_array_t *ra) {
10099 ra_clear_containers(ra);
10100 ra_clear_without_containers(ra);
10101 }
10102
10103 bool extend_array(roaring_array_t *ra, int32_t k) {
10104 int32_t desired_size = ra->size + k;
10105 if (desired_size > ra->allocation_size) {
10106 size_t new_capacity =
10107 (ra->size < 1024) ? 2 * desired_size : 5 * desired_size / 4;
10108
10109 return realloc_array(ra, new_capacity);
10110 }
10111 return true;
10112 }
10113
10114 void ra_append(roaring_array_t *ra, uint16_t key, void *container,
10115 uint8_t typecode) {
10116 extend_array(ra, 1);
10117 const int32_t pos = ra->size;
10118
10119 ra->keys[pos] = key;
10120 ra->containers[pos] = container;
10121 ra->typecodes[pos] = typecode;
10122 ra->size++;
10123 }
10124
10125 void ra_append_copy(roaring_array_t *ra, const roaring_array_t *sa,
10126 uint16_t index, bool copy_on_write) {
10127 extend_array(ra, 1);
10128 const int32_t pos = ra->size;
10129
10130 // old contents is junk not needing freeing
10131 ra->keys[pos] = sa->keys[index];
10132 // the shared container will be in two bitmaps
10133 if (copy_on_write) {
10134 sa->containers[index] = get_copy_of_container(
10135 sa->containers[index], &sa->typecodes[index], copy_on_write);
10136 ra->containers[pos] = sa->containers[index];
10137 ra->typecodes[pos] = sa->typecodes[index];
10138 } else {
10139 ra->containers[pos] =
10140 container_clone(sa->containers[index], sa->typecodes[index]);
10141 ra->typecodes[pos] = sa->typecodes[index];
10142 }
10143 ra->size++;
10144 }
10145
10146 void ra_append_copies_until(roaring_array_t *ra, const roaring_array_t *sa,
10147 uint16_t stopping_key, bool copy_on_write) {
10148 for (int32_t i = 0; i < sa->size; ++i) {
10149 if (sa->keys[i] >= stopping_key) break;
10150 ra_append_copy(ra, sa, i, copy_on_write);
10151 }
10152 }
10153
10154 void ra_append_copy_range(roaring_array_t *ra, const roaring_array_t *sa,
10155 int32_t start_index, int32_t end_index,
10156 bool copy_on_write) {
10157 extend_array(ra, end_index - start_index);
10158 for (int32_t i = start_index; i < end_index; ++i) {
10159 const int32_t pos = ra->size;
10160 ra->keys[pos] = sa->keys[i];
10161 if (copy_on_write) {
10162 sa->containers[i] = get_copy_of_container(
10163 sa->containers[i], &sa->typecodes[i], copy_on_write);
10164 ra->containers[pos] = sa->containers[i];
10165 ra->typecodes[pos] = sa->typecodes[i];
10166 } else {
10167 ra->containers[pos] =
10168 container_clone(sa->containers[i], sa->typecodes[i]);
10169 ra->typecodes[pos] = sa->typecodes[i];
10170 }
10171 ra->size++;
10172 }
10173 }
10174
10175 void ra_append_copies_after(roaring_array_t *ra, const roaring_array_t *sa,
10176 uint16_t before_start, bool copy_on_write) {
10177 int start_location = ra_get_index(sa, before_start);
10178 if (start_location >= 0)
10179 ++start_location;
10180 else
10181 start_location = -start_location - 1;
10182 ra_append_copy_range(ra, sa, start_location, sa->size, copy_on_write);
10183 }
10184
10185 void ra_append_move_range(roaring_array_t *ra, roaring_array_t *sa,
10186 int32_t start_index, int32_t end_index) {
10187 extend_array(ra, end_index - start_index);
10188
10189 for (int32_t i = start_index; i < end_index; ++i) {
10190 const int32_t pos = ra->size;
10191
10192 ra->keys[pos] = sa->keys[i];
10193 ra->containers[pos] = sa->containers[i];
10194 ra->typecodes[pos] = sa->typecodes[i];
10195 ra->size++;
10196 }
10197 }
10198
10199 void ra_append_range(roaring_array_t *ra, roaring_array_t *sa,
10200 int32_t start_index, int32_t end_index,
10201 bool copy_on_write) {
10202 extend_array(ra, end_index - start_index);
10203
10204 for (int32_t i = start_index; i < end_index; ++i) {
10205 const int32_t pos = ra->size;
10206 ra->keys[pos] = sa->keys[i];
10207 if (copy_on_write) {
10208 sa->containers[i] = get_copy_of_container(
10209 sa->containers[i], &sa->typecodes[i], copy_on_write);
10210 ra->containers[pos] = sa->containers[i];
10211 ra->typecodes[pos] = sa->typecodes[i];
10212 } else {
10213 ra->containers[pos] =
10214 container_clone(sa->containers[i], sa->typecodes[i]);
10215 ra->typecodes[pos] = sa->typecodes[i];
10216 }
10217 ra->size++;
10218 }
10219 }
10220
10221 void *ra_get_container(roaring_array_t *ra, uint16_t x, uint8_t *typecode) {
10222 int i = binarySearch(ra->keys, (int32_t)ra->size, x);
10223 if (i < 0) return NULL;
10224 *typecode = ra->typecodes[i];
10225 return ra->containers[i];
10226 }
10227
10228 extern void *ra_get_container_at_index(const roaring_array_t *ra, uint16_t i,
10229 uint8_t *typecode);
10230
10231 void *ra_get_writable_container(roaring_array_t *ra, uint16_t x,
10232 uint8_t *typecode) {
10233 int i = binarySearch(ra->keys, (int32_t)ra->size, x);
10234 if (i < 0) return NULL;
10235 *typecode = ra->typecodes[i];
10236 return get_writable_copy_if_shared(ra->containers[i], typecode);
10237 }
10238
10239 void *ra_get_writable_container_at_index(roaring_array_t *ra, uint16_t i,
10240 uint8_t *typecode) {
10241 assert(i < ra->size);
10242 *typecode = ra->typecodes[i];
10243 return get_writable_copy_if_shared(ra->containers[i], typecode);
10244 }
10245
10246 uint16_t ra_get_key_at_index(const roaring_array_t *ra, uint16_t i) {
10247 return ra->keys[i];
10248 }
10249
10250 extern int32_t ra_get_index(const roaring_array_t *ra, uint16_t x);
10251
10252 extern int32_t ra_advance_until(const roaring_array_t *ra, uint16_t x,
10253 int32_t pos);
10254
10255 // everything skipped over is freed
10256 int32_t ra_advance_until_freeing(roaring_array_t *ra, uint16_t x, int32_t pos) {
10257 while (pos < ra->size && ra->keys[pos] < x) {
10258 container_free(ra->containers[pos], ra->typecodes[pos]);
10259 ++pos;
10260 }
10261 return pos;
10262 }
10263
10264 void ra_insert_new_key_value_at(roaring_array_t *ra, int32_t i, uint16_t key,
10265 void *container, uint8_t typecode) {
10266 extend_array(ra, 1);
10267 // May be an optimization opportunity with DIY memmove
10268 memmove(&(ra->keys[i + 1]), &(ra->keys[i]),
10269 sizeof(uint16_t) * (ra->size - i));
10270 memmove(&(ra->containers[i + 1]), &(ra->containers[i]),
10271 sizeof(void *) * (ra->size - i));
10272 memmove(&(ra->typecodes[i + 1]), &(ra->typecodes[i]),
10273 sizeof(uint8_t) * (ra->size - i));
10274 ra->keys[i] = key;
10275 ra->containers[i] = container;
10276 ra->typecodes[i] = typecode;
10277 ra->size++;
10278 }
10279
10280 // note: Java routine set things to 0, enabling GC.
10281 // Java called it "resize" but it was always used to downsize.
10282 // Allowing upsize would break the conventions about
10283 // valid containers below ra->size.
10284
10285 void ra_downsize(roaring_array_t *ra, int32_t new_length) {
10286 assert(new_length <= ra->size);
10287 ra->size = new_length;
10288 }
10289
10290 void ra_remove_at_index(roaring_array_t *ra, int32_t i) {
10291 memmove(&(ra->containers[i]), &(ra->containers[i + 1]),
10292 sizeof(void *) * (ra->size - i - 1));
10293 memmove(&(ra->keys[i]), &(ra->keys[i + 1]),
10294 sizeof(uint16_t) * (ra->size - i - 1));
10295 memmove(&(ra->typecodes[i]), &(ra->typecodes[i + 1]),
10296 sizeof(uint8_t) * (ra->size - i - 1));
10297 ra->size--;
10298 }
10299
10300 void ra_remove_at_index_and_free(roaring_array_t *ra, int32_t i) {
10301 container_free(ra->containers[i], ra->typecodes[i]);
10302 ra_remove_at_index(ra, i);
10303 }
10304
10305 // used in inplace andNot only, to slide left the containers from
10306 // the mutated RoaringBitmap that are after the largest container of
10307 // the argument RoaringBitmap. In use it should be followed by a call to
10308 // downsize.
10309 //
10310 void ra_copy_range(roaring_array_t *ra, uint32_t begin, uint32_t end,
10311 uint32_t new_begin) {
10312 assert(begin <= end);
10313 assert(new_begin < begin);
10314
10315 const int range = end - begin;
10316
10317 // We ensure to previously have freed overwritten containers
10318 // that are not copied elsewhere
10319
10320 memmove(&(ra->containers[new_begin]), &(ra->containers[begin]),
10321 sizeof(void *) * range);
10322 memmove(&(ra->keys[new_begin]), &(ra->keys[begin]),
10323 sizeof(uint16_t) * range);
10324 memmove(&(ra->typecodes[new_begin]), &(ra->typecodes[begin]),
10325 sizeof(uint8_t) * range);
10326 }
10327
10328 void ra_shift_right(roaring_array_t *ra, int32_t count, int32_t distance) {
10329 extend_array(ra, distance);
10330 int32_t srcpos = ra->size - count;
10331 int32_t dstpos = srcpos + distance;
10332 memmove(&(ra->keys[dstpos]), &(ra->keys[srcpos]),
10333 sizeof(uint16_t) * count);
10334 memmove(&(ra->containers[dstpos]), &(ra->containers[srcpos]),
10335 sizeof(void *) * count);
10336 memmove(&(ra->typecodes[dstpos]), &(ra->typecodes[srcpos]),
10337 sizeof(uint8_t) * count);
10338 ra->size += distance;
10339 }
10340
10341
10342 size_t ra_size_in_bytes(roaring_array_t *ra) {
10343 size_t cardinality = 0;
10344 size_t tot_len =
10345 1 /* initial byte type */ + 4 /* tot_len */ + sizeof(roaring_array_t) +
10346 ra->size * (sizeof(uint16_t) + sizeof(void *) + sizeof(uint8_t));
10347 for (int32_t i = 0; i < ra->size; i++) {
10348 tot_len +=
10349 (container_serialization_len(ra->containers[i], ra->typecodes[i]) +
10350 sizeof(uint16_t));
10351 cardinality +=
10352 container_get_cardinality(ra->containers[i], ra->typecodes[i]);
10353 }
10354
10355 if ((cardinality * sizeof(uint32_t) + sizeof(uint32_t)) < tot_len) {
10356 return cardinality * sizeof(uint32_t) + 1 + sizeof(uint32_t);
10357 }
10358 return tot_len;
10359 }
10360
10361 void ra_to_uint32_array(const roaring_array_t *ra, uint32_t *ans) {
10362 size_t ctr = 0;
10363 for (int32_t i = 0; i < ra->size; ++i) {
10364 int num_added = container_to_uint32_array(
10365 ans + ctr, ra->containers[i], ra->typecodes[i],
10366 ((uint32_t)ra->keys[i]) << 16);
10367 ctr += num_added;
10368 }
10369 }
10370
10371 bool ra_has_run_container(const roaring_array_t *ra) {
10372 for (int32_t k = 0; k < ra->size; ++k) {
10373 if (get_container_type(ra->containers[k], ra->typecodes[k]) ==
10374 RUN_CONTAINER_TYPE_CODE)
10375 return true;
10376 }
10377 return false;
10378 }
10379
10380 uint32_t ra_portable_header_size(const roaring_array_t *ra) {
10381 if (ra_has_run_container(ra)) {
10382 if (ra->size <
10383 NO_OFFSET_THRESHOLD) { // for small bitmaps, we omit the offsets
10384 return 4 + (ra->size + 7) / 8 + 4 * ra->size;
10385 }
10386 return 4 + (ra->size + 7) / 8 +
10387 8 * ra->size; // - 4 because we pack the size with the cookie
10388 } else {
10389 return 4 + 4 + 8 * ra->size;
10390 }
10391 }
10392
10393 size_t ra_portable_size_in_bytes(const roaring_array_t *ra) {
10394 size_t count = ra_portable_header_size(ra);
10395
10396 for (int32_t k = 0; k < ra->size; ++k) {
10397 count += container_size_in_bytes(ra->containers[k], ra->typecodes[k]);
10398 }
10399 return count;
10400 }
10401
10402 size_t ra_portable_serialize(const roaring_array_t *ra, char *buf) {
10403 char *initbuf = buf;
10404 uint32_t startOffset = 0;
10405 bool hasrun = ra_has_run_container(ra);
10406 if (hasrun) {
10407 uint32_t cookie = SERIAL_COOKIE | ((ra->size - 1) << 16);
10408 memcpy(buf, &cookie, sizeof(cookie));
10409 buf += sizeof(cookie);
10410 uint32_t s = (ra->size + 7) / 8;
10411 uint8_t *bitmapOfRunContainers = (uint8_t *)calloc(s, 1);
10412 assert(bitmapOfRunContainers != NULL); // todo: handle
10413 for (int32_t i = 0; i < ra->size; ++i) {
10414 if (get_container_type(ra->containers[i], ra->typecodes[i]) ==
10415 RUN_CONTAINER_TYPE_CODE) {
10416 bitmapOfRunContainers[i / 8] |= (1 << (i % 8));
10417 }
10418 }
10419 memcpy(buf, bitmapOfRunContainers, s);
10420 buf += s;
10421 free(bitmapOfRunContainers);
10422 if (ra->size < NO_OFFSET_THRESHOLD) {
10423 startOffset = 4 + 4 * ra->size + s;
10424 } else {
10425 startOffset = 4 + 8 * ra->size + s;
10426 }
10427 } else { // backwards compatibility
10428 uint32_t cookie = SERIAL_COOKIE_NO_RUNCONTAINER;
10429
10430 memcpy(buf, &cookie, sizeof(cookie));
10431 buf += sizeof(cookie);
10432 memcpy(buf, &ra->size, sizeof(ra->size));
10433 buf += sizeof(ra->size);
10434
10435 startOffset = 4 + 4 + 4 * ra->size + 4 * ra->size;
10436 }
10437 for (int32_t k = 0; k < ra->size; ++k) {
10438 memcpy(buf, &ra->keys[k], sizeof(ra->keys[k]));
10439 buf += sizeof(ra->keys[k]);
10440 // get_cardinality returns a value in [1,1<<16], subtracting one
10441 // we get [0,1<<16 - 1] which fits in 16 bits
10442 uint16_t card = (uint16_t)(
10443 container_get_cardinality(ra->containers[k], ra->typecodes[k]) - 1);
10444 memcpy(buf, &card, sizeof(card));
10445 buf += sizeof(card);
10446 }
10447 if ((!hasrun) || (ra->size >= NO_OFFSET_THRESHOLD)) {
10448 // writing the containers offsets
10449 for (int32_t k = 0; k < ra->size; k++) {
10450 memcpy(buf, &startOffset, sizeof(startOffset));
10451 buf += sizeof(startOffset);
10452 startOffset =
10453 startOffset +
10454 container_size_in_bytes(ra->containers[k], ra->typecodes[k]);
10455 }
10456 }
10457 for (int32_t k = 0; k < ra->size; ++k) {
10458 buf += container_write(ra->containers[k], ra->typecodes[k], buf);
10459 }
10460 return buf - initbuf;
10461 }
10462
10463 // Quickly checks whether there is a serialized bitmap at the pointer,
10464 // not exceeding size "maxbytes" in bytes. This function does not allocate
10465 // memory dynamically.
10466 //
10467 // This function returns 0 if and only if no valid bitmap is found.
10468 // Otherwise, it returns how many bytes are occupied.
10469 //
10470 size_t ra_portable_deserialize_size(const char *buf, const size_t maxbytes) {
10471 size_t bytestotal = sizeof(int32_t);// for cookie
10472 if(bytestotal > maxbytes) return 0;
10473 uint32_t cookie;
10474 memcpy(&cookie, buf, sizeof(int32_t));
10475 buf += sizeof(uint32_t);
10476 if ((cookie & 0xFFFF) != SERIAL_COOKIE &&
10477 cookie != SERIAL_COOKIE_NO_RUNCONTAINER) {
10478 return 0;
10479 }
10480 int32_t size;
10481
10482 if ((cookie & 0xFFFF) == SERIAL_COOKIE)
10483 size = (cookie >> 16) + 1;
10484 else {
10485 bytestotal += sizeof(int32_t);
10486 if(bytestotal > maxbytes) return 0;
10487 memcpy(&size, buf, sizeof(int32_t));
10488 buf += sizeof(uint32_t);
10489 }
10490 if (size > (1<<16)) {
10491 return 0; // logically impossible
10492 }
10493 char *bitmapOfRunContainers = NULL;
10494 bool hasrun = (cookie & 0xFFFF) == SERIAL_COOKIE;
10495 if (hasrun) {
10496 int32_t s = (size + 7) / 8;
10497 bytestotal += s;
10498 if(bytestotal > maxbytes) return 0;
10499 bitmapOfRunContainers = (char *)buf;
10500 buf += s;
10501 }
10502 bytestotal += size * 2 * sizeof(uint16_t);
10503 if(bytestotal > maxbytes) return 0;
10504 uint16_t *keyscards = (uint16_t *)buf;
10505 buf += size * 2 * sizeof(uint16_t);
10506 if ((!hasrun) || (size >= NO_OFFSET_THRESHOLD)) {
10507 // skipping the offsets
10508 bytestotal += size * 4;
10509 if(bytestotal > maxbytes) return 0;
10510 buf += size * 4;
10511 }
10512 // Reading the containers
10513 for (int32_t k = 0; k < size; ++k) {
10514 uint16_t tmp;
10515 memcpy(&tmp, keyscards + 2*k+1, sizeof(tmp));
10516 uint32_t thiscard = tmp + 1;
10517 bool isbitmap = (thiscard > DEFAULT_MAX_SIZE);
10518 bool isrun = false;
10519 if(hasrun) {
10520 if((bitmapOfRunContainers[k / 8] & (1 << (k % 8))) != 0) {
10521 isbitmap = false;
10522 isrun = true;
10523 }
10524 }
10525 if (isbitmap) {
10526 size_t containersize = BITSET_CONTAINER_SIZE_IN_WORDS * sizeof(uint64_t);
10527 bytestotal += containersize;
10528 if(bytestotal > maxbytes) return 0;
10529 buf += containersize;
10530 } else if (isrun) {
10531 bytestotal += sizeof(uint16_t);
10532 if(bytestotal > maxbytes) return 0;
10533 uint16_t n_runs;
10534 memcpy(&n_runs, buf, sizeof(uint16_t));
10535 buf += sizeof(uint16_t);
10536 size_t containersize = n_runs * sizeof(rle16_t);
10537 bytestotal += containersize;
10538 if(bytestotal > maxbytes) return 0;
10539 buf += containersize;
10540 } else {
10541 size_t containersize = thiscard * sizeof(uint16_t);
10542 bytestotal += containersize;
10543 if(bytestotal > maxbytes) return 0;
10544 buf += containersize;
10545 }
10546 }
10547 return bytestotal;
10548 }
10549
10550
10551 // this function populates answer from the content of buf (reading up to maxbytes bytes).
10552 // The function returns false if a properly serialized bitmap cannot be found.
10553 // if it returns true, readbytes is populated by how many bytes were read, we have that *readbytes <= maxbytes.
10554 bool ra_portable_deserialize(roaring_array_t *answer, const char *buf, const size_t maxbytes, size_t * readbytes) {
10555 *readbytes = sizeof(int32_t);// for cookie
10556 if(*readbytes > maxbytes) {
10557 fprintf(stderr, "Ran out of bytes while reading first 4 bytes.\n");
10558 return false;
10559 }
10560 uint32_t cookie;
10561 memcpy(&cookie, buf, sizeof(int32_t));
10562 buf += sizeof(uint32_t);
10563 if ((cookie & 0xFFFF) != SERIAL_COOKIE &&
10564 cookie != SERIAL_COOKIE_NO_RUNCONTAINER) {
10565 fprintf(stderr, "I failed to find one of the right cookies. Found %u\n",
10566 cookie);
10567 return false;
10568 }
10569 int32_t size;
10570
10571 if ((cookie & 0xFFFF) == SERIAL_COOKIE)
10572 size = (cookie >> 16) + 1;
10573 else {
10574 *readbytes += sizeof(int32_t);
10575 if(*readbytes > maxbytes) {
10576 fprintf(stderr, "Ran out of bytes while reading second part of the cookie.\n");
10577 return false;
10578 }
10579 memcpy(&size, buf, sizeof(int32_t));
10580 buf += sizeof(uint32_t);
10581 }
10582 if (size > (1<<16)) {
10583 fprintf(stderr, "You cannot have so many containers, the data must be corrupted: %u\n",
10584 size);
10585 return false; // logically impossible
10586 }
10587 const char *bitmapOfRunContainers = NULL;
10588 bool hasrun = (cookie & 0xFFFF) == SERIAL_COOKIE;
10589 if (hasrun) {
10590 int32_t s = (size + 7) / 8;
10591 *readbytes += s;
10592 if(*readbytes > maxbytes) {// data is corrupted?
10593 fprintf(stderr, "Ran out of bytes while reading run bitmap.\n");
10594 return false;
10595 }
10596 bitmapOfRunContainers = buf;
10597 buf += s;
10598 }
10599 uint16_t *keyscards = (uint16_t *)buf;
10600
10601 *readbytes += size * 2 * sizeof(uint16_t);
10602 if(*readbytes > maxbytes) {
10603 fprintf(stderr, "Ran out of bytes while reading key-cardinality array.\n");
10604 return false;
10605 }
10606 buf += size * 2 * sizeof(uint16_t);
10607
10608 bool is_ok = ra_init_with_capacity(answer, size);
10609 if (!is_ok) {
10610 fprintf(stderr, "Failed to allocate memory for roaring array. Bailing out.\n");
10611 return false;
10612 }
10613
10614 for (int32_t k = 0; k < size; ++k) {
10615 uint16_t tmp;
10616 memcpy(&tmp, keyscards + 2*k, sizeof(tmp));
10617 answer->keys[k] = tmp;
10618 }
10619 if ((!hasrun) || (size >= NO_OFFSET_THRESHOLD)) {
10620 *readbytes += size * 4;
10621 if(*readbytes > maxbytes) {// data is corrupted?
10622 fprintf(stderr, "Ran out of bytes while reading offsets.\n");
10623 ra_clear(answer);// we need to clear the containers already allocated, and the roaring array
10624 return false;
10625 }
10626
10627 // skipping the offsets
10628 buf += size * 4;
10629 }
10630 // Reading the containers
10631 for (int32_t k = 0; k < size; ++k) {
10632 uint16_t tmp;
10633 memcpy(&tmp, keyscards + 2*k+1, sizeof(tmp));
10634 uint32_t thiscard = tmp + 1;
10635 bool isbitmap = (thiscard > DEFAULT_MAX_SIZE);
10636 bool isrun = false;
10637 if(hasrun) {
10638 if((bitmapOfRunContainers[k / 8] & (1 << (k % 8))) != 0) {
10639 isbitmap = false;
10640 isrun = true;
10641 }
10642 }
10643 if (isbitmap) {
10644 // we check that the read is allowed
10645 size_t containersize = BITSET_CONTAINER_SIZE_IN_WORDS * sizeof(uint64_t);
10646 *readbytes += containersize;
10647 if(*readbytes > maxbytes) {
10648 fprintf(stderr, "Running out of bytes while reading a bitset container.\n");
10649 ra_clear(answer);// we need to clear the containers already allocated, and the roaring array
10650 return false;
10651 }
10652 // it is now safe to read
10653 bitset_container_t *c = bitset_container_create();
10654 if(c == NULL) {// memory allocation failure
10655 fprintf(stderr, "Failed to allocate memory for a bitset container.\n");
10656 ra_clear(answer);// we need to clear the containers already allocated, and the roaring array
10657 return false;
10658 }
10659 answer->size++;
10660 buf += bitset_container_read(thiscard, c, buf);
10661 answer->containers[k] = c;
10662 answer->typecodes[k] = BITSET_CONTAINER_TYPE_CODE;
10663 } else if (isrun) {
10664 // we check that the read is allowed
10665 *readbytes += sizeof(uint16_t);
10666 if(*readbytes > maxbytes) {
10667 fprintf(stderr, "Running out of bytes while reading a run container (header).\n");
10668 ra_clear(answer);// we need to clear the containers already allocated, and the roaring array
10669 return false;
10670 }
10671 uint16_t n_runs;
10672 memcpy(&n_runs, buf, sizeof(uint16_t));
10673 size_t containersize = n_runs * sizeof(rle16_t);
10674 *readbytes += containersize;
10675 if(*readbytes > maxbytes) {// data is corrupted?
10676 fprintf(stderr, "Running out of bytes while reading a run container.\n");
10677 ra_clear(answer);// we need to clear the containers already allocated, and the roaring array
10678 return false;
10679 }
10680 // it is now safe to read
10681
10682 run_container_t *c = run_container_create();
10683 if(c == NULL) {// memory allocation failure
10684 fprintf(stderr, "Failed to allocate memory for a run container.\n");
10685 ra_clear(answer);// we need to clear the containers already allocated, and the roaring array
10686 return false;
10687 }
10688 answer->size++;
10689 buf += run_container_read(thiscard, c, buf);
10690 answer->containers[k] = c;
10691 answer->typecodes[k] = RUN_CONTAINER_TYPE_CODE;
10692 } else {
10693 // we check that the read is allowed
10694 size_t containersize = thiscard * sizeof(uint16_t);
10695 *readbytes += containersize;
10696 if(*readbytes > maxbytes) {// data is corrupted?
10697 fprintf(stderr, "Running out of bytes while reading an array container.\n");
10698 ra_clear(answer);// we need to clear the containers already allocated, and the roaring array
10699 return false;
10700 }
10701 // it is now safe to read
10702 array_container_t *c =
10703 array_container_create_given_capacity(thiscard);
10704 if(c == NULL) {// memory allocation failure
10705 fprintf(stderr, "Failed to allocate memory for an array container.\n");
10706 ra_clear(answer);// we need to clear the containers already allocated, and the roaring array
10707 return false;
10708 }
10709 answer->size++;
10710 buf += array_container_read(thiscard, c, buf);
10711 answer->containers[k] = c;
10712 answer->typecodes[k] = ARRAY_CONTAINER_TYPE_CODE;
10713 }
10714 }
10715 return true;
10716 }
10717 /* end file src/roaring_array.c */
10718 /* begin file src/roaring_priority_queue.c */
10719
10720 struct roaring_pq_element_s {
10721 uint64_t size;
10722 bool is_temporary;
10723 roaring_bitmap_t *bitmap;
10724 };
10725
10726 typedef struct roaring_pq_element_s roaring_pq_element_t;
10727
10728 struct roaring_pq_s {
10729 roaring_pq_element_t *elements;
10730 uint64_t size;
10731 };
10732
10733 typedef struct roaring_pq_s roaring_pq_t;
10734
10735 static inline bool compare(roaring_pq_element_t *t1, roaring_pq_element_t *t2) {
10736 return t1->size < t2->size;
10737 }
10738
10739 static void pq_add(roaring_pq_t *pq, roaring_pq_element_t *t) {
10740 uint64_t i = pq->size;
10741 pq->elements[pq->size++] = *t;
10742 while (i > 0) {
10743 uint64_t p = (i - 1) >> 1;
10744 roaring_pq_element_t ap = pq->elements[p];
10745 if (!compare(t, &ap)) break;
10746 pq->elements[i] = ap;
10747 i = p;
10748 }
10749 pq->elements[i] = *t;
10750 }
10751
10752 static void pq_free(roaring_pq_t *pq) {
10753 free(pq->elements);
10754 pq->elements = NULL; // paranoid
10755 free(pq);
10756 }
10757
10758 static void percolate_down(roaring_pq_t *pq, uint32_t i) {
10759 uint32_t size = (uint32_t)pq->size;
10760 uint32_t hsize = size >> 1;
10761 roaring_pq_element_t ai = pq->elements[i];
10762 while (i < hsize) {
10763 uint32_t l = (i << 1) + 1;
10764 uint32_t r = l + 1;
10765 roaring_pq_element_t bestc = pq->elements[l];
10766 if (r < size) {
10767 if (compare(pq->elements + r, &bestc)) {
10768 l = r;
10769 bestc = pq->elements[r];
10770 }
10771 }
10772 if (!compare(&bestc, &ai)) {
10773 break;
10774 }
10775 pq->elements[i] = bestc;
10776 i = l;
10777 }
10778 pq->elements[i] = ai;
10779 }
10780
10781 static roaring_pq_t *create_pq(const roaring_bitmap_t **arr, uint32_t length) {
10782 roaring_pq_t *answer = (roaring_pq_t *)malloc(sizeof(roaring_pq_t));
10783 answer->elements =
10784 (roaring_pq_element_t *)malloc(sizeof(roaring_pq_element_t) * length);
10785 answer->size = length;
10786 for (uint32_t i = 0; i < length; i++) {
10787 answer->elements[i].bitmap = (roaring_bitmap_t *)arr[i];
10788 answer->elements[i].is_temporary = false;
10789 answer->elements[i].size =
10790 roaring_bitmap_portable_size_in_bytes(arr[i]);
10791 }
10792 for (int32_t i = (length >> 1); i >= 0; i--) {
10793 percolate_down(answer, i);
10794 }
10795 return answer;
10796 }
10797
10798 static roaring_pq_element_t pq_poll(roaring_pq_t *pq) {
10799 roaring_pq_element_t ans = *pq->elements;
10800 if (pq->size > 1) {
10801 pq->elements[0] = pq->elements[--pq->size];
10802 percolate_down(pq, 0);
10803 } else
10804 --pq->size;
10805 // memmove(pq->elements,pq->elements+1,(pq->size-1)*sizeof(roaring_pq_element_t));--pq->size;
10806 return ans;
10807 }
10808
10809 // this function consumes and frees the inputs
10810 static roaring_bitmap_t *lazy_or_from_lazy_inputs(roaring_bitmap_t *x1,
10811 roaring_bitmap_t *x2) {
10812 uint8_t container_result_type = 0;
10813 const int length1 = ra_get_size(&x1->high_low_container),
10814 length2 = ra_get_size(&x2->high_low_container);
10815 if (0 == length1) {
10816 roaring_bitmap_free(x1);
10817 return x2;
10818 }
10819 if (0 == length2) {
10820 roaring_bitmap_free(x2);
10821 return x1;
10822 }
10823 uint32_t neededcap = length1 > length2 ? length2 : length1;
10824 roaring_bitmap_t *answer = roaring_bitmap_create_with_capacity(neededcap);
10825 int pos1 = 0, pos2 = 0;
10826 uint8_t container_type_1, container_type_2;
10827 uint16_t s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
10828 uint16_t s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
10829 while (true) {
10830 if (s1 == s2) {
10831 // todo: unsharing can be inefficient as it may create a clone where
10832 // none
10833 // is needed, but it has the benefit of being easy to reason about.
10834 ra_unshare_container_at_index(&x1->high_low_container, pos1);
10835 void *c1 = ra_get_container_at_index(&x1->high_low_container, pos1,
10836 &container_type_1);
10837 assert(container_type_1 != SHARED_CONTAINER_TYPE_CODE);
10838 ra_unshare_container_at_index(&x2->high_low_container, pos2);
10839 void *c2 = ra_get_container_at_index(&x2->high_low_container, pos2,
10840 &container_type_2);
10841 assert(container_type_2 != SHARED_CONTAINER_TYPE_CODE);
10842 void *c;
10843
10844 if ((container_type_2 == BITSET_CONTAINER_TYPE_CODE) &&
10845 (container_type_1 != BITSET_CONTAINER_TYPE_CODE)) {
10846 c = container_lazy_ior(c2, container_type_2, c1,
10847 container_type_1,
10848 &container_result_type);
10849 container_free(c1, container_type_1);
10850 if (c != c2) {
10851 container_free(c2, container_type_2);
10852 }
10853 } else {
10854 c = container_lazy_ior(c1, container_type_1, c2,
10855 container_type_2,
10856 &container_result_type);
10857 container_free(c2, container_type_2);
10858 if (c != c1) {
10859 container_free(c1, container_type_1);
10860 }
10861 }
10862 // since we assume that the initial containers are non-empty, the
10863 // result here
10864 // can only be non-empty
10865 ra_append(&answer->high_low_container, s1, c,
10866 container_result_type);
10867 ++pos1;
10868 ++pos2;
10869 if (pos1 == length1) break;
10870 if (pos2 == length2) break;
10871 s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
10872 s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
10873
10874 } else if (s1 < s2) { // s1 < s2
10875 void *c1 = ra_get_container_at_index(&x1->high_low_container, pos1,
10876 &container_type_1);
10877 ra_append(&answer->high_low_container, s1, c1, container_type_1);
10878 pos1++;
10879 if (pos1 == length1) break;
10880 s1 = ra_get_key_at_index(&x1->high_low_container, pos1);
10881
10882 } else { // s1 > s2
10883 void *c2 = ra_get_container_at_index(&x2->high_low_container, pos2,
10884 &container_type_2);
10885 ra_append(&answer->high_low_container, s2, c2, container_type_2);
10886 pos2++;
10887 if (pos2 == length2) break;
10888 s2 = ra_get_key_at_index(&x2->high_low_container, pos2);
10889 }
10890 }
10891 if (pos1 == length1) {
10892 ra_append_move_range(&answer->high_low_container,
10893 &x2->high_low_container, pos2, length2);
10894 } else if (pos2 == length2) {
10895 ra_append_move_range(&answer->high_low_container,
10896 &x1->high_low_container, pos1, length1);
10897 }
10898 ra_clear_without_containers(&x1->high_low_container);
10899 ra_clear_without_containers(&x2->high_low_container);
10900 free(x1);
10901 free(x2);
10902 return answer;
10903 }
10904
10905 /**
10906 * Compute the union of 'number' bitmaps using a heap. This can
10907 * sometimes be faster than roaring_bitmap_or_many which uses
10908 * a naive algorithm. Caller is responsible for freeing the
10909 * result.
10910 */
10911 roaring_bitmap_t *roaring_bitmap_or_many_heap(uint32_t number,
10912 const roaring_bitmap_t **x) {
10913 if (number == 0) {
10914 return roaring_bitmap_create();
10915 }
10916 if (number == 1) {
10917 return roaring_bitmap_copy(x[0]);
10918 }
10919 roaring_pq_t *pq = create_pq(x, number);
10920 while (pq->size > 1) {
10921 roaring_pq_element_t x1 = pq_poll(pq);
10922 roaring_pq_element_t x2 = pq_poll(pq);
10923
10924 if (x1.is_temporary && x2.is_temporary) {
10925 roaring_bitmap_t *newb =
10926 lazy_or_from_lazy_inputs(x1.bitmap, x2.bitmap);
10927 // should normally return a fresh new bitmap *except* that
10928 // it can return x1.bitmap or x2.bitmap in degenerate cases
10929 bool temporary = !((newb == x1.bitmap) && (newb == x2.bitmap));
10930 uint64_t bsize = roaring_bitmap_portable_size_in_bytes(newb);
10931 roaring_pq_element_t newelement = {
10932 .size = bsize, .is_temporary = temporary, .bitmap = newb};
10933 pq_add(pq, &newelement);
10934 } else if (x2.is_temporary) {
10935 roaring_bitmap_lazy_or_inplace(x2.bitmap, x1.bitmap, false);
10936 x2.size = roaring_bitmap_portable_size_in_bytes(x2.bitmap);
10937 pq_add(pq, &x2);
10938 } else if (x1.is_temporary) {
10939 roaring_bitmap_lazy_or_inplace(x1.bitmap, x2.bitmap, false);
10940 x1.size = roaring_bitmap_portable_size_in_bytes(x1.bitmap);
10941
10942 pq_add(pq, &x1);
10943 } else {
10944 roaring_bitmap_t *newb =
10945 roaring_bitmap_lazy_or(x1.bitmap, x2.bitmap, false);
10946 uint64_t bsize = roaring_bitmap_portable_size_in_bytes(newb);
10947 roaring_pq_element_t newelement = {
10948 .size = bsize, .is_temporary = true, .bitmap = newb};
10949
10950 pq_add(pq, &newelement);
10951 }
10952 }
10953 roaring_pq_element_t X = pq_poll(pq);
10954 roaring_bitmap_t *answer = X.bitmap;
10955 roaring_bitmap_repair_after_lazy(answer);
10956 pq_free(pq);
10957 return answer;
10958 }
10959 /* end file src/roaring_priority_queue.c */