/// <summary>Scan the tables of which lines are inserted and deleted, /// producing an edit script in forward order. /// </summary> /// dynamic array private List<Difference> CreateDiffs(FileData DataA, FileData DataB) { List<Difference> a = new List<Difference>(); Difference aItem; int StartA, StartB; int LineA, LineB; LineA = 0; LineB = 0; while (LineA < DataA.LineAmount || LineB < DataB.LineAmount) { if ((LineA < DataA.LineAmount) && (!DataA.ModifiedLinesState[LineA]) && (LineB < DataB.LineAmount) && (!DataB.ModifiedLinesState[LineB])) { // equal lines LineA++; LineB++; } else { // maybe deleted and/or inserted lines StartA = LineA; StartB = LineB; while (LineA < DataA.LineAmount && (LineB >= DataB.LineAmount || DataA.ModifiedLinesState[LineA])) { LineA++; } while (LineB < DataB.LineAmount && (LineA >= DataA.LineAmount || DataB.ModifiedLinesState[LineB])) { LineB++; } if ((StartA < LineA) || (StartB < LineB)) { // store a new difference-item aItem = new Difference(StartA, StartB, LineA - StartA, LineB - StartB); a.Add(aItem); } } } return a; }
/// <summary> /// This is the divide-and-conquer implementation of the longes common-subsequence (LCS) algorithm. /// </summary> /// <param name="DataA">sequence A</param> /// <param name="LowerA">lower bound of the actual range in DataA</param> /// <param name="UpperA">upper bound of the actual range in DataA (exclusive)</param> /// <param name="DataB">sequence B</param> /// <param name="LowerB">lower bound of the actual range in DataB</param> /// <param name="UpperB">upper bound of the actual range in DataB (exclusive)</param> /// <param name="DownVector">a vector for the (0,0) to (x,y) search. Passed as a parameter for speed reasons.</param> /// <param name="UpVector">a vector for the (u,v) to (N,M) search. Passed as a parameter for speed reasons.</param> private void LCS(FileData DataA, int LowerA, int UpperA, FileData DataB, int LowerB, int UpperB, int[] DownVector, int[] UpVector) { // Fast walkthrough equal lines at the start while (LowerA < UpperA && LowerB < UpperB && DataA.LinesNumbers[LowerA] == DataB.LinesNumbers[LowerB]) { LowerA++; LowerB++; } // Fast walkthrough equal lines at the end while (LowerA < UpperA && LowerB < UpperB && DataA.LinesNumbers[UpperA - 1] == DataB.LinesNumbers[UpperB - 1]) { --UpperA; --UpperB; } if (LowerA == UpperA) { // mark as inserted lines. while (LowerB < UpperB) { DataB.ModifiedLinesState.Insert(LowerB++, true); } } else if (LowerB == UpperB) { // mark as deleted lines. while (LowerA < UpperA) { DataA.ModifiedLinesState.Insert(LowerA++, true); } } else { // Find the middle snakea and length of an optimal path for A and B SMSRD smsrd = GenerateSMSCoords(DataA, LowerA, UpperA, DataB, LowerB, UpperB, DownVector, UpVector); // The path is from LowerX to (x,y) and (x,y) to UpperX LCS(DataA, LowerA, smsrd.GetX, DataB, LowerB, smsrd.GetY, DownVector, UpVector); LCS(DataA, smsrd.GetX, UpperA, DataB, smsrd.GetY, UpperB, DownVector, UpVector); } }
/// <summary> /// Method generates SMS coords /// </summary> /// <param name="firstFileData"></param> /// <param name="LowerA"></param> /// <param name="UpperA"></param> /// <param name="secondFileData"></param> /// <param name="LowerB"></param> /// <param name="UpperB"></param> /// <param name="DownVector"></param> /// <param name="UpVector"></param> /// <returns></returns> private static SMSRD GenerateSMSCoords(FileData firstFileData, int LowerA, int UpperA, FileData secondFileData, int LowerB, int UpperB, int[] DownVector, int[] UpVector) { int linesSummary = firstFileData.LineAmount + secondFileData.LineAmount + 1; int DownK = LowerA - LowerB; // the k-line to start the forward search int UpK = UpperA - UpperB; // the k-line to start the reverse search int Delta = (UpperA - LowerA) - (UpperB - LowerB); bool oddDelta = (Delta & 1) != 0; // The vectors in the publication accepts negative indexes. the vectors implemented here are 0-based // and are access using a specific offset: UpOffset UpVector and DownOffset for DownVektor int DownOffset = linesSummary - DownK; int UpOffset = linesSummary - UpK; int MaxD = ((UpperA - LowerA + UpperB - LowerB) / 2) + 1; // init vectors DownVector[DownOffset + DownK + 1] = LowerA; UpVector[UpOffset + UpK - 1] = UpperA; for (int D = 0; D <= MaxD; D++) { // Extend the forward path. for (int k = DownK - D; k <= DownK + D; k += 2) { // find the only or better starting point int x, y; if (k == DownK - D) { // down x = DownVector[DownOffset + k + 1]; } else { // a step to the right x = DownVector[DownOffset + k - 1] + 1; if ((k < DownK + D) && (DownVector[DownOffset + k + 1] >= x)) { // down x = DownVector[DownOffset + k + 1]; } } y = x - k; // find the end of the furthest reaching forward D-path in diagonal k. while ((x < UpperA) && (y < UpperB) && (firstFileData.LinesNumbers[x] == secondFileData.LinesNumbers[y])) { x++; y++; } DownVector[DownOffset + k] = x; // overlap if (oddDelta && (UpK - D < k) && (k < UpK + D)) { if (UpVector[UpOffset + k] <= DownVector[DownOffset + k]) { return new SMSRD(DownVector[DownOffset + k], DownVector[DownOffset + k] - k); } } } // Extend the reverse path. for (int k = UpK - D; k <= UpK + D; k += 2) { // find the only or better starting point int x, y; if (k == UpK + D) { // up x = UpVector[UpOffset + k - 1]; } else { // left x = UpVector[UpOffset + k + 1] - 1; if ((k > UpK - D) && (UpVector[UpOffset + k - 1] < x)) { // up x = UpVector[UpOffset + k - 1]; } } y = x - k; while ((x > LowerA) && (y > LowerB) && (firstFileData.LinesNumbers[x - 1] == secondFileData.LinesNumbers[y - 1])) { // diagonal x--; y--; } UpVector[UpOffset + k] = x; // overlap if (!oddDelta && (DownK - D <= k) && (k <= DownK + D)) { if (UpVector[UpOffset + k] <= DownVector[DownOffset + k]) { return new SMSRD(DownVector[DownOffset + k], DownVector[DownOffset + k] - k); } } } } return null; }
/// <summary> /// Find the difference in 2 arrays of integers. /// </summary> /// <param name="ArrayA">A-version of the numbers (usualy the old one)</param> /// <param name="ArrayB">B-version of the numbers (usualy the new one)</param> /// <returns>Returns a array of Items that describe the differences.</returns> private List<Difference> DiffInt(List<int> ArrayA, List<int> ArrayB) { // The A-Version of the data (original data) to be compared. FileData DataA = new FileData(ArrayA); // The B-Version of the data (modified data) to be compared. FileData DataB = new FileData(ArrayB); int MAX = DataA.LineAmount + DataB.LineAmount + 1; /// vector for the (0,0) to (x,y) search int[] DownVector = new int[2 * MAX + 2]; /// vector for the (u,v) to (N,M) search int[] UpVector = new int[2 * MAX + 2]; LCS(DataA, 0, DataA.LineAmount, DataB, 0, DataB.LineAmount, DownVector, UpVector); return CreateDiffs(DataA, DataB); }
/// <summary> /// If a sequence of modified lines starts with a line that contains the same content /// as the line that appends the changes, the difference sequence is modified so that the /// appended line and not the starting line is marked as modified. /// This leads to more readable diff sequences when comparing text files. /// </summary> /// <param name="Data">A Diff data buffer containing the identified changes.</param> private void Optimize(FileData Data) { int StartPos, EndPos; StartPos = 0; while (StartPos < Data.LineAmount) { while ((StartPos < Data.LineAmount) && (Data.ModifiedLinesState[StartPos] == false)) { StartPos++; } EndPos = StartPos; while ((EndPos < Data.LineAmount) && (Data.ModifiedLinesState[EndPos] == true)) { EndPos++; } if ((EndPos < Data.LineAmount) && (Data.LinesNumbers[StartPos] == Data.LinesNumbers[EndPos])) { Data.ModifiedLinesState[StartPos] = false; Data.ModifiedLinesState[EndPos] = true; } else { StartPos = EndPos; } } }
/// <summary> /// Find the difference in 2 text documents, comparing by textlines. /// The algorithm itself is comparing 2 arrays of numbers so when comparing 2 text documents /// each line is converted into a (hash) number. This hash-value is computed by storing all /// textlines into a common hashtable so i can find dublicates in there, and generating a /// new number each time a new textline is inserted. /// </summary> /// <param name="TextA">A-version of the text (usualy the old one)</param> /// <param name="TextB">B-version of the text (usualy the new one)</param> /// <param name="trimSpace">When set to true, all leading and trailing whitespace characters are stripped out before the comparation is done.</param> /// <param name="ignoreSpace">When set to true, all whitespace characters are converted to a single space character before the comparation is done.</param> /// <param name="ignoreCase">When set to true, all characters are converted to their lowercase equivivalence before the comparation is done.</param> /// <returns>Returns a array of Differences that describe the differences.</returns> private List<Difference> DiffText(List<string> TextA, List<string> TextB, bool ignoreCase) { // prepare the input-text and convert to comparable numbers. Hashtable h = new Hashtable(GetFileLength(TextA) + GetFileLength(TextB)); // The A-Version of the data (original data) to be compared. FileData DataA = new FileData(DiffCodes(TextA, h, ignoreCase)); // The B-Version of the data (modified data) to be compared. FileData DataB = new FileData(DiffCodes(TextB, h, ignoreCase)); h = null; // free up hashtable memory (maybe) int MAX = DataA.LineAmount + DataB.LineAmount + 1; /// vector for the (0,0) to (x,y) search int[] DownVector = new int[2 * MAX + 2]; /// vector for the (u,v) to (N,M) search int[] UpVector = new int[2 * MAX + 2]; LCS(DataA, 0, DataA.LineAmount, DataB, 0, DataB.LineAmount, DownVector, UpVector); Optimize(DataA); Optimize(DataB); return CreateDiffs(DataA, DataB); }