NAME

Algorithm::Diff - Compute `intelligent' differences between two files / lists


SYNOPSIS

  use Algorithm::Diff qw(diff LCS trverse_sequences);

  @lcs = LCS(\@seq1, \@seq2, $comparison_function);

  @diffs = diff(\@seq1, \@seq2, $comparison_function);
  
  traverse_sequences(\@seq1, \@seq2,
                     { MATCH => $callback,
                       DISCARD_A => $callback,
                       DISCARD_B => $callback,
                     },
                     $comparison_function);


INTRODUCTION

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 exports three functions, which we'll deal with in ascending order of difficulty: LCS, diff, and traverse_sequences.


LCS

Given references to two lists of items, LCS returns a list containing their longest common subsequence. In scalar context, it returns a reference to such a list.

  @lcs    = LCS(\@seq1, \@seq2, $comparison_function);
  $lcsref = LCS(\@seq1, \@seq2, $comparison_function);

$comparison_function, if supplied, should be a function that gets an item from each input list and returns true if they are considered equal. It is optional, and if omitted, defaults to `eq'.


diff

  @diffs     = diff(\@seq1, \@seq2, $comparison_function);
  $diffs_ref = diff(\@seq1, \@seq2, $comparison_function);

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 accepts an optional comparison function; if specified, it will be called with pairs of elements and is expected to return true if the elements are considered equal. If not specified, it defaults to eq.


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 callback which specifies the callback functions, like this:

  traverse_sequences(\@seq1, \@seq2,
                     { MATCH => $callback_1,
                       DISCARD_A => $callback_2,
                       DISCARD_B => $callback_3,
                     },
                    );

Callbacks 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.

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 accepts an optional comparison function; if specified, it will be called with pairs of elements and is expected to return true if the elements are considered equal. If not specified, or if undef, it defaults to eq.

Any additional arguments to travese_sequences are passed to the callback functions.

For examples of how to use this, see the code. the LCS and diff functions are implemented on top of traverse_sequences.


MAILING LIST

To join a low-volume mailing list for announcements related to diff and Algorithm::Diff, send an empty mail message to mjd-perl-diff-request@plover.com.


AUTHOR

Mark-Jason Dominus, mjd-perl-diff@plover.com.

Visit my diff/LCS web page at http://www.plover.com/~mjd/perl/diff/.