R.xs

filter: Statementstmt 84.4% 184 / 218 Branchbran 65.0% 78 / 120 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 77.5% 262 / 338 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« rtree.c ↑ Summary
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5 #include "salad/rtree.h"
6 #include <stdlib.h>
7 #include <stdint.h>
8 #include <stdio.h>
9 #include <stdbool.h>
10 #include <string.h>
11  
12 #define XS_KEY "_xs"
13 #define OPTS_KEY "constructor.ro.opts"
14  
15 const uint32_t extent_size = 1024 * 8;
16  
17 struct rtree_w_ecount {
18 int count;
19 struct rtree tree[1];
20 };
21  
22  
23 static void *
24 784 extent_alloc(void *ctx)
25 {
26 784 void *mem = malloc(extent_size);
27 784 ▶ if (!mem)
Branch: 1 branch
BranchTrueFalseTotal
if (!mem) 0 784 784
28 0 croak("Can not allocate %d bytes for R extent", extent_size);
29 784 int *p_extent_count = (int *)ctx;
30 784 ++*p_extent_count;
31 784 return mem;
32 }
33  
34 static void
35 784 extent_free(void *ctx, void *page)
36 {
37 784 int *p_extent_count = (int *)ctx;
38 784 --*p_extent_count;
39 784 free(page);
40 784 }
41  
42  
43 inline static struct rtree_w_ecount *
44 100239 self_fetch_tree(pTHX_ SV *self)
45 {
46 100239 self = SvRV(self);
47 100239 SV **stree = hv_fetchs((HV *)self, XS_KEY, 0);
48 100239 ▶ if (*stree)
Branch: 1 branch
BranchTrueFalseTotal
if (*stree) 100239 0 100239
49 100239 return (struct rtree_w_ecount *)SvIV(*stree);
50 0 return NULL;
51 }
52  
53 inline static int
54 100220 _fill_rect(pTHX_ struct rtree *t, struct rtree_rect *r, SV * pref, int check)
55 {
56 100220 ▶ if (!((SvOK(pref)) && (SvROK(pref)) && (SvTYPE(SvRV(pref)) == SVt_PVAV))) {
Branch: 3 branches
BranchTrueFalseTotal
if (!((SvOK(pref)) && (SvROK(pref)) && (SvTYPE(SvRV(pref)) == SVt_PVAV))) { 100219 1 100220
if (!((SvOK(pref)) && (SvROK(pref)) && (SvTYPE(SvRV(pref)) == SVt_PVAV))) { 100219 0 100219
if (!((SvOK(pref)) && (SvROK(pref)) && (SvTYPE(SvRV(pref)) == SVt_PVAV))) { 1 100218 100219
57 2 ▶ if (check)
Branch: 1 branch
BranchTrueFalseTotal
if (check) 2 0 2
58 2 return 0;
59 0 croak("$point or $rect must be an ArrayRef[x0[,x1],y0[,y1]...] object");
60 }
61  
62 100218 AV *p = (AV *)SvRV(pref);
63  
64 100218 ▶ if (av_len(p) + 1 == t->dimension) { /* point */
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(p) + 1 == t->dimension) { /* point */ 100108 110 100218
65 300324 ▶ for (unsigned i = 0; i < t->dimension; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned i = 0; i < t->dimension; i++) { 200216 100108 300324
66 200216 SV **item = av_fetch(p, i, 0);
67  
68 200216 ▶ if (!(item && looks_like_number(*item))) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(item && looks_like_number(*item))) { 200216 0 200216
if (!(item && looks_like_number(*item))) { 0 200216 200216
69 0 ▶ if (check)
Branch: 1 branch
BranchTrueFalseTotal
if (check) 0 0 0
70 0 return 0;
71 0 croak("'%s' is not a number (check point[%d])",
72 *item,
73 i
74 );
75 }
76 200216 r->coords[i * 2 + 0] = r->coords[i * 2 + 1] = SvNV(*item);
77 }
78  
79 110 ▶ } else if (av_len(p) + 1 == t->dimension * 2) { /* box */
Branch: 1 branch
BranchTrueFalseTotal
} else if (av_len(p) + 1 == t->dimension * 2) { /* box */ 108 2 110
80 108 unsigned no = 0;
81  
82 324 ▶ for (unsigned i = 0; i < t->dimension; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned i = 0; i < t->dimension; i++) { 216 108 324
83  
84 216 SV **item = av_fetch(p, no, 0);
85  
86  
87 216 ▶ if (!(item && looks_like_number(*item))) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(item && looks_like_number(*item))) { 216 0 216
if (!(item && looks_like_number(*item))) { 0 216 216
88 0 ▶ if (check)
Branch: 1 branch
BranchTrueFalseTotal
if (check) 0 0 0
89 0 return 0;
90 0 croak("'%s' is not a number (check rect[%d])",
91 *item,
92 no
93 );
94 }
95 216 r->coords[i * 2] = SvNV(*item);
96 216 no++;
97 }
98  
99 324 ▶ for (unsigned i = 0; i < t->dimension; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned i = 0; i < t->dimension; i++) { 216 108 324
100  
101 216 SV **item = av_fetch(p, no, 0);
102  
103  
104 216 ▶ if (!(item && looks_like_number(*item))) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(item && looks_like_number(*item))) { 216 0 216
if (!(item && looks_like_number(*item))) { 0 216 216
105 0 ▶ if (check)
Branch: 1 branch
BranchTrueFalseTotal
if (check) 0 0 0
106 0 return 0;
107 0 croak("'%s' is not a number (check rect[%d])",
108 *item,
109 no
110 );
111 }
112 216 r->coords[i * 2 + 1] = SvNV(*item);
113 216 no++;
114 }
115 108 rtree_rect_normalize(r, t->dimension);
116 } else {
117 2 ▶ if (check)
Branch: 1 branch
BranchTrueFalseTotal
if (check) 1 1 2
118 1 return 0;
119 1 croak(
120 "I can't understand if the array is point "
121 "or rect (asize=%d, must be %d or %d)",
122 av_len(p) + 1,
123 t->dimension,
124 t->dimension * 2
125 );
126 }
127  
128 /*
129 printf("[%5.2f %5.2f %5.2f %5.2f] - %d\n",
130 r->coords[0],
131 r->coords[1],
132 r->coords[2],
133 r->coords[3],
134 av_len(p) + 1);
135 */
136 100216 return 1;
137 }
138  
139 MODULE = DR::R PACKAGE = DR::R
140 PROTOTYPES: DISABLE
141 SV * new(SV *class, ...)
142 CODE:
143 14 HV * opts = (HV *)sv_2mortal((SV *)newHV());
144 const char *classname;
145  
146 14 ▶ if (sv_isobject(class)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_isobject(class)) { 1 13 14
147 1 classname = sv_reftype(SvRV(class), 1);
148 1 SV **popts = hv_fetchs((HV *)SvRV(class), OPTS_KEY, 0);
149 1 ▶ if (popts) {
Branch: 1 branch
BranchTrueFalseTotal
if (popts) { 1 0 1
150 1 hv_iterinit((HV *)SvRV(*popts));
151 3 for (;;) {
152 4 HE *iter = hv_iternext((HV *)SvRV(*popts));
153 4 ▶ if (!iter)
Branch: 1 branch
BranchTrueFalseTotal
if (!iter) 1 3 4
154 1 break;
155 3 SV *k = hv_iterkeysv(iter);
156 3 SV *v = HeVAL(iter);
157 3 hv_store_ent(opts, k, newSVsv(v), 0);
158 }
159 }
160 } else {
161 13 classname = SvPV_nolen(class);
162 }
163  
164 14 HV *self = newHV();
165 14 SV *bself = newRV_noinc((SV *)self);
166 14 sv_bless(bself, gv_stashpv(classname, TRUE));
167 20 ▶ for (unsigned i = 1; i < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned i = 1; i < items; i += 2) { 6 14 20
168 6 ▶ if (i < items - 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (i < items - 1) { 6 0 6
169 6 hv_store_ent(opts, ST(i), newSVsv(ST(i + 1)), 0);
170 } else {
171 0 hv_store_ent(opts, ST(i), newSV(0), 0);
172 }
173 }
174 14 hv_stores(self, OPTS_KEY, newRV((SV *)opts));
175  
176 14 int d = 2;
177 14 SV ** ds = hv_fetchs(opts, "dimension", 0);
178 14 ▶ if (ds) {
Branch: 1 branch
BranchTrueFalseTotal
if (ds) { 5 9 14
179 5 d = SvIV(*ds);
180  
181 5 ▶ if (d > RTREE_MAX_DIMENSION) {
Branch: 1 branch
BranchTrueFalseTotal
if (d > RTREE_MAX_DIMENSION) { 0 5 5
182 0 croak("Too high dimension value: %d (max %d)",
183 d,
184 RTREE_MAX_DIMENSION);
185 5 ▶ } else if (d < 1) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (d < 1) { 0 5 5
186 0 croak("Too low dimension value: %d (min 1)", d);
187 }
188 }
189 14 hv_stores(opts, "dimension", newSViv(d));
190  
191 14 unsigned dist_type = RTREE_EUCLID;
192 14 SV ** dt = hv_fetchs(opts, "dist_type", 0);
193 14 ▶ if (dt) {
Branch: 1 branch
BranchTrueFalseTotal
if (dt) { 2 12 14
194 STRLEN len;
195 2 const char *p = SvPV(*dt, len);
196 2 ▶ if (strncmp(p, "EUCLID", len) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strncmp(p, "EUCLID", len) == 0) { 0 2 2
197 0 dist_type = RTREE_EUCLID;
198 2 ▶ } else if (strncmp(p, "MANHATTAN", len) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strncmp(p, "MANHATTAN", len) == 0) { 2 0 2
199 2 dist_type = RTREE_MANHATTAN;
200 } else {
201 0 croak("dist_type can be in ('EUCLID', 'MANHATTAN')");
202 }
203 } else {
204 12 hv_stores(opts, "dist_type", newSVpvs("EUCLID"));
205 }
206  
207  
208 14 struct rtree_w_ecount *t = malloc(sizeof(struct rtree_w_ecount));
209 14 t->count = 0;
210 14 rtree_init(t->tree,
211 d,
212 extent_size,
213 extent_alloc,
214 extent_free,
215 14 &t->count,
216 dist_type
217 );
218 14 hv_stores(self, XS_KEY, newSViv((IV)t));
219 14 RETVAL = bself;
220 OUTPUT:
221 RETVAL
222  
223 void DESTROY(SV *self)
224 CODE:
225 /* warn("DESTRUCTOR"); */
226 14 struct rtree_w_ecount *t = self_fetch_tree(aTHX_ self);
227 14 ▶ if (t) {
Branch: 1 branch
BranchTrueFalseTotal
if (t) { 14 0 14
228  
229 struct rtree_rect rect;
230 struct rtree_iterator iterator;
231  
232 14 rtree_iterator_init(&iterator);
233 14 memset(&rect, 0, sizeof(rect));
234  
235 14 ▶ if (rtree_search(t->tree, &rect, SOP_ALL, &iterator)) {
Branch: 1 branch
BranchTrueFalseTotal
if (rtree_search(t->tree, &rect, SOP_ALL, &iterator)) { 6 8 14
236 6 for (SV *item = rtree_iterator_next(&iterator);
237 100210 ▶ item;
Branch: 1 branch
BranchTrueFalseTotal
item; 100204 6 100210
238 100204 item = rtree_iterator_next(&iterator)) {
239  
240 100204 SvREFCNT_dec(item);
241 }
242 }
243  
244 14 rtree_iterator_destroy(&iterator);
245 14 rtree_destroy(t->tree);
246 14 free(t);
247  
248 14 self = SvRV(self);
249 14 hv_delete((HV *)self, XS_KEY, sizeof(XS_KEY), 0);
250 }
251  
252 SV * insert(SV *self, SV *point, SV *object)
253 CODE:
254 100207 ▶ if (!SvOK(object))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(object)) 0 100207 100207
255 0 croak("inserted object must be defined");
256  
257 100207 struct rtree_w_ecount *t = self_fetch_tree(aTHX_ self);
258 100207 ▶ if (!t)
Branch: 1 branch
BranchTrueFalseTotal
if (!t) 0 100207 100207
259 0 croak("Object has already been destroyed");
260  
261 struct rtree_rect rect;
262 100207 _fill_rect(aTHX_ t->tree, &rect, point, 0);
263  
264  
265 100206 SV *item = newSVsv(object);
266 100206 rtree_insert(t->tree, &rect, item);
267  
268 100206 RETVAL = newSViv((IV)item);
269 OUTPUT:
270 RETVAL
271  
272  
273  
274 SV * dimension(SV *self)
275 CODE:
276 5 struct rtree_w_ecount *tree = self_fetch_tree(aTHX_ self);
277 5 ▶ if (tree)
Branch: 1 branch
BranchTrueFalseTotal
if (tree) 5 0 5
278 5 RETVAL = newSViv(tree->tree->dimension);
279 else
280 0 RETVAL = newSV(0);
281 OUTPUT:
282 RETVAL
283  
284 SV * remove(SV *self, SV *point, SV *id)
285 CODE:
286 3 struct rtree_w_ecount *t = self_fetch_tree(aTHX_ self);
287 3 ▶ if (!t)
Branch: 1 branch
BranchTrueFalseTotal
if (!t) 0 3 3
288 0 croak("Object has already been destroyed");
289  
290 struct rtree_rect rect;
291 3 _fill_rect(aTHX_ t->tree, &rect, point, 0);
292  
293 3 ▶ if (!SvOK(id))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(id)) 0 3 3
294 0 croak("Usage: $r->remove($point, $id)");
295  
296 3 IV oid = SvIV(id);
297  
298 3 ▶ if (rtree_remove(t->tree, &rect, (void *)oid)) {
Branch: 1 branch
BranchTrueFalseTotal
if (rtree_remove(t->tree, &rect, (void *)oid)) { 2 1 3
299 2 SV *item = (SV *)oid;
300 2 RETVAL = item;
301 } else {
302 1 RETVAL = newSV(0);
303 }
304 OUTPUT:
305 RETVAL
306  
307 SV * foreach(SV *self, SV *type, SV *point, SV *cb)
308 CODE:
309 5 struct rtree_w_ecount *t = self_fetch_tree(aTHX_ self);
310 5 ▶ if (!t)
Branch: 1 branch
BranchTrueFalseTotal
if (!t) 0 5 5
311 0 croak("Object has already been destroyed");
312 5 int op = SOP_ALL;
313 STRLEN tlen;
314 const char *stype;
315  
316 struct rtree_iterator iterator;
317 5 rtree_iterator_init(&iterator);
318  
319 struct rtree_rect rect;
320 5 _fill_rect(aTHX_ t->tree, &rect, point, 0);
321  
322 5 ▶ if (SvOK(type)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(type)) { 5 0 5
323 5 stype = SvPV(type, tlen);
324 5 ▶ if (strncmp(stype, "EQ", tlen) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strncmp(stype, "EQ", tlen) == 0) { 0 5 5
325 0 op = SOP_EQUALS;
326 5 ▶ } else if (strncmp(stype, "NEIGHBOR", tlen) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strncmp(stype, "NEIGHBOR", tlen) == 0) { 1 4 5
327 1 op = SOP_NEIGHBOR;
328 4 ▶ } else if (strncmp(stype, "CONTAINS", tlen) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strncmp(stype, "CONTAINS", tlen) == 0) { 0 4 4
329 0 op = SOP_CONTAINS;
330 4 ▶ } else if (strncmp(stype, "CONTAINS!", tlen) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strncmp(stype, "CONTAINS!", tlen) == 0) { 0 4 4
331 0 op = SOP_STRICT_CONTAINS;
332 4 ▶ } else if (strncmp(stype, "OVERLAPS", tlen) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strncmp(stype, "OVERLAPS", tlen) == 0) { 2 2 4
333 2 op = SOP_OVERLAPS;
334 2 ▶ } else if (strncmp(stype, "BELONGS", tlen) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strncmp(stype, "BELONGS", tlen) == 0) { 1 1 2
335 1 op = SOP_BELONGS;
336 1 ▶ } else if (strncmp(stype, "BELONGS!", tlen) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strncmp(stype, "BELONGS!", tlen) == 0) { 1 0 1
337 1 op = SOP_STRICT_BELONGS;
338 0 ▶ } else if (strncmp(stype, "ALL", tlen) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strncmp(stype, "ALL", tlen) == 0) { 0 0 0
339 0 op = SOP_ALL;
340 } else {
341 0 croak("Unknown iterator type: '%.*s'",
342 tlen, stype);
343 }
344 }
345  
346 5 ▶ if (!(SvROK(cb) && SvTYPE(cb) | SVt_PVCV)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(SvROK(cb) && SvTYPE(cb) | SVt_PVCV)) { 0 5 5
347 0 croak("Usage: $t->foreach($type, $rect, sub { ... })");
348 }
349  
350 5 int no = 0;
351 5 ▶ if (rtree_search(t->tree, &rect, op, &iterator)) {
Branch: 1 branch
BranchTrueFalseTotal
if (rtree_search(t->tree, &rect, op, &iterator)) { 5 0 5
352 SV *item;
353 int count;
354 5 int cont = 1;
355 426 ▶ while(cont) {
Branch: 1 branch
BranchTrueFalseTotal
while(cont) { 424 2 426
356 424 item = rtree_iterator_next(&iterator);
357 424 ▶ if (!item)
Branch: 1 branch
BranchTrueFalseTotal
if (!item) 3 421 424
358 3 break;
359  
360 421 dSP;
361 421 ENTER;
362 421 SAVETMPS;
363  
364 421 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 421 421
365 421 ▶ EXTEND(SP, 3);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 3); 0 421 421
366 421 PUSHs(item);
367 421 PUSHs(sv_2mortal(newSViv((IV)item)));
368 421 PUSHs(sv_2mortal(newSViv(no)));
369 421 no++;
370 421 PUTBACK;
371  
372 421 count = call_sv(cb, G_ARRAY);
373 421 SPAGAIN;
374  
375 421 switch(count) {
376 419 case 0:
377 419 cont = 1;
378 419 break;
379 2 case 1:
380 2 cont = sv_2bool(POPs);
381 2 break;
382 0 default:
383 0 cont = 1;
384 0 break;
385 }
386  
387  
388  
389 421 PUTBACK;
390 421 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 421 0 421
391 421 LEAVE;
392 }
393 }
394 5 rtree_iterator_destroy(&iterator);
395 5 RETVAL = newSVsv(self);
396 OUTPUT:
397 RETVAL
398  
399 int is_point_or_rect(SV *self, SV *point)
400 CODE:
401 5 struct rtree_w_ecount *t = self_fetch_tree(aTHX_ self);
402 5 ▶ if (!t)
Branch: 1 branch
BranchTrueFalseTotal
if (!t) 0 5 5
403 0 croak("Object has already been destroyed");
404  
405 struct rtree_rect rect;
406 5 RETVAL = _fill_rect(aTHX_ t->tree, &rect, point, 1);
407 OUTPUT:
408 RETVAL
409  
410 SV * iterator_types(SV *class)
411 CODE:
412 static AV *list = NULL;
413  
414 6 ▶ if (!list) {
Branch: 1 branch
BranchTrueFalseTotal
if (!list) { 1 5 6
415 1 list = newAV();
416 1 av_push(list, newSVpvs("ALL"));
417 1 av_push(list, newSVpvs("BELONGS!"));
418 1 av_push(list, newSVpvs("BELONGS"));
419 1 av_push(list, newSVpvs("CONTAINS!"));
420 1 av_push(list, newSVpvs("CONTAINS"));
421 1 av_push(list, newSVpvs("EQ"));
422 1 av_push(list, newSVpvs("NEIGHBOR"));
423 1 av_push(list, newSVpvs("OVERLAPS"));
424 }
425  
426 6 RETVAL = newRV((SV *)list);
427 (void)class;
428 OUTPUT:
429 RETVAL
430  
431  
432 SV * _ping()
433 CODE:
434 1 SV *rv = newSVpvs("pong");
435 1 RETVAL = rv;
436 OUTPUT:
437 RETVAL
438  
« rtree.c ↑ Summary