File Coverage

blib/lib/Algorithm/LCS/XS.pm
Criterion Covered Total %
statement 49 49 100.0
branch 4 8 50.0
condition 9 18 50.0
subroutine 11 11 100.0
pod 3 5 60.0
total 76 91 83.5


line stmt bran cond sub pod time code
1             package Algorithm::LCS::XS;
2              
3 1     1   63283 use 5.008;
  1         3  
4 1     1   5 use strict;
  1         2  
  1         17  
5 1     1   4 use warnings;
  1         4  
  1         17  
6 1     1   378 use version;
  1         1653  
  1         5  
7             our $VERSION = qv(2.0.0);
8 1     1   92 use base 'Exporter';
  1         2  
  1         609  
9             our @EXPORT_OK = qw/&ADLCS &LCSidx/;
10             require XSLoader;
11             XSLoader::load('Algorithm::LCS::XS', $VERSION);
12              
13             ##############################
14             # code adapted from Algorithm::Diff
15              
16             sub line_map {
17 200003     200003 1 244108 my $ctx = shift;
18 200003         210889 my %lines;
19 200003         369844 push @{ $lines{$_[$_]} }, $_ for 0..$#_; # values MUST be SvIOK
  11400174         15303038  
20 200003         295708 \%lines;
21             }
22              
23             sub callback {
24 1     1 1 9027 my ($ctx, @b) = @_;
25 1         3 my $h = $ctx->line_map(@b);
26 100001 50   100001   1654076 sub { @_ ? _core_loop($ctx, $_[0], 0, $#{$_[0]}, $h) : @b }
  100001         5167449  
27 1         5 }
28              
29             sub LCS {
30 200002     200002 1 283603 my ($ctx, $a, $b) = @_;
31 200002         310404 my ($amin, $amax, $bmin, $bmax) = (0, $#$a, 0, $#$b);
32              
33 200002   33     769164 while ($amin <= $amax and $bmin <= $bmax and $a->[$amin] eq $b->[$bmin]) {
      66        
34 200002         226006 $amin++;
35 200002         600362 $bmin++;
36             }
37 200002   33     616159 while ($amin <= $amax and $bmin <= $bmax and $a->[$amax] eq $b->[$bmax]) {
      66        
38 400004         407450 $amax--;
39 400004         1103453 $bmax--;
40             }
41              
42 200002         592902 my $h = $ctx->line_map(@$b[$bmin..$bmax]); # line numbers are off by $bmin
43              
44 200002 50       476584 return $amin + _core_loop($ctx, $a, $amin, $amax, $h) + ($#$a - $amax)
45             unless wantarray;
46              
47 200002         8592625 my @lcs = _core_loop($ctx,$a,$amin,$amax,$h);
48 200002 50       378148 if ($bmin > 0) {
49 200002         1179896 $_->[1] += $bmin for @lcs; # correct line numbers
50             }
51              
52 200002         1458029 map([$_ => $_], 0 .. ($amin-1)),
53             @lcs,
54             map([$_ => ++$bmax], ($amax+1) .. $#$a);
55             }
56              
57              
58             my $alg;
59              
60             sub LCSidx {
61 100001   66 100001 0 1728992 $alg //= Algorithm::LCS::XS->new;
62 100001         119902 my (@l, @r);
63 100001         167373 for my $arr ($alg->LCS(@_)) {
64 4000040         4293875 push @l, $$arr[0];
65 4000040         4582132 push @r, $$arr[1];
66             }
67 100001         1872393 return \@l, \@r;
68             }
69              
70             sub ADLCS {
71 100000   33 100000 0 2553850510 $alg //= Algorithm::LCS::XS->new;
72 100000         109877 my @rv;
73 100000         161765 for my $arr ($alg->LCS(@_)) {
74 4000000         5214081 $rv[$arr->[0]] = $_[1][$arr->[0]];
75             }
76 100000 50       1881739 return wantarray ? @rv : \@rv;
77             }
78              
79             1;
80              
81             __END__