### PatienceDiff and PatienceDiffPlus Function Signature Source: https://github.com/jontrent/patiencediff/blob/dev/README.md The core functions for both algorithms accept two arrays of strings, representing the lines of text to be compared. They return a result object detailing the differences, including inserted, deleted, and potentially moved lines. ```javascript result = patienceDiff(aLines, bLines) // or result = patienceDiffPlus(aLines, bLines) ``` -------------------------------- ### PatienceDiff and PatienceDiffPlus Source: https://github.com/jontrent/patiencediff/blob/dev/README.md These functions calculate the differences between two arrays of strings using the Patience Diff algorithm. PatienceDiffPlus additionally identifies moved lines. ```APIDOC ## PatienceDiff / PatienceDiffPlus ### Description Calculates the differences between two arrays of strings. `PatienceDiffPlus` extends this by identifying moved lines. ### Method N/A (JavaScript functions) ### Endpoint N/A (JavaScript functions) ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body - **aLines** (Array) - Required - An array of strings representing the original lines of text. - **bLines** (Array) - Required - An array of strings representing the new lines of text. ### Request Example ```javascript const result = patienceDiff(aLines, bLines); // or const resultPlus = patienceDiffPlus(aLines, bLines); ``` ### Response #### Success Response (200) - **lines** (Array) - An array of objects, each representing a line with its content and index information. - **line** (string) - The line of text. - **aIndex** (number) - The index of the line in the original `aLines` array, or -1 if added. - **bIndex** (number) - The index of the line in the new `bLines` array, or -1 if deleted. - **moved** (boolean) - True if the line was identified as moved (only applicable to `PatienceDiffPlus`). - **lineCountDeleted** (number) - The total number of lines deleted from `aLines`. - **lineCountInserted** (number) - The total number of lines inserted from `bLines`. - **lineCountMoved** (number) - The number of lines identified as moved (only applicable to `PatienceDiffPlus`). #### Response Example ```json { "lines": [ { "line": "line 1", "aIndex": 0, "bIndex": 0, "moved": false }, { "line": "line 2", "aIndex": 1, "bIndex": -1, "moved": false }, { "line": "line 3", "aIndex": -1, "bIndex": 1, "moved": false } ], "lineCountDeleted": 1, "lineCountInserted": 1, "lineCountMoved": 0 } ``` ``` -------------------------------- ### JavaScript: Patience Diff Algorithm Implementation Source: https://github.com/jontrent/patiencediff/blob/dev/PatienceDiff.html This snippet contains the core JavaScript implementation of the Patience Diff algorithm. It is used to find the longest common subsequence and generate a diff output. ```javascript var aLines = `/\/ Opening comment\n#include \n\n/\/ Frobs foo heartily\nint frobnitz(int foo)\n{\n int i;\n for(i = 0; i < 10; i++)\n {\n printf(\"Your answer is: \");\n printf(\"%d\\n\", foo);\n }\n}\n\nint fact(int n)\n{\n if(n > 1)\n {\n return fact(n-1) * n;\n }\n return 1;\n}\n\nint main(int argc, char **argv)\n{\n frobnitz(fact(10));\n}`; var bLines = `#include \n\nint fib(int n)\n{\n if(n > 2)\n {\n return fib(n-1) + fib(n-2);\n }\n return 1;\n}\n\n/\/ Frobs foo heartily\nint frobnitz(int foo)\n{\n int i;\n for(i = 0; i < 10; i++)\n {\n printf(\"%d\\n\", foo);\n }\n}\n\nint main(int argc, char **argv)\n{\n frobnitz(fib(10));\n}\n/\/ Closing comment`; function init() { document.getElementById("aLines").value = aLines; document.getElementById("bLines").value = bLines; } function calcDiff(diffVsPlus) { var a = document.getElementById("aLines").value.split("\n"); var b = document.getElementById("bLines").value.split("\n"); if (diffVsPlus) { var diff = patienceDiff(a , b); } else { var diff = patienceDiffPlus(a , b); } var diffLines = ""; diff.lines.forEach((o) => { if (o.bIndex < 0 && o.moved) { diffLines += "-m "; } else if (o.moved) { diffLines += "+m "; } else if (o.aIndex < 0) { diffLines += "+ "; } else if (o.bIndex < 0) { diffLines += "- "; } else { diffLines += " "; } diffLines += o.line + "\n"; }); document.getElementById("diff").value = diffLines; } ``` -------------------------------- ### PatienceDiff Result Object Structure Source: https://github.com/jontrent/patiencediff/blob/dev/README.md The result object from the diff functions contains an array of line objects, each with details about its content, original index, new index, and whether it was moved (for PatienceDiffPlus). It also includes counts for deleted, inserted, and moved lines. ```javascript result.lines = [ { line: 'line text', aIndex: original_index or -1, bIndex: new_index or -1, moved: true or false (only in PatienceDiffPlus) } ] result.lineCountDeleted = number result.lineCountInserted = number result.lineCountMoved = number ``` -------------------------------- ### findUnique Helper Function Source: https://github.com/jontrent/patiencediff/blob/dev/README.md A helper function used by the Patience Diff algorithm to find unique lines between two arrays within a specified range. ```APIDOC ## findUnique(arr, lo, hi) ### Description Finds unique lines within a specified range of an array using JavaScript's Map. It's a prerequisite for the Longest Common Subsequence algorithm. ### Method N/A (JavaScript function) ### Endpoint N/A (JavaScript function) ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example ```javascript const uniqueLinesMap = findUnique(myArray, 0, myArray.length); ``` ### Response #### Success Response (200) - **Map** - A JavaScript Map where keys are unique lines and values are objects containing line counts and last matched indices. #### Response Example ```javascript // Example of a Map entry (conceptual) Map { "unique line content" => { count: 1, lastIndex: 15 } } ``` ``` -------------------------------- ### PatienceDiff findUnique Function Source: https://github.com/jontrent/patiencediff/blob/dev/README.md This helper function is used by the Patience Diff algorithm to find unique lines between two arrays. It utilizes JavaScript's Map to count line occurrences and identify unique entries within a specified range (lo, hi). ```javascript findUnique(arr, lo, hi) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.