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