LCS.pm

filter: Statementstmt 100% 167 / 167 Branchbran 98.5% 67 / 68 Conditioncond 100% 17 / 18 (1 uncoverable) MC/DCmcdc 100% 10 / 12 (2 uncoverable) Subroutinesub 100% 19 / 19 Podpod 100% 13 / 13 totaltotal 99.6% 293 / 297 (3 uncoverable) CC 54 SCAR 17.3
CC
54
Cov
100%
lcs2align
10.0
LLCS
7.0
_all_lcs
7.0
CRAP 5.7
« LCS/Tiny.pm ↑ Summary
1 package LCS;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 our $VERSION = '0.11';
8  
9 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 6 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
12 6 my $class = shift;
13 # uncoverable condition false
14 6 ▶ bless @_ ? @_ > 1 ? {@_} : {%{$_[0]}} : {}, ref $class || $class;
Branch: 2 branches
BranchTrueFalseTotal
@_ ? : 3 3 6
@_ > 1 ? : 1 2 3
Condition100%2/3
ref $class || $class
l!l&&r!l&&!r
420
MC/DC100%0/2
ref $class || $class
ref $class $class
Truth table100%2/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
15 }
16  
17 24 ▶sub align {
Subroutine align: called CC=1 SCAR=0.0
Pod documented
18 24 my ($self, $X, $Y) = @_;
19  
20 24 return $self->lcs2align(
21 $X, $Y, $self->LCS($X,$Y)
22 );
23 }
24  
25 126 ▶sub lcs2align {
Subroutine lcs2align: called CC=10 SCAR=23.0
Pod documented
26 126 my ($self, $X, $Y, $LCS) = @_;
27  
28 126 my $hunks = [];
29  
30 126 my $Xcurrent = -1;
31 126 my $Ycurrent = -1;
32 126 my $Xtemp;
33 my $Ytemp;
34  
35 126 for my $hunk (@$LCS) {
36 506 ▶ while ( ($Xcurrent+1 < $hunk->[0] || $Ycurrent+1 < $hunk->[1]) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Xcurrent + 1 < $hunk->[0] or $Ycurrent + 1 < $hunk->[1]) 250 506 756
Condition100%3/3
$Xcurrent + 1 < $hunk->[0] or $Ycurrent + 1 < $hunk->[1]
l!l&&r!l&&!r
18664506
MC/DC100%2/2
$Xcurrent + 1 < $hunk->[0] or $Ycurrent + 1 < $hunk->[1]
$Xcurrent + 1 < $hunk->[0] $Ycurrent + 1 < $hunk->[1]
Truth table100%3/3
A or B
A= $Xcurrent + 1 < $hunk->[0]B= $Ycurrent + 1 < $hunk->[1]
ABresult
1X1
011
000
37 250 $Xtemp = '';
38 250 $Ytemp = '';
39 250 ▶ if ($Xcurrent+1 < $hunk->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Xcurrent + 1 < $hunk->[0]) 186 64 250
40 186 $Xcurrent++;
41 186 $Xtemp = $X->[$Xcurrent];
42 }
43 250 ▶ if ($Ycurrent+1 < $hunk->[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Ycurrent + 1 < $hunk->[1]) 118 132 250
44 118 $Ycurrent++;
45 118 $Ytemp = $Y->[$Ycurrent];
46 }
47 250 push @$hunks,[$Xtemp,$Ytemp];
48 }
49  
50 506 $Xcurrent = $hunk->[0];
51 506 $Ycurrent = $hunk->[1];
52 506 push @$hunks,[$X->[$Xcurrent],$Y->[$Ycurrent]]; # elements
53 }
54 126 ▶ while ( ($Xcurrent+1 <= $#$X || $Ycurrent+1 <= $#$Y) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Xcurrent + 1 <= $X->$#* or $Ycurrent + 1 <= $Y->$#*) 136 126 262
Condition100%3/3
$Xcurrent + 1 <= $X->$#* or $Ycurrent + 1 <= $Y->$#*
l!l&&r!l&&!r
10234126
MC/DC100%2/2
$Xcurrent + 1 <= $X->$#* or $Ycurrent + 1 <= $Y->$#*
$Xcurrent + 1 <= $X->$#* $Ycurrent + 1 <= $Y->$#*
Truth table100%3/3
A or B
A= $Xcurrent + 1 <= $X->$#*B= $Ycurrent + 1 <= $Y->$#*
ABresult
1X1
011
000
55 136 $Xtemp = '';
56 136 $Ytemp = '';
57 136 ▶ if ($Xcurrent+1 <= $#$X) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Xcurrent + 1 <= $X->$#*) 102 34 136
58 102 $Xcurrent++;
59 102 $Xtemp = $X->[$Xcurrent];
60 }
61 136 ▶ if ($Ycurrent+1 <= $#$Y) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Ycurrent + 1 <= $Y->$#*) 76 60 136
62 76 $Ycurrent++;
63 76 $Ytemp = $Y->[$Ycurrent];
64 }
65 136 push @$hunks,[$Xtemp,$Ytemp];
66 }
67 126 return $hunks;
68 }
69  
70 72 ▶sub sequences2hunks {
Subroutine sequences2hunks: called CC=1 SCAR=0.0
Pod documented
71 72 my ($self, $a, $b) = @_;
72 72 return [ map { [ $a->[$_], $b->[$_] ] } 0..$#$a ];
73 }
74  
75 6 ▶sub clcs2lcs {
Subroutine clcs2lcs: called CC=3 SCAR=11.0
Pod documented
76 6 my ($self, $clcs) = @_;
77 6 my $lcs = [];
78 6 for my $entry (@$clcs) {
79 8 ▶ for (my $i = 0; $i < $entry->[2];$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $entry->[2]) 15 8 23
80 15 push @$lcs,[$entry->[0]+$i,$entry->[1]+$i];
81 }
82 }
83 6 return $lcs;
84 }
85  
86 6 ▶sub lcs2clcs {
Subroutine lcs2clcs: called CC=4 SCAR=13.9
Pod documented
87 6 my ($self, $lcs) = @_;
88 6 my $clcs = [];
89 6 for my $entry (@$lcs) {
90 15 ▶ if (@$clcs && $clcs->[-1]->[0] + $clcs->[-1]->[2] == $entry->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$clcs and $clcs->[-1][0] + $clcs->[-1][2] == $entry->[0]) { } 7 8 15
Condition100%3/3
@$clcs and $clcs->[-1][0] + $clcs->[-1][2] == $entry->[0]
!ll&&!rl&&r
627
MC/DC100%2/2
@$clcs and $clcs->[-1][0] + $clcs->[-1][2] == $entry->[0]
@$clcs $clcs->[-1][0] + $clcs->[-1][2] == $entry->[0]
Truth table100%3/3
A and B
A= @$clcsB= $clcs->[-1][0] + $clcs->[-1][2] == $entry->[0]
ABresult
0X0
100
111
91 7 $clcs->[-1]->[2]++;
92 }
93 else {
94 8 push @$clcs,[$entry->[0],$entry->[1],1];
95 }
96 }
97 6 return $clcs;
98 }
99  
100 24 ▶sub hunks2sequences {
Subroutine hunks2sequences: called CC=2 SCAR=6.9
Pod documented
101 24 my ($self, $hunks) = @_;
102  
103 24 my $a = [];
104 24 my $b = [];
105  
106 24 for my $hunk (@$hunks) {
107 173 push @$a, $hunk->[0];
108 173 push @$b, $hunk->[1];
109 }
110 24 return ($a,$b);
111 }
112  
113 48 ▶sub align2strings {
Subroutine align2strings: called CC=3 SCAR=11.0
Pod documented
114 48 my ($self, $hunks,$gap) = @_;
115 #$gap //= '_';
116 48 ▶ $gap = (defined $gap) ? $gap : '_';
Branch: 1 branch
BranchTrueFalseTotal
defined $gap ? : 24 24 48
117  
118 48 my $a = '';
119 48 my $b = '';
120  
121 48 for my $hunk (@$hunks) {
122 346 my ($ae,$be) = $self->fill_strings($hunk->[0],$hunk->[1],$gap);
123 346 $a .= $ae;
124 346 $b .= $be;
125 }
126 48 return ($a,$b);
127 }
128  
129 346 ▶sub fill_strings {
Subroutine fill_strings: called CC=6 SCAR=17.9
Pod documented
130 346 my ($self, $string1,$string2, $gap) = @_;
131 #$gap //= '_';
132 346 ▶ $gap = (defined $gap) ? $gap : '_';
Branch: 1 branch
BranchTrueFalseTotal
defined $gap ? : 346 0 346
133  
134 346 my @m = $string1 =~ m/(\X)/g;
135 346 my @n = $string2 =~ m/(\X)/g;
136 346 my $max = max(scalar(@m),scalar(@n));
137 346 ▶ if ($max - scalar(@m) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max - scalar(@m) > 0) 28 318 346
138 28 for (1..$max-scalar(@m)) {
139 28 $string1 .= $gap;
140 }
141 }
142 346 ▶ if ($max - scalar(@n) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max - scalar(@n) > 0) 68 278 346
143 68 for (1..$max-scalar(@n)) {
144 68 $string2 .= $gap;
145 }
146 }
147 346 return ($string1,$string2);
148 }
149  
150 24 ▶sub LLCS {
Subroutine LLCS: called CC=7 SCAR=19.5
Pod documented
151 24 my ($self,$X,$Y) = @_;
152  
153 24 my $m = scalar @$X;
154 24 my $n = scalar @$Y;
155  
156 24 my $c = [];
157  
158 24 for my $i (0..1) {
159 48 for my $j (0..$n) {
160 326 $c->[$i][$j]=0;
161 }
162 }
163  
164 24 my ($i,$j);
165  
166 24 ▶ for ($i=1; $i <= $m; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $m) 159 24 183
167 159 ▶ for ($j=1; $j <= $n; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j <= $n) 2984 159 3143
168 2984 ▶ if ($X->[$i-1] eq $Y->[$j-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$X[$i - 1] eq $$Y[$j - 1]) { } 146 2838 2984
169 146 $c->[1][$j] = $c->[0][$j-1]+1;
170 }
171 else {
172 2838 $c->[1][$j] = max($c->[1][$j-1],$c->[0][$j]);
173 }
174 }
175 159 ▶ for ($j = 1; $j <= $n; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j <= $n) 2984 159 3143
176 2984 $c->[0][$j] = $c->[1][$j];
177 }
178 }
179 24 return ($c->[1][$n]);
180 }
181  
182  
183 96 ▶sub LCS {
Subroutine LCS: called CC=6 SCAR=17.9
Pod documented
184 96 my ($self,$X,$Y) = @_;
185  
186 96 my $m = scalar @$X;
187 96 my $n = scalar @$Y;
188  
189 96 my $c = [];
190 96 my ($i,$j);
191 96 ▶ for ($i=0;$i<=$m;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $m) 732 96 828
192 732 ▶ for ($j=0;$j<=$n;$j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j <= $n) 13224 732 13956
193 13224 $c->[$i][$j]=0;
194 }
195 }
196 96 ▶ for ($i=1;$i<=$m;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $m) 636 96 732
197 636 ▶ for ($j=1;$j<=$n;$j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j <= $n) 11936 636 12572
198 11936 ▶ if ($X->[$i-1] eq $Y->[$j-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$X[$i - 1] eq $$Y[$j - 1]) { } 584 11352 11936
199 584 $c->[$i][$j] = $c->[$i-1][$j-1]+1;
200 }
201 else {
202 11352 $c->[$i][$j] = max($c->[$i][$j-1], $c->[$i-1][$j]);
203 }
204 }
205 }
206 96 my $path = $self->_lcs($X,$Y,$c,$m,$n,[]);
207  
208 96 return $path;
209 }
210  
211  
212 14536 ▶sub max {
Subroutine max: called CC=2 SCAR=6.9
Pod documented
213 14536 ▶ ($_[0] > $_[1]) ? $_[0] : $_[1];
Branch: 1 branch
BranchTrueFalseTotal
$_[0] > $_[1] ? : 6563 7973 14536
214 }
215  
216  
217 96 ▶sub _lcs {
Subroutine _lcs: called CC=5 SCAR=16.1
218 96 my ($self,$X,$Y,$c,$i,$j,$L) = @_;
219  
220 96 ▶ while ($i > 0 && $j > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0 and $j > 0) 692 96 788
Condition100%3/3
$i > 0 and $j > 0
!ll&&!rl&&r
6036692
MC/DC100%2/2
$i > 0 and $j > 0
$i > 0 $j > 0
Truth table100%3/3
A and B
A= $i > 0B= $j > 0
ABresult
0X0
100
111
221 692 ▶ if ($X->[$i-1] eq $Y->[$j-1]) {
Branch: 2 branches
BranchTrueFalseTotal
if ($$X[$i - 1] eq $$Y[$j - 1]) { } 412 280 692
elsif ($c->[$i][$j] == $$c[$i - 1][$j]) { } 180 100 280
222 412 unshift @{$L},[$i-1,$j-1];
223 412 $i--;
224 412 $j--;
225 }
226 elsif ($c->[$i][$j] == $c->[$i-1][$j]) {
227 180 $i--;
228 }
229 else {
230 100 $j--;
231 }
232 }
233 96 return $L;
234 }
235  
236  
237 96 ▶sub _all_lcs {
Subroutine _all_lcs: called CC=7 SCAR=19.5
238 96 my ($self,$ranks,$rank,$max) = @_;
239  
240 96 my $R = [[]];
241  
242 96 ▶ while ($rank <= $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rank <= $max) 412 96 508
243 412 my @temp;
244 412 for my $path (@$R) {
245 612 for my $hunk (@{$ranks->{$rank}}) {
246 1020 ▶ if (scalar @{$path} == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @{$path;} == 0) { } 152 868 1020
elsif ($path->[-1][0] < $hunk->[0] and $path->[-1][1] < $hunk->[1]) { } 520 476 996
247 152 push @temp,[$hunk];
248 }
249 ▶ elsif (($path->[-1][0] < $hunk->[0]) && ($path->[-1][1] < $hunk->[1])) {
Condition100%3/3
$path->[-1][0] < $hunk->[0] and $path->[-1][1] < $hunk->[1]
!ll&&!rl&&r
128220520
MC/DC100%2/2
$path->[-1][0] < $hunk->[0] and $path->[-1][1] < $hunk->[1]
$path->[-1][0] < $hunk->[0] $path->[-1][1] < $hunk->[1]
Truth table100%3/3
A and B
A= $path->[-1][0] < $hunk->[0]B= $path->[-1][1] < $hunk->[1]
ABresult
0X0
100
111
250 520 push @temp,[@$path,$hunk];
251 }
252 }
253 }
254 412 @$R = @temp;
255 412 $rank++;
256 }
257 96 return $R;
258 }
259  
260 # get all LCS of two arrays
261 # records the matches by rank
262 96 ▶sub allLCS {
Subroutine allLCS: called CC=7 SCAR=19.5
Pod documented
263 96 my ($self,$X,$Y) = @_;
264  
265 96 my $m = scalar @$X;
266 96 my $n = scalar @$Y;
267  
268 96 my $ranks = {}; # e.g. '4' => [[3,6],[4,5]]
269 96 my $c = [];
270 96 my ($i,$j);
271  
272 96 for (0..$m) {$c->[$_][0]=0;}
273 96 for (0..$n) {$c->[0][$_]=0;}
274 96 ▶ for ($i=1;$i<=$m;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $m) 636 96 732
275 636 ▶ for ($j=1;$j<=$n;$j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j <= $n) 11936 636 12572
276 11936 ▶ if ($X->[$i-1] eq $Y->[$j-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$X[$i - 1] eq $$Y[$j - 1]) { } 584 11352 11936
277 584 $c->[$i][$j] = $c->[$i-1][$j-1]+1;
278 584 push @{$ranks->{$c->[$i][$j]}},[$i-1,$j-1];
279 }
280 else {
281 11352 ▶ $c->[$i][$j] =
Branch: 1 branch
BranchTrueFalseTotal
$c->[$i][$j - 1] > $$c[$i - 1][$j] ? : 5196 6156 11352
282 ($c->[$i][$j-1] > $c->[$i-1][$j])
283 ? $c->[$i][$j-1]
284 : $c->[$i-1][$j];
285 }
286 }
287 }
288 96 my $max = scalar keys %$ranks;
289 96 return $self->_all_lcs($ranks,1,$max);
290 }
291  
292 2 1;
293 __END__
« LCS/Tiny.pm ↑ Summary