xs/set.xs

filter: Statementstmt 97.5% 117 / 120 Branchbran 91.0% 72 / 78 (1 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 94.9% 189 / 198 (1 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/hamt/hamt_ops.h ↑ Summary xs/map_transient.xs »
1 # HAMT::Set. The trie is the map's with entries that carry no value, so
2 # membership, add and remove are the map's lookup, put and delete; what is a
3 # set's own is the algebra.
4  
5 MODULE = HAMT PACKAGE = HAMT::Set
6  
7 # THAW is the serialisers' name for it: (klass, serialiser, members...).
8 void
9 new(klass, ...)
10 SV *klass
11 ALIAS:
12 THAW = 1
13 PREINIT:
14 hamt_map *m;
15 SV *obj;
16 I32 i;
17 static const char *const name[2] = { "HAMT::Set::new", "HAMT::Set::THAW" };
18 CODE:
19 3301 ▶ if (items < 1 + ix) croak_xs_usage(cv, "klass, serialiser, ...");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1 + ix) croak_xs_usage(cv, "klass, serialiser, ..."); 1 3300 3301
20 3300 obj = hamt_handle_new(aTHX_ &hamt_set_vtbl, hamt_class_stash(aTHX_ klass),
21 3300 NULL, 0, &m, name[ix]);
22 462743 ▶ for (i = 1 + ix; i < items; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1 + ix; i < items; i++) 459864 2879 462743
23 459864 hamt_xs_put(aTHX_ m, ST(i), NULL, name[ix]);
24 2879 ST(0) = obj;
25 2879 XSRETURN(1);
26  
27 void
28 add(self, ...)
29 SV *self
30 PREINIT:
31 hamt_map *m, *n;
32 hamt_node *was;
33 SV *obj;
34 I32 i;
35 CODE:
36 1055 m = hamt_handle(aTHX_ self, &hamt_set_vtbl, "HAMT::Set::add", "set");
37 1055 was = m->root;
38 1055 ▶ if (GIMME_V == G_VOID) {
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_VOID) { 3 1052 1055
39 3 hamt_void_warn(aTHX_ "HAMT::Set::add");
40 3 XSRETURN_EMPTY;
41 }
42 1052 obj = hamt_handle_new(aTHX_ &hamt_set_vtbl, SvSTASH(SvRV(self)),
43 m->root, m->count, &n, "HAMT::Set::add");
44 32024 ▶ for (i = 1; i < items; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) 31293 731 32024
45 31293 hamt_xs_put(aTHX_ n, ST(i), NULL, "HAMT::Set::add");
46 731 ▶ if (n->root != was) ST(0) = obj;
Branch: 1 branch
BranchTrueFalseTotal
if (n->root != was) ST(0) = obj; 469 262 731
47 731 XSRETURN(1);
48  
49 void
50 remove(self, ...)
51 SV *self
52 PREINIT:
53 hamt_map *m, *n;
54 hamt_node *was;
55 SV *obj;
56 I32 i;
57 CODE:
58 829 m = hamt_handle(aTHX_ self, &hamt_set_vtbl, "HAMT::Set::remove", "set");
59 829 was = m->root;
60 829 ▶ if (GIMME_V == G_VOID) {
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_VOID) { 1 828 829
61 1 hamt_void_warn(aTHX_ "HAMT::Set::remove");
62 1 XSRETURN_EMPTY;
63 }
64 828 obj = hamt_handle_new(aTHX_ &hamt_set_vtbl, SvSTASH(SvRV(self)),
65 m->root, m->count, &n, "HAMT::Set::remove");
66 9378 ▶ for (i = 1; i < items; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) 8653 725 9378
67 8653 hamt_xs_del(aTHX_ n, ST(i), "HAMT::Set::remove");
68 725 ▶ if (n->root != was) ST(0) = obj;
Branch: 1 branch
BranchTrueFalseTotal
if (n->root != was) ST(0) = obj; 257 468 725
69 725 XSRETURN(1);
70  
71 void
72 has(self, member)
73 SV *self
74 SV *member
75 PREINIT:
76 hamt_map *m;
77 hamt_key k;
78 CODE:
79 142 m = hamt_handle(aTHX_ self, &hamt_set_vtbl, "HAMT::Set::has", "set");
80 141 hamt_key_from_sv(aTHX_ member, &k);
81 141 ▶ 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); 75 66 141
82 141 XSRETURN(1);
83  
84 void
85 count(self)
86 SV *self
87 PREINIT:
88 hamt_map *m;
89 CODE:
90 6660 m = hamt_handle(aTHX_ self, &hamt_set_vtbl, "HAMT::Set::count", "set");
91 6657 ST(0) = sv_2mortal(newSVuv((UV)m->count));
92 6657 XSRETURN(1);
93  
94 # FREEZE is the serialisers' name for it: a set is carried as its members.
95 void
96 members(self, ...)
97 SV *self
98 ALIAS:
99 FREEZE = 1
100 PREINIT:
101 hamt_map *m;
102 hamt_iter it;
103 hamt_entry *e;
104 static const char *const name[2] = { "HAMT::Set::members", "HAMT::Set::FREEZE" };
105 PPCODE:
106 8273 ▶ 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 8271 8273
if (items != 1 + ix) croak_xs_usage(cv, ix ? "self, serialiser" : "self"); 1 1 2
107 8271 m = hamt_handle(aTHX_ self, &hamt_set_vtbl, name[ix], "set");
108 8271 ▶ if (GIMME_V == G_VOID) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_VOID) XSRETURN_EMPTY; 1 8270 8271
109 8270 ▶ if (GIMME_V == G_SCALAR) {
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_SCALAR) { 2 8268 8270
110 2 PUSHs(sv_2mortal(newSVuv((UV)m->count)));
111 2 XSRETURN(1);
112 }
113 8268 ▶ # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true 0 8268 8268
114 0 # uncoverable condition left count:1
115 8268 ▶ if (m->count > ((size_t)-1 >> 2))
Branch: 2 branches
BranchTrueFalseTotal
if (m->count > ((size_t)-1 >> 2)) 8268 0 8268
if (m->count > ((size_t)-1 >> 2)) 0 8268 8268
116 8268 croak("%s: too many members for one list", name[ix]); /* # uncoverable statement */
117 1042724 ▶ EXTEND(SP, (SSize_t)m->count);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, (SSize_t)m->count); 1034456 8268 1042724
118 1034456 hamt_iter_init(&it, m->root);
119 while ((e = hamt_iter_next(&it)))
120 PUSHs(sv_2mortal(hamt_key_to_sv(aTHX_ e)));
121  
122 void
123 each(self, code)
124 SV *self
125 SV *code
126 PREINIT:
127 hamt_map *m;
128 hamt_node *root;
129 hamt_iter it;
130 hamt_entry *e;
131 9 SV *keysv = NULL, *err = NULL;
132 int how;
133 CODE:
134 9 m = hamt_handle(aTHX_ self, &hamt_set_vtbl, "HAMT::Set::each", "set");
135 9 hamt_xs_need_code(aTHX_ code, "HAMT::Set::each");
136 8 root = m->root;
137 8 ENTER;
138 8 SAVETMPS;
139 8 hamt_xs_hold_root(aTHX_ root);
140 8 hamt_xs_walk_enter(aTHX);
141 8 hamt_iter_init(&it, root);
142 1216 ▶ while ((e = hamt_iter_next(&it))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((e = hamt_iter_next(&it))) { 1211 5 1216
143 1211 keysv = hamt_xs_scratch(aTHX_ keysv);
144 1211 sv_setpvn(keysv, e->key, (STRLEN)e->klen);
145 1211 ▶ if (e->utf8) SvUTF8_on(keysv); else SvUTF8_off(keysv); /* # uncoverable condition left count:1 */
Branch: 1 branch
BranchTrueFalseTotal
if (e->utf8) SvUTF8_on(keysv); else SvUTF8_off(keysv); /* # uncoverable condition left count:1 */ 0 1211 1211
146 1211 how = hamt_xs_call(aTHX_ code, &keysv, 1, NULL);
147 1211 ▶ 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 */ 1210 1 1211
if (how == HAMT_XS_LAST || (err = hamt_xs_call_err(aTHX_ how, 1))) break; /* # uncoverable branch true count:1 class:ignore_covered_err */ 1208 2 1210
148 }
149 8 hamt_xs_call_leave(aTHX);
150 8 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 8 0 8
151 8 LEAVE;
152 8 ▶ if (err) hamt_xs_rethrow(aTHX_ err);
Branch: 1 branch
BranchTrueFalseTotal
if (err) hamt_xs_rethrow(aTHX_ err); 2 6 8
153 6 XSRETURN(1);
154  
155 # union, intersect and difference share a body. None of them runs Perl, so
156 # nothing needs holding across the walk; each returns the receiver itself
157 # when the result is the receiver, and union returns the other set when the
158 # result is that and it is of the receiver's class.
159 void
160 union(self, other)
161 SV *self
162 SV *other
163 ALIAS:
164 intersect = 1
165 difference = 2
166 PREINIT:
167 hamt_map *m, *o, *n;
168 hamt_node *r;
169 5210 size_t count = 0;
170 5210 int err = 0;
171 HV *stash;
172 static const char *const name[3] = {
173 "HAMT::Set::union", "HAMT::Set::intersect", "HAMT::Set::difference"
174 };
175 CODE:
176 5210 m = hamt_handle(aTHX_ self, &hamt_set_vtbl, name[ix], "set");
177 5210 o = hamt_handle(aTHX_ other, &hamt_set_vtbl, name[ix], "set");
178 5207 ▶ if (GIMME_V == G_VOID) {
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_VOID) { 4 5203 5207
179 4 hamt_void_warn(aTHX_ name[ix]);
180 4 XSRETURN_EMPTY;
181 }
182 5203 stash = SvSTASH(SvRV(self));
183 5203 ▶ if (ix == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ix == 0) { 1786 3417 5203
184 hamt_merge_ctx c;
185 1786 c.cx = HAMT_CX;
186 1786 c.resolve = NULL;
187 1786 c.ud = NULL;
188 1786 r = hamt_map_merge(&c, m->root, o->root);
189 1786 err = c.err;
190 1786 count = m->count + c.added;
191 }
192 else {
193 hamt_set_ctx c;
194 3417 c.cx = HAMT_CX;
195 1703 r = ix == 1 ? hamt_set_intersect(&c, m->root, o->root, m->count, o->count, &count)
196 3417 ▶ : hamt_set_subtract(&c, m->root, o->root, m->count, o->count, &count);
Branch: 1 branch
BranchTrueFalseTotal
: hamt_set_subtract(&c, m->root, o->root, m->count, o->count, &count); 1703 1714 3417
197 3417 err = c.err;
198 }
199 5203 ▶ if (err) croak("%s: out of memory", name[ix]);
Branch: 1 branch
BranchTrueFalseTotal
if (err) croak("%s: out of memory", name[ix]); 164 5039 5203
200  
201 5039 ▶ if (r == m->root) {
Branch: 1 branch
BranchTrueFalseTotal
if (r == m->root) { 1202 3837 5039
202 1202 ▶ if (r) hamt_node_release(HAMT_CX, r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) hamt_node_release(HAMT_CX, r); 1196 6 1202
203 1202 XSRETURN(1);
204 }
205 3837 ▶ if (ix == 0 && r == o->root && SvSTASH(SvRV(other)) == stash) {
Branch: 3 branches
BranchTrueFalseTotal
if (ix == 0 && r == o->root && SvSTASH(SvRV(other)) == stash) { 1416 2421 3837
if (ix == 0 && r == o->root && SvSTASH(SvRV(other)) == stash) { 195 1221 1416
if (ix == 0 && r == o->root && SvSTASH(SvRV(other)) == stash) { 194 1 195
206 194 ▶ # uncoverable branch false
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch false 194 0 194
207 194 # uncoverable condition right count:1
208 194 if (r) hamt_node_release(HAMT_CX, r);
209 ST(0) = other;
210 XSRETURN(1);
211 }
212 3643 /* The new handle takes a reference of its own; the one the walk
213 3643 ▶ * returned is released whether or not making the handle works. */
Branch: 1 branch
BranchTrueFalseTotal
* returned is released whether or not making the handle works. */ 2710 933 3643
214 3643 ENTER;
215 3635 if (r) SAVEDESTRUCTOR_X(hamt_xs_root_unref, r);
216 3635 ST(0) = hamt_handle_new(aTHX_ &hamt_set_vtbl, stash, r, count, &n, name[ix]);
217 LEAVE;
218 XSRETURN(1);
219  
220 void
221 is_subset(self, other)
222 SV *self
223 SV *other
224 ALIAS:
225 is_disjoint = 1
226 PREINIT:
227 hamt_map *m, *o;
228 size_t visits;
229 static const char *const name[2] = { "HAMT::Set::is_subset", "HAMT::Set::is_disjoint" };
230 CODE:
231 3322 m = hamt_handle(aTHX_ self, &hamt_set_vtbl, name[ix], "set");
232 3322 o = hamt_handle(aTHX_ other, &hamt_set_vtbl, name[ix], "set");
233 3320 ▶ ST(0) = boolSV(ix == 0
Branch: 2 branches
BranchTrueFalseTotal
ST(0) = boolSV(ix == 0 1660 1660 3320
ST(0) = boolSV(ix == 0 943 2377 3320
234 ? hamt_set_subset(m->root, o->root, m->count, o->count, &visits)
235 : hamt_set_disjoint(m->root, o->root, &visits));
236 3320 XSRETURN(1);
237  
238 void
239 equals(self, other)
240 SV *self
241 SV *other
242 PREINIT:
243 hamt_map *m, *o;
244 hamt_eq_ctx c;
245 CODE:
246 1676 m = hamt_handle(aTHX_ self, &hamt_set_vtbl, "HAMT::Set::equals", "set");
247 1676 o = hamt_handle_try(aTHX_ other, &hamt_set_vtbl);
248 1676 ▶ if (!o || o->count != m->count) XSRETURN_NO;
Branch: 2 branches
BranchTrueFalseTotal
if (!o || o->count != m->count) XSRETURN_NO; 1672 4 1676
if (!o || o->count != m->count) XSRETURN_NO; 1429 243 1672
249 243 c.cx = HAMT_CX;
250 243 c.eq = NULL;
251 243 c.ud = NULL;
252 243 c.visits = 0;
253 243 ▶ ST(0) = boolSV(hamt_node_equal(&c, m->root, o->root) > 0);
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = boolSV(hamt_node_equal(&c, m->root, o->root) > 0); 190 53 243
254 243 XSRETURN(1);
255  
256 void
257 transient(self)
258 SV *self
259 PREINIT:
260 hamt_map *m;
261 CODE:
262 201 m = hamt_handle(aTHX_ self, &hamt_set_vtbl, "HAMT::Set::transient", "set");
263 401 ST(0) = hamt_txn_new(aTHX_ &hamt_set_txn_vtbl, "HAMT::Set::Transient",
264 201 SvSTASH(SvRV(self)), m->root, m->count,
265 "HAMT::Set::transient");
266 200 XSRETURN(1);
267  
268 void
269 _check(self)
270 SV *self
271 PREINIT:
272 hamt_map *m;
273 CODE:
274 6609 m = hamt_handle(aTHX_ self, &hamt_set_vtbl, "HAMT::Set::_check", "set");
275 6609 ST(0) = sv_2mortal(newSViv((IV)hamt_map_check(m->root, m->count)));
276 6609 XSRETURN(1);
277  
278 void
279 CLONE_SKIP(...)
280 CODE:
281 0 ST(0) = &PL_sv_yes;
282 0 XSRETURN(1);
« include/hamt/hamt_ops.h ↑ Summary xs/map_transient.xs »