| 1 |
|
| #ifndef HAMT_ALGEBRA_H |
| 2 |
|
| #define HAMT_ALGEBRA_H |
| 3 |
|
| |
| 4 |
|
| /* Operations over TWO tries at once: are they equal, what is their union, |
| 5 |
|
| * where do they differ. |
| 6 |
|
| * |
| 7 |
|
| * All three walk the two together, position by position, and all three get |
| 8 |
|
| * their speed from one fact: if the two nodes at the same place are the same |
| 9 |
|
| * pointer, everything under them is the same, and there is nothing to do. |
| 10 |
|
| * Two versions of one map differ along a handful of paths, so comparing or |
| 11 |
|
| * merging them costs what changed between them and not what they hold. |
| 12 |
|
| * |
| 13 |
|
| * They can be walked in step at all because of the canonical shape: the |
| 14 |
|
| * same keys always make the same trie, so a position that holds a child in |
| 15 |
|
| * one and an entry in the other is a real difference in content, never an |
| 16 |
|
| * accident of history. |
| 17 |
|
| * |
| 18 |
|
| * Callbacks return 0 to carry on. Anything else stops the operation, which |
| 19 |
|
| * unwinds what it had built and reports HAMT_ERR_ABORT; a callback that ran |
| 20 |
|
| * out of memory returns HAMT_ERR_OOM and that is reported instead. */ |
| 21 |
|
| |
| 22 |
|
| #define HAMT_OK 0 |
| 23 |
|
| #define HAMT_ERR_ABORT 1 |
| 24 |
|
| #define HAMT_ERR_OOM 2 |
| 25 |
|
| |
| 26 |
|
| /* How many entries a subtree holds. Visits every node of it. */ |
| 27 |
204767 |
| static size_t hamt_node_count(const hamt_node *n) { |
| 28 |
|
| uint32_t nd, N, i; |
| 29 |
|
| size_t total; |
| 30 |
|
| /* |
| 31 |
|
| # uncoverable branch true |
| 32 |
|
| # uncoverable condition left count:1 */ |
| 33 |
204767 |
▶ | if (!n) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!n) return 0; |
0 |
204767 |
204767 |
|
| 34 |
204767 |
| nd = hamt_node_ndata(n); |
| 35 |
204767 |
| N = hamt_node_nslots(n); |
| 36 |
204767 |
| total = nd; |
| 37 |
365827 |
▶ | for (i = nd; i < N; i++) total += hamt_node_count((const hamt_node *)n->slot[i]); |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = nd; i < N; i++) total += hamt_node_count((const hamt_node *)n->slot[i]); |
161060 |
204767 |
365827 |
|
| 38 |
204767 |
| return total; |
| 39 |
|
| } |
| 40 |
|
| |
| 41 |
|
| /* ---- equality ---------------------------------------------------------- */ |
| 42 |
|
| |
| 43 |
|
| /* 1 equal, 0 not, negative to stop. */ |
| 44 |
|
| typedef int (*hamt_eq_fn)(void *ud, void *cx, void *va, void *vb); |
| 45 |
|
| |
| 46 |
|
| typedef struct hamt_eq_ctx { |
| 47 |
|
| void *cx; |
| 48 |
|
| hamt_eq_fn eq; /* NULL: HAMT_VAL_EQ */ |
| 49 |
|
| void *ud; |
| 50 |
|
| size_t visits; /* node pairs looked at */ |
| 51 |
|
| } hamt_eq_ctx; |
| 52 |
|
| |
| 53 |
237717 |
| static int hamt_val_same(hamt_eq_ctx *c, void *va, void *vb) { |
| 54 |
237717 |
▶ | if (va == vb) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (va == vb) return 1; |
7029 |
230688 |
237717 |
|
| 55 |
|
| /* |
| 56 |
|
| # uncoverable branch true |
| 57 |
|
| # uncoverable condition left count:1 |
| 58 |
|
| # uncoverable condition left count:2 */ |
| 59 |
230688 |
▶ | if (!va || !vb) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!va || !vb) return 0; |
230688 |
0 |
230688 |
| if (!va || !vb) return 0; |
0 |
230688 |
230688 |
|
| 60 |
230688 |
▶ | if (c->eq) return c->eq(c->ud, c->cx, va, vb); |
Branch: 1 branch
| Branch | True | False | Total |
| if (c->eq) return c->eq(c->ud, c->cx, va, vb); |
17048 |
213640 |
230688 |
|
| 61 |
213640 |
| return HAMT_VAL_EQ(c->cx, va, vb) ? 1 : 0; |
| 62 |
|
| } |
| 63 |
|
| |
| 64 |
|
| /* 1 when the two subtrees hold the same keys with equal values, 0 when they |
| 65 |
|
| * do not, negative when the comparison callback stopped it. */ |
| 66 |
200013 |
| static int hamt_node_equal(hamt_eq_ctx *c, const hamt_node *a, const hamt_node *b) { |
| 67 |
|
| uint32_t nd, N, i, j; |
| 68 |
|
| |
| 69 |
200013 |
▶ | if (a == b) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (a == b) return 1; |
3552 |
196461 |
200013 |
|
| 70 |
|
| /* |
| 71 |
|
| # uncoverable branch true class:ignore_covered_err |
| 72 |
|
| # uncoverable branch true count:1 class:ignore_covered_err |
| 73 |
|
| # uncoverable condition left count:1 |
| 74 |
|
| # uncoverable condition left count:2 */ |
| 75 |
196461 |
▶ | if (!a || !b) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!a || !b) return 0; |
196461 |
0 |
196461 |
| if (!a || !b) return 0; |
0 |
196461 |
196461 |
|
| 76 |
196461 |
| c->visits++; |
| 77 |
|
| |
| 78 |
196461 |
▶ | if (a->datamap != b->datamap || a->nodemap != b->nodemap || a->ncoll != b->ncoll) /* # uncoverable condition left count:2 */ |
Branch: 3 branches
| Branch | True | False | Total |
| if (a->datamap != b->datamap || a->nodemap != b->nodemap || a->ncoll != b->ncoll) /* # uncoverable condition left count:2 */ |
196422 |
39 |
196461 |
| if (a->datamap != b->datamap || a->nodemap != b->nodemap || a->ncoll != b->ncoll) /* # uncoverable condition left count:2 */ |
196422 |
0 |
196422 |
| if (a->datamap != b->datamap || a->nodemap != b->nodemap || a->ncoll != b->ncoll) /* # uncoverable condition left count:2 */ |
14 |
196408 |
196422 |
|
| 79 |
53 |
| return 0; |
| 80 |
196408 |
| nd = hamt_node_ndata(a); |
| 81 |
196408 |
| N = hamt_node_nslots(a); |
| 82 |
|
| |
| 83 |
196408 |
▶ | if (a->ncoll) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (a->ncoll) { |
15228 |
181180 |
196408 |
|
| 84 |
|
| /* The same keys, in whatever order each node happens to hold them. */ |
| 85 |
100853 |
▶ | for (i = 0; i < nd; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < nd; i++) { |
85639 |
15214 |
100853 |
|
| 86 |
85639 |
| const hamt_entry *ea = (const hamt_entry *)a->slot[i]; |
| 87 |
85639 |
| const hamt_entry *eb = NULL; |
| 88 |
|
| int rc; |
| 89 |
931132 |
▶ | for (j = 0; j < nd; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < nd; j++) { |
931124 |
8 |
931132 |
|
| 90 |
931124 |
| eb = (const hamt_entry *)b->slot[j]; |
| 91 |
931124 |
▶ | if (hamt_entry_key_eq(eb, ea->key, ea->klen, ea->utf8, ea->hash)) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (hamt_entry_key_eq(eb, ea->key, ea->klen, ea->utf8, ea->hash)) break; |
85631 |
845493 |
931124 |
|
| 92 |
|
| } |
| 93 |
85639 |
▶ | if (j == nd) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (j == nd) return 0; |
8 |
85631 |
85639 |
|
| 94 |
85631 |
| rc = hamt_val_same(c, ea->val, eb->val); |
| 95 |
85631 |
▶ | if (rc <= 0) return rc; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc <= 0) return rc; |
6 |
85625 |
85631 |
|
| 96 |
|
| } |
| 97 |
15214 |
| return 1; |
| 98 |
|
| } |
| 99 |
|
| |
| 100 |
282585 |
▶ | for (i = 0; i < nd; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < nd; i++) { |
102497 |
180088 |
282585 |
|
| 101 |
102497 |
| const hamt_entry *ea = (const hamt_entry *)a->slot[i]; |
| 102 |
102497 |
| const hamt_entry *eb = (const hamt_entry *)b->slot[i]; |
| 103 |
|
| int rc; |
| 104 |
102497 |
▶ | if (ea == eb) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ea == eb) continue; |
15342 |
87155 |
102497 |
|
| 105 |
87155 |
▶ | if (!hamt_entry_key_eq(eb, ea->key, ea->klen, ea->utf8, ea->hash)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hamt_entry_key_eq(eb, ea->key, ea->klen, ea->utf8, ea->hash)) return 0; |
2 |
87153 |
87155 |
|
| 106 |
87153 |
| rc = hamt_val_same(c, ea->val, eb->val); |
| 107 |
87150 |
▶ | if (rc <= 0) return rc; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc <= 0) return rc; |
1087 |
86063 |
87150 |
|
| 108 |
|
| } |
| 109 |
376322 |
▶ | for (; i < N; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (; i < N; i++) { |
196608 |
179714 |
376322 |
|
| 110 |
196608 |
| int rc = hamt_node_equal(c, (const hamt_node *)a->slot[i], |
| 111 |
196608 |
| (const hamt_node *)b->slot[i]); |
| 112 |
196608 |
▶ | if (rc <= 0) return rc; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc <= 0) return rc; |
374 |
196234 |
196608 |
|
| 113 |
|
| } |
| 114 |
179714 |
| return 1; |
| 115 |
|
| } |
| 116 |
|
| |
| 117 |
|
| /* ---- union ------------------------------------------------------------- */ |
| 118 |
|
| |
| 119 |
|
| /* Called for a key in both maps whose two values are not the same pointer. |
| 120 |
|
| * Sets *out to the entry the result should hold, with one reference for the |
| 121 |
|
| * caller: `ea` or `eb` retained, or a new entry under the same key. |
| 122 |
|
| * Returns HAMT_OK, HAMT_ERR_ABORT or HAMT_ERR_OOM. */ |
| 123 |
|
| typedef int (*hamt_resolve_fn)(void *ud, void *cx, hamt_entry *ea, |
| 124 |
|
| hamt_entry *eb, hamt_entry **out); |
| 125 |
|
| |
| 126 |
|
| typedef struct hamt_merge_ctx { |
| 127 |
|
| void *cx; |
| 128 |
|
| hamt_resolve_fn resolve; /* NULL: the second map's entry wins */ |
| 129 |
|
| void *ud; |
| 130 |
|
| size_t added; /* keys of the second map that the first lacked */ |
| 131 |
|
| size_t visits; /* node pairs looked at */ |
| 132 |
|
| int err; |
| 133 |
|
| } hamt_merge_ctx; |
| 134 |
|
| |
| 135 |
|
| /* The entry for a key both maps hold. One reference, or NULL with err set. */ |
| 136 |
72140 |
| static hamt_entry *hamt_merge_resolve(hamt_merge_ctx *c, hamt_entry *ea, |
| 137 |
|
| hamt_entry *eb) { |
| 138 |
72140 |
| hamt_entry *out = NULL; |
| 139 |
|
| int rc; |
| 140 |
|
| |
| 141 |
|
| /* The first map's entry when there is nothing to choose between, so the |
| 142 |
|
| * result shares with the first map wherever it can. */ |
| 143 |
72140 |
▶ | if (ea == eb || ea->val == eb->val) return hamt_entry_retain(ea); |
Branch: 2 branches
| Branch | True | False | Total |
| if (ea == eb || ea->val == eb->val) return hamt_entry_retain(ea); |
62256 |
9884 |
72140 |
| if (ea == eb || ea->val == eb->val) return hamt_entry_retain(ea); |
51990 |
10266 |
62256 |
|
| 144 |
10266 |
▶ | if (!c->resolve) return hamt_entry_retain(eb); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!c->resolve) return hamt_entry_retain(eb); |
3562 |
6704 |
10266 |
|
| 145 |
|
| |
| 146 |
6704 |
| rc = c->resolve(c->ud, c->cx, ea, eb, &out); |
| 147 |
6704 |
▶ | if (rc != HAMT_OK) { c->err = rc; return NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc != HAMT_OK) { c->err = rc; return NULL; } |
1100 |
5604 |
6704 |
|
| 148 |
5604 |
| return out; |
| 149 |
|
| } |
| 150 |
|
| |
| 151 |
|
| /* One entry from one map into a subtree of the other. `e_is_first` says |
| 152 |
|
| * which map the entry came from, which is the order the resolver is told. |
| 153 |
|
| * `*found` is whether the subtree already had the key. Returns `n` itself, |
| 154 |
|
| * retained, when the subtree is unchanged. */ |
| 155 |
18111 |
| static hamt_node *hamt_merge_put(hamt_merge_ctx *c, hamt_node *n, unsigned depth, |
| 156 |
|
| hamt_entry *e, int e_is_first, int *found) { |
| 157 |
18111 |
| hamt_entry *old = hamt_node_find(n, depth, e->key, e->klen, e->utf8, e->hash); |
| 158 |
|
| hamt_entry *r; |
| 159 |
|
| hamt_node *out; |
| 160 |
18111 |
| int added = 0; |
| 161 |
|
| |
| 162 |
18111 |
| *found = old != NULL; |
| 163 |
18111 |
▶ | if (!old) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!old) { |
9952 |
8159 |
18111 |
|
| 164 |
9952 |
| out = hamt_node_put(c->cx, n, depth, e, &added); |
| 165 |
9952 |
▶ | if (!out) c->err = HAMT_ERR_OOM; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!out) c->err = HAMT_ERR_OOM; |
136 |
9816 |
9952 |
|
| 166 |
9952 |
| return out; |
| 167 |
|
| } |
| 168 |
|
| |
| 169 |
8159 |
▶ | r = e_is_first ? hamt_merge_resolve(c, e, old) : hamt_merge_resolve(c, old, e); |
Branch: 1 branch
| Branch | True | False | Total |
| r = e_is_first ? hamt_merge_resolve(c, e, old) : hamt_merge_resolve(c, old, e); |
3766 |
4393 |
8159 |
|
| 170 |
8159 |
▶ | if (!r) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!r) return NULL; |
5 |
8154 |
8159 |
|
| 171 |
|
| /* When the choice is the entry already there, the put gives `n` back. */ |
| 172 |
8154 |
| out = hamt_node_put(c->cx, n, depth, r, &added); |
| 173 |
8154 |
| hamt_entry_release(c->cx, r); |
| 174 |
8154 |
▶ | if (!out) c->err = HAMT_ERR_OOM; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!out) c->err = HAMT_ERR_OOM; |
4 |
8150 |
8154 |
|
| 175 |
8154 |
| return out; |
| 176 |
|
| } |
| 177 |
|
| |
| 178 |
|
| static hamt_node *hamt_node_merge(hamt_merge_ctx *c, hamt_node *a, hamt_node *b, |
| 179 |
|
| unsigned depth); |
| 180 |
|
| |
| 181 |
|
| /* Two collision nodes: the first's entries, each resolved against the |
| 182 |
|
| * second's where the key is in both, then the second's that the first has |
| 183 |
|
| * not got. */ |
| 184 |
8122 |
| static hamt_node *hamt_coll_merge(hamt_merge_ctx *c, hamt_node *a, hamt_node *b) { |
| 185 |
8122 |
| size_t cap = (size_t)a->ncoll + (size_t)b->ncoll; |
| 186 |
8122 |
| size_t bytes = cap * sizeof(void *); |
| 187 |
|
| hamt_entry **list; |
| 188 |
8122 |
| uint32_t n = 0, i, j; |
| 189 |
8122 |
| int same_a = 1, same_b = 1; |
| 190 |
8122 |
| hamt_node *r = NULL; |
| 191 |
|
| |
| 192 |
|
| /* |
| 193 |
|
| # uncoverable branch true |
| 194 |
|
| # uncoverable condition left count:1 */ |
| 195 |
8122 |
▶ | if (cap > 0xffffffffU) { c->err = HAMT_ERR_OOM; return NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (cap > 0xffffffffU) { c->err = HAMT_ERR_OOM; return NULL; } |
0 |
8122 |
8122 |
|
| 196 |
8122 |
| list = (hamt_entry **)HAMT_ALLOC(c->cx, bytes); |
| 197 |
8122 |
▶ | if (!list) { c->err = HAMT_ERR_OOM; return NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!list) { c->err = HAMT_ERR_OOM; return NULL; } |
4 |
8118 |
8122 |
|
| 198 |
|
| |
| 199 |
55609 |
▶ | for (i = 0; i < a->ncoll; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < a->ncoll; i++) { |
47497 |
8112 |
55609 |
|
| 200 |
47497 |
| hamt_entry *ea = (hamt_entry *)a->slot[i], *eb = NULL, *e; |
| 201 |
351435 |
▶ | for (j = 0; j < b->ncoll; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < b->ncoll; j++) { |
317132 |
34303 |
351435 |
|
| 202 |
317132 |
| eb = (hamt_entry *)b->slot[j]; |
| 203 |
317132 |
▶ | if (hamt_entry_key_eq(eb, ea->key, ea->klen, ea->utf8, ea->hash)) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (hamt_entry_key_eq(eb, ea->key, ea->klen, ea->utf8, ea->hash)) break; |
13194 |
303938 |
317132 |
|
| 204 |
|
| } |
| 205 |
47497 |
▶ | if (j == b->ncoll) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (j == b->ncoll) { |
34303 |
13194 |
47497 |
|
| 206 |
34303 |
| e = hamt_entry_retain(ea); |
| 207 |
34303 |
| same_b = 0; |
| 208 |
|
| } |
| 209 |
|
| else { |
| 210 |
13194 |
| e = hamt_merge_resolve(c, ea, eb); |
| 211 |
13194 |
▶ | if (!e) goto fail; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!e) goto fail; |
6 |
13188 |
13194 |
|
| 212 |
13188 |
▶ | if (e != ea) same_a = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (e != ea) same_a = 0; |
1551 |
11637 |
13188 |
|
| 213 |
13188 |
▶ | if (e != eb) same_b = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (e != eb) same_b = 0; |
10612 |
2576 |
13188 |
|
| 214 |
|
| } |
| 215 |
47491 |
| list[n++] = e; |
| 216 |
|
| } |
| 217 |
54340 |
▶ | for (j = 0; j < b->ncoll; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < b->ncoll; j++) { |
46228 |
8112 |
54340 |
|
| 218 |
46228 |
| hamt_entry *eb = (hamt_entry *)b->slot[j]; |
| 219 |
350725 |
▶ | for (i = 0; i < a->ncoll; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < a->ncoll; i++) { |
317670 |
33055 |
350725 |
|
| 220 |
317670 |
| hamt_entry *ea = (hamt_entry *)a->slot[i]; |
| 221 |
317670 |
▶ | if (hamt_entry_key_eq(ea, eb->key, eb->klen, eb->utf8, eb->hash)) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (hamt_entry_key_eq(ea, eb->key, eb->klen, eb->utf8, eb->hash)) break; |
13173 |
304497 |
317670 |
|
| 222 |
|
| } |
| 223 |
46228 |
▶ | if (i < a->ncoll) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (i < a->ncoll) continue; |
13173 |
33055 |
46228 |
|
| 224 |
33055 |
| list[n++] = hamt_entry_retain(eb); |
| 225 |
33055 |
| c->added++; |
| 226 |
33055 |
| same_a = 0; |
| 227 |
|
| } |
| 228 |
|
| |
| 229 |
8112 |
▶ | if (same_a) r = hamt_node_retain(a); |
Branch: 1 branch
| Branch | True | False | Total |
| if (same_a) r = hamt_node_retain(a); |
574 |
7538 |
8112 |
|
| 230 |
7538 |
▶ | else if (same_b) r = hamt_node_retain(b); |
Branch: 1 branch
| Branch | True | False | Total |
| else if (same_b) r = hamt_node_retain(b); |
167 |
7371 |
7538 |
|
| 231 |
|
| else { |
| 232 |
7371 |
| r = hamt_node_new(c->cx, n); |
| 233 |
7371 |
▶ | if (!r) { c->err = HAMT_ERR_OOM; goto fail; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!r) { c->err = HAMT_ERR_OOM; goto fail; } |
4 |
7367 |
7371 |
|
| 234 |
7367 |
| r->ncoll = n; |
| 235 |
7367 |
| memcpy(r->slot, list, (size_t)n * sizeof(void *)); |
| 236 |
7367 |
| HAMT_FREE(c->cx, list, bytes); |
| 237 |
7367 |
| return r; /* the list's references are the node's now */ |
| 238 |
|
| } |
| 239 |
|
| |
| 240 |
751 |
| fail: |
| 241 |
4524 |
▶ | for (i = 0; i < n; i++) hamt_entry_release(c->cx, list[i]); |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) hamt_entry_release(c->cx, list[i]); |
3773 |
751 |
4524 |
|
| 242 |
751 |
| HAMT_FREE(c->cx, list, bytes); |
| 243 |
751 |
| return r; |
| 244 |
|
| } |
| 245 |
|
| |
| 246 |
|
| /* The union of two subtrees that hang at the same place. One reference, or |
| 247 |
|
| * NULL with err set. Returns `a` itself when the result is exactly `a`, and |
| 248 |
|
| * `b` itself when it is exactly `b`, so a caller can tell that nothing |
| 249 |
|
| * changed on that side by comparing pointers. */ |
| 250 |
161817 |
| static hamt_node *hamt_node_merge(hamt_merge_ctx *c, hamt_node *a, hamt_node *b, |
| 251 |
|
| unsigned depth) { |
| 252 |
|
| void *rd[32], *rc[32]; /* what goes in the result: entries, children */ |
| 253 |
161817 |
| uint32_t datamap = 0, nodemap = 0, nd = 0, nc = 0, pos, i, N; |
| 254 |
|
| uint32_t na_slots, nb_slots; |
| 255 |
161817 |
| int same_a = 1, same_b = 1; |
| 256 |
|
| hamt_node *r; |
| 257 |
|
| |
| 258 |
161817 |
▶ | if (a == b) return hamt_node_retain(a); |
Branch: 1 branch
| Branch | True | False | Total |
| if (a == b) return hamt_node_retain(a); |
9258 |
152559 |
161817 |
|
| 259 |
152559 |
| c->visits++; |
| 260 |
152559 |
▶ | if (a->ncoll) return hamt_coll_merge(c, a, b); |
Branch: 1 branch
| Branch | True | False | Total |
| if (a->ncoll) return hamt_coll_merge(c, a, b); |
8122 |
144437 |
152559 |
|
| 261 |
|
| |
| 262 |
144437 |
| na_slots = hamt_node_nslots(a); |
| 263 |
144437 |
| nb_slots = hamt_node_nslots(b); |
| 264 |
|
| |
| 265 |
|
| /* Every pointer put in rd[] and rc[] is one this function owns a |
| 266 |
|
| * reference to, shared or newly made alike, so the two ways out - build |
| 267 |
|
| * the node, or give everything back - need not tell them apart. */ |
| 268 |
4682317 |
▶ | for (pos = 0; pos < 32; pos++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (pos = 0; pos < 32; pos++) { |
4542214 |
140103 |
4682317 |
|
| 269 |
4542214 |
| uint32_t bit = (uint32_t)1 << pos; |
| 270 |
4542214 |
| hamt_entry *ea = NULL, *eb = NULL; |
| 271 |
4542214 |
| hamt_node *ca = NULL, *cb = NULL; |
| 272 |
|
| |
| 273 |
4542214 |
▶ | if (a->datamap & bit) |
Branch: 1 branch
| Branch | True | False | Total |
| if (a->datamap & bit) |
235991 |
4306223 |
4542214 |
|
| 274 |
235991 |
| ea = (hamt_entry *)a->slot[hamt_popcnt32(a->datamap & (bit - 1))]; |
| 275 |
4306223 |
▶ | else if (a->nodemap & bit) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (a->nodemap & bit) |
182038 |
4124185 |
4306223 |
|
| 276 |
182038 |
| ca = (hamt_node *)a->slot[na_slots - 1 - hamt_popcnt32(a->nodemap & (bit - 1))]; |
| 277 |
4542214 |
▶ | if (b->datamap & bit) |
Branch: 1 branch
| Branch | True | False | Total |
| if (b->datamap & bit) |
234087 |
4308127 |
4542214 |
|
| 278 |
234087 |
| eb = (hamt_entry *)b->slot[hamt_popcnt32(b->datamap & (bit - 1))]; |
| 279 |
4308127 |
▶ | else if (b->nodemap & bit) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (b->nodemap & bit) |
179892 |
4128235 |
4308127 |
|
| 280 |
179892 |
| cb = (hamt_node *)b->slot[nb_slots - 1 - hamt_popcnt32(b->nodemap & (bit - 1))]; |
| 281 |
|
| |
| 282 |
4542214 |
▶ | if (!ea && !ca && !eb && !cb) continue; |
Branch: 4 branches
| Branch | True | False | Total |
| if (!ea && !ca && !eb && !cb) continue; |
4306223 |
235991 |
4542214 |
| if (!ea && !ca && !eb && !cb) continue; |
4124185 |
182038 |
4306223 |
| if (!ea && !ca && !eb && !cb) continue; |
3975045 |
149140 |
4124185 |
| if (!ea && !ca && !eb && !cb) continue; |
3959825 |
15220 |
3975045 |
|
| 283 |
|
| |
| 284 |
582389 |
▶ | if (!eb && !cb) { /* the first map's alone */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (!eb && !cb) { /* the first map's alone */ |
348302 |
234087 |
582389 |
| if (!eb && !cb) { /* the first map's alone */ |
168410 |
179892 |
348302 |
|
| 285 |
168410 |
| same_b = 0; |
| 286 |
168410 |
▶ | if (ea) { rd[nd++] = hamt_entry_retain(ea); datamap |= bit; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (ea) { rd[nd++] = hamt_entry_retain(ea); datamap |= bit; } |
150965 |
17445 |
168410 |
|
| 287 |
17445 |
| else { rc[nc++] = hamt_node_retain(ca); nodemap |= bit; } |
| 288 |
|
| } |
| 289 |
413979 |
▶ | else if (!ea && !ca) { /* the second's alone */ |
Branch: 2 branches
| Branch | True | False | Total |
| else if (!ea && !ca) { /* the second's alone */ |
328953 |
85026 |
413979 |
| else if (!ea && !ca) { /* the second's alone */ |
164360 |
164593 |
328953 |
|
| 290 |
164360 |
| same_a = 0; |
| 291 |
164360 |
▶ | if (eb) { rd[nd++] = hamt_entry_retain(eb); datamap |= bit; c->added++; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (eb) { rd[nd++] = hamt_entry_retain(eb); datamap |= bit; c->added++; } |
149140 |
15220 |
164360 |
|
| 292 |
|
| else { |
| 293 |
15220 |
| rc[nc++] = hamt_node_retain(cb); |
| 294 |
15220 |
| nodemap |= bit; |
| 295 |
15220 |
| c->added += hamt_node_count(cb); |
| 296 |
|
| } |
| 297 |
|
| } |
| 298 |
249619 |
▶ | else if (ea && eb) { |
Branch: 2 branches
| Branch | True | False | Total |
| else if (ea && eb) { |
85026 |
164593 |
249619 |
| else if (ea && eb) { |
75931 |
9095 |
85026 |
|
| 299 |
150136 |
▶ | if (hamt_entry_key_eq(eb, ea->key, ea->klen, ea->utf8, ea->hash)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hamt_entry_key_eq(eb, ea->key, ea->klen, ea->utf8, ea->hash)) { |
50787 |
25144 |
75931 |
|
| 300 |
50787 |
| hamt_entry *e = hamt_merge_resolve(c, ea, eb); |
| 301 |
50787 |
▶ | if (!e) goto fail; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!e) goto fail; |
1089 |
49698 |
50787 |
|
| 302 |
49698 |
▶ | if (e != ea) same_a = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (e != ea) same_a = 0; |
5849 |
43849 |
49698 |
|
| 303 |
49698 |
▶ | if (e != eb) same_b = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (e != eb) same_b = 0; |
37802 |
11896 |
49698 |
|
| 304 |
49698 |
| rd[nd++] = e; |
| 305 |
49698 |
| datamap |= bit; |
| 306 |
|
| } |
| 307 |
|
| else { /* two keys, one position */ |
| 308 |
25144 |
| hamt_node *sub = hamt_node_pair(c->cx, ea, eb, depth + 1); |
| 309 |
25144 |
▶ | if (!sub) { c->err = HAMT_ERR_OOM; goto fail; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sub) { c->err = HAMT_ERR_OOM; goto fail; } |
637 |
24507 |
25144 |
|
| 310 |
24507 |
| same_a = same_b = 0; |
| 311 |
24507 |
| c->added++; |
| 312 |
24507 |
| rc[nc++] = sub; |
| 313 |
24507 |
| nodemap |= bit; |
| 314 |
|
| } |
| 315 |
|
| } |
| 316 |
173688 |
▶ | else if (ea) { /* an entry meets a subtree */ |
Branch: 1 branch
| Branch | True | False | Total |
| else if (ea) { /* an entry meets a subtree */ |
9095 |
164593 |
173688 |
|
| 317 |
|
| int found; |
| 318 |
9095 |
| hamt_node *sub = hamt_merge_put(c, cb, depth + 1, ea, 1, &found); |
| 319 |
9095 |
▶ | if (!sub) goto fail; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sub) goto fail; |
72 |
9023 |
9095 |
|
| 320 |
9023 |
| same_a = 0; |
| 321 |
9023 |
▶ | if (sub != cb) same_b = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (sub != cb) same_b = 0; |
5390 |
3633 |
9023 |
|
| 322 |
9023 |
▶ | c->added += hamt_node_count(cb) - (found ? 1 : 0); |
Branch: 1 branch
| Branch | True | False | Total |
| c->added += hamt_node_count(cb) - (found ? 1 : 0); |
3762 |
5261 |
9023 |
|
| 323 |
9023 |
| rc[nc++] = sub; |
| 324 |
9023 |
| nodemap |= bit; |
| 325 |
|
| } |
| 326 |
164593 |
▶ | else if (eb) { /* a subtree meets an entry */ |
Branch: 1 branch
| Branch | True | False | Total |
| else if (eb) { /* a subtree meets an entry */ |
9016 |
155577 |
164593 |
|
| 327 |
|
| int found; |
| 328 |
9016 |
| hamt_node *sub = hamt_merge_put(c, ca, depth + 1, eb, 0, &found); |
| 329 |
9016 |
▶ | if (!sub) goto fail; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sub) goto fail; |
73 |
8943 |
9016 |
|
| 330 |
8943 |
▶ | if (sub != ca) same_a = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (sub != ca) same_a = 0; |
4843 |
4100 |
8943 |
|
| 331 |
8943 |
| same_b = 0; |
| 332 |
8943 |
▶ | if (!found) c->added++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!found) c->added++; |
4555 |
4388 |
8943 |
|
| 333 |
8943 |
| rc[nc++] = sub; |
| 334 |
8943 |
| nodemap |= bit; |
| 335 |
|
| } |
| 336 |
|
| else { /* two subtrees */ |
| 337 |
155577 |
| hamt_node *sub = hamt_node_merge(c, ca, cb, depth + 1); |
| 338 |
155577 |
▶ | if (!sub) goto fail; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sub) goto fail; |
2463 |
153114 |
155577 |
|
| 339 |
153114 |
▶ | if (sub != ca) same_a = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (sub != ca) same_a = 0; |
133686 |
19428 |
153114 |
|
| 340 |
153114 |
▶ | if (sub != cb) same_b = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (sub != cb) same_b = 0; |
140604 |
12510 |
153114 |
|
| 341 |
153114 |
| rc[nc++] = sub; |
| 342 |
153114 |
| nodemap |= bit; |
| 343 |
|
| } |
| 344 |
|
| } |
| 345 |
|
| |
| 346 |
140103 |
▶ | if (same_a || same_b) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (same_a || same_b) { |
130241 |
9862 |
140103 |
| if (same_a || same_b) { |
3458 |
126783 |
130241 |
|
| 347 |
30809 |
▶ | for (i = 0; i < nd; i++) hamt_entry_release(c->cx, (hamt_entry *)rd[i]); |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < nd; i++) hamt_entry_release(c->cx, (hamt_entry *)rd[i]); |
17489 |
13320 |
30809 |
|
| 348 |
29335 |
▶ | for (i = 0; i < nc; i++) hamt_node_release(c->cx, (hamt_node *)rc[i]); |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < nc; i++) hamt_node_release(c->cx, (hamt_node *)rc[i]); |
16015 |
13320 |
29335 |
|
| 349 |
13320 |
▶ | return hamt_node_retain(same_a ? a : b); |
Branch: 1 branch
| Branch | True | False | Total |
| return hamt_node_retain(same_a ? a : b); |
9862 |
3458 |
13320 |
|
| 350 |
|
| } |
| 351 |
|
| |
| 352 |
126783 |
| N = nd + nc; |
| 353 |
126783 |
| r = hamt_node_new(c->cx, N); |
| 354 |
126783 |
▶ | if (!r) { c->err = HAMT_ERR_OOM; goto fail; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!r) { c->err = HAMT_ERR_OOM; goto fail; } |
241 |
126542 |
126783 |
|
| 355 |
126542 |
| r->datamap = datamap; |
| 356 |
126542 |
| r->nodemap = nodemap; |
| 357 |
451435 |
▶ | for (i = 0; i < nd; i++) r->slot[i] = rd[i]; |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < nd; i++) r->slot[i] = rd[i]; |
324893 |
126542 |
451435 |
|
| 358 |
322149 |
▶ | for (i = 0; i < nc; i++) r->slot[N - 1 - i] = rc[i]; /* lowest position last */ |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < nc; i++) r->slot[N - 1 - i] = rc[i]; /* lowest position last */ |
195607 |
126542 |
322149 |
|
| 359 |
126542 |
| return r; |
| 360 |
|
| |
| 361 |
4575 |
| fail: |
| 362 |
11996 |
▶ | for (i = 0; i < nd; i++) hamt_entry_release(c->cx, (hamt_entry *)rd[i]); |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < nd; i++) hamt_entry_release(c->cx, (hamt_entry *)rd[i]); |
7421 |
4575 |
11996 |
|
| 363 |
21205 |
▶ | for (i = 0; i < nc; i++) hamt_node_release(c->cx, (hamt_node *)rc[i]); |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < nc; i++) hamt_node_release(c->cx, (hamt_node *)rc[i]); |
16630 |
4575 |
21205 |
|
| 364 |
4575 |
| return NULL; |
| 365 |
|
| } |
| 366 |
|
| |
| 367 |
|
| /* The root of a trie holding every key of both. Where a key is in both, the |
| 368 |
|
| * resolver chooses, or the second map's entry wins when there is none. |
| 369 |
|
| * |
| 370 |
|
| * One reference, owned by the caller. c->added is how many keys the second |
| 371 |
|
| * map brought that the first did not have, so the result's count is the |
| 372 |
|
| * first map's plus that. The result IS `a` when `b` added and changed |
| 373 |
|
| * nothing, and IS `b` when the result is exactly `b`. |
| 374 |
|
| * |
| 375 |
|
| * NULL with c->err set means it stopped: nothing is leaked and neither |
| 376 |
|
| * input is touched. NULL with c->err clear means both were empty. */ |
| 377 |
6252 |
| static hamt_node *hamt_map_merge(hamt_merge_ctx *c, hamt_node *a, hamt_node *b) { |
| 378 |
6252 |
| c->added = 0; |
| 379 |
6252 |
| c->visits = 0; |
| 380 |
6252 |
| c->err = HAMT_OK; |
| 381 |
6252 |
▶ | if (!b) return a ? hamt_node_retain(a) : NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!b) return a ? hamt_node_retain(a) : NULL; |
7 |
6245 |
6252 |
| if (!b) return a ? hamt_node_retain(a) : NULL; |
2 |
5 |
7 |
|
| 382 |
6245 |
▶ | if (!a) { c->added = hamt_node_count(b); return hamt_node_retain(b); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!a) { c->added = hamt_node_count(b); return hamt_node_retain(b); } |
5 |
6240 |
6245 |
|
| 383 |
6240 |
| return hamt_node_merge(c, a, b, 0); |
| 384 |
|
| } |
| 385 |
|
| |
| 386 |
|
| /* ---- difference -------------------------------------------------------- */ |
| 387 |
|
| |
| 388 |
|
| #define HAMT_DIFF_ADD 1 /* in the second map only: ea is NULL */ |
| 389 |
|
| #define HAMT_DIFF_REMOVE 2 /* in the first map only: eb is NULL */ |
| 390 |
|
| #define HAMT_DIFF_CHANGE 3 /* in both, with values that are not equal */ |
| 391 |
|
| |
| 392 |
|
| /* 0 to carry on, anything else to stop. */ |
| 393 |
|
| typedef int (*hamt_diff_fn)(void *ud, void *cx, int kind, hamt_entry *ea, |
| 394 |
|
| hamt_entry *eb); |
| 395 |
|
| |
| 396 |
|
| typedef struct hamt_diff_ctx { |
| 397 |
|
| void *cx; |
| 398 |
|
| hamt_diff_fn fn; |
| 399 |
|
| void *ud; |
| 400 |
|
| hamt_eq_ctx eq; /* how two values under one key are compared */ |
| 401 |
|
| size_t visits; /* node pairs looked at */ |
| 402 |
|
| size_t reported; |
| 403 |
|
| } hamt_diff_ctx; |
| 404 |
|
| |
| 405 |
206979 |
| static int hamt_diff_report(hamt_diff_ctx *c, int kind, hamt_entry *ea, |
| 406 |
|
| hamt_entry *eb) { |
| 407 |
206979 |
| c->reported++; |
| 408 |
206979 |
▶ | return c->fn ? c->fn(c->ud, c->cx, kind, ea, eb) : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| return c->fn ? c->fn(c->ud, c->cx, kind, ea, eb) : 0; |
201649 |
5330 |
206979 |
|
| 409 |
|
| } |
| 410 |
|
| |
| 411 |
|
| /* The same key on both sides: a change, or nothing. */ |
| 412 |
22664 |
| static int hamt_diff_same_key(hamt_diff_ctx *c, hamt_entry *ea, hamt_entry *eb) { |
| 413 |
|
| int rc; |
| 414 |
22664 |
▶ | if (ea == eb) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ea == eb) return 0; |
5538 |
17126 |
22664 |
|
| 415 |
17126 |
| rc = hamt_val_same(&c->eq, ea->val, eb->val); |
| 416 |
|
| /* |
| 417 |
|
| # uncoverable branch true |
| 418 |
|
| # uncoverable condition left count:1 */ |
| 419 |
17126 |
▶ | if (rc < 0) return HAMT_ERR_ABORT; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc < 0) return HAMT_ERR_ABORT; |
0 |
17126 |
17126 |
|
| 420 |
17126 |
▶ | return rc ? 0 : hamt_diff_report(c, HAMT_DIFF_CHANGE, ea, eb); |
Branch: 1 branch
| Branch | True | False | Total |
| return rc ? 0 : hamt_diff_report(c, HAMT_DIFF_CHANGE, ea, eb); |
13915 |
3211 |
17126 |
|
| 421 |
|
| } |
| 422 |
|
| |
| 423 |
|
| /* Everything under a subtree is on one side only. */ |
| 424 |
10408 |
| static int hamt_diff_all(hamt_diff_ctx *c, const hamt_node *n, int kind) { |
| 425 |
|
| hamt_iter it; |
| 426 |
|
| hamt_entry *e; |
| 427 |
|
| int rc; |
| 428 |
10408 |
| hamt_iter_init(&it, n); |
| 429 |
186419 |
▶ | while ((e = hamt_iter_next(&it))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((e = hamt_iter_next(&it))) { |
177152 |
9267 |
186419 |
|
| 430 |
88723 |
| rc = kind == HAMT_DIFF_ADD ? hamt_diff_report(c, kind, NULL, e) |
| 431 |
177152 |
▶ | : hamt_diff_report(c, kind, e, NULL); |
Branch: 1 branch
| Branch | True | False | Total |
| : hamt_diff_report(c, kind, e, NULL); |
88723 |
88429 |
177152 |
|
| 432 |
177152 |
▶ | if (rc) return rc; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc) return rc; |
1141 |
176011 |
177152 |
|
| 433 |
|
| } |
| 434 |
9267 |
| return 0; |
| 435 |
|
| } |
| 436 |
|
| |
| 437 |
|
| /* One entry against a subtree of the other map. `kind` is what the |
| 438 |
|
| * subtree's other entries are: ADD when the subtree is the second map's. */ |
| 439 |
1399 |
| static int hamt_diff_entry_vs_node(hamt_diff_ctx *c, hamt_entry *e, |
| 440 |
|
| const hamt_node *n, int kind) { |
| 441 |
|
| hamt_iter it; |
| 442 |
|
| hamt_entry *x; |
| 443 |
1399 |
| int rc, found = 0; |
| 444 |
|
| |
| 445 |
1399 |
| hamt_iter_init(&it, n); |
| 446 |
9196 |
▶ | while ((x = hamt_iter_next(&it))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((x = hamt_iter_next(&it))) { |
7857 |
1339 |
9196 |
|
| 447 |
7857 |
▶ | if (hamt_entry_key_eq(x, e->key, e->klen, e->utf8, e->hash)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hamt_entry_key_eq(x, e->key, e->klen, e->utf8, e->hash)) { |
1311 |
6546 |
7857 |
|
| 448 |
1311 |
| found = 1; |
| 449 |
1311 |
| rc = kind == HAMT_DIFF_ADD ? hamt_diff_same_key(c, e, x) |
| 450 |
1311 |
▶ | : hamt_diff_same_key(c, x, e); |
Branch: 1 branch
| Branch | True | False | Total |
| : hamt_diff_same_key(c, x, e); |
834 |
477 |
1311 |
|
| 451 |
|
| } |
| 452 |
|
| else { |
| 453 |
6546 |
| rc = kind == HAMT_DIFF_ADD ? hamt_diff_report(c, kind, NULL, x) |
| 454 |
6546 |
▶ | : hamt_diff_report(c, kind, x, NULL); |
Branch: 1 branch
| Branch | True | False | Total |
| : hamt_diff_report(c, kind, x, NULL); |
3617 |
2929 |
6546 |
|
| 455 |
|
| } |
| 456 |
7857 |
▶ | if (rc) return rc; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc) return rc; |
60 |
7797 |
7857 |
|
| 457 |
|
| } |
| 458 |
1339 |
▶ | if (found) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (found) return 0; |
1295 |
44 |
1339 |
|
| 459 |
23 |
| return kind == HAMT_DIFF_ADD ? hamt_diff_report(c, HAMT_DIFF_REMOVE, e, NULL) |
| 460 |
67 |
▶ | : hamt_diff_report(c, HAMT_DIFF_ADD, NULL, e); |
Branch: 1 branch
| Branch | True | False | Total |
| : hamt_diff_report(c, HAMT_DIFF_ADD, NULL, e); |
23 |
21 |
44 |
|
| 461 |
|
| } |
| 462 |
|
| |
| 463 |
11475 |
| static int hamt_node_diff(hamt_diff_ctx *c, const hamt_node *a, const hamt_node *b) { |
| 464 |
|
| uint32_t pos, i, j, na_slots, nb_slots; |
| 465 |
|
| int rc; |
| 466 |
|
| |
| 467 |
11475 |
▶ | if (a == b) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (a == b) return 0; |
755 |
10720 |
11475 |
|
| 468 |
10720 |
| c->visits++; |
| 469 |
|
| |
| 470 |
10720 |
▶ | if (a->ncoll) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (a->ncoll) { |
310 |
10410 |
10720 |
|
| 471 |
2944 |
▶ | for (i = 0; i < a->ncoll; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < a->ncoll; i++) { |
2638 |
306 |
2944 |
|
| 472 |
2638 |
| hamt_entry *ea = (hamt_entry *)a->slot[i], *eb = NULL; |
| 473 |
53433 |
▶ | for (j = 0; j < b->ncoll; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < b->ncoll; j++) { |
52664 |
769 |
53433 |
|
| 474 |
52664 |
| eb = (hamt_entry *)b->slot[j]; |
| 475 |
52664 |
▶ | if (hamt_entry_key_eq(eb, ea->key, ea->klen, ea->utf8, ea->hash)) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (hamt_entry_key_eq(eb, ea->key, ea->klen, ea->utf8, ea->hash)) break; |
1869 |
50795 |
52664 |
|
| 476 |
|
| } |
| 477 |
769 |
| rc = j == b->ncoll ? hamt_diff_report(c, HAMT_DIFF_REMOVE, ea, NULL) |
| 478 |
2638 |
▶ | : hamt_diff_same_key(c, ea, eb); |
Branch: 1 branch
| Branch | True | False | Total |
| : hamt_diff_same_key(c, ea, eb); |
769 |
1869 |
2638 |
|
| 479 |
2638 |
▶ | if (rc) return rc; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc) return rc; |
4 |
2634 |
2638 |
|
| 480 |
|
| } |
| 481 |
2932 |
▶ | for (j = 0; j < b->ncoll; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < b->ncoll; j++) { |
2628 |
304 |
2932 |
|
| 482 |
2628 |
| hamt_entry *eb = (hamt_entry *)b->slot[j]; |
| 483 |
53413 |
▶ | for (i = 0; i < a->ncoll; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < a->ncoll; i++) { |
52646 |
767 |
53413 |
|
| 484 |
52646 |
| hamt_entry *ea = (hamt_entry *)a->slot[i]; |
| 485 |
52646 |
▶ | if (hamt_entry_key_eq(ea, eb->key, eb->klen, eb->utf8, eb->hash)) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (hamt_entry_key_eq(ea, eb->key, eb->klen, eb->utf8, eb->hash)) break; |
1861 |
50785 |
52646 |
|
| 486 |
|
| } |
| 487 |
2628 |
▶ | if (i < a->ncoll) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (i < a->ncoll) continue; |
1861 |
767 |
2628 |
|
| 488 |
767 |
| rc = hamt_diff_report(c, HAMT_DIFF_ADD, NULL, eb); |
| 489 |
767 |
▶ | if (rc) return rc; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc) return rc; |
2 |
765 |
767 |
|
| 490 |
|
| } |
| 491 |
304 |
| return 0; |
| 492 |
|
| } |
| 493 |
|
| |
| 494 |
10410 |
| na_slots = hamt_node_nslots(a); |
| 495 |
10410 |
| nb_slots = hamt_node_nslots(b); |
| 496 |
|
| |
| 497 |
331309 |
▶ | for (pos = 0; pos < 32; pos++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (pos = 0; pos < 32; pos++) { |
321583 |
9726 |
331309 |
|
| 498 |
321583 |
| uint32_t bit = (uint32_t)1 << pos; |
| 499 |
321583 |
| hamt_entry *ea = NULL, *eb = NULL; |
| 500 |
321583 |
| const hamt_node *ca = NULL, *cb = NULL; |
| 501 |
|
| |
| 502 |
321583 |
▶ | if (a->datamap & bit) |
Branch: 1 branch
| Branch | True | False | Total |
| if (a->datamap & bit) |
22942 |
298641 |
321583 |
|
| 503 |
22942 |
| ea = (hamt_entry *)a->slot[hamt_popcnt32(a->datamap & (bit - 1))]; |
| 504 |
298641 |
▶ | else if (a->nodemap & bit) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (a->nodemap & bit) |
14604 |
284037 |
298641 |
|
| 505 |
14604 |
| ca = (const hamt_node *)a->slot[na_slots - 1 - hamt_popcnt32(a->nodemap & (bit - 1))]; |
| 506 |
321583 |
▶ | if (b->datamap & bit) |
Branch: 1 branch
| Branch | True | False | Total |
| if (b->datamap & bit) |
25212 |
296371 |
321583 |
|
| 507 |
25212 |
| eb = (hamt_entry *)b->slot[hamt_popcnt32(b->datamap & (bit - 1))]; |
| 508 |
296371 |
▶ | else if (b->nodemap & bit) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (b->nodemap & bit) |
15066 |
281305 |
296371 |
|
| 509 |
15066 |
| cb = (const hamt_node *)b->slot[nb_slots - 1 - hamt_popcnt32(b->nodemap & (bit - 1))]; |
| 510 |
|
| |
| 511 |
321583 |
▶ | if (!ea && !ca && !eb && !cb) continue; |
Branch: 4 branches
| Branch | True | False | Total |
| if (!ea && !ca && !eb && !cb) continue; |
298641 |
22942 |
321583 |
| if (!ea && !ca && !eb && !cb) continue; |
284037 |
14604 |
298641 |
| if (!ea && !ca && !eb && !cb) continue; |
279052 |
4985 |
284037 |
| if (!ea && !ca && !eb && !cb) continue; |
274099 |
4953 |
279052 |
|
| 512 |
|
| |
| 513 |
47484 |
▶ | if (!eb && !cb) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!eb && !cb) |
22272 |
25212 |
47484 |
| if (!eb && !cb) |
7206 |
15066 |
22272 |
|
| 514 |
7206 |
| rc = ea ? hamt_diff_report(c, HAMT_DIFF_REMOVE, ea, NULL) |
| 515 |
7206 |
▶ | : hamt_diff_all(c, ca, HAMT_DIFF_REMOVE); |
Branch: 1 branch
| Branch | True | False | Total |
| : hamt_diff_all(c, ca, HAMT_DIFF_REMOVE); |
2356 |
4850 |
7206 |
|
| 516 |
40278 |
▶ | else if (!ea && !ca) |
Branch: 2 branches
| Branch | True | False | Total |
| else if (!ea && !ca) |
19692 |
20586 |
40278 |
| else if (!ea && !ca) |
9938 |
9754 |
19692 |
|
| 517 |
9938 |
| rc = eb ? hamt_diff_report(c, HAMT_DIFF_ADD, NULL, eb) |
| 518 |
9938 |
▶ | : hamt_diff_all(c, cb, HAMT_DIFF_ADD); |
Branch: 1 branch
| Branch | True | False | Total |
| : hamt_diff_all(c, cb, HAMT_DIFF_ADD); |
4985 |
4953 |
9938 |
|
| 519 |
30340 |
▶ | else if (ea && eb) { |
Branch: 2 branches
| Branch | True | False | Total |
| else if (ea && eb) { |
20586 |
9754 |
30340 |
| else if (ea && eb) { |
19707 |
879 |
20586 |
|
| 520 |
39414 |
▶ | if (hamt_entry_key_eq(eb, ea->key, ea->klen, ea->utf8, ea->hash)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (hamt_entry_key_eq(eb, ea->key, ea->klen, ea->utf8, ea->hash)) |
19484 |
223 |
19707 |
|
| 521 |
19484 |
| rc = hamt_diff_same_key(c, ea, eb); |
| 522 |
|
| else { |
| 523 |
223 |
| rc = hamt_diff_report(c, HAMT_DIFF_REMOVE, ea, NULL); |
| 524 |
223 |
▶ | if (!rc) rc = hamt_diff_report(c, HAMT_DIFF_ADD, NULL, eb); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!rc) rc = hamt_diff_report(c, HAMT_DIFF_ADD, NULL, eb); |
222 |
1 |
223 |
|
| 525 |
|
| } |
| 526 |
|
| } |
| 527 |
10633 |
▶ | else if (ea) rc = hamt_diff_entry_vs_node(c, ea, cb, HAMT_DIFF_ADD); |
Branch: 1 branch
| Branch | True | False | Total |
| else if (ea) rc = hamt_diff_entry_vs_node(c, ea, cb, HAMT_DIFF_ADD); |
879 |
9754 |
10633 |
|
| 528 |
9754 |
▶ | else if (eb) rc = hamt_diff_entry_vs_node(c, eb, ca, HAMT_DIFF_REMOVE); |
Branch: 1 branch
| Branch | True | False | Total |
| else if (eb) rc = hamt_diff_entry_vs_node(c, eb, ca, HAMT_DIFF_REMOVE); |
520 |
9234 |
9754 |
|
| 529 |
9234 |
| else rc = hamt_node_diff(c, ca, cb); |
| 530 |
|
| |
| 531 |
47484 |
▶ | if (rc) return rc; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc) return rc; |
684 |
46800 |
47484 |
|
| 532 |
|
| } |
| 533 |
9726 |
| return 0; |
| 534 |
|
| } |
| 535 |
|
| |
| 536 |
|
| /* Reports every key that is in one map and not the other, or in both with |
| 537 |
|
| * values that are not equal, once each and in no promised order. Allocates |
| 538 |
|
| * nothing. 0, or whatever stopped it. */ |
| 539 |
3382 |
| static int hamt_map_diff(hamt_diff_ctx *c, const hamt_node *a, const hamt_node *b) { |
| 540 |
3382 |
| c->visits = 0; |
| 541 |
3382 |
| c->reported = 0; |
| 542 |
3382 |
| c->eq.visits = 0; |
| 543 |
3382 |
▶ | if (a == b) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (a == b) return 0; |
536 |
2846 |
3382 |
|
| 544 |
2846 |
▶ | if (!a) return hamt_diff_all(c, b, HAMT_DIFF_ADD); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!a) return hamt_diff_all(c, b, HAMT_DIFF_ADD); |
302 |
2544 |
2846 |
|
| 545 |
2544 |
▶ | if (!b) return hamt_diff_all(c, a, HAMT_DIFF_REMOVE); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!b) return hamt_diff_all(c, a, HAMT_DIFF_REMOVE); |
303 |
2241 |
2544 |
|
| 546 |
2241 |
| return hamt_node_diff(c, a, b); |
| 547 |
|
| } |
| 548 |
|
| |
| 549 |
|
| /* ---- intersection and difference -------------------------------------- |
| 550 |
|
| * |
| 551 |
|
| * Both keep some of the FIRST trie's entries and none of the second's: the |
| 552 |
|
| * intersection keeps those whose key the second trie has, the difference |
| 553 |
|
| * those whose key it has not. Values play no part, so these serve a set, |
| 554 |
|
| * whose entries have none, and would serve a map's keys as well. |
| 555 |
|
| * |
| 556 |
|
| * Unlike a union, these REMOVE, so a subtree can come back smaller than a |
| 557 |
|
| * subtree is allowed to be. The canonical shape is restored on the way up, |
| 558 |
|
| * exactly as delete does it: what comes back from a position is a part - |
| 559 |
|
| * nothing, or a lone entry for the parent to hold in place of the child, or |
| 560 |
|
| * a node. |
| 561 |
|
| * |
| 562 |
|
| * COUNTING. The result's size has to be known without walking the result, |
| 563 |
|
| * or the speed of skipping shared subtrees is spent counting them. Two |
| 564 |
|
| * tallies are kept - entries kept and entries removed - and either gives |
| 565 |
|
| * the size, as `kept` or as the first trie's count less `removed`. Each is |
| 566 |
|
| * exact only if one kind of skipped subtree is walked after all: `kept` |
| 567 |
|
| * needs the subtrees that are kept whole, `removed` the ones removed whole. |
| 568 |
|
| * `count_shared` picks: when the second trie is small next to the first, |
| 569 |
|
| * the subtrees the two have in common are small, and those are the ones |
| 570 |
|
| * walked; otherwise the ones only the first has. The caller sets it from |
| 571 |
|
| * the two counts, and reads back whichever tally that makes exact. */ |
| 572 |
|
| |
| 573 |
|
| #define HAMT_PART_NONE 0 |
| 574 |
|
| #define HAMT_PART_ENTRY 1 |
| 575 |
|
| #define HAMT_PART_NODE 2 |
| 576 |
|
| |
| 577 |
|
| typedef struct hamt_part { |
| 578 |
|
| int kind; |
| 579 |
|
| void *p; /* one reference, the receiver's */ |
| 580 |
|
| } hamt_part; |
| 581 |
|
| |
| 582 |
|
| typedef struct hamt_set_ctx { |
| 583 |
|
| void *cx; |
| 584 |
|
| int count_shared; /* walk the subtrees common to both, not the first's own */ |
| 585 |
|
| size_t kept; |
| 586 |
|
| size_t removed; |
| 587 |
|
| size_t visits; /* node pairs looked at */ |
| 588 |
|
| size_t walked; /* entries counted by walking a subtree */ |
| 589 |
|
| int err; |
| 590 |
|
| } hamt_set_ctx; |
| 591 |
|
| |
| 592 |
|
| /* The size of a subtree that is being kept or removed whole, found by |
| 593 |
|
| * walking it. The tally of what this costs is what the tests hold to |
| 594 |
|
| * account: it must stay small whenever one of the two results is. */ |
| 595 |
19459 |
| static size_t hamt_set_walk(hamt_set_ctx *c, const hamt_node *n) { |
| 596 |
19459 |
| size_t k = hamt_node_count(n); |
| 597 |
19459 |
| c->walked += k; |
| 598 |
19459 |
| return k; |
| 599 |
|
| } |
| 600 |
|
| |
| 601 |
256424 |
| static hamt_part hamt_part_make(int kind, void *p) { |
| 602 |
|
| hamt_part r; |
| 603 |
256424 |
| r.kind = kind; |
| 604 |
256424 |
| r.p = p; |
| 605 |
256424 |
| return r; |
| 606 |
|
| } |
| 607 |
|
| |
| 608 |
|
| /* What a position's gathered entries and children amount to. Every pointer |
| 609 |
|
| * in rd[] and rc[] is one the caller owns a reference to; all of them are |
| 610 |
|
| * used or released here. `same` means they are exactly what `a` holds, in |
| 611 |
|
| * which case `a` itself is the answer. */ |
| 612 |
229664 |
| static hamt_part hamt_part_build(hamt_set_ctx *c, hamt_node *a, unsigned depth, |
| 613 |
|
| int same, uint32_t datamap, uint32_t nodemap, |
| 614 |
|
| void **rd, uint32_t nd, void **rc, uint32_t nc) { |
| 615 |
229664 |
| uint32_t i, N = nd + nc; |
| 616 |
|
| hamt_node *r; |
| 617 |
|
| |
| 618 |
229664 |
▶ | if (same || N == 0 || c->err) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (same || N == 0 || c->err) { |
167757 |
61907 |
229664 |
| if (same || N == 0 || c->err) { |
104978 |
62779 |
167757 |
| if (same || N == 0 || c->err) { |
53 |
104925 |
104978 |
|
| 619 |
175956 |
▶ | for (i = 0; i < nd; i++) hamt_entry_release(c->cx, (hamt_entry *)rd[i]); |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < nd; i++) hamt_entry_release(c->cx, (hamt_entry *)rd[i]); |
51217 |
124739 |
175956 |
|
| 620 |
196052 |
▶ | for (i = 0; i < nc; i++) hamt_node_release(c->cx, (hamt_node *)rc[i]); |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < nc; i++) hamt_node_release(c->cx, (hamt_node *)rc[i]); |
71313 |
124739 |
196052 |
|
| 621 |
124739 |
▶ | if (c->err || N == 0) return hamt_part_make(HAMT_PART_NONE, NULL); |
Branch: 2 branches
| Branch | True | False | Total |
| if (c->err || N == 0) return hamt_part_make(HAMT_PART_NONE, NULL); |
124501 |
238 |
124739 |
| if (c->err || N == 0) return hamt_part_make(HAMT_PART_NONE, NULL); |
62594 |
61907 |
124501 |
|
| 622 |
61907 |
| return hamt_part_make(HAMT_PART_NODE, hamt_node_retain(a)); |
| 623 |
|
| } |
| 624 |
|
| /* One entry and nothing else, below the root: the parent holds it. */ |
| 625 |
104925 |
▶ | if (nc == 0 && nd == 1 && depth > 0) |
Branch: 3 branches
| Branch | True | False | Total |
| if (nc == 0 && nd == 1 && depth > 0) |
45298 |
59627 |
104925 |
| if (nc == 0 && nd == 1 && depth > 0) |
28191 |
17107 |
45298 |
| if (nc == 0 && nd == 1 && depth > 0) |
28038 |
153 |
28191 |
|
| 626 |
28038 |
| return hamt_part_make(HAMT_PART_ENTRY, rd[0]); |
| 627 |
|
| |
| 628 |
76887 |
| r = hamt_node_new(c->cx, N); |
| 629 |
76887 |
▶ | if (!r) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!r) { |
62 |
76825 |
76887 |
|
| 630 |
62 |
| c->err = HAMT_ERR_OOM; |
| 631 |
183 |
▶ | for (i = 0; i < nd; i++) hamt_entry_release(c->cx, (hamt_entry *)rd[i]); |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < nd; i++) hamt_entry_release(c->cx, (hamt_entry *)rd[i]); |
121 |
62 |
183 |
|
| 632 |
196 |
▶ | for (i = 0; i < nc; i++) hamt_node_release(c->cx, (hamt_node *)rc[i]); |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < nc; i++) hamt_node_release(c->cx, (hamt_node *)rc[i]); |
134 |
62 |
196 |
|
| 633 |
62 |
| return hamt_part_make(HAMT_PART_NONE, NULL); |
| 634 |
|
| } |
| 635 |
76825 |
| r->datamap = datamap; |
| 636 |
76825 |
| r->nodemap = nodemap; |
| 637 |
151896 |
▶ | for (i = 0; i < nd; i++) r->slot[i] = rd[i]; |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < nd; i++) r->slot[i] = rd[i]; |
75071 |
76825 |
151896 |
|
| 638 |
171912 |
▶ | for (i = 0; i < nc; i++) r->slot[N - 1 - i] = rc[i]; |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < nc; i++) r->slot[N - 1 - i] = rc[i]; |
95087 |
76825 |
171912 |
|
| 639 |
76825 |
| return hamt_part_make(HAMT_PART_NODE, r); |
| 640 |
|
| } |
| 641 |
|
| |
| 642 |
|
| /* Two collision nodes: the first's entries that the second has (`keep_common`) |
| 643 |
|
| * or has not. */ |
| 644 |
15620 |
| static hamt_part hamt_coll_filter(hamt_set_ctx *c, hamt_node *a, hamt_node *b, |
| 645 |
|
| int keep_common) { |
| 646 |
15620 |
| size_t bytes = (size_t)a->ncoll * sizeof(void *); |
| 647 |
15620 |
| hamt_entry **list = (hamt_entry **)HAMT_ALLOC(c->cx, bytes); |
| 648 |
15620 |
| uint32_t n = 0, i, j; |
| 649 |
15620 |
| hamt_part out = hamt_part_make(HAMT_PART_NONE, NULL); |
| 650 |
|
| |
| 651 |
15620 |
▶ | if (!list) { c->err = HAMT_ERR_OOM; return out; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!list) { c->err = HAMT_ERR_OOM; return out; } |
2 |
15618 |
15620 |
|
| 652 |
104432 |
▶ | for (i = 0; i < a->ncoll; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < a->ncoll; i++) { |
88814 |
15618 |
104432 |
|
| 653 |
88814 |
| hamt_entry *ea = (hamt_entry *)a->slot[i]; |
| 654 |
572714 |
▶ | for (j = 0; j < b->ncoll; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < b->ncoll; j++) { |
507174 |
65540 |
572714 |
|
| 655 |
507174 |
| hamt_entry *eb = (hamt_entry *)b->slot[j]; |
| 656 |
507174 |
▶ | if (hamt_entry_key_eq(eb, ea->key, ea->klen, ea->utf8, ea->hash)) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (hamt_entry_key_eq(eb, ea->key, ea->klen, ea->utf8, ea->hash)) break; |
23274 |
483900 |
507174 |
|
| 657 |
|
| } |
| 658 |
88814 |
▶ | if ((j < b->ncoll) == (keep_common != 0)) { list[n++] = ea; c->kept++; } |
Branch: 1 branch
| Branch | True | False | Total |
| if ((j < b->ncoll) == (keep_common != 0)) { list[n++] = ea; c->kept++; } |
44407 |
44407 |
88814 |
|
| 659 |
44407 |
| else c->removed++; |
| 660 |
|
| } |
| 661 |
|
| |
| 662 |
15618 |
▶ | if (n == a->ncoll) out = hamt_part_make(HAMT_PART_NODE, hamt_node_retain(a)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == a->ncoll) out = hamt_part_make(HAMT_PART_NODE, hamt_node_retain(a)); |
4476 |
11142 |
15618 |
|
| 663 |
11142 |
▶ | else if (n == 1) out = hamt_part_make(HAMT_PART_ENTRY, hamt_entry_retain(list[0])); |
Branch: 1 branch
| Branch | True | False | Total |
| else if (n == 1) out = hamt_part_make(HAMT_PART_ENTRY, hamt_entry_retain(list[0])); |
1632 |
9510 |
11142 |
|
| 664 |
9510 |
▶ | else if (n > 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (n > 1) { |
5034 |
4476 |
9510 |
|
| 665 |
5034 |
| hamt_node *r = hamt_node_new(c->cx, n); |
| 666 |
5034 |
▶ | if (!r) c->err = HAMT_ERR_OOM; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!r) c->err = HAMT_ERR_OOM; |
2 |
5032 |
5034 |
|
| 667 |
|
| else { |
| 668 |
5032 |
| r->ncoll = n; |
| 669 |
5032 |
| memcpy(r->slot, list, (size_t)n * sizeof(void *)); |
| 670 |
5032 |
| hamt_node_adopt(r, HAMT_NO_SKIP); |
| 671 |
5032 |
| out = hamt_part_make(HAMT_PART_NODE, r); |
| 672 |
|
| } |
| 673 |
|
| } |
| 674 |
15618 |
| HAMT_FREE(c->cx, list, bytes); |
| 675 |
15618 |
| return out; |
| 676 |
|
| } |
| 677 |
|
| |
| 678 |
|
| /* Where the gathering loop puts a part that came back from below. */ |
| 679 |
|
| #define HAMT_PART_PLACE(part, child, bit) \ |
| 680 |
|
| do { \ |
| 681 |
|
| if ((part).kind == HAMT_PART_NODE) { \ |
| 682 |
|
| if ((part).p != (void *)(child)) same = 0; \ |
| 683 |
|
| rc[nc++] = (part).p; nodemap |= (bit); \ |
| 684 |
|
| } \ |
| 685 |
|
| else { \ |
| 686 |
|
| same = 0; \ |
| 687 |
|
| if ((part).kind == HAMT_PART_ENTRY) { \ |
| 688 |
|
| rd[nd++] = (part).p; datamap |= (bit); \ |
| 689 |
|
| } \ |
| 690 |
|
| } \ |
| 691 |
|
| } while (0) |
| 692 |
|
| |
| 693 |
125120 |
| static hamt_part hamt_node_intersect(hamt_set_ctx *c, hamt_node *a, hamt_node *b, |
| 694 |
|
| unsigned depth) { |
| 695 |
|
| void *rd[32], *rc[32]; |
| 696 |
125120 |
| uint32_t datamap = 0, nodemap = 0, nd = 0, nc = 0, pos, na_slots, nb_slots; |
| 697 |
125120 |
| int same = 1; |
| 698 |
|
| |
| 699 |
125120 |
| c->visits++; |
| 700 |
125120 |
▶ | if (a->ncoll) return hamt_coll_filter(c, a, b, 1); |
Branch: 1 branch
| Branch | True | False | Total |
| if (a->ncoll) return hamt_coll_filter(c, a, b, 1); |
7810 |
117310 |
125120 |
|
| 701 |
|
| |
| 702 |
117310 |
| na_slots = hamt_node_nslots(a); |
| 703 |
117310 |
| nb_slots = hamt_node_nslots(b); |
| 704 |
|
| |
| 705 |
3868235 |
▶ | for (pos = 0; pos < 32 && !c->err; pos++) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (pos = 0; pos < 32 && !c->err; pos++) { |
3751031 |
117204 |
3868235 |
| for (pos = 0; pos < 32 && !c->err; pos++) { |
3750925 |
106 |
3751031 |
|
| 706 |
3750925 |
| uint32_t bit = (uint32_t)1 << pos; |
| 707 |
3750925 |
| hamt_entry *ea = NULL, *eb = NULL; |
| 708 |
3750925 |
| hamt_node *ca = NULL, *cb = NULL; |
| 709 |
|
| |
| 710 |
3750925 |
▶ | if (a->datamap & bit) |
Branch: 1 branch
| Branch | True | False | Total |
| if (a->datamap & bit) |
127185 |
3623740 |
3750925 |
|
| 711 |
127185 |
| ea = (hamt_entry *)a->slot[hamt_popcnt32(a->datamap & (bit - 1))]; |
| 712 |
3623740 |
▶ | else if (a->nodemap & bit) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (a->nodemap & bit) |
148322 |
3475418 |
3623740 |
|
| 713 |
148322 |
| ca = (hamt_node *)a->slot[na_slots - 1 - hamt_popcnt32(a->nodemap & (bit - 1))]; |
| 714 |
3475418 |
| else continue; /* nothing here to keep */ |
| 715 |
|
| |
| 716 |
275507 |
▶ | if (b->datamap & bit) |
Branch: 1 branch
| Branch | True | False | Total |
| if (b->datamap & bit) |
55116 |
220391 |
275507 |
|
| 717 |
55116 |
| eb = (hamt_entry *)b->slot[hamt_popcnt32(b->datamap & (bit - 1))]; |
| 718 |
220391 |
▶ | else if (b->nodemap & bit) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (b->nodemap & bit) |
139049 |
81342 |
220391 |
|
| 719 |
139049 |
| cb = (hamt_node *)b->slot[nb_slots - 1 - hamt_popcnt32(b->nodemap & (bit - 1))]; |
| 720 |
|
| |
| 721 |
275507 |
▶ | if (ea) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ea) { |
127185 |
148322 |
275507 |
|
| 722 |
47340 |
| int has = eb ? hamt_entry_key_eq(eb, ea->key, ea->klen, ea->utf8, ea->hash) |
| 723 |
213793 |
▶ | : cb ? hamt_node_find(cb, depth + 1, ea->key, ea->klen, ea->utf8, ea->hash) != NULL |
Branch: 1 branch
| Branch | True | False | Total |
| : cb ? hamt_node_find(cb, depth + 1, ea->key, ea->klen, ea->utf8, ea->hash) != NULL |
47340 |
79845 |
127185 |
|
| 724 |
86608 |
| : 0; |
| 725 |
127185 |
▶ | if (has) { rd[nd++] = hamt_entry_retain(ea); datamap |= bit; c->kept++; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (has) { rd[nd++] = hamt_entry_retain(ea); datamap |= bit; c->kept++; } |
41961 |
85224 |
127185 |
|
| 726 |
85224 |
| else { same = 0; c->removed++; } |
| 727 |
|
| } |
| 728 |
148322 |
▶ | else if (!eb && !cb) { /* a subtree with no partner */ |
Branch: 2 branches
| Branch | True | False | Total |
| else if (!eb && !cb) { /* a subtree with no partner */ |
140546 |
7776 |
148322 |
| else if (!eb && !cb) { /* a subtree with no partner */ |
8260 |
132286 |
140546 |
|
| 729 |
8260 |
| same = 0; |
| 730 |
8260 |
▶ | if (!c->count_shared) c->removed += hamt_set_walk(c, ca); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!c->count_shared) c->removed += hamt_set_walk(c, ca); |
5209 |
3051 |
8260 |
|
| 731 |
|
| } |
| 732 |
140062 |
▶ | else if (eb) { /* at most one of a subtree */ |
Branch: 1 branch
| Branch | True | False | Total |
| else if (eb) { /* at most one of a subtree */ |
7776 |
132286 |
140062 |
|
| 733 |
7776 |
| hamt_entry *e = hamt_node_find(ca, depth + 1, eb->key, eb->klen, eb->utf8, eb->hash); |
| 734 |
7776 |
| same = 0; |
| 735 |
7776 |
▶ | if (!c->count_shared) c->removed += hamt_set_walk(c, ca) - (e ? 1 : 0); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!c->count_shared) c->removed += hamt_set_walk(c, ca) - (e ? 1 : 0); |
5173 |
2603 |
7776 |
| if (!c->count_shared) c->removed += hamt_set_walk(c, ca) - (e ? 1 : 0); |
3235 |
1938 |
5173 |
|
| 736 |
7776 |
▶ | if (e) { rd[nd++] = hamt_entry_retain(e); datamap |= bit; c->kept++; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (e) { rd[nd++] = hamt_entry_retain(e); datamap |= bit; c->kept++; } |
4444 |
3332 |
7776 |
|
| 737 |
|
| } |
| 738 |
132286 |
▶ | else if (ca == cb) { /* the same subtree: all of it */ |
Branch: 1 branch
| Branch | True | False | Total |
| else if (ca == cb) { /* the same subtree: all of it */ |
8915 |
123371 |
132286 |
|
| 739 |
8915 |
| rc[nc++] = hamt_node_retain(ca); |
| 740 |
8915 |
| nodemap |= bit; |
| 741 |
8915 |
▶ | if (c->count_shared) c->kept += hamt_set_walk(c, ca); |
Branch: 1 branch
| Branch | True | False | Total |
| if (c->count_shared) c->kept += hamt_set_walk(c, ca); |
62 |
8853 |
8915 |
|
| 742 |
|
| } |
| 743 |
|
| else { |
| 744 |
123371 |
| hamt_part part = hamt_node_intersect(c, ca, cb, depth + 1); |
| 745 |
123371 |
▶ | HAMT_PART_PLACE(part, ca, bit); |
Branch: 3 branches
| Branch | True | False | Total |
| HAMT_PART_PLACE(part, ca, bit); |
48795 |
74576 |
123371 |
| HAMT_PART_PLACE(part, ca, bit); |
39181 |
9614 |
48795 |
| HAMT_PART_PLACE(part, ca, bit); |
17442 |
57134 |
74576 |
|
| 746 |
|
| } |
| 747 |
|
| } |
| 748 |
117310 |
| return hamt_part_build(c, a, depth, same, datamap, nodemap, rd, nd, rc, nc); |
| 749 |
|
| } |
| 750 |
|
| |
| 751 |
120164 |
| static hamt_part hamt_node_subtract(hamt_set_ctx *c, hamt_node *a, hamt_node *b, |
| 752 |
|
| unsigned depth) { |
| 753 |
|
| void *rd[32], *rc[32]; |
| 754 |
120164 |
| uint32_t datamap = 0, nodemap = 0, nd = 0, nc = 0, pos, na_slots, nb_slots; |
| 755 |
120164 |
| int same = 1; |
| 756 |
|
| |
| 757 |
120164 |
| c->visits++; |
| 758 |
120164 |
▶ | if (a->ncoll) return hamt_coll_filter(c, a, b, 0); |
Branch: 1 branch
| Branch | True | False | Total |
| if (a->ncoll) return hamt_coll_filter(c, a, b, 0); |
7810 |
112354 |
120164 |
|
| 759 |
|
| |
| 760 |
112354 |
| na_slots = hamt_node_nslots(a); |
| 761 |
112354 |
| nb_slots = hamt_node_nslots(b); |
| 762 |
|
| |
| 763 |
3704261 |
▶ | for (pos = 0; pos < 32 && !c->err; pos++) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (pos = 0; pos < 32 && !c->err; pos++) { |
3592037 |
112224 |
3704261 |
| for (pos = 0; pos < 32 && !c->err; pos++) { |
3591927 |
110 |
3592037 |
|
| 764 |
3591927 |
| uint32_t bit = (uint32_t)1 << pos; |
| 765 |
3591927 |
| hamt_entry *ea = NULL, *eb = NULL; |
| 766 |
3591927 |
| hamt_node *ca = NULL, *cb = NULL; |
| 767 |
|
| |
| 768 |
3591927 |
▶ | if (a->datamap & bit) |
Branch: 1 branch
| Branch | True | False | Total |
| if (a->datamap & bit) |
111208 |
3480719 |
3591927 |
|
| 769 |
111208 |
| ea = (hamt_entry *)a->slot[hamt_popcnt32(a->datamap & (bit - 1))]; |
| 770 |
3480719 |
▶ | else if (a->nodemap & bit) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (a->nodemap & bit) |
142520 |
3338199 |
3480719 |
|
| 771 |
142520 |
| ca = (hamt_node *)a->slot[na_slots - 1 - hamt_popcnt32(a->nodemap & (bit - 1))]; |
| 772 |
3338199 |
| else continue; |
| 773 |
|
| |
| 774 |
253728 |
▶ | if (b->datamap & bit) |
Branch: 1 branch
| Branch | True | False | Total |
| if (b->datamap & bit) |
48173 |
205555 |
253728 |
|
| 775 |
48173 |
| eb = (hamt_entry *)b->slot[hamt_popcnt32(b->datamap & (bit - 1))]; |
| 776 |
205555 |
▶ | else if (b->nodemap & bit) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (b->nodemap & bit) |
133102 |
72453 |
205555 |
|
| 777 |
133102 |
| cb = (hamt_node *)b->slot[nb_slots - 1 - hamt_popcnt32(b->nodemap & (bit - 1))]; |
| 778 |
|
| |
| 779 |
253728 |
▶ | if (ea) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ea) { |
111208 |
142520 |
253728 |
|
| 780 |
41054 |
| int has = eb ? hamt_entry_key_eq(eb, ea->key, ea->klen, ea->utf8, ea->hash) |
| 781 |
187021 |
▶ | : cb ? hamt_node_find(cb, depth + 1, ea->key, ea->klen, ea->utf8, ea->hash) != NULL |
Branch: 1 branch
| Branch | True | False | Total |
| : cb ? hamt_node_find(cb, depth + 1, ea->key, ea->klen, ea->utf8, ea->hash) != NULL |
41054 |
70154 |
111208 |
|
| 782 |
75813 |
| : 0; |
| 783 |
111208 |
▶ | if (has) { same = 0; c->removed++; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (has) { same = 0; c->removed++; } |
35860 |
75348 |
111208 |
|
| 784 |
75348 |
| else { rd[nd++] = hamt_entry_retain(ea); datamap |= bit; c->kept++; } |
| 785 |
|
| } |
| 786 |
142520 |
▶ | else if (!eb && !cb) { /* a subtree with no partner: kept whole */ |
Branch: 2 branches
| Branch | True | False | Total |
| else if (!eb && !cb) { /* a subtree with no partner: kept whole */ |
135401 |
7119 |
142520 |
| else if (!eb && !cb) { /* a subtree with no partner: kept whole */ |
7958 |
127443 |
135401 |
|
| 787 |
7958 |
| rc[nc++] = hamt_node_retain(ca); |
| 788 |
7958 |
| nodemap |= bit; |
| 789 |
7958 |
▶ | if (!c->count_shared) c->kept += hamt_set_walk(c, ca); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!c->count_shared) c->kept += hamt_set_walk(c, ca); |
4656 |
3302 |
7958 |
|
| 790 |
|
| } |
| 791 |
134562 |
▶ | else if (eb) { /* a subtree less at most one key */ |
Branch: 1 branch
| Branch | True | False | Total |
| else if (eb) { /* a subtree less at most one key */ |
7119 |
127443 |
134562 |
|
| 792 |
7119 |
| hamt_entry *e = hamt_node_find(ca, depth + 1, eb->key, eb->klen, eb->utf8, eb->hash); |
| 793 |
7119 |
▶ | if (!c->count_shared) c->kept += hamt_set_walk(c, ca) - (e ? 1 : 0); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!c->count_shared) c->kept += hamt_set_walk(c, ca) - (e ? 1 : 0); |
4266 |
2853 |
7119 |
| if (!c->count_shared) c->kept += hamt_set_walk(c, ca) - (e ? 1 : 0); |
2732 |
1534 |
4266 |
|
| 794 |
7119 |
▶ | if (!e) { rc[nc++] = hamt_node_retain(ca); nodemap |= bit; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!e) { rc[nc++] = hamt_node_retain(ca); nodemap |= bit; } |
2928 |
4191 |
7119 |
|
| 795 |
|
| else { |
| 796 |
|
| int status; |
| 797 |
4191 |
| hamt_node *sub = hamt_node_del(c->cx, ca, depth + 1, eb->key, eb->klen, |
| 798 |
4191 |
| eb->utf8, eb->hash, &status); |
| 799 |
4191 |
| same = 0; |
| 800 |
4191 |
| c->removed++; |
| 801 |
4191 |
▶ | if (status == HAMT_DEL_OOM || !sub) { c->err = HAMT_ERR_OOM; break; } /* # uncoverable condition left count:2 */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (status == HAMT_DEL_OOM || !sub) { c->err = HAMT_ERR_OOM; break; } /* # uncoverable condition left count:2 */ |
4171 |
20 |
4191 |
| if (status == HAMT_DEL_OOM || !sub) { c->err = HAMT_ERR_OOM; break; } /* # uncoverable condition left count:2 */ |
0 |
4171 |
4171 |
|
| 802 |
4171 |
▶ | if (hamt_node_is_single(sub)) { /* down to one: it moves up */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (hamt_node_is_single(sub)) { /* down to one: it moves up */ |
3024 |
1147 |
4171 |
|
| 803 |
3024 |
| rd[nd++] = hamt_entry_retain((hamt_entry *)sub->slot[0]); |
| 804 |
3024 |
| datamap |= bit; |
| 805 |
3024 |
| hamt_node_release(c->cx, sub); |
| 806 |
|
| } |
| 807 |
1147 |
| else { rc[nc++] = sub; nodemap |= bit; } |
| 808 |
|
| } |
| 809 |
|
| } |
| 810 |
127443 |
▶ | else if (ca == cb) { /* the same subtree: none of it */ |
Branch: 1 branch
| Branch | True | False | Total |
| else if (ca == cb) { /* the same subtree: none of it */ |
9026 |
118417 |
127443 |
|
| 811 |
9026 |
| same = 0; |
| 812 |
9026 |
▶ | if (c->count_shared) c->removed += hamt_set_walk(c, ca); |
Branch: 1 branch
| Branch | True | False | Total |
| if (c->count_shared) c->removed += hamt_set_walk(c, ca); |
93 |
8933 |
9026 |
|
| 813 |
|
| } |
| 814 |
|
| else { |
| 815 |
118417 |
| hamt_part part = hamt_node_subtract(c, ca, cb, depth + 1); |
| 816 |
118417 |
▶ | HAMT_PART_PLACE(part, ca, bit); |
Branch: 3 branches
| Branch | True | False | Total |
| HAMT_PART_PLACE(part, ca, bit); |
96791 |
21626 |
118417 |
| HAMT_PART_PLACE(part, ca, bit); |
40778 |
56013 |
96791 |
| HAMT_PART_PLACE(part, ca, bit); |
12228 |
9398 |
21626 |
|
| 817 |
|
| } |
| 818 |
|
| } |
| 819 |
112354 |
| return hamt_part_build(c, a, depth, same, datamap, nodemap, rd, nd, rc, nc); |
| 820 |
|
| } |
| 821 |
|
| |
| 822 |
4051 |
| static void hamt_set_ctx_start(hamt_set_ctx *c, size_t ca, size_t cb) { |
| 823 |
4051 |
| c->count_shared = cb <= ca / 2; |
| 824 |
4051 |
| c->kept = 0; |
| 825 |
4051 |
| c->removed = 0; |
| 826 |
4051 |
| c->visits = 0; |
| 827 |
4051 |
| c->walked = 0; |
| 828 |
4051 |
| c->err = HAMT_OK; |
| 829 |
4051 |
| } |
| 830 |
|
| |
| 831 |
|
| /* The root of a trie holding the first trie's entries whose key the second |
| 832 |
|
| * also has. `ca` and `cb` are the two counts; `*count` is the result's. |
| 833 |
|
| * The result IS `a` when the second has every key of the first. NULL is an |
| 834 |
|
| * empty result, or a failure when c->err is set. */ |
| 835 |
1929 |
| static hamt_node *hamt_set_intersect(hamt_set_ctx *c, hamt_node *a, hamt_node *b, |
| 836 |
|
| size_t ca, size_t cb, size_t *count) { |
| 837 |
|
| hamt_part part; |
| 838 |
|
| |
| 839 |
1929 |
| hamt_set_ctx_start(c, ca, cb); |
| 840 |
1929 |
| *count = 0; |
| 841 |
1929 |
▶ | if (!a || !b) return NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!a || !b) return NULL; |
1926 |
3 |
1929 |
| if (!a || !b) return NULL; |
1 |
1925 |
1926 |
|
| 842 |
1925 |
▶ | if (a == b) { *count = ca; return hamt_node_retain(a); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (a == b) { *count = ca; return hamt_node_retain(a); } |
176 |
1749 |
1925 |
|
| 843 |
|
| |
| 844 |
1749 |
| part = hamt_node_intersect(c, a, b, 0); |
| 845 |
1749 |
▶ | if (c->err || part.kind == HAMT_PART_NONE) return NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (c->err || part.kind == HAMT_PART_NONE) return NULL; |
1715 |
34 |
1749 |
| if (c->err || part.kind == HAMT_PART_NONE) return NULL; |
563 |
1152 |
1715 |
|
| 846 |
1152 |
▶ | *count = c->count_shared ? c->kept : ca - c->removed; |
Branch: 1 branch
| Branch | True | False | Total |
| *count = c->count_shared ? c->kept : ca - c->removed; |
118 |
1034 |
1152 |
|
| 847 |
1152 |
| return (hamt_node *)part.p; |
| 848 |
|
| } |
| 849 |
|
| |
| 850 |
|
| /* The root of a trie holding the first trie's entries whose key the second |
| 851 |
|
| * has NOT. The result IS `a` when the two share no key. */ |
| 852 |
2122 |
| static hamt_node *hamt_set_subtract(hamt_set_ctx *c, hamt_node *a, hamt_node *b, |
| 853 |
|
| size_t ca, size_t cb, size_t *count) { |
| 854 |
|
| hamt_part part; |
| 855 |
|
| |
| 856 |
2122 |
| hamt_set_ctx_start(c, ca, cb); |
| 857 |
2122 |
| *count = 0; |
| 858 |
2122 |
▶ | if (!a || a == b) return NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!a || a == b) return NULL; |
2120 |
2 |
2122 |
| if (!a || a == b) return NULL; |
372 |
1748 |
2120 |
|
| 859 |
1748 |
▶ | if (!b) { *count = ca; return hamt_node_retain(a); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!b) { *count = ca; return hamt_node_retain(a); } |
1 |
1747 |
1748 |
|
| 860 |
|
| |
| 861 |
1747 |
| part = hamt_node_subtract(c, a, b, 0); |
| 862 |
1747 |
▶ | if (c->err || part.kind == HAMT_PART_NONE) return NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (c->err || part.kind == HAMT_PART_NONE) return NULL; |
1695 |
52 |
1747 |
| if (c->err || part.kind == HAMT_PART_NONE) return NULL; |
193 |
1502 |
1695 |
|
| 863 |
1502 |
▶ | *count = c->count_shared ? ca - c->removed : c->kept; |
Branch: 1 branch
| Branch | True | False | Total |
| *count = c->count_shared ? ca - c->removed : c->kept; |
249 |
1253 |
1502 |
|
| 864 |
1502 |
| return (hamt_node *)part.p; |
| 865 |
|
| } |
| 866 |
|
| |
| 867 |
|
| /* ---- two questions that need no result --------------------------------- */ |
| 868 |
|
| |
| 869 |
|
| /* 1 when every key of `a` is a key of `b`. Stops at the first that is not, |
| 870 |
|
| * and allocates nothing. */ |
| 871 |
19005 |
| static int hamt_node_subset(const hamt_node *a, const hamt_node *b, unsigned depth, |
| 872 |
|
| size_t *visits) { |
| 873 |
|
| uint32_t pos, i, j, na_slots, nb_slots; |
| 874 |
|
| |
| 875 |
19005 |
▶ | if (a == b) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (a == b) return 1; |
8865 |
10140 |
19005 |
|
| 876 |
10140 |
| (*visits)++; |
| 877 |
|
| |
| 878 |
10140 |
▶ | if (a->ncoll) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (a->ncoll) { |
269 |
9871 |
10140 |
|
| 879 |
1193 |
▶ | for (i = 0; i < a->ncoll; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < a->ncoll; i++) { |
1086 |
107 |
1193 |
|
| 880 |
1086 |
| const hamt_entry *ea = (const hamt_entry *)a->slot[i]; |
| 881 |
9940 |
▶ | for (j = 0; j < b->ncoll; j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < b->ncoll; j++) |
9778 |
162 |
9940 |
|
| 882 |
9778 |
▶ | if (hamt_entry_key_eq((const hamt_entry *)b->slot[j], |
Branch: 1 branch
| Branch | True | False | Total |
| if (hamt_entry_key_eq((const hamt_entry *)b->slot[j], |
924 |
8854 |
9778 |
|
| 883 |
10702 |
| ea->key, ea->klen, ea->utf8, ea->hash)) break; |
| 884 |
1086 |
▶ | if (j == b->ncoll) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (j == b->ncoll) return 0; |
162 |
924 |
1086 |
|
| 885 |
|
| } |
| 886 |
107 |
| return 1; |
| 887 |
|
| } |
| 888 |
|
| |
| 889 |
|
| /* Anything of `a` at a position where `b` has nothing settles it. */ |
| 890 |
9871 |
▶ | if ((a->datamap | a->nodemap) & ~(b->datamap | b->nodemap)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if ((a->datamap | a->nodemap) & ~(b->datamap | b->nodemap)) return 0; |
271 |
9600 |
9871 |
|
| 891 |
|
| /* A subtree holds two keys at least, and cannot fit in one entry. */ |
| 892 |
9600 |
▶ | if (a->nodemap & b->datamap) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (a->nodemap & b->datamap) return 0; |
117 |
9483 |
9600 |
|
| 893 |
|
| |
| 894 |
9483 |
| na_slots = hamt_node_nslots(a); |
| 895 |
9483 |
| nb_slots = hamt_node_nslots(b); |
| 896 |
242654 |
▶ | for (pos = 0; pos < 32; pos++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (pos = 0; pos < 32; pos++) { |
235415 |
7239 |
242654 |
|
| 897 |
235415 |
| uint32_t bit = (uint32_t)1 << pos; |
| 898 |
235415 |
▶ | if (a->datamap & bit) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (a->datamap & bit) { |
20018 |
215397 |
235415 |
|
| 899 |
20018 |
| const hamt_entry *ea = (const hamt_entry *) |
| 900 |
20018 |
| a->slot[hamt_popcnt32(a->datamap & (bit - 1))]; |
| 901 |
20018 |
▶ | if (b->datamap & bit) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (b->datamap & bit) { |
17736 |
2282 |
20018 |
|
| 902 |
17736 |
| const hamt_entry *eb = (const hamt_entry *) |
| 903 |
17736 |
| b->slot[hamt_popcnt32(b->datamap & (bit - 1))]; |
| 904 |
17736 |
▶ | if (!hamt_entry_key_eq(eb, ea->key, ea->klen, ea->utf8, ea->hash)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hamt_entry_key_eq(eb, ea->key, ea->klen, ea->utf8, ea->hash)) return 0; |
14 |
17722 |
17736 |
|
| 905 |
|
| } |
| 906 |
|
| else { |
| 907 |
2282 |
| const hamt_node *cb = (const hamt_node *) |
| 908 |
2282 |
| b->slot[nb_slots - 1 - hamt_popcnt32(b->nodemap & (bit - 1))]; |
| 909 |
2282 |
▶ | if (!hamt_node_find(cb, depth + 1, ea->key, ea->klen, ea->utf8, ea->hash)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hamt_node_find(cb, depth + 1, ea->key, ea->klen, ea->utf8, ea->hash)) return 0; |
55 |
2227 |
2282 |
|
| 910 |
|
| } |
| 911 |
|
| } |
| 912 |
215397 |
▶ | else if (a->nodemap & bit) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (a->nodemap & bit) { |
17788 |
197609 |
215397 |
|
| 913 |
17788 |
| const hamt_node *ca = (const hamt_node *) |
| 914 |
17788 |
| a->slot[na_slots - 1 - hamt_popcnt32(a->nodemap & (bit - 1))]; |
| 915 |
17788 |
| const hamt_node *cb = (const hamt_node *) |
| 916 |
17788 |
| b->slot[nb_slots - 1 - hamt_popcnt32(b->nodemap & (bit - 1))]; |
| 917 |
17788 |
▶ | if (!hamt_node_subset(ca, cb, depth + 1, visits)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hamt_node_subset(ca, cb, depth + 1, visits)) return 0; |
2175 |
15613 |
17788 |
|
| 918 |
|
| } |
| 919 |
|
| } |
| 920 |
7239 |
| return 1; |
| 921 |
|
| } |
| 922 |
|
| |
| 923 |
2112 |
| static int hamt_set_subset(const hamt_node *a, const hamt_node *b, size_t ca, |
| 924 |
|
| size_t cb, size_t *visits) { |
| 925 |
2112 |
| *visits = 0; |
| 926 |
2112 |
▶ | if (!a) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!a) return 1; |
2 |
2110 |
2112 |
|
| 927 |
2110 |
▶ | if (!b || ca > cb) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!b || ca > cb) return 0; |
2109 |
1 |
2110 |
| if (!b || ca > cb) return 0; |
892 |
1217 |
2109 |
|
| 928 |
1217 |
| return hamt_node_subset(a, b, 0, visits); |
| 929 |
|
| } |
| 930 |
|
| |
| 931 |
|
| /* 1 when the two have no key in common. Stops at the first they share. */ |
| 932 |
58468 |
| static int hamt_node_disjoint(const hamt_node *a, const hamt_node *b, unsigned depth, |
| 933 |
|
| size_t *visits) { |
| 934 |
|
| uint32_t common, pos, i, j, na_slots, nb_slots; |
| 935 |
|
| |
| 936 |
58468 |
▶ | if (a == b) return 0; /* a node is never empty */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (a == b) return 0; /* a node is never empty */ |
462 |
58006 |
58468 |
|
| 937 |
58006 |
| (*visits)++; |
| 938 |
|
| |
| 939 |
58006 |
▶ | if (a->ncoll) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (a->ncoll) { |
3739 |
54267 |
58006 |
|
| 940 |
23787 |
▶ | for (i = 0; i < a->ncoll; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < a->ncoll; i++) { |
20278 |
3509 |
23787 |
|
| 941 |
20278 |
| const hamt_entry *ea = (const hamt_entry *)a->slot[i]; |
| 942 |
144739 |
▶ | for (j = 0; j < b->ncoll; j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < b->ncoll; j++) |
124691 |
20048 |
144739 |
|
| 943 |
124691 |
▶ | if (hamt_entry_key_eq((const hamt_entry *)b->slot[j], |
Branch: 1 branch
| Branch | True | False | Total |
| if (hamt_entry_key_eq((const hamt_entry *)b->slot[j], |
230 |
124461 |
124691 |
|
| 944 |
124921 |
| ea->key, ea->klen, ea->utf8, ea->hash)) return 0; |
| 945 |
|
| } |
| 946 |
3509 |
| return 1; |
| 947 |
|
| } |
| 948 |
|
| |
| 949 |
|
| /* Only positions both use can hold a shared key. */ |
| 950 |
54267 |
| common = (a->datamap | a->nodemap) & (b->datamap | b->nodemap); |
| 951 |
54267 |
| na_slots = hamt_node_nslots(a); |
| 952 |
54267 |
| nb_slots = hamt_node_nslots(b); |
| 953 |
1662402 |
▶ | for (pos = 0; pos < 32; pos++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (pos = 0; pos < 32; pos++) { |
1612264 |
50138 |
1662402 |
|
| 954 |
1612264 |
| uint32_t bit = (uint32_t)1 << pos; |
| 955 |
1612264 |
| const hamt_entry *ea = NULL, *eb = NULL; |
| 956 |
1612264 |
| const hamt_node *ca = NULL, *cb = NULL; |
| 957 |
|
| |
| 958 |
1612264 |
▶ | if (!(common & bit)) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(common & bit)) continue; |
1542713 |
69551 |
1612264 |
|
| 959 |
69551 |
▶ | if (a->datamap & bit) ea = (const hamt_entry *)a->slot[hamt_popcnt32(a->datamap & (bit - 1))]; |
Branch: 1 branch
| Branch | True | False | Total |
| if (a->datamap & bit) ea = (const hamt_entry *)a->slot[hamt_popcnt32(a->datamap & (bit - 1))]; |
10933 |
58618 |
69551 |
|
| 960 |
58618 |
| else ca = (const hamt_node *)a->slot[na_slots - 1 - hamt_popcnt32(a->nodemap & (bit - 1))]; |
| 961 |
69551 |
▶ | if (b->datamap & bit) eb = (const hamt_entry *)b->slot[hamt_popcnt32(b->datamap & (bit - 1))]; |
Branch: 1 branch
| Branch | True | False | Total |
| if (b->datamap & bit) eb = (const hamt_entry *)b->slot[hamt_popcnt32(b->datamap & (bit - 1))]; |
9634 |
59917 |
69551 |
|
| 962 |
59917 |
| else cb = (const hamt_node *)b->slot[nb_slots - 1 - hamt_popcnt32(b->nodemap & (bit - 1))]; |
| 963 |
|
| |
| 964 |
69551 |
▶ | if (ea && eb) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (ea && eb) { |
10933 |
58618 |
69551 |
| if (ea && eb) { |
7435 |
3498 |
10933 |
|
| 965 |
7435 |
▶ | if (hamt_entry_key_eq(eb, ea->key, ea->klen, ea->utf8, ea->hash)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (hamt_entry_key_eq(eb, ea->key, ea->klen, ea->utf8, ea->hash)) return 0; |
489 |
6946 |
7435 |
|
| 966 |
|
| } |
| 967 |
62116 |
▶ | else if (ea) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (ea) { |
3498 |
58618 |
62116 |
|
| 968 |
3498 |
▶ | if (hamt_node_find(cb, depth + 1, ea->key, ea->klen, ea->utf8, ea->hash)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (hamt_node_find(cb, depth + 1, ea->key, ea->klen, ea->utf8, ea->hash)) return 0; |
55 |
3443 |
3498 |
|
| 969 |
|
| } |
| 970 |
58618 |
▶ | else if (eb) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (eb) { |
2199 |
56419 |
58618 |
|
| 971 |
2199 |
▶ | if (hamt_node_find(ca, depth + 1, eb->key, eb->klen, eb->utf8, eb->hash)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (hamt_node_find(ca, depth + 1, eb->key, eb->klen, eb->utf8, eb->hash)) return 0; |
51 |
2148 |
2199 |
|
| 972 |
|
| } |
| 973 |
56419 |
▶ | else if (!hamt_node_disjoint(ca, cb, depth + 1, visits)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (!hamt_node_disjoint(ca, cb, depth + 1, visits)) return 0; |
3534 |
52885 |
56419 |
|
| 974 |
|
| } |
| 975 |
50138 |
| return 1; |
| 976 |
|
| } |
| 977 |
|
| |
| 978 |
2052 |
| static int hamt_set_disjoint(const hamt_node *a, const hamt_node *b, size_t *visits) { |
| 979 |
2052 |
| *visits = 0; |
| 980 |
2052 |
▶ | if (!a || !b) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!a || !b) return 1; |
2050 |
2 |
2052 |
| if (!a || !b) return 1; |
1 |
2049 |
2050 |
|
| 981 |
2049 |
| return hamt_node_disjoint(a, b, 0, visits); |
| 982 |
|
| } |
| 983 |
|
| |
| 984 |
|
| #endif |