| 1 |
|
| #define PERL_NO_GET_CONTEXT |
| 2 |
|
| #define NO_XSLOCKS |
| 3 |
|
| #include "EXTERN.h" |
| 4 |
|
| #include "perl.h" |
| 5 |
|
| #include "XSUB.h" |
| 6 |
|
| |
| 7 |
|
| #define MIN(a,b) (((a)<(b))?(a):(b)) |
| 8 |
|
| #define MAX(a,b) (((a)>(b))?(a):(b)) |
| 9 |
|
| |
| 10 |
|
| MODULE = Text::Levenshtein::XS PACKAGE = Text::Levenshtein::XS |
| 11 |
|
| |
| 12 |
|
| PROTOTYPES: ENABLE |
| 13 |
|
| |
| 14 |
|
| void * |
| 15 |
|
| xs_distance (arraySource, arrayTarget, maxDistance) |
| 16 |
|
| AV * arraySource |
| 17 |
|
| AV * arrayTarget |
| 18 |
|
| SV * maxDistance |
| 19 |
|
| INIT: |
| 20 |
|
| unsigned int i,j,edits,*s,*t,*v0,*v1; |
| 21 |
54 |
| unsigned int lenSource = av_len(arraySource)+1; |
| 22 |
54 |
| unsigned int lenTarget = av_len(arrayTarget)+1; |
| 23 |
|
| /* hold the user supplied argument for max distance */ |
| 24 |
54 |
| unsigned int md = SvUV(maxDistance); |
| 25 |
|
| /* mdx contains a calculated max different (md) to use in the algorithm itself */ |
| 26 |
54 |
▶ | unsigned int mdx = (md == 0) ? MAX(lenSource,lenTarget) : md; |
Branch: 1 branch
| Branch | True | False | Total |
| unsigned int mdx = (md == 0) ? MAX(lenSource,lenTarget) : md; |
30 |
24 |
54 |
|
| 27 |
54 |
| unsigned int diff = MAX(lenSource , lenTarget) - MIN(lenSource, lenTarget); |
| 28 |
54 |
| unsigned int undef = 0; |
| 29 |
|
| SV* elem; |
| 30 |
|
| |
| 31 |
54 |
▶ | if(lenSource == 0 || lenTarget == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if(lenSource == 0 || lenTarget == 0) { |
50 |
4 |
54 |
| if(lenSource == 0 || lenTarget == 0) { |
3 |
47 |
50 |
|
| 32 |
7 |
▶ | if( md != 0 && MAX(lenSource, lenTarget) > md ) { |
Branch: 2 branches
| Branch | True | False | Total |
| if( md != 0 && MAX(lenSource, lenTarget) > md ) { |
4 |
3 |
7 |
| if( md != 0 && MAX(lenSource, lenTarget) > md ) { |
2 |
2 |
4 |
|
| 33 |
2 |
| XSRETURN_UNDEF; |
| 34 |
|
| } |
| 35 |
|
| else { |
| 36 |
5 |
▶ | XPUSHs(sv_2mortal(newSVuv( MAX(lenSource, lenTarget) ))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSVuv( MAX(lenSource, lenTarget) ))); |
0 |
5 |
5 |
|
| 37 |
5 |
| XSRETURN(1); |
| 38 |
|
| } |
| 39 |
|
| } |
| 40 |
|
| |
| 41 |
47 |
▶ | if (diff > mdx) |
Branch: 1 branch
| Branch | True | False | Total |
| if (diff > mdx) |
4 |
43 |
47 |
|
| 42 |
4 |
| XSRETURN_UNDEF; |
| 43 |
|
| PPCODE: |
| 44 |
|
| { |
| 45 |
43 |
| Newx(s, (lenSource + 1), unsigned int); // source |
| 46 |
43 |
| Newx(t, (lenTarget + 1), unsigned int); // target |
| 47 |
43 |
| Newx(v0, (lenTarget + 1), unsigned int); // vector 0 |
| 48 |
43 |
| Newx(v1, (lenTarget + 1), unsigned int); // vector 1 |
| 49 |
|
| /* init first distance row with worst-case distance values */ |
| 50 |
1270 |
▶ | for (i=0; i < (lenTarget + 1); i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0; i < (lenTarget + 1); i++) { |
1227 |
43 |
1270 |
|
| 51 |
1227 |
| v0[i] = i; |
| 52 |
|
| } |
| 53 |
|
| |
| 54 |
30304 |
▶ | for (i=0; i < lenSource; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0; i < lenSource; i++) { |
30261 |
43 |
30304 |
|
| 55 |
30261 |
▶ | if( undef > 0 ) |
Branch: 1 branch
| Branch | True | False | Total |
| if( undef > 0 ) |
0 |
30261 |
30261 |
|
| 56 |
0 |
| break; |
| 57 |
|
| |
| 58 |
30261 |
| elem = sv_2mortal(av_shift(arraySource)); |
| 59 |
30261 |
| s[i] = SvUV((SV *)elem); |
| 60 |
|
| |
| 61 |
30261 |
| v1[0] = i + 1; |
| 62 |
|
| |
| 63 |
30031074 |
▶ | for (j = 0; j < lenTarget; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < lenTarget; j++) { |
30000813 |
30261 |
30031074 |
|
| 64 |
30000813 |
▶ | if(i == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(i == 0) { |
1184 |
29999629 |
30000813 |
|
| 65 |
1184 |
| elem = sv_2mortal(av_shift(arrayTarget)); |
| 66 |
1184 |
| t[j] = SvUV((SV *)elem); |
| 67 |
|
| } |
| 68 |
|
| |
| 69 |
30000813 |
| v1[j + 1] = MIN(MIN(v1[j] + 1, v0[j + 1] + 1), (v0[j] + ((s[i] == t[j]) ? 0 : 1))); |
| 70 |
|
| |
| 71 |
|
| /* Check the current distance once we have reached the appropriate index */ |
| 72 |
|
| /* v1[0] == index of current distance of v1 (i.e. v1[v1[0]] == current distance) */ |
| 73 |
|
| /* We also take diff into account so we can guess if current distance + length */ |
| 74 |
|
| /* difference would push the total edit distance over the max distance */ |
| 75 |
30000813 |
▶ | if( v1[0] == j ) |
Branch: 1 branch
| Branch | True | False | Total |
| if( v1[0] == j ) |
1119 |
29999694 |
30000813 |
|
| 76 |
1119 |
▶ | if( mdx < ((diff > v1[j]) ? (diff - v1[j]) : (diff + v1[j])) ) |
Branch: 2 branches
| Branch | True | False | Total |
| if( mdx < ((diff > v1[j]) ? (diff - v1[j]) : (diff + v1[j])) ) |
1034 |
85 |
1119 |
| if( mdx < ((diff > v1[j]) ? (diff - v1[j]) : (diff + v1[j])) ) |
10 |
1109 |
1119 |
|
| 77 |
10 |
▶ | if( mdx > v1[j] ) |
Branch: 1 branch
| Branch | True | False | Total |
| if( mdx > v1[j] ) |
0 |
10 |
10 |
|
| 78 |
0 |
| undef = 1; |
| 79 |
|
| } |
| 80 |
|
| |
| 81 |
|
| |
| 82 |
|
| /* copy v1 to v0. no need to copy the array on the last iteration */ |
| 83 |
30261 |
▶ | if( i < lenSource ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if( i < lenSource ) { |
30261 |
0 |
30261 |
|
| 84 |
30061335 |
▶ | for (j = 0; j < (lenTarget + 1); j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < (lenTarget + 1); j++) |
30031074 |
30261 |
30061335 |
|
| 85 |
30031074 |
| v0[j] = v1[j]; |
| 86 |
|
| } |
| 87 |
|
| } |
| 88 |
|
| |
| 89 |
43 |
▶ | if( md > 0 && md < v1[lenTarget] ) |
Branch: 2 branches
| Branch | True | False | Total |
| if( md > 0 && md < v1[lenTarget] ) |
16 |
27 |
43 |
| if( md > 0 && md < v1[lenTarget] ) |
5 |
11 |
16 |
|
| 90 |
5 |
| undef = 1; |
| 91 |
|
| |
| 92 |
|
| /* don't check md here so that if something is wrong with the earlier short circuit the tests will catch it */ |
| 93 |
43 |
▶ | XPUSHs(sv_2mortal( (undef == 1) ? &PL_sv_undef : newSVuv(v1[lenTarget]) )); |
Branch: 2 branches
| Branch | True | False | Total |
| XPUSHs(sv_2mortal( (undef == 1) ? &PL_sv_undef : newSVuv(v1[lenTarget]) )); |
0 |
43 |
43 |
| XPUSHs(sv_2mortal( (undef == 1) ? &PL_sv_undef : newSVuv(v1[lenTarget]) )); |
38 |
5 |
43 |
|
| 94 |
|
| |
| 95 |
43 |
| Safefree(s); |
| 96 |
43 |
| Safefree(t); |
| 97 |
43 |
| Safefree(v0); |
| 98 |
43 |
| Safefree(v1); |
| 99 |
|
| } /* PPCODE */ |
| 100 |
|
| |