| 1 | # This is a version of Algorithm::Diff that uses only a comparison function, | ||
| 2 | # like versions <= 0.59 used to. | ||
| 3 | # $Revision: 1.3 $ | ||
| 4 | |||
| 5 | package # don't index | ||
| 6 | Algorithm::DiffOld; | ||
| 7 | use strict; | ||
| 8 | use vars qw($VERSION @EXPORT_OK @ISA @EXPORT); | ||
| 9 | use integer; # see below in _replaceNextLargerWith() for mod to make | ||
| 10 | # if you don't use this | ||
| 11 | require Exporter; | ||
| 12 | @ISA = qw(Exporter); | ||
| 13 | @EXPORT = qw(); | ||
| 14 | @EXPORT_OK = qw(LCS diff traverse_sequences); | ||
| 15 | $VERSION = 1.10; # manually tracking Algorithm::Diff | ||
| 16 | |||
| 17 | # McIlroy-Hunt diff algorithm | ||
| 18 | # Adapted from the Smalltalk code of Mario I. Wolczko, <mario@wolczko.com> | ||
| 19 | # by Ned Konz, perl@bike-nomad.com | ||
| 20 | |||
| 21 | =head1 NAME | ||
| 22 | |||
| 23 | Algorithm::DiffOld - Compute `intelligent' differences between two files / lists | ||
| 24 | but use the old (<=0.59) interface. | ||
| 25 | |||
| 26 | =head1 NOTE | ||
| 27 | |||
| 28 | This has been provided as part of the Algorithm::Diff package by Ned Konz. | ||
| 29 | This particular module is B<ONLY> for people who B<HAVE> to have the old | ||
| 30 | interface, which uses a comparison function rather than a key generating | ||
| 31 | function. | ||
| 32 | |||
| 33 | Because each of the lines in one array have to be compared with each | ||
| 34 | of the lines in the other array, this does M*N comparisons. This can | ||
| 35 | be very slow. I clocked it at taking 18 times as long as the stock | ||
| 36 | version of Algorithm::Diff for a 4000-line file. It will get worse | ||
| 37 | quadratically as array sizes increase. | ||
| 38 | |||
| 39 | =head1 SYNOPSIS | ||
| 40 | |||
| 41 | use Algorithm::DiffOld qw(diff LCS traverse_sequences); | ||
| 42 | |||
| 43 | @lcs = LCS( \@seq1, \@seq2, $comparison_function ); | ||
| 44 | |||
| 45 | $lcsref = LCS( \@seq1, \@seq2, $comparison_function ); | ||
| 46 | |||
| 47 | @diffs = diff( \@seq1, \@seq2, $comparison_function ); | ||
| 48 | |||
| 49 | traverse_sequences( \@seq1, \@seq2, | ||
| 50 | { MATCH => $callback, | ||
| 51 | DISCARD_A => $callback, | ||
| 52 | DISCARD_B => $callback, | ||
| 53 | }, | ||
| 54 | $comparison_function ); | ||
| 55 | |||
| 56 | =head1 COMPARISON FUNCTIONS | ||
| 57 | |||
| 58 | Each of the main routines should be passed a comparison function. If you | ||
| 59 | aren't passing one in, B<use Algorithm::Diff instead>. | ||
| 60 | |||
| 61 | These functions should return a true value when two items should compare | ||
| 62 | as equal. | ||
| 63 | |||
| 64 | For instance, | ||
| 65 | |||
| 66 | @lcs = LCS( \@seq1, \@seq2, sub { my ($a, $b) = @_; $a eq $b } ); | ||
| 67 | |||
| 68 | but if that is all you're doing with your comparison function, just use | ||
| 69 | Algorithm::Diff and let it do this (this is its default). | ||
| 70 | |||
| 71 | Or: | ||
| 72 | |||
| 73 | sub someFunkyComparisonFunction | ||
| 74 | { | ||
| 75 | my ($a, $b) = @_; | ||
| 76 | $a =~ m{$b}; | ||
| 77 | } | ||
| 78 | |||
| 79 | @diffs = diff( \@lines, \@patterns, \&someFunkyComparisonFunction ); | ||
| 80 | |||
| 81 | which would allow you to diff an array @lines which consists of text | ||
| 82 | lines with an array @patterns which consists of regular expressions. | ||
| 83 | |||
| 84 | This is actually the reason I wrote this version -- there is no way | ||
| 85 | to do this with a key generation function as in the stock Algorithm::Diff. | ||
| 86 | |||
| 87 | =cut | ||
| 88 | |||
| 89 | # Find the place at which aValue would normally be inserted into the array. If | ||
| 90 | # that place is already occupied by aValue, do nothing, and return undef. If | ||
| 91 | # the place does not exist (i.e., it is off the end of the array), add it to | ||
| 92 | # the end, otherwise replace the element at that point with aValue. | ||
| 93 | # It is assumed that the array's values are numeric. | ||
| 94 | # This is where the bulk (75%) of the time is spent in this module, so try to | ||
| 95 | # make it fast! | ||
| 96 | |||
| 97 | sub _replaceNextLargerWith | ||
| 98 | { | ||
| 99 | my ( $array, $aValue, $high ) = @_; | ||
| 100 | $high ||= $#$array; | ||
| 101 | |||
| 102 | # off the end? | ||
| 103 | if ( $high == -1 || $aValue > $array->[ -1 ] ) | ||
| 104 | { | ||
| 105 | push( @$array, $aValue ); | ||
| 106 | return $high + 1; | ||
| 107 | } | ||
| 108 | |||
| 109 | # binary search for insertion point... | ||
| 110 | my $low = 0; | ||
| 111 | my $index; | ||
| 112 | my $found; | ||
| 113 | while ( $low <= $high ) | ||
| 114 | { | ||
| 115 | $index = ( $high + $low ) / 2; | ||
| 116 | # $index = int(( $high + $low ) / 2); # without 'use integer' | ||
| 117 | $found = $array->[ $index ]; | ||
| 118 | |||
| 119 | if ( $aValue == $found ) | ||
| 120 | { | ||
| 121 | return undef; | ||
| 122 | } | ||
| 123 | elsif ( $aValue > $found ) | ||
| 124 | { | ||
| 125 | $low = $index + 1; | ||
| 126 | } | ||
| 127 | else | ||
| 128 | { | ||
| 129 | $high = $index - 1; | ||
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | # now insertion point is in $low. | ||
| 134 | $array->[ $low ] = $aValue; # overwrite next larger | ||
| 135 | return $low; | ||
| 136 | } | ||
| 137 | |||
| 138 | # This method computes the longest common subsequence in $a and $b. | ||
| 139 | |||
| 140 | # Result is array or ref, whose contents is such that | ||
| 141 | # $a->[ $i ] == $b->[ $result[ $i ] ] | ||
| 142 | # foreach $i in ( 0 .. $#result ) if $result[ $i ] is defined. | ||
| 143 | |||
| 144 | # An additional argument may be passed; this is a CODE ref to a comparison | ||
| 145 | # routine. By default, comparisons will use "eq" . | ||
| 146 | # Note that this routine will be called as many as M*N times, so make it fast! | ||
| 147 | |||
| 148 | # Additional parameters, if any, will be passed to the key generation routine. | ||
| 149 | |||
| 150 | sub _longestCommonSubsequence | ||
| 151 | { | ||
| 152 | my $a = shift; # array ref | ||
| 153 | my $b = shift; # array ref | ||
| 154 | my $compare = shift || sub { my $a = shift; my $b = shift; $a eq $b }; | ||
| 155 | |||
| 156 | my $aStart = 0; | ||
| 157 | my $aFinish = $#$a; | ||
| 158 | my $bStart = 0; | ||
| 159 | my $bFinish = $#$b; | ||
| 160 | my $matchVector = []; | ||
| 161 | |||
| 162 | # First we prune off any common elements at the beginning | ||
| 163 | while ( $aStart <= $aFinish | ||
| 164 | and $bStart <= $bFinish | ||
| 165 | and &$compare( $a->[ $aStart ], $b->[ $bStart ], @_ ) ) | ||
| 166 | { | ||
| 167 | $matchVector->[ $aStart++ ] = $bStart++; | ||
| 168 | } | ||
| 169 | |||
| 170 | # now the end | ||
| 171 | while ( $aStart <= $aFinish | ||
| 172 | and $bStart <= $bFinish | ||
| 173 | and &$compare( $a->[ $aFinish ], $b->[ $bFinish ], @_ ) ) | ||
| 174 | { | ||
| 175 | $matchVector->[ $aFinish-- ] = $bFinish--; | ||
| 176 | } | ||
| 177 | |||
| 178 | my $thresh = []; | ||
| 179 | my $links = []; | ||
| 180 | |||
| 181 | my ( $i, $ai, $j, $k ); | ||
| 182 | for ( $i = $aStart; $i <= $aFinish; $i++ ) | ||
| 183 | { | ||
| 184 | $k = 0; | ||
| 185 | # look for each element of @b between $bStart and $bFinish | ||
| 186 | # that matches $a->[ $i ], in reverse order | ||
| 187 | for ($j = $bFinish; $j >= $bStart; $j--) | ||
| 188 | { | ||
| 189 | next if ! &$compare( $a->[$i], $b->[$j], @_ ); | ||
| 190 | # optimization: most of the time this will be true | ||
| 191 | if ( $k | ||
| 192 | and $thresh->[ $k ] > $j | ||
| 193 | and $thresh->[ $k - 1 ] < $j ) | ||
| 194 | { | ||
| 195 | $thresh->[ $k ] = $j; | ||
| 196 | } | ||
| 197 | else | ||
| 198 | { | ||
| 199 | $k = _replaceNextLargerWith( $thresh, $j, $k ); | ||
| 200 | } | ||
| 201 | |||
| 202 | # oddly, it's faster to always test this (CPU cache?). | ||
| 203 | if ( defined( $k ) ) | ||
| 204 | { | ||
| 205 | $links->[ $k ] = | ||
| 206 | [ ( $k ? $links->[ $k - 1 ] : undef ), $i, $j ]; | ||
| 207 | } | ||
| 208 | } | ||
| 209 | } | ||
| 210 | |||
| 211 | if ( @$thresh ) | ||
| 212 | { | ||
| 213 | for ( my $link = $links->[ $#$thresh ]; $link; $link = $link->[ 0 ] ) | ||
| 214 | { | ||
| 215 | $matchVector->[ $link->[ 1 ] ] = $link->[ 2 ]; | ||
| 216 | } | ||
| 217 | } | ||
| 218 | |||
| 219 | return wantarray ? @$matchVector : $matchVector; | ||
| 220 | } | ||
| 221 | |||
| 222 | sub traverse_sequences | ||
| 223 | { | ||
| 224 | my $a = shift; # array ref | ||
| 225 | my $b = shift; # array ref | ||
| 226 | my $callbacks = shift || { }; | ||
| 227 | my $compare = shift; | ||
| 228 | my $matchCallback = $callbacks->{'MATCH'} || sub { }; | ||
| 229 | my $discardACallback = $callbacks->{'DISCARD_A'} || sub { }; | ||
| 230 | my $finishedACallback = $callbacks->{'A_FINISHED'}; | ||
| 231 | my $discardBCallback = $callbacks->{'DISCARD_B'} || sub { }; | ||
| 232 | my $finishedBCallback = $callbacks->{'B_FINISHED'}; | ||
| 233 | my $matchVector = _longestCommonSubsequence( $a, $b, $compare, @_ ); | ||
| 234 | # Process all the lines in match vector | ||
| 235 | my $lastA = $#$a; | ||
| 236 | my $lastB = $#$b; | ||
| 237 | my $bi = 0; | ||
| 238 | my $ai; | ||
| 239 | for ( $ai = 0; $ai <= $#$matchVector; $ai++ ) | ||
| 240 | { | ||
| 241 | my $bLine = $matchVector->[ $ai ]; | ||
| 242 | if ( defined( $bLine ) ) # matched | ||
| 243 | { | ||
| 244 | &$discardBCallback( $ai, $bi++, @_ ) while $bi < $bLine; | ||
| 245 | &$matchCallback( $ai, $bi++, @_ ); | ||
| 246 | } | ||
| 247 | else | ||
| 248 | { | ||
| 249 | &$discardACallback( $ai, $bi, @_ ); | ||
| 250 | } | ||
| 251 | } | ||
| 252 | # the last entry (if any) processed was a match. | ||
| 253 | |||
| 254 | if ( defined( $finishedBCallback ) && $ai <= $lastA ) | ||
| 255 | { | ||
| 256 | &$finishedBCallback( $bi, @_ ); | ||
| 257 | } | ||
| 258 | else | ||
| 259 | { | ||
| 260 | &$discardACallback( $ai++, $bi, @_ ) while ( $ai <= $lastA ); | ||
| 261 | } | ||
| 262 | |||
| 263 | if ( defined( $finishedACallback ) && $bi <= $lastB ) | ||
| 264 | { | ||
| 265 | &$finishedACallback( $ai, @_ ); | ||
| 266 | } | ||
| 267 | else | ||
| 268 | { | ||
| 269 | &$discardBCallback( $ai, $bi++, @_ ) while ( $bi <= $lastB ); | ||
| 270 | } | ||
| 271 | return 1; | ||
| 272 | } | ||
| 273 | |||
| 274 | sub LCS | ||
| 275 | { | ||
| 276 | my $a = shift; # array ref | ||
| 277 | my $matchVector = _longestCommonSubsequence( $a, @_ ); | ||
| 278 | my @retval; | ||
| 279 | my $i; | ||
| 280 | for ( $i = 0; $i <= $#$matchVector; $i++ ) | ||
| 281 | { | ||
| 282 | if ( defined( $matchVector->[ $i ] ) ) | ||
| 283 | { | ||
| 284 | push( @retval, $a->[ $i ] ); | ||
| 285 | } | ||
| 286 | } | ||
| 287 | return wantarray ? @retval : \@retval; | ||
| 288 | } | ||
| 289 | |||
| 290 | sub diff | ||
| 291 | { | ||
| 292 | my $a = shift; # array ref | ||
| 293 | my $b = shift; # array ref | ||
| 294 | my $retval = []; | ||
| 295 | my $hunk = []; | ||
| 296 | my $discard = sub { push( @$hunk, [ '-', $_[ 0 ], $a->[ $_[ 0 ] ] ] ) }; | ||
| 297 | my $add = sub { push( @$hunk, [ '+', $_[ 1 ], $b->[ $_[ 1 ] ] ] ) }; | ||
| 298 | my $match = sub { push( @$retval, $hunk ) if scalar(@$hunk); $hunk = [] }; | ||
| 299 | traverse_sequences( $a, $b, | ||
| 300 | { MATCH => $match, DISCARD_A => $discard, DISCARD_B => $add }, | ||
| 301 | @_ ); | ||
| 302 | &$match(); | ||
| 303 | return wantarray ? @$retval : $retval; | ||
| 304 | } | ||
| 305 | |||
| 306 | 1; |