XS.xs

filter: Statementstmt 53.6% 66 / 123 Branchbran 43.7% 14 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 51.6% 80 / 155 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« mba/varray.c ↑ Summary mba/diff.c »
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4  
5 #define NEED_newRV_noinc
6 #define NEED_sv_2pvbyte
7  
8 #include "ppport.h"
9  
10 #include <string.h>
11  
12 #include <mba/diff.h>
13 #include <mba/diff.c>
14 #include <mba/varray.h>
15 #include <mba/varray.c>
16 #include <mba/allocator.h>
17 #include <mba/allocator.c>
18 #include <mba/msgno.c>
19 #include <mba/suba.h>
20 #include <mba/suba.c>
21  
22 struct CTX {
23 IV dummy;
24 };
25  
26 inline
27 2 static IV lcs_DESTROY(SV *sv)
28 {
29 2 struct CTX *ctx = (struct CTX *)SvIVX(SvRV(sv));
30 2 ▶ if (ctx == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (ctx == NULL) 0 2 2
31 0 return 0;
32  
33 2 free(ctx);
34 2 return 1;
35 }
36  
37 inline
38 2 static SV *lcs_new(char *class)
39 {
40 2 struct CTX *ctx = malloc(sizeof *ctx);
41 struct LK *end;
42  
43 2 return sv_setref_pv(newSV(0),class,ctx);
44 }
45  
46 inline
47 static int
48 _cmp_str(const void *object1, const void *object2, void *context)
49 {
50 return strcmp(object1, object2);
51 }
52  
53 inline
54 static const void *
55 _av_idx(const void *a, int idx, void *context)
56 {
57 SV *line = *av_fetch((AV *)a, idx, 0);
58 STRLEN klen;
59 char *key = SvPVbyte(line, klen);
60  
61 return key;
62 }
63  
64 inline
65 static int
66 1292 _cmp_idx(const void *a, int idx1, const void *b, int idx2, void *context)
67 {
68  
69 1292 SV *s1 = *av_fetch((AV *)a, idx1, 0);
70 STRLEN len1;
71 1292 char *key1 = SvPVbyte(s1, len1);
72  
73 1292 ▶ SV *s2 = *av_fetch((AV *)b, idx2, 0);
Branch: 1 branch
BranchTrueFalseTotal
SV *s2 = *av_fetch((AV *)b, idx2, 0); 1288 4 1292
74 STRLEN len2;
75 1292 char *key2 = SvPVbyte(s2, len2);
76
77 1292 return strcmp(key1, key2);
78 }
79  
80  
81 MODULE = LCS::XS PACKAGE = LCS::XS PREFIX = lcs_
82 PROTOTYPES: DISABLED
83  
84 SV *lcs_new(char *class)
85  
86 IV lcs_DESTROY(SV *sv)
87  
88 SV *
89 lcs_posindex(obj,a)
90 SV *obj
91 AV *a
92
93 PREINIT:
94 0 struct CTX *ctx = (struct CTX *)SvIVX(SvRV(obj));
95 IV i;
96 IV l;
97 CODE:
98 0 l = av_len(a);
99
100 0 HV * pos_hash = newHV();
101  
102 0 ▶ for (i = 0; i <= l; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= l; ++i) { 0 0 0
103 0 SV *line = *av_fetch(a, i, 0);
104 STRLEN klen;
105 0 char *key = SvPVbyte(line, klen);
106
107 AV * matches;
108 0 SV **lines = hv_fetch(pos_hash, key, klen, 0);
109 0 ▶ if (lines != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (lines != NULL) { 0 0 0
110 0 matches = (AV *)SvRV(*lines);
111 }
112 else {
113 0 matches = newAV();
114 0 hv_store(pos_hash, key, klen, newRV_noinc((SV *)matches), 0);
115 }
116 0 av_push(matches, newSViv(i));
117 }
118  
119 0 RETVAL = (SV*)newRV_noinc((SV *)pos_hash);
120 OUTPUT:
121 RETVAL
122  
123 SV *
124 lcs_posbit(obj,a)
125 SV *obj
126 AV *a
127
128 PREINIT:
129 0 struct CTX *ctx = (struct CTX *)SvIVX(SvRV(obj));
130 IV i;
131 IV l;
132 IV bits;
133 CODE:
134 0 l = av_len(a);
135
136 0 HV * pos_hash = newHV();
137  
138 0 ▶ for (i = 0; i < l; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < l; ++i) { 0 0 0
139 0 SV *line = *av_fetch(a, i, 0);
140 STRLEN klen;
141 0 char *key = SvPVbyte(line, klen);
142
143 AV * matches;
144 0 SV **lines = hv_fetch(pos_hash, key, klen, 0);
145 0 ▶ if (lines != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (lines != NULL) { 0 0 0
146 0 matches = (AV *)SvRV(*lines);
147 0 bits = SvIVX(*av_fetch(matches, 0, 0));
148 0 bits |= 1 << (i % 64);
149 0 av_store(matches,0,newSViv(bits));
150 }
151 else {
152 0 matches = newAV();
153 0 bits = 1 << (i % 64);
154 0 av_store(matches,0,newSViv(bits));
155 0 hv_store(pos_hash, key, klen, newRV_noinc((SV *)matches), 0);
156 }
157 }
158  
159 0 RETVAL = (SV*)newRV_noinc((SV *)pos_hash);
160 OUTPUT:
161 RETVAL
162  
163 void lcs_LCS(obj, s1, s2)
164 SV *obj
165 AV * s1
166 AV * s2
167  
168 PREINIT:
169 29 struct CTX *ctx = (struct CTX *)SvIVX(SvRV(obj));
170  
171 PPCODE:
172 int d, sn, i;
173 29 struct varray *ses = varray_new(sizeof(struct diff_edit), NULL);
174
175 IV n;
176 IV m;
177 29 n = av_len(s1);
178 29 m = av_len(s2);
179  
180 29 d = diff(s1, 0, n+1, s2, 0, m+1, &_cmp_idx, NULL, 0, ses, &sn, NULL);
181  
182
183 int x,y,j;
184 29 x=y=0;
185  
186 29 XSprePUSH;
187 /*AV *av = newAV();*/
188  
189 146 ▶ for (i = 0; i < sn; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < sn; i++) { 117 29 146
190 117 struct diff_edit *e = varray_get(ses, i);
191
192 117 switch (e->op) {
193 44 case DIFF_MATCH:
194 /*printf("MAT: ");*/
195 /*printf("off %d len %d\n", e->off, e->len);*/
196 355 ▶ for (j = 0; j < e->len; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < e->len; j++) { 311 44 355
197 /*printf("x %d y %d\n", x, y);*/
198
199 AV *arr;
200 311 arr = newAV();
201 311 av_push(arr, newSViv(x));
202 311 av_push(arr, newSViv(y));
203 /*av_push( av, (SV*)arr );*/
204 311 ▶ XPUSHs(sv_2mortal(newRV_noinc((SV *)arr)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newRV_noinc((SV *)arr))); 0 311 311
205
206 311 x++;
207 311 y++;
208 }
209 44 break;
210 40 case DIFF_DELETE:
211 /*printf("DEL: ");*/
212 /*printf("off %d len %d\n", e->off, e->len);*/
213 40 x += e->len;
214 40 break;
215 33 case DIFF_INSERT:
216 /*printf("INS: ");*/
217 /*printf("off %d len %d\n", e->off, e->len);*/
218 33 y += e->len;
219 33 break;
220 }
221 }
222  
223
224
225 29 varray_del(ses);
226 /*return newRV_noinc( (SV*)av );*/
227  
228  
229  
230 void lcs_LCSs(obj, s1, s2)
231 SV *obj
232 SV * s1
233 SV * s2
234  
235 PREINIT:
236 1 struct CTX *ctx = (struct CTX *)SvIVX(SvRV(obj));
237  
238 PPCODE:
239 int d, sn, i;
240 1 struct varray *ses = varray_new(sizeof(struct diff_edit), NULL);
241  
242 STRLEN n;
243 STRLEN m;
244 1 ▶ char *a = SvPV (s1, n);
Branch: 1 branch
BranchTrueFalseTotal
char *a = SvPV (s1, n); 1 0 1
245 1 char *b = SvPV (s2, m);
246
247 1 d = diff(a, 0, n, b, 0, m, NULL, NULL, 0, ses, &sn, NULL);
248  
249
250 int x,y,j;
251 1 x=y=0;
252  
253 1 XSprePUSH;
254  
255 4 ▶ for (i = 0; i < sn; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < sn; i++) { 3 1 4
256 3 struct diff_edit *e = varray_get(ses, i);
257
258 3 switch (e->op) {
259 1 case DIFF_MATCH:
260 /*printf("MAT: ");*/
261 /*printf("off %d len %d\n", e->off, e->len);*/
262 2 ▶ for (j = 0; j < e->len; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < e->len; j++) { 1 1 2
263 /*printf("x %d y %d\n", x, y);*/
264
265 AV *arr;
266 1 arr = newAV();
267 1 av_push(arr, newSViv(x));
268 1 av_push(arr, newSViv(y));
269 1 ▶ XPUSHs(sv_2mortal(newRV_noinc((SV *)arr)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newRV_noinc((SV *)arr))); 0 1 1
270
271 1 x++;
272 1 y++;
273 }
274 1 break;
275 1 case DIFF_DELETE:
276 /*printf("DEL: ");*/
277 /*printf("off %d len %d\n", e->off, e->len);*/
278 1 x += e->len;
279 1 break;
280 1 case DIFF_INSERT:
281 /*printf("INS: ");*/
282 /*printf("off %d len %d\n", e->off, e->len);*/
283 1 y += e->len;
284 1 break;
285 }
286 }
287
288 1 varray_del(ses);
289  
290  
291  
292 void lcs_cLCSs(obj, s1, s2)
293  
294 SV *obj
295 SV * s1
296 SV * s2
297  
298 PREINIT:
299 0 struct CTX *ctx = (struct CTX *)SvIVX(SvRV(obj));
300  
301 PPCODE:
302 int d, sn, i;
303 0 struct varray *ses = varray_new(sizeof(struct diff_edit), NULL);
304  
305 STRLEN n;
306 STRLEN m;
307 0 ▶ char *a = SvPV (s1, n);
Branch: 1 branch
BranchTrueFalseTotal
char *a = SvPV (s1, n); 0 0 0
308 0 char *b = SvPV (s2, m);
309
310 0 d = diff(a, 0, n, b, 0, m, NULL, NULL, 0, ses, &sn, NULL);
311  
312
313 int x,y,j;
314 0 x=y=0;
315  
316 0 XSprePUSH;
317  
318 0 ▶ for (i = 0; i < sn; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < sn; i++) { 0 0 0
319 0 struct diff_edit *e = varray_get(ses, i);
320
321 0 switch (e->op) {
322 0 case DIFF_MATCH:
323 /*printf("x %d y %d\n", x, y);*/
324 if (1) {
325 AV *arr;
326 0 arr = newAV();
327 0 av_push(arr, newSViv(x));
328 0 av_push(arr, newSViv(y));
329 0 av_push(arr, newSViv(e->len));
330 0 ▶ XPUSHs(sv_2mortal(newRV_noinc((SV *)arr)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newRV_noinc((SV *)arr))); 0 0 0
331 0 x += e->len;
332 0 y += e->len;
333 }
334 0 break;
335 0 case DIFF_DELETE:
336 /*printf("DEL: ");*/
337 /*printf("off %d len %d\n", e->off, e->len);*/
338 0 x += e->len;
339 0 break;
340 0 case DIFF_INSERT:
341 /*printf("INS: ");*/
342 /*printf("off %d len %d\n", e->off, e->len);*/
343 0 y += e->len;
344 0 break;
345 }
346 }
347
348 0 varray_del(ses);
349  
« mba/varray.c ↑ Summary mba/diff.c »