include/hamt/hamt_algebra.h

filter: Statementstmt 99.2% 524 / 528 Branchbran 92.9% 500 / 538 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 96.0% 1024 / 1066 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/vector_transient.xs ↑ Summary xs/vector.xs »
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 139508 static size_t hamt_node_count(const hamt_node *n) {
28 uint32_t nd, N, i;
29 size_t total;
30 139508 ▶ if (!n) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return 0; 0 139508 139508
31 139508 nd = hamt_node_ndata(n);
32 139508 N = hamt_node_nslots(n);
33 139508 total = nd;
34 250475 ▶ 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]); 110967 139508 250475
35 139508 return total;
36 }
37  
38 /* ---- equality ---------------------------------------------------------- */
39  
40 /* 1 equal, 0 not, negative to stop. */
41 typedef int (*hamt_eq_fn)(void *ud, void *cx, void *va, void *vb);
42  
43 typedef struct hamt_eq_ctx {
44 void *cx;
45 hamt_eq_fn eq; /* NULL: HAMT_VAL_EQ */
46 void *ud;
47 size_t visits; /* node pairs looked at */
48 } hamt_eq_ctx;
49  
50 210651 static int hamt_val_same(hamt_eq_ctx *c, void *va, void *vb) {
51 210651 ▶ if (va == vb) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (va == vb) return 1; 5301 205350 210651
52 205350 ▶ if (!va || !vb) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!va || !vb) return 0; 205350 0 205350
if (!va || !vb) return 0; 0 205350 205350
53 205350 ▶ 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); 8050 197300 205350
54 197300 return HAMT_VAL_EQ(c->cx, va, vb) ? 1 : 0;
55 }
56  
57 /* 1 when the two subtrees hold the same keys with equal values, 0 when they
58 * do not, negative when the comparison callback stopped it. */
59 188204 static int hamt_node_equal(hamt_eq_ctx *c, const hamt_node *a, const hamt_node *b) {
60 uint32_t nd, N, i, j;
61  
62 188204 ▶ if (a == b) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (a == b) return 1; 2440 185764 188204
63 185764 ▶ if (!a || !b) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!a || !b) return 0; 185764 0 185764
if (!a || !b) return 0; 0 185764 185764
64 185764 c->visits++;
65  
66 185764 ▶ if (a->datamap != b->datamap || a->nodemap != b->nodemap || a->ncoll != b->ncoll)
Branch: 3 branches
BranchTrueFalseTotal
if (a->datamap != b->datamap || a->nodemap != b->nodemap || a->ncoll != b->ncoll) 185724 40 185764
if (a->datamap != b->datamap || a->nodemap != b->nodemap || a->ncoll != b->ncoll) 185724 0 185724
if (a->datamap != b->datamap || a->nodemap != b->nodemap || a->ncoll != b->ncoll) 12 185712 185724
67 52 return 0;
68 185712 nd = hamt_node_ndata(a);
69 185712 N = hamt_node_nslots(a);
70  
71 185712 ▶ if (a->ncoll) {
Branch: 1 branch
BranchTrueFalseTotal
if (a->ncoll) { 14593 171119 185712
72 /* The same keys, in whatever order each node happens to hold them. */
73 98913 ▶ for (i = 0; i < nd; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nd; i++) { 84333 14580 98913
74 84333 const hamt_entry *ea = (const hamt_entry *)a->slot[i];
75 84333 const hamt_entry *eb = NULL;
76 int rc;
77 941472 ▶ for (j = 0; j < nd; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < nd; j++) { 941463 9 941472
78 941463 eb = (const hamt_entry *)b->slot[j];
79 941463 ▶ 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; 84324 857139 941463
80 }
81 84333 ▶ if (j == nd) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (j == nd) return 0; 9 84324 84333
82 84324 rc = hamt_val_same(c, ea->val, eb->val);
83 84324 ▶ if (rc <= 0) return rc;
Branch: 1 branch
BranchTrueFalseTotal
if (rc <= 0) return rc; 4 84320 84324
84 }
85 14580 return 1;
86 }
87  
88 269088 ▶ for (i = 0; i < nd; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nd; i++) { 98007 171081 269088
89 98007 const hamt_entry *ea = (const hamt_entry *)a->slot[i];
90 98007 const hamt_entry *eb = (const hamt_entry *)b->slot[i];
91 int rc;
92 98007 ▶ if (ea == eb) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (ea == eb) continue; 13799 84208 98007
93 84208 ▶ 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; 0 84208 84208
94 84208 rc = hamt_val_same(c, ea->val, eb->val);
95 84205 ▶ if (rc <= 0) return rc;
Branch: 1 branch
BranchTrueFalseTotal
if (rc <= 0) return rc; 35 84170 84205
96 }
97 358369 ▶ for (; i < N; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; i < N; i++) { 187623 170746 358369
98 187623 int rc = hamt_node_equal(c, (const hamt_node *)a->slot[i],
99 187623 (const hamt_node *)b->slot[i]);
100 187623 ▶ if (rc <= 0) return rc;
Branch: 1 branch
BranchTrueFalseTotal
if (rc <= 0) return rc; 335 187288 187623
101 }
102 170746 return 1;
103 }
104  
105 /* ---- union ------------------------------------------------------------- */
106  
107 /* Called for a key in both maps whose two values are not the same pointer.
108 * Sets *out to the entry the result should hold, with one reference for the
109 * caller: `ea` or `eb` retained, or a new entry under the same key.
110 * Returns HAMT_OK, HAMT_ERR_ABORT or HAMT_ERR_OOM. */
111 typedef int (*hamt_resolve_fn)(void *ud, void *cx, hamt_entry *ea,
112 hamt_entry *eb, hamt_entry **out);
113  
114 typedef struct hamt_merge_ctx {
115 void *cx;
116 hamt_resolve_fn resolve; /* NULL: the second map's entry wins */
117 void *ud;
118 size_t added; /* keys of the second map that the first lacked */
119 size_t visits; /* node pairs looked at */
120 int err;
121 } hamt_merge_ctx;
122  
123 /* The entry for a key both maps hold. One reference, or NULL with err set. */
124 48910 static hamt_entry *hamt_merge_resolve(hamt_merge_ctx *c, hamt_entry *ea,
125 hamt_entry *eb) {
126 48910 hamt_entry *out = NULL;
127 int rc;
128  
129 /* The first map's entry when there is nothing to choose between, so the
130 * result shares with the first map wherever it can. */
131 48910 ▶ 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); 44333 4577 48910
if (ea == eb || ea->val == eb->val) return hamt_entry_retain(ea); 40353 3980 44333
132 3980 ▶ if (!c->resolve) return hamt_entry_retain(eb);
Branch: 1 branch
BranchTrueFalseTotal
if (!c->resolve) return hamt_entry_retain(eb); 2045 1935 3980
133  
134 1935 rc = c->resolve(c->ud, c->cx, ea, eb, &out);
135 1935 ▶ if (rc != HAMT_OK) { c->err = rc; return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (rc != HAMT_OK) { c->err = rc; return NULL; } 21 1914 1935
136 1914 return out;
137 }
138  
139 /* One entry from one map into a subtree of the other. `e_is_first` says
140 * which map the entry came from, which is the order the resolver is told.
141 * `*found` is whether the subtree already had the key. Returns `n` itself,
142 * retained, when the subtree is unchanged. */
143 13345 static hamt_node *hamt_merge_put(hamt_merge_ctx *c, hamt_node *n, unsigned depth,
144 hamt_entry *e, int e_is_first, int *found) {
145 13345 hamt_entry *old = hamt_node_find(n, depth, e->key, e->klen, e->utf8, e->hash);
146 hamt_entry *r;
147 hamt_node *out;
148 13345 int added = 0;
149  
150 13345 *found = old != NULL;
151 13345 ▶ if (!old) {
Branch: 1 branch
BranchTrueFalseTotal
if (!old) { 6740 6605 13345
152 6740 out = hamt_node_put(c->cx, n, depth, e, &added);
153 6740 ▶ if (!out) c->err = HAMT_ERR_OOM;
Branch: 1 branch
BranchTrueFalseTotal
if (!out) c->err = HAMT_ERR_OOM; 0 6740 6740
154 6740 return out;
155 }
156  
157 6605 ▶ 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); 3204 3401 6605
158 6605 ▶ if (!r) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) return NULL; 3 6602 6605
159 /* When the choice is the entry already there, the put gives `n` back. */
160 6602 out = hamt_node_put(c->cx, n, depth, r, &added);
161 6602 hamt_entry_release(c->cx, r);
162 6602 ▶ if (!out) c->err = HAMT_ERR_OOM;
Branch: 1 branch
BranchTrueFalseTotal
if (!out) c->err = HAMT_ERR_OOM; 0 6602 6602
163 6602 return out;
164 }
165  
166 static hamt_node *hamt_node_merge(hamt_merge_ctx *c, hamt_node *a, hamt_node *b,
167 unsigned depth);
168  
169 /* Two collision nodes: the first's entries, each resolved against the
170 * second's where the key is in both, then the second's that the first has
171 * not got. */
172 8039 static hamt_node *hamt_coll_merge(hamt_merge_ctx *c, hamt_node *a, hamt_node *b) {
173 8039 size_t cap = (size_t)a->ncoll + (size_t)b->ncoll;
174 8039 size_t bytes = cap * sizeof(void *);
175 hamt_entry **list;
176 8039 uint32_t n = 0, i, j;
177 8039 int same_a = 1, same_b = 1;
178 8039 hamt_node *r = NULL;
179  
180 8039 ▶ 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 8039 8039
181 8039 list = (hamt_entry **)HAMT_ALLOC(c->cx, bytes);
182 8039 ▶ if (!list) { c->err = HAMT_ERR_OOM; return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!list) { c->err = HAMT_ERR_OOM; return NULL; } 0 8039 8039
183  
184 54660 ▶ for (i = 0; i < a->ncoll; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < a->ncoll; i++) { 46621 8039 54660
185 46621 hamt_entry *ea = (hamt_entry *)a->slot[i], *eb = NULL, *e;
186 344759 ▶ for (j = 0; j < b->ncoll; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < b->ncoll; j++) { 311129 33630 344759
187 311129 eb = (hamt_entry *)b->slot[j];
188 311129 ▶ 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; 12991 298138 311129
189 }
190 46621 ▶ if (j == b->ncoll) {
Branch: 1 branch
BranchTrueFalseTotal
if (j == b->ncoll) { 33630 12991 46621
191 33630 e = hamt_entry_retain(ea);
192 33630 same_b = 0;
193 }
194 else {
195 12991 e = hamt_merge_resolve(c, ea, eb);
196 12991 ▶ if (!e) goto fail;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) goto fail; 0 12991 12991
197 12991 ▶ if (e != ea) same_a = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (e != ea) same_a = 0; 1347 11644 12991
198 12991 ▶ if (e != eb) same_b = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (e != eb) same_b = 0; 10520 2471 12991
199 }
200 46621 list[n++] = e;
201 }
202 53670 ▶ for (j = 0; j < b->ncoll; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < b->ncoll; j++) { 45631 8039 53670
203 45631 hamt_entry *eb = (hamt_entry *)b->slot[j];
204 343515 ▶ for (i = 0; i < a->ncoll; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < a->ncoll; i++) { 310875 32640 343515
205 310875 hamt_entry *ea = (hamt_entry *)a->slot[i];
206 310875 ▶ 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; 12991 297884 310875
207 }
208 45631 ▶ if (i < a->ncoll) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (i < a->ncoll) continue; 12991 32640 45631
209 32640 list[n++] = hamt_entry_retain(eb);
210 32640 c->added++;
211 32640 same_a = 0;
212 }
213  
214 8039 ▶ if (same_a) r = hamt_node_retain(a);
Branch: 1 branch
BranchTrueFalseTotal
if (same_a) r = hamt_node_retain(a); 606 7433 8039
215 7433 ▶ else if (same_b) r = hamt_node_retain(b);
Branch: 1 branch
BranchTrueFalseTotal
else if (same_b) r = hamt_node_retain(b); 163 7270 7433
216 else {
217 7270 r = hamt_node_new(c->cx, n);
218 7270 ▶ if (!r) { c->err = HAMT_ERR_OOM; goto fail; }
Branch: 1 branch
BranchTrueFalseTotal
if (!r) { c->err = HAMT_ERR_OOM; goto fail; } 0 7270 7270
219 7270 r->ncoll = n;
220 7270 memcpy(r->slot, list, (size_t)n * sizeof(void *));
221 7270 HAMT_FREE(c->cx, list, bytes);
222 7270 return r; /* the list's references are the node's now */
223 }
224  
225 769 fail:
226 4505 ▶ 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]); 3736 769 4505
227 769 HAMT_FREE(c->cx, list, bytes);
228 769 return r;
229 }
230  
231 /* The union of two subtrees that hang at the same place. One reference, or
232 * NULL with err set. Returns `a` itself when the result is exactly `a`, and
233 * `b` itself when it is exactly `b`, so a caller can tell that nothing
234 * changed on that side by comparing pointers. */
235 131580 static hamt_node *hamt_node_merge(hamt_merge_ctx *c, hamt_node *a, hamt_node *b,
236 unsigned depth) {
237 void *rd[32], *rc[32]; /* what goes in the result: entries, children */
238 131580 uint32_t datamap = 0, nodemap = 0, nd = 0, nc = 0, pos, i, N;
239 uint32_t na_slots, nb_slots;
240 131580 int same_a = 1, same_b = 1;
241 hamt_node *r;
242  
243 131580 ▶ if (a == b) return hamt_node_retain(a);
Branch: 1 branch
BranchTrueFalseTotal
if (a == b) return hamt_node_retain(a); 9218 122362 131580
244 122362 c->visits++;
245 122362 ▶ if (a->ncoll) return hamt_coll_merge(c, a, b);
Branch: 1 branch
BranchTrueFalseTotal
if (a->ncoll) return hamt_coll_merge(c, a, b); 8039 114323 122362
246  
247 114323 na_slots = hamt_node_nslots(a);
248 114323 nb_slots = hamt_node_nslots(b);
249  
250 /* Every pointer put in rd[] and rc[] is one this function owns a
251 * reference to, shared or newly made alike, so the two ways out - build
252 * the node, or give everything back - need not tell them apart. */
253 3772124 ▶ for (pos = 0; pos < 32; pos++) {
Branch: 1 branch
BranchTrueFalseTotal
for (pos = 0; pos < 32; pos++) { 3657826 114298 3772124
254 3657826 uint32_t bit = (uint32_t)1 << pos;
255 3657826 hamt_entry *ea = NULL, *eb = NULL;
256 3657826 hamt_node *ca = NULL, *cb = NULL;
257  
258 3657826 ▶ if (a->datamap & bit)
Branch: 1 branch
BranchTrueFalseTotal
if (a->datamap & bit) 112326 3545500 3657826
259 112326 ea = (hamt_entry *)a->slot[hamt_popcnt32(a->datamap & (bit - 1))];
260 3545500 ▶ else if (a->nodemap & bit)
Branch: 1 branch
BranchTrueFalseTotal
else if (a->nodemap & bit) 143982 3401518 3545500
261 143982 ca = (hamt_node *)a->slot[na_slots - 1 - hamt_popcnt32(a->nodemap & (bit - 1))];
262 3657826 ▶ if (b->datamap & bit)
Branch: 1 branch
BranchTrueFalseTotal
if (b->datamap & bit) 110138 3547688 3657826
263 110138 eb = (hamt_entry *)b->slot[hamt_popcnt32(b->datamap & (bit - 1))];
264 3547688 ▶ else if (b->nodemap & bit)
Branch: 1 branch
BranchTrueFalseTotal
else if (b->nodemap & bit) 143456 3404232 3547688
265 143456 cb = (hamt_node *)b->slot[nb_slots - 1 - hamt_popcnt32(b->nodemap & (bit - 1))];
266  
267 3657826 ▶ if (!ea && !ca && !eb && !cb) continue;
Branch: 4 branches
BranchTrueFalseTotal
if (!ea && !ca && !eb && !cb) continue; 3545500 112326 3657826
if (!ea && !ca && !eb && !cb) continue; 3401518 143982 3545500
if (!ea && !ca && !eb && !cb) continue; 3336350 65168 3401518
if (!ea && !ca && !eb && !cb) continue; 3329202 7148 3336350
268  
269 328624 ▶ if (!eb && !cb) { /* the first map's alone */
Branch: 2 branches
BranchTrueFalseTotal
if (!eb && !cb) { /* the first map's alone */ 218486 110138 328624
if (!eb && !cb) { /* the first map's alone */ 75030 143456 218486
270 75030 same_b = 0;
271 75030 ▶ if (ea) { rd[nd++] = hamt_entry_retain(ea); datamap |= bit; }
Branch: 1 branch
BranchTrueFalseTotal
if (ea) { rd[nd++] = hamt_entry_retain(ea); datamap |= bit; } 67759 7271 75030
272 7271 else { rc[nc++] = hamt_node_retain(ca); nodemap |= bit; }
273 }
274 253594 ▶ else if (!ea && !ca) { /* the second's alone */
Branch: 2 branches
BranchTrueFalseTotal
else if (!ea && !ca) { /* the second's alone */ 209027 44567 253594
else if (!ea && !ca) { /* the second's alone */ 72316 136711 209027
275 72316 same_a = 0;
276 72316 ▶ 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++; } 65168 7148 72316
277 else {
278 7148 rc[nc++] = hamt_node_retain(cb);
279 7148 nodemap |= bit;
280 7148 c->added += hamt_node_count(cb);
281 }
282 }
283 181278 ▶ else if (ea && eb) {
Branch: 2 branches
BranchTrueFalseTotal
else if (ea && eb) { 44567 136711 181278
else if (ea && eb) { 38096 6471 44567
284 76174 ▶ 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)) { 29314 8782 38096
285 29314 hamt_entry *e = hamt_merge_resolve(c, ea, eb);
286 29314 ▶ if (!e) goto fail;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) goto fail; 18 29296 29314
287 29296 ▶ if (e != ea) same_a = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (e != ea) same_a = 0; 1931 27365 29296
288 29296 ▶ if (e != eb) same_b = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (e != eb) same_b = 0; 25191 4105 29296
289 29296 rd[nd++] = e;
290 29296 datamap |= bit;
291 }
292 else { /* two keys, one position */
293 8782 hamt_node *sub = hamt_node_pair(c->cx, ea, eb, depth + 1);
294 8782 ▶ if (!sub) { c->err = HAMT_ERR_OOM; goto fail; }
Branch: 1 branch
BranchTrueFalseTotal
if (!sub) { c->err = HAMT_ERR_OOM; goto fail; } 0 8782 8782
295 8782 same_a = same_b = 0;
296 8782 c->added++;
297 8782 rc[nc++] = sub;
298 8782 nodemap |= bit;
299 }
300 }
301 143182 ▶ else if (ea) { /* an entry meets a subtree */
Branch: 1 branch
BranchTrueFalseTotal
else if (ea) { /* an entry meets a subtree */ 6471 136711 143182
302 int found;
303 6471 hamt_node *sub = hamt_merge_put(c, cb, depth + 1, ea, 1, &found);
304 6471 ▶ if (!sub) goto fail;
Branch: 1 branch
BranchTrueFalseTotal
if (!sub) goto fail; 1 6470 6471
305 6470 same_a = 0;
306 6470 ▶ if (sub != cb) same_b = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (sub != cb) same_b = 0; 3388 3082 6470
307 6470 ▶ c->added += hamt_node_count(cb) - (found ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
c->added += hamt_node_count(cb) - (found ? 1 : 0); 3203 3267 6470
308 6470 rc[nc++] = sub;
309 6470 nodemap |= bit;
310 }
311 136711 ▶ else if (eb) { /* a subtree meets an entry */
Branch: 1 branch
BranchTrueFalseTotal
else if (eb) { /* a subtree meets an entry */ 6874 129837 136711
312 int found;
313 6874 hamt_node *sub = hamt_merge_put(c, ca, depth + 1, eb, 0, &found);
314 6874 ▶ if (!sub) goto fail;
Branch: 1 branch
BranchTrueFalseTotal
if (!sub) goto fail; 2 6872 6874
315 6872 ▶ if (sub != ca) same_a = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (sub != ca) same_a = 0; 3761 3111 6872
316 6872 same_b = 0;
317 6872 ▶ if (!found) c->added++;
Branch: 1 branch
BranchTrueFalseTotal
if (!found) c->added++; 3473 3399 6872
318 6872 rc[nc++] = sub;
319 6872 nodemap |= bit;
320 }
321 else { /* two subtrees */
322 129837 hamt_node *sub = hamt_node_merge(c, ca, cb, depth + 1);
323 129837 ▶ if (!sub) goto fail;
Branch: 1 branch
BranchTrueFalseTotal
if (!sub) goto fail; 4 129833 129837
324 129833 ▶ if (sub != ca) same_a = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (sub != ca) same_a = 0; 112193 17640 129833
325 129833 ▶ if (sub != cb) same_b = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (sub != cb) same_b = 0; 118484 11349 129833
326 129833 rc[nc++] = sub;
327 129833 nodemap |= bit;
328 }
329 }
330  
331 114298 ▶ if (same_a || same_b) {
Branch: 2 branches
BranchTrueFalseTotal
if (same_a || same_b) { 106216 8082 114298
if (same_a || same_b) { 2340 103876 106216
332 19681 ▶ 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]); 9259 10422 19681
333 26395 ▶ 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]); 15973 10422 26395
334 10422 ▶ return hamt_node_retain(same_a ? a : b);
Branch: 1 branch
BranchTrueFalseTotal
return hamt_node_retain(same_a ? a : b); 8082 2340 10422
335 }
336  
337 103876 N = nd + nc;
338 103876 r = hamt_node_new(c->cx, N);
339 103876 ▶ if (!r) { c->err = HAMT_ERR_OOM; goto fail; }
Branch: 1 branch
BranchTrueFalseTotal
if (!r) { c->err = HAMT_ERR_OOM; goto fail; } 0 103876 103876
340 103876 r->datamap = datamap;
341 103876 r->nodemap = nodemap;
342 256798 ▶ 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]; 152922 103876 256798
343 254202 ▶ 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 */ 150326 103876 254202
344 103876 return r;
345  
346 25 fail:
347 67 ▶ 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]); 42 25 67
348 102 ▶ 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]); 77 25 102
349 25 return NULL;
350 }
351  
352 /* The root of a trie holding every key of both. Where a key is in both, the
353 * resolver chooses, or the second map's entry wins when there is none.
354 *
355 * One reference, owned by the caller. c->added is how many keys the second
356 * map brought that the first did not have, so the result's count is the
357 * first map's plus that. The result IS `a` when `b` added and changed
358 * nothing, and IS `b` when the result is exactly `b`.
359 *
360 * NULL with c->err set means it stopped: nothing is leaked and neither
361 * input is touched. NULL with c->err clear means both were empty. */
362 1751 static hamt_node *hamt_map_merge(hamt_merge_ctx *c, hamt_node *a, hamt_node *b) {
363 1751 c->added = 0;
364 1751 c->visits = 0;
365 1751 c->err = HAMT_OK;
366 1751 ▶ if (!b) return a ? hamt_node_retain(a) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!b) return a ? hamt_node_retain(a) : NULL; 3 1748 1751
if (!b) return a ? hamt_node_retain(a) : NULL; 2 1 3
367 1748 ▶ 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 1743 1748
368 1743 return hamt_node_merge(c, a, b, 0);
369 }
370  
371 /* ---- difference -------------------------------------------------------- */
372  
373 #define HAMT_DIFF_ADD 1 /* in the second map only: ea is NULL */
374 #define HAMT_DIFF_REMOVE 2 /* in the first map only: eb is NULL */
375 #define HAMT_DIFF_CHANGE 3 /* in both, with values that are not equal */
376  
377 /* 0 to carry on, anything else to stop. */
378 typedef int (*hamt_diff_fn)(void *ud, void *cx, int kind, hamt_entry *ea,
379 hamt_entry *eb);
380  
381 typedef struct hamt_diff_ctx {
382 void *cx;
383 hamt_diff_fn fn;
384 void *ud;
385 hamt_eq_ctx eq; /* how two values under one key are compared */
386 size_t visits; /* node pairs looked at */
387 size_t reported;
388 } hamt_diff_ctx;
389  
390 16630 static int hamt_diff_report(hamt_diff_ctx *c, int kind, hamt_entry *ea,
391 hamt_entry *eb) {
392 16630 c->reported++;
393 16630 ▶ 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; 16595 35 16630
394 }
395  
396 /* The same key on both sides: a change, or nothing. */
397 15711 static int hamt_diff_same_key(hamt_diff_ctx *c, hamt_entry *ea, hamt_entry *eb) {
398 int rc;
399 15711 ▶ if (ea == eb) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ea == eb) return 0; 1775 13936 15711
400 13936 rc = hamt_val_same(&c->eq, ea->val, eb->val);
401 13936 ▶ if (rc < 0) return HAMT_ERR_ABORT;
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) return HAMT_ERR_ABORT; 0 13936 13936
402 13936 ▶ 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); 10734 3202 13936
403 }
404  
405 /* Everything under a subtree is on one side only. */
406 246 static int hamt_diff_all(hamt_diff_ctx *c, const hamt_node *n, int kind) {
407 hamt_iter it;
408 hamt_entry *e;
409 int rc;
410 246 hamt_iter_init(&it, n);
411 826 ▶ while ((e = hamt_iter_next(&it))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((e = hamt_iter_next(&it))) { 581 245 826
412 429 rc = kind == HAMT_DIFF_ADD ? hamt_diff_report(c, kind, NULL, e)
413 581 ▶ : hamt_diff_report(c, kind, e, NULL);
Branch: 1 branch
BranchTrueFalseTotal
: hamt_diff_report(c, kind, e, NULL); 429 152 581
414 581 ▶ if (rc) return rc;
Branch: 1 branch
BranchTrueFalseTotal
if (rc) return rc; 1 580 581
415 }
416 245 return 0;
417 }
418  
419 /* One entry against a subtree of the other map. `kind` is what the
420 * subtree's other entries are: ADD when the subtree is the second map's. */
421 698 static int hamt_diff_entry_vs_node(hamt_diff_ctx *c, hamt_entry *e,
422 const hamt_node *n, int kind) {
423 hamt_iter it;
424 hamt_entry *x;
425 698 int rc, found = 0;
426  
427 698 hamt_iter_init(&it, n);
428 2480 ▶ while ((x = hamt_iter_next(&it))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((x = hamt_iter_next(&it))) { 1782 698 2480
429 1782 ▶ 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)) { 654 1128 1782
430 654 found = 1;
431 654 rc = kind == HAMT_DIFF_ADD ? hamt_diff_same_key(c, e, x)
432 654 ▶ : hamt_diff_same_key(c, x, e);
Branch: 1 branch
BranchTrueFalseTotal
: hamt_diff_same_key(c, x, e); 488 166 654
433 }
434 else {
435 1128 rc = kind == HAMT_DIFF_ADD ? hamt_diff_report(c, kind, NULL, x)
436 1128 ▶ : hamt_diff_report(c, kind, x, NULL);
Branch: 1 branch
BranchTrueFalseTotal
: hamt_diff_report(c, kind, x, NULL); 900 228 1128
437 }
438 1782 ▶ if (rc) return rc;
Branch: 1 branch
BranchTrueFalseTotal
if (rc) return rc; 0 1782 1782
439 }
440 698 ▶ if (found) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (found) return 0; 654 44 698
441 22 return kind == HAMT_DIFF_ADD ? hamt_diff_report(c, HAMT_DIFF_REMOVE, e, NULL)
442 66 ▶ : hamt_diff_report(c, HAMT_DIFF_ADD, NULL, e);
Branch: 1 branch
BranchTrueFalseTotal
: hamt_diff_report(c, HAMT_DIFF_ADD, NULL, e); 22 22 44
443 }
444  
445 7455 static int hamt_node_diff(hamt_diff_ctx *c, const hamt_node *a, const hamt_node *b) {
446 uint32_t pos, i, j, na_slots, nb_slots;
447 int rc;
448  
449 7455 ▶ if (a == b) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (a == b) return 0; 673 6782 7455
450 6782 c->visits++;
451  
452 6782 ▶ if (a->ncoll) {
Branch: 1 branch
BranchTrueFalseTotal
if (a->ncoll) { 300 6482 6782
453 2925 ▶ for (i = 0; i < a->ncoll; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < a->ncoll; i++) { 2625 300 2925
454 2625 hamt_entry *ea = (hamt_entry *)a->slot[i], *eb = NULL;
455 54762 ▶ for (j = 0; j < b->ncoll; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < b->ncoll; j++) { 53983 779 54762
456 53983 eb = (hamt_entry *)b->slot[j];
457 53983 ▶ 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; 1846 52137 53983
458 }
459 779 rc = j == b->ncoll ? hamt_diff_report(c, HAMT_DIFF_REMOVE, ea, NULL)
460 2625 ▶ : hamt_diff_same_key(c, ea, eb);
Branch: 1 branch
BranchTrueFalseTotal
: hamt_diff_same_key(c, ea, eb); 779 1846 2625
461 2625 ▶ if (rc) return rc;
Branch: 1 branch
BranchTrueFalseTotal
if (rc) return rc; 0 2625 2625
462 }
463 2925 ▶ for (j = 0; j < b->ncoll; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < b->ncoll; j++) { 2625 300 2925
464 2625 hamt_entry *eb = (hamt_entry *)b->slot[j];
465 54762 ▶ for (i = 0; i < a->ncoll; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < a->ncoll; i++) { 53983 779 54762
466 53983 hamt_entry *ea = (hamt_entry *)a->slot[i];
467 53983 ▶ 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; 1846 52137 53983
468 }
469 2625 ▶ if (i < a->ncoll) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (i < a->ncoll) continue; 1846 779 2625
470 779 rc = hamt_diff_report(c, HAMT_DIFF_ADD, NULL, eb);
471 779 ▶ if (rc) return rc;
Branch: 1 branch
BranchTrueFalseTotal
if (rc) return rc; 0 779 779
472 }
473 300 return 0;
474 }
475  
476 6482 na_slots = hamt_node_nslots(a);
477 6482 nb_slots = hamt_node_nslots(b);
478  
479 213709 ▶ for (pos = 0; pos < 32; pos++) {
Branch: 1 branch
BranchTrueFalseTotal
for (pos = 0; pos < 32; pos++) { 207235 6474 213709
480 207235 uint32_t bit = (uint32_t)1 << pos;
481 207235 hamt_entry *ea = NULL, *eb = NULL;
482 207235 const hamt_node *ca = NULL, *cb = NULL;
483  
484 207235 ▶ if (a->datamap & bit)
Branch: 1 branch
BranchTrueFalseTotal
if (a->datamap & bit) 14477 192758 207235
485 14477 ea = (hamt_entry *)a->slot[hamt_popcnt32(a->datamap & (bit - 1))];
486 192758 ▶ else if (a->nodemap & bit)
Branch: 1 branch
BranchTrueFalseTotal
else if (a->nodemap & bit) 7671 185087 192758
487 7671 ca = (const hamt_node *)a->slot[na_slots - 1 - hamt_popcnt32(a->nodemap & (bit - 1))];
488 207235 ▶ if (b->datamap & bit)
Branch: 1 branch
BranchTrueFalseTotal
if (b->datamap & bit) 15228 192007 207235
489 15228 eb = (hamt_entry *)b->slot[hamt_popcnt32(b->datamap & (bit - 1))];
490 192007 ▶ else if (b->nodemap & bit)
Branch: 1 branch
BranchTrueFalseTotal
else if (b->nodemap & bit) 8100 183907 192007
491 8100 cb = (const hamt_node *)b->slot[nb_slots - 1 - hamt_popcnt32(b->nodemap & (bit - 1))];
492  
493 207235 ▶ if (!ea && !ca && !eb && !cb) continue;
Branch: 4 branches
BranchTrueFalseTotal
if (!ea && !ca && !eb && !cb) continue; 192758 14477 207235
if (!ea && !ca && !eb && !cb) continue; 185087 7671 192758
if (!ea && !ca && !eb && !cb) continue; 183418 1669 185087
if (!ea && !ca && !eb && !cb) continue; 183243 175 183418
494  
495 23992 ▶ if (!eb && !cb)
Branch: 2 branches
BranchTrueFalseTotal
if (!eb && !cb) 8764 15228 23992
if (!eb && !cb) 664 8100 8764
496 664 rc = ea ? hamt_diff_report(c, HAMT_DIFF_REMOVE, ea, NULL)
497 664 ▶ : hamt_diff_all(c, ca, HAMT_DIFF_REMOVE);
Branch: 1 branch
BranchTrueFalseTotal
: hamt_diff_all(c, ca, HAMT_DIFF_REMOVE); 596 68 664
498 23328 ▶ else if (!ea && !ca)
Branch: 2 branches
BranchTrueFalseTotal
else if (!ea && !ca) 9447 13881 23328
else if (!ea && !ca) 1844 7603 9447
499 1844 rc = eb ? hamt_diff_report(c, HAMT_DIFF_ADD, NULL, eb)
500 1844 ▶ : hamt_diff_all(c, cb, HAMT_DIFF_ADD);
Branch: 1 branch
BranchTrueFalseTotal
: hamt_diff_all(c, cb, HAMT_DIFF_ADD); 1669 175 1844
501 21484 ▶ else if (ea && eb) {
Branch: 2 branches
BranchTrueFalseTotal
else if (ea && eb) { 13881 7603 21484
else if (ea && eb) { 13371 510 13881
502 26742 ▶ 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)) 13211 160 13371
503 13211 rc = hamt_diff_same_key(c, ea, eb);
504 else {
505 160 rc = hamt_diff_report(c, HAMT_DIFF_REMOVE, ea, NULL);
506 160 ▶ 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); 160 0 160
507 }
508 }
509 8113 ▶ 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); 510 7603 8113
510 7603 ▶ 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); 188 7415 7603
511 7415 else rc = hamt_node_diff(c, ca, cb);
512  
513 23992 ▶ if (rc) return rc;
Branch: 1 branch
BranchTrueFalseTotal
if (rc) return rc; 8 23984 23992
514 }
515 6474 return 0;
516 }
517  
518 /* Reports every key that is in one map and not the other, or in both with
519 * values that are not equal, once each and in no promised order. Allocates
520 * nothing. 0, or whatever stopped it. */
521 53 static int hamt_map_diff(hamt_diff_ctx *c, const hamt_node *a, const hamt_node *b) {
522 53 c->visits = 0;
523 53 c->reported = 0;
524 53 c->eq.visits = 0;
525 53 ▶ if (a == b) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (a == b) return 0; 10 43 53
526 43 ▶ 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); 1 42 43
527 42 ▶ 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); 2 40 42
528 40 return hamt_node_diff(c, a, b);
529 }
530  
531 /* ---- intersection and difference --------------------------------------
532 *
533 * Both keep some of the FIRST trie's entries and none of the second's: the
534 * intersection keeps those whose key the second trie has, the difference
535 * those whose key it has not. Values play no part, so these serve a set,
536 * whose entries have none, and would serve a map's keys as well.
537 *
538 * Unlike a union, these REMOVE, so a subtree can come back smaller than a
539 * subtree is allowed to be. The canonical shape is restored on the way up,
540 * exactly as delete does it: what comes back from a position is a part -
541 * nothing, or a lone entry for the parent to hold in place of the child, or
542 * a node.
543 *
544 * COUNTING. The result's size has to be known without walking the result,
545 * or the speed of skipping shared subtrees is spent counting them. Two
546 * tallies are kept - entries kept and entries removed - and either gives
547 * the size, as `kept` or as the first trie's count less `removed`. Each is
548 * exact only if one kind of skipped subtree is walked after all: `kept`
549 * needs the subtrees that are kept whole, `removed` the ones removed whole.
550 * `count_shared` picks: when the second trie is small next to the first,
551 * the subtrees the two have in common are small, and those are the ones
552 * walked; otherwise the ones only the first has. The caller sets it from
553 * the two counts, and reads back whichever tally that makes exact. */
554  
555 #define HAMT_PART_NONE 0
556 #define HAMT_PART_ENTRY 1
557 #define HAMT_PART_NODE 2
558  
559 typedef struct hamt_part {
560 int kind;
561 void *p; /* one reference, the receiver's */
562 } hamt_part;
563  
564 typedef struct hamt_set_ctx {
565 void *cx;
566 int count_shared; /* walk the subtrees common to both, not the first's own */
567 size_t kept;
568 size_t removed;
569 size_t visits; /* node pairs looked at */
570 size_t walked; /* entries counted by walking a subtree */
571 int err;
572 } hamt_set_ctx;
573  
574 /* The size of a subtree that is being kept or removed whole, found by
575 * walking it. The tally of what this costs is what the tests hold to
576 * account: it must stay small whenever one of the two results is. */
577 14918 static size_t hamt_set_walk(hamt_set_ctx *c, const hamt_node *n) {
578 14918 size_t k = hamt_node_count(n);
579 14918 c->walked += k;
580 14918 return k;
581 }
582  
583 245062 static hamt_part hamt_part_make(int kind, void *p) {
584 hamt_part r;
585 245062 r.kind = kind;
586 245062 r.p = p;
587 245062 return r;
588 }
589  
590 /* What a position's gathered entries and children amount to. Every pointer
591 * in rd[] and rc[] is one the caller owns a reference to; all of them are
592 * used or released here. `same` means they are exactly what `a` holds, in
593 * which case `a` itself is the answer. */
594 218362 static hamt_part hamt_part_build(hamt_set_ctx *c, hamt_node *a, unsigned depth,
595 int same, uint32_t datamap, uint32_t nodemap,
596 void **rd, uint32_t nd, void **rc, uint32_t nc) {
597 218362 uint32_t i, N = nd + nc;
598 hamt_node *r;
599  
600 218362 ▶ if (same || N == 0 || c->err) {
Branch: 3 branches
BranchTrueFalseTotal
if (same || N == 0 || c->err) { 157998 60364 218362
if (same || N == 0 || c->err) { 96945 61053 157998
if (same || N == 0 || c->err) { 0 96945 96945
601 168933 ▶ 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]); 47516 121417 168933
602 191484 ▶ 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]); 70067 121417 191484
603 121417 ▶ 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); 121417 0 121417
if (c->err || N == 0) return hamt_part_make(HAMT_PART_NONE, NULL); 61053 60364 121417
604 60364 return hamt_part_make(HAMT_PART_NODE, hamt_node_retain(a));
605 }
606 /* One entry and nothing else, below the root: the parent holds it. */
607 96945 ▶ if (nc == 0 && nd == 1 && depth > 0)
Branch: 3 branches
BranchTrueFalseTotal
if (nc == 0 && nd == 1 && depth > 0) 39143 57802 96945
if (nc == 0 && nd == 1 && depth > 0) 26781 12362 39143
if (nc == 0 && nd == 1 && depth > 0) 26628 153 26781
608 26628 return hamt_part_make(HAMT_PART_ENTRY, rd[0]);
609  
610 70317 r = hamt_node_new(c->cx, N);
611 70317 ▶ if (!r) {
Branch: 1 branch
BranchTrueFalseTotal
if (!r) { 0 70317 70317
612 0 c->err = HAMT_ERR_OOM;
613 0 ▶ 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]); 0 0 0
614 0 ▶ 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]); 0 0 0
615 0 return hamt_part_make(HAMT_PART_NONE, NULL);
616 }
617 70317 r->datamap = datamap;
618 70317 r->nodemap = nodemap;
619 128653 ▶ 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]; 58336 70317 128653
620 156786 ▶ 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]; 86469 70317 156786
621 70317 return hamt_part_make(HAMT_PART_NODE, r);
622 }
623  
624 /* Two collision nodes: the first's entries that the second has (`keep_common`)
625 * or has not. */
626 15580 static hamt_part hamt_coll_filter(hamt_set_ctx *c, hamt_node *a, hamt_node *b,
627 int keep_common) {
628 15580 size_t bytes = (size_t)a->ncoll * sizeof(void *);
629 15580 hamt_entry **list = (hamt_entry **)HAMT_ALLOC(c->cx, bytes);
630 15580 uint32_t n = 0, i, j;
631 15580 hamt_part out = hamt_part_make(HAMT_PART_NONE, NULL);
632  
633 15580 ▶ if (!list) { c->err = HAMT_ERR_OOM; return out; }
Branch: 1 branch
BranchTrueFalseTotal
if (!list) { c->err = HAMT_ERR_OOM; return out; } 0 15580 15580
634 103784 ▶ for (i = 0; i < a->ncoll; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < a->ncoll; i++) { 88204 15580 103784
635 88204 hamt_entry *ea = (hamt_entry *)a->slot[i];
636 568420 ▶ for (j = 0; j < b->ncoll; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < b->ncoll; j++) { 503504 64916 568420
637 503504 hamt_entry *eb = (hamt_entry *)b->slot[j];
638 503504 ▶ 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; 23288 480216 503504
639 }
640 88204 ▶ 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++; } 44102 44102 88204
641 44102 else c->removed++;
642 }
643  
644 15580 ▶ 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)); 4460 11120 15580
645 11120 ▶ 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])); 1750 9370 11120
646 9370 ▶ else if (n > 1) {
Branch: 1 branch
BranchTrueFalseTotal
else if (n > 1) { 4910 4460 9370
647 4910 hamt_node *r = hamt_node_new(c->cx, n);
648 4910 ▶ if (!r) c->err = HAMT_ERR_OOM;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) c->err = HAMT_ERR_OOM; 0 4910 4910
649 else {
650 4910 r->ncoll = n;
651 4910 memcpy(r->slot, list, (size_t)n * sizeof(void *));
652 4910 hamt_node_adopt(r, HAMT_NO_SKIP);
653 4910 out = hamt_part_make(HAMT_PART_NODE, r);
654 }
655 }
656 15580 HAMT_FREE(c->cx, list, bytes);
657 15580 return out;
658 }
659  
660 /* Where the gathering loop puts a part that came back from below. */
661 #define HAMT_PART_PLACE(part, child, bit) \
662 do { \
663 if ((part).kind == HAMT_PART_NODE) { \
664 if ((part).p != (void *)(child)) same = 0; \
665 rc[nc++] = (part).p; nodemap |= (bit); \
666 } \
667 else { \
668 same = 0; \
669 if ((part).kind == HAMT_PART_ENTRY) { \
670 rd[nd++] = (part).p; datamap |= (bit); \
671 } \
672 } \
673 } while (0)
674  
675 119352 static hamt_part hamt_node_intersect(hamt_set_ctx *c, hamt_node *a, hamt_node *b,
676 unsigned depth) {
677 void *rd[32], *rc[32];
678 119352 uint32_t datamap = 0, nodemap = 0, nd = 0, nc = 0, pos, na_slots, nb_slots;
679 119352 int same = 1;
680  
681 119352 c->visits++;
682 119352 ▶ 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); 7790 111562 119352
683  
684 111562 na_slots = hamt_node_nslots(a);
685 111562 nb_slots = hamt_node_nslots(b);
686  
687 3681546 ▶ for (pos = 0; pos < 32 && !c->err; pos++) {
Branch: 2 branches
BranchTrueFalseTotal
for (pos = 0; pos < 32 && !c->err; pos++) { 3569984 111562 3681546
for (pos = 0; pos < 32 && !c->err; pos++) { 3569984 0 3569984
688 3569984 uint32_t bit = (uint32_t)1 << pos;
689 3569984 hamt_entry *ea = NULL, *eb = NULL;
690 3569984 hamt_node *ca = NULL, *cb = NULL;
691  
692 3569984 ▶ if (a->datamap & bit)
Branch: 1 branch
BranchTrueFalseTotal
if (a->datamap & bit) 109038 3460946 3569984
693 109038 ea = (hamt_entry *)a->slot[hamt_popcnt32(a->datamap & (bit - 1))];
694 3460946 ▶ else if (a->nodemap & bit)
Branch: 1 branch
BranchTrueFalseTotal
else if (a->nodemap & bit) 139784 3321162 3460946
695 139784 ca = (hamt_node *)a->slot[na_slots - 1 - hamt_popcnt32(a->nodemap & (bit - 1))];
696 3321162 else continue; /* nothing here to keep */
697  
698 248822 ▶ if (b->datamap & bit)
Branch: 1 branch
BranchTrueFalseTotal
if (b->datamap & bit) 43405 205417 248822
699 43405 eb = (hamt_entry *)b->slot[hamt_popcnt32(b->datamap & (bit - 1))];
700 205417 ▶ else if (b->nodemap & bit)
Branch: 1 branch
BranchTrueFalseTotal
else if (b->nodemap & bit) 132874 72543 205417
701 132874 cb = (hamt_node *)b->slot[nb_slots - 1 - hamt_popcnt32(b->nodemap & (bit - 1))];
702  
703 248822 ▶ if (ea) {
Branch: 1 branch
BranchTrueFalseTotal
if (ea) { 109038 139784 248822
704 36972 int has = eb ? hamt_entry_key_eq(eb, ea->key, ea->klen, ea->utf8, ea->hash)
705 187317 ▶ : 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 36972 72066 109038
706 78279 : 0;
707 109038 ▶ 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++; } 31684 77354 109038
708 77354 else { same = 0; c->removed++; }
709 }
710 139784 ▶ else if (!eb && !cb) { /* a subtree with no partner */
Branch: 2 branches
BranchTrueFalseTotal
else if (!eb && !cb) { /* a subtree with no partner */ 133351 6433 139784
else if (!eb && !cb) { /* a subtree with no partner */ 6690 126661 133351
711 6690 same = 0;
712 6690 ▶ 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); 3903 2787 6690
713 }
714 133094 ▶ else if (eb) { /* at most one of a subtree */
Branch: 1 branch
BranchTrueFalseTotal
else if (eb) { /* at most one of a subtree */ 6433 126661 133094
715 6433 hamt_entry *e = hamt_node_find(ca, depth + 1, eb->key, eb->klen, eb->utf8, eb->hash);
716 6433 same = 0;
717 6433 ▶ 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); 4207 2226 6433
if (!c->count_shared) c->removed += hamt_set_walk(c, ca) - (e ? 1 : 0); 2329 1878 4207
718 6433 ▶ 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++; } 3255 3178 6433
719 }
720 126661 ▶ else if (ca == cb) { /* the same subtree: all of it */
Branch: 1 branch
BranchTrueFalseTotal
else if (ca == cb) { /* the same subtree: all of it */ 8810 117851 126661
721 8810 rc[nc++] = hamt_node_retain(ca);
722 8810 nodemap |= bit;
723 8810 ▶ 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); 0 8810 8810
724 }
725 else {
726 117851 hamt_part part = hamt_node_intersect(c, ca, cb, depth + 1);
727 117851 ▶ HAMT_PART_PLACE(part, ca, bit);
Branch: 3 branches
BranchTrueFalseTotal
HAMT_PART_PLACE(part, ca, bit); 44756 73095 117851
HAMT_PART_PLACE(part, ca, bit); 36147 8609 44756
HAMT_PART_PLACE(part, ca, bit); 16611 56484 73095
728 }
729 }
730 111562 return hamt_part_build(c, a, depth, same, datamap, nodemap, rd, nd, rc, nc);
731 }
732  
733 114590 static hamt_part hamt_node_subtract(hamt_set_ctx *c, hamt_node *a, hamt_node *b,
734 unsigned depth) {
735 void *rd[32], *rc[32];
736 114590 uint32_t datamap = 0, nodemap = 0, nd = 0, nc = 0, pos, na_slots, nb_slots;
737 114590 int same = 1;
738  
739 114590 c->visits++;
740 114590 ▶ 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); 7790 106800 114590
741  
742 106800 na_slots = hamt_node_nslots(a);
743 106800 nb_slots = hamt_node_nslots(b);
744  
745 3524400 ▶ for (pos = 0; pos < 32 && !c->err; pos++) {
Branch: 2 branches
BranchTrueFalseTotal
for (pos = 0; pos < 32 && !c->err; pos++) { 3417600 106800 3524400
for (pos = 0; pos < 32 && !c->err; pos++) { 3417600 0 3417600
746 3417600 uint32_t bit = (uint32_t)1 << pos;
747 3417600 hamt_entry *ea = NULL, *eb = NULL;
748 3417600 hamt_node *ca = NULL, *cb = NULL;
749  
750 3417600 ▶ if (a->datamap & bit)
Branch: 1 branch
BranchTrueFalseTotal
if (a->datamap & bit) 93389 3324211 3417600
751 93389 ea = (hamt_entry *)a->slot[hamt_popcnt32(a->datamap & (bit - 1))];
752 3324211 ▶ else if (a->nodemap & bit)
Branch: 1 branch
BranchTrueFalseTotal
else if (a->nodemap & bit) 133807 3190404 3324211
753 133807 ca = (hamt_node *)a->slot[na_slots - 1 - hamt_popcnt32(a->nodemap & (bit - 1))];
754 3190404 else continue;
755  
756 227196 ▶ if (b->datamap & bit)
Branch: 1 branch
BranchTrueFalseTotal
if (b->datamap & bit) 36686 190510 227196
757 36686 eb = (hamt_entry *)b->slot[hamt_popcnt32(b->datamap & (bit - 1))];
758 190510 ▶ else if (b->nodemap & bit)
Branch: 1 branch
BranchTrueFalseTotal
else if (b->nodemap & bit) 127195 63315 190510
759 127195 cb = (hamt_node *)b->slot[nb_slots - 1 - hamt_popcnt32(b->nodemap & (bit - 1))];
760  
761 227196 ▶ if (ea) {
Branch: 1 branch
BranchTrueFalseTotal
if (ea) { 93389 133807 227196
762 31132 int has = eb ? hamt_entry_key_eq(eb, ea->key, ea->klen, ea->utf8, ea->hash)
763 160855 ▶ : 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 31132 62257 93389
764 67466 : 0;
765 93389 ▶ if (has) { same = 0; c->removed++; }
Branch: 1 branch
BranchTrueFalseTotal
if (has) { same = 0; c->removed++; } 26131 67258 93389
766 67258 else { rd[nd++] = hamt_entry_retain(ea); datamap |= bit; c->kept++; }
767 }
768 133807 ▶ 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 */ 128253 5554 133807
else if (!eb && !cb) { /* a subtree with no partner: kept whole */ 6267 121986 128253
769 6267 rc[nc++] = hamt_node_retain(ca);
770 6267 nodemap |= bit;
771 6267 ▶ 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); 3480 2787 6267
772 }
773 127540 ▶ else if (eb) { /* a subtree less at most one key */
Branch: 1 branch
BranchTrueFalseTotal
else if (eb) { /* a subtree less at most one key */ 5554 121986 127540
774 5554 hamt_entry *e = hamt_node_find(ca, depth + 1, eb->key, eb->klen, eb->utf8, eb->hash);
775 5554 ▶ 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); 3328 2226 5554
if (!c->count_shared) c->kept += hamt_set_walk(c, ca) - (e ? 1 : 0); 1858 1470 3328
776 5554 ▶ if (!e) { rc[nc++] = hamt_node_retain(ca); nodemap |= bit; }
Branch: 1 branch
BranchTrueFalseTotal
if (!e) { rc[nc++] = hamt_node_retain(ca); nodemap |= bit; } 2770 2784 5554
777 else {
778 int status;
779 2784 hamt_node *sub = hamt_node_del(c->cx, ca, depth + 1, eb->key, eb->klen,
780 2784 eb->utf8, eb->hash, &status);
781 2784 same = 0;
782 2784 c->removed++;
783 2784 ▶ if (status == HAMT_DEL_OOM || !sub) { c->err = HAMT_ERR_OOM; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (status == HAMT_DEL_OOM || !sub) { c->err = HAMT_ERR_OOM; break; } 2784 0 2784
if (status == HAMT_DEL_OOM || !sub) { c->err = HAMT_ERR_OOM; break; } 0 2784 2784
784 2784 ▶ 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 */ 1905 879 2784
785 1905 rd[nd++] = hamt_entry_retain((hamt_entry *)sub->slot[0]);
786 1905 datamap |= bit;
787 1905 hamt_node_release(c->cx, sub);
788 }
789 879 else { rc[nc++] = sub; nodemap |= bit; }
790 }
791 }
792 121986 ▶ else if (ca == cb) { /* the same subtree: none of it */
Branch: 1 branch
BranchTrueFalseTotal
else if (ca == cb) { /* the same subtree: none of it */ 8892 113094 121986
793 8892 same = 0;
794 8892 ▶ 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); 0 8892 8892
795 }
796 else {
797 113094 hamt_part part = hamt_node_subtract(c, ca, cb, depth + 1);
798 113094 ▶ HAMT_PART_PLACE(part, ca, bit);
Branch: 3 branches
BranchTrueFalseTotal
HAMT_PART_PLACE(part, ca, bit); 93054 20040 113094
HAMT_PART_PLACE(part, ca, bit); 37595 55459 93054
HAMT_PART_PLACE(part, ca, bit); 11767 8273 20040
799 }
800 }
801 106800 return hamt_part_build(c, a, depth, same, datamap, nodemap, rd, nd, rc, nc);
802 }
803  
804 3359 static void hamt_set_ctx_start(hamt_set_ctx *c, size_t ca, size_t cb) {
805 3359 c->count_shared = cb <= ca / 2;
806 3359 c->kept = 0;
807 3359 c->removed = 0;
808 3359 c->visits = 0;
809 3359 c->walked = 0;
810 3359 c->err = HAMT_OK;
811 3359 }
812  
813 /* The root of a trie holding the first trie's entries whose key the second
814 * also has. `ca` and `cb` are the two counts; `*count` is the result's.
815 * The result IS `a` when the second has every key of the first. NULL is an
816 * empty result, or a failure when c->err is set. */
817 1679 static hamt_node *hamt_set_intersect(hamt_set_ctx *c, hamt_node *a, hamt_node *b,
818 size_t ca, size_t cb, size_t *count) {
819 hamt_part part;
820  
821 1679 hamt_set_ctx_start(c, ca, cb);
822 1679 *count = 0;
823 1679 ▶ if (!a || !b) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!a || !b) return NULL; 1678 1 1679
if (!a || !b) return NULL; 1 1677 1678
824 1677 ▶ 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 1501 1677
825  
826 1501 part = hamt_node_intersect(c, a, b, 0);
827 1501 ▶ if (c->err || part.kind == HAMT_PART_NONE) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (c->err || part.kind == HAMT_PART_NONE) return NULL; 1501 0 1501
if (c->err || part.kind == HAMT_PART_NONE) return NULL; 563 938 1501
828 938 ▶ *count = c->count_shared ? c->kept : ca - c->removed;
Branch: 1 branch
BranchTrueFalseTotal
*count = c->count_shared ? c->kept : ca - c->removed; 114 824 938
829 938 return (hamt_node *)part.p;
830 }
831  
832 /* The root of a trie holding the first trie's entries whose key the second
833 * has NOT. The result IS `a` when the two share no key. */
834 1680 static hamt_node *hamt_set_subtract(hamt_set_ctx *c, hamt_node *a, hamt_node *b,
835 size_t ca, size_t cb, size_t *count) {
836 hamt_part part;
837  
838 1680 hamt_set_ctx_start(c, ca, cb);
839 1680 *count = 0;
840 1680 ▶ if (!a || a == b) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!a || a == b) return NULL; 1680 0 1680
if (!a || a == b) return NULL; 183 1497 1680
841 1497 ▶ if (!b) { *count = ca; return hamt_node_retain(a); }
Branch: 1 branch
BranchTrueFalseTotal
if (!b) { *count = ca; return hamt_node_retain(a); } 1 1496 1497
842  
843 1496 part = hamt_node_subtract(c, a, b, 0);
844 1496 ▶ if (c->err || part.kind == HAMT_PART_NONE) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (c->err || part.kind == HAMT_PART_NONE) return NULL; 1496 0 1496
if (c->err || part.kind == HAMT_PART_NONE) return NULL; 193 1303 1496
845 1303 ▶ *count = c->count_shared ? ca - c->removed : c->kept;
Branch: 1 branch
BranchTrueFalseTotal
*count = c->count_shared ? ca - c->removed : c->kept; 244 1059 1303
846 1303 return (hamt_node *)part.p;
847 }
848  
849 /* ---- two questions that need no result --------------------------------- */
850  
851 /* 1 when every key of `a` is a key of `b`. Stops at the first that is not,
852 * and allocates nothing. */
853 11599 static int hamt_node_subset(const hamt_node *a, const hamt_node *b, unsigned depth,
854 size_t *visits) {
855 uint32_t pos, i, j, na_slots, nb_slots;
856  
857 11599 ▶ if (a == b) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (a == b) return 1; 5406 6193 11599
858 6193 (*visits)++;
859  
860 6193 ▶ if (a->ncoll) {
Branch: 1 branch
BranchTrueFalseTotal
if (a->ncoll) { 270 5923 6193
861 1183 ▶ for (i = 0; i < a->ncoll; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < a->ncoll; i++) { 1084 99 1183
862 1084 const hamt_entry *ea = (const hamt_entry *)a->slot[i];
863 10023 ▶ for (j = 0; j < b->ncoll; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < b->ncoll; j++) 9852 171 10023
864 9852 ▶ 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], 913 8939 9852
865 10765 ea->key, ea->klen, ea->utf8, ea->hash)) break;
866 1084 ▶ if (j == b->ncoll) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (j == b->ncoll) return 0; 171 913 1084
867 }
868 99 return 1;
869 }
870  
871 /* Anything of `a` at a position where `b` has nothing settles it. */
872 5923 ▶ 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; 289 5634 5923
873 /* A subtree holds two keys at least, and cannot fit in one entry. */
874 5634 ▶ if (a->nodemap & b->datamap) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (a->nodemap & b->datamap) return 0; 103 5531 5634
875  
876 5531 na_slots = hamt_node_nslots(a);
877 5531 nb_slots = hamt_node_nslots(b);
878 108710 ▶ for (pos = 0; pos < 32; pos++) {
Branch: 1 branch
BranchTrueFalseTotal
for (pos = 0; pos < 32; pos++) { 105525 3185 108710
879 105525 uint32_t bit = (uint32_t)1 << pos;
880 105525 ▶ if (a->datamap & bit) {
Branch: 1 branch
BranchTrueFalseTotal
if (a->datamap & bit) { 7364 98161 105525
881 7364 const hamt_entry *ea = (const hamt_entry *)
882 7364 a->slot[hamt_popcnt32(a->datamap & (bit - 1))];
883 7364 ▶ if (b->datamap & bit) {
Branch: 1 branch
BranchTrueFalseTotal
if (b->datamap & bit) { 6197 1167 7364
884 6197 const hamt_entry *eb = (const hamt_entry *)
885 6197 b->slot[hamt_popcnt32(b->datamap & (bit - 1))];
886 6197 ▶ 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; 8 6189 6197
887 }
888 else {
889 1167 const hamt_node *cb = (const hamt_node *)
890 1167 b->slot[nb_slots - 1 - hamt_popcnt32(b->nodemap & (bit - 1))];
891 1167 ▶ 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; 48 1119 1167
892 }
893 }
894 98161 ▶ else if (a->nodemap & bit) {
Branch: 1 branch
BranchTrueFalseTotal
else if (a->nodemap & bit) { 10602 87559 98161
895 10602 const hamt_node *ca = (const hamt_node *)
896 10602 a->slot[na_slots - 1 - hamt_popcnt32(a->nodemap & (bit - 1))];
897 10602 const hamt_node *cb = (const hamt_node *)
898 10602 b->slot[nb_slots - 1 - hamt_popcnt32(b->nodemap & (bit - 1))];
899 10602 ▶ 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; 2290 8312 10602
900 }
901 }
902 3185 return 1;
903 }
904  
905 1673 static int hamt_set_subset(const hamt_node *a, const hamt_node *b, size_t ca,
906 size_t cb, size_t *visits) {
907 1673 *visits = 0;
908 1673 ▶ if (!a) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!a) return 1; 2 1671 1673
909 1671 ▶ if (!b || ca > cb) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!b || ca > cb) return 0; 1670 1 1671
if (!b || ca > cb) return 0; 673 997 1670
910 997 return hamt_node_subset(a, b, 0, visits);
911 }
912  
913 /* 1 when the two have no key in common. Stops at the first they share. */
914 55453 static int hamt_node_disjoint(const hamt_node *a, const hamt_node *b, unsigned depth,
915 size_t *visits) {
916 uint32_t common, pos, i, j, na_slots, nb_slots;
917  
918 55453 ▶ if (a == b) return 0; /* a node is never empty */
Branch: 1 branch
BranchTrueFalseTotal
if (a == b) return 0; /* a node is never empty */ 473 54980 55453
919 54980 (*visits)++;
920  
921 54980 ▶ if (a->ncoll) {
Branch: 1 branch
BranchTrueFalseTotal
if (a->ncoll) { 3730 51250 54980
922 23641 ▶ for (i = 0; i < a->ncoll; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < a->ncoll; i++) { 20151 3490 23641
923 20151 const hamt_entry *ea = (const hamt_entry *)a->slot[i];
924 145227 ▶ for (j = 0; j < b->ncoll; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < b->ncoll; j++) 125316 19911 145227
925 125316 ▶ 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], 240 125076 125316
926 125556 ea->key, ea->klen, ea->utf8, ea->hash)) return 0;
927 }
928 3490 return 1;
929 }
930  
931 /* Only positions both use can hold a shared key. */
932 51250 common = (a->datamap | a->nodemap) & (b->datamap | b->nodemap);
933 51250 na_slots = hamt_node_nslots(a);
934 51250 nb_slots = hamt_node_nslots(b);
935 1570065 ▶ for (pos = 0; pos < 32; pos++) {
Branch: 1 branch
BranchTrueFalseTotal
for (pos = 0; pos < 32; pos++) { 1522681 47384 1570065
936 1522681 uint32_t bit = (uint32_t)1 << pos;
937 1522681 const hamt_entry *ea = NULL, *eb = NULL;
938 1522681 const hamt_node *ca = NULL, *cb = NULL;
939  
940 1522681 ▶ if (!(common & bit)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!(common & bit)) continue; 1458390 64291 1522681
941 64291 ▶ 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))]; 8423 55868 64291
942 55868 else ca = (const hamt_node *)a->slot[na_slots - 1 - hamt_popcnt32(a->nodemap & (bit - 1))];
943 64291 ▶ 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))]; 8175 56116 64291
944 56116 else cb = (const hamt_node *)b->slot[nb_slots - 1 - hamt_popcnt32(b->nodemap & (bit - 1))];
945  
946 64291 ▶ if (ea && eb) {
Branch: 2 branches
BranchTrueFalseTotal
if (ea && eb) { 8423 55868 64291
if (ea && eb) { 6090 2333 8423
947 6090 ▶ 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; 251 5839 6090
948 }
949 58201 ▶ else if (ea) {
Branch: 1 branch
BranchTrueFalseTotal
else if (ea) { 2333 55868 58201
950 2333 ▶ 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; 79 2254 2333
951 }
952 55868 ▶ else if (eb) {
Branch: 1 branch
BranchTrueFalseTotal
else if (eb) { 2085 53783 55868
953 2085 ▶ 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; 54 2031 2085
954 }
955 53783 ▶ 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; 3482 50301 53783
956 }
957 47384 return 1;
958 }
959  
960 1673 static int hamt_set_disjoint(const hamt_node *a, const hamt_node *b, size_t *visits) {
961 1673 *visits = 0;
962 1673 ▶ if (!a || !b) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (!a || !b) return 1; 1671 2 1673
if (!a || !b) return 1; 1 1670 1671
963 1670 return hamt_node_disjoint(a, b, 0, visits);
964 }
965  
966 #endif
« xs/vector_transient.xs ↑ Summary xs/vector.xs »