DiffEngine.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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. /** @var string[] */
  48. private $from;
  49. /** @var string[] */
  50. private $to;
  51. private $m;
  52. private $n;
  53. private $tooLong;
  54. private $powLimit;
  55. protected $bailoutComplexity = 0;
  56. // State variables
  57. private $maxDifferences;
  58. private $lcsLengthCorrectedForHeuristic = false;
  59. // Output variables
  60. public $length;
  61. public $removed;
  62. public $added;
  63. public $heuristicUsed;
  64. function __construct( $tooLong = 2000000, $powLimit = 1.45 ) {
  65. $this->tooLong = $tooLong;
  66. $this->powLimit = $powLimit;
  67. }
  68. /**
  69. * Performs diff
  70. *
  71. * @param string[] $from_lines
  72. * @param string[] $to_lines
  73. * @throws ComplexityException
  74. *
  75. * @return DiffOp[]
  76. */
  77. public function diff( $from_lines, $to_lines ) {
  78. // Diff and store locally
  79. $this->diffInternal( $from_lines, $to_lines );
  80. // Merge edits when possible
  81. $this->shiftBoundaries( $from_lines, $this->removed, $this->added );
  82. $this->shiftBoundaries( $to_lines, $this->added, $this->removed );
  83. // Compute the edit operations.
  84. $n_from = count( $from_lines );
  85. $n_to = count( $to_lines );
  86. $edits = [];
  87. $xi = $yi = 0;
  88. while ( $xi < $n_from || $yi < $n_to ) {
  89. assert( $yi < $n_to || $this->removed[$xi] );
  90. assert( $xi < $n_from || $this->added[$yi] );
  91. // Skip matching "snake".
  92. $copy = [];
  93. while ( $xi < $n_from && $yi < $n_to
  94. && !$this->removed[$xi] && !$this->added[$yi]
  95. ) {
  96. $copy[] = $from_lines[$xi++];
  97. ++$yi;
  98. }
  99. if ( $copy ) {
  100. $edits[] = new DiffOpCopy( $copy );
  101. }
  102. // Find deletes & adds.
  103. $delete = [];
  104. while ( $xi < $n_from && $this->removed[$xi] ) {
  105. $delete[] = $from_lines[$xi++];
  106. }
  107. $add = [];
  108. while ( $yi < $n_to && $this->added[$yi] ) {
  109. $add[] = $to_lines[$yi++];
  110. }
  111. if ( $delete && $add ) {
  112. $edits[] = new DiffOpChange( $delete, $add );
  113. } elseif ( $delete ) {
  114. $edits[] = new DiffOpDelete( $delete );
  115. } elseif ( $add ) {
  116. $edits[] = new DiffOpAdd( $add );
  117. }
  118. }
  119. return $edits;
  120. }
  121. /**
  122. * Sets the complexity (in comparison operations) that can't be exceeded
  123. * @param int $value
  124. */
  125. public function setBailoutComplexity( $value ) {
  126. $this->bailoutComplexity = $value;
  127. }
  128. /**
  129. * Adjust inserts/deletes of identical lines to join changes
  130. * as much as possible.
  131. *
  132. * We do something when a run of changed lines include a
  133. * line at one end and has an excluded, identical line at the other.
  134. * We are free to choose which identical line is included.
  135. * `compareseq' usually chooses the one at the beginning,
  136. * but usually it is cleaner to consider the following identical line
  137. * to be the "change".
  138. *
  139. * This is extracted verbatim from analyze.c (GNU diffutils-2.7).
  140. *
  141. * @param string[] $lines
  142. * @param string[] $changed
  143. * @param string[] $other_changed
  144. */
  145. private function shiftBoundaries( array $lines, array &$changed, array $other_changed ) {
  146. $i = 0;
  147. $j = 0;
  148. assert( count( $lines ) == count( $changed ) );
  149. $len = count( $lines );
  150. $other_len = count( $other_changed );
  151. while ( 1 ) {
  152. /*
  153. * Scan forwards to find beginning of another run of changes.
  154. * Also keep track of the corresponding point in the other file.
  155. *
  156. * Throughout this code, $i and $j are adjusted together so that
  157. * the first $i elements of $changed and the first $j elements
  158. * of $other_changed both contain the same number of zeros
  159. * (unchanged lines).
  160. * Furthermore, $j is always kept so that $j == $other_len or
  161. * $other_changed[$j] == false.
  162. */
  163. while ( $j < $other_len && $other_changed[$j] ) {
  164. $j++;
  165. }
  166. while ( $i < $len && !$changed[$i] ) {
  167. assert( $j < $other_len && !$other_changed[$j] );
  168. $i++;
  169. $j++;
  170. while ( $j < $other_len && $other_changed[$j] ) {
  171. $j++;
  172. }
  173. }
  174. if ( $i == $len ) {
  175. break;
  176. }
  177. $start = $i;
  178. // Find the end of this run of changes.
  179. while ( ++$i < $len && $changed[$i] ) {
  180. continue;
  181. }
  182. do {
  183. /*
  184. * Record the length of this run of changes, so that
  185. * we can later determine whether the run has grown.
  186. */
  187. $runlength = $i - $start;
  188. /*
  189. * Move the changed region back, so long as the
  190. * previous unchanged line matches the last changed one.
  191. * This merges with previous changed regions.
  192. */
  193. while ( $start > 0 && $lines[$start - 1] == $lines[$i - 1] ) {
  194. $changed[--$start] = 1;
  195. $changed[--$i] = false;
  196. while ( $start > 0 && $changed[$start - 1] ) {
  197. $start--;
  198. }
  199. assert( $j > 0 );
  200. while ( $other_changed[--$j] ) {
  201. continue;
  202. }
  203. assert( $j >= 0 && !$other_changed[$j] );
  204. }
  205. /*
  206. * Set CORRESPONDING to the end of the changed run, at the last
  207. * point where it corresponds to a changed run in the other file.
  208. * CORRESPONDING == LEN means no such point has been found.
  209. */
  210. $corresponding = $j < $other_len ? $i : $len;
  211. /*
  212. * Move the changed region forward, so long as the
  213. * first changed line matches the following unchanged one.
  214. * This merges with following changed regions.
  215. * Do this second, so that if there are no merges,
  216. * the changed region is moved forward as far as possible.
  217. */
  218. while ( $i < $len && $lines[$start] == $lines[$i] ) {
  219. $changed[$start++] = false;
  220. $changed[$i++] = 1;
  221. while ( $i < $len && $changed[$i] ) {
  222. $i++;
  223. }
  224. assert( $j < $other_len && !$other_changed[$j] );
  225. $j++;
  226. if ( $j < $other_len && $other_changed[$j] ) {
  227. $corresponding = $i;
  228. while ( $j < $other_len && $other_changed[$j] ) {
  229. $j++;
  230. }
  231. }
  232. }
  233. } while ( $runlength != $i - $start );
  234. /*
  235. * If possible, move the fully-merged run of changes
  236. * back to a corresponding run in the other file.
  237. */
  238. while ( $corresponding < $i ) {
  239. $changed[--$start] = 1;
  240. $changed[--$i] = 0;
  241. assert( $j > 0 );
  242. while ( $other_changed[--$j] ) {
  243. continue;
  244. }
  245. assert( $j >= 0 && !$other_changed[$j] );
  246. }
  247. }
  248. }
  249. /**
  250. * @param string[] $from
  251. * @param string[] $to
  252. * @throws ComplexityException
  253. */
  254. protected function diffInternal( array $from, array $to ) {
  255. // remember initial lengths
  256. $m = count( $from );
  257. $n = count( $to );
  258. $this->heuristicUsed = false;
  259. // output
  260. $removed = $m > 0 ? array_fill( 0, $m, true ) : [];
  261. $added = $n > 0 ? array_fill( 0, $n, true ) : [];
  262. // reduce the complexity for the next step (intentionally done twice)
  263. // remove common tokens at the start
  264. $i = 0;
  265. while ( $i < $m && $i < $n && $from[$i] === $to[$i] ) {
  266. $removed[$i] = $added[$i] = false;
  267. unset( $from[$i], $to[$i] );
  268. ++$i;
  269. }
  270. // remove common tokens at the end
  271. $j = 1;
  272. while ( $i + $j <= $m && $i + $j <= $n && $from[$m - $j] === $to[$n - $j] ) {
  273. $removed[$m - $j] = $added[$n - $j] = false;
  274. unset( $from[$m - $j], $to[$n - $j] );
  275. ++$j;
  276. }
  277. $this->from = $newFromIndex = $this->to = $newToIndex = [];
  278. // remove tokens not in both sequences
  279. $shared = [];
  280. foreach ( $from as $key ) {
  281. $shared[$key] = false;
  282. }
  283. foreach ( $to as $index => &$el ) {
  284. if ( array_key_exists( $el, $shared ) ) {
  285. // keep it
  286. $this->to[] = $el;
  287. $shared[$el] = true;
  288. $newToIndex[] = $index;
  289. }
  290. }
  291. foreach ( $from as $index => &$el ) {
  292. if ( $shared[$el] ) {
  293. // keep it
  294. $this->from[] = $el;
  295. $newFromIndex[] = $index;
  296. }
  297. }
  298. unset( $shared, $from, $to );
  299. $this->m = count( $this->from );
  300. $this->n = count( $this->to );
  301. if ( $this->bailoutComplexity > 0 && $this->m * $this->n > $this->bailoutComplexity ) {
  302. throw new ComplexityException();
  303. }
  304. $this->removed = $this->m > 0 ? array_fill( 0, $this->m, true ) : [];
  305. $this->added = $this->n > 0 ? array_fill( 0, $this->n, true ) : [];
  306. if ( $this->m == 0 || $this->n == 0 ) {
  307. $this->length = 0;
  308. } else {
  309. $this->maxDifferences = ceil( ( $this->m + $this->n ) / 2.0 );
  310. if ( $this->m * $this->n > $this->tooLong ) {
  311. // limit complexity to D^POW_LIMIT for long sequences
  312. $this->maxDifferences = floor( $this->maxDifferences ** ( $this->powLimit - 1.0 ) );
  313. wfDebug( "Limiting max number of differences to $this->maxDifferences\n" );
  314. }
  315. /*
  316. * The common prefixes and suffixes are always part of some LCS, include
  317. * them now to reduce our search space
  318. */
  319. $max = min( $this->m, $this->n );
  320. for ( $forwardBound = 0; $forwardBound < $max
  321. && $this->from[$forwardBound] === $this->to[$forwardBound];
  322. ++$forwardBound
  323. ) {
  324. $this->removed[$forwardBound] = $this->added[$forwardBound] = false;
  325. }
  326. $backBoundL1 = $this->m - 1;
  327. $backBoundL2 = $this->n - 1;
  328. while ( $backBoundL1 >= $forwardBound && $backBoundL2 >= $forwardBound
  329. && $this->from[$backBoundL1] === $this->to[$backBoundL2]
  330. ) {
  331. $this->removed[$backBoundL1--] = $this->added[$backBoundL2--] = false;
  332. }
  333. $temp = array_fill( 0, $this->m + $this->n + 1, 0 );
  334. $V = [ $temp, $temp ];
  335. $snake = [ 0, 0, 0 ];
  336. $this->length = $forwardBound + $this->m - $backBoundL1 - 1
  337. + $this->lcs_rec(
  338. $forwardBound,
  339. $backBoundL1,
  340. $forwardBound,
  341. $backBoundL2,
  342. $V,
  343. $snake
  344. );
  345. }
  346. $this->m = $m;
  347. $this->n = $n;
  348. $this->length += $i + $j - 1;
  349. foreach ( $this->removed as $key => &$removed_elem ) {
  350. if ( !$removed_elem ) {
  351. $removed[$newFromIndex[$key]] = false;
  352. }
  353. }
  354. foreach ( $this->added as $key => &$added_elem ) {
  355. if ( !$added_elem ) {
  356. $added[$newToIndex[$key]] = false;
  357. }
  358. }
  359. $this->removed = $removed;
  360. $this->added = $added;
  361. }
  362. function diff_range( $from_lines, $to_lines ) {
  363. // Diff and store locally
  364. $this->diff( $from_lines, $to_lines );
  365. unset( $from_lines, $to_lines );
  366. $ranges = [];
  367. $xi = $yi = 0;
  368. while ( $xi < $this->m || $yi < $this->n ) {
  369. // Matching "snake".
  370. while ( $xi < $this->m && $yi < $this->n
  371. && !$this->removed[$xi]
  372. && !$this->added[$yi]
  373. ) {
  374. ++$xi;
  375. ++$yi;
  376. }
  377. // Find deletes & adds.
  378. $xstart = $xi;
  379. while ( $xi < $this->m && $this->removed[$xi] ) {
  380. ++$xi;
  381. }
  382. $ystart = $yi;
  383. while ( $yi < $this->n && $this->added[$yi] ) {
  384. ++$yi;
  385. }
  386. if ( $xi > $xstart || $yi > $ystart ) {
  387. $ranges[] = new RangeDifference( $xstart, $xi, $ystart, $yi );
  388. }
  389. }
  390. return $ranges;
  391. }
  392. private function lcs_rec( $bottoml1, $topl1, $bottoml2, $topl2, &$V, &$snake ) {
  393. // check that both sequences are non-empty
  394. if ( $bottoml1 > $topl1 || $bottoml2 > $topl2 ) {
  395. return 0;
  396. }
  397. $d = $this->find_middle_snake( $bottoml1, $topl1, $bottoml2,
  398. $topl2, $V, $snake );
  399. // need to store these so we don't lose them when they're
  400. // overwritten by the recursion
  401. list( $startx, $starty, $len ) = $snake;
  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 ) {
  446. $value_to_add_forward = 1;
  447. } else {
  448. $value_to_add_forward = 0;
  449. }
  450. if ( $N & 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 ) {
  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. }