{
    "content": [
        {
            "type": "text",
            "text": "# Algorithm::Diff (perldoc)\n\n## NAME\n\nAlgorithm::Diff - Compute `intelligent' differences between two files / lists\n\n## SYNOPSIS\n\nrequire Algorithm::Diff;\n# This example produces traditional 'diff' output:\nmy $diff = Algorithm::Diff->new( \\@seq1, \\@seq2 );\n$diff->Base( 1 );   # Return line numbers, not indices\nwhile(  $diff->Next()  ) {\nnext   if  $diff->Same();\nmy $sep = '';\nif(  ! $diff->Items(2)  ) {\nprintf \"%d,%dd%d\\n\",\n$diff->Get(qw( Min1 Max1 Max2 ));\n} elsif(  ! $diff->Items(1)  ) {\nprintf \"%da%d,%d\\n\",\n$diff->Get(qw( Max1 Min2 Max2 ));\n} else {\n$sep = \"---\\n\";\nprintf \"%d,%dc%d,%d\\n\",\n$diff->Get(qw( Min1 Max1 Min2 Max2 ));\n}\nprint \"< $\"   for  $diff->Items(1);\nprint $sep;\nprint \"> $\"   for  $diff->Items(2);\n}\n# Alternate interfaces:\nuse Algorithm::Diff qw(\nLCS LCSlength LCSidx\ndiff sdiff compactdiff\ntraversesequences traversebalanced );\n@lcs    = LCS( \\@seq1, \\@seq2 );\n$lcsref = LCS( \\@seq1, \\@seq2 );\n$count  = LCSlength( \\@seq1, \\@seq2 );\n( $seq1idxref, $seq2idxref ) = LCSidx( \\@seq1, \\@seq2 );\n# Complicated interfaces:\n@diffs  = diff( \\@seq1, \\@seq2 );\n@sdiffs = sdiff( \\@seq1, \\@seq2 );\n@cdiffs = compactdiff( \\@seq1, \\@seq2 );\ntraversesequences(\n\\@seq1,\n\\@seq2,\n{   MATCH     => \\&callback1,\nDISCARDA => \\&callback2,\nDISCARDB => \\&callback3,\n},\n\\&keygenerator,\n@extraargs,\n);\ntraversebalanced(\n\\@seq1,\n\\@seq2,\n{   MATCH     => \\&callback1,\nDISCARDA => \\&callback2,\nDISCARDB => \\&callback3,\nCHANGE    => \\&callback4,\n},\n\\&keygenerator,\n@extraargs,\n);\n\n## Sections\n\n- **NAME**\n- **SYNOPSIS**\n- **INTRODUCTION**\n- **USAGE**\n- **KEY GENERATION FUNCTIONS**\n- **ERROR CHECKING**\n- **AUTHOR**\n- **LICENSE**\n- **MAILING LIST**\n- **CREDITS**\n\nUse structuredContent.sections for detailed options, examples, and full documentation.\n"
        }
    ],
    "structuredContent": {
        "command": "Algorithm::Diff",
        "section": "",
        "mode": "perldoc",
        "summary": "Algorithm::Diff - Compute `intelligent' differences between two files / lists",
        "synopsis": "require Algorithm::Diff;\n# This example produces traditional 'diff' output:\nmy $diff = Algorithm::Diff->new( \\@seq1, \\@seq2 );\n$diff->Base( 1 );   # Return line numbers, not indices\nwhile(  $diff->Next()  ) {\nnext   if  $diff->Same();\nmy $sep = '';\nif(  ! $diff->Items(2)  ) {\nprintf \"%d,%dd%d\\n\",\n$diff->Get(qw( Min1 Max1 Max2 ));\n} elsif(  ! $diff->Items(1)  ) {\nprintf \"%da%d,%d\\n\",\n$diff->Get(qw( Max1 Min2 Max2 ));\n} else {\n$sep = \"---\\n\";\nprintf \"%d,%dc%d,%d\\n\",\n$diff->Get(qw( Min1 Max1 Min2 Max2 ));\n}\nprint \"< $\"   for  $diff->Items(1);\nprint $sep;\nprint \"> $\"   for  $diff->Items(2);\n}\n# Alternate interfaces:\nuse Algorithm::Diff qw(\nLCS LCSlength LCSidx\ndiff sdiff compactdiff\ntraversesequences traversebalanced );\n@lcs    = LCS( \\@seq1, \\@seq2 );\n$lcsref = LCS( \\@seq1, \\@seq2 );\n$count  = LCSlength( \\@seq1, \\@seq2 );\n( $seq1idxref, $seq2idxref ) = LCSidx( \\@seq1, \\@seq2 );\n# Complicated interfaces:\n@diffs  = diff( \\@seq1, \\@seq2 );\n@sdiffs = sdiff( \\@seq1, \\@seq2 );\n@cdiffs = compactdiff( \\@seq1, \\@seq2 );\ntraversesequences(\n\\@seq1,\n\\@seq2,\n{   MATCH     => \\&callback1,\nDISCARDA => \\&callback2,\nDISCARDB => \\&callback3,\n},\n\\&keygenerator,\n@extraargs,\n);\ntraversebalanced(\n\\@seq1,\n\\@seq2,\n{   MATCH     => \\&callback1,\nDISCARDA => \\&callback2,\nDISCARDB => \\&callback3,\nCHANGE    => \\&callback4,\n},\n\\&keygenerator,\n@extraargs,\n);",
        "tldr_summary": null,
        "tldr_examples": [],
        "tldr_source": null,
        "flags": [],
        "examples": [],
        "see_also": [],
        "section_outline": [
            {
                "name": "NAME",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "SYNOPSIS",
                "lines": 72,
                "subsections": []
            },
            {
                "name": "INTRODUCTION",
                "lines": 51,
                "subsections": []
            },
            {
                "name": "USAGE",
                "lines": 567,
                "subsections": []
            },
            {
                "name": "KEY GENERATION FUNCTIONS",
                "lines": 69,
                "subsections": []
            },
            {
                "name": "ERROR CHECKING",
                "lines": 3,
                "subsections": []
            },
            {
                "name": "AUTHOR",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "LICENSE",
                "lines": 5,
                "subsections": []
            },
            {
                "name": "MAILING LIST",
                "lines": 4,
                "subsections": []
            },
            {
                "name": "CREDITS",
                "lines": 19,
                "subsections": []
            }
        ],
        "sections": {
            "NAME": {
                "content": "Algorithm::Diff - Compute `intelligent' differences between two files / lists\n",
                "subsections": []
            },
            "SYNOPSIS": {
                "content": "require Algorithm::Diff;\n\n# This example produces traditional 'diff' output:\n\nmy $diff = Algorithm::Diff->new( \\@seq1, \\@seq2 );\n\n$diff->Base( 1 );   # Return line numbers, not indices\nwhile(  $diff->Next()  ) {\nnext   if  $diff->Same();\nmy $sep = '';\nif(  ! $diff->Items(2)  ) {\nprintf \"%d,%dd%d\\n\",\n$diff->Get(qw( Min1 Max1 Max2 ));\n} elsif(  ! $diff->Items(1)  ) {\nprintf \"%da%d,%d\\n\",\n$diff->Get(qw( Max1 Min2 Max2 ));\n} else {\n$sep = \"---\\n\";\nprintf \"%d,%dc%d,%d\\n\",\n$diff->Get(qw( Min1 Max1 Min2 Max2 ));\n}\nprint \"< $\"   for  $diff->Items(1);\nprint $sep;\nprint \"> $\"   for  $diff->Items(2);\n}\n\n\n# Alternate interfaces:\n\nuse Algorithm::Diff qw(\nLCS LCSlength LCSidx\ndiff sdiff compactdiff\ntraversesequences traversebalanced );\n\n@lcs    = LCS( \\@seq1, \\@seq2 );\n$lcsref = LCS( \\@seq1, \\@seq2 );\n$count  = LCSlength( \\@seq1, \\@seq2 );\n\n( $seq1idxref, $seq2idxref ) = LCSidx( \\@seq1, \\@seq2 );\n\n\n# Complicated interfaces:\n\n@diffs  = diff( \\@seq1, \\@seq2 );\n\n@sdiffs = sdiff( \\@seq1, \\@seq2 );\n\n@cdiffs = compactdiff( \\@seq1, \\@seq2 );\n\ntraversesequences(\n\\@seq1,\n\\@seq2,\n{   MATCH     => \\&callback1,\nDISCARDA => \\&callback2,\nDISCARDB => \\&callback3,\n},\n\\&keygenerator,\n@extraargs,\n);\n\ntraversebalanced(\n\\@seq1,\n\\@seq2,\n{   MATCH     => \\&callback1,\nDISCARDA => \\&callback2,\nDISCARDB => \\&callback3,\nCHANGE    => \\&callback4,\n},\n\\&keygenerator,\n@extraargs,\n);\n",
                "subsections": []
            },
            "INTRODUCTION": {
                "content": "(by Mark-Jason Dominus)\n\nI once read an article written by the authors of \"diff\"; they said that they worked very hard on\nthe algorithm until they found the right one.\n\nI think what they ended up using (and I hope someone will correct me, because I am not very\nconfident about this) was the `longest common subsequence' method. In the LCS problem, you have\ntwo sequences of items:\n\na b c d f g h j q z\n\na b c d e f g i j k r x y z\n\nand you want to find the longest sequence of items that is present in both original sequences in\nthe same order. That is, you want to find a new sequence *S* which can be obtained from the\nfirst sequence by deleting some items, and from the second sequence by deleting other items. You\nalso want *S* to be as long as possible. In this case *S* is\n\na b c d f g j z\n\nFrom there it's only a small step to get diff-like output:\n\ne   h i   k   q r x y\n+   - +   +   - + + +\n\nThis module solves the LCS problem. It also includes a canned function to generate \"diff\"-like\noutput.\n\nIt might seem from the example above that the LCS of two sequences is always pretty obvious, but\nthat's not always the case, especially when the two sequences have many repeated elements. For\nexample, consider\n\na x b y c z p d q\na b c a x b y c z\n\nA naive approach might start by matching up the \"a\" and \"b\" that appear at the beginning of each\nsequence, like this:\n\na x b y c         z p d q\na   b   c a b y c z\n\nThis finds the common subsequence \"a b c z\". But actually, the LCS is \"a x b y c z\":\n\na x b y c z p d q\na b c a x b y c z\n\nor\n\na       x b y c z p d q\na b c a x b y c z\n",
                "subsections": []
            },
            "USAGE": {
                "content": "(See also the README file and several example scripts include with this module.)\n\nThis module now provides an object-oriented interface that uses less memory and is easier to use\nthan most of the previous procedural interfaces. It also still provides several exportable\nfunctions. We'll deal with these in ascending order of difficulty: \"LCS\", \"LCSlength\",\n\"LCSidx\", OO interface, \"prepare\", \"diff\", \"sdiff\", \"traversesequences\", and\n\"traversebalanced\".\n\n\"LCS\"\nGiven references to two lists of items, LCS returns an array containing their longest common\nsubsequence. In scalar context, it returns a reference to such a list.\n\n@lcs    = LCS( \\@seq1, \\@seq2 );\n$lcsref = LCS( \\@seq1, \\@seq2 );\n\n\"LCS\" may be passed an optional third parameter; this is a CODE reference to a key generation\nfunction. See \"KEY GENERATION FUNCTIONS\".\n\n@lcs    = LCS( \\@seq1, \\@seq2, \\&keyGen, @args );\n$lcsref = LCS( \\@seq1, \\@seq2, \\&keyGen, @args );\n\nAdditional parameters, if any, will be passed to the key generation routine.\n\n\"LCSlength\"\nThis is just like \"LCS\" except it only returns the length of the longest common subsequence.\nThis provides a performance gain of about 9% compared to \"LCS\".\n\n\"LCSidx\"\nLike \"LCS\" except it returns references to two arrays. The first array contains the indices into\n@seq1 where the LCS items are located. The second array contains the indices into @seq2 where\nthe LCS items are located.\n\nTherefore, the following three lists will contain the same values:\n\nmy( $idx1, $idx2 ) = LCSidx( \\@seq1, \\@seq2 );\nmy @list1 = @seq1[ @$idx1 ];\nmy @list2 = @seq2[ @$idx2 ];\nmy @list3 = LCS( \\@seq1, \\@seq2 );\n\n\"new\"\n$diff = Algorithm::Diff->new( \\@seq1, \\@seq2 );\n$diff = Algorithm::Diff->new( \\@seq1, \\@seq2, \\%opts );\n\n\"new\" computes the smallest set of additions and deletions necessary to turn the first sequence\ninto the second and compactly records them in the object.\n\nYou use the object to iterate over *hunks*, where each hunk represents a contiguous section of\nitems which should be added, deleted, replaced, or left unchanged.\n\nThe following summary of all of the methods looks a lot like Perl code but some of the symbols\nhave different meanings:\n\n[ ]     Encloses optional arguments\n:       Is followed by the default value for an optional argument\n|       Separates alternate return results\n\nMethod summary:\n\n$obj        = Algorithm::Diff->new( \\@seq1, \\@seq2, [ \\%opts ] );\n$pos        = $obj->Next(  [ $count : 1 ] );\n$revPos     = $obj->Prev(  [ $count : 1 ] );\n$obj        = $obj->Reset( [ $pos : 0 ] );\n$copy       = $obj->Copy(  [ $pos, [ $newBase ] ] );\n$oldBase    = $obj->Base(  [ $newBase ] );\n\nNote that all of the following methods \"die\" if used on an object that is \"reset\" (not currently\npointing at any hunk).\n\n$bits       = $obj->Diff(  );\n@items|$cnt = $obj->Same(  );\n@items|$cnt = $obj->Items( $seqNum );\n@idxs |$cnt = $obj->Range( $seqNum, [ $base ] );\n$minIdx     = $obj->Min(   $seqNum, [ $base ] );\n$maxIdx     = $obj->Max(   $seqNum, [ $base ] );\n@values     = $obj->Get(   @names );\n\nPassing in \"undef\" for an optional argument is always treated the same as if no argument were\npassed in.\n\n\"Next\"\n$pos = $diff->Next();    # Move forward 1 hunk\n$pos = $diff->Next( 2 ); # Move forward 2 hunks\n$pos = $diff->Next(-5);  # Move backward 5 hunks\n\n\"Next\" moves the object to point at the next hunk. The object starts out \"reset\", which\nmeans it isn't pointing at any hunk. If the object is reset, then \"Next()\" moves to the\nfirst hunk.\n\n\"Next\" returns a true value iff the move didn't go past the last hunk. So Next(0) will\nreturn true iff the object is not reset.\n\nActually, \"Next\" returns the object's new position, which is a number between 1 and the\nnumber of hunks (inclusive), or returns a false value.\n\n\"Prev\"\n\"Prev($N)\" is almost identical to \"Next(-$N)\"; it moves to the $Nth previous hunk. On a\n'reset' object, \"Prev()\" [and \"Next(-1)\"] move to the last hunk.\n\nThe position returned by \"Prev\" is relative to the *end* of the hunks; -1 for the last hunk,\n-2 for the second-to-last, etc.\n\n\"Reset\"\n$diff->Reset();     # Reset the object's position\n$diff->Reset($pos); # Move to the specified hunk\n$diff->Reset(1);    # Move to the first hunk\n$diff->Reset(-1);   # Move to the last hunk\n\n\"Reset\" returns the object, so, for example, you could use \"$diff->Reset()->Next(-1)\" to get\nthe number of hunks.\n\n\"Copy\"\n$copy = $diff->Copy( $newPos, $newBase );\n\n\"Copy\" returns a copy of the object. The copy and the original object share most of their\ndata, so making copies takes very little memory. The copy maintains its own position\n(separate from the original), which is the main purpose of copies. It also maintains its own\nbase.\n\nBy default, the copy's position starts out the same as the original object's position. But\n\"Copy\" takes an optional first argument to set the new position, so the following three\nsnippets are equivalent:\n\n$copy = $diff->Copy($pos);\n\n$copy = $diff->Copy();\n$copy->Reset($pos);\n\n$copy = $diff->Copy()->Reset($pos);\n\n\"Copy\" takes an optional second argument to set the base for the copy. If you wish to change\nthe base of the copy but leave the position the same as in the original, here are two\nequivalent ways:\n\n$copy = $diff->Copy();\n$copy->Base( 0 );\n\n$copy = $diff->Copy(undef,0);\n\nHere are two equivalent way to get a \"reset\" copy:\n\n$copy = $diff->Copy(0);\n\n$copy = $diff->Copy()->Reset();\n\n\"Diff\"\n$bits = $obj->Diff();\n\n\"Diff\" returns a true value iff the current hunk contains items that are different between\nthe two sequences. It actually returns one of the follow 4 values:\n\n3   \"3==(1|2)\". This hunk contains items from @seq1 and the items from @seq2 that should\nreplace them. Both sequence 1 and 2 contain changed items so both the 1 and 2 bits are\nset.\n\n2   This hunk only contains items from @seq2 that should be inserted (not items from @seq1).\nOnly sequence 2 contains changed items so only the 2 bit is set.\n\n1   This hunk only contains items from @seq1 that should be deleted (not items from @seq2).\nOnly sequence 1 contains changed items so only the 1 bit is set.\n\n0   This means that the items in this hunk are the same in both sequences. Neither sequence\n1 nor 2 contain changed items so neither the 1 nor the 2 bits are set.\n\n\"Same\"\n\"Same\" returns a true value iff the current hunk contains items that are the same in both\nsequences. It actually returns the list of items if they are the same or an empty list if\nthey aren't. In a scalar context, it returns the size of the list.\n\n\"Items\"\n$count = $diff->Items(2);\n@items = $diff->Items($seqNum);\n\n\"Items\" returns the (number of) items from the specified sequence that are part of the\ncurrent hunk.\n\nIf the current hunk contains only insertions, then \"$diff->Items(1)\" will return an empty\nlist (0 in a scalar context). If the current hunk contains only deletions, then\n\"$diff->Items(2)\" will return an empty list (0 in a scalar context).\n\nIf the hunk contains replacements, then both \"$diff->Items(1)\" and \"$diff->Items(2)\" will\nreturn different, non-empty lists.\n\nOtherwise, the hunk contains identical items and all of the following will return the same\nlists:\n\n@items = $diff->Items(1);\n@items = $diff->Items(2);\n@items = $diff->Same();\n\n\"Range\"\n$count = $diff->Range( $seqNum );\n@indices = $diff->Range( $seqNum );\n@indices = $diff->Range( $seqNum, $base );\n\n\"Range\" is like \"Items\" except that it returns a list of *indices* to the items rather than\nthe items themselves. By default, the index of the first item (in each sequence) is 0 but\nthis can be changed by calling the \"Base\" method. So, by default, the following two snippets\nreturn the same lists:\n\n@list = $diff->Items(2);\n@list = @seq2[ $diff->Range(2) ];\n\nYou can also specify the base to use as the second argument. So the following two snippets\n*always* return the same lists:\n\n@list = $diff->Items(1);\n@list = @seq1[ $diff->Range(1,0) ];\n\n\"Base\"\n$curBase = $diff->Base();\n$oldBase = $diff->Base($newBase);\n\n\"Base\" sets and/or returns the current base (usually 0 or 1) that is used when you request\nrange information. The base defaults to 0 so that range information is returned as array\nindices. You can set the base to 1 if you want to report traditional line numbers instead.\n\n\"Min\"\n$min1 = $diff->Min(1);\n$min = $diff->Min( $seqNum, $base );\n\n\"Min\" returns the first value that \"Range\" would return (given the same arguments) or\nreturns \"undef\" if \"Range\" would return an empty list.\n\n\"Max\"\n\"Max\" returns the last value that \"Range\" would return or \"undef\".\n\n\"Get\"\n( $n, $x, $r ) = $diff->Get(qw( min1 max1 range1 ));\n@values = $diff->Get(qw( 0min2 1max2 range2 same base ));\n\n\"Get\" returns one or more scalar values. You pass in a list of the names of the values you\nwant returned. Each name must match one of the following regexes:\n\n/^(-?\\d+)?(min|max)[12]$/i\n/^(range[12]|same|diff|base)$/i\n\nThe 1 or 2 after a name says which sequence you want the information for (and where allowed,\nit is required). The optional number before \"min\" or \"max\" is the base to use. So the\nfollowing equalities hold:\n\n$diff->Get('min1') == $diff->Min(1)\n$diff->Get('0min2') == $diff->Min(2,0)\n\nUsing \"Get\" in a scalar context when you've passed in more than one name is a fatal error\n(\"die\" is called).\n\n\"prepare\"\nGiven a reference to a list of items, \"prepare\" returns a reference to a hash which can be used\nwhen comparing this sequence to other sequences with \"LCS\" or \"LCSlength\".\n\n$prep = prepare( \\@seq1 );\nfor $i ( 0 .. 10000 )\n{\n@lcs = LCS( $prep, $seq[$i] );\n# do something useful with @lcs\n}\n\n\"prepare\" may be passed an optional third parameter; this is a CODE reference to a key\ngeneration function. See \"KEY GENERATION FUNCTIONS\".\n\n$prep = prepare( \\@seq1, \\&keyGen );\nfor $i ( 0 .. 10000 )\n{\n@lcs = LCS( $seq[$i], $prep, \\&keyGen );\n# do something useful with @lcs\n}\n\nUsing \"prepare\" provides a performance gain of about 50% when calling LCS many times compared\nwith not preparing.\n\n\"diff\"\n@diffs     = diff( \\@seq1, \\@seq2 );\n$diffsref = diff( \\@seq1, \\@seq2 );\n\n\"diff\" computes the smallest set of additions and deletions necessary to turn the first sequence\ninto the second, and returns a description of these changes. The description is a list of\n*hunks*; each hunk represents a contiguous section of items which should be added, deleted, or\nreplaced. (Hunks containing unchanged items are not included.)\n\nThe return value of \"diff\" is a list of hunks, or, in scalar context, a reference to such a\nlist. If there are no differences, the list will be empty.\n\nHere is an example. Calling \"diff\" for the following two sequences:\n\na b c e h j l m n p\nb c d e f j k l m r s t\n\nwould produce the following list:\n\n(\n[ [ '-', 0, 'a' ] ],\n\n[ [ '+', 2, 'd' ] ],\n\n[ [ '-', 4, 'h' ],\n[ '+', 4, 'f' ] ],\n\n[ [ '+', 6, 'k' ] ],\n\n[ [ '-',  8, 'n' ],\n[ '-',  9, 'p' ],\n[ '+',  9, 'r' ],\n[ '+', 10, 's' ],\n[ '+', 11, 't' ] ],\n)\n\nThere are five hunks here. The first hunk says that the \"a\" at position 0 of the first sequence\nshould be deleted (\"-\"). The second hunk says that the \"d\" at position 2 of the second sequence\nshould be inserted (\"+\"). The third hunk says that the \"h\" at position 4 of the first sequence\nshould be removed and replaced with the \"f\" from position 4 of the second sequence. And so on.\n\n\"diff\" may be passed an optional third parameter; this is a CODE reference to a key generation\nfunction. See \"KEY GENERATION FUNCTIONS\".\n\nAdditional parameters, if any, will be passed to the key generation routine.\n\n\"sdiff\"\n@sdiffs     = sdiff( \\@seq1, \\@seq2 );\n$sdiffsref = sdiff( \\@seq1, \\@seq2 );\n\n\"sdiff\" computes all necessary components to show two sequences and their minimized differences\nside by side, just like the Unix-utility *sdiff* does:\n\nsame             same\nbefore     |     after\nold        <     -\n-          >     new\n\nIt returns a list of array refs, each pointing to an array of display instructions. In scalar\ncontext it returns a reference to such a list. If there are no differences, the list will have\none entry per item, each indicating that the item was unchanged.\n\nDisplay instructions consist of three elements: A modifier indicator (\"+\": Element added, \"-\":\nElement removed, \"u\": Element unmodified, \"c\": Element changed) and the value of the old and new\nelements, to be displayed side-by-side.\n\nAn \"sdiff\" of the following two sequences:\n\na b c e h j l m n p\nb c d e f j k l m r s t\n\nresults in\n\n( [ '-', 'a', ''  ],\n[ 'u', 'b', 'b' ],\n[ 'u', 'c', 'c' ],\n[ '+', '',  'd' ],\n[ 'u', 'e', 'e' ],\n[ 'c', 'h', 'f' ],\n[ 'u', 'j', 'j' ],\n[ '+', '',  'k' ],\n[ 'u', 'l', 'l' ],\n[ 'u', 'm', 'm' ],\n[ 'c', 'n', 'r' ],\n[ 'c', 'p', 's' ],\n[ '+', '',  't' ],\n)\n\n\"sdiff\" may be passed an optional third parameter; this is a CODE reference to a key generation\nfunction. See \"KEY GENERATION FUNCTIONS\".\n\nAdditional parameters, if any, will be passed to the key generation routine.\n\n\"compactdiff\"\n\"compactdiff\" is much like \"sdiff\" except it returns a much more compact description consisting\nof just one flat list of indices. An example helps explain the format:\n\nmy @a = qw( a b c   e  h j   l m n p      );\nmy @b = qw(   b c d e f  j k l m    r s t );\n@cdiff = compactdiff( \\@a, \\@b );\n# Returns:\n#   @a      @b       @a       @b\n#  start   start   values   values\n(    0,      0,   #       =\n0,      0,   #    a  !\n1,      0,   #  b c  =  b c\n3,      2,   #       !  d\n3,      3,   #    e  =  e\n4,      4,   #    f  !  h\n5,      5,   #    j  =  j\n6,      6,   #       !  k\n6,      7,   #  l m  =  l m\n8,      9,   #  n p  !  r s t\n10,     12,   #\n);\n\nThe 0th, 2nd, 4th, etc. entries are all indices into @seq1 (@a in the above example) indicating\nwhere a hunk begins. The 1st, 3rd, 5th, etc. entries are all indices into @seq2 (@b in the above\nexample) indicating where the same hunk begins.\n\nSo each pair of indices (except the last pair) describes where a hunk begins (in each sequence).\nSince each hunk must end at the item just before the item that starts the next hunk, the next\npair of indices can be used to determine where the hunk ends.\n\nSo, the first 4 entries (0..3) describe the first hunk. Entries 0 and 1 describe where the first\nhunk begins (and so are always both 0). Entries 2 and 3 describe where the next hunk begins, so\nsubtracting 1 from each tells us where the first hunk ends. That is, the first hunk contains\nitems $diff[0] through \"$diff[2] - 1\" of the first sequence and contains items $diff[1] through\n\"$diff[3] - 1\" of the second sequence.\n\nIn other words, the first hunk consists of the following two lists of items:\n\n#  1st pair     2nd pair\n# of indices   of indices\n@list1 = @a[ $cdiff[0] .. $cdiff[2]-1 ];\n@list2 = @b[ $cdiff[1] .. $cdiff[3]-1 ];\n# Hunk start   Hunk end\n\nNote that the hunks will always alternate between those that are part of the LCS (those that\ncontain unchanged items) and those that contain changes. This means that all we need to be told\nis whether the first hunk is a 'same' or 'diff' hunk and we can determine which of the other\nhunks contain 'same' items or 'diff' items.\n\nBy convention, we always make the first hunk contain unchanged items. So the 1st, 3rd, 5th, etc.\nhunks (all odd-numbered hunks if you start counting from 1) all contain unchanged items. And the\n2nd, 4th, 6th, etc. hunks (all even-numbered hunks if you start counting from 1) all contain\nchanged items.\n\nSince @a and @b don't begin with the same value, the first hunk in our example is empty\n(otherwise we'd violate the above convention). Note that the first 4 index values in our example\nare all zero. Plug these values into our previous code block and we get:\n\n@hunk1a = @a[ 0 .. 0-1 ];\n@hunk1b = @b[ 0 .. 0-1 ];\n\nAnd \"0..-1\" returns the empty list.\n\nMove down one pair of indices (2..5) and we get the offset ranges for the second hunk, which\ncontains changed items.\n\nSince @diff[2..5] contains (0,0,1,0) in our example, the second hunk consists of these two lists\nof items:\n\n@hunk2a = @a[ $cdiff[2] .. $cdiff[4]-1 ];\n@hunk2b = @b[ $cdiff[3] .. $cdiff[5]-1 ];\n# or\n@hunk2a = @a[ 0 .. 1-1 ];\n@hunk2b = @b[ 0 .. 0-1 ];\n# or\n@hunk2a = @a[ 0 .. 0 ];\n@hunk2b = @b[ 0 .. -1 ];\n# or\n@hunk2a = ( 'a' );\n@hunk2b = ( );\n\nThat is, we would delete item 0 ('a') from @a.\n\nSince @diff[4..7] contains (1,0,3,2) in our example, the third hunk consists of these two lists\nof items:\n\n@hunk3a = @a[ $cdiff[4] .. $cdiff[6]-1 ];\n@hunk3a = @b[ $cdiff[5] .. $cdiff[7]-1 ];\n# or\n@hunk3a = @a[ 1 .. 3-1 ];\n@hunk3a = @b[ 0 .. 2-1 ];\n# or\n@hunk3a = @a[ 1 .. 2 ];\n@hunk3a = @b[ 0 .. 1 ];\n# or\n@hunk3a = qw( b c );\n@hunk3a = qw( b c );\n\nNote that this third hunk contains unchanged items as our convention demands.\n\nYou can continue this process until you reach the last two indices, which will always be the\nnumber of items in each sequence. This is required so that subtracting one from each will give\nyou the indices to the last items in each sequence.\n\n\"traversesequences\"\n\"traversesequences\" used to be the most general facility provided by this module (the new OO\ninterface is more powerful and much easier to use).\n\nImagine that there are two arrows. Arrow A points to an element of sequence A, and arrow B\npoints to an element of the sequence B. Initially, the arrows point to the first elements of the\nrespective sequences. \"traversesequences\" will advance the arrows through the sequences one\nelement at a time, calling an appropriate user-specified callback function before each advance.\nIt will advance the arrows in such a way that if there are equal elements $A[$i] and $B[$j]\nwhich are equal and which are part of the LCS, there will be some moment during the execution of\n\"traversesequences\" when arrow A is pointing to $A[$i] and arrow B is pointing to $B[$j]. When\nthis happens, \"traversesequences\" will call the \"MATCH\" callback function and then it will\nadvance both arrows.\n\nOtherwise, one of the arrows is pointing to an element of its sequence that is not part of the\nLCS. \"traversesequences\" will advance that arrow and will call the \"DISCARDA\" or the\n\"DISCARDB\" callback, depending on which arrow it advanced. If both arrows point to elements\nthat are not part of the LCS, then \"traversesequences\" will advance one of them and call the\nappropriate callback, but it is not specified which it will call.\n\nThe arguments to \"traversesequences\" are the two sequences to traverse, and a hash which\nspecifies the callback functions, like this:\n\ntraversesequences(\n\\@seq1, \\@seq2,\n{   MATCH => $callback1,\nDISCARDA => $callback2,\nDISCARDB => $callback3,\n}\n);\n\nCallbacks for MATCH, DISCARDA, and DISCARDB are invoked with at least the indices of the two\narrows as their arguments. They are not expected to return any values. If a callback is omitted\nfrom the table, it is not called.\n\nCallbacks for AFINISHED and BFINISHED are invoked with at least the corresponding index in A\nor B.\n\nIf arrow A reaches the end of its sequence, before arrow B does, \"traversesequences\" will call\nthe \"AFINISHED\" callback when it advances arrow B, if there is such a function; if not it will\ncall \"DISCARDB\" instead. Similarly if arrow B finishes first. \"traversesequences\" returns when\nboth arrows are at the ends of their respective sequences. It returns true on success and false\non failure. At present there is no way to fail.\n\n\"traversesequences\" may be passed an optional fourth parameter; this is a CODE reference to a\nkey generation function. See \"KEY GENERATION FUNCTIONS\".\n\nAdditional parameters, if any, will be passed to the key generation function.\n\nIf you want to pass additional parameters to your callbacks, but don't need a custom key\ngeneration function, you can get the default by passing undef:\n\ntraversesequences(\n\\@seq1, \\@seq2,\n{   MATCH => $callback1,\nDISCARDA => $callback2,\nDISCARDB => $callback3,\n},\nundef,     # default key-gen\n$myArgument1,\n$myArgument2,\n$myArgument3,\n);\n\n\"traversesequences\" does not have a useful return value; you are expected to plug in the\nappropriate behavior with the callback functions.\n\n\"traversebalanced\"\n\"traversebalanced\" is an alternative to \"traversesequences\". It uses a different algorithm to\niterate through the entries in the computed LCS. Instead of sticking to one side and showing\nelement changes as insertions and deletions only, it will jump back and forth between the two\nsequences and report *changes* occurring as deletions on one side followed immediately by an\ninsertion on the other side.\n\nIn addition to the \"DISCARDA\", \"DISCARDB\", and \"MATCH\" callbacks supported by\n\"traversesequences\", \"traversebalanced\" supports a \"CHANGE\" callback indicating that one\nelement got \"replaced\" by another:\n\ntraversebalanced(\n\\@seq1, \\@seq2,\n{   MATCH => $callback1,\nDISCARDA => $callback2,\nDISCARDB => $callback3,\nCHANGE    => $callback4,\n}\n);\n\nIf no \"CHANGE\" callback is specified, \"traversebalanced\" will map \"CHANGE\" events to\n\"DISCARDA\" and \"DISCARDB\" actions, therefore resulting in a similar behaviour as\n\"traversesequences\" with different order of events.\n\n\"traversebalanced\" might be a bit slower than \"traversesequences\", noticeable only while\nprocessing huge amounts of data.\n\nThe \"sdiff\" function of this module is implemented as call to \"traversebalanced\".\n\n\"traversebalanced\" does not have a useful return value; you are expected to plug in the\nappropriate behavior with the callback functions.\n",
                "subsections": []
            },
            "KEY GENERATION FUNCTIONS": {
                "content": "Most of the functions accept an optional extra parameter. This is a CODE reference to a key\ngenerating (hashing) function that should return a string that uniquely identifies a given\nelement. It should be the case that if two elements are to be considered equal, their keys\nshould be the same (and the other way around). If no key generation function is provided, the\nkey will be the element as a string.\n\nBy default, comparisons will use \"eq\" and elements will be turned into keys using the default\nstringizing operator '\"\"'.\n\nWhere this is important is when you're comparing something other than strings. If it is the case\nthat you have multiple different objects that should be considered to be equal, you should\nsupply a key generation function. Otherwise, you have to make sure that your arrays contain\nunique references.\n\nFor instance, consider this example:\n\npackage Person;\n\nsub new\n{\nmy $package = shift;\nreturn bless { name => '', ssn => '', @ }, $package;\n}\n\nsub clone\n{\nmy $old = shift;\nmy $new = bless { %$old }, ref($old);\n}\n\nsub hash\n{\nreturn shift()->{'ssn'};\n}\n\nmy $person1 = Person->new( name => 'Joe', ssn => '123-45-6789' );\nmy $person2 = Person->new( name => 'Mary', ssn => '123-47-0000' );\nmy $person3 = Person->new( name => 'Pete', ssn => '999-45-2222' );\nmy $person4 = Person->new( name => 'Peggy', ssn => '123-45-9999' );\nmy $person5 = Person->new( name => 'Frank', ssn => '000-45-9999' );\n\nIf you did this:\n\nmy $array1 = [ $person1, $person2, $person4 ];\nmy $array2 = [ $person1, $person3, $person4, $person5 ];\nAlgorithm::Diff::diff( $array1, $array2 );\n\neverything would work out OK (each of the objects would be converted into a string like\n\"Person=HASH(0x82425b0)\" for comparison).\n\nBut if you did this:\n\nmy $array1 = [ $person1, $person2, $person4 ];\nmy $array2 = [ $person1, $person3, $person4->clone(), $person5 ];\nAlgorithm::Diff::diff( $array1, $array2 );\n\n$person4 and $person4->clone() (which have the same name and SSN) would be seen as different\nobjects. If you wanted them to be considered equivalent, you would have to pass in a key\ngeneration function:\n\nmy $array1 = [ $person1, $person2, $person4 ];\nmy $array2 = [ $person1, $person3, $person4->clone(), $person5 ];\nAlgorithm::Diff::diff( $array1, $array2, \\&Person::hash );\n\nThis would use the 'ssn' field in each Person as a comparison key, and so would consider\n$person4 and $person4->clone() as equal.\n\nYou may also pass additional parameters to the key generation function if you wish.\n",
                "subsections": []
            },
            "ERROR CHECKING": {
                "content": "If you pass these routines a non-reference and they expect a reference, they will die with a\nmessage.\n",
                "subsections": []
            },
            "AUTHOR": {
                "content": "This version released by Tye McQueen (http://perlmonks.org/?node=tye).\n",
                "subsections": []
            },
            "LICENSE": {
                "content": "Parts Copyright (c) 2000-2004 Ned Konz. All rights reserved. Parts by Tye McQueen.\n\nThis program is free software; you can redistribute it and/or modify it under the same terms as\nPerl.\n",
                "subsections": []
            },
            "MAILING LIST": {
                "content": "Mark-Jason still maintains a mailing list. To join a low-volume mailing list for announcements\nrelated to diff and Algorithm::Diff, send an empty mail message to\nmjd-perl-diff-request@plover.com.\n",
                "subsections": []
            },
            "CREDITS": {
                "content": "Versions through 0.59 (and much of this documentation) were written by:\n\nMark-Jason Dominus\n\nThis version borrows some documentation and routine names from Mark-Jason's, but Diff.pm's code\nwas completely replaced.\n\nThis code was adapted from the Smalltalk code of Mario Wolczko <mario@wolczko.com>, which is\navailable at ftp://st.cs.uiuc.edu/pub/Smalltalk/MANCHESTER/manchester/4.0/diff.st\n\n\"sdiff\" and \"traversebalanced\" were written by Mike Schilli <m@perlmeister.com>.\n\nThe algorithm is that described in *A Fast Algorithm for Computing Longest Common Subsequences*,\nCACM, vol.20, no.5, pp.350-353, May 1977, with a few minor improvements to improve the speed.\n\nMuch work was done by Ned Konz (perl@bike-nomad.com).\n\nThe OO interface and some other changes are by Tye McQueen.\n",
                "subsections": []
            }
        }
    }
}