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