forked from groehner/JavaEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUDiff.pas
More file actions
566 lines (517 loc) · 17.3 KB
/
UDiff.pas
File metadata and controls
566 lines (517 loc) · 17.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
unit UDiff;
(* ******************************************************************************
* Component TDiff *
* Version: 4.1 *
* Date: 7 November 2009 *
* Compilers: Delphi 7 - Delphi 2009 *
* Author: Angus Johnson - angusj-AT-myrealbox-DOT-com *
* Copyright: © 2001-2009 Angus Johnson *
* *
* Licence to use, terms and conditions: *
* The code in the TDiff component is released as freeware *
* provided you agree to the following terms & conditions: *
* 1. the copyright notice, terms and conditions are *
* left unchanged *
* 2. modifications to the code by other authors must be *
* clearly documented and accompanied by the modifier's name. *
* 3. the TDiff component may be freely compiled into binary *
* format and no acknowledgement is required. However, a *
* discrete acknowledgement would be appreciated (eg. in a *
* program's 'About Box'). *
* *
* Description: Component to list differences between two integer arrays *
* using a "longest common subsequence" algorithm. *
* Typically, this component is used to diff 2 text files *
* once their individuals lines have been hashed. *
* *
* Acknowledgements: The key algorithm in this component is based on: *
* "An O(NP) Sequence Comparison Algorithm" *
* by Sun Wu, Udi Manber & Gene Myers *
* and uses a "divide-and-conquer" technique to avoid *
* using exponential amounts of memory as described in *
* "An O(ND) Difference Algorithm and its Variations" *
* By E Myers - Algorithmica Vol. 1 No. 2, 1986, pp. 251-266 *
****************************************************************************** *)
(* ******************************************************************************
* History: *
* 13 December 2001 - Original release (used Myer's O(ND) Difference Algorithm) *
* 22 April 2008 - Complete rewrite to greatly improve the code and *
* provide a much simpler view of differences through a New *
* 'Compares' property. *
* 21 May 2008 - Another complete code rewrite to use Sun Wu et al.'s *
* O(NP) Sequence Comparison Algorithm which more than *
* halves times of typical comparisons. *
* 24 May 2008 - Reimplemented "divide-and-conquer" technique (which was *
* omitted in 21 May release) so memory use is again minimal.*
* 25 May 2008 - Removed recursion to avoid the possibility of running out *
* of stack memory during massive comparisons. *
* 2 June 2008 - Bugfix: incorrect number of appended AddChangeInt() calls *
* in Execute() for integer arrays. (It was OK with Chars) *
* Added check to prevent repeat calls to Execute() while *
* already executing. *
* Added extra parse of differences to find occasional *
* missed matches. (See readme.txt for further discussion) *
* 7 November 2009 - Updated so now compiles in newer versions of Delphi. *
****************************************************************************** *)
interface
uses
Windows, Classes;
const
MAX_DIAGONAL = $FFFFFF; // ~16 million
type
TArrOfInteger = array of Integer;
{$IFDEF UNICODE}
P8Bits = PByte;
{$ELSE}
P8Bits = PAnsiChar;
{$ENDIF}
PDiags = ^TDiags;
TDiags = array [-MAX_DIAGONAL .. MAX_DIAGONAL] of Integer;
TChangeKind = (ckNone, ckAdd, ckDelete, ckModify);
PCompareRec = ^TCompareRec;
TCompareRec = record
Kind: TChangeKind;
OldIndex1, OldIndex2: Integer;
case Boolean of
False:
(Chr1, Chr2: Char);
True:
(Int1, Int2: Integer);
end;
PDiffVars = ^TDiffVars;
TDiffVars = record
Offset1: Integer;
Offset2: Integer;
Len1: Integer;
Len2: Integer;
end;
TDiffStats = record
matches: Integer;
adds: Integer;
deletes: Integer;
modifies: Integer;
end;
TDiff = class(TComponent)
private
FCompareList: TList;
FDiffList: TList; // this TList circumvents the need for recursion
FCancelled: Boolean;
FExecuting: Boolean;
FCompareInts: Boolean; // ie are we comparing integer arrays
FDiagBufferF: Pointer;
FDiagBufferB: Pointer;
FDiagF, FDiagB: PDiags;
FInts1, FInts2: TArrOfInteger;
FDiffStats: TDiffStats;
FLastCompareRec: TCompareRec;
procedure PushDiff(Offset1, Offset2, Len1, Len2: Integer);
function PopDiff: Boolean;
procedure InitDiagArrays(Len1, Len2: Integer);
procedure DiffInt(Offset1, Offset2, Len1, Len2: Integer);
function SnakeIntF(IntK, Offset1, Offset2, Len1, Len2: Integer): Boolean;
function SnakeIntB(IntK, Offset1, Offset2, Len1, Len2: Integer): Boolean;
procedure AddChangeInt(Offset1, Range: Integer; ChangeKind: TChangeKind);
function GetCount: Integer;
function GetCompare(Index: Integer): TCompareRec;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
// compare array of integers ...
function Execute(Pints1, Pints2: TArrOfInteger;
Len1, Len2: Integer): Boolean;
// Cancel allows interrupting excessively prolonged comparisons
procedure Cancel;
procedure Clear;
property Cancelled: Boolean read FCancelled;
property Count: Integer read GetCount;
property Compares[Index: Integer]: TCompareRec read GetCompare; default;
property DiffStats: TDiffStats read FDiffStats;
end;
implementation
uses Forms;
constructor TDiff.Create(AOwner: TComponent);
begin
inherited;
FCompareList := TList.Create;
FDiffList := TList.Create;
end;
destructor TDiff.Destroy;
begin
Clear;
FCompareList.Free;
FDiffList.Free;
inherited;
end;
function TDiff.Execute(Pints1, Pints2: TArrOfInteger;
Len1, Len2: Integer): Boolean;
var
Len1Minus1: Integer;
begin
if FExecuting then
Exit(False);
FCancelled := False;
FExecuting := True;
try
Clear;
Len1Minus1 := Len1 - 1;
FCompareList.Capacity := Len1 + Len2;
FCompareInts := True;
GetMem(FDiagBufferF, SizeOf(Integer) * (Len1 + Len2 + 3));
GetMem(FDiagBufferB, SizeOf(Integer) * (Len1 + Len2 + 3));
FInts1 := Pints1;
FInts2 := Pints2;
try
PushDiff(0, 0, Len1, Len2);
while PopDiff do;
finally
FreeMem(FDiagBufferF);
FreeMem(FDiagBufferB);
end;
if FCancelled then
begin
Clear;
Exit(False);
end;
// correct the occasional missed match ...
for var I := 1 to Count - 1 do
with PCompareRec(FCompareList[I])^ do
if (Kind = ckModify) and (Int1 = Int2) then
begin
Kind := ckNone;
Dec(FDiffStats.modifies);
Inc(FDiffStats.matches);
end;
// finally, append any trailing matches onto compareList ...
with FLastCompareRec do
AddChangeInt(oldIndex1, Len1Minus1 - oldIndex1, ckNone);
finally
FExecuting := False;
Result := True;
end;
end;
procedure TDiff.PushDiff(Offset1, Offset2, Len1, Len2: Integer);
var
DiffVars: PDiffVars;
begin
New(DiffVars);
DiffVars.Offset1 := Offset1;
DiffVars.Offset2 := Offset2;
DiffVars.Len1 := Len1;
DiffVars.Len2 := Len2;
FDiffList.Add(DiffVars);
end;
function TDiff.PopDiff: Boolean;
begin
var
Idx := FDiffList.Count - 1;
Result := Idx >= 0;
if not Result then
Exit;
var
DiffVars := PDiffVars(FDiffList[Idx]);
with DiffVars^ do
if FCompareInts then
DiffInt(Offset1, Offset2, Len1, Len2);
Dispose(DiffVars);
FDiffList.Delete(Idx);
end;
// ------------------------------------------------------------------------------
procedure TDiff.InitDiagArrays(Len1, Len2: Integer);
begin
// assumes that top and bottom matches have been excluded
P8Bits(FDiagF) := P8Bits(FDiagBufferF) - SizeOf(Integer) *
(MAX_DIAGONAL - (Len1 + 1));
for var I := -(Len1 + 1) to (Len2 + 1) do
FDiagF[I] := -MaxInt;
FDiagF[1] := -1;
P8Bits(FDiagB) := P8Bits(FDiagBufferB) - SizeOf(Integer) *
(MAX_DIAGONAL - (Len1 + 1));
for var I := -(Len1 + 1) to (Len2 + 1) do
FDiagB[I] := MaxInt;
FDiagB[Len2 - Len1 + 1] := Len2;
end;
procedure TDiff.DiffInt(Offset1, Offset2, Len1, Len2: Integer);
var
Posi, Delta: Integer;
begin
// trim matching bottoms ...
while (Len1 > 0) and (Len2 > 0) and (FInts1[Offset1] = FInts2[Offset2]) do
begin
Inc(Offset1);
Inc(Offset2);
Dec(Len1);
Dec(Len2);
end;
// trim matching tops ...
while (Len1 > 0) and (Len2 > 0) and
(FInts1[Offset1 + Len1 - 1] = FInts2[Offset2 + Len2 - 1]) do
begin
Dec(Len1);
Dec(Len2);
end;
// stop diff'ing if minimal conditions reached ...
if (Len1 = 0) then
begin
AddChangeInt(Offset1, Len2, ckAdd);
Exit;
end
else if (Len2 = 0) then
begin
AddChangeInt(Offset1, Len1, ckDelete);
Exit;
end
else if (Len1 = 1) and (Len2 = 1) then
begin
AddChangeInt(Offset1, 1, ckDelete);
AddChangeInt(Offset1, 1, ckAdd);
Exit;
end;
Posi := -1;
Delta := Len2 - Len1;
InitDiagArrays(Len1, Len2);
if Delta < 0 then
begin
repeat
Inc(Posi);
if (Posi mod 1024) = 1023 then
begin
Application.ProcessMessages;
if FCancelled then
Exit;
end;
// nb: the Snake order is important here
for var I := Posi downto Delta + 1 do
if SnakeIntF(I, Offset1, Offset2, Len1, Len2) then
Exit;
for var I := -Posi + Delta to Delta - 1 do
if SnakeIntF(I, Offset1, Offset2, Len1, Len2) then
Exit;
for var I := Delta - Posi to -1 do
if SnakeIntB(I, Offset1, Offset2, Len1, Len2) then
Exit;
for var I := Posi downto 1 do
if SnakeIntB(I, Offset1, Offset2, Len1, Len2) then
Exit;
if SnakeIntF(Delta, Offset1, Offset2, Len1, Len2) then
Exit;
if SnakeIntB(0, Offset1, Offset2, Len1, Len2) then
Exit;
until False;
end
else
begin
repeat
Inc(Posi);
if (Posi mod 1024) = 1023 then
begin
Application.ProcessMessages;
if FCancelled then
Exit;
end;
// nb: the Snake order is important here
for var I := -Posi to Delta - 1 do
if SnakeIntF(I, Offset1, Offset2, Len1, Len2) then
Exit;
for var I := Posi + Delta downto Delta + 1 do
if SnakeIntF(I, Offset1, Offset2, Len1, Len2) then
Exit;
for var I := Delta + Posi downto 1 do
if SnakeIntB(I, Offset1, Offset2, Len1, Len2) then
Exit;
for var I := -Posi to -1 do
if SnakeIntB(I, Offset1, Offset2, Len1, Len2) then
Exit;
if SnakeIntF(Delta, Offset1, Offset2, Len1, Len2) then
Exit;
if SnakeIntB(0, Offset1, Offset2, Len1, Len2) then
Exit;
until False;
end;
end;
function TDiff.SnakeIntF(IntK, Offset1, Offset2, Len1, Len2: Integer): Boolean;
var
IntX, IntY: Integer;
begin
if FDiagF[IntK + 1] > FDiagF[IntK - 1] then
IntY := FDiagF[IntK + 1]
else
IntY := FDiagF[IntK - 1] + 1;
IntX := IntY - IntK;
while (IntX < Len1 - 1) and (IntY < Len2 - 1) and
(FInts1[Offset1 + IntX + 1] = FInts2[Offset2 + IntY + 1]) do
begin
Inc(IntX);
Inc(IntY);
end;
FDiagF[IntK] := IntY;
Result := (FDiagF[IntK] >= FDiagB[IntK]);
if not Result then
Exit;
Inc(IntX);
Inc(IntY);
PushDiff(Offset1 + IntX, Offset2 + IntY, Len1 - IntX, Len2 - IntY);
PushDiff(Offset1, Offset2, IntX, IntY);
end;
function TDiff.SnakeIntB(IntK, Offset1, Offset2, Len1, Len2: Integer): Boolean;
var
IntX, IntY: Integer;
begin
if FDiagB[IntK - 1] < FDiagB[IntK + 1] then
IntY := FDiagB[IntK - 1]
else
IntY := FDiagB[IntK + 1] - 1;
IntX := IntY - IntK;
while (IntX >= 0) and (IntY >= 0) and (FInts1[Offset1 + IntX] = FInts2[Offset2 + IntY]) do
begin
Dec(IntX);
Dec(IntY);
end;
FDiagB[IntK] := IntY;
Result := FDiagB[IntK] <= FDiagF[IntK];
if not Result then
Exit;
Inc(IntX);
Inc(IntY);
PushDiff(Offset1 + IntX, Offset2 + IntY, Len1 - IntX, Len2 - IntY);
PushDiff(Offset1, Offset2, IntX, IntY);
end;
procedure TDiff.AddChangeInt(Offset1, Range: Integer; ChangeKind: TChangeKind);
var
IntJ: Integer;
CompareRec: PCompareRec;
begin
// first, add any unchanged items into this list ...
while (FLastCompareRec.oldIndex1 < Offset1 - 1) do
begin
with FLastCompareRec do
begin
Kind := ckNone;
Inc(oldIndex1);
Inc(oldIndex2);
Int1 := FInts1[oldIndex1];
Int2 := FInts2[oldIndex2];
end;
New(CompareRec);
CompareRec^ := FLastCompareRec;
FCompareList.Add(CompareRec);
Inc(FDiffStats.matches);
end;
case ChangeKind of
ckNone:
for var I := 1 to Range do
begin
with FLastCompareRec do
begin
Kind := ckNone;
Inc(oldIndex1);
Inc(oldIndex2);
Int1 := FInts1[oldIndex1];
Int2 := FInts2[oldIndex2];
end;
New(CompareRec);
CompareRec^ := FLastCompareRec;
FCompareList.Add(CompareRec);
Inc(FDiffStats.matches);
end;
ckAdd:
begin
for var I := 1 to Range do
begin
with FLastCompareRec do
begin
// check if a Range of adds are following a Range of deletes
// and convert them to modifies ...
if Kind = ckDelete then
begin
IntJ := FCompareList.Count - 1;
while (IntJ > 0) and
(PCompareRec(FCompareList[IntJ - 1]).Kind = ckDelete) do
Dec(IntJ);
PCompareRec(FCompareList[IntJ]).Kind := ckModify;
Dec(FDiffStats.deletes);
Inc(FDiffStats.modifies);
Inc(FLastCompareRec.oldIndex2);
PCompareRec(FCompareList[IntJ]).oldIndex2 :=
FLastCompareRec.oldIndex2;
PCompareRec(FCompareList[IntJ]).Int2 := FInts2[oldIndex2];
if IntJ = FCompareList.Count - 1 then
FLastCompareRec.Kind := ckModify;
Continue;
end;
Kind := ckAdd;
Int1 := $0;
Inc(oldIndex2);
Int2 := FInts2[oldIndex2]; // ie what we added
end;
New(CompareRec);
CompareRec^ := FLastCompareRec;
FCompareList.Add(CompareRec);
Inc(FDiffStats.adds);
end;
end;
ckDelete:
begin
for var I := 1 to Range do
begin
with FLastCompareRec do
begin
// check if a Range of deletes are following a Range of adds
// and convert them to modifies ...
if Kind = ckAdd then
begin
IntJ := FCompareList.Count - 1;
while (IntJ > 0) and
(PCompareRec(FCompareList[IntJ - 1]).Kind = ckAdd) do
Dec(IntJ);
PCompareRec(FCompareList[IntJ]).Kind := ckModify;
Dec(FDiffStats.adds);
Inc(FDiffStats.modifies);
Inc(FLastCompareRec.oldIndex1);
PCompareRec(FCompareList[IntJ]).oldIndex1 :=
FLastCompareRec.oldIndex1;
PCompareRec(FCompareList[IntJ]).Int1 := FInts1[oldIndex1];
if IntJ = FCompareList.Count - 1 then
FLastCompareRec.Kind := ckModify;
Continue;
end;
Kind := ckDelete;
Int2 := $0;
Inc(oldIndex1);
Int1 := FInts1[oldIndex1]; // ie what we deleted
end;
New(CompareRec);
CompareRec^ := FLastCompareRec;
FCompareList.Add(CompareRec);
Inc(FDiffStats.deletes);
end;
end;
end;
end;
procedure TDiff.Clear;
begin
for var I := 0 to FCompareList.Count - 1 do
Dispose(PCompareRec(FCompareList[I]));
FCompareList.Clear;
FLastCompareRec.Kind := ckNone;
FLastCompareRec.oldIndex1 := -1;
FLastCompareRec.oldIndex2 := -1;
FDiffStats.matches := 0;
FDiffStats.adds := 0;
FDiffStats.deletes := 0;
FDiffStats.modifies := 0;
FInts1 := nil;
FInts2 := nil;
end;
function TDiff.GetCount: Integer;
begin
Result := FCompareList.Count;
end;
function TDiff.GetCompare(Index: Integer): TCompareRec;
begin
Result := PCompareRec(FCompareList[Index])^;
end;
procedure TDiff.Cancel;
begin
FCancelled := True;
end;
end.