Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

diff: simplify LCS_matrix() #915

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions bin/diff
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,8 @@ package

use strict;

use List::Util qw(max);

our $VERSION = '0.57';

sub LCS_matrix {
Expand All @@ -626,23 +628,18 @@ sub LCS_matrix {
$al = @$a;
$bl = @$b;

my ($i, $j);

$x[0] = [(0) x ($bl+1)];
for ($i=1; $i<=$al; $i++) {
my $r = $x[$i] = [];
$r->[0] = 0;
for ($j=1; $j<=$bl; $j++) {
for my $i (1 .. $al) {
my $r = $x[$i] = [ 0 ];
for my $j (1 .. $bl) {
# If the first two items are the same...
if (defined $eq
? $eq->($a->[-$i], $b->[-$j])
: $a->[-$i] eq $b->[-$j]
) {
$r->[$j] = 1 + $x[$i-1][$j-1];
} else {
my $pi = $x[$i][$j-1];
my $pj = $x[$i-1][$j];
$r->[$j] = ($pi > $pj ? $pi : $pj);
$r->[$j] = max($x[$i][$j-1], $x[$i-1][$j]);
}
}
}
Expand Down
Loading