| 1 | namespace my.utils |
| 2 | { |
| 3 | using System; |
| 4 | using System.Collections; |
| 5 | using System.Text; |
| 6 | using System.Text.RegularExpressions; |
| 7 | |
| 8 | /// <summary> |
| 9 | /// This Class implements the Difference Algorithm published in |
| 10 | /// "An O(ND) Difference Algorithm and its Variations" by Eugene Myers |
| 11 | /// Algorithmica Vol. 1 No. 2, 1986, p 251. |
| 12 | /// |
| 13 | /// There are many C, Java, Lisp implementations public available but they all seem to come |
| 14 | /// from the same source (diffutils) that is under the (unfree) GNU public License |
| 15 | /// and cannot be reused as a sourcecode for a commercial application. |
| 16 | /// There are very old C implementations that use other (worse) algorithms. |
| 17 | /// Microsoft also published sourcecode of a diff-tool (windiff) that uses some tree data. |
| 18 | /// Also, a direct transfer from a C source to C# is not easy because there is a lot of pointer |
| 19 | /// arithmetic in the typical C solutions and i need a managed solution. |
| 20 | /// These are the reasons why I implemented the original published algorithm from the scratch and |
| 21 | /// make it avaliable without the GNU license limitations. |
| 22 | /// I do not need a high performance diff tool because it is used only sometimes. |
| 23 | /// I will do some performace tweaking when needed. |
| 24 | /// |
| 25 | /// The algorithm itself is comparing 2 arrays of numbers so when comparing 2 text documents |
| 26 | /// each line is converted into a (hash) number. See DiffText(). |
| 27 | /// |
| 28 | /// Some chages to the original algorithm: |
| 29 | /// The original algorithm was described using a recursive approach and comparing zero indexed arrays. |
| 30 | /// Extracting sub-arrays and rejoining them is very performance and memory intensive so the same |
| 31 | /// (readonly) data arrays are passed arround together with their lower and upper bounds. |
| 32 | /// This circumstance makes the LCS and SMS functions more complicate. |
| 33 | /// I added some code to the LCS function to get a fast response on sub-arrays that are identical, |
| 34 | /// completely deleted or inserted. |
| 35 | /// |
| 36 | /// The result from a comparisation is stored in 2 arrays that flag for modified (deleted or inserted) |
| 37 | /// lines in the 2 data arrays. These bits are then analysed to produce a array of Item objects. |
| 38 | /// |
| 39 | /// Further possible optimizations: |
| 40 | /// (first rule: don't do it; second: don't do it yet) |
| 41 | /// The arrays DataA and DataB are passed as parameters, but are never changed after the creation |
| 42 | /// so they can be members of the class to avoid the paramter overhead. |
| 43 | /// In SMS is a lot of boundary arithmetic in the for-D and for-k loops that can be done by increment |
| 44 | /// and decrement of local variables. |
| 45 | /// The DownVector and UpVector arrays are alywas created and destroyed each time the SMS gets called. |
| 46 | /// It is possible to reuse tehm when transfering them to members of the class. |
| 47 | /// See TODO: hints. |
| 48 | /// |
| 49 | /// diff.cs: A port of the algorythm to C# |
| 50 | /// Copyright (c) by Matthias Hertel, http://www.mathertel.de |
| 51 | /// This work is licensed under a BSD style license. See http://www.mathertel.de/License.aspx |
| 52 | /// |
| 53 | /// Changes: |
| 54 | /// 2002.09.20 There was a "hang" in some situations. |
| 55 | /// Now I undestand a little bit more of the SMS algorithm. |
| 56 | /// There have been overlapping boxes; that where analyzed partial differently. |
| 57 | /// One return-point is enough. |
| 58 | /// A assertion was added in CreateDiffs when in debug-mode, that counts the number of equal (no modified) lines in both arrays. |
| 59 | /// They must be identical. |
| 60 | /// |
| 61 | /// 2003.02.07 Out of bounds error in the Up/Down vector arrays in some situations. |
| 62 | /// The two vetors are now accessed using different offsets that are adjusted using the start k-Line. |
| 63 | /// A test case is added. |
| 64 | /// |
| 65 | /// 2006.03.05 Some documentation and a direct Diff entry point. |
| 66 | /// |
| 67 | /// 2006.03.08 Refactored the API to static methods on the Diff class to make usage simpler. |
| 68 | /// 2006.03.10 using the standard Debug class for self-test now. |
| 69 | /// compile with: csc /target:exe /out:diffTest.exe /d:DEBUG /d:TRACE /d:SELFTEST Diff.cs |
| 70 | /// 2007.01.06 license agreement changed to a BSD style license. |
| 71 | /// 2007.06.03 added the Optimize method. |
| 72 | /// 2007.09.23 UpVector and DownVector optimization by Jan Stoklasa (). |
| 73 | /// 2008.05.31 Adjusted the testing code that failed because of the Optimize method (not a bug in the diff algorithm). |
| 74 | /// 2008.10.08 Fixing a test case and adding a new test case. |
| 75 | /// </summary> |
| 76 | |
| 77 | public class Diff |
| 78 | { |
| 79 | |
| 80 | /// <summary>details of one difference.</summary> |
| 81 | public struct Item |
| 82 | { |
| 83 | /// <summary>Start Line number in Data A.</summary> |
| 84 | public int StartA; |
| 85 | /// <summary>Start Line number in Data B.</summary> |
| 86 | public int StartB; |
| 87 | |
| 88 | /// <summary>Number of changes in Data A.</summary> |
| 89 | public int deletedA; |
| 90 | /// <summary>Number of changes in Data B.</summary> |
| 91 | public int insertedB; |
| 92 | } // Item |
| 93 | |
| 94 | /// <summary> |
| 95 | /// Shortest Middle Snake Return Data |
| 96 | /// </summary> |
| 97 | private struct SMSRD |
| 98 | { |
| 99 | internal int x, y; |
| 100 | // internal int u, v; // 2002.09.20: no need for 2 points |
| 101 | } |
| 102 | |
| 103 | |
| 104 | #region self-Test |
| 105 | |
| 106 | #if (SELFTEST) |
| 107 | /// <summary> |
| 108 | /// start a self- / box-test for some diff cases and report to the debug output. |
| 109 | /// </summary> |
| 110 | /// <param name="args">not used</param> |
| 111 | /// <returns>always 0</returns> |
| 112 | public static int Main(string[] args) { |
| 113 | StringBuilder ret = new StringBuilder(); |
| 114 | string a, b; |
| 115 | |
| 116 | System.Diagnostics.ConsoleTraceListener ctl = new System.Diagnostics.ConsoleTraceListener(false); |
| 117 | System.Diagnostics.Debug.Listeners.Add(ctl); |
| 118 | |
| 119 | System.Console.WriteLine("Diff Self Test..."); |
| 120 | |
| 121 | // test all changes |
| 122 | a = "a,b,c,d,e,f,g,h,i,j,k,l".Replace(',', '\n'); |
| 123 | b = "0,1,2,3,4,5,6,7,8,9".Replace(',', '\n'); |
| 124 | System.Diagnostics.Debug.Assert(TestHelper(Diff.DiffText(a, b, false, false, false)) |
| 125 | == "12.10.0.0*", |
| 126 | "all-changes test failed."); |
| 127 | System.Diagnostics.Debug.WriteLine("all-changes test passed."); |
| 128 | // test all same |
| 129 | a = "a,b,c,d,e,f,g,h,i,j,k,l".Replace(',', '\n'); |
| 130 | b = a; |
| 131 | System.Diagnostics.Debug.Assert(TestHelper(Diff.DiffText(a, b, false, false, false)) |
| 132 | == "", |
| 133 | "all-same test failed."); |
| 134 | System.Diagnostics.Debug.WriteLine("all-same test passed."); |
| 135 | |
| 136 | // test snake |
| 137 | a = "a,b,c,d,e,f".Replace(',', '\n'); |
| 138 | b = "b,c,d,e,f,x".Replace(',', '\n'); |
| 139 | System.Diagnostics.Debug.Assert(TestHelper(Diff.DiffText(a, b, false, false, false)) |
| 140 | == "1.0.0.0*0.1.6.5*", |
| 141 | "snake test failed."); |
| 142 | System.Diagnostics.Debug.WriteLine("snake test passed."); |
| 143 | |
| 144 | // 2002.09.20 - repro |
| 145 | a = "c1,a,c2,b,c,d,e,g,h,i,j,c3,k,l".Replace(',', '\n'); |
| 146 | b = "C1,a,C2,b,c,d,e,I1,e,g,h,i,j,C3,k,I2,l".Replace(',', '\n'); |
| 147 | System.Diagnostics.Debug.Assert(TestHelper(Diff.DiffText(a, b, false, false, false)) |
| 148 | == "1.1.0.0*1.1.2.2*0.2.7.7*1.1.11.13*0.1.13.15*", |
| 149 | "repro20020920 test failed."); |
| 150 | System.Diagnostics.Debug.WriteLine("repro20020920 test passed."); |
| 151 | |
| 152 | // 2003.02.07 - repro |
| 153 | a = "F".Replace(',', '\n'); |
| 154 | b = "0,F,1,2,3,4,5,6,7".Replace(',', '\n'); |
| 155 | System.Diagnostics.Debug.Assert(TestHelper(Diff.DiffText(a, b, false, false, false)) |
| 156 | == "0.1.0.0*0.7.1.2*", |
| 157 | "repro20030207 test failed."); |
| 158 | System.Diagnostics.Debug.WriteLine("repro20030207 test passed."); |
| 159 | |
| 160 | // Muegel - repro |
| 161 | a = "HELLO\nWORLD"; |
| 162 | b = "\n\nhello\n\n\n\nworld\n"; |
| 163 | System.Diagnostics.Debug.Assert(TestHelper(Diff.DiffText(a, b, false, false, false)) |
| 164 | == "2.8.0.0*", |
| 165 | "repro20030409 test failed."); |
| 166 | System.Diagnostics.Debug.WriteLine("repro20030409 test passed."); |
| 167 | |
| 168 | // test some differences |
| 169 | a = "a,b,-,c,d,e,f,f".Replace(',', '\n'); |
| 170 | b = "a,b,x,c,e,f".Replace(',', '\n'); |
| 171 | System.Diagnostics.Debug.Assert(TestHelper(Diff.DiffText(a, b, false, false, false)) |
| == "1.1.2.2*1.0.4.4*1.0.6.5*", | |
| 172 | == "1.1.2.2*1.0.4.4*1.0.7.6*", |
| 173 | "some-changes test failed."); |
| 174 | System.Diagnostics.Debug.WriteLine("some-changes test passed."); |
| 175 | |
| 176 | // test one change within long chain of repeats |
| 177 | a = "a,a,a,a,a,a,a,a,a,a".Replace(',', '\n'); |
| 178 | b = "a,a,a,a,-,a,a,a,a,a".Replace(',', '\n'); |
| 179 | System.Diagnostics.Debug.Assert(TestHelper(Diff.DiffText(a, b, false, false, false)) |
| 180 | == "0.1.4.4*1.0.9.10*", |
| 181 | "long chain of repeats test failed."); |
| 182 | |
| 183 | System.Diagnostics.Debug.WriteLine("End."); |
| 184 | System.Diagnostics.Debug.Flush(); |
| 185 | |
| 186 | return (0); |
| 187 | } |
| 188 | |
| 189 | |
| 190 | public static string TestHelper(Item []f) { |
| 191 | StringBuilder ret = new StringBuilder(); |
| 192 | for (int n = 0; n < f.Length; n++) { |
| 193 | ret.Append(f[n].deletedA.ToString() + "." + f[n].insertedB.ToString() + "." + f[n].StartA.ToString() + "." + f[n].StartB.ToString() + "*"); |
| 194 | } |
| 195 | // Debug.Write(5, "TestHelper", ret.ToString()); |
| 196 | return (ret.ToString()); |
| 197 | } |
| 198 | #endif |
| 199 | #endregion |
| 200 | |
| 201 | |
| 202 | /// <summary> |
| 203 | /// Find the difference in 2 texts, comparing by textlines. |
| 204 | /// </summary> |
| 205 | /// <param name="TextA">A-version of the text (usualy the old one)</param> |
| 206 | /// <param name="TextB">B-version of the text (usualy the new one)</param> |
| 207 | /// <returns>Returns a array of Items that describe the differences.</returns> |
| 208 | public Item[] DiffText(string TextA, string TextB) { |
| 209 | return (DiffText(TextA, TextB, false, false, false)); |
| 210 | } // DiffText |
| 211 | |
| 212 | |
| 213 | /// <summary> |
| 214 | /// Find the difference in 2 text documents, comparing by textlines. |
| 215 | /// The algorithm itself is comparing 2 arrays of numbers so when comparing 2 text documents |
| 216 | /// each line is converted into a (hash) number. This hash-value is computed by storing all |
| 217 | /// textlines into a common hashtable so i can find dublicates in there, and generating a |
| 218 | /// new number each time a new textline is inserted. |
| 219 | /// </summary> |
| 220 | /// <param name="TextA">A-version of the text (usualy the old one)</param> |
| 221 | /// <param name="TextB">B-version of the text (usualy the new one)</param> |
| 222 | /// <param name="trimSpace">When set to true, all leading and trailing whitespace characters are stripped out before the comparation is done.</param> |
| 223 | /// <param name="ignoreSpace">When set to true, all whitespace characters are converted to a single space character before the comparation is done.</param> |
| 224 | /// <param name="ignoreCase">When set to true, all characters are converted to their lowercase equivivalence before the comparation is done.</param> |
| 225 | /// <returns>Returns a array of Items that describe the differences.</returns> |
| 226 | public static Item[] DiffText(string TextA, string TextB, bool trimSpace, bool ignoreSpace, bool ignoreCase) { |
| 227 | // prepare the input-text and convert to comparable numbers. |
| 228 | Hashtable h = new Hashtable(TextA.Length + TextB.Length); |
| 229 | |
| 230 | // The A-Version of the data (original data) to be compared. |
| 231 | DiffData DataA = new DiffData(DiffCodes(TextA, h, trimSpace, ignoreSpace, ignoreCase)); |
| 232 | |
| 233 | // The B-Version of the data (modified data) to be compared. |
| 234 | DiffData DataB = new DiffData(DiffCodes(TextB, h, trimSpace, ignoreSpace, ignoreCase)); |
| 235 | |
| 236 | h = null; // free up hashtable memory (maybe) |
| 237 | |
| 238 | int MAX = DataA.Length + DataB.Length + 1; |
| 239 | /// vector for the (0,0) to (x,y) search |
| 240 | int[] DownVector = new int[2 * MAX + 2]; |
| 241 | /// vector for the (u,v) to (N,M) search |
| 242 | int[] UpVector = new int[2 * MAX + 2]; |
| 243 | |
| 244 | LCS(DataA, 0, DataA.Length, DataB, 0, DataB.Length, DownVector, UpVector); |
| 245 | |
| 246 | Optimize(DataA); |
| 247 | Optimize(DataB); |
| 248 | return CreateDiffs(DataA, DataB); |
| 249 | } // DiffText |
| 250 | |
| 251 | |
| 252 | /// <summary> |
| 253 | /// If a sequence of modified lines starts with a line that contains the same content |
| 254 | /// as the line that appends the changes, the difference sequence is modified so that the |
| 255 | /// appended line and not the starting line is marked as modified. |
| 256 | /// This leads to more readable diff sequences when comparing text files. |
| 257 | /// </summary> |
| 258 | /// <param name="Data">A Diff data buffer containing the identified changes.</param> |
| 259 | private static void Optimize(DiffData Data) { |
| 260 | int StartPos, EndPos; |
| 261 | |
| 262 | StartPos = 0; |
| 263 | while (StartPos < Data.Length) { |
| 264 | while ((StartPos < Data.Length) && (Data.modified[StartPos] == false)) |
| 265 | StartPos++; |
| 266 | EndPos = StartPos; |
| 267 | while ((EndPos < Data.Length) && (Data.modified[EndPos] == true)) |
| 268 | EndPos++; |
| 269 | |
| 270 | if ((EndPos < Data.Length) && (Data.data[StartPos] == Data.data[EndPos])) { |
| 271 | Data.modified[StartPos] = false; |
| 272 | Data.modified[EndPos] = true; |
| 273 | } else { |
| 274 | StartPos = EndPos; |
| 275 | } // if |
| 276 | } // while |
| 277 | } // Optimize |
| 278 | |
| 279 | |
| 280 | /// <summary> |
| 281 | /// Find the difference in 2 arrays of integers. |
| 282 | /// </summary> |
| 283 | /// <param name="ArrayA">A-version of the numbers (usualy the old one)</param> |
| 284 | /// <param name="ArrayB">B-version of the numbers (usualy the new one)</param> |
| 285 | /// <returns>Returns a array of Items that describe the differences.</returns> |
| 286 | public static Item[] DiffInt(int[] ArrayA, int[] ArrayB) { |
| 287 | // The A-Version of the data (original data) to be compared. |
| 288 | DiffData DataA = new DiffData(ArrayA); |
| 289 | |
| 290 | // The B-Version of the data (modified data) to be compared. |
| 291 | DiffData DataB = new DiffData(ArrayB); |
| 292 | |
| 293 | int MAX = DataA.Length + DataB.Length + 1; |
| 294 | /// vector for the (0,0) to (x,y) search |
| 295 | int[] DownVector = new int[2 * MAX + 2]; |
| 296 | /// vector for the (u,v) to (N,M) search |
| 297 | int[] UpVector = new int[2 * MAX + 2]; |
| 298 | |
| 299 | LCS(DataA, 0, DataA.Length, DataB, 0, DataB.Length, DownVector, UpVector); |
| 300 | return CreateDiffs(DataA, DataB); |
| 301 | } // Diff |
| 302 | |
| 303 | |
| 304 | /// <summary> |
| 305 | /// This function converts all textlines of the text into unique numbers for every unique textline |
| 306 | /// so further work can work only with simple numbers. |
| 307 | /// </summary> |
| 308 | /// <param name="aText">the input text</param> |
| 309 | /// <param name="h">This extern initialized hashtable is used for storing all ever used textlines.</param> |
| 310 | /// <param name="trimSpace">ignore leading and trailing space characters</param> |
| 311 | /// <returns>a array of integers.</returns> |
| 312 | private static int[] DiffCodes(string aText, Hashtable h, bool trimSpace, bool ignoreSpace, bool ignoreCase) { |
| 313 | // get all codes of the text |
| 314 | string[] Lines; |
| 315 | int[] Codes; |
| 316 | int lastUsedCode = h.Count; |
| 317 | object aCode; |
| 318 | string s; |
| 319 | |
| 320 | // strip off all cr, only use lf as textline separator. |
| 321 | aText = aText.Replace("\r", ""); |
| 322 | Lines = aText.Split('\n'); |
| 323 | |
| 324 | Codes = new int[Lines.Length]; |
| 325 | |
| 326 | for (int i = 0; i < Lines.Length; ++i) { |
| 327 | s = Lines[i]; |
| 328 | if (trimSpace) |
| 329 | s = s.Trim(); |
| 330 | |
| 331 | if (ignoreSpace) { |
| 332 | s = Regex.Replace(s, "\\s+", " "); // TODO: optimization: faster blank removal. |
| 333 | } |
| 334 | |
| 335 | if (ignoreCase) |
| 336 | s = s.ToLower(); |
| 337 | |
| 338 | aCode = h[s]; |
| 339 | if (aCode == null) { |
| 340 | lastUsedCode++; |
| 341 | h[s] = lastUsedCode; |
| 342 | Codes[i] = lastUsedCode; |
| 343 | } else { |
| 344 | Codes[i] = (int)aCode; |
| 345 | } // if |
| 346 | } // for |
| 347 | return (Codes); |
| 348 | } // DiffCodes |
| 349 | |
| 350 | |
| 351 | /// <summary> |
| 352 | /// This is the algorithm to find the Shortest Middle Snake (SMS). |
| 353 | /// </summary> |
| 354 | /// <param name="DataA">sequence A</param> |
| 355 | /// <param name="LowerA">lower bound of the actual range in DataA</param> |
| 356 | /// <param name="UpperA">upper bound of the actual range in DataA (exclusive)</param> |
| 357 | /// <param name="DataB">sequence B</param> |
| 358 | /// <param name="LowerB">lower bound of the actual range in DataB</param> |
| 359 | /// <param name="UpperB">upper bound of the actual range in DataB (exclusive)</param> |
| 360 | /// <param name="DownVector">a vector for the (0,0) to (x,y) search. Passed as a parameter for speed reasons.</param> |
| 361 | /// <param name="UpVector">a vector for the (u,v) to (N,M) search. Passed as a parameter for speed reasons.</param> |
| 362 | /// <returns>a MiddleSnakeData record containing x,y and u,v</returns> |
| 363 | private static SMSRD SMS(DiffData DataA, int LowerA, int UpperA, DiffData DataB, int LowerB, int UpperB, |
| 364 | int[] DownVector, int[] UpVector) { |
| 365 | |
| 366 | SMSRD ret; |
| 367 | int MAX = DataA.Length + DataB.Length + 1; |
| 368 | |
| 369 | int DownK = LowerA - LowerB; // the k-line to start the forward search |
| 370 | int UpK = UpperA - UpperB; // the k-line to start the reverse search |
| 371 | |
| 372 | int Delta = (UpperA - LowerA) - (UpperB - LowerB); |
| 373 | bool oddDelta = (Delta & 1) != 0; |
| 374 | |
| 375 | // The vectors in the publication accepts negative indexes. the vectors implemented here are 0-based |
| 376 | // and are access using a specific offset: UpOffset UpVector and DownOffset for DownVektor |
| 377 | int DownOffset = MAX - DownK; |
| 378 | int UpOffset = MAX - UpK; |
| 379 | |
| 380 | int MaxD = ((UpperA - LowerA + UpperB - LowerB) / 2) + 1; |
| 381 | |
| 382 | // Debug.Write(2, "SMS", String.Format("Search the box: A[{0}-{1}] to B[{2}-{3}]", LowerA, UpperA, LowerB, UpperB)); |
| 383 | |
| 384 | // init vectors |
| 385 | DownVector[DownOffset + DownK + 1] = LowerA; |
| 386 | UpVector[UpOffset + UpK - 1] = UpperA; |
| 387 | |
| 388 | for (int D = 0; D <= MaxD; D++) { |
| 389 | |
| 390 | // Extend the forward path. |
| 391 | for (int k = DownK - D; k <= DownK + D; k += 2) { |
| 392 | // Debug.Write(0, "SMS", "extend forward path " + k.ToString()); |
| 393 | |
| 394 | // find the only or better starting point |
| 395 | int x, y; |
| 396 | if (k == DownK - D) { |
| 397 | x = DownVector[DownOffset + k + 1]; // down |
| 398 | } else { |
| 399 | x = DownVector[DownOffset + k - 1] + 1; // a step to the right |
| 400 | if ((k < DownK + D) && (DownVector[DownOffset + k + 1] >= x)) |
| 401 | x = DownVector[DownOffset + k + 1]; // down |
| 402 | } |
| 403 | y = x - k; |
| 404 | |
| 405 | // find the end of the furthest reaching forward D-path in diagonal k. |
| 406 | while ((x < UpperA) && (y < UpperB) && (DataA.data[x] == DataB.data[y])) { |
| 407 | x++; y++; |
| 408 | } |
| 409 | DownVector[DownOffset + k] = x; |
| 410 | |
| 411 | // overlap ? |
| 412 | if (oddDelta && (UpK - D < k) && (k < UpK + D)) { |
| 413 | if (UpVector[UpOffset + k] <= DownVector[DownOffset + k]) { |
| 414 | ret.x = DownVector[DownOffset + k]; |
| 415 | ret.y = DownVector[DownOffset + k] - k; |
| 416 | // ret.u = UpVector[UpOffset + k]; // 2002.09.20: no need for 2 points |
| 417 | // ret.v = UpVector[UpOffset + k] - k; |
| 418 | return (ret); |
| 419 | } // if |
| 420 | } // if |
| 421 | |
| 422 | } // for k |
| 423 | |
| 424 | // Extend the reverse path. |
| 425 | for (int k = UpK - D; k <= UpK + D; k += 2) { |
| 426 | // Debug.Write(0, "SMS", "extend reverse path " + k.ToString()); |
| 427 | |
| 428 | // find the only or better starting point |
| 429 | int x, y; |
| 430 | if (k == UpK + D) { |
| 431 | x = UpVector[UpOffset + k - 1]; // up |
| 432 | } else { |
| 433 | x = UpVector[UpOffset + k + 1] - 1; // left |
| 434 | if ((k > UpK - D) && (UpVector[UpOffset + k - 1] < x)) |
| 435 | x = UpVector[UpOffset + k - 1]; // up |
| 436 | } // if |
| 437 | y = x - k; |
| 438 | |
| 439 | while ((x > LowerA) && (y > LowerB) && (DataA.data[x - 1] == DataB.data[y - 1])) { |
| 440 | x--; y--; // diagonal |
| 441 | } |
| 442 | UpVector[UpOffset + k] = x; |
| 443 | |
| 444 | // overlap ? |
| 445 | if (!oddDelta && (DownK - D <= k) && (k <= DownK + D)) { |
| 446 | if (UpVector[UpOffset + k] <= DownVector[DownOffset + k]) { |
| 447 | ret.x = DownVector[DownOffset + k]; |
| 448 | ret.y = DownVector[DownOffset + k] - k; |
| 449 | // ret.u = UpVector[UpOffset + k]; // 2002.09.20: no need for 2 points |
| 450 | // ret.v = UpVector[UpOffset + k] - k; |
| 451 | return (ret); |
| 452 | } // if |
| 453 | } // if |
| 454 | |
| 455 | } // for k |
| 456 | |
| 457 | } // for D |
| 458 | |
| 459 | throw new ApplicationException("the algorithm should never come here."); |
| 460 | } // SMS |
| 461 | |
| 462 | |
| 463 | /// <summary> |
| 464 | /// This is the divide-and-conquer implementation of the longes common-subsequence (LCS) |
| 465 | /// algorithm. |
| 466 | /// The published algorithm passes recursively parts of the A and B sequences. |
| 467 | /// To avoid copying these arrays the lower and upper bounds are passed while the sequences stay constant. |
| 468 | /// </summary> |
| 469 | /// <param name="DataA">sequence A</param> |
| 470 | /// <param name="LowerA">lower bound of the actual range in DataA</param> |
| 471 | /// <param name="UpperA">upper bound of the actual range in DataA (exclusive)</param> |
| 472 | /// <param name="DataB">sequence B</param> |
| 473 | /// <param name="LowerB">lower bound of the actual range in DataB</param> |
| 474 | /// <param name="UpperB">upper bound of the actual range in DataB (exclusive)</param> |
| 475 | /// <param name="DownVector">a vector for the (0,0) to (x,y) search. Passed as a parameter for speed reasons.</param> |
| 476 | /// <param name="UpVector">a vector for the (u,v) to (N,M) search. Passed as a parameter for speed reasons.</param> |
| 477 | private static void LCS(DiffData DataA, int LowerA, int UpperA, DiffData DataB, int LowerB, int UpperB, int[] DownVector, int[] UpVector) { |
| 478 | // Debug.Write(2, "LCS", String.Format("Analyse the box: A[{0}-{1}] to B[{2}-{3}]", LowerA, UpperA, LowerB, UpperB)); |
| 479 | |
| 480 | // Fast walkthrough equal lines at the start |
| 481 | while (LowerA < UpperA && LowerB < UpperB && DataA.data[LowerA] == DataB.data[LowerB]) { |
| 482 | LowerA++; LowerB++; |
| 483 | } |
| 484 | |
| 485 | // Fast walkthrough equal lines at the end |
| 486 | while (LowerA < UpperA && LowerB < UpperB && DataA.data[UpperA - 1] == DataB.data[UpperB - 1]) { |
| 487 | --UpperA; --UpperB; |
| 488 | } |
| 489 | |
| 490 | if (LowerA == UpperA) { |
| 491 | // mark as inserted lines. |
| 492 | while (LowerB < UpperB) |
| 493 | DataB.modified[LowerB++] = true; |
| 494 | |
| 495 | } else if (LowerB == UpperB) { |
| 496 | // mark as deleted lines. |
| 497 | while (LowerA < UpperA) |
| 498 | DataA.modified[LowerA++] = true; |
| 499 | |
| 500 | } else { |
| 501 | // Find the middle snakea and length of an optimal path for A and B |
| 502 | SMSRD smsrd = SMS(DataA, LowerA, UpperA, DataB, LowerB, UpperB, DownVector, UpVector); |
| 503 | // Debug.Write(2, "MiddleSnakeData", String.Format("{0},{1}", smsrd.x, smsrd.y)); |
| 504 | |
| 505 | // The path is from LowerX to (x,y) and (x,y) to UpperX |
| 506 | LCS(DataA, LowerA, smsrd.x, DataB, LowerB, smsrd.y, DownVector, UpVector); |
| 507 | LCS(DataA, smsrd.x, UpperA, DataB, smsrd.y, UpperB, DownVector, UpVector); // 2002.09.20: no need for 2 points |
| 508 | } |
| 509 | } // LCS() |
| 510 | |
| 511 | |
| 512 | /// <summary>Scan the tables of which lines are inserted and deleted, |
| 513 | /// producing an edit script in forward order. |
| 514 | /// </summary> |
| 515 | /// dynamic array |
| 516 | private static Item[] CreateDiffs(DiffData DataA, DiffData DataB) { |
| 517 | ArrayList a = new ArrayList(); |
| 518 | Item aItem; |
| 519 | Item[] result; |
| 520 | |
| 521 | int StartA, StartB; |
| 522 | int LineA, LineB; |
| 523 | |
| 524 | LineA = 0; |
| 525 | LineB = 0; |
| 526 | while (LineA < DataA.Length || LineB < DataB.Length) { |
| 527 | if ((LineA < DataA.Length) && (!DataA.modified[LineA]) |
| 528 | && (LineB < DataB.Length) && (!DataB.modified[LineB])) { |
| 529 | // equal lines |
| 530 | LineA++; |
| 531 | LineB++; |
| 532 | |
| 533 | } else { |
| 534 | // maybe deleted and/or inserted lines |
| 535 | StartA = LineA; |
| 536 | StartB = LineB; |
| 537 | |
| 538 | while (LineA < DataA.Length && (LineB >= DataB.Length || DataA.modified[LineA])) |
| 539 | // while (LineA < DataA.Length && DataA.modified[LineA]) |
| 540 | LineA++; |
| 541 | |
| 542 | while (LineB < DataB.Length && (LineA >= DataA.Length || DataB.modified[LineB])) |
| 543 | // while (LineB < DataB.Length && DataB.modified[LineB]) |
| 544 | LineB++; |
| 545 | |
| 546 | if ((StartA < LineA) || (StartB < LineB)) { |
| 547 | // store a new difference-item |
| 548 | aItem = new Item(); |
| 549 | aItem.StartA = StartA; |
| 550 | aItem.StartB = StartB; |
| 551 | aItem.deletedA = LineA - StartA; |
| 552 | aItem.insertedB = LineB - StartB; |
| 553 | a.Add(aItem); |
| 554 | } // if |
| 555 | } // if |
| 556 | } // while |
| 557 | |
| 558 | result = new Item[a.Count]; |
| 559 | a.CopyTo(result); |
| 560 | |
| 561 | return (result); |
| 562 | } |
| 563 | |
| 564 | } // class Diff |
| 565 | |
| 566 | /// <summary>Data on one input file being compared. |
| 567 | /// </summary> |
| 568 | internal class DiffData |
| 569 | { |
| 570 | |
| 571 | /// <summary>Number of elements (lines).</summary> |
| 572 | internal int Length; |
| 573 | |
| 574 | /// <summary>Buffer of numbers that will be compared.</summary> |
| 575 | internal int[] data; |
| 576 | |
| 577 | /// <summary> |
| 578 | /// Array of booleans that flag for modified data. |
| 579 | /// This is the result of the diff. |
| 580 | /// This means deletedA in the first Data or inserted in the second Data. |
| 581 | /// </summary> |
| 582 | internal bool[] modified; |
| 583 | |
| 584 | /// <summary> |
| 585 | /// Initialize the Diff-Data buffer. |
| 586 | /// </summary> |
| 587 | /// <param name="data">reference to the buffer</param> |
| 588 | internal DiffData(int[] initData) { |
| 589 | data = initData; |
| 590 | Length = initData.Length; |
| 591 | modified = new bool[Length + 2]; |
| 592 | } // DiffData |
| 593 | |
| 594 | } // class DiffData |
| 595 | |
| 596 | } // namespace |
This page is part of the http://www.mathertel.de/ web site.