damerau-int.c

filter: Statementstmt 94.2% 66 / 70 Branchbran 91.6% 33 / 36 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 93.4% 99 / 106 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Text/Levenshtein/Damerau/XS.pm ↑ Summary XS.xs »
1 /* ugexe@cpan.org (Nick Logan) */
2  
3 #define MIN(a,b) (((a)<(b))?(a):(b))
4  
5 /* Our unsorted dictionary linked list. */
6 /* Note we use character ints, not chars. */
7  
8 struct dictionary{
9 unsigned int key;
10 unsigned int value;
11 struct dictionary* next;
12 };
13 typedef struct dictionary item;
14  
15  
16 58 static __inline item* push(unsigned int key,item* curr){
17 item* head;
18 58 head = malloc(sizeof(item));
19 58 head->key = key;
20 58 head->value = 0;
21 58 head->next = curr;
22 58 return head;
23 }
24  
25  
26 319 static __inline item* find(item* head,unsigned int key){
27 319 item* iterator = head;
28 707 ▶ while(iterator){
Branch: 1 branch
BranchTrueFalseTotal
while(iterator){ 707 0 707
29 707 ▶ if(iterator->key == key){
Branch: 1 branch
BranchTrueFalseTotal
if(iterator->key == key){ 319 388 707
30 319 return iterator;
31 }
32 388 iterator = iterator->next;
33 }
34  
35 0 return NULL;
36 }
37  
38 134 static __inline item* uniquePush(item* head,unsigned int key){
39 134 item* iterator = head;
40  
41 259 ▶ while(iterator){
Branch: 1 branch
BranchTrueFalseTotal
while(iterator){ 201 58 259
42 201 ▶ if(iterator->key == key){
Branch: 1 branch
BranchTrueFalseTotal
if(iterator->key == key){ 76 125 201
43 76 return head;
44 }
45 125 iterator = iterator->next;
46 }
47  
48 58 return push(key,head);
49 }
50  
51 18 static void dict_free(item* head){
52 18 item* iterator = head;
53 76 ▶ while(iterator){
Branch: 1 branch
BranchTrueFalseTotal
while(iterator){ 58 18 76
54 58 item* temp = iterator;
55 58 iterator = iterator->next;
56 58 free(temp);
57 }
58  
59 18 head = NULL;
60 18 }
61  
62 /* End of Dictionary Stuff */
63  
64  
65  
66  
67 /* All calculations/work are done here */
68  
69 19 static int distance(unsigned int src[],unsigned int tgt[],unsigned int x,unsigned int y,unsigned int maxDistance){
70 19 item *head = NULL;
71 unsigned int swapCount,swapScore,targetCharCount,i,j;
72 19 unsigned int *scores = malloc( (x + 2) * (y + 2) * sizeof(unsigned int) );
73 19 unsigned int score_ceil = x + y;
74 unsigned int curr_score;
75 19 ▶ unsigned int diff = x > y ? (x - y) : (y - x);
Branch: 1 branch
BranchTrueFalseTotal
unsigned int diff = x > y ? (x - y) : (y - x); 8 11 19
76  
77 19 ▶ if(maxDistance != 0 && diff > maxDistance) {
Branch: 2 branches
BranchTrueFalseTotal
if(maxDistance != 0 && diff > maxDistance) { 9 10 19
if(maxDistance != 0 && diff > maxDistance) { 1 8 9
78 1 free(scores);
79 1 return -1;
80 }
81  
82 /* intialize matrix start values */
83 18 scores[0] = score_ceil;
84 18 scores[1 * (y + 2) + 0] = score_ceil;
85 18 scores[0 * (y + 2) + 1] = score_ceil;
86 18 scores[1 * (y + 2) + 1] = 0;
87 18 head = uniquePush(uniquePush(head,src[0]),tgt[0]);
88  
89 /* work loops */
90 /* i = src index */
91 /* j = tgt index */
92 85 ▶ for(i=1;i<=x;i++){
Branch: 1 branch
BranchTrueFalseTotal
for(i=1;i<=x;i++){ 67 18 85
93 67 ▶ if(i < x)
Branch: 1 branch
BranchTrueFalseTotal
if(i < x) 49 18 67
94 49 head = uniquePush(head,src[i]);
95 67 scores[(i+1) * (y + 2) + 1] = i;
96 67 scores[(i+1) * (y + 2) + 0] = score_ceil;
97 67 swapCount = 0;
98  
99 319 ▶ for(j=1;j<=y;j++){
Branch: 1 branch
BranchTrueFalseTotal
for(j=1;j<=y;j++){ 252 67 319
100 252 ▶ if(i == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if(i == 1) { 67 185 252
101 67 ▶ if(j < y)
Branch: 1 branch
BranchTrueFalseTotal
if(j < y) 49 18 67
102 49 head = uniquePush(head,tgt[j]);
103 67 scores[1 * (y + 2) + (j + 1)] = j;
104 67 scores[0 * (y + 2) + (j + 1)] = score_ceil;
105 }
106 252 curr_score = 0;
107  
108 252 targetCharCount = find(head,tgt[j-1])->value;
109 252 swapScore = scores[targetCharCount * (y + 2) + swapCount] + i - targetCharCount - 1 + j - swapCount;
110  
111 252 ▶ if(src[i-1] != tgt[j-1]){
Branch: 1 branch
BranchTrueFalseTotal
if(src[i-1] != tgt[j-1]){ 139 113 252
112 139 scores[(i+1) * (y + 2) + (j + 1)] = MIN(swapScore,(MIN(scores[i * (y + 2) + j], MIN(scores[(i+1) * (y + 2) + j], scores[i * (y + 2) + (j + 1)])) + 1));
113 }else{
114 113 swapCount = j;
115 113 scores[(i+1) * (y + 2) + (j + 1)] = MIN(scores[i * (y + 2) + j], swapScore);
116 }
117  
118 252 curr_score = MIN(curr_score, scores[(i+1) * (y + 2) + (j + 1)]);
119 }
120  
121 67 ▶ if(maxDistance != 0 && curr_score > maxDistance) {
Branch: 2 branches
BranchTrueFalseTotal
if(maxDistance != 0 && curr_score > maxDistance) { 28 39 67
if(maxDistance != 0 && curr_score > maxDistance) { 0 28 28
122 0 dict_free(head);
123 0 free(scores);
124 0 return -1;
125 }
126  
127 67 find(head,src[i-1])->value = i;
128 }
129  
130 {
131 18 unsigned int score = scores[(x+1) * (y + 2) + (y + 1)];
132 18 dict_free(head);
133 18 free(scores);
134 18 ▶ return (maxDistance != 0 && maxDistance < score)?(-1):score;
Branch: 2 branches
BranchTrueFalseTotal
return (maxDistance != 0 && maxDistance < score)?(-1):score; 8 10 18
return (maxDistance != 0 && maxDistance < score)?(-1):score; 8 0 8
135 }
136 }
« blib/lib/Text/Levenshtein/Damerau/XS.pm ↑ Summary XS.xs »