xs/map.xs

filter: Statementstmt 97.7% 217 / 222 Branchbran 87.1% 122 / 140 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 93.6% 339 / 362 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/map_transient.xs ↑ Summary xs/set.xs »
1 # HAMT::Map. Every method that "changes" a map builds a new handle around a
2 # new root and leaves the receiver's alone.
3 #
4 # These are CODE blocks that set ST(0) and return by count, not PPCODE with a
5 # local SP: storing a pair can run Perl (a tied value, a key with a string
6 # overload), which can move the stack, and ST() reads its base afresh each
7 # time where a saved SP would not.
8  
9 MODULE = HAMT PACKAGE = HAMT::Map
10  
11 void
12 new(klass, ...)
13 SV *klass
14 PREINIT:
15 hamt_map *m;
16 SV *obj;
17 I32 i;
18 CODE:
19 613 ▶ if ((items - 1) & 1)
Branch: 1 branch
BranchTrueFalseTotal
if ((items - 1) & 1) 1 612 613
20 1 croak("HAMT::Map::new: odd number of arguments");
21 612 obj = hamt_handle_new(aTHX_ &hamt_map_vtbl, hamt_class_stash(aTHX_ klass),
22 NULL, 0, &m, "HAMT::Map::new");
23 211104 ▶ for (i = 1; i < items; i += 2)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i += 2) 210493 611 211104
24 210493 hamt_xs_put(aTHX_ m, ST(i), ST(i + 1), "HAMT::Map::new");
25 611 ST(0) = obj;
26 611 XSRETURN(1);
27  
28 void
29 from_hash(klass, href)
30 SV *klass
31 SV *href
32 PREINIT:
33 hamt_map *m;
34 SV *obj;
35 HV *hv;
36 HE *he;
37 CODE:
38 9 SvGETMAGIC(href);
39 9 ▶ if (!SvROK(href) || SvTYPE(SvRV(href)) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(href) || SvTYPE(SvRV(href)) != SVt_PVHV) 8 1 9
if (!SvROK(href) || SvTYPE(SvRV(href)) != SVt_PVHV) 1 7 8
40 2 croak("HAMT::Map::from_hash: not a hash reference");
41 7 hv = (HV *)SvRV(href);
42 7 obj = hamt_handle_new(aTHX_ &hamt_map_vtbl, hamt_class_stash(aTHX_ klass),
43 NULL, 0, &m, "HAMT::Map::from_hash");
44 7 hv_iterinit(hv);
45 5023 ▶ while ((he = hv_iternext(hv))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(hv))) { 5016 7 5023
46 hamt_key k;
47 5016 hamt_key_from_he(aTHX_ hv, he, &k);
48 5016 hamt_xs_put_key(aTHX_ m, &k, hv_iterval(hv, he), "HAMT::Map::from_hash");
49 }
50 7 ST(0) = obj;
51 7 XSRETURN(1);
52  
53 void
54 set(self, ...)
55 SV *self
56 PREINIT:
57 hamt_map *m, *n;
58 hamt_node *was;
59 SV *obj;
60 I32 i;
61 CODE:
62 48487 m = hamt_handle(aTHX_ self, &hamt_map_vtbl, "HAMT::Map::set", "map");
63 48478 was = m->root;
64 48478 ▶ if ((items - 1) & 1)
Branch: 1 branch
BranchTrueFalseTotal
if ((items - 1) & 1) 2 48476 48478
65 2 croak("HAMT::Map::set: odd number of arguments");
66 48476 ▶ if (GIMME_V == G_VOID) {
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_VOID) { 3 48473 48476
67 3 hamt_void_warn(aTHX_ "HAMT::Map::set");
68 3 XSRETURN_EMPTY;
69 }
70 48473 obj = hamt_handle_new(aTHX_ &hamt_map_vtbl, SvSTASH(SvRV(self)),
71 m->root, m->count, &n, "HAMT::Map::set");
72 98951 ▶ for (i = 1; i < items; i += 2)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i += 2) 50480 48471 98951
73 50480 hamt_xs_put(aTHX_ n, ST(i), ST(i + 1), "HAMT::Map::set");
74 /* Nothing changed: the receiver is the answer. */
75 48471 ▶ if (n->root != was) ST(0) = obj;
Branch: 1 branch
BranchTrueFalseTotal
if (n->root != was) ST(0) = obj; 48467 4 48471
76 48471 XSRETURN(1);
77  
78 void
79 delete(self, ...)
80 SV *self
81 PREINIT:
82 hamt_map *m, *n;
83 hamt_node *was;
84 SV *obj;
85 I32 i;
86 CODE:
87 28528 m = hamt_handle(aTHX_ self, &hamt_map_vtbl, "HAMT::Map::delete", "map");
88 28519 was = m->root;
89 28519 ▶ if (GIMME_V == G_VOID) {
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_VOID) { 2 28517 28519
90 2 hamt_void_warn(aTHX_ "HAMT::Map::delete");
91 2 XSRETURN_EMPTY;
92 }
93 28517 obj = hamt_handle_new(aTHX_ &hamt_map_vtbl, SvSTASH(SvRV(self)),
94 m->root, m->count, &n, "HAMT::Map::delete");
95 57037 ▶ for (i = 1; i < items; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) 28520 28517 57037
96 28520 hamt_xs_del(aTHX_ n, ST(i), "HAMT::Map::delete");
97 28517 ▶ if (n->root != was) ST(0) = obj;
Branch: 1 branch
BranchTrueFalseTotal
if (n->root != was) ST(0) = obj; 16533 11984 28517
98 28517 XSRETURN(1);
99  
100 void
101 get(self, key)
102 SV *self
103 SV *key
104 PREINIT:
105 hamt_map *m;
106 hamt_key k;
107 hamt_entry *e;
108 CODE:
109 644863 m = hamt_handle(aTHX_ self, &hamt_map_vtbl, "HAMT::Map::get", "map");
110 644852 hamt_key_from_sv(aTHX_ key, &k);
111 644852 e = hamt_map_find(m->root, k.p, (size_t)k.len, k.utf8, k.hash);
112 /* The stored scalar itself, kept alive past the map by a reference
113 * of its own. A missing key is a fresh undef, never the shared one:
114 * that would be an alias to a scalar the whole interpreter uses. */
115 639114 ▶ ST(0) = (e && e->val)
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = (e && e->val) 639114 0 639114
116 639114 ? sv_2mortal(SvREFCNT_inc_simple_NN((SV *)e->val))
117 644852 ▶ : sv_newmortal();
Branch: 1 branch
BranchTrueFalseTotal
: sv_newmortal(); 639114 5738 644852
118 644852 XSRETURN(1);
119  
120 void
121 exists(self, key)
122 SV *self
123 SV *key
124 PREINIT:
125 hamt_map *m;
126 hamt_key k;
127 CODE:
128 634802 m = hamt_handle(aTHX_ self, &hamt_map_vtbl, "HAMT::Map::exists", "map");
129 634793 hamt_key_from_sv(aTHX_ key, &k);
130 634793 ▶ ST(0) = boolSV(hamt_map_find(m->root, k.p, (size_t)k.len, k.utf8, k.hash) != NULL);
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = boolSV(hamt_map_find(m->root, k.p, (size_t)k.len, k.utf8, k.hash) != NULL); 629043 5750 634793
131 634793 XSRETURN(1);
132  
133 void
134 count(self)
135 SV *self
136 PREINIT:
137 hamt_map *m;
138 CODE:
139 67268 m = hamt_handle(aTHX_ self, &hamt_map_vtbl, "HAMT::Map::count", "map");
140 67259 ST(0) = sv_2mortal(newSVuv((UV)m->count));
141 67259 XSRETURN(1);
142  
143 # keys, values and pairs share a body. In list context the list, in the
144 # trie's own order, which is the same for all three; in scalar context the
145 # count. Nothing here runs Perl, so a local SP is safe.
146 void
147 keys(self)
148 SV *self
149 ALIAS:
150 values = 1
151 pairs = 2
152 PREINIT:
153 hamt_map *m;
154 hamt_iter it;
155 hamt_entry *e;
156 static const char *const name[3] = {
157 "HAMT::Map::keys", "HAMT::Map::values", "HAMT::Map::pairs"
158 };
159 PPCODE:
160 894 m = hamt_handle(aTHX_ self, &hamt_map_vtbl, name[ix], "map");
161 867 ▶ if (GIMME_V == G_VOID) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_VOID) XSRETURN_EMPTY; 1 866 867
162 866 ▶ if (GIMME_V == G_SCALAR) {
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_SCALAR) { 3 863 866
163 3 PUSHs(sv_2mortal(newSVuv((UV)m->count)));
164 3 XSRETURN(1);
165 }
166 /* Twice the count has to fit the signed size EXTEND takes. There
167 * is no SSize_t_MAX before 5.16 or so; a quarter of size_t is under
168 * it on every platform. */
169 863 ▶ if (m->count > ((size_t)-1 >> 2))
Branch: 1 branch
BranchTrueFalseTotal
if (m->count > ((size_t)-1 >> 2)) 0 863 863
170 0 croak("%s: too many entries for one list", name[ix]);
171 863 ▶ EXTEND(SP, (SSize_t)(ix == 2 ? m->count * 2 : m->count));
Branch: 5 branches
BranchTrueFalseTotal
EXTEND(SP, (SSize_t)(ix == 2 ? m->count * 2 : m->count)); 842 21 863
EXTEND(SP, (SSize_t)(ix == 2 ? m->count * 2 : m->count)); 863 0 863
EXTEND(SP, (SSize_t)(ix == 2 ? m->count * 2 : m->count)); 842 21 863
EXTEND(SP, (SSize_t)(ix == 2 ? m->count * 2 : m->count)); 0 863 863
EXTEND(SP, (SSize_t)(ix == 2 ? m->count * 2 : m->count)); 0 0 0
172 863 hamt_iter_init(&it, m->root);
173 642327 ▶ while ((e = hamt_iter_next(&it))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((e = hamt_iter_next(&it))) { 640601 863 641464
174 640601 ▶ if (ix != 1) PUSHs(sv_2mortal(hamt_key_to_sv(aTHX_ e)));
Branch: 1 branch
BranchTrueFalseTotal
if (ix != 1) PUSHs(sv_2mortal(hamt_key_to_sv(aTHX_ e))); 640097 504 640601
175 640601 ▶ if (ix != 0) PUSHs(e->val ? sv_2mortal(SvREFCNT_inc_simple_NN((SV *)e->val))
Branch: 2 branches
BranchTrueFalseTotal
if (ix != 0) PUSHs(e->val ? sv_2mortal(SvREFCNT_inc_simple_NN((SV *)e->val)) 638630 1971 640601
if (ix != 0) PUSHs(e->val ? sv_2mortal(SvREFCNT_inc_simple_NN((SV *)e->val)) 638630 0 638630
176 : sv_newmortal());
177 }
178  
179 void
180 to_hash(self)
181 SV *self
182 PREINIT:
183 hamt_map *m;
184 hamt_iter it;
185 hamt_entry *e;
186 HV *hv;
187 CODE:
188 13 m = hamt_handle(aTHX_ self, &hamt_map_vtbl, "HAMT::Map::to_hash", "map");
189 4 hv = newHV();
190 4 ST(0) = sv_2mortal(newRV_noinc((SV *)hv));
191 4 hamt_iter_init(&it, m->root);
192 22 ▶ while ((e = hamt_iter_next(&it))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((e = hamt_iter_next(&it))) { 14 4 18
193 SV *v;
194 14 ▶ if (e->klen > (size_t)I32_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (e->klen > (size_t)I32_MAX) 0 14 14
195 0 croak("HAMT::Map::to_hash: a key is too long for a hash");
196 /* A copy, so the hash is an ordinary one: writable throughout. */
197 14 ▶ v = e->val ? newSVsv((SV *)e->val) : newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
v = e->val ? newSVsv((SV *)e->val) : newSV(0); 14 0 14
198 14 ▶ if (!hv_store(hv, e->key, e->utf8 ? -(I32)e->klen : (I32)e->klen, v, 0))
Branch: 2 branches
BranchTrueFalseTotal
if (!hv_store(hv, e->key, e->utf8 ? -(I32)e->klen : (I32)e->klen, v, 0)) 2 12 14
if (!hv_store(hv, e->key, e->utf8 ? -(I32)e->klen : (I32)e->klen, v, 0)) 14 0 14
199 0 SvREFCNT_dec(v);
200 }
201 4 XSRETURN(1);
202  
203 # Calls the sub with each key and its value. Returns the map.
204 void
205 each(self, code)
206 SV *self
207 SV *code
208 PREINIT:
209 hamt_map *m;
210 hamt_node *root;
211 hamt_iter it;
212 hamt_entry *e;
213 52 SV *keysv = NULL, *args[2], *err = NULL;
214 int how;
215 CODE:
216 52 m = hamt_handle(aTHX_ self, &hamt_map_vtbl, "HAMT::Map::each", "map");
217 52 hamt_xs_need_code(aTHX_ code, "HAMT::Map::each");
218 50 root = m->root;
219 50 ENTER;
220 50 SAVETMPS;
221 50 hamt_xs_hold_root(aTHX_ root);
222 50 hamt_xs_walk_enter(aTHX);
223 50 hamt_iter_init(&it, root);
224 6370 ▶ while ((e = hamt_iter_next(&it))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((e = hamt_iter_next(&it))) { 6353 17 6370
225 6353 keysv = hamt_xs_scratch(aTHX_ keysv);
226 6353 sv_setpvn(keysv, e->key, (STRLEN)e->klen);
227 6353 ▶ if (e->utf8) SvUTF8_on(keysv); else SvUTF8_off(keysv);
Branch: 1 branch
BranchTrueFalseTotal
if (e->utf8) SvUTF8_on(keysv); else SvUTF8_off(keysv); 3 6350 6353
228 6353 args[0] = keysv;
229 6353 args[1] = hamt_xs_val_sv(aTHX_ e);
230 /* The call frees whatever temporaries the sub makes. */
231 6353 how = hamt_xs_call(aTHX_ code, args, 2, NULL);
232 6353 ▶ if (how == HAMT_XS_LAST || (err = hamt_xs_call_err(aTHX_ how, 1))) break;
Branch: 2 branches
BranchTrueFalseTotal
if (how == HAMT_XS_LAST || (err = hamt_xs_call_err(aTHX_ how, 1))) break; 6345 8 6353
if (how == HAMT_XS_LAST || (err = hamt_xs_call_err(aTHX_ how, 1))) break; 6320 25 6345
233 }
234 50 hamt_xs_call_leave(aTHX);
235 50 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 49 1 50
236 50 LEAVE;
237 50 ▶ if (err) hamt_xs_rethrow(aTHX_ err);
Branch: 1 branch
BranchTrueFalseTotal
if (err) hamt_xs_rethrow(aTHX_ err); 25 25 50
238 25 XSRETURN(1);
239  
240 # True when the other thing is a map holding the same keys with equal values.
241 void
242 equals(self, other, ...)
243 SV *self
244 SV *other
245 PREINIT:
246 hamt_map *m, *o;
247 hamt_eq_ctx c;
248 hamt_node *ra, *rb;
249 int rc;
250 CODE:
251 321 m = hamt_handle(aTHX_ self, &hamt_map_vtbl, "HAMT::Map::equals", "map");
252 321 ▶ if (items > 3) croak("HAMT::Map::equals: too many arguments");
Branch: 1 branch
BranchTrueFalseTotal
if (items > 3) croak("HAMT::Map::equals: too many arguments"); 0 321 321
253 321 ▶ if (items == 3) hamt_xs_need_code(aTHX_ ST(2), "HAMT::Map::equals");
Branch: 1 branch
BranchTrueFalseTotal
if (items == 3) hamt_xs_need_code(aTHX_ ST(2), "HAMT::Map::equals"); 13 308 321
254 320 o = hamt_handle_try(aTHX_ other, &hamt_map_vtbl);
255 320 ▶ if (!o || o->count != m->count) XSRETURN_NO;
Branch: 2 branches
BranchTrueFalseTotal
if (!o || o->count != m->count) XSRETURN_NO; 316 4 320
if (!o || o->count != m->count) XSRETURN_NO; 17 299 316
256 299 ra = m->root;
257 299 rb = o->root;
258 299 c.cx = HAMT_CX;
259 299 ▶ c.eq = items == 3 ? hamt_xs_eq_cb : NULL;
Branch: 1 branch
BranchTrueFalseTotal
c.eq = items == 3 ? hamt_xs_eq_cb : NULL; 12 287 299
260 299 ▶ c.ud = items == 3 ? (void *)ST(2) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
c.ud = items == 3 ? (void *)ST(2) : NULL; 12 287 299
261 299 c.visits = 0;
262 299 ENTER;
263 299 hamt_xs_hold_root(aTHX_ ra);
264 299 hamt_xs_hold_root(aTHX_ rb);
265 299 rc = hamt_node_equal(&c, ra, rb);
266 296 LEAVE;
267 296 ▶ ST(0) = boolSV(rc > 0);
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = boolSV(rc > 0); 262 34 296
268 296 XSRETURN(1);
269  
270 # Every key of both. For a key in both, the other map's value, or what the
271 # sub returns when one is given.
272 void
273 merge(self, other, ...)
274 SV *self
275 SV *other
276 PREINIT:
277 hamt_map *m, *o, *n;
278 hamt_merge_ctx c;
279 hamt_xs_sub res;
280 hamt_node *ra, *rb, *r;
281 HV *stash;
282 size_t count;
283 SV *obj;
284 CODE:
285 44 m = hamt_handle(aTHX_ self, &hamt_map_vtbl, "HAMT::Map::merge", "map");
286 44 o = hamt_handle(aTHX_ other, &hamt_map_vtbl, "HAMT::Map::merge", "map");
287 43 ▶ if (items > 3) croak("HAMT::Map::merge: too many arguments");
Branch: 1 branch
BranchTrueFalseTotal
if (items > 3) croak("HAMT::Map::merge: too many arguments"); 0 43 43
288 43 ▶ if (items == 3) hamt_xs_need_code(aTHX_ ST(2), "HAMT::Map::merge");
Branch: 1 branch
BranchTrueFalseTotal
if (items == 3) hamt_xs_need_code(aTHX_ ST(2), "HAMT::Map::merge"); 24 19 43
289 42 ▶ if (GIMME_V == G_VOID) {
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_VOID) { 1 41 42
290 1 hamt_void_warn(aTHX_ "HAMT::Map::merge");
291 1 XSRETURN_EMPTY;
292 }
293 41 ra = m->root;
294 41 rb = o->root;
295 41 count = m->count;
296 41 stash = SvSTASH(SvRV(self));
297 41 ▶ res.code = items == 3 ? ST(2) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
res.code = items == 3 ? ST(2) : NULL; 23 18 41
298 41 res.err = NULL;
299 41 c.cx = HAMT_CX;
300 41 ▶ c.resolve = items == 3 ? hamt_xs_resolve_cb : NULL;
Branch: 1 branch
BranchTrueFalseTotal
c.resolve = items == 3 ? hamt_xs_resolve_cb : NULL; 23 18 41
301 41 c.ud = &res;
302  
303 41 ENTER;
304 41 hamt_xs_hold_root(aTHX_ ra);
305 41 hamt_xs_hold_root(aTHX_ rb);
306 41 r = hamt_map_merge(&c, ra, rb);
307 41 ▶ if (c.err == HAMT_ERR_ABORT) {
Branch: 1 branch
BranchTrueFalseTotal
if (c.err == HAMT_ERR_ABORT) { 7 34 41
308 /* The resolver died. Put its error back and die with it, object
309 * or string, exactly as it was. */
310 7 ▶ sv_setsv(ERRSV, res.err);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, res.err); 7 0 7
311 7 SvREFCNT_dec(res.err);
312 7 croak(NULL);
313 }
314 34 ▶ if (c.err) croak("HAMT::Map::merge: out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (c.err) croak("HAMT::Map::merge: out of memory"); 0 34 34
315  
316 34 ▶ if (r == ra) {
Branch: 1 branch
BranchTrueFalseTotal
if (r == ra) { 6 28 34
317 /* The other map added nothing and changed nothing. */
318 6 ▶ if (r) hamt_node_release(HAMT_CX, r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) hamt_node_release(HAMT_CX, r); 5 1 6
319 6 LEAVE;
320 6 XSRETURN(1);
321 }
322 28 ▶ if (r == rb && SvSTASH(SvRV(other)) == stash) {
Branch: 2 branches
BranchTrueFalseTotal
if (r == rb && SvSTASH(SvRV(other)) == stash) { 5 23 28
if (r == rb && SvSTASH(SvRV(other)) == stash) { 4 1 5
323 /* The result is exactly the other map, and it is of our class. */
324 4 ▶ if (r) hamt_node_release(HAMT_CX, r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) hamt_node_release(HAMT_CX, r); 4 0 4
325 4 LEAVE;
326 4 ST(0) = other;
327 4 XSRETURN(1);
328 }
329 24 ▶ if (r) SAVEDESTRUCTOR_X(hamt_xs_root_unref, r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SAVEDESTRUCTOR_X(hamt_xs_root_unref, r); 24 0 24
330 24 obj = hamt_handle_new(aTHX_ &hamt_map_vtbl, stash, r, count + c.added, &n,
331 "HAMT::Map::merge");
332 24 LEAVE;
333 24 ST(0) = obj;
334 24 XSRETURN(1);
335  
336 # The map with one key's value replaced by what the sub makes of it.
337 void
338 update(self, key, code)
339 SV *self
340 SV *key
341 SV *code
342 PREINIT:
343 hamt_map *m, *n;
344 hamt_key k;
345 hamt_node *root;
346 hamt_entry *e;
347 SV *obj, *keep, *res, *err;
348 int same;
349 CODE:
350 50 m = hamt_handle(aTHX_ self, &hamt_map_vtbl, "HAMT::Map::update", "map");
351 50 hamt_xs_need_code(aTHX_ code, "HAMT::Map::update");
352 49 ▶ if (GIMME_V == G_VOID) {
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_VOID) { 1 48 49
353 1 hamt_void_warn(aTHX_ "HAMT::Map::update");
354 1 XSRETURN_EMPTY;
355 }
356 48 hamt_key_from_sv(aTHX_ key, &k);
357 /* The sub may change or free the scalar the key was read from, so the
358 * bytes are kept in a scalar of this call's own. */
359 48 keep = sv_2mortal(newSVpvn(k.p, k.len));
360 48 k.p = SvPVX(keep);
361 48 root = m->root;
362 48 obj = hamt_handle_new(aTHX_ &hamt_map_vtbl, SvSTASH(SvRV(self)), root,
363 m->count, &n, "HAMT::Map::update");
364 48 e = hamt_map_find(root, k.p, (size_t)k.len, k.utf8, k.hash);
365 {
366 SV *arg;
367 48 ENTER;
368 48 SAVETMPS;
369 48 hamt_xs_hold_root(aTHX_ root);
370 48 arg = hamt_xs_val_sv(aTHX_ e);
371 48 hamt_xs_call_enter(aTHX);
372 48 err = hamt_xs_call_err(aTHX_ hamt_xs_call(aTHX_ code, &arg, 1, &res), 0);
373 48 hamt_xs_call_leave(aTHX);
374 /* Stored before the frame goes: what the sub returned is a
375 * temporary of that frame. */
376 /* By value, not by address: a sub's return is always a copy. */
377 48 ▶ same = err || (e && e->val && hamt_sv_eq_cx(HAMT_CX, res, (SV *)e->val));
Branch: 4 branches
BranchTrueFalseTotal
same = err || (e && e->val && hamt_sv_eq_cx(HAMT_CX, res, (SV *)e->val)); 23 25 48
same = err || (e && e->val && hamt_sv_eq_cx(HAMT_CX, res, (SV *)e->val)); 21 2 23
same = err || (e && e->val && hamt_sv_eq_cx(HAMT_CX, res, (SV *)e->val)); 21 0 21
same = err || (e && e->val && hamt_sv_eq_cx(HAMT_CX, res, (SV *)e->val)); 4 17 21
378 48 ▶ if (!same) hamt_xs_put_key(aTHX_ n, &k, res, "HAMT::Map::update");
Branch: 1 branch
BranchTrueFalseTotal
if (!same) hamt_xs_put_key(aTHX_ n, &k, res, "HAMT::Map::update"); 19 29 48
379 48 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 48 0 48
380 48 LEAVE;
381 }
382 48 ▶ if (err) hamt_xs_rethrow(aTHX_ err);
Branch: 1 branch
BranchTrueFalseTotal
if (err) hamt_xs_rethrow(aTHX_ err); 25 23 48
383 23 ▶ if (!same) ST(0) = obj;
Branch: 1 branch
BranchTrueFalseTotal
if (!same) ST(0) = obj; 19 4 23
384 23 XSRETURN(1);
385  
386 # Calls the sub for every key that is in one map and not the other, or in
387 # both with values that are not equal. Returns how many that was.
388 void
389 diff(self, other, code)
390 SV *self
391 SV *other
392 SV *code
393 PREINIT:
394 hamt_map *m, *o;
395 hamt_diff_ctx c;
396 hamt_xs_sub sub;
397 hamt_node *ra, *rb;
398 CODE:
399 34 m = hamt_handle(aTHX_ self, &hamt_map_vtbl, "HAMT::Map::diff", "map");
400 34 o = hamt_handle(aTHX_ other, &hamt_map_vtbl, "HAMT::Map::diff", "map");
401 33 hamt_xs_need_code(aTHX_ code, "HAMT::Map::diff");
402 32 ra = m->root;
403 32 rb = o->root;
404 32 c.cx = HAMT_CX;
405 32 sub.code = code;
406 32 sub.err = NULL;
407 32 c.fn = hamt_xs_diff_cb;
408 32 c.ud = &sub;
409 32 c.eq.cx = HAMT_CX;
410 32 c.eq.eq = NULL;
411 32 c.eq.ud = NULL;
412 32 ENTER;
413 32 hamt_xs_hold_root(aTHX_ ra);
414 32 hamt_xs_hold_root(aTHX_ rb);
415 32 hamt_xs_walk_enter(aTHX);
416 32 hamt_map_diff(&c, ra, rb);
417 32 hamt_xs_call_leave(aTHX);
418 32 LEAVE;
419 32 ▶ if (sub.err) hamt_xs_rethrow(aTHX_ sub.err);
Branch: 1 branch
BranchTrueFalseTotal
if (sub.err) hamt_xs_rethrow(aTHX_ sub.err); 3 29 32
420 29 ST(0) = sv_2mortal(newSVuv((UV)c.reported));
421 29 XSRETURN(1);
422  
423 # The keys of the map, as a set. The set shares nothing with the map: a
424 # map's entries carry values and a set's do not.
425 void
426 key_set(self)
427 SV *self
428 PREINIT:
429 hamt_map *m, *n;
430 hamt_node *root;
431 hamt_iter it;
432 hamt_entry *e;
433 SV *obj;
434 CODE:
435 6 m = hamt_handle(aTHX_ self, &hamt_map_vtbl, "HAMT::Map::key_set", "map");
436 6 root = m->root;
437 6 obj = hamt_handle_new(aTHX_ &hamt_set_vtbl, gv_stashpvs("HAMT::Set", GV_ADD),
438 NULL, 0, &n, "HAMT::Map::key_set");
439 6 hamt_iter_init(&it, root);
440 3510 ▶ while ((e = hamt_iter_next(&it))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((e = hamt_iter_next(&it))) { 3504 6 3510
441 hamt_key k;
442 3504 k.p = e->key; k.len = (STRLEN)e->klen; k.utf8 = e->utf8; k.hash = e->hash;
443 3504 hamt_xs_put_key(aTHX_ n, &k, NULL, "HAMT::Map::key_set");
444 }
445 6 ST(0) = obj;
446 6 XSRETURN(1);
447  
448 # A transient opened on this map: it starts out holding what the map holds,
449 # and the map never sees what is done to it.
450 void
451 transient(self)
452 SV *self
453 PREINIT:
454 hamt_map *m;
455 CODE:
456 230 m = hamt_handle(aTHX_ self, &hamt_map_vtbl, "HAMT::Map::transient", "map");
457 460 ST(0) = hamt_txn_new(aTHX_ &hamt_map_txn_vtbl, "HAMT::Map::Transient",
458 230 SvSTASH(SvRV(self)), m->root, m->count,
459 "HAMT::Map::transient");
460 230 XSRETURN(1);
461  
462 # For the tests: 0, or the number of the rule in hamt_check.h that the trie
463 # breaks.
464 void
465 _check(self)
466 SV *self
467 PREINIT:
468 hamt_map *m;
469 CODE:
470 20977 m = hamt_handle(aTHX_ self, &hamt_map_vtbl, "HAMT::Map::_check", "map");
471 20968 ST(0) = sv_2mortal(newSViv((IV)hamt_map_check(m->root, m->count)));
472 20968 XSRETURN(1);
473  
474 # A handle is not carried into a new thread: its nodes hold this
475 # interpreter's scalars.
476 void
477 CLONE_SKIP(...)
478 CODE:
479 0 ST(0) = &PL_sv_yes;
480 0 XSRETURN(1);
« xs/map_transient.xs ↑ Summary xs/set.xs »