/* Compute the intersection of src_1 and src_2 and write the result to * dst. */ void array_bitset_container_intersection(const array_container_t *src_1, const bitset_container_t *src_2, array_container_t *dst) { if (dst->capacity < src_1->cardinality) array_container_grow(dst, src_1->cardinality, INT32_MAX, false); const int32_t origcard = src_1->cardinality; dst->cardinality = 0; for (int i = 0; i < origcard; ++i) { // could probably be vectorized uint16_t key = src_1->array[i]; // next bit could be branchless if (bitset_container_contains(src_2, key)) { dst->array[dst->cardinality++] = key; } } }
/* Compute the andnot of src_1 and src_2 and write the result to * dst, a valid array container that could be the same as dst.*/ void array_bitset_container_andnot(const array_container_t *src_1, const bitset_container_t *src_2, array_container_t *dst) { // follows Java implementation as of June 2016 if (dst->capacity < src_1->cardinality) array_container_grow(dst, src_1->cardinality, INT32_MAX, false); int32_t newcard = 0; const int32_t origcard = src_1->cardinality; for (int i = 0; i < origcard; ++i) { uint16_t key = src_1->array[i]; if (!bitset_container_contains(src_2, key)) { dst->array[newcard++] = key; } } dst->cardinality = newcard; }
/* Compute the intersection of src_1 and src_2 and write the result to * *dst. If the result is true then the result is a bitset_container_t * otherwise is a array_container_t. */ bool run_bitset_container_intersection(const run_container_t *src_1, const bitset_container_t *src_2, void **dst) { int32_t card = run_container_cardinality(src_1); if (card <= DEFAULT_MAX_SIZE) { // result can only be an array (assuming that we never make a // RunContainer) if (card > src_2->cardinality) { card = src_2->cardinality; } array_container_t *answer = array_container_create_given_capacity(card); *dst = answer; if (*dst == NULL) { return false; } for (int32_t rlepos = 0; rlepos < src_1->n_runs; ++rlepos) { rle16_t rle = src_1->runs[rlepos]; uint32_t endofrun = (uint32_t)rle.value + rle.length; for (uint32_t runValue = rle.value; runValue <= endofrun; ++runValue) { if (bitset_container_contains(src_2, runValue)) { answer->array[answer->cardinality++] = (uint16_t)runValue; } } } return false; } if (*dst == src_2) { // we attempt in-place bitset_container_t *answer = (bitset_container_t *)*dst; uint32_t start = 0; for (int32_t rlepos = 0; rlepos < src_1->n_runs; ++rlepos) { const rle16_t rle = src_1->runs[rlepos]; uint32_t end = rle.value; bitset_reset_range(src_2->array, start, end); start = end + rle.length + 1; } bitset_reset_range(src_2->array, start, UINT32_C(1) << 16); answer->cardinality = bitset_container_compute_cardinality(answer); if (src_2->cardinality > DEFAULT_MAX_SIZE) { return true; } else { array_container_t *newanswer = array_container_from_bitset(src_2); if (newanswer == NULL) { *dst = NULL; return false; } *dst = newanswer; return false; } } else { // no inplace // we expect the answer to be a bitmap (if we are lucky) bitset_container_t *answer = bitset_container_clone(src_2); *dst = answer; if (answer == NULL) { return true; } uint32_t start = 0; for (int32_t rlepos = 0; rlepos < src_1->n_runs; ++rlepos) { const rle16_t rle = src_1->runs[rlepos]; uint32_t end = rle.value; bitset_reset_range(answer->array, start, end); start = end + rle.length + 1; } bitset_reset_range(answer->array, start, UINT32_C(1) << 16); answer->cardinality = bitset_container_compute_cardinality(answer); if (answer->cardinality > DEFAULT_MAX_SIZE) { return true; } else { array_container_t *newanswer = array_container_from_bitset(answer); bitset_container_free(*dst); if (newanswer == NULL) { *dst = NULL; return false; } *dst = newanswer; return false; } } }