DiffEngine.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. <?php
  2. /**
  3. * New version of the difference engine
  4. *
  5. * Copyright © 2008 Guy Van den Broeck <guy@guyvdb.eu>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. * http://www.gnu.org/copyleft/gpl.html
  21. *
  22. * @file
  23. * @ingroup DifferenceEngine
  24. */
  25. use MediaWiki\Diff\ComplexityException;
  26. // FIXME: Don't use assert() in this file
  27. // phpcs:disable MediaWiki.Usage.ForbiddenFunctions.assert
  28. /**
  29. * This diff implementation is mainly lifted from the LCS algorithm of the Eclipse project which
  30. * in turn is based on Myers' "An O(ND) difference algorithm and its variations"
  31. * (http://citeseer.ist.psu.edu/myers86ond.html) with range compression (see Wu et al.'s
  32. * "An O(NP) Sequence Comparison Algorithm").
  33. *
  34. * This implementation supports an upper bound on the execution time.
  35. *
  36. * Some ideas (and a bit of code) are from analyze.c, from GNU
  37. * diffutils-2.7, which can be found at:
  38. * ftp://gnudist.gnu.org/pub/gnu/diffutils/diffutils-2.7.tar.gz
  39. *
  40. * Complexity: O((M + N)D) worst case time, O(M + N + D^2) expected time, O(M + N) space
  41. *
  42. * @author Guy Van den Broeck, Geoffrey T. Dairiki, Tim Starling
  43. * @ingroup DifferenceEngine
  44. */
  45. class DiffEngine {
  46. // Input variables
  47. private $from;
  48. private $to;
  49. private $m;
  50. private $n;
  51. private $tooLong;
  52. private $powLimit;
  53. protected $bailoutComplexity = 0;
  54. // State variables
  55. private $maxDifferences;
  56. private $lcsLengthCorrectedForHeuristic = false;
  57. // Output variables
  58. public $length;
  59. public $removed;
  60. public $added;
  61. public $heuristicUsed;
  62. function __construct( $tooLong = 2000000, $powLimit = 1.45 ) {
  63. $this->tooLong = $tooLong;
  64. $this->powLimit = $powLimit;
  65. }
  66. /**
  67. * Performs diff
  68. *
  69. * @param string[] $from_lines
  70. * @param string[] $to_lines
  71. * @throws ComplexityException
  72. *
  73. * @return DiffOp[]
  74. */
  75. public function diff( $from_lines, $to_lines ) {
  76. // Diff and store locally
  77. $this->diffInternal( $from_lines, $to_lines );
  78. // Merge edits when possible
  79. $this->shiftBoundaries( $from_lines, $this->removed, $this->added );
  80. $this->shiftBoundaries( $to_lines, $this->added, $this->removed );
  81. // Compute the edit operations.
  82. $n_from = count( $from_lines );
  83. $n_to = count( $to_lines );
  84. $edits = [];
  85. $xi = $yi = 0;
  86. while ( $xi < $n_from || $yi < $n_to ) {
  87. assert( $yi < $n_to || $this->removed[$xi] );
  88. assert( $xi < $n_from || $this->added[$yi] );
  89. // Skip matching "snake".
  90. $copy = [];
  91. while ( $xi < $n_from && $yi < $n_to
  92. && !$this->removed[$xi] && !$this->added[$yi]
  93. ) {
  94. $copy[] = $from_lines[$xi++];
  95. ++$yi;
  96. }
  97. if ( $copy ) {
  98. $edits[] = new DiffOpCopy( $copy );
  99. }
  100. // Find deletes & adds.
  101. $delete = [];
  102. while ( $xi < $n_from && $this->removed[$xi] ) {
  103. $delete[] = $from_lines[$xi++];
  104. }
  105. $add = [];
  106. while ( $yi < $n_to && $this->added[$yi] ) {
  107. $add[] = $to_lines[$yi++];
  108. }
  109. if ( $delete && $add ) {
  110. $edits[] = new DiffOpChange( $delete, $add );
  111. } elseif ( $delete ) {
  112. $edits[] = new DiffOpDelete( $delete );
  113. } elseif ( $add ) {
  114. $edits[] = new DiffOpAdd( $add );
  115. }
  116. }
  117. return $edits;
  118. }
  119. /**
  120. * Sets the complexity (in comparison operations) that can't be exceeded
  121. * @param int $value
  122. */
  123. public function setBailoutComplexity( $value ) {
  124. $this->bailoutComplexity = $value;
  125. }
  126. /**
  127. * Adjust inserts/deletes of identical lines to join changes
  128. * as much as possible.
  129. *
  130. * We do something when a run of changed lines include a
  131. * line at one end and has an excluded, identical line at the other.
  132. * We are free to choose which identical line is included.
  133. * `compareseq' usually chooses the one at the beginning,
  134. * but usually it is cleaner to consider the following identical line
  135. * to be the "change".
  136. *
  137. * This is extracted verbatim from analyze.c (GNU diffutils-2.7).
  138. *
  139. * @param string[] $lines
  140. * @param string[] $changed
  141. * @param string[] $other_changed
  142. */
  143. private function shiftBoundaries( array $lines, array &$changed, array $other_changed ) {
  144. $i = 0;
  145. $j = 0;
  146. assert( count( $lines ) == count( $changed ) );
  147. $len = count( $lines );
  148. $other_len = count( $other_changed );
  149. while ( 1 ) {
  150. /*
  151. * Scan forwards to find beginning of another run of changes.
  152. * Also keep track of the corresponding point in the other file.
  153. *
  154. * Throughout this code, $i and $j are adjusted together so that
  155. * the first $i elements of $changed and the first $j elements
  156. * of $other_changed both contain the same number of zeros
  157. * (unchanged lines).
  158. * Furthermore, $j is always kept so that $j == $other_len or
  159. * $other_changed[$j] == false.
  160. */
  161. while ( $j < $other_len && $other_changed[$j] ) {
  162. $j++;
  163. }
  164. while ( $i < $len && !$changed[$i] ) {
  165. assert( $j < $other_len && !$other_changed[$j] );
  166. $i++;
  167. $j++;
  168. while ( $j < $other_len && $other_changed[$j] ) {
  169. $j++;
  170. }
  171. }
  172. if ( $i == $len ) {
  173. break;
  174. }
  175. $start = $i;
  176. // Find the end of this run of changes.
  177. while ( ++$i < $len && $changed[$i] ) {
  178. continue;
  179. }
  180. do {
  181. /*
  182. * Record the length of this run of changes, so that
  183. * we can later determine whether the run has grown.
  184. */
  185. $runlength = $i - $start;
  186. /*
  187. * Move the changed region back, so long as the
  188. * previous unchanged line matches the last changed one.
  189. * This merges with previous changed regions.
  190. */
  191. while ( $start > 0 && $lines[$start - 1] == $lines[$i - 1] ) {
  192. $changed[--$start] = 1;
  193. $changed[--$i] = false;
  194. while ( $start > 0 && $changed[$start - 1] ) {
  195. $start--;
  196. }
  197. assert( $j > 0 );
  198. while ( $other_changed[--$j] ) {
  199. continue;
  200. }
  201. assert( $j >= 0 && !$other_changed[$j] );
  202. }
  203. /*
  204. * Set CORRESPONDING to the end of the changed run, at the last
  205. * point where it corresponds to a changed run in the other file.
  206. * CORRESPONDING == LEN means no such point has been found.
  207. */
  208. $corresponding = $j < $other_len ? $i : $len;
  209. /*
  210. * Move the changed region forward, so long as the
  211. * first changed line matches the following unchanged one.
  212. * This merges with following changed regions.
  213. * Do this second, so that if there are no merges,
  214. * the changed region is moved forward as far as possible.
  215. */
  216. while ( $i < $len && $lines[$start] == $lines[$i] ) {
  217. $changed[$start++] = false;
  218. $changed[$i++] = 1;
  219. while ( $i < $len && $changed[$i] ) {
  220. $i++;
  221. }
  222. assert( $j < $other_len && !$other_changed[$j] );
  223. $j++;
  224. if ( $j < $other_len && $other_changed[$j] ) {
  225. $corresponding = $i;
  226. while ( $j < $other_len && $other_changed[$j] ) {
  227. $j++;
  228. }
  229. }
  230. }
  231. } while ( $runlength != $i - $start );
  232. /*
  233. * If possible, move the fully-merged run of changes
  234. * back to a corresponding run in the other file.
  235. */
  236. while ( $corresponding < $i ) {
  237. $changed[--$start] = 1;
  238. $changed[--$i] = 0;
  239. assert( $j > 0 );
  240. while ( $other_changed[--$j] ) {
  241. continue;
  242. }
  243. assert( $j >= 0 && !$other_changed[$j] );
  244. }
  245. }
  246. }
  247. /**
  248. * @param string[] $from
  249. * @param string[] $to
  250. * @throws ComplexityException
  251. */
  252. protected function diffInternal( array $from, array $to ) {
  253. // remember initial lengths
  254. $m = count( $from );
  255. $n = count( $to );
  256. $this->heuristicUsed = false;
  257. // output
  258. $removed = $m > 0 ? array_fill( 0, $m, true ) : [];
  259. $added = $n > 0 ? array_fill( 0, $n, true ) : [];
  260. // reduce the complexity for the next step (intentionally done twice)
  261. // remove common tokens at the start
  262. $i = 0;
  263. while ( $i < $m && $i < $n && $from[$i] === $to[$i] ) {
  264. $removed[$i] = $added[$i] = false;
  265. unset( $from[$i], $to[$i] );
  266. ++$i;
  267. }
  268. // remove common tokens at the end
  269. $j = 1;
  270. while ( $i + $j <= $m && $i + $j <= $n && $from[$m - $j] === $to[$n - $j] ) {
  271. $removed[$m - $j] = $added[$n - $j] = false;
  272. unset( $from[$m - $j], $to[$n - $j] );
  273. ++$j;
  274. }
  275. $this->from = $newFromIndex = $this->to = $newToIndex = [];
  276. // remove tokens not in both sequences
  277. $shared = [];
  278. foreach ( $from as $key ) {
  279. $shared[$key] = false;
  280. }
  281. foreach ( $to as $index => &$el ) {
  282. if ( array_key_exists( $el, $shared ) ) {
  283. // keep it
  284. $this->to[] = $el;
  285. $shared[$el] = true;
  286. $newToIndex[] = $index;
  287. }
  288. }
  289. foreach ( $from as $index => &$el ) {
  290. if ( $shared[$el] ) {
  291. // keep it
  292. $this->from[] = $el;
  293. $newFromIndex[] = $index;
  294. }
  295. }
  296. unset( $shared, $from, $to );
  297. $this->m = count( $this->from );
  298. $this->n = count( $this->to );
  299. if ( $this->bailoutComplexity > 0 && $this->m * $this->n > $this->bailoutComplexity ) {
  300. throw new ComplexityException();
  301. }
  302. $this->removed = $this->m > 0 ? array_fill( 0, $this->m, true ) : [];
  303. $this->added = $this->n > 0 ? array_fill( 0, $this->n, true ) : [];
  304. if ( $this->m == 0 || $this->n == 0 ) {
  305. $this->length = 0;
  306. } else {
  307. $this->maxDifferences = ceil( ( $this->m + $this->n ) / 2.0 );
  308. if ( $this->m * $this->n > $this->tooLong ) {
  309. // limit complexity to D^POW_LIMIT for long sequences
  310. $this->maxDifferences = floor( $this->maxDifferences ** ( $this->powLimit - 1.0 ) );
  311. wfDebug( "Limiting max number of differences to $this->maxDifferences\n" );
  312. }
  313. /*
  314. * The common prefixes and suffixes are always part of some LCS, include
  315. * them now to reduce our search space
  316. */
  317. $max = min( $this->m, $this->n );
  318. for ( $forwardBound = 0; $forwardBound < $max
  319. && $this->from[$forwardBound] === $this->to[$forwardBound];
  320. ++$forwardBound
  321. ) {
  322. $this->removed[$forwardBound] = $this->added[$forwardBound] = false;
  323. }
  324. $backBoundL1 = $this->m - 1;
  325. $backBoundL2 = $this->n - 1;
  326. while ( $backBoundL1 >= $forwardBound && $backBoundL2 >= $forwardBound
  327. && $this->from[$backBoundL1] === $this->to[$backBoundL2]
  328. ) {
  329. $this->removed[$backBoundL1--] = $this->added[$backBoundL2--] = false;
  330. }
  331. $temp = array_fill( 0, $this->m + $this->n + 1, 0 );
  332. $V = [ $temp, $temp ];
  333. $snake = [ 0, 0, 0 ];
  334. $this->length = $forwardBound + $this->m - $backBoundL1 - 1
  335. + $this->lcs_rec(
  336. $forwardBound,
  337. $backBoundL1,
  338. $forwardBound,
  339. $backBoundL2,
  340. $V,
  341. $snake
  342. );
  343. }
  344. $this->m = $m;
  345. $this->n = $n;
  346. $this->length += $i + $j - 1;
  347. foreach ( $this->removed as $key => &$removed_elem ) {
  348. if ( !$removed_elem ) {
  349. $removed[$newFromIndex[$key]] = false;
  350. }
  351. }
  352. foreach ( $this->added as $key => &$added_elem ) {
  353. if ( !$added_elem ) {
  354. $added[$newToIndex[$key]] = false;
  355. }
  356. }
  357. $this->removed = $removed;
  358. $this->added = $added;
  359. }
  360. function diff_range( $from_lines, $to_lines ) {
  361. // Diff and store locally
  362. $this->diff( $from_lines, $to_lines );
  363. unset( $from_lines, $to_lines );
  364. $ranges = [];
  365. $xi = $yi = 0;
  366. while ( $xi < $this->m || $yi < $this->n ) {
  367. // Matching "snake".
  368. while ( $xi < $this->m && $yi < $this->n
  369. && !$this->removed[$xi]
  370. && !$this->added[$yi]
  371. ) {
  372. ++$xi;
  373. ++$yi;
  374. }
  375. // Find deletes & adds.
  376. $xstart = $xi;
  377. while ( $xi < $this->m && $this->removed[$xi] ) {
  378. ++$xi;
  379. }
  380. $ystart = $yi;
  381. while ( $yi < $this->n && $this->added[$yi] ) {
  382. ++$yi;
  383. }
  384. if ( $xi > $xstart || $yi > $ystart ) {
  385. $ranges[] = new RangeDifference( $xstart, $xi, $ystart, $yi );
  386. }
  387. }
  388. return $ranges;
  389. }
  390. private function lcs_rec( $bottoml1, $topl1, $bottoml2, $topl2, &$V, &$snake ) {
  391. // check that both sequences are non-empty
  392. if ( $bottoml1 > $topl1 || $bottoml2 > $topl2 ) {
  393. return 0;
  394. }
  395. $d = $this->find_middle_snake( $bottoml1, $topl1, $bottoml2,
  396. $topl2, $V, $snake );
  397. // need to store these so we don't lose them when they're
  398. // overwritten by the recursion
  399. $len = $snake[2];
  400. $startx = $snake[0];
  401. $starty = $snake[1];
  402. // the middle snake is part of the LCS, store it
  403. for ( $i = 0; $i < $len; ++$i ) {
  404. $this->removed[$startx + $i] = $this->added[$starty + $i] = false;
  405. }
  406. if ( $d > 1 ) {
  407. return $len
  408. + $this->lcs_rec( $bottoml1, $startx - 1, $bottoml2,
  409. $starty - 1, $V, $snake )
  410. + $this->lcs_rec( $startx + $len, $topl1, $starty + $len,
  411. $topl2, $V, $snake );
  412. } elseif ( $d == 1 ) {
  413. /*
  414. * In this case the sequences differ by exactly 1 line. We have
  415. * already saved all the lines after the difference in the for loop
  416. * above, now we need to save all the lines before the difference.
  417. */
  418. $max = min( $startx - $bottoml1, $starty - $bottoml2 );
  419. for ( $i = 0; $i < $max; ++$i ) {
  420. $this->removed[$bottoml1 + $i] =
  421. $this->added[$bottoml2 + $i] = false;
  422. }
  423. return $max + $len;
  424. }
  425. return $len;
  426. }
  427. private function find_middle_snake( $bottoml1, $topl1, $bottoml2, $topl2, &$V, &$snake ) {
  428. $from = &$this->from;
  429. $to = &$this->to;
  430. $V0 = &$V[0];
  431. $V1 = &$V[1];
  432. $snake0 = &$snake[0];
  433. $snake1 = &$snake[1];
  434. $snake2 = &$snake[2];
  435. $bottoml1_min_1 = $bottoml1 - 1;
  436. $bottoml2_min_1 = $bottoml2 - 1;
  437. $N = $topl1 - $bottoml1_min_1;
  438. $M = $topl2 - $bottoml2_min_1;
  439. $delta = $N - $M;
  440. $maxabsx = $N + $bottoml1;
  441. $maxabsy = $M + $bottoml2;
  442. $limit = min( $this->maxDifferences, ceil( ( $N + $M ) / 2 ) );
  443. // value_to_add_forward: a 0 or 1 that we add to the start
  444. // offset to make it odd/even
  445. if ( ( $M & 1 ) == 1 ) {
  446. $value_to_add_forward = 1;
  447. } else {
  448. $value_to_add_forward = 0;
  449. }
  450. if ( ( $N & 1 ) == 1 ) {
  451. $value_to_add_backward = 1;
  452. } else {
  453. $value_to_add_backward = 0;
  454. }
  455. $start_forward = -$M;
  456. $end_forward = $N;
  457. $start_backward = -$N;
  458. $end_backward = $M;
  459. $limit_min_1 = $limit - 1;
  460. $limit_plus_1 = $limit + 1;
  461. $V0[$limit_plus_1] = 0;
  462. $V1[$limit_min_1] = $N;
  463. $limit = min( $this->maxDifferences, ceil( ( $N + $M ) / 2 ) );
  464. if ( ( $delta & 1 ) == 1 ) {
  465. for ( $d = 0; $d <= $limit; ++$d ) {
  466. $start_diag = max( $value_to_add_forward + $start_forward, -$d );
  467. $end_diag = min( $end_forward, $d );
  468. $value_to_add_forward = 1 - $value_to_add_forward;
  469. // compute forward furthest reaching paths
  470. for ( $k = $start_diag; $k <= $end_diag; $k += 2 ) {
  471. if ( $k == -$d || ( $k < $d
  472. && $V0[$limit_min_1 + $k] < $V0[$limit_plus_1 + $k] )
  473. ) {
  474. $x = $V0[$limit_plus_1 + $k];
  475. } else {
  476. $x = $V0[$limit_min_1 + $k] + 1;
  477. }
  478. $absx = $snake0 = $x + $bottoml1;
  479. $absy = $snake1 = $x - $k + $bottoml2;
  480. while ( $absx < $maxabsx && $absy < $maxabsy && $from[$absx] === $to[$absy] ) {
  481. ++$absx;
  482. ++$absy;
  483. }
  484. $x = $absx - $bottoml1;
  485. $snake2 = $absx - $snake0;
  486. $V0[$limit + $k] = $x;
  487. if ( $k >= $delta - $d + 1 && $k <= $delta + $d - 1
  488. && $x >= $V1[$limit + $k - $delta]
  489. ) {
  490. return 2 * $d - 1;
  491. }
  492. // check to see if we can cut down the diagonal range
  493. if ( $x >= $N && $end_forward > $k - 1 ) {
  494. $end_forward = $k - 1;
  495. } elseif ( $absy - $bottoml2 >= $M ) {
  496. $start_forward = $k + 1;
  497. $value_to_add_forward = 0;
  498. }
  499. }
  500. $start_diag = max( $value_to_add_backward + $start_backward, -$d );
  501. $end_diag = min( $end_backward, $d );
  502. $value_to_add_backward = 1 - $value_to_add_backward;
  503. // compute backward furthest reaching paths
  504. for ( $k = $start_diag; $k <= $end_diag; $k += 2 ) {
  505. if ( $k == $d
  506. || ( $k != -$d && $V1[$limit_min_1 + $k] < $V1[$limit_plus_1 + $k] )
  507. ) {
  508. $x = $V1[$limit_min_1 + $k];
  509. } else {
  510. $x = $V1[$limit_plus_1 + $k] - 1;
  511. }
  512. $y = $x - $k - $delta;
  513. $snake2 = 0;
  514. while ( $x > 0 && $y > 0
  515. && $from[$x + $bottoml1_min_1] === $to[$y + $bottoml2_min_1]
  516. ) {
  517. --$x;
  518. --$y;
  519. ++$snake2;
  520. }
  521. $V1[$limit + $k] = $x;
  522. // check to see if we can cut down our diagonal range
  523. if ( $x <= 0 ) {
  524. $start_backward = $k + 1;
  525. $value_to_add_backward = 0;
  526. } elseif ( $y <= 0 && $end_backward > $k - 1 ) {
  527. $end_backward = $k - 1;
  528. }
  529. }
  530. }
  531. } else {
  532. for ( $d = 0; $d <= $limit; ++$d ) {
  533. $start_diag = max( $value_to_add_forward + $start_forward, -$d );
  534. $end_diag = min( $end_forward, $d );
  535. $value_to_add_forward = 1 - $value_to_add_forward;
  536. // compute forward furthest reaching paths
  537. for ( $k = $start_diag; $k <= $end_diag; $k += 2 ) {
  538. if ( $k == -$d
  539. || ( $k < $d && $V0[$limit_min_1 + $k] < $V0[$limit_plus_1 + $k] )
  540. ) {
  541. $x = $V0[$limit_plus_1 + $k];
  542. } else {
  543. $x = $V0[$limit_min_1 + $k] + 1;
  544. }
  545. $absx = $snake0 = $x + $bottoml1;
  546. $absy = $snake1 = $x - $k + $bottoml2;
  547. while ( $absx < $maxabsx && $absy < $maxabsy && $from[$absx] === $to[$absy] ) {
  548. ++$absx;
  549. ++$absy;
  550. }
  551. $x = $absx - $bottoml1;
  552. $snake2 = $absx - $snake0;
  553. $V0[$limit + $k] = $x;
  554. // check to see if we can cut down the diagonal range
  555. if ( $x >= $N && $end_forward > $k - 1 ) {
  556. $end_forward = $k - 1;
  557. } elseif ( $absy - $bottoml2 >= $M ) {
  558. $start_forward = $k + 1;
  559. $value_to_add_forward = 0;
  560. }
  561. }
  562. $start_diag = max( $value_to_add_backward + $start_backward, -$d );
  563. $end_diag = min( $end_backward, $d );
  564. $value_to_add_backward = 1 - $value_to_add_backward;
  565. // compute backward furthest reaching paths
  566. for ( $k = $start_diag; $k <= $end_diag; $k += 2 ) {
  567. if ( $k == $d
  568. || ( $k != -$d && $V1[$limit_min_1 + $k] < $V1[$limit_plus_1 + $k] )
  569. ) {
  570. $x = $V1[$limit_min_1 + $k];
  571. } else {
  572. $x = $V1[$limit_plus_1 + $k] - 1;
  573. }
  574. $y = $x - $k - $delta;
  575. $snake2 = 0;
  576. while ( $x > 0 && $y > 0
  577. && $from[$x + $bottoml1_min_1] === $to[$y + $bottoml2_min_1]
  578. ) {
  579. --$x;
  580. --$y;
  581. ++$snake2;
  582. }
  583. $V1[$limit + $k] = $x;
  584. if ( $k >= -$delta - $d && $k <= $d - $delta
  585. && $x <= $V0[$limit + $k + $delta]
  586. ) {
  587. $snake0 = $bottoml1 + $x;
  588. $snake1 = $bottoml2 + $y;
  589. return 2 * $d;
  590. }
  591. // check to see if we can cut down our diagonal range
  592. if ( $x <= 0 ) {
  593. $start_backward = $k + 1;
  594. $value_to_add_backward = 0;
  595. } elseif ( $y <= 0 && $end_backward > $k - 1 ) {
  596. $end_backward = $k - 1;
  597. }
  598. }
  599. }
  600. }
  601. /*
  602. * computing the true LCS is too expensive, instead find the diagonal
  603. * with the most progress and pretend a midle snake of length 0 occurs
  604. * there.
  605. */
  606. $most_progress = self::findMostProgress( $M, $N, $limit, $V );
  607. $snake0 = $bottoml1 + $most_progress[0];
  608. $snake1 = $bottoml2 + $most_progress[1];
  609. $snake2 = 0;
  610. wfDebug( "Computing the LCS is too expensive. Using a heuristic.\n" );
  611. $this->heuristicUsed = true;
  612. return 5; /*
  613. * HACK: since we didn't really finish the LCS computation
  614. * we don't really know the length of the SES. We don't do
  615. * anything with the result anyway, unless it's <=1. We know
  616. * for a fact SES > 1 so 5 is as good a number as any to
  617. * return here
  618. */
  619. }
  620. private static function findMostProgress( $M, $N, $limit, $V ) {
  621. $delta = $N - $M;
  622. if ( ( $M & 1 ) == ( $limit & 1 ) ) {
  623. $forward_start_diag = max( -$M, -$limit );
  624. } else {
  625. $forward_start_diag = max( 1 - $M, -$limit );
  626. }
  627. $forward_end_diag = min( $N, $limit );
  628. if ( ( $N & 1 ) == ( $limit & 1 ) ) {
  629. $backward_start_diag = max( -$N, -$limit );
  630. } else {
  631. $backward_start_diag = max( 1 - $N, -$limit );
  632. }
  633. $backward_end_diag = -min( $M, $limit );
  634. $temp = [ 0, 0, 0 ];
  635. $max_progress = array_fill( 0, ceil( max( $forward_end_diag - $forward_start_diag,
  636. $backward_end_diag - $backward_start_diag ) / 2 ), $temp );
  637. $num_progress = 0; // the 1st entry is current, it is initialized
  638. // with 0s
  639. // first search the forward diagonals
  640. for ( $k = $forward_start_diag; $k <= $forward_end_diag; $k += 2 ) {
  641. $x = $V[0][$limit + $k];
  642. $y = $x - $k;
  643. if ( $x > $N || $y > $M ) {
  644. continue;
  645. }
  646. $progress = $x + $y;
  647. if ( $progress > $max_progress[0][2] ) {
  648. $num_progress = 0;
  649. $max_progress[0][0] = $x;
  650. $max_progress[0][1] = $y;
  651. $max_progress[0][2] = $progress;
  652. } elseif ( $progress == $max_progress[0][2] ) {
  653. ++$num_progress;
  654. $max_progress[$num_progress][0] = $x;
  655. $max_progress[$num_progress][1] = $y;
  656. $max_progress[$num_progress][2] = $progress;
  657. }
  658. }
  659. $max_progress_forward = true; // initially the maximum
  660. // progress is in the forward
  661. // direction
  662. // now search the backward diagonals
  663. for ( $k = $backward_start_diag; $k <= $backward_end_diag; $k += 2 ) {
  664. $x = $V[1][$limit + $k];
  665. $y = $x - $k - $delta;
  666. if ( $x < 0 || $y < 0 ) {
  667. continue;
  668. }
  669. $progress = $N - $x + $M - $y;
  670. if ( $progress > $max_progress[0][2] ) {
  671. $num_progress = 0;
  672. $max_progress_forward = false;
  673. $max_progress[0][0] = $x;
  674. $max_progress[0][1] = $y;
  675. $max_progress[0][2] = $progress;
  676. } elseif ( $progress == $max_progress[0][2] && !$max_progress_forward ) {
  677. ++$num_progress;
  678. $max_progress[$num_progress][0] = $x;
  679. $max_progress[$num_progress][1] = $y;
  680. $max_progress[$num_progress][2] = $progress;
  681. }
  682. }
  683. // return the middle diagonal with maximal progress.
  684. return $max_progress[(int)floor( $num_progress / 2 )];
  685. }
  686. /**
  687. * @return mixed
  688. */
  689. public function getLcsLength() {
  690. if ( $this->heuristicUsed && !$this->lcsLengthCorrectedForHeuristic ) {
  691. $this->lcsLengthCorrectedForHeuristic = true;
  692. $this->length = $this->m - array_sum( $this->added );
  693. }
  694. return $this->length;
  695. }
  696. }
  697. /**
  698. * Alternative representation of a set of changes, by the index
  699. * ranges that are changed.
  700. *
  701. * @ingroup DifferenceEngine
  702. */
  703. class RangeDifference {
  704. /** @var int */
  705. public $leftstart;
  706. /** @var int */
  707. public $leftend;
  708. /** @var int */
  709. public $leftlength;
  710. /** @var int */
  711. public $rightstart;
  712. /** @var int */
  713. public $rightend;
  714. /** @var int */
  715. public $rightlength;
  716. function __construct( $leftstart, $leftend, $rightstart, $rightend ) {
  717. $this->leftstart = $leftstart;
  718. $this->leftend = $leftend;
  719. $this->leftlength = $leftend - $leftstart;
  720. $this->rightstart = $rightstart;
  721. $this->rightend = $rightend;
  722. $this->rightlength = $rightend - $rightstart;
  723. }
  724. }