xs/set.xs

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