R.xs

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