NAME
Algorithm::Diff - Compute `intelligent' differences between two files / lists
SYNOPSIS
use Algorithm::Diff qw(diff sdiff LCS traverse_sequences
traverse_balanced);
@lcs = LCS( \@seq1, \@seq2 );
@lcs = LCS( \@seq1, \@seq2, $key_generation_function );
$lcsref = LCS( \@seq1, \@seq2 );
$lcsref = LCS( \@seq1, \@seq2, $key_generation_function );
@diffs = diff( \@seq1, \@seq2 );
@diffs = diff( \@seq1, \@seq2, $key_generation_function );
@sdiffs = sdiff( \@seq1, \@seq2 );
@sdiffs = sdiff( \@seq1, \@seq2, $key_generation_function );
traverse_sequences( \@seq1, \@seq2,
{ MATCH => $callback,
DISCARD_A => $callback,
DISCARD_B => $callback,
} );
traverse_sequences( \@seq1, \@seq2,
{ MATCH => $callback,
DISCARD_A => $callback,
DISCARD_B => $callback,
},
$key_generation_function );
traverse_balanced( \@seq1, \@seq2,
{ MATCH => $callback,
DISCARD_A => $callback,
DISCARD_B => $callback,
CHANGE => $callback,
} );
INTRODUCTION
(by Mark-Jason Dominus)
I once read an article written by the authors of diff
; they said that they hard worked very hard on the algorithm until they found the right one.
I think what they ended up using (and I hope someone will correct me, because I am not very confident about this) was the `longest common subsequence' method. in the LCS problem, you have two sequences of items:
a b c d f g h j q z
a b c d e f g i j k r x y z
and you want to find the longest sequence of items that is present in both original sequences in the same order. That is, you want to find a new sequence S which can be obtained from the first sequence by deleting some items, and from the secend sequence by deleting other items. You also want S to be as long as possible. In this case S is
a b c d f g j z
From there it's only a small step to get diff-like output:
e h i k q r x y
+ - + + - + + +
This module solves the LCS problem. It also includes a canned function to generate diff
-like output.
It might seem from the example above that the LCS of two sequences is always pretty obvious, but that's not always the case, especially when the two sequences have many repeated elements. For example, consider
a x b y c z p d q
a b c a x b y c z
A naive approach might start by matching up the a
and b
that appear at the beginning of each sequence, like this:
a x b y c z p d q
a b c a b y c z
This finds the common subsequence a b c z
. But actually, the LCS is a x b y c z
:
a x b y c z p d q
a b c a x b y c z
USAGE
This module provides three exportable functions, which we'll deal with in ascending order of difficulty: LCS
, diff
, sdiff
, traverse_sequences
, and traverse_balanced
.
LCS
Given references to two lists of items, LCS returns an array containing their longest common subsequence. In scalar context, it returns a reference to such a list.
@lcs = LCS( \@seq1, \@seq2 );
$lcsref = LCS( \@seq1, \@seq2 );
LCS
may be passed an optional third parameter; this is a CODE reference to a key generation function. See "KEY GENERATION FUNCTIONS".
@lcs = LCS( \@seq1, \@seq2, $keyGen );
$lcsref = LCS( \@seq1, \@seq2, $keyGen );
Additional parameters, if any, will be passed to the key generation routine.
diff
@diffs = diff( \@seq1, \@seq2 );
$diffs_ref = diff( \@seq1, \@seq2 );
diff
computes the smallest set of additions and deletions necessary to turn the first sequence into the second, and returns a description of these changes. The description is a list of hunks; each hunk represents a contiguous section of items which should be added, deleted, or replaced. The return value of diff
is a list of hunks, or, in scalar context, a reference to such a list.
Here is an example: The diff of the following two sequences:
a b c e h j l m n p
b c d e f j k l m r s t
Result:
[
[ [ '-', 0, 'a' ] ],
[ [ '+', 2, 'd' ] ],
[ [ '-', 4, 'h' ] ,
[ '+', 4, 'f' ] ],
[ [ '+', 6, 'k' ] ],
[ [ '-', 8, 'n' ],
[ '-', 9, 'p' ],
[ '+', 9, 'r' ],
[ '+', 10, 's' ],
[ '+', 11, 't' ],
]
]
There are five hunks here. The first hunk says that the a
at position 0 of the first sequence should be deleted (-
). The second hunk says that the d
at position 2 of the second sequence should be inserted (+
). The third hunk says that the h
at position 4 of the first sequence should be removed and replaced with the f
from position 4 of the second sequence. The other two hunks similarly.
diff
may be passed an optional third parameter; this is a CODE reference to a key generation function. See "KEY GENERATION FUNCTIONS".
Additional parameters, if any, will be passed to the key generation routine.
sdiff
@sdiffs = sdiff( \@seq1, \@seq2 );
$sdiffs_ref = sdiff( \@seq1, \@seq2 );
sdiff
computes all necessary components to show two sequences and their minimized differences side by side, just like the Unix-utility sdiff does:
same same
before | after
old < -
- > new
It returns a list of array refs, each pointing to an array of display instructions. In scalar context it returns a reference to such a list.
Display instructions consist of three elements: A modifier indicator (+
: Element added, -
: Element removed, u
: Element unmodified, c
: Element changed) and the value of the old and new elements, to be displayed side by side.
An sdiff
of the following two sequences:
a b c e h j l m n p
b c d e f j k l m r s t
results in
[ [ '-', 'a', '' ], [ 'u', 'b', 'b' ], [ 'u', 'c', 'c' ], [ '+', '', 'd' ], [ 'u', 'e', 'e' ], [ 'c', 'h', 'f' ], [ 'u', 'j', 'j' ], [ '+', '', 'k' ], [ 'u', 'l', 'l' ], [ 'u', 'm', 'm' ], [ 'c', 'n', 'r' ], [ 'c', 'p', 's' ], [ '+', '', 't' ] ]
sdiff
may be passed an optional third parameter; this is a CODE reference to a key generation function. See "KEY GENERATION FUNCTIONS".
Additional parameters, if any, will be passed to the key generation routine.
traverse_sequences
traverse_sequences
is the most general facility provided by this module; diff
and LCS
are implemented as calls to it.
Imagine that there are two arrows. Arrow A points to an element of sequence A, and arrow B points to an element of the sequence B. Initially, the arrows point to the first elements of the respective sequences. traverse_sequences
will advance the arrows through the sequences one element at a time, calling an appropriate user-specified callback function before each advance. It willadvance the arrows in such a way that if there are equal elements $A[$i]
and $B[$j]
which are equal and which are part of the LCS, there will be some moment during the execution of traverse_sequences
when arrow A is pointing to $A[$i]
and arrow B is pointing to $B[$j]
. When this happens, traverse_sequences
will call the MATCH
callback function and then it will advance both arrows.
Otherwise, one of the arrows is pointing to an element of its sequence that is not part of the LCS. traverse_sequences
will advance that arrow and will call the DISCARD_A
or the DISCARD_B
callback, depending on which arrow it advanced. If both arrows point to elements that are not part of the LCS, then traverse_sequences
will advance one of them and call the appropriate callback, but it is not specified which it will call.
The arguments to traverse_sequences
are the two sequences to traverse, and a hash which specifies the callback functions, like this:
traverse_sequences( \@seq1, \@seq2,
{ MATCH => $callback_1,
DISCARD_A => $callback_2,
DISCARD_B => $callback_3,
} );
Callbacks for MATCH, DISCARD_A, and DISCARD_B are invoked with at least the indices of the two arrows as their arguments. They are not expected to return any values. If a callback is omitted from the table, it is not called.
Callbacks for A_FINISHED and B_FINISHED are invoked with at least the corresponding index in A or B.
If arrow A reaches the end of its sequence, before arrow B does, traverse_sequences
will call the A_FINISHED
callback when it advances arrow B, if there is such a function; if not it will call DISCARD_B
instead. Similarly if arrow B finishes first. traverse_sequences
returns when both arrows are at the ends of their respective sequences. It returns true on success and false on failure. At present there is no way to fail.
traverse_sequences
may be passed an optional fourth parameter; this is a CODE reference to a key generation function. See "KEY GENERATION FUNCTIONS".
Additional parameters, if any, will be passed to the key generation function.
traverse_balanced
traverse_balanced
is an alternative to traverse_sequences
. It uses a different algorithm to iterate through the entries in the computed LCS. Instead of sticking to one side and showing element changes as insertions and deletions only, it will jump back and forth between the two sequences and report changes occurring as deletions on one side followed immediatly by an insertion on the other side.
In addition to the DISCARD_A
, DISCARD_B
, and MATCH
callbacks supported by traverse_sequences
, traverse_balanced
supports a CHANGE
callback indicating that one element got replaced
by another:
traverse_sequences( \@seq1, \@seq2,
{ MATCH => $callback_1,
DISCARD_A => $callback_2,
DISCARD_B => $callback_3,
CHANGE => $callback_4,
} );
If no CHANGE
callback is specified, traverse_balanced
will map CHANGE
events to DISCARD_A
and DISCARD_B
actions, therefore resulting in a similar behaviour as traverse_sequences
with different order of events.
traverse_balanced
might be a bit slower than traverse_sequences
, noticable only while processing huge amounts of data.
The sdiff
function of this module is implemented as call to traverse_balanced
.
KEY GENERATION FUNCTIONS
diff
, LCS
, and traverse_sequences
accept an optional last parameter. This is a CODE reference to a key generating (hashing) function that should return a string that uniquely identifies a given element. It should be the case that if two elements are to be considered equal, their keys should be the same (and the other way around). If no key generation function is provided, the key will be the element as a string.
By default, comparisons will use "eq" and elements will be turned into keys using the default stringizing operator '""'.
Where this is important is when you're comparing something other than strings. If it is the case that you have multiple different objects that should be considered to be equal, you should supply a key generation function. Otherwise, you have to make sure that your arrays contain unique references.
For instance, consider this example:
package Person;
sub new
{
my $package = shift;
return bless { name => '', ssn => '', @_ }, $package;
}
sub clone
{
my $old = shift;
my $new = bless { %$old }, ref($old);
}
sub hash
{
return shift()->{'ssn'};
}
my $person1 = Person->new( name => 'Joe', ssn => '123-45-6789' );
my $person2 = Person->new( name => 'Mary', ssn => '123-47-0000' );
my $person3 = Person->new( name => 'Pete', ssn => '999-45-2222' );
my $person4 = Person->new( name => 'Peggy', ssn => '123-45-9999' );
my $person5 = Person->new( name => 'Frank', ssn => '000-45-9999' );
If you did this:
my $array1 = [ $person1, $person2, $person4 ];
my $array2 = [ $person1, $person3, $person4, $person5 ];
Algorithm::Diff::diff( $array1, $array2 );
everything would work out OK (each of the objects would be converted into a string like "Person=HASH(0x82425b0)" for comparison).
But if you did this:
my $array1 = [ $person1, $person2, $person4 ];
my $array2 = [ $person1, $person3, $person4->clone(), $person5 ];
Algorithm::Diff::diff( $array1, $array2 );
$person4 and $person4->clone() (which have the same name and SSN) would be seen as different objects. If you wanted them to be considered equivalent, you would have to pass in a key generation function:
my $array1 = [ $person1, $person2, $person4 ];
my $array2 = [ $person1, $person3, $person4->clone(), $person5 ];
Algorithm::Diff::diff( $array1, $array2, \&Person::hash );
This would use the 'ssn' field in each Person as a comparison key, and so would consider $person4 and $person4->clone() as equal.
You may also pass additional parameters to the key generation function if you wish.
AUTHOR
This version by Ned Konz, perl@bike-nomad.com
LICENSE
Copyright (c) 2000-2002 Ned Konz. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
CREDITS
Versions through 0.59 (and much of this documentation) were written by:
Mark-Jason Dominus, mjd-perl-diff@plover.com
This version borrows the documentation and names of the routines from Mark-Jason's, but has all new code in Diff.pm.
This code was adapted from the Smalltalk code of Mario Wolczko <mario@wolczko.com>, which is available at ftp://st.cs.uiuc.edu/pub/Smalltalk/MANCHESTER/manchester/4.0/diff.st
sdiff
and traverse_balanced
were written by Mike Schilli <m@perlmeister.com>.
The algorithm is that described in A Fast Algorithm for Computing Longest Common Subsequences, CACM, vol.20, no.5, pp.350-353, May 1977, with a few minor improvements to improve the speed.