include/hamt/hamt_algebra.h

filter: Statementstmt 100% 528 / 528 Branchbran 98.7% 529 / 538 (5 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 99.3% 1057 / 1066 (5 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/hamt/hamt_vec.h ↑ Summary include/hamt/hamt_map.h »
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 213626 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 213626 ▶ if (!n) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return 0; 0 213626 213626
34 213626 nd = hamt_node_ndata(n);
35 213626 N = hamt_node_nslots(n);
36 213626 total = nd;
37 381904 ▶ for (i = nd; i < N; i++) total += hamt_node_count((const hamt_node *)n->slot[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = nd; i < N; i++) total += hamt_node_count((const hamt_node *)n->slot[i]); 168278 213626 381904
38 213626 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 237875 static int hamt_val_same(hamt_eq_ctx *c, void *va, void *vb) {
54 237875 ▶ if (va == vb) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (va == vb) return 1; 7150 230725 237875
55 /*
56 # uncoverable branch true
57 # uncoverable condition left count:1
58 # uncoverable condition left count:2 */
59 230725 ▶ if (!va || !vb) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!va || !vb) return 0; 230725 0 230725
if (!va || !vb) return 0; 0 230725 230725
60 230725 ▶ if (c->eq) return c->eq(c->ud, c->cx, va, vb);
Branch: 1 branch
BranchTrueFalseTotal
if (c->eq) return c->eq(c->ud, c->cx, va, vb); 17084 213641 230725
61 213641 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 195917 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 195917 ▶ if (a == b) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (a == b) return 1; 3547 192370 195917
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 192370 ▶ if (!a || !b) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!a || !b) return 0; 192370 0 192370
if (!a || !b) return 0; 0 192370 192370
76 192370 c->visits++;
77  
78 192370 ▶ if (a->datamap != b->datamap || a->nodemap != b->nodemap || a->ncoll != b->ncoll) /* # uncoverable condition left count:2 */
Branch: 3 branches
BranchTrueFalseTotal
if (a->datamap != b->datamap || a->nodemap != b->nodemap || a->ncoll != b->ncoll) /* # uncoverable condition left count:2 */ 192331 39 192370
if (a->datamap != b->datamap || a->nodemap != b->nodemap || a->ncoll != b->ncoll) /* # uncoverable condition left count:2 */ 192331 0 192331
if (a->datamap != b->datamap || a->nodemap != b->nodemap || a->ncoll != b->ncoll) /* # uncoverable condition left count:2 */ 14 192317 192331
79 53 return 0;
80 192317 nd = hamt_node_ndata(a);
81 192317 N = hamt_node_nslots(a);
82  
83 192317 ▶ if (a->ncoll) {
Branch: 1 branch
BranchTrueFalseTotal
if (a->ncoll) { 14888 177429 192317
84 /* The same keys, in whatever order each node happens to hold them. */
85 99771 ▶ for (i = 0; i < nd; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nd; i++) { 84895 14876 99771
86 84895 const hamt_entry *ea = (const hamt_entry *)a->slot[i];
87 84895 const hamt_entry *eb = NULL;
88 int rc;
89 940106 ▶ for (j = 0; j < nd; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < nd; j++) { 940098 8 940106
90 940098 eb = (const hamt_entry *)b->slot[j];
91 940098 ▶ if (hamt_entry_key_eq(eb, ea->key, ea->klen, ea->utf8, ea->hash)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (hamt_entry_key_eq(eb, ea->key, ea->klen, ea->utf8, ea->hash)) break; 84887 855211 940098
92 }
93 84895 ▶ if (j == nd) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (j == nd) return 0; 8 84887 84895
94 84887 rc = hamt_val_same(c, ea->val, eb->val);
95 84887 ▶ if (rc <= 0) return rc;
Branch: 1 branch
BranchTrueFalseTotal
if (rc <= 0) return rc; 4 84883 84887
96 }
97 14876 return 1;
98 }
99  
100 279798 ▶ for (i = 0; i < nd; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nd; i++) { 103463 176335 279798
101 103463 const hamt_entry *ea = (const hamt_entry *)a->slot[i];
102 103463 const hamt_entry *eb = (const hamt_entry *)b->slot[i];
103 int rc;
104 103463 ▶ if (ea == eb) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (ea == eb) continue; 15406 88057 103463
105 88057 ▶ if (!hamt_entry_key_eq(eb, ea->key, ea->klen, ea->utf8, ea->hash)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!hamt_entry_key_eq(eb, ea->key, ea->klen, ea->utf8, ea->hash)) return 0; 2 88055 88057
106 88055 rc = hamt_val_same(c, ea->val, eb->val);
107 88052 ▶ if (rc <= 0) return rc;
Branch: 1 branch
BranchTrueFalseTotal
if (rc <= 0) return rc; 1089 86963 88052
108 }
109 368497 ▶ for (; i < N; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; i < N; i++) { 192511 175986 368497
110 192511 int rc = hamt_node_equal(c, (const hamt_node *)a->slot[i],
111 192511 (const hamt_node *)b->slot[i]);
112 192511 ▶ if (rc <= 0) return rc;
Branch: 1 branch
BranchTrueFalseTotal
if (rc <= 0) return rc; 349 192162 192511
113 }
114 175986 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 72350 static hamt_entry *hamt_merge_resolve(hamt_merge_ctx *c, hamt_entry *ea,
137 hamt_entry *eb) {
138 72350 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 72350 ▶ if (ea == eb || ea->val == eb->val) return hamt_entry_retain(ea);
Branch: 2 branches
BranchTrueFalseTotal
if (ea == eb || ea->val == eb->val) return hamt_entry_retain(ea); 62256 10094 72350
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
BranchTrueFalseTotal
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
BranchTrueFalseTotal
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 18817 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 18817 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 18817 int added = 0;
161  
162 18817 *found = old != NULL;
163 18817 ▶ if (!old) {
Branch: 1 branch
BranchTrueFalseTotal
if (!old) { 10108 8709 18817
164 10108 out = hamt_node_put(c->cx, n, depth, e, &added);
165 10108 ▶ if (!out) c->err = HAMT_ERR_OOM;
Branch: 1 branch
BranchTrueFalseTotal
if (!out) c->err = HAMT_ERR_OOM; 136 9972 10108
166 10108 return out;
167 }
168  
169 8709 ▶ r = e_is_first ? hamt_merge_resolve(c, e, old) : hamt_merge_resolve(c, old, e);
Branch: 1 branch
BranchTrueFalseTotal
r = e_is_first ? hamt_merge_resolve(c, e, old) : hamt_merge_resolve(c, old, e); 4007 4702 8709
170 8709 ▶ if (!r) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) return NULL; 5 8704 8709
171 /* When the choice is the entry already there, the put gives `n` back. */
172 8704 out = hamt_node_put(c->cx, n, depth, r, &added);
173 8704 hamt_entry_release(c->cx, r);
174 8704 ▶ if (!out) c->err = HAMT_ERR_OOM;
Branch: 1 branch
BranchTrueFalseTotal
if (!out) c->err = HAMT_ERR_OOM; 4 8700 8704
175 8704 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 8124 static hamt_node *hamt_coll_merge(hamt_merge_ctx *c, hamt_node *a, hamt_node *b) {
185 8124 size_t cap = (size_t)a->ncoll + (size_t)b->ncoll;
186 8124 size_t bytes = cap * sizeof(void *);
187 hamt_entry **list;
188 8124 uint32_t n = 0, i, j;
189 8124 int same_a = 1, same_b = 1;
190 8124 hamt_node *r = NULL;
191  
192 /*
193 # uncoverable branch true
194 # uncoverable condition left count:1 */
195 8124 ▶ if (cap > 0xffffffffU) { c->err = HAMT_ERR_OOM; return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (cap > 0xffffffffU) { c->err = HAMT_ERR_OOM; return NULL; } 0 8124 8124
196 8124 list = (hamt_entry **)HAMT_ALLOC(c->cx, bytes);
197 8124 ▶ if (!list) { c->err = HAMT_ERR_OOM; return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!list) { c->err = HAMT_ERR_OOM; return NULL; } 4 8120 8124
198  
199 55708 ▶ for (i = 0; i < a->ncoll; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < a->ncoll; i++) { 47594 8114 55708
200 47594 hamt_entry *ea = (hamt_entry *)a->slot[i], *eb = NULL, *e;
201 358202 ▶ for (j = 0; j < b->ncoll; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < b->ncoll; j++) { 324149 34053 358202
202 324149 eb = (hamt_entry *)b->slot[j];
203 324149 ▶ if (hamt_entry_key_eq(eb, ea->key, ea->klen, ea->utf8, ea->hash)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (hamt_entry_key_eq(eb, ea->key, ea->klen, ea->utf8, ea->hash)) break; 13541 310608 324149
204 }
205 47594 ▶ if (j == b->ncoll) {
Branch: 1 branch
BranchTrueFalseTotal
if (j == b->ncoll) { 34053 13541 47594
206 34053 e = hamt_entry_retain(ea);
207 34053 same_b = 0;
208 }
209 else {
210 13541 e = hamt_merge_resolve(c, ea, eb);
211 13541 ▶ if (!e) goto fail;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) goto fail; 6 13535 13541
212 13535 ▶ if (e != ea) same_a = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (e != ea) same_a = 0; 1521 12014 13535
213 13535 ▶ if (e != eb) same_b = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (e != eb) same_b = 0; 10862 2673 13535
214 }
215 47588 list[n++] = e;
216 }
217 54724 ▶ for (j = 0; j < b->ncoll; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < b->ncoll; j++) { 46610 8114 54724
218 46610 hamt_entry *eb = (hamt_entry *)b->slot[j];
219 356855 ▶ for (i = 0; i < a->ncoll; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < a->ncoll; i++) { 323765 33090 356855
220 323765 hamt_entry *ea = (hamt_entry *)a->slot[i];
221 323765 ▶ if (hamt_entry_key_eq(ea, eb->key, eb->klen, eb->utf8, eb->hash)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (hamt_entry_key_eq(ea, eb->key, eb->klen, eb->utf8, eb->hash)) break; 13520 310245 323765
222 }
223 46610 ▶ if (i < a->ncoll) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (i < a->ncoll) continue; 13520 33090 46610
224 33090 list[n++] = hamt_entry_retain(eb);
225 33090 c->added++;
226 33090 same_a = 0;
227 }
228  
229 8114 ▶ if (same_a) r = hamt_node_retain(a);
Branch: 1 branch
BranchTrueFalseTotal
if (same_a) r = hamt_node_retain(a); 663 7451 8114
230 7451 ▶ else if (same_b) r = hamt_node_retain(b);
Branch: 1 branch
BranchTrueFalseTotal
else if (same_b) r = hamt_node_retain(b); 171 7280 7451
231 else {
232 7280 r = hamt_node_new(c->cx, n);
233 7280 ▶ if (!r) { c->err = HAMT_ERR_OOM; goto fail; }
Branch: 1 branch
BranchTrueFalseTotal
if (!r) { c->err = HAMT_ERR_OOM; goto fail; } 4 7276 7280
234 7276 r->ncoll = n;
235 7276 memcpy(r->slot, list, (size_t)n * sizeof(void *));
236 7276 HAMT_FREE(c->cx, list, bytes);
237 7276 return r; /* the list's references are the node's now */
238 }
239  
240 844 fail:
241 4931 ▶ for (i = 0; i < n; i++) hamt_entry_release(c->cx, list[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) hamt_entry_release(c->cx, list[i]); 4087 844 4931
242 844 HAMT_FREE(c->cx, list, bytes);
243 844 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 161597 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 161597 uint32_t datamap = 0, nodemap = 0, nd = 0, nc = 0, pos, i, N;
254 uint32_t na_slots, nb_slots;
255 161597 int same_a = 1, same_b = 1;
256 hamt_node *r;
257  
258 161597 ▶ if (a == b) return hamt_node_retain(a);
Branch: 1 branch
BranchTrueFalseTotal
if (a == b) return hamt_node_retain(a); 9191 152406 161597
259 152406 c->visits++;
260 152406 ▶ if (a->ncoll) return hamt_coll_merge(c, a, b);
Branch: 1 branch
BranchTrueFalseTotal
if (a->ncoll) return hamt_coll_merge(c, a, b); 8124 144282 152406
261  
262 144282 na_slots = hamt_node_nslots(a);
263 144282 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 4677285 ▶ for (pos = 0; pos < 32; pos++) {
Branch: 1 branch
BranchTrueFalseTotal
for (pos = 0; pos < 32; pos++) { 4537337 139948 4677285
269 4537337 uint32_t bit = (uint32_t)1 << pos;
270 4537337 hamt_entry *ea = NULL, *eb = NULL;
271 4537337 hamt_node *ca = NULL, *cb = NULL;
272  
273 4537337 ▶ if (a->datamap & bit)
Branch: 1 branch
BranchTrueFalseTotal
if (a->datamap & bit) 234174 4303163 4537337
274 234174 ea = (hamt_entry *)a->slot[hamt_popcnt32(a->datamap & (bit - 1))];
275 4303163 ▶ else if (a->nodemap & bit)
Branch: 1 branch
BranchTrueFalseTotal
else if (a->nodemap & bit) 182850 4120313 4303163
276 182850 ca = (hamt_node *)a->slot[na_slots - 1 - hamt_popcnt32(a->nodemap & (bit - 1))];
277 4537337 ▶ if (b->datamap & bit)
Branch: 1 branch
BranchTrueFalseTotal
if (b->datamap & bit) 234435 4302902 4537337
278 234435 eb = (hamt_entry *)b->slot[hamt_popcnt32(b->datamap & (bit - 1))];
279 4302902 ▶ else if (b->nodemap & bit)
Branch: 1 branch
BranchTrueFalseTotal
else if (b->nodemap & bit) 179597 4123305 4302902
280 179597 cb = (hamt_node *)b->slot[nb_slots - 1 - hamt_popcnt32(b->nodemap & (bit - 1))];
281  
282 4537337 ▶ if (!ea && !ca && !eb && !cb) continue;
Branch: 4 branches
BranchTrueFalseTotal
if (!ea && !ca && !eb && !cb) continue; 4303163 234174 4537337
if (!ea && !ca && !eb && !cb) continue; 4120313 182850 4303163
if (!ea && !ca && !eb && !cb) continue; 3971121 149192 4120313
if (!ea && !ca && !eb && !cb) continue; 3956274 14847 3971121
283  
284 581063 ▶ if (!eb && !cb) { /* the first map's alone */
Branch: 2 branches
BranchTrueFalseTotal
if (!eb && !cb) { /* the first map's alone */ 346628 234435 581063
if (!eb && !cb) { /* the first map's alone */ 167031 179597 346628
285 167031 same_b = 0;
286 167031 ▶ if (ea) { rd[nd++] = hamt_entry_retain(ea); datamap |= bit; }
Branch: 1 branch
BranchTrueFalseTotal
if (ea) { rd[nd++] = hamt_entry_retain(ea); datamap |= bit; } 148962 18069 167031
287 18069 else { rc[nc++] = hamt_node_retain(ca); nodemap |= bit; }
288 }
289 414032 ▶ else if (!ea && !ca) { /* the second's alone */
Branch: 2 branches
BranchTrueFalseTotal
else if (!ea && !ca) { /* the second's alone */ 328820 85212 414032
else if (!ea && !ca) { /* the second's alone */ 164039 164781 328820
290 164039 same_a = 0;
291 164039 ▶ if (eb) { rd[nd++] = hamt_entry_retain(eb); datamap |= bit; c->added++; }
Branch: 1 branch
BranchTrueFalseTotal
if (eb) { rd[nd++] = hamt_entry_retain(eb); datamap |= bit; c->added++; } 149192 14847 164039
292 else {
293 14847 rc[nc++] = hamt_node_retain(cb);
294 14847 nodemap |= bit;
295 14847 c->added += hamt_node_count(cb);
296 }
297 }
298 249993 ▶ else if (ea && eb) {
Branch: 2 branches
BranchTrueFalseTotal
else if (ea && eb) { 85212 164781 249993
else if (ea && eb) { 75819 9393 85212
299 149912 ▶ if (hamt_entry_key_eq(eb, ea->key, ea->klen, ea->utf8, ea->hash)) {
Branch: 1 branch
BranchTrueFalseTotal
if (hamt_entry_key_eq(eb, ea->key, ea->klen, ea->utf8, ea->hash)) { 50100 25719 75819
300 50100 hamt_entry *e = hamt_merge_resolve(c, ea, eb);
301 50100 ▶ if (!e) goto fail;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) goto fail; 1089 49011 50100
302 49011 ▶ if (e != ea) same_a = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (e != ea) same_a = 0; 5841 43170 49011
303 49011 ▶ if (e != eb) same_b = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (e != eb) same_b = 0; 37040 11971 49011
304 49011 rd[nd++] = e;
305 49011 datamap |= bit;
306 }
307 else { /* two keys, one position */
308 25719 hamt_node *sub = hamt_node_pair(c->cx, ea, eb, depth + 1);
309 25719 ▶ if (!sub) { c->err = HAMT_ERR_OOM; goto fail; }
Branch: 1 branch
BranchTrueFalseTotal
if (!sub) { c->err = HAMT_ERR_OOM; goto fail; } 637 25082 25719
310 25082 same_a = same_b = 0;
311 25082 c->added++;
312 25082 rc[nc++] = sub;
313 25082 nodemap |= bit;
314 }
315 }
316 174174 ▶ else if (ea) { /* an entry meets a subtree */
Branch: 1 branch
BranchTrueFalseTotal
else if (ea) { /* an entry meets a subtree */ 9393 164781 174174
317 int found;
318 9393 hamt_node *sub = hamt_merge_put(c, cb, depth + 1, ea, 1, &found);
319 9393 ▶ if (!sub) goto fail;
Branch: 1 branch
BranchTrueFalseTotal
if (!sub) goto fail; 72 9321 9393
320 9321 same_a = 0;
321 9321 ▶ if (sub != cb) same_b = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (sub != cb) same_b = 0; 5451 3870 9321
322 9321 ▶ c->added += hamt_node_count(cb) - (found ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
c->added += hamt_node_count(cb) - (found ? 1 : 0); 4003 5318 9321
323 9321 rc[nc++] = sub;
324 9321 nodemap |= bit;
325 }
326 164781 ▶ else if (eb) { /* a subtree meets an entry */
Branch: 1 branch
BranchTrueFalseTotal
else if (eb) { /* a subtree meets an entry */ 9424 155357 164781
327 int found;
328 9424 hamt_node *sub = hamt_merge_put(c, ca, depth + 1, eb, 0, &found);
329 9424 ▶ if (!sub) goto fail;
Branch: 1 branch
BranchTrueFalseTotal
if (!sub) goto fail; 73 9351 9424
330 9351 ▶ if (sub != ca) same_a = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (sub != ca) same_a = 0; 4965 4386 9351
331 9351 same_b = 0;
332 9351 ▶ if (!found) c->added++;
Branch: 1 branch
BranchTrueFalseTotal
if (!found) c->added++; 4654 4697 9351
333 9351 rc[nc++] = sub;
334 9351 nodemap |= bit;
335 }
336 else { /* two subtrees */
337 155357 hamt_node *sub = hamt_node_merge(c, ca, cb, depth + 1);
338 155357 ▶ if (!sub) goto fail;
Branch: 1 branch
BranchTrueFalseTotal
if (!sub) goto fail; 2463 152894 155357
339 152894 ▶ if (sub != ca) same_a = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (sub != ca) same_a = 0; 132421 20473 152894
340 152894 ▶ if (sub != cb) same_b = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (sub != cb) same_b = 0; 140401 12493 152894
341 152894 rc[nc++] = sub;
342 152894 nodemap |= bit;
343 }
344 }
345  
346 139948 ▶ if (same_a || same_b) {
Branch: 2 branches
BranchTrueFalseTotal
if (same_a || same_b) { 129063 10885 139948
if (same_a || same_b) { 3504 125559 129063
347 32140 ▶ for (i = 0; i < nd; i++) hamt_entry_release(c->cx, (hamt_entry *)rd[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nd; i++) hamt_entry_release(c->cx, (hamt_entry *)rd[i]); 17751 14389 32140
348 31369 ▶ for (i = 0; i < nc; i++) hamt_node_release(c->cx, (hamt_node *)rc[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nc; i++) hamt_node_release(c->cx, (hamt_node *)rc[i]); 16980 14389 31369
349 14389 ▶ return hamt_node_retain(same_a ? a : b);
Branch: 1 branch
BranchTrueFalseTotal
return hamt_node_retain(same_a ? a : b); 10885 3504 14389
350 }
351  
352 125559 N = nd + nc;
353 125559 r = hamt_node_new(c->cx, N);
354 125559 ▶ if (!r) { c->err = HAMT_ERR_OOM; goto fail; }
Branch: 1 branch
BranchTrueFalseTotal
if (!r) { c->err = HAMT_ERR_OOM; goto fail; } 241 125318 125559
355 125318 r->datamap = datamap;
356 125318 r->nodemap = nodemap;
357 447285 ▶ for (i = 0; i < nd; i++) r->slot[i] = rd[i];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nd; i++) r->slot[i] = rd[i]; 321967 125318 447285
358 321258 ▶ for (i = 0; i < nc; i++) r->slot[N - 1 - i] = rc[i]; /* lowest position last */
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nc; i++) r->slot[N - 1 - i] = rc[i]; /* lowest position last */ 195940 125318 321258
359 125318 return r;
360  
361 4575 fail:
362 12022 ▶ for (i = 0; i < nd; i++) hamt_entry_release(c->cx, (hamt_entry *)rd[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nd; i++) hamt_entry_release(c->cx, (hamt_entry *)rd[i]); 7447 4575 12022
363 21219 ▶ for (i = 0; i < nc; i++) hamt_node_release(c->cx, (hamt_node *)rc[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nc; i++) hamt_node_release(c->cx, (hamt_node *)rc[i]); 16644 4575 21219
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
BranchTrueFalseTotal
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
BranchTrueFalseTotal
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
BranchTrueFalseTotal
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 22735 static int hamt_diff_same_key(hamt_diff_ctx *c, hamt_entry *ea, hamt_entry *eb) {
413 int rc;
414 22735 ▶ if (ea == eb) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ea == eb) return 0; 5609 17126 22735
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
BranchTrueFalseTotal
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
BranchTrueFalseTotal
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 10432 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 10432 hamt_iter_init(&it, n);
429 186457 ▶ while ((e = hamt_iter_next(&it))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((e = hamt_iter_next(&it))) { 177166 9291 186457
430 88737 rc = kind == HAMT_DIFF_ADD ? hamt_diff_report(c, kind, NULL, e)
431 177166 ▶ : hamt_diff_report(c, kind, e, NULL);
Branch: 1 branch
BranchTrueFalseTotal
: hamt_diff_report(c, kind, e, NULL); 88737 88429 177166
432 177166 ▶ if (rc) return rc;
Branch: 1 branch
BranchTrueFalseTotal
if (rc) return rc; 1141 176025 177166
433 }
434 9291 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 1403 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 1403 int rc, found = 0;
444  
445 1403 hamt_iter_init(&it, n);
446 9136 ▶ while ((x = hamt_iter_next(&it))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((x = hamt_iter_next(&it))) { 7794 1342 9136
447 7794 ▶ if (hamt_entry_key_eq(x, e->key, e->klen, e->utf8, e->hash)) {
Branch: 1 branch
BranchTrueFalseTotal
if (hamt_entry_key_eq(x, e->key, e->klen, e->utf8, e->hash)) { 1308 6486 7794
448 1308 found = 1;
449 1308 rc = kind == HAMT_DIFF_ADD ? hamt_diff_same_key(c, e, x)
450 1308 ▶ : hamt_diff_same_key(c, x, e);
Branch: 1 branch
BranchTrueFalseTotal
: hamt_diff_same_key(c, x, e); 803 505 1308
451 }
452 else {
453 6486 rc = kind == HAMT_DIFF_ADD ? hamt_diff_report(c, kind, NULL, x)
454 6486 ▶ : hamt_diff_report(c, kind, x, NULL);
Branch: 1 branch
BranchTrueFalseTotal
: hamt_diff_report(c, kind, x, NULL); 3531 2955 6486
455 }
456 7794 ▶ if (rc) return rc;
Branch: 1 branch
BranchTrueFalseTotal
if (rc) return rc; 61 7733 7794
457 }
458 1342 ▶ if (found) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (found) return 0; 1292 50 1342
459 26 return kind == HAMT_DIFF_ADD ? hamt_diff_report(c, HAMT_DIFF_REMOVE, e, NULL)
460 76 ▶ : hamt_diff_report(c, HAMT_DIFF_ADD, NULL, e);
Branch: 1 branch
BranchTrueFalseTotal
: hamt_diff_report(c, HAMT_DIFF_ADD, NULL, e); 26 24 50
461 }
462  
463 11581 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 11581 ▶ if (a == b) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (a == b) return 0; 723 10858 11581
468 10858 c->visits++;
469  
470 10858 ▶ if (a->ncoll) {
Branch: 1 branch
BranchTrueFalseTotal
if (a->ncoll) { 319 10539 10858
471 2983 ▶ for (i = 0; i < a->ncoll; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < a->ncoll; i++) { 2668 315 2983
472 2668 hamt_entry *ea = (hamt_entry *)a->slot[i], *eb = NULL;
473 54459 ▶ for (j = 0; j < b->ncoll; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < b->ncoll; j++) { 53671 788 54459
474 53671 eb = (hamt_entry *)b->slot[j];
475 53671 ▶ if (hamt_entry_key_eq(eb, ea->key, ea->klen, ea->utf8, ea->hash)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (hamt_entry_key_eq(eb, ea->key, ea->klen, ea->utf8, ea->hash)) break; 1880 51791 53671
476 }
477 788 rc = j == b->ncoll ? hamt_diff_report(c, HAMT_DIFF_REMOVE, ea, NULL)
478 2668 ▶ : hamt_diff_same_key(c, ea, eb);
Branch: 1 branch
BranchTrueFalseTotal
: hamt_diff_same_key(c, ea, eb); 788 1880 2668
479 2668 ▶ if (rc) return rc;
Branch: 1 branch
BranchTrueFalseTotal
if (rc) return rc; 4 2664 2668
480 }
481 2971 ▶ for (j = 0; j < b->ncoll; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < b->ncoll; j++) { 2658 313 2971
482 2658 hamt_entry *eb = (hamt_entry *)b->slot[j];
483 54439 ▶ for (i = 0; i < a->ncoll; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < a->ncoll; i++) { 53653 786 54439
484 53653 hamt_entry *ea = (hamt_entry *)a->slot[i];
485 53653 ▶ if (hamt_entry_key_eq(ea, eb->key, eb->klen, eb->utf8, eb->hash)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (hamt_entry_key_eq(ea, eb->key, eb->klen, eb->utf8, eb->hash)) break; 1872 51781 53653
486 }
487 2658 ▶ if (i < a->ncoll) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (i < a->ncoll) continue; 1872 786 2658
488 786 rc = hamt_diff_report(c, HAMT_DIFF_ADD, NULL, eb);
489 786 ▶ if (rc) return rc;
Branch: 1 branch
BranchTrueFalseTotal
if (rc) return rc; 2 784 786
490 }
491 313 return 0;
492 }
493  
494 10539 na_slots = hamt_node_nslots(a);
495 10539 nb_slots = hamt_node_nslots(b);
496  
497 335545 ▶ for (pos = 0; pos < 32; pos++) {
Branch: 1 branch
BranchTrueFalseTotal
for (pos = 0; pos < 32; pos++) { 325690 9855 335545
498 325690 uint32_t bit = (uint32_t)1 << pos;
499 325690 hamt_entry *ea = NULL, *eb = NULL;
500 325690 const hamt_node *ca = NULL, *cb = NULL;
501  
502 325690 ▶ if (a->datamap & bit)
Branch: 1 branch
BranchTrueFalseTotal
if (a->datamap & bit) 22930 302760 325690
503 22930 ea = (hamt_entry *)a->slot[hamt_popcnt32(a->datamap & (bit - 1))];
504 302760 ▶ else if (a->nodemap & bit)
Branch: 1 branch
BranchTrueFalseTotal
else if (a->nodemap & bit) 14745 288015 302760
505 14745 ca = (const hamt_node *)a->slot[na_slots - 1 - hamt_popcnt32(a->nodemap & (bit - 1))];
506 325690 ▶ if (b->datamap & bit)
Branch: 1 branch
BranchTrueFalseTotal
if (b->datamap & bit) 25356 300334 325690
507 25356 eb = (hamt_entry *)b->slot[hamt_popcnt32(b->datamap & (bit - 1))];
508 300334 ▶ else if (b->nodemap & bit)
Branch: 1 branch
BranchTrueFalseTotal
else if (b->nodemap & bit) 15165 285169 300334
509 15165 cb = (const hamt_node *)b->slot[nb_slots - 1 - hamt_popcnt32(b->nodemap & (bit - 1))];
510  
511 325690 ▶ if (!ea && !ca && !eb && !cb) continue;
Branch: 4 branches
BranchTrueFalseTotal
if (!ea && !ca && !eb && !cb) continue; 302760 22930 325690
if (!ea && !ca && !eb && !cb) continue; 288015 14745 302760
if (!ea && !ca && !eb && !cb) continue; 282983 5032 288015
if (!ea && !ca && !eb && !cb) continue; 278010 4973 282983
512  
513 47680 ▶ if (!eb && !cb)
Branch: 2 branches
BranchTrueFalseTotal
if (!eb && !cb) 22324 25356 47680
if (!eb && !cb) 7159 15165 22324
514 7159 rc = ea ? hamt_diff_report(c, HAMT_DIFF_REMOVE, ea, NULL)
515 7159 ▶ : hamt_diff_all(c, ca, HAMT_DIFF_REMOVE);
Branch: 1 branch
BranchTrueFalseTotal
: hamt_diff_all(c, ca, HAMT_DIFF_REMOVE); 2305 4854 7159
516 40521 ▶ else if (!ea && !ca)
Branch: 2 branches
BranchTrueFalseTotal
else if (!ea && !ca) 19896 20625 40521
else if (!ea && !ca) 10005 9891 19896
517 10005 rc = eb ? hamt_diff_report(c, HAMT_DIFF_ADD, NULL, eb)
518 10005 ▶ : hamt_diff_all(c, cb, HAMT_DIFF_ADD);
Branch: 1 branch
BranchTrueFalseTotal
: hamt_diff_all(c, cb, HAMT_DIFF_ADD); 5032 4973 10005
519 30516 ▶ else if (ea && eb) {
Branch: 2 branches
BranchTrueFalseTotal
else if (ea && eb) { 20625 9891 30516
else if (ea && eb) { 19773 852 20625
520 39546 ▶ if (hamt_entry_key_eq(eb, ea->key, ea->klen, ea->utf8, ea->hash))
Branch: 1 branch
BranchTrueFalseTotal
if (hamt_entry_key_eq(eb, ea->key, ea->klen, ea->utf8, ea->hash)) 19547 226 19773
521 19547 rc = hamt_diff_same_key(c, ea, eb);
522 else {
523 226 rc = hamt_diff_report(c, HAMT_DIFF_REMOVE, ea, NULL);
524 226 ▶ if (!rc) rc = hamt_diff_report(c, HAMT_DIFF_ADD, NULL, eb);
Branch: 1 branch
BranchTrueFalseTotal
if (!rc) rc = hamt_diff_report(c, HAMT_DIFF_ADD, NULL, eb); 225 1 226
525 }
526 }
527 10743 ▶ else if (ea) rc = hamt_diff_entry_vs_node(c, ea, cb, HAMT_DIFF_ADD);
Branch: 1 branch
BranchTrueFalseTotal
else if (ea) rc = hamt_diff_entry_vs_node(c, ea, cb, HAMT_DIFF_ADD); 852 9891 10743
528 9891 ▶ else if (eb) rc = hamt_diff_entry_vs_node(c, eb, ca, HAMT_DIFF_REMOVE);
Branch: 1 branch
BranchTrueFalseTotal
else if (eb) rc = hamt_diff_entry_vs_node(c, eb, ca, HAMT_DIFF_REMOVE); 551 9340 9891
529 9340 else rc = hamt_node_diff(c, ca, cb);
530  
531 47680 ▶ if (rc) return rc;
Branch: 1 branch
BranchTrueFalseTotal
if (rc) return rc; 684 46996 47680
532 }
533 9855 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
BranchTrueFalseTotal
if (a == b) return 0; 536 2846 3382
544 2846 ▶ if (!a) return hamt_diff_all(c, b, HAMT_DIFF_ADD);
Branch: 1 branch
BranchTrueFalseTotal
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
BranchTrueFalseTotal
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 21175 static size_t hamt_set_walk(hamt_set_ctx *c, const hamt_node *n) {
596 21175 size_t k = hamt_node_count(n);
597 21175 c->walked += k;
598 21175 return k;
599 }
600  
601 256336 static hamt_part hamt_part_make(int kind, void *p) {
602 hamt_part r;
603 256336 r.kind = kind;
604 256336 r.p = p;
605 256336 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 229496 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 229496 uint32_t i, N = nd + nc;
616 hamt_node *r;
617  
618 229496 ▶ if (same || N == 0 || c->err) {
Branch: 3 branches
BranchTrueFalseTotal
if (same || N == 0 || c->err) { 168109 61387 229496
if (same || N == 0 || c->err) { 105818 62291 168109
if (same || N == 0 || c->err) { 53 105765 105818
619 173632 ▶ for (i = 0; i < nd; i++) hamt_entry_release(c->cx, (hamt_entry *)rd[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nd; i++) hamt_entry_release(c->cx, (hamt_entry *)rd[i]); 49901 123731 173632
620 195033 ▶ for (i = 0; i < nc; i++) hamt_node_release(c->cx, (hamt_node *)rc[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nc; i++) hamt_node_release(c->cx, (hamt_node *)rc[i]); 71302 123731 195033
621 123731 ▶ if (c->err || N == 0) return hamt_part_make(HAMT_PART_NONE, NULL);
Branch: 2 branches
BranchTrueFalseTotal
if (c->err || N == 0) return hamt_part_make(HAMT_PART_NONE, NULL); 123493 238 123731
if (c->err || N == 0) return hamt_part_make(HAMT_PART_NONE, NULL); 62106 61387 123493
622 61387 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 105765 ▶ if (nc == 0 && nd == 1 && depth > 0)
Branch: 3 branches
BranchTrueFalseTotal
if (nc == 0 && nd == 1 && depth > 0) 46244 59521 105765
if (nc == 0 && nd == 1 && depth > 0) 29427 16817 46244
if (nc == 0 && nd == 1 && depth > 0) 29274 153 29427
626 29274 return hamt_part_make(HAMT_PART_ENTRY, rd[0]);
627  
628 76491 r = hamt_node_new(c->cx, N);
629 76491 ▶ if (!r) {
Branch: 1 branch
BranchTrueFalseTotal
if (!r) { 62 76429 76491
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
BranchTrueFalseTotal
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
BranchTrueFalseTotal
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 76429 r->datamap = datamap;
636 76429 r->nodemap = nodemap;
637 151351 ▶ for (i = 0; i < nd; i++) r->slot[i] = rd[i];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nd; i++) r->slot[i] = rd[i]; 74922 76429 151351
638 171264 ▶ for (i = 0; i < nc; i++) r->slot[N - 1 - i] = rc[i];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nc; i++) r->slot[N - 1 - i] = rc[i]; 94835 76429 171264
639 76429 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 15642 static hamt_part hamt_coll_filter(hamt_set_ctx *c, hamt_node *a, hamt_node *b,
645 int keep_common) {
646 15642 size_t bytes = (size_t)a->ncoll * sizeof(void *);
647 15642 hamt_entry **list = (hamt_entry **)HAMT_ALLOC(c->cx, bytes);
648 15642 uint32_t n = 0, i, j;
649 15642 hamt_part out = hamt_part_make(HAMT_PART_NONE, NULL);
650  
651 15642 ▶ if (!list) { c->err = HAMT_ERR_OOM; return out; }
Branch: 1 branch
BranchTrueFalseTotal
if (!list) { c->err = HAMT_ERR_OOM; return out; } 2 15640 15642
652 104700 ▶ for (i = 0; i < a->ncoll; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < a->ncoll; i++) { 89060 15640 104700
653 89060 hamt_entry *ea = (hamt_entry *)a->slot[i];
654 583982 ▶ for (j = 0; j < b->ncoll; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < b->ncoll; j++) { 518950 65032 583982
655 518950 hamt_entry *eb = (hamt_entry *)b->slot[j];
656 518950 ▶ if (hamt_entry_key_eq(eb, ea->key, ea->klen, ea->utf8, ea->hash)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (hamt_entry_key_eq(eb, ea->key, ea->klen, ea->utf8, ea->hash)) break; 24028 494922 518950
657 }
658 89060 ▶ if ((j < b->ncoll) == (keep_common != 0)) { list[n++] = ea; c->kept++; }
Branch: 1 branch
BranchTrueFalseTotal
if ((j < b->ncoll) == (keep_common != 0)) { list[n++] = ea; c->kept++; } 44530 44530 89060
659 44530 else c->removed++;
660 }
661  
662 15640 ▶ if (n == a->ncoll) out = hamt_part_make(HAMT_PART_NODE, hamt_node_retain(a));
Branch: 1 branch
BranchTrueFalseTotal
if (n == a->ncoll) out = hamt_part_make(HAMT_PART_NODE, hamt_node_retain(a)); 4440 11200 15640
663 11200 ▶ else if (n == 1) out = hamt_part_make(HAMT_PART_ENTRY, hamt_entry_retain(list[0]));
Branch: 1 branch
BranchTrueFalseTotal
else if (n == 1) out = hamt_part_make(HAMT_PART_ENTRY, hamt_entry_retain(list[0])); 1761 9439 11200
664 9439 ▶ else if (n > 1) {
Branch: 1 branch
BranchTrueFalseTotal
else if (n > 1) { 4999 4440 9439
665 4999 hamt_node *r = hamt_node_new(c->cx, n);
666 4999 ▶ if (!r) c->err = HAMT_ERR_OOM;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) c->err = HAMT_ERR_OOM; 2 4997 4999
667 else {
668 4997 r->ncoll = n;
669 4997 memcpy(r->slot, list, (size_t)n * sizeof(void *));
670 4997 hamt_node_adopt(r, HAMT_NO_SKIP);
671 4997 out = hamt_part_make(HAMT_PART_NODE, r);
672 }
673 }
674 15640 HAMT_FREE(c->cx, list, bytes);
675 15640 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 125070 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 125070 uint32_t datamap = 0, nodemap = 0, nd = 0, nc = 0, pos, na_slots, nb_slots;
697 125070 int same = 1;
698  
699 125070 c->visits++;
700 125070 ▶ if (a->ncoll) return hamt_coll_filter(c, a, b, 1);
Branch: 1 branch
BranchTrueFalseTotal
if (a->ncoll) return hamt_coll_filter(c, a, b, 1); 7821 117249 125070
701  
702 117249 na_slots = hamt_node_nslots(a);
703 117249 nb_slots = hamt_node_nslots(b);
704  
705 3866222 ▶ for (pos = 0; pos < 32 && !c->err; pos++) {
Branch: 2 branches
BranchTrueFalseTotal
for (pos = 0; pos < 32 && !c->err; pos++) { 3749079 117143 3866222
for (pos = 0; pos < 32 && !c->err; pos++) { 3748973 106 3749079
706 3748973 uint32_t bit = (uint32_t)1 << pos;
707 3748973 hamt_entry *ea = NULL, *eb = NULL;
708 3748973 hamt_node *ca = NULL, *cb = NULL;
709  
710 3748973 ▶ if (a->datamap & bit)
Branch: 1 branch
BranchTrueFalseTotal
if (a->datamap & bit) 125439 3623534 3748973
711 125439 ea = (hamt_entry *)a->slot[hamt_popcnt32(a->datamap & (bit - 1))];
712 3623534 ▶ else if (a->nodemap & bit)
Branch: 1 branch
BranchTrueFalseTotal
else if (a->nodemap & bit) 149192 3474342 3623534
713 149192 ca = (hamt_node *)a->slot[na_slots - 1 - hamt_popcnt32(a->nodemap & (bit - 1))];
714 3474342 else continue; /* nothing here to keep */
715  
716 274631 ▶ if (b->datamap & bit)
Branch: 1 branch
BranchTrueFalseTotal
if (b->datamap & bit) 55427 219204 274631
717 55427 eb = (hamt_entry *)b->slot[hamt_popcnt32(b->datamap & (bit - 1))];
718 219204 ▶ else if (b->nodemap & bit)
Branch: 1 branch
BranchTrueFalseTotal
else if (b->nodemap & bit) 139198 80006 219204
719 139198 cb = (hamt_node *)b->slot[nb_slots - 1 - hamt_popcnt32(b->nodemap & (bit - 1))];
720  
721 274631 ▶ if (ea) {
Branch: 1 branch
BranchTrueFalseTotal
if (ea) { 125439 149192 274631
722 47236 int has = eb ? hamt_entry_key_eq(eb, ea->key, ea->klen, ea->utf8, ea->hash)
723 210692 ▶ : cb ? hamt_node_find(cb, depth + 1, ea->key, ea->klen, ea->utf8, ea->hash) != NULL
Branch: 1 branch
BranchTrueFalseTotal
: cb ? hamt_node_find(cb, depth + 1, ea->key, ea->klen, ea->utf8, ea->hash) != NULL 47236 78203 125439
724 85253 : 0;
725 125439 ▶ if (has) { rd[nd++] = hamt_entry_retain(ea); datamap |= bit; c->kept++; }
Branch: 1 branch
BranchTrueFalseTotal
if (has) { rd[nd++] = hamt_entry_retain(ea); datamap |= bit; c->kept++; } 41470 83969 125439
726 83969 else { same = 0; c->removed++; }
727 }
728 149192 ▶ else if (!eb && !cb) { /* a subtree with no partner */
Branch: 2 branches
BranchTrueFalseTotal
else if (!eb && !cb) { /* a subtree with no partner */ 141001 8191 149192
else if (!eb && !cb) { /* a subtree with no partner */ 8853 132148 141001
729 8853 same = 0;
730 8853 ▶ if (!c->count_shared) c->removed += hamt_set_walk(c, ca);
Branch: 1 branch
BranchTrueFalseTotal
if (!c->count_shared) c->removed += hamt_set_walk(c, ca); 5637 3216 8853
731 }
732 140339 ▶ else if (eb) { /* at most one of a subtree */
Branch: 1 branch
BranchTrueFalseTotal
else if (eb) { /* at most one of a subtree */ 8191 132148 140339
733 8191 hamt_entry *e = hamt_node_find(ca, depth + 1, eb->key, eb->klen, eb->utf8, eb->hash);
734 8191 same = 0;
735 8191 ▶ if (!c->count_shared) c->removed += hamt_set_walk(c, ca) - (e ? 1 : 0);
Branch: 2 branches
BranchTrueFalseTotal
if (!c->count_shared) c->removed += hamt_set_walk(c, ca) - (e ? 1 : 0); 5561 2630 8191
if (!c->count_shared) c->removed += hamt_set_walk(c, ca) - (e ? 1 : 0); 3515 2046 5561
736 8191 ▶ if (e) { rd[nd++] = hamt_entry_retain(e); datamap |= bit; c->kept++; }
Branch: 1 branch
BranchTrueFalseTotal
if (e) { rd[nd++] = hamt_entry_retain(e); datamap |= bit; c->kept++; } 4759 3432 8191
737 }
738 132148 ▶ else if (ca == cb) { /* the same subtree: all of it */
Branch: 1 branch
BranchTrueFalseTotal
else if (ca == cb) { /* the same subtree: all of it */ 8827 123321 132148
739 8827 rc[nc++] = hamt_node_retain(ca);
740 8827 nodemap |= bit;
741 8827 ▶ if (c->count_shared) c->kept += hamt_set_walk(c, ca);
Branch: 1 branch
BranchTrueFalseTotal
if (c->count_shared) c->kept += hamt_set_walk(c, ca); 62 8765 8827
742 }
743 else {
744 123321 hamt_part part = hamt_node_intersect(c, ca, cb, depth + 1);
745 123321 ▶ HAMT_PART_PLACE(part, ca, bit);
Branch: 3 branches
BranchTrueFalseTotal
HAMT_PART_PLACE(part, ca, bit); 49433 73888 123321
HAMT_PART_PLACE(part, ca, bit); 38941 10492 49433
HAMT_PART_PLACE(part, ca, bit); 18122 55766 73888
746 }
747 }
748 117249 return hamt_part_build(c, a, depth, same, datamap, nodemap, rd, nd, rc, nc);
749 }
750  
751 120068 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 120068 uint32_t datamap = 0, nodemap = 0, nd = 0, nc = 0, pos, na_slots, nb_slots;
755 120068 int same = 1;
756  
757 120068 c->visits++;
758 120068 ▶ if (a->ncoll) return hamt_coll_filter(c, a, b, 0);
Branch: 1 branch
BranchTrueFalseTotal
if (a->ncoll) return hamt_coll_filter(c, a, b, 0); 7821 112247 120068
759  
760 112247 na_slots = hamt_node_nslots(a);
761 112247 nb_slots = hamt_node_nslots(b);
762  
763 3700730 ▶ for (pos = 0; pos < 32 && !c->err; pos++) {
Branch: 2 branches
BranchTrueFalseTotal
for (pos = 0; pos < 32 && !c->err; pos++) { 3588613 112117 3700730
for (pos = 0; pos < 32 && !c->err; pos++) { 3588503 110 3588613
764 3588503 uint32_t bit = (uint32_t)1 << pos;
765 3588503 hamt_entry *ea = NULL, *eb = NULL;
766 3588503 hamt_node *ca = NULL, *cb = NULL;
767  
768 3588503 ▶ if (a->datamap & bit)
Branch: 1 branch
BranchTrueFalseTotal
if (a->datamap & bit) 109054 3479449 3588503
769 109054 ea = (hamt_entry *)a->slot[hamt_popcnt32(a->datamap & (bit - 1))];
770 3479449 ▶ else if (a->nodemap & bit)
Branch: 1 branch
BranchTrueFalseTotal
else if (a->nodemap & bit) 143434 3336015 3479449
771 143434 ca = (hamt_node *)a->slot[na_slots - 1 - hamt_popcnt32(a->nodemap & (bit - 1))];
772 3336015 else continue;
773  
774 252488 ▶ if (b->datamap & bit)
Branch: 1 branch
BranchTrueFalseTotal
if (b->datamap & bit) 48469 204019 252488
775 48469 eb = (hamt_entry *)b->slot[hamt_popcnt32(b->datamap & (bit - 1))];
776 204019 ▶ else if (b->nodemap & bit)
Branch: 1 branch
BranchTrueFalseTotal
else if (b->nodemap & bit) 133201 70818 204019
777 133201 cb = (hamt_node *)b->slot[nb_slots - 1 - hamt_popcnt32(b->nodemap & (bit - 1))];
778  
779 252488 ▶ if (ea) {
Branch: 1 branch
BranchTrueFalseTotal
if (ea) { 109054 143434 252488
780 40908 int has = eb ? hamt_entry_key_eq(eb, ea->key, ea->klen, ea->utf8, ea->hash)
781 183136 ▶ : cb ? hamt_node_find(cb, depth + 1, ea->key, ea->klen, ea->utf8, ea->hash) != NULL
Branch: 1 branch
BranchTrueFalseTotal
: cb ? hamt_node_find(cb, depth + 1, ea->key, ea->klen, ea->utf8, ea->hash) != NULL 40908 68146 109054
782 74082 : 0;
783 109054 ▶ if (has) { same = 0; c->removed++; }
Branch: 1 branch
BranchTrueFalseTotal
if (has) { same = 0; c->removed++; } 35404 73650 109054
784 73650 else { rd[nd++] = hamt_entry_retain(ea); datamap |= bit; c->kept++; }
785 }
786 143434 ▶ else if (!eb && !cb) { /* a subtree with no partner: kept whole */
Branch: 2 branches
BranchTrueFalseTotal
else if (!eb && !cb) { /* a subtree with no partner: kept whole */ 135873 7561 143434
else if (!eb && !cb) { /* a subtree with no partner: kept whole */ 8608 127265 135873
787 8608 rc[nc++] = hamt_node_retain(ca);
788 8608 nodemap |= bit;
789 8608 ▶ if (!c->count_shared) c->kept += hamt_set_walk(c, ca);
Branch: 1 branch
BranchTrueFalseTotal
if (!c->count_shared) c->kept += hamt_set_walk(c, ca); 5141 3467 8608
790 }
791 134826 ▶ else if (eb) { /* a subtree less at most one key */
Branch: 1 branch
BranchTrueFalseTotal
else if (eb) { /* a subtree less at most one key */ 7561 127265 134826
792 7561 hamt_entry *e = hamt_node_find(ca, depth + 1, eb->key, eb->klen, eb->utf8, eb->hash);
793 7561 ▶ if (!c->count_shared) c->kept += hamt_set_walk(c, ca) - (e ? 1 : 0);
Branch: 2 branches
BranchTrueFalseTotal
if (!c->count_shared) c->kept += hamt_set_walk(c, ca) - (e ? 1 : 0); 4681 2880 7561
if (!c->count_shared) c->kept += hamt_set_walk(c, ca) - (e ? 1 : 0); 2973 1708 4681
794 7561 ▶ if (!e) { rc[nc++] = hamt_node_retain(ca); nodemap |= bit; }
Branch: 1 branch
BranchTrueFalseTotal
if (!e) { rc[nc++] = hamt_node_retain(ca); nodemap |= bit; } 3094 4467 7561
795 else {
796 int status;
797 4467 hamt_node *sub = hamt_node_del(c->cx, ca, depth + 1, eb->key, eb->klen,
798 4467 eb->utf8, eb->hash, &status);
799 4467 same = 0;
800 4467 c->removed++;
801 4467 ▶ if (status == HAMT_DEL_OOM || !sub) { c->err = HAMT_ERR_OOM; break; } /* # uncoverable condition left count:2 */
Branch: 2 branches
BranchTrueFalseTotal
if (status == HAMT_DEL_OOM || !sub) { c->err = HAMT_ERR_OOM; break; } /* # uncoverable condition left count:2 */ 4447 20 4467
if (status == HAMT_DEL_OOM || !sub) { c->err = HAMT_ERR_OOM; break; } /* # uncoverable condition left count:2 */ 0 4447 4447
802 4447 ▶ if (hamt_node_is_single(sub)) { /* down to one: it moves up */
Branch: 1 branch
BranchTrueFalseTotal
if (hamt_node_is_single(sub)) { /* down to one: it moves up */ 3304 1143 4447
803 3304 rd[nd++] = hamt_entry_retain((hamt_entry *)sub->slot[0]);
804 3304 datamap |= bit;
805 3304 hamt_node_release(c->cx, sub);
806 }
807 1143 else { rc[nc++] = sub; nodemap |= bit; }
808 }
809 }
810 127265 ▶ else if (ca == cb) { /* the same subtree: none of it */
Branch: 1 branch
BranchTrueFalseTotal
else if (ca == cb) { /* the same subtree: none of it */ 8944 118321 127265
811 8944 same = 0;
812 8944 ▶ if (c->count_shared) c->removed += hamt_set_walk(c, ca);
Branch: 1 branch
BranchTrueFalseTotal
if (c->count_shared) c->removed += hamt_set_walk(c, ca); 93 8851 8944
813 }
814 else {
815 118321 hamt_part part = hamt_node_subtract(c, ca, cb, depth + 1);
816 118321 ▶ HAMT_PART_PLACE(part, ca, bit);
Branch: 3 branches
BranchTrueFalseTotal
HAMT_PART_PLACE(part, ca, bit); 95166 23155 118321
HAMT_PART_PLACE(part, ca, bit); 40587 54579 95166
HAMT_PART_PLACE(part, ca, bit); 12913 10242 23155
817 }
818 }
819 112247 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
BranchTrueFalseTotal
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
BranchTrueFalseTotal
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
BranchTrueFalseTotal
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
BranchTrueFalseTotal
*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
BranchTrueFalseTotal
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
BranchTrueFalseTotal
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
BranchTrueFalseTotal
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
BranchTrueFalseTotal
*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 18471 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 18471 ▶ if (a == b) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (a == b) return 1; 8679 9792 18471
876 9792 (*visits)++;
877  
878 9792 ▶ if (a->ncoll) {
Branch: 1 branch
BranchTrueFalseTotal
if (a->ncoll) { 244 9548 9792
879 1174 ▶ for (i = 0; i < a->ncoll; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < a->ncoll; i++) { 1073 101 1174
880 1073 const hamt_entry *ea = (const hamt_entry *)a->slot[i];
881 9392 ▶ for (j = 0; j < b->ncoll; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < b->ncoll; j++) 9249 143 9392
882 9249 ▶ if (hamt_entry_key_eq((const hamt_entry *)b->slot[j],
Branch: 1 branch
BranchTrueFalseTotal
if (hamt_entry_key_eq((const hamt_entry *)b->slot[j], 930 8319 9249
883 10179 ea->key, ea->klen, ea->utf8, ea->hash)) break;
884 1073 ▶ if (j == b->ncoll) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (j == b->ncoll) return 0; 143 930 1073
885 }
886 101 return 1;
887 }
888  
889 /* Anything of `a` at a position where `b` has nothing settles it. */
890 9548 ▶ if ((a->datamap | a->nodemap) & ~(b->datamap | b->nodemap)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if ((a->datamap | a->nodemap) & ~(b->datamap | b->nodemap)) return 0; 266 9282 9548
891 /* A subtree holds two keys at least, and cannot fit in one entry. */
892 9282 ▶ if (a->nodemap & b->datamap) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (a->nodemap & b->datamap) return 0; 156 9126 9282
893  
894 9126 na_slots = hamt_node_nslots(a);
895 9126 nb_slots = hamt_node_nslots(b);
896 239180 ▶ for (pos = 0; pos < 32; pos++) {
Branch: 1 branch
BranchTrueFalseTotal
for (pos = 0; pos < 32; pos++) { 232032 7148 239180
897 232032 uint32_t bit = (uint32_t)1 << pos;
898 232032 ▶ if (a->datamap & bit) {
Branch: 1 branch
BranchTrueFalseTotal
if (a->datamap & bit) { 20031 212001 232032
899 20031 const hamt_entry *ea = (const hamt_entry *)
900 20031 a->slot[hamt_popcnt32(a->datamap & (bit - 1))];
901 20031 ▶ if (b->datamap & bit) {
Branch: 1 branch
BranchTrueFalseTotal
if (b->datamap & bit) { 17712 2319 20031
902 17712 const hamt_entry *eb = (const hamt_entry *)
903 17712 b->slot[hamt_popcnt32(b->datamap & (bit - 1))];
904 17712 ▶ if (!hamt_entry_key_eq(eb, ea->key, ea->klen, ea->utf8, ea->hash)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!hamt_entry_key_eq(eb, ea->key, ea->klen, ea->utf8, ea->hash)) return 0; 3 17709 17712
905 }
906 else {
907 2319 const hamt_node *cb = (const hamt_node *)
908 2319 b->slot[nb_slots - 1 - hamt_popcnt32(b->nodemap & (bit - 1))];
909 2319 ▶ if (!hamt_node_find(cb, depth + 1, ea->key, ea->klen, ea->utf8, ea->hash)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!hamt_node_find(cb, depth + 1, ea->key, ea->klen, ea->utf8, ea->hash)) return 0; 51 2268 2319
910 }
911 }
912 212001 ▶ else if (a->nodemap & bit) {
Branch: 1 branch
BranchTrueFalseTotal
else if (a->nodemap & bit) { 17254 194747 212001
913 17254 const hamt_node *ca = (const hamt_node *)
914 17254 a->slot[na_slots - 1 - hamt_popcnt32(a->nodemap & (bit - 1))];
915 17254 const hamt_node *cb = (const hamt_node *)
916 17254 b->slot[nb_slots - 1 - hamt_popcnt32(b->nodemap & (bit - 1))];
917 17254 ▶ if (!hamt_node_subset(ca, cb, depth + 1, visits)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!hamt_node_subset(ca, cb, depth + 1, visits)) return 0; 1924 15330 17254
918 }
919 }
920 7148 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
BranchTrueFalseTotal
if (!a) return 1; 2 2110 2112
927 2110 ▶ if (!b || ca > cb) return 0;
Branch: 2 branches
BranchTrueFalseTotal
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 57457 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 57457 ▶ if (a == b) return 0; /* a node is never empty */
Branch: 1 branch
BranchTrueFalseTotal
if (a == b) return 0; /* a node is never empty */ 466 56991 57457
937 56991 (*visits)++;
938  
939 56991 ▶ if (a->ncoll) {
Branch: 1 branch
BranchTrueFalseTotal
if (a->ncoll) { 3665 53326 56991
940 23314 ▶ for (i = 0; i < a->ncoll; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < a->ncoll; i++) { 19896 3418 23314
941 19896 const hamt_entry *ea = (const hamt_entry *)a->slot[i];
942 146507 ▶ for (j = 0; j < b->ncoll; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < b->ncoll; j++) 126858 19649 146507
943 126858 ▶ if (hamt_entry_key_eq((const hamt_entry *)b->slot[j],
Branch: 1 branch
BranchTrueFalseTotal
if (hamt_entry_key_eq((const hamt_entry *)b->slot[j], 247 126611 126858
944 127105 ea->key, ea->klen, ea->utf8, ea->hash)) return 0;
945 }
946 3418 return 1;
947 }
948  
949 /* Only positions both use can hold a shared key. */
950 53326 common = (a->datamap | a->nodemap) & (b->datamap | b->nodemap);
951 53326 na_slots = hamt_node_nslots(a);
952 53326 nb_slots = hamt_node_nslots(b);
953 1625147 ▶ for (pos = 0; pos < 32; pos++) {
Branch: 1 branch
BranchTrueFalseTotal
for (pos = 0; pos < 32; pos++) { 1576111 49036 1625147
954 1576111 uint32_t bit = (uint32_t)1 << pos;
955 1576111 const hamt_entry *ea = NULL, *eb = NULL;
956 1576111 const hamt_node *ca = NULL, *cb = NULL;
957  
958 1576111 ▶ if (!(common & bit)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!(common & bit)) continue; 1506832 69279 1576111
959 69279 ▶ if (a->datamap & bit) ea = (const hamt_entry *)a->slot[hamt_popcnt32(a->datamap & (bit - 1))];
Branch: 1 branch
BranchTrueFalseTotal
if (a->datamap & bit) ea = (const hamt_entry *)a->slot[hamt_popcnt32(a->datamap & (bit - 1))]; 11419 57860 69279
960 57860 else ca = (const hamt_node *)a->slot[na_slots - 1 - hamt_popcnt32(a->nodemap & (bit - 1))];
961 69279 ▶ if (b->datamap & bit) eb = (const hamt_entry *)b->slot[hamt_popcnt32(b->datamap & (bit - 1))];
Branch: 1 branch
BranchTrueFalseTotal
if (b->datamap & bit) eb = (const hamt_entry *)b->slot[hamt_popcnt32(b->datamap & (bit - 1))]; 10123 59156 69279
962 59156 else cb = (const hamt_node *)b->slot[nb_slots - 1 - hamt_popcnt32(b->nodemap & (bit - 1))];
963  
964 69279 ▶ if (ea && eb) {
Branch: 2 branches
BranchTrueFalseTotal
if (ea && eb) { 11419 57860 69279
if (ea && eb) { 7671 3748 11419
965 7671 ▶ if (hamt_entry_key_eq(eb, ea->key, ea->klen, ea->utf8, ea->hash)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (hamt_entry_key_eq(eb, ea->key, ea->klen, ea->utf8, ea->hash)) return 0; 417 7254 7671
966 }
967 61608 ▶ else if (ea) {
Branch: 1 branch
BranchTrueFalseTotal
else if (ea) { 3748 57860 61608
968 3748 ▶ if (hamt_node_find(cb, depth + 1, ea->key, ea->klen, ea->utf8, ea->hash)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (hamt_node_find(cb, depth + 1, ea->key, ea->klen, ea->utf8, ea->hash)) return 0; 77 3671 3748
969 }
970 57860 ▶ else if (eb) {
Branch: 1 branch
BranchTrueFalseTotal
else if (eb) { 2452 55408 57860
971 2452 ▶ if (hamt_node_find(ca, depth + 1, eb->key, eb->klen, eb->utf8, eb->hash)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (hamt_node_find(ca, depth + 1, eb->key, eb->klen, eb->utf8, eb->hash)) return 0; 80 2372 2452
972 }
973 55408 ▶ else if (!hamt_node_disjoint(ca, cb, depth + 1, visits)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
else if (!hamt_node_disjoint(ca, cb, depth + 1, visits)) return 0; 3716 51692 55408
974 }
975 49036 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
BranchTrueFalseTotal
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
« include/hamt/hamt_vec.h ↑ Summary include/hamt/hamt_map.h »