namespace my.utils { | |
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 | /// </summary> |
74 | |
public class Diff { | |
75 | public class Diff |
76 | { |
77 | |
78 | /// <summary>details of one difference.</summary> |
public struct Item { | |
79 | public struct Item |
80 | { |
81 | /// <summary>Start Line number in Data A.</summary> |
82 | public int StartA; |
83 | /// <summary>Start Line number in Data B.</summary> |
84 | public int StartB; |
85 | |
86 | /// <summary>Number of changes in Data A.</summary> |
87 | public int deletedA; |
88 | /// <summary>Number of changes in Data B.</summary> |
89 | public int insertedB; |
90 | } // Item |
91 | |
92 | /// <summary> |
93 | /// Shortest Middle Snake Return Data |
94 | /// </summary> |
private struct SMSRD { | |
95 | private struct SMSRD |
96 | { |
97 | internal int x, y; |
98 | // internal int u, v; // 2002.09.20: no need for 2 points |
99 | } |
100 | |
101 | |
102 | #region self-Test |
103 | |
104 | #if (SELFTEST) |
105 | /// <summary> |
106 | /// start a self- / box-test for some diff cases and report to the debug output. |
107 | /// </summary> |
108 | /// <param name="args">not used</param> |
109 | /// <returns>always 0</returns> |
110 | public static int Main(string[] args) { |
111 | StringBuilder ret = new StringBuilder(); |
112 | string a, b; |
113 | |
114 | System.Diagnostics.ConsoleTraceListener ctl = new System.Diagnostics.ConsoleTraceListener(false); |
115 | System.Diagnostics.Debug.Listeners.Add(ctl); |
116 | |
117 | System.Console.WriteLine("Diff Self Test..."); |
118 | |
119 | // test all changes |
120 | a = "a,b,c,d,e,f,g,h,i,j,k,l".Replace(',', '\n'); |
121 | b = "0,1,2,3,4,5,6,7,8,9".Replace(',', '\n'); |
122 | System.Diagnostics.Debug.Assert(TestHelper(Diff.DiffText(a, b, false, false, false)) |
123 | == "12.10.0.0*", |
124 | "all-changes test failed."); |
125 | System.Diagnostics.Debug.WriteLine("all-changes test passed."); |
126 | // test all same |
127 | a = "a,b,c,d,e,f,g,h,i,j,k,l".Replace(',', '\n'); |
128 | b = a; |
129 | System.Diagnostics.Debug.Assert(TestHelper(Diff.DiffText(a, b, false, false, false)) |
130 | == "", |
131 | "all-same test failed."); |
132 | System.Diagnostics.Debug.WriteLine("all-same test passed."); |
133 | |
134 | // test snake |
135 | a = "a,b,c,d,e,f".Replace(',', '\n'); |
136 | b = "b,c,d,e,f,x".Replace(',', '\n'); |
137 | System.Diagnostics.Debug.Assert(TestHelper(Diff.DiffText(a, b, false, false, false)) |
138 | == "1.0.0.0*0.1.6.5*", |
139 | "snake test failed."); |
140 | System.Diagnostics.Debug.WriteLine("snake test passed."); |
141 | |
142 | // 2002.09.20 - repro |
143 | a = "c1,a,c2,b,c,d,e,g,h,i,j,c3,k,l".Replace(',', '\n'); |
144 | b = "C1,a,C2,b,c,d,e,I1,e,g,h,i,j,C3,k,I2,l".Replace(',', '\n'); |
145 | System.Diagnostics.Debug.Assert(TestHelper(Diff.DiffText(a, b, false, false, false)) |
146 | == "1.1.0.0*1.1.2.2*0.2.7.7*1.1.11.13*0.1.13.15*", |
147 | "repro20020920 test failed."); |
148 | System.Diagnostics.Debug.WriteLine("repro20020920 test passed."); |
149 | |
150 | // 2003.02.07 - repro |
151 | a = "F".Replace(',', '\n'); |
152 | b = "0,F,1,2,3,4,5,6,7".Replace(',', '\n'); |
153 | System.Diagnostics.Debug.Assert(TestHelper(Diff.DiffText(a, b, false, false, false)) |
154 | == "0.1.0.0*0.7.1.2*", |
155 | "repro20030207 test failed."); |
156 | System.Diagnostics.Debug.WriteLine("repro20030207 test passed."); |
157 | |
158 | // Muegel - repro |
159 | a = "HELLO\nWORLD"; |
160 | b = "\n\nhello\n\n\n\nworld\n"; |
161 | System.Diagnostics.Debug.Assert(TestHelper(Diff.DiffText(a, b, false, false, false)) |
162 | == "2.8.0.0*", |
163 | "repro20030409 test failed."); |
164 | System.Diagnostics.Debug.WriteLine("repro20030409 test passed."); |
165 | |
166 | // test some differences |
167 | a = "a,b,-,c,d,e,f,f".Replace(',', '\n'); |
168 | b = "a,b,x,c,e,f".Replace(',', '\n'); |
169 | System.Diagnostics.Debug.Assert(TestHelper(Diff.DiffText(a, b, false, false, false)) |
170 | == "1.1.2.2*1.0.4.4*1.0.6.5*", |
171 | "some-changes test failed."); |
172 | System.Diagnostics.Debug.WriteLine("some-changes test passed."); |
173 | |
174 | System.Diagnostics.Debug.WriteLine("End."); |
175 | System.Diagnostics.Debug.Flush(); |
176 | |
177 | return (0); |
178 | } |
179 | |
180 | |
181 | public static string TestHelper(Item []f) { |
182 | StringBuilder ret = new StringBuilder(); |
183 | for (int n = 0; n < f.Length; n++) { |
184 | ret.Append(f[n].deletedA.ToString() + "." + f[n].insertedB.ToString() + "." + f[n].StartA.ToString() + "." + f[n].StartB.ToString() + "*"); |
185 | } |
186 | // Debug.Write(5, "TestHelper", ret.ToString()); |
187 | return (ret.ToString()); |
188 | } |
189 | #endif |
190 | #endregion |
191 | |
192 | |
193 | /// <summary> |
194 | /// Find the difference in 2 texts, comparing by textlines. |
195 | /// </summary> |
196 | /// <param name="TextA">A-version of the text (usualy the old one)</param> |
197 | /// <param name="TextB">B-version of the text (usualy the new one)</param> |
198 | /// <returns>Returns a array of Items that describe the differences.</returns> |
199 | public Item[] DiffText(string TextA, string TextB) { |
200 | return (DiffText(TextA, TextB, false, false, false)); |
201 | } // DiffText |
202 | |
203 | |
204 | /// <summary> |
205 | /// Find the difference in 2 text documents, comparing by textlines. |
206 | /// The algorithm itself is comparing 2 arrays of numbers so when comparing 2 text documents |
207 | /// each line is converted into a (hash) number. This hash-value is computed by storing all |
208 | /// textlines into a common hashtable so i can find dublicates in there, and generating a |
209 | /// new number each time a new textline is inserted. |
210 | /// </summary> |
211 | /// <param name="TextA">A-version of the text (usualy the old one)</param> |
212 | /// <param name="TextB">B-version of the text (usualy the new one)</param> |
213 | /// <param name="trimSpace">When set to true, all leading and trailing whitespace characters are stripped out before the comparation is done.</param> |
214 | /// <param name="ignoreSpace">When set to true, all whitespace characters are converted to a single space character before the comparation is done.</param> |
215 | /// <param name="ignoreCase">When set to true, all characters are converted to their lowercase equivivalence before the comparation is done.</param> |
216 | /// <returns>Returns a array of Items that describe the differences.</returns> |
217 | public static Item[] DiffText(string TextA, string TextB, bool trimSpace, bool ignoreSpace, bool ignoreCase) { |
218 | // prepare the input-text and convert to comparable numbers. |
219 | Hashtable h = new Hashtable(TextA.Length + TextB.Length); |
220 | |
221 | // The A-Version of the data (original data) to be compared. |
222 | DiffData DataA = new DiffData(DiffCodes(TextA, h, trimSpace, ignoreSpace, ignoreCase)); |
223 | |
224 | // The B-Version of the data (modified data) to be compared. |
225 | DiffData DataB = new DiffData(DiffCodes(TextB, h, trimSpace, ignoreSpace, ignoreCase)); |
226 | |
227 | h = null; // free up hashtable memory (maybe) |
228 | |
LCS(DataA, 0, DataA.Length, DataB, 0, DataB.Length); | |
229 | int MAX = DataA.Length + DataB.Length + 1; |
230 | /// vector for the (0,0) to (x,y) search |
231 | int[] DownVector = new int[2 * MAX + 2]; |
232 | /// vector for the (u,v) to (N,M) search |
233 | int[] UpVector = new int[2 * MAX + 2]; |
234 | |
235 | LCS(DataA, 0, DataA.Length, DataB, 0, DataB.Length, DownVector, UpVector); |
236 | |
237 | Optimize(DataA); |
238 | Optimize(DataB); |
239 | return CreateDiffs(DataA, DataB); |
240 | } // DiffText |
241 | |
242 | |
243 | /// <summary> |
/// If a sequence of modified lines starts with line that also | |
/// appends the changes, the difference is modified so that the | |
/// appended line and not the starting line is modified. | |
244 | /// If a sequence of modified lines starts with a line that contains the same content |
245 | /// as the line that appends the changes, the difference sequence is modified so that the |
246 | /// appended line and not the starting line is marked as modified. |
247 | /// This leads to more readable diff sequences when comparing text files. |
248 | /// </summary> |
249 | /// <param name="Data">A Diff data buffer containing the identified changes.</param> |
250 | private static void Optimize(DiffData Data) { |
251 | int StartPos, EndPos; |
252 | |
253 | StartPos = 0; |
254 | while (StartPos < Data.Length) { |
255 | while ((StartPos < Data.Length) && (Data.modified[StartPos] == false)) |
256 | StartPos++; |
257 | EndPos = StartPos; |
258 | while ((EndPos < Data.Length) && (Data.modified[EndPos] == true)) |
259 | EndPos++; |
260 | |
261 | if ((EndPos < Data.Length) && (Data.data[StartPos] == Data.data[EndPos])) { |
262 | Data.modified[StartPos] = false; |
263 | Data.modified[EndPos] = true; |
264 | } else { |
265 | StartPos = EndPos; |
266 | } // if |
267 | } // while |
268 | } // Optimize |
269 | |
270 | |
271 | /// <summary> |
272 | /// Find the difference in 2 arrays of integers. |
273 | /// </summary> |
274 | /// <param name="ArrayA">A-version of the numbers (usualy the old one)</param> |
275 | /// <param name="ArrayB">B-version of the numbers (usualy the new one)</param> |
276 | /// <returns>Returns a array of Items that describe the differences.</returns> |
277 | public static Item[] DiffInt(int[] ArrayA, int[] ArrayB) { |
278 | // The A-Version of the data (original data) to be compared. |
279 | DiffData DataA = new DiffData(ArrayA); |
280 | |
281 | // The B-Version of the data (modified data) to be compared. |
282 | DiffData DataB = new DiffData(ArrayB); |
283 | |
LCS(DataA, 0, DataA.Length, DataB, 0, DataB.Length); | |
284 | int MAX = DataA.Length + DataB.Length + 1; |
285 | /// vector for the (0,0) to (x,y) search |
286 | int[] DownVector = new int[2 * MAX + 2]; |
287 | /// vector for the (u,v) to (N,M) search |
288 | int[] UpVector = new int[2 * MAX + 2]; |
289 | |
290 | LCS(DataA, 0, DataA.Length, DataB, 0, DataB.Length, DownVector, UpVector); |
291 | return CreateDiffs(DataA, DataB); |
292 | } // Diff |
293 | |
294 | |
295 | /// <summary> |
296 | /// This function converts all textlines of the text into unique numbers for every unique textline |
297 | /// so further work can work only with simple numbers. |
298 | /// </summary> |
299 | /// <param name="aText">the input text</param> |
300 | /// <param name="h">This extern initialized hashtable is used for storing all ever used textlines.</param> |
301 | /// <param name="trimSpace">ignore leading and trailing space characters</param> |
302 | /// <returns>a array of integers.</returns> |
303 | private static int[] DiffCodes(string aText, Hashtable h, bool trimSpace, bool ignoreSpace, bool ignoreCase) { |
304 | // get all codes of the text |
305 | string[] Lines; |
306 | int[] Codes; |
307 | int lastUsedCode = h.Count; |
308 | object aCode; |
309 | string s; |
310 | |
311 | // strip off all cr, only use lf as textline separator. |
312 | aText = aText.Replace("\r", ""); |
313 | Lines = aText.Split('\n'); |
314 | |
315 | Codes = new int[Lines.Length]; |
316 | |
317 | for (int i = 0; i < Lines.Length; ++i) { |
318 | s = Lines[i]; |
319 | if (trimSpace) |
320 | s = s.Trim(); |
321 | |
322 | if (ignoreSpace) { |
323 | s = Regex.Replace(s, "\\s+", " "); // TODO: optimization: faster blank removal. |
324 | } |
325 | |
326 | if (ignoreCase) |
327 | s = s.ToLower(); |
328 | |
329 | aCode = h[s]; |
330 | if (aCode == null) { |
331 | lastUsedCode++; |
332 | h[s] = lastUsedCode; |
333 | Codes[i] = lastUsedCode; |
334 | } else { |
335 | Codes[i] = (int)aCode; |
336 | } // if |
337 | } // for |
338 | return (Codes); |
339 | } // DiffCodes |
340 | |
341 | |
342 | /// <summary> |
343 | /// This is the algorithm to find the Shortest Middle Snake (SMS). |
344 | /// </summary> |
345 | /// <param name="DataA">sequence A</param> |
346 | /// <param name="LowerA">lower bound of the actual range in DataA</param> |
347 | /// <param name="UpperA">upper bound of the actual range in DataA (exclusive)</param> |
348 | /// <param name="DataB">sequence B</param> |
349 | /// <param name="LowerB">lower bound of the actual range in DataB</param> |
350 | /// <param name="UpperB">upper bound of the actual range in DataB (exclusive)</param> |
351 | /// <param name="DownVector">a vector for the (0,0) to (x,y) search. Passed as a parameter for speed reasons.</param> |
352 | /// <param name="UpVector">a vector for the (u,v) to (N,M) search. Passed as a parameter for speed reasons.</param> |
353 | /// <returns>a MiddleSnakeData record containing x,y and u,v</returns> |
private static SMSRD SMS(DiffData DataA, int LowerA, int UpperA, DiffData DataB, int LowerB, int UpperB) { | |
354 | private static SMSRD SMS(DiffData DataA, int LowerA, int UpperA, DiffData DataB, int LowerB, int UpperB, |
355 | int[] DownVector, int[] UpVector) { |
356 | |
357 | SMSRD ret; |
358 | int MAX = DataA.Length + DataB.Length + 1; |
359 | |
360 | int DownK = LowerA - LowerB; // the k-line to start the forward search |
361 | int UpK = UpperA - UpperB; // the k-line to start the reverse search |
362 | |
363 | int Delta = (UpperA - LowerA) - (UpperB - LowerB); |
364 | bool oddDelta = (Delta & 1) != 0; |
365 | |
/// 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]; | |
366 | // The vectors in the publication accepts negative indexes. the vectors implemented here are 0-based |
367 | // and are access using a specific offset: UpOffset UpVector and DownOffset for DownVektor |
368 | int DownOffset = MAX - DownK; |
369 | int UpOffset = MAX - UpK; |
370 | |
371 | int MaxD = ((UpperA - LowerA + UpperB - LowerB) / 2) + 1; |
372 | |
373 | // Debug.Write(2, "SMS", String.Format("Search the box: A[{0}-{1}] to B[{2}-{3}]", LowerA, UpperA, LowerB, UpperB)); |
374 | |
375 | // init vectors |
376 | DownVector[DownOffset + DownK + 1] = LowerA; |
377 | UpVector[UpOffset + UpK - 1] = UpperA; |
378 | |
379 | for (int D = 0; D <= MaxD; D++) { |
380 | |
381 | // Extend the forward path. |
382 | for (int k = DownK - D; k <= DownK + D; k += 2) { |
383 | // Debug.Write(0, "SMS", "extend forward path " + k.ToString()); |
384 | |
385 | // find the only or better starting point |
386 | int x, y; |
387 | if (k == DownK - D) { |
388 | x = DownVector[DownOffset + k + 1]; // down |
389 | } else { |
390 | x = DownVector[DownOffset + k - 1] + 1; // a step to the right |
391 | if ((k < DownK + D) && (DownVector[DownOffset + k + 1] >= x)) |
392 | x = DownVector[DownOffset + k + 1]; // down |
393 | } |
394 | y = x - k; |
395 | |
396 | // find the end of the furthest reaching forward D-path in diagonal k. |
397 | while ((x < UpperA) && (y < UpperB) && (DataA.data[x] == DataB.data[y])) { |
398 | x++; y++; |
399 | } |
400 | DownVector[DownOffset + k] = x; |
401 | |
402 | // overlap ? |
403 | if (oddDelta && (UpK - D < k) && (k < UpK + D)) { |
404 | if (UpVector[UpOffset + k] <= DownVector[DownOffset + k]) { |
405 | ret.x = DownVector[DownOffset + k]; |
406 | ret.y = DownVector[DownOffset + k] - k; |
407 | // ret.u = UpVector[UpOffset + k]; // 2002.09.20: no need for 2 points |
408 | // ret.v = UpVector[UpOffset + k] - k; |
409 | return (ret); |
410 | } // if |
411 | } // if |
412 | |
413 | } // for k |
414 | |
415 | // Extend the reverse path. |
416 | for (int k = UpK - D; k <= UpK + D; k += 2) { |
417 | // Debug.Write(0, "SMS", "extend reverse path " + k.ToString()); |
418 | |
419 | // find the only or better starting point |
420 | int x, y; |
421 | if (k == UpK + D) { |
422 | x = UpVector[UpOffset + k - 1]; // up |
423 | } else { |
424 | x = UpVector[UpOffset + k + 1] - 1; // left |
425 | if ((k > UpK - D) && (UpVector[UpOffset + k - 1] < x)) |
426 | x = UpVector[UpOffset + k - 1]; // up |
427 | } // if |
428 | y = x - k; |
429 | |
430 | while ((x > LowerA) && (y > LowerB) && (DataA.data[x - 1] == DataB.data[y - 1])) { |
431 | x--; y--; // diagonal |
432 | } |
433 | UpVector[UpOffset + k] = x; |
434 | |
435 | // overlap ? |
436 | if (!oddDelta && (DownK - D <= k) && (k <= DownK + D)) { |
437 | if (UpVector[UpOffset + k] <= DownVector[DownOffset + k]) { |
438 | ret.x = DownVector[DownOffset + k]; |
439 | ret.y = DownVector[DownOffset + k] - k; |
440 | // ret.u = UpVector[UpOffset + k]; // 2002.09.20: no need for 2 points |
441 | // ret.v = UpVector[UpOffset + k] - k; |
442 | return (ret); |
443 | } // if |
444 | } // if |
445 | |
446 | } // for k |
447 | |
448 | } // for D |
449 | |
450 | throw new ApplicationException("the algorithm should never come here."); |
451 | } // SMS |
452 | |
453 | |
454 | /// <summary> |
455 | /// This is the divide-and-conquer implementation of the longes common-subsequence (LCS) |
456 | /// algorithm. |
457 | /// The published algorithm passes recursively parts of the A and B sequences. |
458 | /// To avoid copying these arrays the lower and upper bounds are passed while the sequences stay constant. |
459 | /// </summary> |
460 | /// <param name="DataA">sequence A</param> |
461 | /// <param name="LowerA">lower bound of the actual range in DataA</param> |
462 | /// <param name="UpperA">upper bound of the actual range in DataA (exclusive)</param> |
463 | /// <param name="DataB">sequence B</param> |
464 | /// <param name="LowerB">lower bound of the actual range in DataB</param> |
465 | /// <param name="UpperB">upper bound of the actual range in DataB (exclusive)</param> |
private static void LCS(DiffData DataA, int LowerA, int UpperA, DiffData DataB, int LowerB, int UpperB) { | |
466 | /// <param name="DownVector">a vector for the (0,0) to (x,y) search. Passed as a parameter for speed reasons.</param> |
467 | /// <param name="UpVector">a vector for the (u,v) to (N,M) search. Passed as a parameter for speed reasons.</param> |
468 | private static void LCS(DiffData DataA, int LowerA, int UpperA, DiffData DataB, int LowerB, int UpperB, int[] DownVector, int[] UpVector) { |
469 | // Debug.Write(2, "LCS", String.Format("Analyse the box: A[{0}-{1}] to B[{2}-{3}]", LowerA, UpperA, LowerB, UpperB)); |
470 | |
471 | // Fast walkthrough equal lines at the start |
472 | while (LowerA < UpperA && LowerB < UpperB && DataA.data[LowerA] == DataB.data[LowerB]) { |
473 | LowerA++; LowerB++; |
474 | } |
475 | |
476 | // Fast walkthrough equal lines at the end |
477 | while (LowerA < UpperA && LowerB < UpperB && DataA.data[UpperA - 1] == DataB.data[UpperB - 1]) { |
478 | --UpperA; --UpperB; |
479 | } |
480 | |
481 | if (LowerA == UpperA) { |
482 | // mark as inserted lines. |
483 | while (LowerB < UpperB) |
484 | DataB.modified[LowerB++] = true; |
485 | |
486 | } else if (LowerB == UpperB) { |
487 | // mark as deleted lines. |
488 | while (LowerA < UpperA) |
489 | DataA.modified[LowerA++] = true; |
490 | |
491 | } else { |
492 | // Find the middle snakea and length of an optimal path for A and B |
SMSRD smsrd = SMS(DataA, LowerA, UpperA, DataB, LowerB, UpperB); | |
493 | SMSRD smsrd = SMS(DataA, LowerA, UpperA, DataB, LowerB, UpperB, DownVector, UpVector); |
494 | // Debug.Write(2, "MiddleSnakeData", String.Format("{0},{1}", smsrd.x, smsrd.y)); |
495 | |
496 | // The path is from LowerX to (x,y) and (x,y) to UpperX |
LCS(DataA, LowerA, smsrd.x, DataB, LowerB, smsrd.y); | |
LCS(DataA, smsrd.x, UpperA, DataB, smsrd.y, UpperB); // 2002.09.20: no need for 2 points | |
497 | LCS(DataA, LowerA, smsrd.x, DataB, LowerB, smsrd.y, DownVector, UpVector); |
498 | LCS(DataA, smsrd.x, UpperA, DataB, smsrd.y, UpperB, DownVector, UpVector); // 2002.09.20: no need for 2 points |
499 | } |
500 | } // LCS() |
501 | |
502 | |
503 | /// <summary>Scan the tables of which lines are inserted and deleted, |
504 | /// producing an edit script in forward order. |
505 | /// </summary> |
506 | /// dynamic array |
507 | private static Item[] CreateDiffs(DiffData DataA, DiffData DataB) { |
508 | ArrayList a = new ArrayList(); |
509 | Item aItem; |
510 | Item[] result; |
511 | |
512 | int StartA, StartB; |
513 | int LineA, LineB; |
514 | |
515 | LineA = 0; |
516 | LineB = 0; |
517 | while (LineA < DataA.Length || LineB < DataB.Length) { |
518 | if ((LineA < DataA.Length) && (!DataA.modified[LineA]) |
519 | && (LineB < DataB.Length) && (!DataB.modified[LineB])) { |
520 | // equal lines |
521 | LineA++; |
522 | LineB++; |
523 | |
524 | } else { |
525 | // maybe deleted and/or inserted lines |
526 | StartA = LineA; |
527 | StartB = LineB; |
528 | |
529 | while (LineA < DataA.Length && (LineB >= DataB.Length || DataA.modified[LineA])) |
530 | // while (LineA < DataA.Length && DataA.modified[LineA]) |
531 | LineA++; |
532 | |
533 | while (LineB < DataB.Length && (LineA >= DataA.Length || DataB.modified[LineB])) |
534 | // while (LineB < DataB.Length && DataB.modified[LineB]) |
535 | LineB++; |
536 | |
537 | if ((StartA < LineA) || (StartB < LineB)) { |
538 | // store a new difference-item |
539 | aItem = new Item(); |
540 | aItem.StartA = StartA; |
541 | aItem.StartB = StartB; |
542 | aItem.deletedA = LineA - StartA; |
543 | aItem.insertedB = LineB - StartB; |
544 | a.Add(aItem); |
545 | } // if |
546 | } // if |
547 | } // while |
548 | |
549 | result = new Item[a.Count]; |
550 | a.CopyTo(result); |
551 | |
552 | return (result); |
553 | } |
554 | |
555 | } // class Diff |
556 | |
557 | /// <summary>Data on one input file being compared. |
558 | /// </summary> |
internal class DiffData { | |
559 | internal class DiffData |
560 | { |
561 | |
562 | /// <summary>Number of elements (lines).</summary> |
563 | internal int Length; |
564 | |
565 | /// <summary>Buffer of numbers that will be compared.</summary> |
566 | internal int[] data; |
567 | |
568 | /// <summary> |
569 | /// Array of booleans that flag for modified data. |
570 | /// This is the result of the diff. |
571 | /// This means deletedA in the first Data or inserted in the second Data. |
572 | /// </summary> |
573 | internal bool[] modified; |
574 | |
575 | /// <summary> |
576 | /// Initialize the Diff-Data buffer. |
577 | /// </summary> |
578 | /// <param name="data">reference to the buffer</param> |
579 | internal DiffData(int[] initData) { |
580 | data = initData; |
581 | Length = initData.Length; |
582 | modified = new bool[Length + 2]; |
583 | } // DiffData |
584 | |
585 | } // class DiffData |
586 | |
587 | } // namespace |
This page is part of the http://www.mathertel.de/ web site.