-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdiff.pas
444 lines (410 loc) · 13.4 KB
/
diff.pas
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
UNIT diff;
INTERFACE
USES sysutils, Classes, math;
CONST MAX_DIAGONAL = $FFFFFF; //~16 million
TYPE
{$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,
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 = object
private
fCompareList: TList;
fDiffList: TList; //this TList circumvents the need for recursion
DiagBufferF: pointer;
DiagBufferB: pointer;
DiagF, DiagB: PDiags;
Ints1, Ints2: PInteger;
fDiffStats: TDiffStats;
fLastCompareRec: TCompareRec;
PROCEDURE PushDiff(CONST offset1, offset2, len1, len2: integer);
FUNCTION PopDiff: boolean;
PROCEDURE InitDiagArrays(CONST len1, len2: integer);
PROCEDURE DiffInt(offset1, offset2, len1, len2: integer);
FUNCTION SnakeIntF(CONST k,offset1,offset2,len1,len2: integer): boolean;
FUNCTION SnakeIntB(CONST k,offset1,offset2,len1,len2: integer): boolean;
PROCEDURE AddChangeInt(CONST offset1, range: integer; ChangeKind: TChangeKind);
FUNCTION GetCompareCount: integer;
FUNCTION GetCompare(CONST index: integer): TCompareRec;
public
CONSTRUCTOR create;
DESTRUCTOR destroy;
//compare either and array of characters or an array of integers ...
FUNCTION execute(CONST pInts1, pInts2: PInteger; CONST len1, len2: integer): boolean; overload;
FUNCTION execute(CONST txt1,txt2:ansistring): boolean; overload;
//Cancel allows interrupting excessively prolonged comparisons
PROCEDURE clear;
PROPERTY count: integer read GetCompareCount;
PROPERTY Compares[index: integer]: TCompareRec read GetCompare; default;
PROPERTY DiffStats: TDiffStats read fDiffStats;
end;
IMPLEMENTATION
CONSTRUCTOR TDiff.create;
begin
fCompareList := TList.create;
fDiffList := TList.create;
end;
//------------------------------------------------------------------------------
DESTRUCTOR TDiff.destroy;
begin
clear;
fCompareList.free;
fDiffList.free;
inherited;
end;
//------------------------------------------------------------------------------
FUNCTION TDiff.execute(CONST pInts1, pInts2: PInteger; CONST len1, len2: integer): boolean;
VAR i, Len1Minus1: integer;
begin
result:=true;
try
clear;
Len1Minus1 := len1 -1;
fCompareList.Capacity := len1 + len2;
getMem(DiagBufferF, sizeOf(integer)*(len1+len2+3));
getMem(DiagBufferB, sizeOf(integer)*(len1+len2+3));
Ints1 := pInts1;
Ints2 := pInts2;
try
PushDiff(0, 0, len1, len2);
while PopDiff do;
finally
freeMem(DiagBufferF);
freeMem(DiagBufferB);
end;
//correct the occasional missed match ...
for 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
end;
end;
//------------------------------------------------------------------------------
FUNCTION TDiff.execute(CONST txt1,txt2:ansistring): boolean;
VAR i:longint;
pInts1,pInts2:PInteger;
begin
getMem(pInts1,length(txt1)*sizeOf(integer));
for i:=1 to length(txt1) do pInts1[i-1]:=ord(txt1[i]);
getMem(pInts2,length(txt2)*sizeOf(integer));
for i:=1 to length(txt2) do pInts2[i-1]:=ord(txt2[i]);
result:=execute(pInts1,pInts2,length(txt1),length(txt2));
freeMem(pInts1,length(txt1)*sizeOf(integer));
freeMem(pInts2,length(txt2)*sizeOf(integer));
end;
//------------------------------------------------------------------------------
PROCEDURE TDiff.PushDiff(CONST 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;
VAR
DiffVars: PDiffVars;
idx: integer;
begin
idx := fDiffList.count -1;
result := idx >= 0;
if not result then exit;
DiffVars := PDiffVars(fDiffList[idx]);
with DiffVars^ do
DiffInt(offset1, offset2, len1, len2);
dispose(DiffVars);
fDiffList.delete(idx);
end;
//------------------------------------------------------------------------------
PROCEDURE TDiff.InitDiagArrays(CONST len1, len2: integer);
VAR
i: integer;
begin
//assumes that top and bottom matches have been excluded
P8Bits(DiagF) := P8Bits(DiagBufferF) - sizeOf(integer)*(MAX_DIAGONAL-(len1+1));
for i := - (len1+1) to (len2+1) do DiagF^[i] := -maxInt;
DiagF^[1] := -1;
P8Bits(DiagB) := P8Bits(DiagBufferB) - sizeOf(integer)*(MAX_DIAGONAL-(len1+1));
for i := - (len1+1) to (len2+1) do DiagB^[i] := maxInt;
DiagB^[len2-len1+1] := len2;
end;
//------------------------------------------------------------------------------
PROCEDURE TDiff.DiffInt(offset1, offset2, len1, len2: integer);
VAR
p, k, delta: integer;
begin
//trim matching bottoms ...
while (len1 > 0) and (len2 > 0) and (Ints1[offset1] = Ints2[offset2]) do
begin
inc(offset1); inc(offset2); dec(len1); dec(len2);
end;
//trim matching tops ...
while (len1 > 0) and (len2 > 0) and (Ints1[offset1+len1-1] = Ints2[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;
p := -1;
delta := len2 - len1;
InitDiagArrays(len1, len2);
if delta < 0 then
begin
repeat
inc(p);
//nb: the Snake order is important here
for k := p downto delta +1 do
if SnakeIntF(k,offset1,offset2,len1,len2) then exit;
for k := -p + delta to delta-1 do
if SnakeIntF(k,offset1,offset2,len1,len2) then exit;
for k := delta -p to -1 do
if SnakeIntB(k,offset1,offset2,len1,len2) then exit;
for k := p downto 1 do
if SnakeIntB(k,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(p);
//nb: the Snake order is important here
for k := -p to delta -1 do
if SnakeIntF(k,offset1,offset2,len1,len2) then exit;
for k := p + delta downto delta +1 do
if SnakeIntF(k,offset1,offset2,len1,len2) then exit;
for k := delta + p downto 1 do
if SnakeIntB(k,offset1,offset2,len1,len2) then exit;
for k := -p to -1 do
if SnakeIntB(k,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(CONST k,offset1,offset2,len1,len2: integer): boolean;
VAR
x,y: integer;
begin
if DiagF^[k+1] > DiagF^[k-1] then
y := DiagF^[k+1] else
y := DiagF^[k-1]+1;
x := y - k;
while (x < len1-1) and (y < len2-1) and
(Ints1[offset1+x+1] = Ints2[offset2+y+1]) do
begin
inc(x); inc(y);
end;
DiagF^[k] := y;
result := (DiagF^[k] >= DiagB^[k]);
if not result then exit;
inc(x); inc(y);
PushDiff(offset1+x, offset2+y, len1-x, len2-y);
PushDiff(offset1, offset2, x, y);
end;
//------------------------------------------------------------------------------
FUNCTION TDiff.SnakeIntB(CONST k,offset1,offset2,len1,len2: integer): boolean;
VAR
x,y: integer;
begin
if DiagB^[k-1] < DiagB^[k+1] then
y := DiagB^[k-1] else
y := DiagB^[k+1]-1;
x := y - k;
while (x >= 0) and (y >= 0) and (Ints1[offset1+x] = Ints2[offset2+y]) do
begin
dec(x); dec(y);
end;
DiagB^[k] := y;
result := DiagB^[k] <= DiagF^[k];
if not result then exit;
inc(x); inc(y);
PushDiff(offset1+x, offset2+y, len1-x, len2-y);
PushDiff(offset1, offset2, x, y);
end;
//------------------------------------------------------------------------------
PROCEDURE TDiff.AddChangeInt(CONST offset1, range: integer; ChangeKind: TChangeKind);
VAR
i,j: 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 := Ints1[oldIndex1];
int2 := Ints2[oldIndex2];
end;
new(compareRec);
compareRec^ := fLastCompareRec;
fCompareList.add(compareRec);
inc(fDiffStats.matches);
end;
case ChangeKind of
ckNone:
for i := 1 to range do
begin
with fLastCompareRec do
begin
kind := ckNone;
inc(oldIndex1);
inc(oldIndex2);
int1 := Ints1[oldIndex1];
int2 := Ints2[oldIndex2];
end;
new(compareRec);
compareRec^ := fLastCompareRec;
fCompareList.add(compareRec);
inc(fDiffStats.matches);
end;
ckAdd :
begin
for 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
j := fCompareList.count -1;
while (j > 0) and (PCompareRec(fCompareList[j-1])^.kind = ckDelete) do
dec(j);
PCompareRec(fCompareList[j])^.kind := ckModify;
dec(fDiffStats.deletes);
inc(fDiffStats.modifies);
inc(fLastCompareRec.oldIndex2);
PCompareRec(fCompareList[j])^.oldIndex2 := fLastCompareRec.oldIndex2;
PCompareRec(fCompareList[j])^.int2 := Ints2[oldIndex2];
if j = fCompareList.count-1 then fLastCompareRec.kind := ckModify;
continue;
end;
kind := ckAdd;
int1 := $0;
inc(oldIndex2);
int2 := Ints2[oldIndex2]; //ie what we added
end;
new(compareRec);
compareRec^ := fLastCompareRec;
fCompareList.add(compareRec);
inc(fDiffStats.adds);
end;
end;
ckDelete :
begin
for 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
j := fCompareList.count -1;
while (j > 0) and (PCompareRec(fCompareList[j-1])^.kind = ckAdd) do
dec(j);
PCompareRec(fCompareList[j])^.kind := ckModify;
dec(fDiffStats.adds);
inc(fDiffStats.modifies);
inc(fLastCompareRec.oldIndex1);
PCompareRec(fCompareList[j])^.oldIndex1 := fLastCompareRec.oldIndex1;
PCompareRec(fCompareList[j])^.int1 := Ints1[oldIndex1];
if j = fCompareList.count-1 then fLastCompareRec.kind := ckModify;
continue;
end;
kind := ckDelete;
int2 := $0;
inc(oldIndex1);
int1 := Ints1[oldIndex1]; //ie what we deleted
end;
new(compareRec);
compareRec^ := fLastCompareRec;
fCompareList.add(compareRec);
inc(fDiffStats.deletes);
end;
end;
end;
end;
//------------------------------------------------------------------------------
PROCEDURE TDiff.clear;
VAR
i: integer;
begin
for 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;
Ints1 := nil; Ints2 := nil;
end;
//------------------------------------------------------------------------------
FUNCTION TDiff.GetCompareCount: integer;
begin
result := fCompareList.count;
end;
//------------------------------------------------------------------------------
FUNCTION TDiff.GetCompare(CONST index: integer): TCompareRec;
begin
result := PCompareRec(fCompareList[index])^;
end;
//------------------------------------------------------------------------------
end.