xs/map.xs

filter: Statementstmt 97.7% 218 / 223 Branchbran 90.4% 123 / 136 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 94.9% 341 / 359 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/set.xs ↑ Summary xs/map_transient.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 1177 ▶ if ((items - 1) & 1)
Branch: 1 branch
BranchTrueFalseTotal
if ((items - 1) & 1) 1 1176 1177
20 1 croak("HAMT::Map::new: odd number of arguments");
21 1176 obj = hamt_handle_new(aTHX_ &hamt_map_vtbl, hamt_class_stash(aTHX_ klass),
22 NULL, 0, &m, "HAMT::Map::new");
23 278161 ▶ for (i = 1; i < items; i += 2)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i += 2) 277504 657 278161
24 277504 hamt_xs_put(aTHX_ m, ST(i), ST(i + 1), "HAMT::Map::new");
25 657 ST(0) = obj;
26 657 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 429 SvGETMAGIC(href);
39 429 ▶ if (!SvROK(href) || SvTYPE(SvRV(href)) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(href) || SvTYPE(SvRV(href)) != SVt_PVHV) 428 1 429
if (!SvROK(href) || SvTYPE(SvRV(href)) != SVt_PVHV) 1 427 428
40 2 croak("HAMT::Map::from_hash: not a hash reference");
41 427 hv = (HV *)SvRV(href);
42 427 obj = hamt_handle_new(aTHX_ &hamt_map_vtbl, hamt_class_stash(aTHX_ klass),
43 NULL, 0, &m, "HAMT::Map::from_hash");
44 426 hv_iterinit(hv);
45 67338 ▶ while ((he = hv_iternext(hv))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(hv))) { 67330 8 67338
46 hamt_key k;
47 67330 hamt_key_from_he(aTHX_ hv, he, &k);
48 67330 hamt_xs_put_key(aTHX_ m, &k, hv_iterval(hv, he), "HAMT::Map::from_hash");
49 }
50 8 ST(0) = obj;
51 8 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 48883 m = hamt_handle(aTHX_ self, &hamt_map_vtbl, "HAMT::Map::set", "map");
63 48874 was = m->root;
64 48874 ▶ if ((items - 1) & 1)
Branch: 1 branch
BranchTrueFalseTotal
if ((items - 1) & 1) 2 48872 48874
65 2 croak("HAMT::Map::set: odd number of arguments");
66 48872 ▶ if (GIMME_V == G_VOID) {
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_VOID) { 3 48869 48872
67 3 hamt_void_warn(aTHX_ "HAMT::Map::set");
68 3 XSRETURN_EMPTY;
69 }
70 48869 obj = hamt_handle_new(aTHX_ &hamt_map_vtbl, SvSTASH(SvRV(self)),
71 m->root, m->count, &n, "HAMT::Map::set");
72 129848 ▶ for (i = 1; i < items; i += 2)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i += 2) 81356 48492 129848
73 81356 hamt_xs_put(aTHX_ n, ST(i), ST(i + 1), "HAMT::Map::set");
74 /* Nothing changed: the receiver is the answer. */
75 48492 ▶ if (n->root != was) ST(0) = obj;
Branch: 1 branch
BranchTrueFalseTotal
if (n->root != was) ST(0) = obj; 48488 4 48492
76 48492 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 28871 m = hamt_handle(aTHX_ self, &hamt_map_vtbl, "HAMT::Map::delete", "map");
88 28862 was = m->root;
89 28862 ▶ if (GIMME_V == G_VOID) {
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_VOID) { 2 28860 28862
90 2 hamt_void_warn(aTHX_ "HAMT::Map::delete");
91 2 XSRETURN_EMPTY;
92 }
93 28860 obj = hamt_handle_new(aTHX_ &hamt_map_vtbl, SvSTASH(SvRV(self)),
94 m->root, m->count, &n, "HAMT::Map::delete");
95 64900 ▶ for (i = 1; i < items; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) 36167 28733 64900
96 36167 hamt_xs_del(aTHX_ n, ST(i), "HAMT::Map::delete");
97 28733 ▶ if (n->root != was) ST(0) = obj;
Branch: 1 branch
BranchTrueFalseTotal
if (n->root != was) ST(0) = obj; 16545 12188 28733
98 28733 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 644865 m = hamt_handle(aTHX_ self, &hamt_map_vtbl, "HAMT::Map::get", "map");
110 644854 hamt_key_from_sv(aTHX_ key, &k);
111 644854 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 639116 ▶ ST(0) = (e && e->val)
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = (e && e->val) 639116 0 639116
116 639116 ? sv_2mortal(SvREFCNT_inc_simple_NN((SV *)e->val))
117 644854 ▶ : sv_newmortal();
Branch: 1 branch
BranchTrueFalseTotal
: sv_newmortal(); 639116 5738 644854
118 644854 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 634804 m = hamt_handle(aTHX_ self, &hamt_map_vtbl, "HAMT::Map::exists", "map");
129 634795 hamt_key_from_sv(aTHX_ key, &k);
130 634795 ▶ 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); 629044 5751 634795
131 634795 XSRETURN(1);
132  
133 void
134 count(self)
135 SV *self
136 PREINIT:
137 hamt_map *m;
138 CODE:
139 71737 m = hamt_handle(aTHX_ self, &hamt_map_vtbl, "HAMT::Map::count", "map");
140 71728 ST(0) = sv_2mortal(newSVuv((UV)m->count));
141 71728 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 897 m = hamt_handle(aTHX_ self, &hamt_map_vtbl, name[ix], "map");
162 870 ▶ if (GIMME_V == G_VOID) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_VOID) XSRETURN_EMPTY; 1 869 870
163 869 ▶ if (GIMME_V == G_SCALAR) {
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_SCALAR) { 3 866 869
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 866 ▶ # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true 0 866 866
171 0 # uncoverable condition left count:1
172 if (m->count > ((size_t)-1 >> 2))
173 croak("%s: too many entries for one list", name[ix]); /* # uncoverable statement */
174 /* Counted outside EXTEND: on a DEBUGGING perl from 5.28 to 5.36 the
175 866 ▶ * macro declares a local named ix, which hides the alias's ix in
Branch: 1 branch
BranchTrueFalseTotal
* macro declares a local named ix, which hides the alias's ix in 843 23 866
176 866 ▶ * its argument and extended by the count alone for pairs. */
Branch: 2 branches
BranchTrueFalseTotal
* its argument and extended by the count alone for pairs. */ 866 0 866
* its argument and extended by the count alone for pairs. */ 0 866 866
177 866 n = (SSize_t)(ix == 2 ? m->count * 2 : m->count);
178 643233 ▶ EXTEND(SP, n);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, n); 641501 866 642367
179 641501 ▶ hamt_iter_init(&it, m->root);
Branch: 1 branch
BranchTrueFalseTotal
hamt_iter_init(&it, m->root); 640697 804 641501
180 641501 ▶ while ((e = hamt_iter_next(&it))) {
Branch: 2 branches
BranchTrueFalseTotal
while ((e = hamt_iter_next(&it))) { 639230 2271 641501
while ((e = hamt_iter_next(&it))) { 639230 0 639230
181 if (ix != 1) PUSHs(sv_2mortal(hamt_key_to_sv(aTHX_ e)));
182 if (ix != 0) PUSHs(e->val ? sv_2mortal(SvREFCNT_inc_simple_NN((SV *)e->val)) /* # uncoverable condition right count:2 */
183 : sv_newmortal());
184 }
185  
186 void
187 to_hash(self)
188 SV *self
189 PREINIT:
190 hamt_map *m;
191 hamt_iter it;
192 hamt_entry *e;
193 HV *hv;
194 CODE:
195 14 m = hamt_handle(aTHX_ self, &hamt_map_vtbl, "HAMT::Map::to_hash", "map");
196 5 hv = newHV();
197 5 ST(0) = sv_2mortal(newRV_noinc((SV *)hv));
198 5 hamt_iter_init(&it, m->root);
199 324 ▶ while ((e = hamt_iter_next(&it))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((e = hamt_iter_next(&it))) { 314 5 319
200 SV *v;
201 314 ▶ # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true 0 314 314
202 0 # uncoverable condition left count:1
203 if (e->klen > (size_t)I32_MAX)
204 314 ▶ croak("HAMT::Map::to_hash: a key is too long for a hash"); /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
croak("HAMT::Map::to_hash: a key is too long for a hash"); /* # uncoverable statement */ 314 0 314
205 314 ▶ /* A copy, so the hash is an ordinary one: writable throughout. */
Branch: 2 branches
BranchTrueFalseTotal
/* A copy, so the hash is an ordinary one: writable throughout. */ 2 312 314
/* A copy, so the hash is an ordinary one: writable throughout. */ 314 0 314
206 0 v = e->val ? newSVsv((SV *)e->val) : newSV(0); /* # uncoverable condition right count:1 */
207 # uncoverable branch true
208 5 # uncoverable condition left count:2
209 if (!hv_store(hv, e->key, e->utf8 ? -(I32)e->klen : (I32)e->klen, v, 0))
210 SvREFCNT_dec(v); /* # uncoverable statement */
211 }
212 XSRETURN(1);
213  
214 # Calls the sub with each key and its value. Returns the map.
215 void
216 each(self, code)
217 SV *self
218 SV *code
219 PREINIT:
220 hamt_map *m;
221 hamt_node *root;
222 hamt_iter it;
223 hamt_entry *e;
224 55 SV *keysv = NULL, *args[2], *err = NULL;
225 int how;
226 CODE:
227 55 m = hamt_handle(aTHX_ self, &hamt_map_vtbl, "HAMT::Map::each", "map");
228 55 hamt_xs_need_code(aTHX_ code, "HAMT::Map::each");
229 53 root = m->root;
230 53 ENTER;
231 53 SAVETMPS;
232 53 hamt_xs_hold_root(aTHX_ root);
233 53 hamt_xs_walk_enter(aTHX);
234 53 hamt_iter_init(&it, root);
235 6675 ▶ while ((e = hamt_iter_next(&it))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((e = hamt_iter_next(&it))) { 6655 20 6675
236 6655 keysv = hamt_xs_scratch(aTHX_ keysv);
237 6655 sv_setpvn(keysv, e->key, (STRLEN)e->klen);
238 6655 ▶ 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 6652 6655
239 6655 args[0] = keysv;
240 6655 args[1] = hamt_xs_val_sv(aTHX_ e);
241 /* The call frees whatever temporaries the sub makes. */
242 6655 how = hamt_xs_call(aTHX_ code, args, 2, NULL);
243 6655 ▶ if (how == HAMT_XS_LAST || (err = hamt_xs_call_err(aTHX_ how, 1))) break; /* # uncoverable branch true count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (how == HAMT_XS_LAST || (err = hamt_xs_call_err(aTHX_ how, 1))) break; /* # uncoverable branch true count:1 class:ignore_covered_err */ 6647 8 6655
if (how == HAMT_XS_LAST || (err = hamt_xs_call_err(aTHX_ how, 1))) break; /* # uncoverable branch true count:1 class:ignore_covered_err */ 6622 25 6647
244 }
245 53 hamt_xs_call_leave(aTHX);
246 53 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 52 1 53
247 53 LEAVE;
248 53 ▶ if (err) hamt_xs_rethrow(aTHX_ err);
Branch: 1 branch
BranchTrueFalseTotal
if (err) hamt_xs_rethrow(aTHX_ err); 25 28 53
249 28 XSRETURN(1);
250  
251 # True when the other thing is a map holding the same keys with equal values.
252 void
253 equals(self, other, ...)
254 SV *self
255 SV *other
256 PREINIT:
257 hamt_map *m, *o;
258 hamt_eq_ctx c;
259 hamt_node *ra, *rb;
260 int rc;
261 CODE:
262 331 m = hamt_handle(aTHX_ self, &hamt_map_vtbl, "HAMT::Map::equals", "map");
263 331 ▶ if (items > 3) croak("HAMT::Map::equals: too many arguments");
Branch: 1 branch
BranchTrueFalseTotal
if (items > 3) croak("HAMT::Map::equals: too many arguments"); 1 330 331
264 330 ▶ 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"); 14 316 330
265 329 o = hamt_handle_try(aTHX_ other, &hamt_map_vtbl);
266 329 ▶ if (!o || o->count != m->count) XSRETURN_NO;
Branch: 2 branches
BranchTrueFalseTotal
if (!o || o->count != m->count) XSRETURN_NO; 325 4 329
if (!o || o->count != m->count) XSRETURN_NO; 19 306 325
267 306 ra = m->root;
268 306 rb = o->root;
269 306 c.cx = HAMT_CX;
270 306 ▶ c.eq = items == 3 ? hamt_xs_eq_cb : NULL;
Branch: 1 branch
BranchTrueFalseTotal
c.eq = items == 3 ? hamt_xs_eq_cb : NULL; 13 293 306
271 306 ▶ c.ud = items == 3 ? (void *)ST(2) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
c.ud = items == 3 ? (void *)ST(2) : NULL; 13 293 306
272 306 c.visits = 0;
273 306 ENTER;
274 306 hamt_xs_hold_root(aTHX_ ra);
275 306 hamt_xs_hold_root(aTHX_ rb);
276 306 rc = hamt_node_equal(&c, ra, rb);
277 303 LEAVE;
278 303 ▶ ST(0) = boolSV(rc > 0);
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = boolSV(rc > 0); 266 37 303
279 303 XSRETURN(1);
280  
281 # Every key of both. For a key in both, the other map's value, or what the
282 # sub returns when one is given.
283 void
284 merge(self, other, ...)
285 SV *self
286 SV *other
287 PREINIT:
288 hamt_map *m, *o, *n;
289 hamt_merge_ctx c;
290 hamt_xs_sub res;
291 hamt_node *ra, *rb, *r;
292 HV *stash;
293 size_t count;
294 SV *obj;
295 CODE:
296 972 m = hamt_handle(aTHX_ self, &hamt_map_vtbl, "HAMT::Map::merge", "map");
297 972 o = hamt_handle(aTHX_ other, &hamt_map_vtbl, "HAMT::Map::merge", "map");
298 971 ▶ if (items > 3) croak("HAMT::Map::merge: too many arguments");
Branch: 1 branch
BranchTrueFalseTotal
if (items > 3) croak("HAMT::Map::merge: too many arguments"); 1 970 971
299 970 ▶ 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"); 488 482 970
300 969 ▶ if (GIMME_V == G_VOID) {
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_VOID) { 1 968 969
301 1 hamt_void_warn(aTHX_ "HAMT::Map::merge");
302 1 XSRETURN_EMPTY;
303 }
304 968 ra = m->root;
305 968 rb = o->root;
306 968 count = m->count;
307 968 stash = SvSTASH(SvRV(self));
308 968 ▶ res.code = items == 3 ? ST(2) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
res.code = items == 3 ? ST(2) : NULL; 487 481 968
309 968 res.err = NULL;
310 968 c.cx = HAMT_CX;
311 968 ▶ c.resolve = items == 3 ? hamt_xs_resolve_cb : NULL;
Branch: 1 branch
BranchTrueFalseTotal
c.resolve = items == 3 ? hamt_xs_resolve_cb : NULL; 487 481 968
312 968 c.ud = &res;
313  
314 968 ENTER;
315 968 hamt_xs_hold_root(aTHX_ ra);
316 968 hamt_xs_hold_root(aTHX_ rb);
317 968 r = hamt_map_merge(&c, ra, rb);
318 968 ▶ if (c.err == HAMT_ERR_ABORT) {
Branch: 1 branch
BranchTrueFalseTotal
if (c.err == HAMT_ERR_ABORT) { 7 961 968
319 /* The resolver died. Put its error back and die with it, object
320 * or string, exactly as it was. */
321 7 ▶ sv_setsv(ERRSV, res.err);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, res.err); 7 0 7
322 7 SvREFCNT_dec(res.err);
323 7 croak(NULL);
324 }
325 961 ▶ if (c.err) croak("HAMT::Map::merge: out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (c.err) croak("HAMT::Map::merge: out of memory"); 905 56 961
326  
327 56 ▶ if (r == ra) {
Branch: 1 branch
BranchTrueFalseTotal
if (r == ra) { 9 47 56
328 /* The other map added nothing and changed nothing. */
329 9 ▶ if (r) hamt_node_release(HAMT_CX, r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) hamt_node_release(HAMT_CX, r); 5 4 9
330 9 LEAVE;
331 9 XSRETURN(1);
332 }
333 47 ▶ if (r == rb && SvSTASH(SvRV(other)) == stash) {
Branch: 2 branches
BranchTrueFalseTotal
if (r == rb && SvSTASH(SvRV(other)) == stash) { 6 41 47
if (r == rb && SvSTASH(SvRV(other)) == stash) { 5 1 6
334 /* The result is exactly the other map, and it is of our class. */
335 5 ▶ # uncoverable branch false
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch false 5 0 5
336 5 # uncoverable condition right count:1
337 5 if (r) hamt_node_release(HAMT_CX, r);
338 5 LEAVE;
339 ST(0) = other;
340 42 ▶ XSRETURN(1);
Branch: 1 branch
BranchTrueFalseTotal
XSRETURN(1); 42 0 42
341 42 }
342 # uncoverable branch false
343 33 # uncoverable condition right count:1
344 33 if (r) SAVEDESTRUCTOR_X(hamt_xs_root_unref, r);
345 33 obj = hamt_handle_new(aTHX_ &hamt_map_vtbl, stash, r, count + c.added, &n,
346 "HAMT::Map::merge");
347 LEAVE;
348 ST(0) = obj;
349 XSRETURN(1);
350  
351 # The map with one key's value replaced by what the sub makes of it.
352 void
353 update(self, key, code)
354 SV *self
355 SV *key
356 SV *code
357 PREINIT:
358 hamt_map *m, *n;
359 hamt_key k;
360 hamt_node *root;
361 hamt_entry *e;
362 SV *obj, *keep, *res, *err;
363 int same;
364 CODE:
365 76 m = hamt_handle(aTHX_ self, &hamt_map_vtbl, "HAMT::Map::update", "map");
366 76 hamt_xs_need_code(aTHX_ code, "HAMT::Map::update");
367 75 ▶ if (GIMME_V == G_VOID) {
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_VOID) { 1 74 75
368 1 hamt_void_warn(aTHX_ "HAMT::Map::update");
369 1 XSRETURN_EMPTY;
370 }
371 74 hamt_key_from_sv(aTHX_ key, &k);
372 /* The sub may change or free the scalar the key was read from, so the
373 * bytes are kept in a scalar of this call's own. */
374 74 keep = sv_2mortal(newSVpvn(k.p, k.len));
375 74 k.p = SvPVX(keep);
376 74 root = m->root;
377 74 obj = hamt_handle_new(aTHX_ &hamt_map_vtbl, SvSTASH(SvRV(self)), root,
378 m->count, &n, "HAMT::Map::update");
379 71 e = hamt_map_find(root, k.p, (size_t)k.len, k.utf8, k.hash);
380 {
381 SV *arg;
382 71 ENTER;
383 71 SAVETMPS;
384 71 hamt_xs_hold_root(aTHX_ root);
385 71 arg = hamt_xs_val_sv(aTHX_ e);
386 71 hamt_xs_call_enter(aTHX);
387 71 err = hamt_xs_call_err(aTHX_ hamt_xs_call(aTHX_ code, &arg, 1, &res), 0);
388 71 hamt_xs_call_leave(aTHX);
389 /* Stored before the frame goes: what the sub returned is a
390 * temporary of that frame. */
391 /* By value, not by address: a sub's return is always a copy. */
392 71 ▶ same = err || (e && e->val && hamt_sv_eq_cx(HAMT_CX, res, (SV *)e->val)); /* # uncoverable condition right count:3 */
Branch: 4 branches
BranchTrueFalseTotal
same = err || (e && e->val && hamt_sv_eq_cx(HAMT_CX, res, (SV *)e->val)); /* # uncoverable condition right count:3 */ 46 25 71
same = err || (e && e->val && hamt_sv_eq_cx(HAMT_CX, res, (SV *)e->val)); /* # uncoverable condition right count:3 */ 40 6 46
same = err || (e && e->val && hamt_sv_eq_cx(HAMT_CX, res, (SV *)e->val)); /* # uncoverable condition right count:3 */ 40 0 40
same = err || (e && e->val && hamt_sv_eq_cx(HAMT_CX, res, (SV *)e->val)); /* # uncoverable condition right count:3 */ 4 36 40
393 71 ▶ 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"); 42 29 71
394 51 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 51 0 51
395 51 LEAVE;
396 }
397 51 ▶ if (err) hamt_xs_rethrow(aTHX_ err);
Branch: 1 branch
BranchTrueFalseTotal
if (err) hamt_xs_rethrow(aTHX_ err); 25 26 51
398 26 ▶ if (!same) ST(0) = obj;
Branch: 1 branch
BranchTrueFalseTotal
if (!same) ST(0) = obj; 22 4 26
399 26 XSRETURN(1);
400  
401 # Calls the sub for every key that is in one map and not the other, or in
402 # both with values that are not equal. Returns how many that was.
403 void
404 diff(self, other, code)
405 SV *self
406 SV *other
407 SV *code
408 PREINIT:
409 hamt_map *m, *o;
410 hamt_diff_ctx c;
411 hamt_xs_sub sub;
412 hamt_node *ra, *rb;
413 CODE:
414 1252 m = hamt_handle(aTHX_ self, &hamt_map_vtbl, "HAMT::Map::diff", "map");
415 1252 o = hamt_handle(aTHX_ other, &hamt_map_vtbl, "HAMT::Map::diff", "map");
416 1251 hamt_xs_need_code(aTHX_ code, "HAMT::Map::diff");
417 1250 ra = m->root;
418 1250 rb = o->root;
419 1250 c.cx = HAMT_CX;
420 1250 sub.code = code;
421 1250 sub.err = NULL;
422 1250 c.fn = hamt_xs_diff_cb;
423 1250 c.ud = &sub;
424 1250 c.eq.cx = HAMT_CX;
425 1250 c.eq.eq = NULL;
426 1250 c.eq.ud = NULL;
427 1250 ENTER;
428 1250 hamt_xs_hold_root(aTHX_ ra);
429 1250 hamt_xs_hold_root(aTHX_ rb);
430 1250 hamt_xs_walk_enter(aTHX);
431 1250 hamt_map_diff(&c, ra, rb);
432 1250 hamt_xs_call_leave(aTHX);
433 1250 LEAVE;
434 1250 ▶ if (sub.err) hamt_xs_rethrow(aTHX_ sub.err);
Branch: 1 branch
BranchTrueFalseTotal
if (sub.err) hamt_xs_rethrow(aTHX_ sub.err); 3 1247 1250
435 1247 ST(0) = sv_2mortal(newSVuv((UV)c.reported));
436 1247 XSRETURN(1);
437  
438 # The keys of the map, as a set. The set shares nothing with the map: a
439 # map's entries carry values and a set's do not.
440 void
441 key_set(self)
442 SV *self
443 PREINIT:
444 hamt_map *m, *n;
445 hamt_node *root;
446 hamt_iter it;
447 hamt_entry *e;
448 SV *obj;
449 CODE:
450 426 m = hamt_handle(aTHX_ self, &hamt_map_vtbl, "HAMT::Map::key_set", "map");
451 426 root = m->root;
452 426 obj = hamt_handle_new(aTHX_ &hamt_set_vtbl, gv_stashpvs("HAMT::Set", GV_ADD),
453 NULL, 0, &n, "HAMT::Map::key_set");
454 425 hamt_iter_init(&it, root);
455 66235 ▶ while ((e = hamt_iter_next(&it))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((e = hamt_iter_next(&it))) { 66228 7 66235
456 hamt_key k;
457 66228 k.p = e->key; k.len = (STRLEN)e->klen; k.utf8 = e->utf8; k.hash = e->hash;
458 66228 hamt_xs_put_key(aTHX_ n, &k, NULL, "HAMT::Map::key_set");
459 }
460 7 ST(0) = obj;
461 7 XSRETURN(1);
462  
463 # A transient opened on this map: it starts out holding what the map holds,
464 # and the map never sees what is done to it.
465 void
466 transient(self)
467 SV *self
468 PREINIT:
469 hamt_map *m;
470 CODE:
471 569 m = hamt_handle(aTHX_ self, &hamt_map_vtbl, "HAMT::Map::transient", "map");
472 1136 ST(0) = hamt_txn_new(aTHX_ &hamt_map_txn_vtbl, "HAMT::Map::Transient",
473 569 SvSTASH(SvRV(self)), m->root, m->count,
474 "HAMT::Map::transient");
475 567 XSRETURN(1);
476  
477 # For the tests: 0, or the number of the rule in hamt_check.h that the trie
478 # breaks.
479 void
480 _check(self)
481 SV *self
482 PREINIT:
483 hamt_map *m;
484 CODE:
485 25436 m = hamt_handle(aTHX_ self, &hamt_map_vtbl, "HAMT::Map::_check", "map");
486 25427 ST(0) = sv_2mortal(newSViv((IV)hamt_map_check(m->root, m->count)));
487 25427 XSRETURN(1);
488  
489 # A handle is not carried into a new thread: its nodes hold this
490 # interpreter's scalars.
491 void
492 CLONE_SKIP(...)
493 CODE:
494 0 ST(0) = &PL_sv_yes;
495 0 XSRETURN(1);
« xs/set.xs ↑ Summary xs/map_transient.xs »