xs/map.xs

filter: Statementstmt 97.7% 218 / 223 Branchbran 88.2% 120 / 136 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 94.1% 338 / 359 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 SSize_t n;
157 static const char *const name[3] = {
158 "HAMT::Map::keys", "HAMT::Map::values", "HAMT::Map::pairs"
159 };
160 PPCODE:
161 894 m = hamt_handle(aTHX_ self, &hamt_map_vtbl, name[ix], "map");
162 867 ▶ if (GIMME_V == G_VOID) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_VOID) XSRETURN_EMPTY; 1 866 867
163 866 ▶ if (GIMME_V == G_SCALAR) {
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_SCALAR) { 3 863 866
164 3 PUSHs(sv_2mortal(newSVuv((UV)m->count)));
165 3 XSRETURN(1);
166 }
167 /* Twice the count has to fit the signed size EXTEND takes. There
168 * is no SSize_t_MAX before 5.16 or so; a quarter of size_t is under
169 * it on every platform. */
170 863 ▶ if (m->count > ((size_t)-1 >> 2))
Branch: 1 branch
BranchTrueFalseTotal
if (m->count > ((size_t)-1 >> 2)) 0 863 863
171 0 croak("%s: too many entries for one list", name[ix]);
172 /* Counted outside EXTEND: on a DEBUGGING perl from 5.28 to 5.36 the
173 * macro declares a local named ix, which hides the alias's ix in
174 * its argument and extended by the count alone for pairs. */
175 863 ▶ n = (SSize_t)(ix == 2 ? m->count * 2 : m->count);
Branch: 1 branch
BranchTrueFalseTotal
n = (SSize_t)(ix == 2 ? m->count * 2 : m->count); 842 21 863
176 863 ▶ EXTEND(SP, n);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, n); 863 0 863
EXTEND(SP, n); 0 863 863
177 863 hamt_iter_init(&it, m->root);
178 642327 ▶ while ((e = hamt_iter_next(&it))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((e = hamt_iter_next(&it))) { 640601 863 641464
179 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
180 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
181 : sv_newmortal());
182 }
183  
184 void
185 to_hash(self)
186 SV *self
187 PREINIT:
188 hamt_map *m;
189 hamt_iter it;
190 hamt_entry *e;
191 HV *hv;
192 CODE:
193 13 m = hamt_handle(aTHX_ self, &hamt_map_vtbl, "HAMT::Map::to_hash", "map");
194 4 hv = newHV();
195 4 ST(0) = sv_2mortal(newRV_noinc((SV *)hv));
196 4 hamt_iter_init(&it, m->root);
197 22 ▶ while ((e = hamt_iter_next(&it))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((e = hamt_iter_next(&it))) { 14 4 18
198 SV *v;
199 14 ▶ if (e->klen > (size_t)I32_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (e->klen > (size_t)I32_MAX) 0 14 14
200 0 croak("HAMT::Map::to_hash: a key is too long for a hash");
201 /* A copy, so the hash is an ordinary one: writable throughout. */
202 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
203 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
204 0 SvREFCNT_dec(v);
205 }
206 4 XSRETURN(1);
207  
208 # Calls the sub with each key and its value. Returns the map.
209 void
210 each(self, code)
211 SV *self
212 SV *code
213 PREINIT:
214 hamt_map *m;
215 hamt_node *root;
216 hamt_iter it;
217 hamt_entry *e;
218 52 SV *keysv = NULL, *args[2], *err = NULL;
219 int how;
220 CODE:
221 52 m = hamt_handle(aTHX_ self, &hamt_map_vtbl, "HAMT::Map::each", "map");
222 52 hamt_xs_need_code(aTHX_ code, "HAMT::Map::each");
223 50 root = m->root;
224 50 ENTER;
225 50 SAVETMPS;
226 50 hamt_xs_hold_root(aTHX_ root);
227 50 hamt_xs_walk_enter(aTHX);
228 50 hamt_iter_init(&it, root);
229 6370 ▶ while ((e = hamt_iter_next(&it))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((e = hamt_iter_next(&it))) { 6353 17 6370
230 6353 keysv = hamt_xs_scratch(aTHX_ keysv);
231 6353 sv_setpvn(keysv, e->key, (STRLEN)e->klen);
232 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
233 6353 args[0] = keysv;
234 6353 args[1] = hamt_xs_val_sv(aTHX_ e);
235 /* The call frees whatever temporaries the sub makes. */
236 6353 how = hamt_xs_call(aTHX_ code, args, 2, NULL);
237 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
238 }
239 50 hamt_xs_call_leave(aTHX);
240 50 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 49 1 50
241 50 LEAVE;
242 50 ▶ if (err) hamt_xs_rethrow(aTHX_ err);
Branch: 1 branch
BranchTrueFalseTotal
if (err) hamt_xs_rethrow(aTHX_ err); 25 25 50
243 25 XSRETURN(1);
244  
245 # True when the other thing is a map holding the same keys with equal values.
246 void
247 equals(self, other, ...)
248 SV *self
249 SV *other
250 PREINIT:
251 hamt_map *m, *o;
252 hamt_eq_ctx c;
253 hamt_node *ra, *rb;
254 int rc;
255 CODE:
256 321 m = hamt_handle(aTHX_ self, &hamt_map_vtbl, "HAMT::Map::equals", "map");
257 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
258 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
259 320 o = hamt_handle_try(aTHX_ other, &hamt_map_vtbl);
260 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
261 299 ra = m->root;
262 299 rb = o->root;
263 299 c.cx = HAMT_CX;
264 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
265 299 ▶ c.ud = items == 3 ? (void *)ST(2) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
c.ud = items == 3 ? (void *)ST(2) : NULL; 12 287 299
266 299 c.visits = 0;
267 299 ENTER;
268 299 hamt_xs_hold_root(aTHX_ ra);
269 299 hamt_xs_hold_root(aTHX_ rb);
270 299 rc = hamt_node_equal(&c, ra, rb);
271 296 LEAVE;
272 296 ▶ ST(0) = boolSV(rc > 0);
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = boolSV(rc > 0); 262 34 296
273 296 XSRETURN(1);
274  
275 # Every key of both. For a key in both, the other map's value, or what the
276 # sub returns when one is given.
277 void
278 merge(self, other, ...)
279 SV *self
280 SV *other
281 PREINIT:
282 hamt_map *m, *o, *n;
283 hamt_merge_ctx c;
284 hamt_xs_sub res;
285 hamt_node *ra, *rb, *r;
286 HV *stash;
287 size_t count;
288 SV *obj;
289 CODE:
290 44 m = hamt_handle(aTHX_ self, &hamt_map_vtbl, "HAMT::Map::merge", "map");
291 44 o = hamt_handle(aTHX_ other, &hamt_map_vtbl, "HAMT::Map::merge", "map");
292 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
293 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
294 42 ▶ if (GIMME_V == G_VOID) {
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_VOID) { 1 41 42
295 1 hamt_void_warn(aTHX_ "HAMT::Map::merge");
296 1 XSRETURN_EMPTY;
297 }
298 41 ra = m->root;
299 41 rb = o->root;
300 41 count = m->count;
301 41 stash = SvSTASH(SvRV(self));
302 41 ▶ res.code = items == 3 ? ST(2) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
res.code = items == 3 ? ST(2) : NULL; 23 18 41
303 41 res.err = NULL;
304 41 c.cx = HAMT_CX;
305 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
306 41 c.ud = &res;
307  
308 41 ENTER;
309 41 hamt_xs_hold_root(aTHX_ ra);
310 41 hamt_xs_hold_root(aTHX_ rb);
311 41 r = hamt_map_merge(&c, ra, rb);
312 41 ▶ if (c.err == HAMT_ERR_ABORT) {
Branch: 1 branch
BranchTrueFalseTotal
if (c.err == HAMT_ERR_ABORT) { 7 34 41
313 /* The resolver died. Put its error back and die with it, object
314 * or string, exactly as it was. */
315 7 ▶ sv_setsv(ERRSV, res.err);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, res.err); 7 0 7
316 7 SvREFCNT_dec(res.err);
317 7 croak(NULL);
318 }
319 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
320  
321 34 ▶ if (r == ra) {
Branch: 1 branch
BranchTrueFalseTotal
if (r == ra) { 6 28 34
322 /* The other map added nothing and changed nothing. */
323 6 ▶ if (r) hamt_node_release(HAMT_CX, r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) hamt_node_release(HAMT_CX, r); 5 1 6
324 6 LEAVE;
325 6 XSRETURN(1);
326 }
327 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
328 /* The result is exactly the other map, and it is of our class. */
329 4 ▶ if (r) hamt_node_release(HAMT_CX, r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) hamt_node_release(HAMT_CX, r); 4 0 4
330 4 LEAVE;
331 4 ST(0) = other;
332 4 XSRETURN(1);
333 }
334 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
335 24 obj = hamt_handle_new(aTHX_ &hamt_map_vtbl, stash, r, count + c.added, &n,
336 "HAMT::Map::merge");
337 24 LEAVE;
338 24 ST(0) = obj;
339 24 XSRETURN(1);
340  
341 # The map with one key's value replaced by what the sub makes of it.
342 void
343 update(self, key, code)
344 SV *self
345 SV *key
346 SV *code
347 PREINIT:
348 hamt_map *m, *n;
349 hamt_key k;
350 hamt_node *root;
351 hamt_entry *e;
352 SV *obj, *keep, *res, *err;
353 int same;
354 CODE:
355 50 m = hamt_handle(aTHX_ self, &hamt_map_vtbl, "HAMT::Map::update", "map");
356 50 hamt_xs_need_code(aTHX_ code, "HAMT::Map::update");
357 49 ▶ if (GIMME_V == G_VOID) {
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_VOID) { 1 48 49
358 1 hamt_void_warn(aTHX_ "HAMT::Map::update");
359 1 XSRETURN_EMPTY;
360 }
361 48 hamt_key_from_sv(aTHX_ key, &k);
362 /* The sub may change or free the scalar the key was read from, so the
363 * bytes are kept in a scalar of this call's own. */
364 48 keep = sv_2mortal(newSVpvn(k.p, k.len));
365 48 k.p = SvPVX(keep);
366 48 root = m->root;
367 48 obj = hamt_handle_new(aTHX_ &hamt_map_vtbl, SvSTASH(SvRV(self)), root,
368 m->count, &n, "HAMT::Map::update");
369 48 e = hamt_map_find(root, k.p, (size_t)k.len, k.utf8, k.hash);
370 {
371 SV *arg;
372 48 ENTER;
373 48 SAVETMPS;
374 48 hamt_xs_hold_root(aTHX_ root);
375 48 arg = hamt_xs_val_sv(aTHX_ e);
376 48 hamt_xs_call_enter(aTHX);
377 48 err = hamt_xs_call_err(aTHX_ hamt_xs_call(aTHX_ code, &arg, 1, &res), 0);
378 48 hamt_xs_call_leave(aTHX);
379 /* Stored before the frame goes: what the sub returned is a
380 * temporary of that frame. */
381 /* By value, not by address: a sub's return is always a copy. */
382 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
383 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
384 48 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 48 0 48
385 48 LEAVE;
386 }
387 48 ▶ if (err) hamt_xs_rethrow(aTHX_ err);
Branch: 1 branch
BranchTrueFalseTotal
if (err) hamt_xs_rethrow(aTHX_ err); 25 23 48
388 23 ▶ if (!same) ST(0) = obj;
Branch: 1 branch
BranchTrueFalseTotal
if (!same) ST(0) = obj; 19 4 23
389 23 XSRETURN(1);
390  
391 # Calls the sub for every key that is in one map and not the other, or in
392 # both with values that are not equal. Returns how many that was.
393 void
394 diff(self, other, code)
395 SV *self
396 SV *other
397 SV *code
398 PREINIT:
399 hamt_map *m, *o;
400 hamt_diff_ctx c;
401 hamt_xs_sub sub;
402 hamt_node *ra, *rb;
403 CODE:
404 34 m = hamt_handle(aTHX_ self, &hamt_map_vtbl, "HAMT::Map::diff", "map");
405 34 o = hamt_handle(aTHX_ other, &hamt_map_vtbl, "HAMT::Map::diff", "map");
406 33 hamt_xs_need_code(aTHX_ code, "HAMT::Map::diff");
407 32 ra = m->root;
408 32 rb = o->root;
409 32 c.cx = HAMT_CX;
410 32 sub.code = code;
411 32 sub.err = NULL;
412 32 c.fn = hamt_xs_diff_cb;
413 32 c.ud = &sub;
414 32 c.eq.cx = HAMT_CX;
415 32 c.eq.eq = NULL;
416 32 c.eq.ud = NULL;
417 32 ENTER;
418 32 hamt_xs_hold_root(aTHX_ ra);
419 32 hamt_xs_hold_root(aTHX_ rb);
420 32 hamt_xs_walk_enter(aTHX);
421 32 hamt_map_diff(&c, ra, rb);
422 32 hamt_xs_call_leave(aTHX);
423 32 LEAVE;
424 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
425 29 ST(0) = sv_2mortal(newSVuv((UV)c.reported));
426 29 XSRETURN(1);
427  
428 # The keys of the map, as a set. The set shares nothing with the map: a
429 # map's entries carry values and a set's do not.
430 void
431 key_set(self)
432 SV *self
433 PREINIT:
434 hamt_map *m, *n;
435 hamt_node *root;
436 hamt_iter it;
437 hamt_entry *e;
438 SV *obj;
439 CODE:
440 6 m = hamt_handle(aTHX_ self, &hamt_map_vtbl, "HAMT::Map::key_set", "map");
441 6 root = m->root;
442 6 obj = hamt_handle_new(aTHX_ &hamt_set_vtbl, gv_stashpvs("HAMT::Set", GV_ADD),
443 NULL, 0, &n, "HAMT::Map::key_set");
444 6 hamt_iter_init(&it, root);
445 3510 ▶ while ((e = hamt_iter_next(&it))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((e = hamt_iter_next(&it))) { 3504 6 3510
446 hamt_key k;
447 3504 k.p = e->key; k.len = (STRLEN)e->klen; k.utf8 = e->utf8; k.hash = e->hash;
448 3504 hamt_xs_put_key(aTHX_ n, &k, NULL, "HAMT::Map::key_set");
449 }
450 6 ST(0) = obj;
451 6 XSRETURN(1);
452  
453 # A transient opened on this map: it starts out holding what the map holds,
454 # and the map never sees what is done to it.
455 void
456 transient(self)
457 SV *self
458 PREINIT:
459 hamt_map *m;
460 CODE:
461 230 m = hamt_handle(aTHX_ self, &hamt_map_vtbl, "HAMT::Map::transient", "map");
462 460 ST(0) = hamt_txn_new(aTHX_ &hamt_map_txn_vtbl, "HAMT::Map::Transient",
463 230 SvSTASH(SvRV(self)), m->root, m->count,
464 "HAMT::Map::transient");
465 230 XSRETURN(1);
466  
467 # For the tests: 0, or the number of the rule in hamt_check.h that the trie
468 # breaks.
469 void
470 _check(self)
471 SV *self
472 PREINIT:
473 hamt_map *m;
474 CODE:
475 20977 m = hamt_handle(aTHX_ self, &hamt_map_vtbl, "HAMT::Map::_check", "map");
476 20968 ST(0) = sv_2mortal(newSViv((IV)hamt_map_check(m->root, m->count)));
477 20968 XSRETURN(1);
478  
479 # A handle is not carried into a new thread: its nodes hold this
480 # interpreter's scalars.
481 void
482 CLONE_SKIP(...)
483 CODE:
484 0 ST(0) = &PL_sv_yes;
485 0 XSRETURN(1);
« xs/map_transient.xs ↑ Summary xs/set.xs »