DiffEngine.php 22 KB

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