Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

Diff.java 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. package jdiff.util;
  2. import java.util.Hashtable;
  3. /** A class to compare vectors of objects. The result of comparison
  4. is a list of <code>change</code> objects which form an
  5. edit script. The objects compared are traditionally lines
  6. of text from two files. Comparison options such as "ignore
  7. whitespace" are implemented by modifying the <code>equals</code>
  8. and <code>hashcode</code> methods for the objects compared.
  9. <p>
  10. The basic algorithm is described in: </br>
  11. "An O(ND) Difference Algorithm and its Variations", Eugene Myers,
  12. Algorithmica Vol. 1 No. 2, 1986, p 251.
  13. <p>
  14. This class outputs different results from GNU diff 1.15 on some
  15. inputs. Our results are actually better (smaller change list, smaller
  16. total size of changes), but it would be nice to know why. Perhaps
  17. there is a memory overwrite bug in GNU diff 1.15.
  18. @author Stuart D. Gathman, translated from GNU diff 1.15
  19. Copyright (C) 2000 Business Management Systems, Inc.
  20. <p>
  21. This program is free software; you can redistribute it and/or modify
  22. it under the terms of the GNU General Public License as published by
  23. the Free Software Foundation; either version 1, or (at your option)
  24. any later version.
  25. <p>
  26. This program is distributed in the hope that it will be useful,
  27. but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. GNU General Public License for more details.
  30. <p>
  31. You should have received a copy of the <a href=COPYING.txt>
  32. GNU General Public License</a>
  33. along with this program; if not, write to the Free Software
  34. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  35. */
  36. public class Diff
  37. {
  38. /** Prepare to find differences between two arrays. Each element of
  39. the arrays is translated to an "equivalence number" based on
  40. the result of <code>equals</code>. The original Object arrays
  41. are no longer needed for computing the differences. They will
  42. be needed again later to print the results of the comparison as
  43. an edit script, if desired.
  44. */
  45. public Diff(Object[] a,Object[] b)
  46. {
  47. Hashtable h = new Hashtable(a.length + b.length);
  48. filevec[0] = new file_data(a,h);
  49. filevec[1] = new file_data(b,h);
  50. }
  51. /** 1 more than the maximum equivalence value used for this or its
  52. sibling file. */
  53. private int equiv_max = 1;
  54. /** When set to true, the comparison uses a heuristic to speed it up.
  55. With this heuristic, for files with a constant small density
  56. of changes, the algorithm is linear in the file size. */
  57. public boolean heuristic = false;
  58. /** When set to true, the algorithm returns a guarranteed minimal
  59. set of changes. This makes things slower, sometimes much slower. */
  60. public boolean no_discards = false;
  61. private int[] xvec, yvec; /* Vectors being compared. */
  62. private int[] fdiag; /* Vector, indexed by diagonal, containing
  63. the X coordinate of the point furthest
  64. along the given diagonal in the forward
  65. search of the edit matrix. */
  66. private int[] bdiag; /* Vector, indexed by diagonal, containing
  67. the X coordinate of the point furthest
  68. along the given diagonal in the backward
  69. search of the edit matrix. */
  70. private int fdiagoff, bdiagoff;
  71. private final file_data[] filevec = new file_data[2];
  72. private int cost;
  73. /** Find the midpoint of the shortest edit script for a specified
  74. portion of the two files.
  75. We scan from the beginnings of the files, and simultaneously from the ends,
  76. doing a breadth-first search through the space of edit-sequence.
  77. When the two searches meet, we have found the midpoint of the shortest
  78. edit sequence.
  79. The value returned is the number of the diagonal on which the midpoint lies.
  80. The diagonal number equals the number of inserted lines minus the number
  81. of deleted lines (counting only lines before the midpoint).
  82. The edit cost is stored into COST; this is the total number of
  83. lines inserted or deleted (counting only lines before the midpoint).
  84. This function assumes that the first lines of the specified portions
  85. of the two files do not match, and likewise that the last lines do not
  86. match. The caller must trim matching lines from the beginning and end
  87. of the portions it is going to specify.
  88. Note that if we return the "wrong" diagonal value, or if
  89. the value of bdiag at that diagonal is "wrong",
  90. the worst this can do is cause suboptimal diff output.
  91. It cannot cause incorrect diff output. */
  92. private int diag (int xoff, int xlim, int yoff, int ylim)
  93. {
  94. final int[] fd = fdiag; // Give the compiler a chance.
  95. final int[] bd = bdiag; // Additional help for the compiler.
  96. final int[] xv = xvec; // Still more help for the compiler.
  97. final int[] yv = yvec; // And more and more . . .
  98. final int dmin = xoff - ylim; // Minimum valid diagonal.
  99. final int dmax = xlim - yoff; // Maximum valid diagonal.
  100. final int fmid = xoff - yoff; // Center diagonal of top-down search.
  101. final int bmid = xlim - ylim; // Center diagonal of bottom-up search.
  102. int fmin = fmid, fmax = fmid; // Limits of top-down search.
  103. int bmin = bmid, bmax = bmid; // Limits of bottom-up search.
  104. /* True if southeast corner is on an odd
  105. diagonal with respect to the northwest. */
  106. final boolean odd = (fmid - bmid & 1) != 0;
  107. fd[fdiagoff + fmid] = xoff;
  108. bd[bdiagoff + bmid] = xlim;
  109. for (int c = 1;; ++c)
  110. {
  111. int d; /* Active diagonal. */
  112. boolean big_snake = false;
  113. /* Extend the top-down search by an edit step in each diagonal. */
  114. if (fmin > dmin)
  115. fd[fdiagoff + --fmin - 1] = -1;
  116. else
  117. ++fmin;
  118. if (fmax < dmax)
  119. fd[fdiagoff + ++fmax + 1] = -1;
  120. else
  121. --fmax;
  122. for (d = fmax; d >= fmin; d -= 2)
  123. {
  124. int x, y, oldx, tlo = fd[fdiagoff + d - 1], thi = fd[fdiagoff + d + 1];
  125. if (tlo >= thi)
  126. x = tlo + 1;
  127. else
  128. x = thi;
  129. oldx = x;
  130. y = x - d;
  131. while (x < xlim && y < ylim && xv[x] == yv[y]) {
  132. ++x; ++y;
  133. }
  134. if (x - oldx > 20)
  135. big_snake = true;
  136. fd[fdiagoff + d] = x;
  137. if (odd && bmin <= d && d <= bmax && bd[bdiagoff + d] <= fd[fdiagoff + d])
  138. {
  139. cost = 2 * c - 1;
  140. return d;
  141. }
  142. }
  143. /* Similar extend the bottom-up search. */
  144. if (bmin > dmin)
  145. bd[bdiagoff + --bmin - 1] = Integer.MAX_VALUE;
  146. else
  147. ++bmin;
  148. if (bmax < dmax)
  149. bd[bdiagoff + ++bmax + 1] = Integer.MAX_VALUE;
  150. else
  151. --bmax;
  152. for (d = bmax; d >= bmin; d -= 2)
  153. {
  154. int x, y, oldx, tlo = bd[bdiagoff + d - 1], thi = bd[bdiagoff + d + 1];
  155. if (tlo < thi)
  156. x = tlo;
  157. else
  158. x = thi - 1;
  159. oldx = x;
  160. y = x - d;
  161. while (x > xoff && y > yoff && xv[x - 1] == yv[y - 1]) {
  162. --x; --y;
  163. }
  164. if (oldx - x > 20)
  165. big_snake = true;
  166. bd[bdiagoff + d] = x;
  167. if (!odd && fmin <= d && d <= fmax && bd[bdiagoff + d] <= fd[fdiagoff + d])
  168. {
  169. cost = 2 * c;
  170. return d;
  171. }
  172. }
  173. /* Heuristic: check occasionally for a diagonal that has made
  174. lots of progress compared with the edit distance.
  175. If we have any such, find the one that has made the most
  176. progress and return it as if it had succeeded.
  177. With this heuristic, for files with a constant small density
  178. of changes, the algorithm is linear in the file size. */
  179. if (c > 200 && big_snake && heuristic)
  180. {
  181. int best = 0;
  182. int bestpos = -1;
  183. for (d = fmax; d >= fmin; d -= 2)
  184. {
  185. int dd = d - fmid;
  186. if ((fd[fdiagoff + d] - xoff)*2 - dd > 12 * (c + (dd > 0 ? dd : -dd)))
  187. {
  188. if (fd[fdiagoff + d] * 2 - dd > best
  189. && fd[fdiagoff + d] - xoff > 20
  190. && fd[fdiagoff + d] - d - yoff > 20)
  191. {
  192. int k;
  193. int x = fd[fdiagoff + d];
  194. /* We have a good enough best diagonal;
  195. now insist that it end with a significant snake. */
  196. for (k = 1; k <= 20; k++)
  197. if (xvec[x - k] != yvec[x - d - k])
  198. break;
  199. if (k == 21)
  200. {
  201. best = fd[fdiagoff + d] * 2 - dd;
  202. bestpos = d;
  203. }
  204. }
  205. }
  206. }
  207. if (best > 0)
  208. {
  209. cost = 2 * c - 1;
  210. return bestpos;
  211. }
  212. best = 0;
  213. for (d = bmax; d >= bmin; d -= 2)
  214. {
  215. int dd = d - bmid;
  216. if ((xlim - bd[bdiagoff + d])*2 + dd > 12 * (c + (dd > 0 ? dd : -dd)))
  217. {
  218. if ((xlim - bd[bdiagoff + d]) * 2 + dd > best
  219. && xlim - bd[bdiagoff + d] > 20
  220. && ylim - (bd[bdiagoff + d] - d) > 20)
  221. {
  222. /* We have a good enough best diagonal;
  223. now insist that it end with a significant snake. */
  224. int k;
  225. int x = bd[bdiagoff + d];
  226. for (k = 0; k < 20; k++)
  227. if (xvec[x + k] != yvec[x - d + k])
  228. break;
  229. if (k == 20)
  230. {
  231. best = (xlim - bd[bdiagoff + d]) * 2 + dd;
  232. bestpos = d;
  233. }
  234. }
  235. }
  236. }
  237. if (best > 0)
  238. {
  239. cost = 2 * c - 1;
  240. return bestpos;
  241. }
  242. }
  243. }
  244. }
  245. /** Compare in detail contiguous subsequences of the two files
  246. which are known, as a whole, to match each other.
  247. The results are recorded in the vectors filevec[N].changed_flag, by
  248. storing a 1 in the element for each line that is an insertion or deletion.
  249. The subsequence of file 0 is [XOFF, XLIM) and likewise for file 1.
  250. Note that XLIM, YLIM are exclusive bounds.
  251. All line numbers are origin-0 and discarded lines are not counted. */
  252. private void compareseq (int xoff, int xlim, int yoff, int ylim) {
  253. /* Slide down the bottom initial diagonal. */
  254. while (xoff < xlim && yoff < ylim && xvec[xoff] == yvec[yoff]) {
  255. ++xoff; ++yoff;
  256. }
  257. /* Slide up the top initial diagonal. */
  258. while (xlim > xoff && ylim > yoff && xvec[xlim - 1] == yvec[ylim - 1]) {
  259. --xlim; --ylim;
  260. }
  261. /* Handle simple cases. */
  262. if (xoff == xlim)
  263. while (yoff < ylim)
  264. filevec[1].changed_flag[1+filevec[1].realindexes[yoff++]] = true;
  265. else if (yoff == ylim)
  266. while (xoff < xlim)
  267. filevec[0].changed_flag[1+filevec[0].realindexes[xoff++]] = true;
  268. else
  269. {
  270. /* Find a point of correspondence in the middle of the files. */
  271. int d = diag (xoff, xlim, yoff, ylim);
  272. int c = cost;
  273. int f = fdiag[fdiagoff + d];
  274. int b = bdiag[bdiagoff + d];
  275. if (c == 1)
  276. {
  277. /* This should be impossible, because it implies that
  278. one of the two subsequences is empty,
  279. and that case was handled above without calling `diag'.
  280. Let's verify that this is true. */
  281. throw new IllegalArgumentException("Empty subsequence");
  282. }
  283. else
  284. {
  285. /* Use that point to split this problem into two subproblems. */
  286. compareseq (xoff, b, yoff, b - d);
  287. /* This used to use f instead of b,
  288. but that is incorrect!
  289. It is not necessarily the case that diagonal d
  290. has a snake from b to f. */
  291. compareseq (b, xlim, b - d, ylim);
  292. }
  293. }
  294. }
  295. /** Discard lines from one file that have no matches in the other file.
  296. */
  297. private void discard_confusing_lines() {
  298. filevec[0].discard_confusing_lines(filevec[1]);
  299. filevec[1].discard_confusing_lines(filevec[0]);
  300. }
  301. private boolean inhibit = false;
  302. /** Adjust inserts/deletes of blank lines to join changes
  303. as much as possible.
  304. */
  305. private void shift_boundaries() {
  306. if (inhibit)
  307. return;
  308. filevec[0].shift_boundaries(filevec[1]);
  309. filevec[1].shift_boundaries(filevec[0]);
  310. }
  311. /** Scan the tables of which lines are inserted and deleted,
  312. producing an edit script in reverse order. */
  313. private change build_reverse_script() {
  314. change script = null;
  315. final boolean[] changed0 = filevec[0].changed_flag;
  316. final boolean[] changed1 = filevec[1].changed_flag;
  317. final int len0 = filevec[0].buffered_lines;
  318. final int len1 = filevec[1].buffered_lines;
  319. /* Note that changedN[len0] does exist, and contains 0. */
  320. int i0 = 0, i1 = 0;
  321. while (i0 < len0 || i1 < len1)
  322. {
  323. if (changed0[1+i0] || changed1[1+i1])
  324. {
  325. int line0 = i0, line1 = i1;
  326. /* Find # lines changed here in each file. */
  327. while (changed0[1+i0]) ++i0;
  328. while (changed1[1+i1]) ++i1;
  329. /* Record this change. */
  330. script = new change(line0, line1, i0 - line0, i1 - line1, script);
  331. }
  332. /* We have reached lines in the two files that match each other. */
  333. i0++; i1++;
  334. }
  335. return script;
  336. }
  337. /** Scan the tables of which lines are inserted and deleted,
  338. producing an edit script in forward order. */
  339. private change build_script() {
  340. change script = null;
  341. final boolean[] changed0 = filevec[0].changed_flag;
  342. final boolean[] changed1 = filevec[1].changed_flag;
  343. final int len0 = filevec[0].buffered_lines;
  344. final int len1 = filevec[1].buffered_lines;
  345. int i0 = len0, i1 = len1;
  346. /* Note that changedN[-1] does exist, and contains 0. */
  347. while (i0 >= 0 || i1 >= 0)
  348. {
  349. if (changed0[i0] || changed1[i1])
  350. {
  351. int line0 = i0, line1 = i1;
  352. /* Find # lines changed here in each file. */
  353. while (changed0[i0]) --i0;
  354. while (changed1[i1]) --i1;
  355. /* Record this change. */
  356. script = new change(i0, i1, line0 - i0, line1 - i1, script);
  357. }
  358. /* We have reached lines in the two files that match each other. */
  359. i0--; i1--;
  360. }
  361. return script;
  362. }
  363. /* Report the differences of two files. DEPTH is the current directory
  364. depth. */
  365. public change diff_2(final boolean reverse) {
  366. /* Some lines are obviously insertions or deletions
  367. because they don't match anything. Detect them now,
  368. and avoid even thinking about them in the main comparison algorithm. */
  369. discard_confusing_lines ();
  370. /* Now do the main comparison algorithm, considering just the
  371. undiscarded lines. */
  372. xvec = filevec[0].undiscarded;
  373. yvec = filevec[1].undiscarded;
  374. int diags =
  375. filevec[0].nondiscarded_lines + filevec[1].nondiscarded_lines + 3;
  376. fdiag = new int[diags];
  377. fdiagoff = filevec[1].nondiscarded_lines + 1;
  378. bdiag = new int[diags];
  379. bdiagoff = filevec[1].nondiscarded_lines + 1;
  380. compareseq (0, filevec[0].nondiscarded_lines,
  381. 0, filevec[1].nondiscarded_lines);
  382. fdiag = null;
  383. bdiag = null;
  384. /* Modify the results slightly to make them prettier
  385. in cases where that can validly be done. */
  386. shift_boundaries ();
  387. /* Get the results of comparison in the form of a chain
  388. of `struct change's -- an edit script. */
  389. if (reverse)
  390. return build_reverse_script();
  391. else
  392. return build_script();
  393. }
  394. /** The result of comparison is an "edit script": a chain of change objects.
  395. Each change represents one place where some lines are deleted
  396. and some are inserted.
  397. LINE0 and LINE1 are the first affected lines in the two files (origin 0).
  398. DELETED is the number of lines deleted here from file 0.
  399. INSERTED is the number of lines inserted here in file 1.
  400. If DELETED is 0 then LINE0 is the number of the line before
  401. which the insertion was done; vice versa for INSERTED and LINE1. */
  402. public static class change {
  403. /** Previous or next edit command. */
  404. public change link;
  405. /** # lines of file 1 changed here. */
  406. public final int inserted;
  407. /** # lines of file 0 changed here. */
  408. public final int deleted;
  409. /** Line number of 1st deleted line. */
  410. public final int line0;
  411. /** Line number of 1st inserted line. */
  412. public final int line1;
  413. /** Cons an additional entry onto the front of an edit script OLD.
  414. LINE0 and LINE1 are the first affected lines in the two files (origin 0).
  415. DELETED is the number of lines deleted here from file 0.
  416. INSERTED is the number of lines inserted here in file 1.
  417. If DELETED is 0 then LINE0 is the number of the line before
  418. which the insertion was done; vice versa for INSERTED and LINE1. */
  419. change(int line0, int line1, int deleted, int inserted, change old) {
  420. this.line0 = line0;
  421. this.line1 = line1;
  422. this.inserted = inserted;
  423. this.deleted = deleted;
  424. this.link = old;
  425. //System.err.println(line0+","+line1+","+inserted+","+deleted);
  426. }
  427. }
  428. /** Data on one input file being compared.
  429. */
  430. class file_data {
  431. /** Allocate changed array for the results of comparison. */
  432. void clear() {
  433. /* Allocate a flag for each line of each file, saying whether that line
  434. is an insertion or deletion.
  435. Allocate an extra element, always zero, at each end of each vector.
  436. */
  437. changed_flag = new boolean[buffered_lines + 2];
  438. }
  439. /** Return equiv_count[I] as the number of lines in this file
  440. that fall in equivalence class I.
  441. @return the array of equivalence class counts.
  442. */
  443. int[] equivCount() {
  444. int[] equiv_count = new int[equiv_max];
  445. for (int i = 0; i < buffered_lines; ++i)
  446. ++equiv_count[equivs[i]];
  447. return equiv_count;
  448. }
  449. /** Discard lines that have no matches in another file.
  450. A line which is discarded will not be considered by the actual
  451. comparison algorithm; it will be as if that line were not in the file.
  452. The file's `realindexes' table maps virtual line numbers
  453. (which don't count the discarded lines) into real line numbers;
  454. this is how the actual comparison algorithm produces results
  455. that are comprehensible when the discarded lines are counted.
  456. <p>
  457. When we discard a line, we also mark it as a deletion or insertion
  458. so that it will be printed in the output.
  459. @param f the other file
  460. */
  461. void discard_confusing_lines(file_data f) {
  462. clear();
  463. /* Set up table of which lines are going to be discarded. */
  464. final byte[] discarded = discardable(f.equivCount());
  465. /* Don't really discard the provisional lines except when they occur
  466. in a run of discardables, with nonprovisionals at the beginning
  467. and end. */
  468. filterDiscards(discarded);
  469. /* Actually discard the lines. */
  470. discard(discarded);
  471. }
  472. /** Mark to be discarded each line that matches no line of another file.
  473. If a line matches many lines, mark it as provisionally discardable.
  474. @see equivCount()
  475. @param counts The count of each equivalence number for the other file.
  476. @return 0=nondiscardable, 1=discardable or 2=provisionally discardable
  477. for each line
  478. */
  479. private byte[] discardable(final int[] counts) {
  480. final int end = buffered_lines;
  481. final byte[] discards = new byte[end];
  482. final int[] equivs = this.equivs;
  483. int many = 5;
  484. int tem = end / 64;
  485. /* Multiply MANY by approximate square root of number of lines.
  486. That is the threshold for provisionally discardable lines. */
  487. while ((tem = tem >> 2) > 0)
  488. many *= 2;
  489. for (int i = 0; i < end; i++)
  490. {
  491. int nmatch;
  492. if (equivs[i] == 0)
  493. continue;
  494. nmatch = counts[equivs[i]];
  495. if (nmatch == 0)
  496. discards[i] = 1;
  497. else if (nmatch > many)
  498. discards[i] = 2;
  499. }
  500. return discards;
  501. }
  502. /** Don't really discard the provisional lines except when they occur
  503. in a run of discardables, with nonprovisionals at the beginning
  504. and end. */
  505. private void filterDiscards(final byte[] discards) {
  506. final int end = buffered_lines;
  507. for (int i = 0; i < end; i++)
  508. {
  509. /* Cancel provisional discards not in middle of run of discards. */
  510. if (discards[i] == 2)
  511. discards[i] = 0;
  512. else if (discards[i] != 0)
  513. {
  514. /* We have found a nonprovisional discard. */
  515. int j;
  516. int length;
  517. int provisional = 0;
  518. /* Find end of this run of discardable lines.
  519. Count how many are provisionally discardable. */
  520. for (j = i; j < end; j++)
  521. {
  522. if (discards[j] == 0)
  523. break;
  524. if (discards[j] == 2)
  525. ++provisional;
  526. }
  527. /* Cancel provisional discards at end, and shrink the run. */
  528. while (j > i && discards[j - 1] == 2) {
  529. discards[--j] = 0; --provisional;
  530. }
  531. /* Now we have the length of a run of discardable lines
  532. whose first and last are not provisional. */
  533. length = j - i;
  534. /* If 1/4 of the lines in the run are provisional,
  535. cancel discarding of all provisional lines in the run. */
  536. if (provisional * 4 > length)
  537. {
  538. while (j > i)
  539. if (discards[--j] == 2)
  540. discards[j] = 0;
  541. }
  542. else
  543. {
  544. int consec;
  545. int minimum = 1;
  546. int tem = length / 4;
  547. /* MINIMUM is approximate square root of LENGTH/4.
  548. A subrun of two or more provisionals can stand
  549. when LENGTH is at least 16.
  550. A subrun of 4 or more can stand when LENGTH >= 64. */
  551. while ((tem = tem >> 2) > 0)
  552. minimum *= 2;
  553. minimum++;
  554. /* Cancel any subrun of MINIMUM or more provisionals
  555. within the larger run. */
  556. for (j = 0, consec = 0; j < length; j++)
  557. if (discards[i + j] != 2)
  558. consec = 0;
  559. else if (minimum == ++consec)
  560. /* Back up to start of subrun, to cancel it all. */
  561. j -= consec;
  562. else if (minimum < consec)
  563. discards[i + j] = 0;
  564. /* Scan from beginning of run
  565. until we find 3 or more nonprovisionals in a row
  566. or until the first nonprovisional at least 8 lines in.
  567. Until that point, cancel any provisionals. */
  568. for (j = 0, consec = 0; j < length; j++)
  569. {
  570. if (j >= 8 && discards[i + j] == 1)
  571. break;
  572. if (discards[i + j] == 2) {
  573. consec = 0; discards[i + j] = 0;
  574. }
  575. else if (discards[i + j] == 0)
  576. consec = 0;
  577. else
  578. consec++;
  579. if (consec == 3)
  580. break;
  581. }
  582. /* I advances to the last line of the run. */
  583. i += length - 1;
  584. /* Same thing, from end. */
  585. for (j = 0, consec = 0; j < length; j++)
  586. {
  587. if (j >= 8 && discards[i - j] == 1)
  588. break;
  589. if (discards[i - j] == 2) {
  590. consec = 0; discards[i - j] = 0;
  591. }
  592. else if (discards[i - j] == 0)
  593. consec = 0;
  594. else
  595. consec++;
  596. if (consec == 3)
  597. break;
  598. }
  599. }
  600. }
  601. }
  602. }
  603. /** Actually discard the lines.
  604. @param discards flags lines to be discarded
  605. */
  606. private void discard(final byte[] discards) {
  607. final int end = buffered_lines;
  608. int j = 0;
  609. for (int i = 0; i < end; ++i)
  610. if (no_discards || discards[i] == 0)
  611. {
  612. undiscarded[j] = equivs[i];
  613. realindexes[j++] = i;
  614. }
  615. else
  616. changed_flag[1+i] = true;
  617. nondiscarded_lines = j;
  618. }
  619. file_data(Object[] data,Hashtable h) {
  620. buffered_lines = data.length;
  621. equivs = new int[buffered_lines];
  622. undiscarded = new int[buffered_lines];
  623. realindexes = new int[buffered_lines];
  624. for (int i = 0; i < data.length; ++i) {
  625. Integer ir = (Integer)h.get(data[i]);
  626. if (ir == null)
  627. h.put(data[i],new Integer(equivs[i] = equiv_max++));
  628. else
  629. equivs[i] = ir.intValue();
  630. }
  631. }
  632. /** Adjust inserts/deletes of blank lines to join changes
  633. as much as possible.
  634. We do something when a run of changed lines include a blank
  635. line at one end and have an excluded blank line at the other.
  636. We are free to choose which blank line is included.
  637. `compareseq' always chooses the one at the beginning,
  638. but usually it is cleaner to consider the following blank line
  639. to be the "change". The only exception is if the preceding blank line
  640. would join this change to other changes.
  641. @param f the file being compared against
  642. */
  643. void shift_boundaries(file_data f) {
  644. final boolean[] changed = changed_flag;
  645. final boolean[] other_changed = f.changed_flag;
  646. int i = 0;
  647. int j = 0;
  648. int i_end = buffered_lines;
  649. int preceding = -1;
  650. int other_preceding = -1;
  651. for (;;)
  652. {
  653. int start, end, other_start;
  654. /* Scan forwards to find beginning of another run of changes.
  655. Also keep track of the corresponding point in the other file. */
  656. while (i < i_end && !changed[1+i])
  657. {
  658. while (other_changed[1+j++])
  659. /* Non-corresponding lines in the other file
  660. will count as the preceding batch of changes. */
  661. other_preceding = j;
  662. i++;
  663. }
  664. if (i == i_end)
  665. break;
  666. start = i;
  667. other_start = j;
  668. for (;;)
  669. {
  670. /* Now find the end of this run of changes. */
  671. while (i < i_end && changed[1+i]) i++;
  672. end = i;
  673. /* If the first changed line matches the following unchanged one,
  674. and this run does not follow right after a previous run,
  675. and there are no lines deleted from the other file here,
  676. then classify the first changed line as unchanged
  677. and the following line as changed in its place. */
  678. /* You might ask, how could this run follow right after another?
  679. Only because the previous run was shifted here. */
  680. if (end != i_end
  681. && equivs[start] == equivs[end]
  682. && !other_changed[1+j]
  683. && end != i_end
  684. && !((preceding >= 0 && start == preceding)
  685. || (other_preceding >= 0
  686. && other_start == other_preceding)))
  687. {
  688. changed[1+end++] = true;
  689. changed[1+start++] = false;
  690. ++i;
  691. /* Since one line-that-matches is now before this run
  692. instead of after, we must advance in the other file
  693. to keep in synch. */
  694. ++j;
  695. }
  696. else
  697. break;
  698. }
  699. preceding = i;
  700. other_preceding = j;
  701. }
  702. }
  703. /** Number of elements (lines) in this file. */
  704. final int buffered_lines;
  705. /** Vector, indexed by line number, containing an equivalence code for
  706. each line. It is this vector that is actually compared with that
  707. of another file to generate differences. */
  708. private final int[] equivs;
  709. /** Vector, like the previous one except that
  710. the elements for discarded lines have been squeezed out. */
  711. final int[] undiscarded;
  712. /** Vector mapping virtual line numbers (not counting discarded lines)
  713. to real ones (counting those lines). Both are origin-0. */
  714. final int[] realindexes;
  715. /** Total number of nondiscarded lines. */
  716. int nondiscarded_lines;
  717. /** Array, indexed by real origin-1 line number,
  718. containing true for a line that is an insertion or a deletion.
  719. The results of comparison are stored here. */
  720. boolean[] changed_flag;
  721. }
  722. }