1#include <roaring/containers/mixed_equal.h>
2
3bool array_container_equal_bitset(const array_container_t* container1,
4 const bitset_container_t* container2) {
5 if (container2->cardinality != BITSET_UNKNOWN_CARDINALITY) {
6 if (container2->cardinality != container1->cardinality) {
7 return false;
8 }
9 }
10 int32_t pos = 0;
11 for (int32_t i = 0; i < BITSET_CONTAINER_SIZE_IN_WORDS; ++i) {
12 uint64_t w = container2->array[i];
13 while (w != 0) {
14 uint64_t t = w & (~w + 1);
15 uint16_t r = i * 64 + __builtin_ctzll(w);
16 if (pos >= container1->cardinality) {
17 return false;
18 }
19 if (container1->array[pos] != r) {
20 return false;
21 }
22 ++pos;
23 w ^= t;
24 }
25 }
26 return (pos == container1->cardinality);
27}
28
29bool run_container_equals_array(const run_container_t* container1,
30 const array_container_t* container2) {
31 if (run_container_cardinality(container1) != container2->cardinality)
32 return false;
33 int32_t pos = 0;
34 for (int i = 0; i < container1->n_runs; ++i) {
35 const uint32_t run_start = container1->runs[i].value;
36 const uint32_t le = container1->runs[i].length;
37
38 if (container2->array[pos] != run_start) {
39 return false;
40 }
41
42 if (container2->array[pos + le] != run_start + le) {
43 return false;
44 }
45
46 pos += le + 1;
47 }
48 return true;
49}
50
51bool run_container_equals_bitset(const run_container_t* container1,
52 const bitset_container_t* container2) {
53
54 int run_card = run_container_cardinality(container1);
55 int bitset_card = (container2->cardinality != BITSET_UNKNOWN_CARDINALITY) ?
56 container2->cardinality :
57 bitset_container_compute_cardinality(container2);
58 if (bitset_card != run_card) {
59 return false;
60 }
61
62 for (int32_t i = 0; i < container1->n_runs; i++) {
63 uint32_t begin = container1->runs[i].value;
64 if (container1->runs[i].length) {
65 uint32_t end = begin + container1->runs[i].length + 1;
66 if (!bitset_container_contains_range(container2, begin, end)) {
67 return false;
68 }
69 } else {
70 if (!bitset_container_contains(container2, begin)) {
71 return false;
72 }
73 }
74 }
75
76 return true;
77}
78