Diff.java
15.9 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
/*
* Decompiled with CFR 0_118.
*/
package com.day.util.diff;
import java.util.HashMap;
public class Diff {
static final String CVS_ID = "$URL: http://svn.day.com/repos/wiki/trunk/wiki-parser/src/main/java/com/day/wiki/parser/util/diff/Diff.java $ $Rev: 29931 $ $Date: 2007-08-07 15:17:24 +0200 (Tue, 07 Aug 2007) $";
private int equiv_max = 1;
public boolean heuristic = false;
public boolean no_discards = false;
private int[] xvec;
private int[] yvec;
private int[] fdiag;
private int[] bdiag;
private int fdiagoff;
private int bdiagoff;
private final FileData[] filevec = new FileData[2];
private int cost;
private boolean inhibit = false;
public Diff(Object[] a, Object[] b) {
HashMap h = new HashMap(a.length + b.length);
this.filevec[0] = new FileData(a, h);
this.filevec[1] = new FileData(b, h);
}
private int diag(int xoff, int xlim, int yoff, int ylim) {
int[] fd = this.fdiag;
int[] bd = this.bdiag;
int[] xv = this.xvec;
int[] yv = this.yvec;
int dmin = xoff - ylim;
int dmax = xlim - yoff;
int fmid = xoff - yoff;
int bmid = xlim - ylim;
int fmin = fmid;
int fmax = fmid;
int bmin = bmid;
int bmax = bmid;
boolean odd = (fmid - bmid & 1) != 0;
fd[this.fdiagoff + fmid] = xoff;
bd[this.bdiagoff + bmid] = xlim;
int c = 1;
do {
int y;
int tlo;
int d;
int oldx;
int thi;
int x;
boolean big_snake = false;
if (fmin > dmin) {
fd[this.fdiagoff + --fmin - 1] = -1;
} else {
++fmin;
}
if (fmax < dmax) {
fd[this.fdiagoff + ++fmax + 1] = -1;
} else {
--fmax;
}
for (d = fmax; d >= fmin; d -= 2) {
tlo = fd[this.fdiagoff + d - 1];
thi = fd[this.fdiagoff + d + 1];
x = tlo >= thi ? tlo + 1 : thi;
oldx = x;
for (y = x - d; x < xlim && y < ylim && xv[x] == yv[y]; ++x, ++y) {
}
if (x - oldx > 20) {
big_snake = true;
}
fd[this.fdiagoff + d] = x;
if (!odd || bmin > d || d > bmax || bd[this.bdiagoff + d] > fd[this.fdiagoff + d]) continue;
this.cost = 2 * c - 1;
return d;
}
if (bmin > dmin) {
bd[this.bdiagoff + --bmin - 1] = Integer.MAX_VALUE;
} else {
++bmin;
}
if (bmax < dmax) {
bd[this.bdiagoff + ++bmax + 1] = Integer.MAX_VALUE;
} else {
--bmax;
}
for (d = bmax; d >= bmin; d -= 2) {
tlo = bd[this.bdiagoff + d - 1];
thi = bd[this.bdiagoff + d + 1];
x = tlo < thi ? tlo : thi - 1;
oldx = x;
for (y = x - d; x > xoff && y > yoff && xv[x - 1] == yv[y - 1]; --x, --y) {
}
if (oldx - x > 20) {
big_snake = true;
}
bd[this.bdiagoff + d] = x;
if (odd || fmin > d || d > fmax || bd[this.bdiagoff + d] > fd[this.fdiagoff + d]) continue;
this.cost = 2 * c;
return d;
}
if (c > 200 && big_snake && this.heuristic) {
int k;
int x2;
int dd;
int best = 0;
int bestpos = -1;
for (d = fmax; d >= fmin; d -= 2) {
if ((fd[this.fdiagoff + d] - xoff) * 2 - dd <= 12 * (c + ((dd = d - fmid) > 0 ? dd : - dd)) || fd[this.fdiagoff + d] * 2 - dd <= best || fd[this.fdiagoff + d] - xoff <= 20 || fd[this.fdiagoff + d] - d - yoff <= 20) continue;
x2 = fd[this.fdiagoff + d];
for (k = 1; k <= 20 && this.xvec[x2 - k] == this.yvec[x2 - d - k]; ++k) {
}
if (k != 21) continue;
best = fd[this.fdiagoff + d] * 2 - dd;
bestpos = d;
}
if (best > 0) {
this.cost = 2 * c - 1;
return bestpos;
}
best = 0;
for (d = bmax; d >= bmin; d -= 2) {
if ((xlim - bd[this.bdiagoff + d]) * 2 + dd <= 12 * (c + ((dd = d - bmid) > 0 ? dd : - dd)) || (xlim - bd[this.bdiagoff + d]) * 2 + dd <= best || xlim - bd[this.bdiagoff + d] <= 20 || ylim - (bd[this.bdiagoff + d] - d) <= 20) continue;
x2 = bd[this.bdiagoff + d];
for (k = 0; k < 20 && this.xvec[x2 + k] == this.yvec[x2 - d + k]; ++k) {
}
if (k != 20) continue;
best = (xlim - bd[this.bdiagoff + d]) * 2 + dd;
bestpos = d;
}
if (best > 0) {
this.cost = 2 * c - 1;
return bestpos;
}
}
++c;
} while (true);
}
private void compareseq(int xoff, int xlim, int yoff, int ylim) {
while (xoff < xlim && yoff < ylim && this.xvec[xoff] == this.yvec[yoff]) {
++xoff;
++yoff;
}
while (xlim > xoff && ylim > yoff && this.xvec[xlim - 1] == this.yvec[ylim - 1]) {
--xlim;
--ylim;
}
if (xoff == xlim) {
while (yoff < ylim) {
this.filevec[1].changed_flag[1 + this.filevec[1].realindexes[yoff++]] = true;
}
} else if (yoff == ylim) {
while (xoff < xlim) {
this.filevec[0].changed_flag[1 + this.filevec[0].realindexes[xoff++]] = true;
}
} else {
int d = this.diag(xoff, xlim, yoff, ylim);
int c = this.cost;
int f = this.fdiag[this.fdiagoff + d];
int b = this.bdiag[this.bdiagoff + d];
if (c == 1) {
throw new IllegalArgumentException("Empty subsequence");
}
this.compareseq(xoff, b, yoff, b - d);
this.compareseq(b, xlim, b - d, ylim);
}
}
private void discard_confusing_lines() {
this.filevec[0].discard_confusing_lines(this.filevec[1]);
this.filevec[1].discard_confusing_lines(this.filevec[0]);
}
private void shift_boundaries() {
if (this.inhibit) {
return;
}
this.filevec[0].shift_boundaries(this.filevec[1]);
this.filevec[1].shift_boundaries(this.filevec[0]);
}
private Change build_reverse_script() {
Change script = null;
boolean[] changed0 = this.filevec[0].changed_flag;
boolean[] changed1 = this.filevec[1].changed_flag;
int len0 = this.filevec[0].buffered_lines;
int len1 = this.filevec[1].buffered_lines;
int i0 = 0;
for (int i1 = 0; i0 < len0 || i1 < len1; ++i0, ++i1) {
if (!changed0[1 + i0] && !changed1[1 + i1]) continue;
int line0 = i0;
int line1 = i1;
while (changed0[1 + i0]) {
++i0;
}
while (changed1[1 + i1]) {
++i1;
}
script = new Change(line0, line1, i0 - line0, i1 - line1, script);
}
return script;
}
private Change build_script() {
Change script = null;
boolean[] changed0 = this.filevec[0].changed_flag;
boolean[] changed1 = this.filevec[1].changed_flag;
int len0 = this.filevec[0].buffered_lines;
int len1 = this.filevec[1].buffered_lines;
int i0 = len0;
for (int i1 = len1; i0 >= 0 || i1 >= 0; --i0, --i1) {
if (!changed0[i0] && !changed1[i1]) continue;
int line0 = i0;
int line1 = i1;
while (changed0[i0]) {
--i0;
}
while (changed1[i1]) {
--i1;
}
script = new Change(i0, i1, line0 - i0, line1 - i1, script);
}
return script;
}
public Change diff_2(boolean reverse) {
this.discard_confusing_lines();
this.xvec = this.filevec[0].undiscarded;
this.yvec = this.filevec[1].undiscarded;
int diags = this.filevec[0].nondiscarded_lines + this.filevec[1].nondiscarded_lines + 3;
this.fdiag = new int[diags];
this.fdiagoff = this.filevec[1].nondiscarded_lines + 1;
this.bdiag = new int[diags];
this.bdiagoff = this.filevec[1].nondiscarded_lines + 1;
this.compareseq(0, this.filevec[0].nondiscarded_lines, 0, this.filevec[1].nondiscarded_lines);
this.fdiag = null;
this.bdiag = null;
this.shift_boundaries();
if (reverse) {
return this.build_reverse_script();
}
return this.build_script();
}
class FileData {
final int buffered_lines;
private final int[] equivs;
final int[] undiscarded;
final int[] realindexes;
int nondiscarded_lines;
boolean[] changed_flag;
void clear() {
this.changed_flag = new boolean[this.buffered_lines + 2];
}
int[] equivCount() {
int[] equiv_count = new int[Diff.this.equiv_max];
for (int i = 0; i < this.buffered_lines; ++i) {
int[] arrn = equiv_count;
int n = this.equivs[i];
arrn[n] = arrn[n] + 1;
}
return equiv_count;
}
void discard_confusing_lines(FileData f) {
this.clear();
byte[] discarded = this.discardable(f.equivCount());
this.filterDiscards(discarded);
this.discard(discarded);
}
private byte[] discardable(int[] counts) {
int end = this.buffered_lines;
byte[] discards = new byte[end];
int[] equivs = this.equivs;
int many = 5;
int tem = end / 64;
while ((tem >>= 2) > 0) {
many *= 2;
}
for (int i = 0; i < end; ++i) {
if (equivs[i] == 0) continue;
int nmatch = counts[equivs[i]];
if (nmatch == 0) {
discards[i] = 1;
continue;
}
if (nmatch <= many) continue;
discards[i] = 2;
}
return discards;
}
private void filterDiscards(byte[] discards) {
int end = this.buffered_lines;
block0 : for (int i = 0; i < end; ++i) {
int j;
if (discards[i] == 2) {
discards[i] = 0;
continue;
}
if (discards[i] == 0) continue;
int provisional = 0;
for (j = i; j < end && discards[j] != 0; ++j) {
if (discards[j] != 2) continue;
++provisional;
}
while (j > i && discards[j - 1] == 2) {
discards[--j] = 0;
--provisional;
}
int length = j - i;
if (provisional * 4 > length) {
while (j > i) {
if (discards[--j] != 2) continue;
discards[j] = 0;
}
continue;
}
int minimum = 1;
int tem = length / 4;
while ((tem >>= 2) > 0) {
minimum *= 2;
}
++minimum;
int consec = 0;
for (j = 0; j < length; ++j) {
if (discards[i + j] != 2) {
consec = 0;
continue;
}
if (minimum == ++consec) {
j -= consec;
continue;
}
if (minimum >= consec) continue;
discards[i + j] = 0;
}
consec = 0;
for (j = 0; j < length && (j < 8 || discards[i + j] != 1); ++j) {
if (discards[i + j] == 2) {
consec = 0;
discards[i + j] = 0;
} else {
consec = discards[i + j] == 0 ? 0 : ++consec;
}
if (consec == 3) break;
}
consec = 0;
for (j = 0; j < length && (j < 8 || discards[(i += length - 1) - j] != 1); ++j) {
if (discards[i - j] == 2) {
consec = 0;
discards[i - j] = 0;
} else {
consec = discards[i - j] == 0 ? 0 : ++consec;
}
if (consec == 3) continue block0;
}
}
}
private void discard(byte[] discards) {
int end = this.buffered_lines;
int j = 0;
for (int i = 0; i < end; ++i) {
if (Diff.this.no_discards || discards[i] == 0) {
this.undiscarded[j] = this.equivs[i];
this.realindexes[j++] = i;
continue;
}
this.changed_flag[1 + i] = true;
}
this.nondiscarded_lines = j;
}
FileData(Object[] data, HashMap h) {
this.buffered_lines = data.length;
this.equivs = new int[this.buffered_lines];
this.undiscarded = new int[this.buffered_lines];
this.realindexes = new int[this.buffered_lines];
for (int i = 0; i < data.length; ++i) {
Integer ir = (Integer)h.get(data[i]);
if (ir == null) {
this.equivs[i] = Diff.this.equiv_max++;
h.put(data[i], new Integer(this.equivs[i]));
continue;
}
this.equivs[i] = ir;
}
}
void shift_boundaries(FileData f) {
boolean[] changed = this.changed_flag;
boolean[] other_changed = f.changed_flag;
int i = 0;
int j = 0;
int i_end = this.buffered_lines;
int preceding = -1;
int other_preceding = -1;
do {
if (i < i_end && !changed[1 + i]) {
while (other_changed[1 + j++]) {
other_preceding = j;
}
++i;
continue;
}
if (i == i_end) break;
int start = i;
int other_start = j;
do {
if (i < i_end && changed[1 + i]) {
++i;
continue;
}
int end = i;
if (end == i_end || this.equivs[start] != this.equivs[end] || other_changed[1 + j] || end == i_end || preceding >= 0 && start == preceding || other_preceding >= 0 && other_start == other_preceding) break;
changed[1 + end++] = true;
changed[1 + start++] = false;
++i;
++j;
} while (true);
preceding = i;
other_preceding = j;
} while (true);
}
}
public static class Change {
public Change nextChange;
public final int inserted;
public final int deleted;
public final int line0;
public final int line1;
Change(int line0, int line1, int deleted, int inserted, Change old) {
this.line0 = line0;
this.line1 = line1;
this.inserted = inserted;
this.deleted = deleted;
this.nextChange = old;
}
}
}