xs/map.xs

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