Server IP : 103.119.228.120 / Your IP : 3.138.122.90 Web Server : Apache System : Linux v8.techscape8.com 3.10.0-1160.119.1.el7.tuxcare.els2.x86_64 #1 SMP Mon Jul 15 12:09:18 UTC 2024 x86_64 User : nobody ( 99) PHP Version : 5.6.40 Disable Function : shell_exec,symlink,system,exec,proc_get_status,proc_nice,proc_terminate,define_syslog_variables,syslog,openlog,closelog,escapeshellcmd,passthru,ocinum cols,ini_alter,leak,listen,chgrp,apache_note,apache_setenv,debugger_on,debugger_off,ftp_exec,dl,dll,myshellexec,proc_open,socket_bind,proc_close,escapeshellarg,parse_ini_filepopen,fpassthru,exec,passthru,escapeshellarg,escapeshellcmd,proc_close,proc_open,ini_alter,popen,show_source,proc_nice,proc_terminate,proc_get_status,proc_close,pfsockopen,leak,apache_child_terminate,posix_kill,posix_mkfifo,posix_setpgid,posix_setsid,posix_setuid,dl,symlink,shell_exec,system,dl,passthru,escapeshellarg,escapeshellcmd,myshellexec,c99_buff_prepare,c99_sess_put,fpassthru,getdisfunc,fx29exec,fx29exec2,is_windows,disp_freespace,fx29sh_getupdate,fx29_buff_prepare,fx29_sess_put,fx29shexit,fx29fsearch,fx29ftpbrutecheck,fx29sh_tools,fx29sh_about,milw0rm,imagez,sh_name,myshellexec,checkproxyhost,dosyayicek,c99_buff_prepare,c99_sess_put,c99getsource,c99sh_getupdate,c99fsearch,c99shexit,view_perms,posix_getpwuid,posix_getgrgid,posix_kill,parse_perms,parsesort,view_perms_color,set_encoder_input,ls_setcheckboxall,ls_reverse_all,rsg_read,rsg_glob,selfURL,dispsecinfo,unix2DosTime,addFile,system,get_users,view_size,DirFiles,DirFilesWide,DirPrintHTMLHeaders,GetFilesTotal,GetTitles,GetTimeTotal,GetMatchesCount,GetFileMatchesCount,GetResultFiles,fs_copy_dir,fs_copy_obj,fs_move_dir,fs_move_obj,fs_rmdir,SearchText,getmicrotime MySQL : ON | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /usr/local/share/man/man3/ |
Upload File : |
.\" Automatically generated by Pod::Man 2.27 (Pod::Simple 3.28) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{ . if \nF \{ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Algorithm::Diff 3" .TH Algorithm::Diff 3 "2014-11-26" "perl v5.16.3" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Algorithm::Diff \- Compute `intelligent' differences between two files / lists .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& require Algorithm::Diff; \& \& # This example produces traditional \*(Aqdiff\*(Aq output: \& \& my $diff = Algorithm::Diff\->new( \e@seq1, \e@seq2 ); \& \& $diff\->Base( 1 ); # Return line numbers, not indices \& while( $diff\->Next() ) { \& next if $diff\->Same(); \& my $sep = \*(Aq\*(Aq; \& if( ! $diff\->Items(2) ) { \& printf "%d,%dd%d\en", \& $diff\->Get(qw( Min1 Max1 Max2 )); \& } elsif( ! $diff\->Items(1) ) { \& printf "%da%d,%d\en", \& $diff\->Get(qw( Max1 Min2 Max2 )); \& } else { \& $sep = "\-\-\-\en"; \& printf "%d,%dc%d,%d\en", \& $diff\->Get(qw( Min1 Max1 Min2 Max2 )); \& } \& print "< $_" for $diff\->Items(1); \& print $sep; \& print "> $_" for $diff\->Items(2); \& } \& \& \& # Alternate interfaces: \& \& use Algorithm::Diff qw( \& LCS LCS_length LCSidx \& diff sdiff compact_diff \& traverse_sequences traverse_balanced ); \& \& @lcs = LCS( \e@seq1, \e@seq2 ); \& $lcsref = LCS( \e@seq1, \e@seq2 ); \& $count = LCS_length( \e@seq1, \e@seq2 ); \& \& ( $seq1idxref, $seq2idxref ) = LCSidx( \e@seq1, \e@seq2 ); \& \& \& # Complicated interfaces: \& \& @diffs = diff( \e@seq1, \e@seq2 ); \& \& @sdiffs = sdiff( \e@seq1, \e@seq2 ); \& \& @cdiffs = compact_diff( \e@seq1, \e@seq2 ); \& \& traverse_sequences( \& \e@seq1, \& \e@seq2, \& { MATCH => \e&callback1, \& DISCARD_A => \e&callback2, \& DISCARD_B => \e&callback3, \& }, \& \e&key_generator, \& @extra_args, \& ); \& \& traverse_balanced( \& \e@seq1, \& \e@seq2, \& { MATCH => \e&callback1, \& DISCARD_A => \e&callback2, \& DISCARD_B => \e&callback3, \& CHANGE => \e&callback4, \& }, \& \e&key_generator, \& @extra_args, \& ); .Ve .SH "INTRODUCTION" .IX Header "INTRODUCTION" (by Mark-Jason Dominus) .PP I once read an article written by the authors of \f(CW\*(C`diff\*(C'\fR; they said that they worked very hard on the algorithm until they found the right one. .PP 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 \s-1LCS\s0 problem, you have two sequences of items: .PP .Vb 1 \& a b c d f g h j q z \& \& a b c d e f g i j k r x y z .Ve .PP 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 \fIS\fR which can be obtained from the first sequence by deleting some items, and from the second sequence by deleting other items. You also want \fIS\fR to be as long as possible. In this case \fIS\fR is .PP .Vb 1 \& a b c d f g j z .Ve .PP From there it's only a small step to get diff-like output: .PP .Vb 2 \& e h i k q r x y \& + \- + + \- + + + .Ve .PP This module solves the \s-1LCS\s0 problem. It also includes a canned function to generate \f(CW\*(C`diff\*(C'\fR\-like output. .PP It might seem from the example above that the \s-1LCS\s0 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 .PP .Vb 2 \& a x b y c z p d q \& a b c a x b y c z .Ve .PP A naive approach might start by matching up the \f(CW\*(C`a\*(C'\fR and \f(CW\*(C`b\*(C'\fR that appear at the beginning of each sequence, like this: .PP .Vb 2 \& a x b y c z p d q \& a b c a b y c z .Ve .PP This finds the common subsequence \f(CW\*(C`a b c z\*(C'\fR. But actually, the \s-1LCS\s0 is \f(CW\*(C`a x b y c z\*(C'\fR: .PP .Vb 2 \& a x b y c z p d q \& a b c a x b y c z .Ve .PP or .PP .Vb 2 \& a x b y c z p d q \& a b c a x b y c z .Ve .SH "USAGE" .IX Header "USAGE" (See also the \s-1README\s0 file and several example scripts include with this module.) .PP This module now provides an object-oriented interface that uses less memory and is easier to use than most of the previous procedural interfaces. It also still provides several exportable functions. We'll deal with these in ascending order of difficulty: \f(CW\*(C`LCS\*(C'\fR, \&\f(CW\*(C`LCS_length\*(C'\fR, \f(CW\*(C`LCSidx\*(C'\fR, \s-1OO\s0 interface, \f(CW\*(C`prepare\*(C'\fR, \f(CW\*(C`diff\*(C'\fR, \f(CW\*(C`sdiff\*(C'\fR, \&\f(CW\*(C`traverse_sequences\*(C'\fR, and \f(CW\*(C`traverse_balanced\*(C'\fR. .ie n .SS """LCS""" .el .SS "\f(CWLCS\fP" .IX Subsection "LCS" Given references to two lists of items, \s-1LCS\s0 returns an array containing their longest common subsequence. In scalar context, it returns a reference to such a list. .PP .Vb 2 \& @lcs = LCS( \e@seq1, \e@seq2 ); \& $lcsref = LCS( \e@seq1, \e@seq2 ); .Ve .PP \&\f(CW\*(C`LCS\*(C'\fR may be passed an optional third parameter; this is a \s-1CODE\s0 reference to a key generation function. See \*(L"\s-1KEY GENERATION FUNCTIONS\*(R"\s0. .PP .Vb 2 \& @lcs = LCS( \e@seq1, \e@seq2, \e&keyGen, @args ); \& $lcsref = LCS( \e@seq1, \e@seq2, \e&keyGen, @args ); .Ve .PP Additional parameters, if any, will be passed to the key generation routine. .ie n .SS """LCS_length""" .el .SS "\f(CWLCS_length\fP" .IX Subsection "LCS_length" This is just like \f(CW\*(C`LCS\*(C'\fR except it only returns the length of the longest common subsequence. This provides a performance gain of about 9% compared to \f(CW\*(C`LCS\*(C'\fR. .ie n .SS """LCSidx""" .el .SS "\f(CWLCSidx\fP" .IX Subsection "LCSidx" Like \f(CW\*(C`LCS\*(C'\fR except it returns references to two arrays. The first array contains the indices into \f(CW@seq1\fR where the \s-1LCS\s0 items are located. The second array contains the indices into \f(CW@seq2\fR where the \s-1LCS\s0 items are located. .PP Therefore, the following three lists will contain the same values: .PP .Vb 4 \& my( $idx1, $idx2 ) = LCSidx( \e@seq1, \e@seq2 ); \& my @list1 = @seq1[ @$idx1 ]; \& my @list2 = @seq2[ @$idx2 ]; \& my @list3 = LCS( \e@seq1, \e@seq2 ); .Ve .ie n .SS """new""" .el .SS "\f(CWnew\fP" .IX Subsection "new" .Vb 2 \& $diff = Algorithm::Diffs\->new( \e@seq1, \e@seq2 ); \& $diff = Algorithm::Diffs\->new( \e@seq1, \e@seq2, \e%opts ); .Ve .PP \&\f(CW\*(C`new\*(C'\fR computes the smallest set of additions and deletions necessary to turn the first sequence into the second and compactly records them in the object. .PP You use the object to iterate over \fIhunks\fR, where each hunk represents a contiguous section of items which should be added, deleted, replaced, or left unchanged. .Sp .RS 4 The following summary of all of the methods looks a lot like Perl code but some of the symbols have different meanings: .Sp .Vb 3 \& [ ] Encloses optional arguments \& : Is followed by the default value for an optional argument \& | Separates alternate return results .Ve .Sp Method summary: .Sp .Vb 6 \& $obj = Algorithm::Diff\->new( \e@seq1, \e@seq2, [ \e%opts ] ); \& $pos = $obj\->Next( [ $count : 1 ] ); \& $revPos = $obj\->Prev( [ $count : 1 ] ); \& $obj = $obj\->Reset( [ $pos : 0 ] ); \& $copy = $obj\->Copy( [ $pos, [ $newBase ] ] ); \& $oldBase = $obj\->Base( [ $newBase ] ); .Ve .Sp Note that all of the following methods \f(CW\*(C`die\*(C'\fR if used on an object that is \*(L"reset\*(R" (not currently pointing at any hunk). .Sp .Vb 7 \& $bits = $obj\->Diff( ); \& @items|$cnt = $obj\->Same( ); \& @items|$cnt = $obj\->Items( $seqNum ); \& @idxs |$cnt = $obj\->Range( $seqNum, [ $base ] ); \& $minIdx = $obj\->Min( $seqNum, [ $base ] ); \& $maxIdx = $obj\->Max( $seqNum, [ $base ] ); \& @values = $obj\->Get( @names ); .Ve .Sp Passing in \f(CW\*(C`undef\*(C'\fR for an optional argument is always treated the same as if no argument were passed in. .Sp \&\f(CW\*(C`Next\*(C'\fR .Sp .Vb 3 \& $pos = $diff\->Next(); # Move forward 1 hunk \& $pos = $diff\->Next( 2 ); # Move forward 2 hunks \& $pos = $diff\->Next(\-5); # Move backward 5 hunks .Ve .Sp \&\f(CW\*(C`Next\*(C'\fR moves the object to point at the next hunk. The object starts out \*(L"reset\*(R", which means it isn't pointing at any hunk. If the object is reset, then \f(CW\*(C`Next()\*(C'\fR moves to the first hunk. .Sp \&\f(CW\*(C`Next\*(C'\fR returns a true value iff the move didn't go past the last hunk. So \f(CWNext(0)\fR will return true iff the object is not reset. .Sp Actually, \f(CW\*(C`Next\*(C'\fR returns the object's new position, which is a number between 1 and the number of hunks (inclusive), or returns a false value. .Sp \&\f(CW\*(C`Prev\*(C'\fR .Sp \&\f(CW\*(C`Prev($N)\*(C'\fR is almost identical to \f(CW\*(C`Next(\-$N)\*(C'\fR; it moves to the \f(CW$Nth\fR previous hunk. On a 'reset' object, \f(CW\*(C`Prev()\*(C'\fR [and \f(CW\*(C`Next(\-1)\*(C'\fR] move to the last hunk. .Sp The position returned by \f(CW\*(C`Prev\*(C'\fR is relative to the \fIend\fR of the hunks; \-1 for the last hunk, \-2 for the second-to-last, etc. .Sp \&\f(CW\*(C`Reset\*(C'\fR .Sp .Vb 4 \& $diff\->Reset(); # Reset the object\*(Aqs position \& $diff\->Reset($pos); # Move to the specified hunk \& $diff\->Reset(1); # Move to the first hunk \& $diff\->Reset(\-1); # Move to the last hunk .Ve .Sp \&\f(CW\*(C`Reset\*(C'\fR returns the object, so, for example, you could use \&\f(CW\*(C`$diff\->Reset()\->Next(\-1)\*(C'\fR to get the number of hunks. .Sp \&\f(CW\*(C`Copy\*(C'\fR .Sp .Vb 1 \& $copy = $diff\->Copy( $newPos, $newBase ); .Ve .Sp \&\f(CW\*(C`Copy\*(C'\fR returns a copy of the object. The copy and the original object share most of their data, so making copies takes very little memory. The copy maintains its own position (separate from the original), which is the main purpose of copies. It also maintains its own base. .Sp By default, the copy's position starts out the same as the original object's position. But \f(CW\*(C`Copy\*(C'\fR takes an optional first argument to set the new position, so the following three snippets are equivalent: .Sp .Vb 1 \& $copy = $diff\->Copy($pos); \& \& $copy = $diff\->Copy(); \& $copy\->Reset($pos); \& \& $copy = $diff\->Copy()\->Reset($pos); .Ve .Sp \&\f(CW\*(C`Copy\*(C'\fR takes an optional second argument to set the base for the copy. If you wish to change the base of the copy but leave the position the same as in the original, here are two equivalent ways: .Sp .Vb 2 \& $copy = $diff\->Copy(); \& $copy\->Base( 0 ); \& \& $copy = $diff\->Copy(undef,0); .Ve .Sp Here are two equivalent way to get a \*(L"reset\*(R" copy: .Sp .Vb 1 \& $copy = $diff\->Copy(0); \& \& $copy = $diff\->Copy()\->Reset(); .Ve .Sp \&\f(CW\*(C`Diff\*(C'\fR .Sp .Vb 1 \& $bits = $obj\->Diff(); .Ve .Sp \&\f(CW\*(C`Diff\*(C'\fR returns a true value iff the current hunk contains items that are different between the two sequences. It actually returns one of the follow 4 values: .IP "3" 4 .IX Item "3" \&\f(CW\*(C`3==(1|2)\*(C'\fR. This hunk contains items from \f(CW@seq1\fR and the items from \f(CW@seq2\fR that should replace them. Both sequence 1 and 2 contain changed items so both the 1 and 2 bits are set. .IP "2" 4 .IX Item "2" This hunk only contains items from \f(CW@seq2\fR that should be inserted (not items from \f(CW@seq1\fR). Only sequence 2 contains changed items so only the 2 bit is set. .IP "1" 4 .IX Item "1" This hunk only contains items from \f(CW@seq1\fR that should be deleted (not items from \f(CW@seq2\fR). Only sequence 1 contains changed items so only the 1 bit is set. .IP "0" 4 This means that the items in this hunk are the same in both sequences. Neither sequence 1 nor 2 contain changed items so neither the 1 nor the 2 bits are set. .RE .RS 4 .Sp \&\f(CW\*(C`Same\*(C'\fR .Sp \&\f(CW\*(C`Same\*(C'\fR returns a true value iff the current hunk contains items that are the same in both sequences. It actually returns the list of items if they are the same or an empty list if they aren't. In a scalar context, it returns the size of the list. .Sp \&\f(CW\*(C`Items\*(C'\fR .Sp .Vb 2 \& $count = $diff\->Items(2); \& @items = $diff\->Items($seqNum); .Ve .Sp \&\f(CW\*(C`Items\*(C'\fR returns the (number of) items from the specified sequence that are part of the current hunk. .Sp If the current hunk contains only insertions, then \&\f(CW\*(C`$diff\->Items(1)\*(C'\fR will return an empty list (0 in a scalar context). If the current hunk contains only deletions, then \f(CW\*(C`$diff\->Items(2)\*(C'\fR will return an empty list (0 in a scalar context). .Sp If the hunk contains replacements, then both \f(CW\*(C`$diff\->Items(1)\*(C'\fR and \&\f(CW\*(C`$diff\->Items(2)\*(C'\fR will return different, non-empty lists. .Sp Otherwise, the hunk contains identical items and all of the following will return the same lists: .Sp .Vb 3 \& @items = $diff\->Items(1); \& @items = $diff\->Items(2); \& @items = $diff\->Same(); .Ve .Sp \&\f(CW\*(C`Range\*(C'\fR .Sp .Vb 3 \& $count = $diff\->Range( $seqNum ); \& @indices = $diff\->Range( $seqNum ); \& @indices = $diff\->Range( $seqNum, $base ); .Ve .Sp \&\f(CW\*(C`Range\*(C'\fR is like \f(CW\*(C`Items\*(C'\fR except that it returns a list of \fIindices\fR to the items rather than the items themselves. By default, the index of the first item (in each sequence) is 0 but this can be changed by calling the \f(CW\*(C`Base\*(C'\fR method. So, by default, the following two snippets return the same lists: .Sp .Vb 2 \& @list = $diff\->Items(2); \& @list = @seq2[ $diff\->Range(2) ]; .Ve .Sp You can also specify the base to use as the second argument. So the following two snippets \fIalways\fR return the same lists: .Sp .Vb 2 \& @list = $diff\->Items(1); \& @list = @seq1[ $diff\->Range(1,0) ]; .Ve .Sp \&\f(CW\*(C`Base\*(C'\fR .Sp .Vb 2 \& $curBase = $diff\->Base(); \& $oldBase = $diff\->Base($newBase); .Ve .Sp \&\f(CW\*(C`Base\*(C'\fR sets and/or returns the current base (usually 0 or 1) that is used when you request range information. The base defaults to 0 so that range information is returned as array indices. You can set the base to 1 if you want to report traditional line numbers instead. .Sp \&\f(CW\*(C`Min\*(C'\fR .Sp .Vb 2 \& $min1 = $diff\->Min(1); \& $min = $diff\->Min( $seqNum, $base ); .Ve .Sp \&\f(CW\*(C`Min\*(C'\fR returns the first value that \f(CW\*(C`Range\*(C'\fR would return (given the same arguments) or returns \f(CW\*(C`undef\*(C'\fR if \f(CW\*(C`Range\*(C'\fR would return an empty list. .Sp \&\f(CW\*(C`Max\*(C'\fR .Sp \&\f(CW\*(C`Max\*(C'\fR returns the last value that \f(CW\*(C`Range\*(C'\fR would return or \f(CW\*(C`undef\*(C'\fR. .Sp \&\f(CW\*(C`Get\*(C'\fR .Sp .Vb 2 \& ( $n, $x, $r ) = $diff\->Get(qw( min1 max1 range1 )); \& @values = $diff\->Get(qw( 0min2 1max2 range2 same base )); .Ve .Sp \&\f(CW\*(C`Get\*(C'\fR returns one or more scalar values. You pass in a list of the names of the values you want returned. Each name must match one of the following regexes: .Sp .Vb 2 \& /^(\-?\ed+)?(min|max)[12]$/i \& /^(range[12]|same|diff|base)$/i .Ve .Sp The 1 or 2 after a name says which sequence you want the information for (and where allowed, it is required). The optional number before \&\*(L"min\*(R" or \*(L"max\*(R" is the base to use. So the following equalities hold: .Sp .Vb 2 \& $diff\->Get(\*(Aqmin1\*(Aq) == $diff\->Min(1) \& $diff\->Get(\*(Aq0min2\*(Aq) == $diff\->Min(2,0) .Ve .Sp Using \f(CW\*(C`Get\*(C'\fR in a scalar context when you've passed in more than one name is a fatal error (\f(CW\*(C`die\*(C'\fR is called). .RE .ie n .SS """prepare""" .el .SS "\f(CWprepare\fP" .IX Subsection "prepare" Given a reference to a list of items, \f(CW\*(C`prepare\*(C'\fR returns a reference to a hash which can be used when comparing this sequence to other sequences with \f(CW\*(C`LCS\*(C'\fR or \f(CW\*(C`LCS_length\*(C'\fR. .PP .Vb 6 \& $prep = prepare( \e@seq1 ); \& for $i ( 0 .. 10_000 ) \& { \& @lcs = LCS( $prep, $seq[$i] ); \& # do something useful with @lcs \& } .Ve .PP \&\f(CW\*(C`prepare\*(C'\fR may be passed an optional third parameter; this is a \s-1CODE\s0 reference to a key generation function. See \*(L"\s-1KEY GENERATION FUNCTIONS\*(R"\s0. .PP .Vb 6 \& $prep = prepare( \e@seq1, \e&keyGen ); \& for $i ( 0 .. 10_000 ) \& { \& @lcs = LCS( $seq[$i], $prep, \e&keyGen ); \& # do something useful with @lcs \& } .Ve .PP Using \f(CW\*(C`prepare\*(C'\fR provides a performance gain of about 50% when calling \s-1LCS\s0 many times compared with not preparing. .ie n .SS """diff""" .el .SS "\f(CWdiff\fP" .IX Subsection "diff" .Vb 2 \& @diffs = diff( \e@seq1, \e@seq2 ); \& $diffs_ref = diff( \e@seq1, \e@seq2 ); .Ve .PP \&\f(CW\*(C`diff\*(C'\fR 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 \fIhunks\fR; each hunk represents a contiguous section of items which should be added, deleted, or replaced. (Hunks containing unchanged items are not included.) .PP The return value of \f(CW\*(C`diff\*(C'\fR is a list of hunks, or, in scalar context, a reference to such a list. If there are no differences, the list will be empty. .PP Here is an example. Calling \f(CW\*(C`diff\*(C'\fR for the following two sequences: .PP .Vb 2 \& a b c e h j l m n p \& b c d e f j k l m r s t .Ve .PP would produce the following list: .PP .Vb 2 \& ( \& [ [ \*(Aq\-\*(Aq, 0, \*(Aqa\*(Aq ] ], \& \& [ [ \*(Aq+\*(Aq, 2, \*(Aqd\*(Aq ] ], \& \& [ [ \*(Aq\-\*(Aq, 4, \*(Aqh\*(Aq ], \& [ \*(Aq+\*(Aq, 4, \*(Aqf\*(Aq ] ], \& \& [ [ \*(Aq+\*(Aq, 6, \*(Aqk\*(Aq ] ], \& \& [ [ \*(Aq\-\*(Aq, 8, \*(Aqn\*(Aq ], \& [ \*(Aq\-\*(Aq, 9, \*(Aqp\*(Aq ], \& [ \*(Aq+\*(Aq, 9, \*(Aqr\*(Aq ], \& [ \*(Aq+\*(Aq, 10, \*(Aqs\*(Aq ], \& [ \*(Aq+\*(Aq, 11, \*(Aqt\*(Aq ] ], \& ) .Ve .PP There are five hunks here. The first hunk says that the \f(CW\*(C`a\*(C'\fR at position 0 of the first sequence should be deleted (\f(CW\*(C`\-\*(C'\fR). The second hunk says that the \f(CW\*(C`d\*(C'\fR at position 2 of the second sequence should be inserted (\f(CW\*(C`+\*(C'\fR). The third hunk says that the \f(CW\*(C`h\*(C'\fR at position 4 of the first sequence should be removed and replaced with the \f(CW\*(C`f\*(C'\fR from position 4 of the second sequence. And so on. .PP \&\f(CW\*(C`diff\*(C'\fR may be passed an optional third parameter; this is a \s-1CODE\s0 reference to a key generation function. See \*(L"\s-1KEY GENERATION FUNCTIONS\*(R"\s0. .PP Additional parameters, if any, will be passed to the key generation routine. .ie n .SS """sdiff""" .el .SS "\f(CWsdiff\fP" .IX Subsection "sdiff" .Vb 2 \& @sdiffs = sdiff( \e@seq1, \e@seq2 ); \& $sdiffs_ref = sdiff( \e@seq1, \e@seq2 ); .Ve .PP \&\f(CW\*(C`sdiff\*(C'\fR computes all necessary components to show two sequences and their minimized differences side by side, just like the Unix-utility \fIsdiff\fR does: .PP .Vb 4 \& same same \& before | after \& old < \- \& \- > new .Ve .PP 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. If there are no differences, the list will have one entry per item, each indicating that the item was unchanged. .PP Display instructions consist of three elements: A modifier indicator (\f(CW\*(C`+\*(C'\fR: Element added, \f(CW\*(C`\-\*(C'\fR: Element removed, \f(CW\*(C`u\*(C'\fR: Element unmodified, \&\f(CW\*(C`c\*(C'\fR: Element changed) and the value of the old and new elements, to be displayed side-by-side. .PP An \f(CW\*(C`sdiff\*(C'\fR of the following two sequences: .PP .Vb 2 \& a b c e h j l m n p \& b c d e f j k l m r s t .Ve .PP results in .PP .Vb 10 \& ( [ \*(Aq\-\*(Aq, \*(Aqa\*(Aq, \*(Aq\*(Aq ], \& [ \*(Aqu\*(Aq, \*(Aqb\*(Aq, \*(Aqb\*(Aq ], \& [ \*(Aqu\*(Aq, \*(Aqc\*(Aq, \*(Aqc\*(Aq ], \& [ \*(Aq+\*(Aq, \*(Aq\*(Aq, \*(Aqd\*(Aq ], \& [ \*(Aqu\*(Aq, \*(Aqe\*(Aq, \*(Aqe\*(Aq ], \& [ \*(Aqc\*(Aq, \*(Aqh\*(Aq, \*(Aqf\*(Aq ], \& [ \*(Aqu\*(Aq, \*(Aqj\*(Aq, \*(Aqj\*(Aq ], \& [ \*(Aq+\*(Aq, \*(Aq\*(Aq, \*(Aqk\*(Aq ], \& [ \*(Aqu\*(Aq, \*(Aql\*(Aq, \*(Aql\*(Aq ], \& [ \*(Aqu\*(Aq, \*(Aqm\*(Aq, \*(Aqm\*(Aq ], \& [ \*(Aqc\*(Aq, \*(Aqn\*(Aq, \*(Aqr\*(Aq ], \& [ \*(Aqc\*(Aq, \*(Aqp\*(Aq, \*(Aqs\*(Aq ], \& [ \*(Aq+\*(Aq, \*(Aq\*(Aq, \*(Aqt\*(Aq ], \& ) .Ve .PP \&\f(CW\*(C`sdiff\*(C'\fR may be passed an optional third parameter; this is a \s-1CODE\s0 reference to a key generation function. See \*(L"\s-1KEY GENERATION FUNCTIONS\*(R"\s0. .PP Additional parameters, if any, will be passed to the key generation routine. .ie n .SS """compact_diff""" .el .SS "\f(CWcompact_diff\fP" .IX Subsection "compact_diff" \&\f(CW\*(C`compact_diff\*(C'\fR is much like \f(CW\*(C`sdiff\*(C'\fR except it returns a much more compact description consisting of just one flat list of indices. An example helps explain the format: .PP .Vb 10 \& my @a = qw( a b c e h j l m n p ); \& my @b = qw( b c d e f j k l m r s t ); \& @cdiff = compact_diff( \e@a, \e@b ); \& # Returns: \& # @a @b @a @b \& # start start values values \& ( 0, 0, # = \& 0, 0, # a ! \& 1, 0, # b c = b c \& 3, 2, # ! d \& 3, 3, # e = e \& 4, 4, # f ! h \& 5, 5, # j = j \& 6, 6, # ! k \& 6, 7, # l m = l m \& 8, 9, # n p ! r s t \& 10, 12, # \& ); .Ve .PP The 0th, 2nd, 4th, etc. entries are all indices into \f(CW@seq1\fR (@a in the above example) indicating where a hunk begins. The 1st, 3rd, 5th, etc. entries are all indices into \f(CW@seq2\fR (@b in the above example) indicating where the same hunk begins. .PP So each pair of indices (except the last pair) describes where a hunk begins (in each sequence). Since each hunk must end at the item just before the item that starts the next hunk, the next pair of indices can be used to determine where the hunk ends. .PP So, the first 4 entries (0..3) describe the first hunk. Entries 0 and 1 describe where the first hunk begins (and so are always both 0). Entries 2 and 3 describe where the next hunk begins, so subtracting 1 from each tells us where the first hunk ends. That is, the first hunk contains items \f(CW$diff[0]\fR through \f(CW\*(C`$diff[2] \- 1\*(C'\fR of the first sequence and contains items \f(CW$diff[1]\fR through \f(CW\*(C`$diff[3] \- 1\*(C'\fR of the second sequence. .PP In other words, the first hunk consists of the following two lists of items: .PP .Vb 5 \& # 1st pair 2nd pair \& # of indices of indices \& @list1 = @a[ $cdiff[0] .. $cdiff[2]\-1 ]; \& @list2 = @b[ $cdiff[1] .. $cdiff[3]\-1 ]; \& # Hunk start Hunk end .Ve .PP Note that the hunks will always alternate between those that are part of the \s-1LCS \s0(those that contain unchanged items) and those that contain changes. This means that all we need to be told is whether the first hunk is a 'same' or 'diff' hunk and we can determine which of the other hunks contain 'same' items or 'diff' items. .PP By convention, we always make the first hunk contain unchanged items. So the 1st, 3rd, 5th, etc. hunks (all odd-numbered hunks if you start counting from 1) all contain unchanged items. And the 2nd, 4th, 6th, etc. hunks (all even-numbered hunks if you start counting from 1) all contain changed items. .PP Since \f(CW@a\fR and \f(CW@b\fR don't begin with the same value, the first hunk in our example is empty (otherwise we'd violate the above convention). Note that the first 4 index values in our example are all zero. Plug these values into our previous code block and we get: .PP .Vb 2 \& @hunk1a = @a[ 0 .. 0\-1 ]; \& @hunk1b = @b[ 0 .. 0\-1 ]; .Ve .PP And \f(CW\*(C`0..\-1\*(C'\fR returns the empty list. .PP Move down one pair of indices (2..5) and we get the offset ranges for the second hunk, which contains changed items. .PP Since \f(CW@diff[2..5]\fR contains (0,0,1,0) in our example, the second hunk consists of these two lists of items: .PP .Vb 11 \& @hunk2a = @a[ $cdiff[2] .. $cdiff[4]\-1 ]; \& @hunk2b = @b[ $cdiff[3] .. $cdiff[5]\-1 ]; \& # or \& @hunk2a = @a[ 0 .. 1\-1 ]; \& @hunk2b = @b[ 0 .. 0\-1 ]; \& # or \& @hunk2a = @a[ 0 .. 0 ]; \& @hunk2b = @b[ 0 .. \-1 ]; \& # or \& @hunk2a = ( \*(Aqa\*(Aq ); \& @hunk2b = ( ); .Ve .PP That is, we would delete item 0 ('a') from \f(CW@a\fR. .PP Since \f(CW@diff[4..7]\fR contains (1,0,3,2) in our example, the third hunk consists of these two lists of items: .PP .Vb 11 \& @hunk3a = @a[ $cdiff[4] .. $cdiff[6]\-1 ]; \& @hunk3a = @b[ $cdiff[5] .. $cdiff[7]\-1 ]; \& # or \& @hunk3a = @a[ 1 .. 3\-1 ]; \& @hunk3a = @b[ 0 .. 2\-1 ]; \& # or \& @hunk3a = @a[ 1 .. 2 ]; \& @hunk3a = @b[ 0 .. 1 ]; \& # or \& @hunk3a = qw( b c ); \& @hunk3a = qw( b c ); .Ve .PP Note that this third hunk contains unchanged items as our convention demands. .PP You can continue this process until you reach the last two indices, which will always be the number of items in each sequence. This is required so that subtracting one from each will give you the indices to the last items in each sequence. .ie n .SS """traverse_sequences""" .el .SS "\f(CWtraverse_sequences\fP" .IX Subsection "traverse_sequences" \&\f(CW\*(C`traverse_sequences\*(C'\fR used to be the most general facility provided by this module (the new \s-1OO\s0 interface is more powerful and much easier to use). .PP 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. \f(CW\*(C`traverse_sequences\*(C'\fR will advance the arrows through the sequences one element at a time, calling an appropriate user-specified callback function before each advance. It will advance the arrows in such a way that if there are equal elements \f(CW$A[$i]\fR and \f(CW$B[$j]\fR which are equal and which are part of the \s-1LCS,\s0 there will be some moment during the execution of \f(CW\*(C`traverse_sequences\*(C'\fR when arrow A is pointing to \f(CW$A[$i]\fR and arrow B is pointing to \f(CW$B[$j]\fR. When this happens, \&\f(CW\*(C`traverse_sequences\*(C'\fR will call the \f(CW\*(C`MATCH\*(C'\fR callback function and then it will advance both arrows. .PP Otherwise, one of the arrows is pointing to an element of its sequence that is not part of the \s-1LCS. \s0\f(CW\*(C`traverse_sequences\*(C'\fR will advance that arrow and will call the \f(CW\*(C`DISCARD_A\*(C'\fR or the \f(CW\*(C`DISCARD_B\*(C'\fR callback, depending on which arrow it advanced. If both arrows point to elements that are not part of the \s-1LCS,\s0 then \f(CW\*(C`traverse_sequences\*(C'\fR will advance one of them and call the appropriate callback, but it is not specified which it will call. .PP The arguments to \f(CW\*(C`traverse_sequences\*(C'\fR are the two sequences to traverse, and a hash which specifies the callback functions, like this: .PP .Vb 7 \& traverse_sequences( \& \e@seq1, \e@seq2, \& { MATCH => $callback_1, \& DISCARD_A => $callback_2, \& DISCARD_B => $callback_3, \& } \& ); .Ve .PP Callbacks for \s-1MATCH, DISCARD_A,\s0 and \s-1DISCARD_B\s0 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. .PP Callbacks for A_FINISHED and B_FINISHED are invoked with at least the corresponding index in A or B. .PP If arrow A reaches the end of its sequence, before arrow B does, \&\f(CW\*(C`traverse_sequences\*(C'\fR will call the \f(CW\*(C`A_FINISHED\*(C'\fR callback when it advances arrow B, if there is such a function; if not it will call \&\f(CW\*(C`DISCARD_B\*(C'\fR instead. Similarly if arrow B finishes first. \&\f(CW\*(C`traverse_sequences\*(C'\fR 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. .PP \&\f(CW\*(C`traverse_sequences\*(C'\fR may be passed an optional fourth parameter; this is a \s-1CODE\s0 reference to a key generation function. See \*(L"\s-1KEY GENERATION FUNCTIONS\*(R"\s0. .PP Additional parameters, if any, will be passed to the key generation function. .PP If you want to pass additional parameters to your callbacks, but don't need a custom key generation function, you can get the default by passing undef: .PP .Vb 11 \& traverse_sequences( \& \e@seq1, \e@seq2, \& { MATCH => $callback_1, \& DISCARD_A => $callback_2, \& DISCARD_B => $callback_3, \& }, \& undef, # default key\-gen \& $myArgument1, \& $myArgument2, \& $myArgument3, \& ); .Ve .PP \&\f(CW\*(C`traverse_sequences\*(C'\fR does not have a useful return value; you are expected to plug in the appropriate behavior with the callback functions. .ie n .SS """traverse_balanced""" .el .SS "\f(CWtraverse_balanced\fP" .IX Subsection "traverse_balanced" \&\f(CW\*(C`traverse_balanced\*(C'\fR is an alternative to \f(CW\*(C`traverse_sequences\*(C'\fR. It uses a different algorithm to iterate through the entries in the computed \s-1LCS.\s0 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 \fIchanges\fR occurring as deletions on one side followed immediately by an insertion on the other side. .PP In addition to the \f(CW\*(C`DISCARD_A\*(C'\fR, \f(CW\*(C`DISCARD_B\*(C'\fR, and \f(CW\*(C`MATCH\*(C'\fR callbacks supported by \f(CW\*(C`traverse_sequences\*(C'\fR, \f(CW\*(C`traverse_balanced\*(C'\fR supports a \f(CW\*(C`CHANGE\*(C'\fR callback indicating that one element got \f(CW\*(C`replaced\*(C'\fR by another: .PP .Vb 8 \& traverse_balanced( \& \e@seq1, \e@seq2, \& { MATCH => $callback_1, \& DISCARD_A => $callback_2, \& DISCARD_B => $callback_3, \& CHANGE => $callback_4, \& } \& ); .Ve .PP If no \f(CW\*(C`CHANGE\*(C'\fR callback is specified, \f(CW\*(C`traverse_balanced\*(C'\fR will map \f(CW\*(C`CHANGE\*(C'\fR events to \f(CW\*(C`DISCARD_A\*(C'\fR and \f(CW\*(C`DISCARD_B\*(C'\fR actions, therefore resulting in a similar behaviour as \f(CW\*(C`traverse_sequences\*(C'\fR with different order of events. .PP \&\f(CW\*(C`traverse_balanced\*(C'\fR might be a bit slower than \f(CW\*(C`traverse_sequences\*(C'\fR, noticeable only while processing huge amounts of data. .PP The \f(CW\*(C`sdiff\*(C'\fR function of this module is implemented as call to \f(CW\*(C`traverse_balanced\*(C'\fR. .PP \&\f(CW\*(C`traverse_balanced\*(C'\fR does not have a useful return value; you are expected to plug in the appropriate behavior with the callback functions. .SH "KEY GENERATION FUNCTIONS" .IX Header "KEY GENERATION FUNCTIONS" Most of the functions accept an optional extra parameter. This is a \&\s-1CODE\s0 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. .PP By default, comparisons will use \*(L"eq\*(R" and elements will be turned into keys using the default stringizing operator '""'. .PP 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. .PP For instance, consider this example: .PP .Vb 1 \& package Person; \& \& sub new \& { \& my $package = shift; \& return bless { name => \*(Aq\*(Aq, ssn => \*(Aq\*(Aq, @_ }, $package; \& } \& \& sub clone \& { \& my $old = shift; \& my $new = bless { %$old }, ref($old); \& } \& \& sub hash \& { \& return shift()\->{\*(Aqssn\*(Aq}; \& } \& \& my $person1 = Person\->new( name => \*(AqJoe\*(Aq, ssn => \*(Aq123\-45\-6789\*(Aq ); \& my $person2 = Person\->new( name => \*(AqMary\*(Aq, ssn => \*(Aq123\-47\-0000\*(Aq ); \& my $person3 = Person\->new( name => \*(AqPete\*(Aq, ssn => \*(Aq999\-45\-2222\*(Aq ); \& my $person4 = Person\->new( name => \*(AqPeggy\*(Aq, ssn => \*(Aq123\-45\-9999\*(Aq ); \& my $person5 = Person\->new( name => \*(AqFrank\*(Aq, ssn => \*(Aq000\-45\-9999\*(Aq ); .Ve .PP If you did this: .PP .Vb 3 \& my $array1 = [ $person1, $person2, $person4 ]; \& my $array2 = [ $person1, $person3, $person4, $person5 ]; \& Algorithm::Diff::diff( $array1, $array2 ); .Ve .PP everything would work out \s-1OK \s0(each of the objects would be converted into a string like \*(L"Person=HASH(0x82425b0)\*(R" for comparison). .PP But if you did this: .PP .Vb 3 \& my $array1 = [ $person1, $person2, $person4 ]; \& my $array2 = [ $person1, $person3, $person4\->clone(), $person5 ]; \& Algorithm::Diff::diff( $array1, $array2 ); .Ve .PP \&\f(CW$person4\fR and \f(CW$person4\fR\->\fIclone()\fR (which have the same name and \s-1SSN\s0) would be seen as different objects. If you wanted them to be considered equivalent, you would have to pass in a key generation function: .PP .Vb 3 \& my $array1 = [ $person1, $person2, $person4 ]; \& my $array2 = [ $person1, $person3, $person4\->clone(), $person5 ]; \& Algorithm::Diff::diff( $array1, $array2, \e&Person::hash ); .Ve .PP This would use the 'ssn' field in each Person as a comparison key, and so would consider \f(CW$person4\fR and \f(CW$person4\fR\->\fIclone()\fR as equal. .PP You may also pass additional parameters to the key generation function if you wish. .SH "ERROR CHECKING" .IX Header "ERROR CHECKING" If you pass these routines a non-reference and they expect a reference, they will die with a message. .SH "AUTHOR" .IX Header "AUTHOR" This version released by Tye McQueen (http://perlmonks.org/?node=tye). .SH "LICENSE" .IX Header "LICENSE" Parts Copyright (c) 2000\-2004 Ned Konz. All rights reserved. Parts by Tye McQueen. .PP This program is free software; you can redistribute it and/or modify it under the same terms as Perl. .SH "MAILING LIST" .IX Header "MAILING LIST" Mark-Jason still maintains a 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. .SH "CREDITS" .IX Header "CREDITS" Versions through 0.59 (and much of this documentation) were written by: .PP Mark-Jason Dominus, mjd\-perl\-diff@plover.com .PP This version borrows some documentation and routine names from Mark-Jason's, but Diff.pm's code was completely replaced. .PP 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 .PP \&\f(CW\*(C`sdiff\*(C'\fR and \f(CW\*(C`traverse_balanced\*(C'\fR were written by Mike Schilli <m@perlmeister.com>. .PP The algorithm is that described in \&\fIA Fast Algorithm for Computing Longest Common Subsequences\fR, \&\s-1CACM,\s0 vol.20, no.5, pp.350\-353, May 1977, with a few minor improvements to improve the speed. .PP Much work was done by Ned Konz (perl@bike\-nomad.com). .PP The \s-1OO\s0 interface and some other changes are by Tye McQueen. .SH "POD ERRORS" .IX Header "POD ERRORS" Hey! \fBThe above document had some coding errors, which are explained below:\fR .IP "Around line 989:" 4 .IX Item "Around line 989:" You can't have =items (as at line 1021) unless the first thing after the =over is an =item .IP "Around line 1108:" 4 .IX Item "Around line 1108:" Expected text after =item, not a number .IP "Around line 1114:" 4 .IX Item "Around line 1114:" Expected text after =item, not a number .IP "Around line 1120:" 4 .IX Item "Around line 1120:" Expected text after =item, not a number