| 1 | package LCS::Tiny; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.006; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | our $VERSION = '0.11'; | |||||||||||||||||||||||||||||||||||||||||
| 7 | #use utf8; | ||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 6 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 6 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 11 | # uncoverable condition false | ||||||||||||||||||||||||||||||||||||||||||
| 12 | 6 | ▶ | bless @_ ? @_ > 1 ? {@_} : {%{$_[0]}} : {}, ref $class || $class; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%2/3
ref $class || $class
MC/DC100%0/2
ref $class || $class
ref $class
$class
Truth table100%2/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | } | ||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | # Implemented according to | ||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | # J. W. Hunt and T. G. Szymanski. | ||||||||||||||||||||||||||||||||||||||||||
| 18 | # A fast algorithm for computing longest common subsequences. | ||||||||||||||||||||||||||||||||||||||||||
| 19 | # Commun. ACM, 20(5):350-353, 1977. | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | 24 | ▶ | sub LCS { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
LCS: called CC=21 SCAR=30.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 24 | my ($self, $a, $b) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 24 | my ($amin, $amax, $bmin, $bmax) = (0, $#$a, 0, $#$b); | |||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 24 | ▶ | while ($amin <= $amax and $bmin <= $bmax and $a->[$amin] eq $b->[$bmin]) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$amin <= $amax and $bmin <= $bmax and $a->[$amin] eq $b->[$bmin]
$amin <= $amax and $bmin <= $bmax
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]
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 13 | $amin++; | |||||||||||||||||||||||||||||||||||||||||
| 28 | 13 | $bmin++; | |||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||
| 30 | 24 | ▶ | while ($amin <= $amax and $bmin <= $bmax and $a->[$amax] eq $b->[$bmax]) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$amin <= $amax and $bmin <= $bmax and $a->[$amax] eq $b->[$bmax]
$amin <= $amax and $bmin <= $bmax
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]
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 7 | $amax--; | |||||||||||||||||||||||||||||||||||||||||
| 32 | 7 | $bmax--; | |||||||||||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 24 | my $bMatches; | |||||||||||||||||||||||||||||||||||||||||
| 36 | 24 | unshift @{ $bMatches->{$b->[$_]} },$_ for $bmin..$bmax; | |||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | 24 | my $matchVector = []; | |||||||||||||||||||||||||||||||||||||||||
| 39 | 24 | my $thresh = []; | |||||||||||||||||||||||||||||||||||||||||
| 40 | 24 | my $links = []; | |||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | 24 | my ( $i, $ai, $j, $k ); | |||||||||||||||||||||||||||||||||||||||||
| 43 | 24 | ▶ | for ( $i = $amin ; $i <= $amax ; $i++ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 139 | $ai = $a->[$i]; | |||||||||||||||||||||||||||||||||||||||||
| 45 | 139 | ▶ | if ( exists( $bMatches->{$ai} ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 95 | for $j ( @{ $bMatches->{$ai} } ) { | |||||||||||||||||||||||||||||||||||||||||
| 47 | 112 | ▶ | if ( !@$thresh || $j > $thresh->[-1] ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not @$thresh or $j > $thresh->[-1]
MC/DC100%2/2
not @$thresh or $j > $thresh->[-1]
not @$thresh
$j > $thresh->[-1]
Truth table100%3/3
not(A) or B
A= not @$threshB= $j > $thresh->[-1]
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 83 | $k = $#$thresh+1; | |||||||||||||||||||||||||||||||||||||||||
| 49 | 83 | $thresh->[$k] = $j; | |||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||
| 51 | #elsif ( $k and $thresh->[$k] > $j and $thresh->[ $k - 1 ] < $j ) { | ||||||||||||||||||||||||||||||||||||||||||
| 52 | # $thresh->[$k] = $j; | ||||||||||||||||||||||||||||||||||||||||||
| 53 | #} | ||||||||||||||||||||||||||||||||||||||||||
| 54 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 55 | # binary search for insertion point | ||||||||||||||||||||||||||||||||||||||||||
| 56 | 29 | $k = 0; | |||||||||||||||||||||||||||||||||||||||||
| 57 | 29 | my $index; | |||||||||||||||||||||||||||||||||||||||||
| 58 | my $found; | ||||||||||||||||||||||||||||||||||||||||||
| 59 | 29 | my $high = $#$thresh; | |||||||||||||||||||||||||||||||||||||||||
| 60 | 29 | ▶ | while ( $k <= $high ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 1 | ▶ | use integer; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 48 | $index = ( $high + $k ) / 2; | |||||||||||||||||||||||||||||||||||||||||
| 63 | #$index = int(( $high + $k ) / 2); # without 'use integer' | ||||||||||||||||||||||||||||||||||||||||||
| 64 | 48 | $found = $thresh->[$index]; | |||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | 48 | ▶ | if ( $j == $found ) { $k = undef; last; } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 16 | elsif ( $j > $found ) { $k = $index + 1; } | |||||||||||||||||||||||||||||||||||||||||
| 68 | 26 | else { $high = $index - 1; } | |||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | # now insertion point is in $k. | ||||||||||||||||||||||||||||||||||||||||||
| 71 | 29 | ▶ | $thresh->[$k] = $j if (defined $k); # overwrite next larger | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | 112 | ▶ | if (defined $k) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 106 | ▶ | $links->[$k] = [ ( $k ? $links->[ $k - 1 ] : undef ), $i, $j ]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 24 | ▶ | if (@$thresh) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 14 | ▶ | for ( my $link = $links->[$#$thresh] ; $link ; $link = $link->[0] ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 83 | unshift @$matchVector,[$link->[1],$link->[2]]; | |||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 24 | return [ map([$_ => $_], 0 .. ($bmin-1)), | |||||||||||||||||||||||||||||||||||||||||
| 85 | @$matchVector, | ||||||||||||||||||||||||||||||||||||||||||
| 86 | map([++$amax => $_], ($bmax+1) .. $#$b) ]; | ||||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | __END__ | ||||||||||||||||||||||||||||||||||||||||||