blib/lib/LCS/BV.pm

filter: Statementstmt 100% 164 / 164 Branchbran 100% 56 / 56 Conditioncond 100% 50 / 51 (1 uncoverable) MC/DCmcdc 100% 26 / 28 (2 uncoverable) Subroutinesub 100% 11 / 11 Podpod 100% 5 / 5 totaltotal 100% 312 / 315 (3 uncoverable) CC 62 SCAR 28.6
CC
62
Cov
100%
LCS
28.0
LLCS
18.0
LLCS_prepared
10.0
CRAP 17.4
↑ Summary
1 package LCS::BV;
2  
3 4 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 our $VERSION = '0.14';
7 #use utf8;
8  
9 4 our $width = int 0.999+log(~0)/log(2);
10  
11 4 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶no warnings 'portable'; # for 0xffffffffffffffff
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 14 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
15 14 my $class = shift;
16 # uncoverable condition false
17 14 ▶ bless @_ ? @_ > 1 ? {@_} : {%{$_[0]}} : {}, ref $class || $class;
Branch: 2 branches
BranchTrueFalseTotal
@_ ? : 6 8 14
@_ > 1 ? : 2 4 6
Condition100%2/3
ref $class || $class
l!l&&r!l&&!r
590
MC/DC100%0/2
ref $class || $class
ref $class $class
Truth table100%2/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
18 }
19  
20 # H. Hyyroe. A Note on Bit-Parallel Alignment Computation. In
21 # M. Simanek and J. Holub, editors, Stringology, pages 79-87. Department
22 # of Computer Science and Engineering, Faculty of Electrical
23 # Engineering, Czech Technical University, 2004.
24  
25 805 ▶sub LLCS {
Subroutine LLCS: called CC=18 SCAR=28.9
Pod documented
26 805 my ($self,$a,$b) = @_;
27  
28 #use integer;
29 #no warnings 'portable'; # for 0xffffffffffffffff
30  
31 # TODO: maybe faster, if we have fewer expensive iterations
32 #if (@$a < @$b) {
33 # my $temp = $a;
34 # $a = $b;
35 # $b = $temp;
36 #}
37  
38 805 my ($amin, $amax, $bmin, $bmax) = (0, $#$a, 0, $#$b);
39  
40 805 ▶ while ($amin <= $amax and $bmin <= $bmax and $a->[$amin] eq $b->[$bmin]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($amin <= $amax and $bmin <= $bmax and $a->[$amin] eq $b->[$bmin]) 673 805 1478
Condition100%6/6
$amin <= $amax and $bmin <= $bmax and $a->[$amin] eq $b->[$bmin]
!ll&&!rl&&r
192613673
$amin <= $amax and $bmin <= $bmax
!ll&&!rl&&r
110821286
MC/DC100%3/3
$amin <= $amax and $bmin <= $bmax and $a->[$amin] eq $b->[$bmin]
$amin <= $amax $bmin <= $bmax $a->[$amin] eq $b->[$bmin]
Truth table100%4/4
A and B and C
A= $amin <= $amaxB= $bmin <= $bmaxC= $a->[$amin] eq $b->[$bmin]
ABCresult
0XX0
10X0
1100
1111
41 673 $amin++;
42 673 $bmin++;
43 }
44 805 ▶ while ($amin <= $amax and $bmin <= $bmax and $a->[$amax] eq $b->[$bmax]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($amin <= $amax and $bmin <= $bmax and $a->[$amax] eq $b->[$bmax]) 532 805 1337
Condition100%6/6
$amin <= $amax and $bmin <= $bmax and $a->[$amax] eq $b->[$bmax]
!ll&&!rl&&r
361444532
$amin <= $amax and $bmin <= $bmax
!ll&&!rl&&r
194167976
MC/DC100%3/3
$amin <= $amax and $bmin <= $bmax and $a->[$amax] eq $b->[$bmax]
$amin <= $amax $bmin <= $bmax $a->[$amax] eq $b->[$bmax]
Truth table100%4/4
A and B and C
A= $amin <= $amaxB= $bmin <= $bmaxC= $a->[$amax] eq $b->[$bmax]
ABCresult
0XX0
10X0
1100
1111
45 532 $amax--;
46 532 $bmax--;
47 }
48  
49 #my %positions;
50  
51 805 ▶ if ($amax < $width ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($amax < $LCS::BV::width) { } 779 26 805
52 779 my %positions;
53 #$positions{$a->[$_]} |= 1 << ($_ % $width) for $amin..$amax;
54 779 for ($amin..$amax) { $positions{$a->[$_]} |= 1 << ($_ % $width); }
55  
56 779 my $v = ~0;
57 779 my ($p,$u);
58  
59 779 for ($bmin..$bmax) {
60 2649 ▶ $p = $positions{$b->[$_]} // 0;
Condition100%2/2
$positions{$b->[$_]} // 0
l!l
10891560
MC/DC100%1/1
$positions{$b->[$_]} // 0
$positions{$b->[$_]}
Truth table100%2/2
A
A= $positions{$b->[$_]}
Aresult
11
00
61 2649 $u = $v & $p;
62 2649 $v = ($v + $u) | ($v - $u);
63 }
64 779 return $amin + _count_bits(~$v) + $#$a - $amax;
65 }
66 else {
67 26 my %positions;
68 #$positions{$a->[$_]}->[$_ / $width] |= 1 << ($_ % $width) for $amin..$amax;
69 26 for ($amin..$amax) { $positions{$a->[$_]}->[$_ / $width] |= 1 << ($_ % $width); }
70  
71 26 my $S;
72 26 my @Vs = (); # $Vs->[$k] = bits;
73  
74 26 my ($p, $u, $carry);
75  
76 26 my $kmax = ($amax+1) / $width;
77 26 ▶ $kmax++ if (($amax+1) % $width);
Branch: 1 branch
BranchTrueFalseTotal
if ($amax + 1) % $LCS::BV::width 24 2 26
78  
79 26 ▶ for (my $k=0; $k < $kmax; $k++ ) { $Vs[$k] = ~0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($k < $kmax) 81 26 107
80  
81 26 for my $j ($bmin..$bmax) {
82 3036 $carry = 0;
83 3036 ▶ for (my $k=0; $k < $kmax; $k++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k < $kmax) 10258 3036 13294
84 10258 $S = $Vs[$k];
85 10258 ▶ $p = $positions{$b->[$j]}->[$k] // 0;
Condition100%2/2
$positions{$b->[$j]}[$k] // 0
l!l
38736385
MC/DC100%1/1
$positions{$b->[$j]}[$k] // 0
$positions{$b->[$j]}[$k]
Truth table100%2/2
A
A= $positions{$b->[$j]}[$k]
Aresult
11
00
86 10258 $u = $S & $p; # [Hyy04]
87 10258 $Vs[$k] = ($S + $u + $carry) | ($S - $u);
88 10258 $carry = (($S & $u) | (($S | $u) & ~($S + $u + $carry))) >> ($width-1) & 1;
89 }
90 }
91  
92 26 my $bitcount = 0;
93  
94 26 for my $k ( @Vs ) {
95 81 $bitcount += _count_bits(~$k);
96 }
97 26 return $amin + $bitcount + $#$a - $amax;
98 }
99 }
100  
101 805 ▶sub LLCS_prepared {
Subroutine LLCS_prepared: called CC=10 SCAR=23.0
Pod documented
102 805 my ($self,$positions,$b) = @_;
103  
104 #if ( ref($a) eq 'HASH' ) # $a contains $positions
105  
106 805 my ($bmin, $bmax) = (0, $#$b);
107 805 my ($amin, $amax) = (0, $self->{'length_a'} - 1);
108  
109 805 ▶ if ($amax < $width ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($amax < $LCS::BV::width) { } 777 28 805
110  
111 777 my $v = ~0;
112 777 my ($p,$u);
113  
114 777 for ($bmin..$bmax) {
115 3711 ▶ $p = $positions->{$b->[$_]} // 0;
Condition100%2/2
$$positions{$b->[$_]} // 0
l!l
23651346
MC/DC100%1/1
$$positions{$b->[$_]} // 0
$$positions{$b->[$_]}
Truth table100%2/2
A
A= $$positions{$b->[$_]}
Aresult
11
00
116 3711 $u = $v & $p;
117 3711 $v = ($v + $u) | ($v - $u);
118 }
119 777 return _count_bits(~$v);
120 }
121 else {
122  
123 28 my $S;
124 28 my @Vs = (); # $Vs->[$k] = bits;
125  
126 28 my ($p, $u, $carry);
127  
128 28 my $kmax = ($amax+1) / $width;
129 28 ▶ $kmax++ if (($amax+1) % $width);
Branch: 1 branch
BranchTrueFalseTotal
if ($amax + 1) % $LCS::BV::width 26 2 28
130  
131 28 ▶ for (my $k=0; $k < $kmax; $k++ ) { $Vs[$k] = ~0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($k < $kmax) 85 28 113
132  
133 28 for my $j ($bmin..$bmax) {
134 3179 $carry = 0;
135 3179 ▶ for (my $k=0; $k < $kmax; $k++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k < $kmax) 10544 3179 13723
136 10544 $S = $Vs[$k];
137 10544 ▶ $p = $positions->{$b->[$j]}->[$k] // 0;
Condition100%2/2
$$positions{$b->[$j]}[$k] // 0
l!l
40166528
MC/DC100%1/1
$$positions{$b->[$j]}[$k] // 0
$$positions{$b->[$j]}[$k]
Truth table100%2/2
A
A= $$positions{$b->[$j]}[$k]
Aresult
11
00
138 10544 $u = $S & $p; # [Hyy04]
139 10544 $Vs[$k] = ($S + $u + $carry) | ($S - $u);
140 10544 $carry = (($S & $u) | (($S | $u) & ~($S + $u + $carry))) >> ($width-1) & 1;
141 }
142 }
143  
144 28 my $bitcount = 0;
145  
146 28 for my $k ( @Vs ) {
147 85 $bitcount += _count_bits(~$k);
148 }
149 28 return $bitcount;
150 }
151 }
152  
153  
154 795 ▶sub LCS {
Subroutine LCS: called CC=28 SCAR=33.3
Pod documented
155 795 my ($self, $a, $b) = @_;
156  
157 #use integer;
158 #no warnings 'portable'; # for 0xffffffffffffffff
159  
160 795 my ($amin, $amax, $bmin, $bmax) = (0, $#$a, 0, $#$b);
161  
162 795 ▶ while ($amin <= $amax and $bmin <= $bmax and $a->[$amin] eq $b->[$bmin]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($amin <= $amax and $bmin <= $bmax and $a->[$amin] eq $b->[$bmin]) 2161 795 2956
Condition100%6/6
$amin <= $amax and $bmin <= $bmax and $a->[$amin] eq $b->[$bmin]
!ll&&!rl&&r
2075882161
$amin <= $amax and $bmin <= $bmax
!ll&&!rl&&r
125822749
MC/DC100%3/3
$amin <= $amax and $bmin <= $bmax and $a->[$amin] eq $b->[$bmin]
$amin <= $amax $bmin <= $bmax $a->[$amin] eq $b->[$bmin]
Truth table100%4/4
A and B and C
A= $amin <= $amaxB= $bmin <= $bmaxC= $a->[$amin] eq $b->[$bmin]
ABCresult
0XX0
10X0
1100
1111
163 2161 $amin++;
164 2161 $bmin++;
165 }
166 795 ▶ while ($amin <= $amax and $bmin <= $bmax and $a->[$amax] eq $b->[$bmax]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($amin <= $amax and $bmin <= $bmax and $a->[$amax] eq $b->[$bmax]) 532 795 1327
Condition100%6/6
$amin <= $amax and $bmin <= $bmax and $a->[$amax] eq $b->[$bmax]
!ll&&!rl&&r
376419532
$amin <= $amax and $bmin <= $bmax
!ll&&!rl&&r
209167951
MC/DC100%3/3
$amin <= $amax and $bmin <= $bmax and $a->[$amax] eq $b->[$bmax]
$amin <= $amax $bmin <= $bmax $a->[$amax] eq $b->[$bmax]
Truth table100%4/4
A and B and C
A= $amin <= $amaxB= $bmin <= $bmaxC= $a->[$amax] eq $b->[$bmax]
ABCresult
0XX0
10X0
1100
1111
167 532 $amax--;
168 532 $bmax--;
169 }
170  
171 795 my %positions;
172 my @lcs;
173  
174 795 ▶ if ($amax < $width ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($amax < $LCS::BV::width) { } 777 18 795
175 777 $positions{$a->[$_]} |= 1 << ($_ % $width) for $amin..$amax;
176  
177 777 my $S = ~0;
178 777 my @Vs = (~0);
179  
180 777 my ($y,$u);
181  
182 # outer loop
183 777 for my $j ($bmin..$bmax) {
184 1617 ▶ $y = $positions{$b->[$j]} // 0;
Condition100%2/2
$positions{$b->[$j]} // 0
l!l
3151302
MC/DC100%1/1
$positions{$b->[$j]} // 0
$positions{$b->[$j]}
Truth table100%2/2
A
A= $positions{$b->[$j]}
Aresult
11
00
185 1617 $u = $S & $y; # [Hyy04]
186 1617 $S = ($S + $u) | ($S - $u); # [Hyy04]
187 1617 $Vs[$j] = $S;
188 }
189  
190 # recover alignment
191 777 my $i = $amax;
192 777 my $j = $bmax;
193  
194 777 ▶ while ($i >= $amin && $j >= $bmin) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= $amin and $j >= $bmin) 1263 777 2040
Condition100%3/3
$i >= $amin and $j >= $bmin
!ll&&!rl&&r
5292481263
MC/DC100%2/2
$i >= $amin and $j >= $bmin
$i >= $amin $j >= $bmin
Truth table100%3/3
A and B
A= $i >= $aminB= $j >= $bmin
ABresult
0X0
100
111
195 1263 ▶ if ($Vs[$j] & (1<<$i)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Vs[$j] & 1 << $i) { } 850 413 1263
196 850 $i--;
197 }
198 else {
199 413 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless ($j and ~$Vs[$j - 1] & 1 << $i) 266 147 413
Condition100%3/3
$j and ~$Vs[$j - 1] & 1 << $i
!ll&&!rl&&r
54212147
MC/DC100%2/2
$j and ~$Vs[$j - 1] & 1 << $i
$j ~$Vs[$j - 1] & 1 << $i
Truth table100%3/3
A and B
A= $jB= ~$Vs[$j - 1] & 1 << $i
ABresult
0X0
100
111
200 $j && ~$Vs[$j-1] & (1<<$i)
201 ) {
202 266 unshift @lcs, [$i,$j];
203 266 $i--;
204 }
205 413 $j--;
206 }
207 }
208 }
209 else {
210 18 $positions{$a->[$_]}->[$_ / $width] |= 1 << ($_ % $width) for $amin..$amax;
211  
212 18 my $S;
213 18 my @Vs = ([~0]);
214 18 my ($y,$u,$carry);
215  
216 18 my $kmax = ($amax+1) / $width;
217 18 ▶ $kmax++ if (($amax+1) % $width);
Branch: 1 branch
BranchTrueFalseTotal
if ($amax + 1) % $LCS::BV::width 14 4 18
218  
219 # outer loop
220 18 for my $j ($bmin..$bmax) {
221 1488 ▶ for (my $k=0; $k < $kmax; $k++ ) { $Vs[$j]->[$k] = ~0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($k < $kmax) 5098 1488 6586
222 1488 $carry = 0;
223  
224 1488 ▶ for (my $k=0; $k < $kmax; $k++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k < $kmax) 5098 1488 6586
225 5098 ▶ $S = ($j > $bmin) ? $Vs[$j-1]->[$k] : ~0;
Branch: 1 branch
BranchTrueFalseTotal
$j > $bmin ? : 5067 31 5098
226 5098 ▶ $y = $positions{$b->[$j]}->[$k] // 0;
Condition100%2/2
$positions{$b->[$j]}[$k] // 0
l!l
5194579
MC/DC100%1/1
$positions{$b->[$j]}[$k] // 0
$positions{$b->[$j]}[$k]
Truth table100%2/2
A
A= $positions{$b->[$j]}[$k]
Aresult
11
00
227 5098 $u = $S & $y; # [Hyy04]
228  
229 5098 $Vs[$j]->[$k] = ($S + $u + $carry) | ($S - $u);
230  
231 5098 $carry = (($S & $u) | (($S | $u) & ~($S + $u + $carry))) >> ($width-1) & 1;
232 }
233 }
234  
235 # recover alignment
236 18 my $i = $amax;
237 18 my $j = $bmax;
238  
239 18 ▶ while ($i >= $amin && $j >= $bmin) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= $amin and $j >= $bmin) 1638 18 1656
Condition100%3/3
$i >= $amin and $j >= $bmin
!ll&&!rl&&r
1621638
MC/DC100%2/2
$i >= $amin and $j >= $bmin
$i >= $amin $j >= $bmin
Truth table100%3/3
A and B
A= $i >= $aminB= $j >= $bmin
ABresult
0X0
100
111
240 1638 my $k = $i / $width;
241 1638 ▶ if ($Vs[$j]->[$k] & (1<<($i % $width))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Vs[$j][$k] & 1 << $i % $LCS::BV::width) { } 1369 269 1638
242 1369 $i--;
243 }
244 else {
245 269 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless ($j and ~$Vs[$j - 1][$k] & 1 << $i % $LCS::BV::width) 169 100 269
Condition100%3/3
$j and ~$Vs[$j - 1][$k] & 1 << $i % $LCS::BV::width
!ll&&!rl&&r
1168100
MC/DC100%2/2
$j and ~$Vs[$j - 1][$k] & 1 << $i % $LCS::BV::width
$j ~$Vs[$j - 1][$k] & 1 << $i % $LCS::BV::width
Truth table100%3/3
A and B
A= $jB= ~$Vs[$j - 1][$k] & 1 << $i % $LCS::BV::width
ABresult
0X0
100
111
246 $j && ~$Vs[$j-1]->[$k] & (1<<($i % $width))
247 ) {
248 169 unshift @lcs, [$i,$j];
249 169 $i--;
250 }
251 269 $j--;
252 }
253 }
254 }
255  
256 return [
257 795 map([$_ => $_], 0 .. ($bmin-1)), ## no critic qw(BuiltinFunctions::RequireBlockMap)
258 @lcs,
259 map([++$amax => $_], ($bmax+1) .. $#$b) ## no critic qw(BuiltinFunctions::RequireBlockMap)
260 ];
261 }
262  
263 805 ▶sub prepare {
Subroutine prepare: called CC=4 SCAR=13.9
Pod documented
264 805 my ($self, $a) = @_;
265  
266 805 $self->{'length_a'} = @{$a};
267  
268 805 my $positions;
269  
270 805 ▶ if ($#$a < $width ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a->$#* < $LCS::BV::width) { } 777 28 805
271 777 $positions->{$a->[$_]} |= 1 << ($_ % $width) for 0..$#$a;
272 }
273 else {
274 28 $positions->{$a->[$_]}->[$_ / $width] |= 1 << ($_ % $width) for 0..$#$a;
275 }
276 805 return $positions;
277 }
278  
279 1732 ▶sub _count_bits {
Subroutine _count_bits: called CC=3 SCAR=11.0
280 1732 my $v = shift;
281  
282 #use integer;
283 #no warnings 'portable'; # for 0xffffffffffffffff
284  
285 1732 ▶ if ($width == 64) {
Branch: 1 branch
BranchTrueFalseTotal
if ($LCS::BV::width == 64) { } 1727 5 1732
286 1727 $v = $v - (($v >> 1) & 0x5555555555555555);
287 1727 $v = ($v & 0x3333333333333333) + (($v >> 2) & 0x3333333333333333);
288 # (bytesof($v) -1) * bitsofbyte = (8-1)*8 = 56 ----------------------vv
289 1727 $v = (($v + ($v >> 4) & 0x0f0f0f0f0f0f0f0f) * 0x0101010101010101) >> 56;
290 1727 return $v;
291 }
292 else {
293 #$v = $v - (($v >> 1) & 0x55555555);
294 #$v = ($v & 0x33333333) + (($v >> 2) & 0x33333333);
295 ## (bytesof($v) -1) * bitsofbyte = (4-1)*8 = 24 ------vv
296 #$v = (($v + ($v >> 4) & 0x0f0f0f0f) * 0x01010101) >> 24
297  
298 5 my $c; # count
299 5 ▶ for ($c = 0; $v; $c++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v) 64 5 69
300 64 $v &= $v - 1; # clear the least significant bit set
301 }
302 5 return $c;
303 }
304 }
305  
306 4 1;
307  
308 __END__
↑ Summary