101 public function diff( $from_lines, $to_lines ) {
110 $n_from = count( $from_lines );
111 $n_to = count( $to_lines );
115 while ( $xi < $n_from || $yi < $n_to ) {
116 assert( $yi < $n_to || $this->removed[$xi] );
117 assert( $xi < $n_from || $this->added[$yi] );
121 while ( $xi < $n_from && $yi < $n_to
122 && !$this->removed[$xi] && !$this->added[$yi]
124 $copy[] = $from_lines[$xi++];
133 while ( $xi < $n_from && $this->removed[$xi] ) {
134 $delete[] = $from_lines[$xi++];
138 while ( $yi < $n_to && $this->added[$yi] ) {
139 $add[] = $to_lines[$yi++];
142 if ( $delete && $add ) {
144 } elseif ( $delete ) {
183 assert( count(
$lines ) == count( $changed ) );
185 $other_len = count( $other_changed );
199 while ( $j < $other_len && $other_changed[$j] ) {
203 while ( $i < $len && !$changed[$i] ) {
204 assert( $j < $other_len && !$other_changed[$j] );
207 while ( $j < $other_len && $other_changed[$j] ) {
219 while ( ++$i < $len && $changed[$i] ) {
228 $runlength = $i - $start;
235 while ( $start > 0 &&
$lines[$start - 1] ==
$lines[$i - 1] ) {
236 $changed[--$start] = 1;
237 $changed[--$i] =
false;
239 while ( $start > 0 && $changed[$start - 1] ) {
243 while ( $other_changed[--$j] ) {
246 assert( $j >= 0 && !$other_changed[$j] );
254 $corresponding = $j < $other_len ? $i : $len;
264 $changed[$start++] =
false;
266 while ( $i < $len && $changed[$i] ) {
270 assert( $j < $other_len && !$other_changed[$j] );
272 if ( $j < $other_len && $other_changed[$j] ) {
274 while ( $j < $other_len && $other_changed[$j] ) {
279 }
while ( $runlength != $i - $start );
285 while ( $corresponding < $i ) {
286 $changed[--$start] = 1;
289 while ( $other_changed[--$j] ) {
292 assert( $j >= 0 && !$other_changed[$j] );
307 $this->heuristicUsed =
false;
311 $added =
$n > 0 ? array_fill( 0,
$n,
true ) : [];
324 while ( $i + $j <=
$m && $i + $j <=
$n &&
$from[
$m - $j] ===
$to[
$n - $j] ) {
330 $this->from = $newFromIndex = $this->to = $newToIndex = [];
334 foreach (
$from as $key ) {
335 $shared[$key] =
false;
338 foreach (
$to as $index => &$el ) {
339 if ( array_key_exists( $el, $shared ) ) {
343 $newToIndex[] = $index;
346 foreach (
$from as $index => &$el ) {
347 if ( $shared[$el] ) {
350 $newFromIndex[] = $index;
356 $this->m = count( $this->from );
357 $this->
n = count( $this->to );
359 if ( $this->bailoutComplexity > 0 && $this->m * $this->
n > $this->bailoutComplexity ) {
363 $this->removed = $this->m > 0 ? array_fill( 0, $this->m,
true ) : [];
364 $this->added = $this->
n > 0 ? array_fill( 0, $this->
n,
true ) : [];
366 if ( $this->m == 0 || $this->
n == 0 ) {
369 $this->maxDifferences = ceil( ( $this->m + $this->
n ) / 2.0 );
370 if ( $this->m * $this->
n > $this->tooLong ) {
372 $this->maxDifferences = floor( $this->maxDifferences ** ( $this->powLimit - 1.0 ) );
373 wfDebug(
"Limiting max number of differences to $this->maxDifferences" );
380 $max = min( $this->m, $this->
n );
381 for ( $forwardBound = 0; $forwardBound < $max
382 && $this->from[$forwardBound] === $this->to[$forwardBound];
385 $this->removed[$forwardBound] = $this->added[$forwardBound] =
false;
388 $backBoundL1 = $this->m - 1;
389 $backBoundL2 = $this->
n - 1;
391 while ( $backBoundL1 >= $forwardBound && $backBoundL2 >= $forwardBound
392 && $this->from[$backBoundL1] === $this->to[$backBoundL2]
394 $this->removed[$backBoundL1--] = $this->added[$backBoundL2--] =
false;
397 $temp = array_fill( 0, $this->m + $this->
n + 1, 0 );
398 $V = [ $temp, $temp ];
399 $snake = [ 0, 0, 0 ];
401 $this->length = $forwardBound + $this->m - $backBoundL1 - 1
415 $this->length += $i + $j - 1;
417 foreach ( $this->removed as $key => &$removed_elem ) {
418 if ( !$removed_elem ) {
419 $removed[$newFromIndex[$key]] =
false;
422 foreach ( $this->added as $key => &$added_elem ) {
423 if ( !$added_elem ) {
424 $added[$newToIndex[$key]] =
false;
440 private function lcs_rec( $bottoml1, $topl1, $bottoml2, $topl2, &$V, &$snake ) {
442 if ( $bottoml1 > $topl1 || $bottoml2 > $topl2 ) {
447 $topl2, $V, $snake );
451 list( $startx, $starty, $len ) = $snake;
454 for ( $i = 0; $i < $len; ++$i ) {
455 $this->removed[$startx + $i] = $this->added[$starty + $i] =
false;
460 + $this->
lcs_rec( $bottoml1, $startx - 1, $bottoml2,
461 $starty - 1, $V, $snake )
462 + $this->
lcs_rec( $startx + $len, $topl1, $starty + $len,
463 $topl2, $V, $snake );
464 } elseif ( $d == 1 ) {
470 $max = min( $startx - $bottoml1, $starty - $bottoml2 );
471 for ( $i = 0; $i < $max; ++$i ) {
472 $this->removed[$bottoml1 + $i] =
473 $this->added[$bottoml2 + $i] =
false;
496 $snake0 = &$snake[0];
497 $snake1 = &$snake[1];
498 $snake2 = &$snake[2];
499 $bottoml1_min_1 = $bottoml1 - 1;
500 $bottoml2_min_1 = $bottoml2 - 1;
501 $N = $topl1 - $bottoml1_min_1;
502 $M = $topl2 - $bottoml2_min_1;
504 $maxabsx = $N + $bottoml1;
505 $maxabsy = $M + $bottoml2;
506 $limit = min( $this->maxDifferences, ceil( ( $N + $M ) / 2 ) );
511 $value_to_add_forward = 1;
513 $value_to_add_forward = 0;
517 $value_to_add_backward = 1;
519 $value_to_add_backward = 0;
522 $start_forward = -$M;
524 $start_backward = -$N;
527 $limit_min_1 = $limit - 1;
528 $limit_plus_1 = $limit + 1;
530 $V0[$limit_plus_1] = 0;
531 $V1[$limit_min_1] = $N;
532 $limit = min( $this->maxDifferences, ceil( ( $N + $M ) / 2 ) );
535 for ( $d = 0; $d <= $limit; ++$d ) {
536 $start_diag = max( $value_to_add_forward + $start_forward, -$d );
537 $end_diag = min( $end_forward, $d );
538 $value_to_add_forward = 1 - $value_to_add_forward;
541 for ( $k = $start_diag; $k <= $end_diag; $k += 2 ) {
542 if ( $k == -$d || ( $k < $d
543 && $V0[$limit_min_1 + $k] < $V0[$limit_plus_1 + $k] )
545 $x = $V0[$limit_plus_1 + $k];
547 $x = $V0[$limit_min_1 + $k] + 1;
550 $absx = $snake0 = $x + $bottoml1;
551 $absy = $snake1 = $x - $k + $bottoml2;
553 while ( $absx < $maxabsx && $absy < $maxabsy &&
$from[$absx] ===
$to[$absy] ) {
557 $x = $absx - $bottoml1;
559 $snake2 = $absx - $snake0;
560 $V0[$limit + $k] = $x;
561 if ( $k >= $delta - $d + 1 && $k <= $delta + $d - 1
562 && $x >= $V1[$limit + $k - $delta]
568 if ( $x >= $N && $end_forward > $k - 1 ) {
569 $end_forward = $k - 1;
570 } elseif ( $absy - $bottoml2 >= $M ) {
571 $start_forward = $k + 1;
572 $value_to_add_forward = 0;
576 $start_diag = max( $value_to_add_backward + $start_backward, -$d );
577 $end_diag = min( $end_backward, $d );
578 $value_to_add_backward = 1 - $value_to_add_backward;
581 for ( $k = $start_diag; $k <= $end_diag; $k += 2 ) {
583 || ( $k != -$d && $V1[$limit_min_1 + $k] < $V1[$limit_plus_1 + $k] )
585 $x = $V1[$limit_min_1 + $k];
587 $x = $V1[$limit_plus_1 + $k] - 1;
590 $y = $x - $k - $delta;
593 while ( $x > 0 && $y > 0
594 &&
$from[$x + $bottoml1_min_1] ===
$to[$y + $bottoml2_min_1]
600 $V1[$limit + $k] = $x;
604 $start_backward = $k + 1;
605 $value_to_add_backward = 0;
606 } elseif ( $y <= 0 && $end_backward > $k - 1 ) {
607 $end_backward = $k - 1;
612 for ( $d = 0; $d <= $limit; ++$d ) {
613 $start_diag = max( $value_to_add_forward + $start_forward, -$d );
614 $end_diag = min( $end_forward, $d );
615 $value_to_add_forward = 1 - $value_to_add_forward;
618 for ( $k = $start_diag; $k <= $end_diag; $k += 2 ) {
620 || ( $k < $d && $V0[$limit_min_1 + $k] < $V0[$limit_plus_1 + $k] )
622 $x = $V0[$limit_plus_1 + $k];
624 $x = $V0[$limit_min_1 + $k] + 1;
627 $absx = $snake0 = $x + $bottoml1;
628 $absy = $snake1 = $x - $k + $bottoml2;
630 while ( $absx < $maxabsx && $absy < $maxabsy &&
$from[$absx] ===
$to[$absy] ) {
634 $x = $absx - $bottoml1;
635 $snake2 = $absx - $snake0;
636 $V0[$limit + $k] = $x;
639 if ( $x >= $N && $end_forward > $k - 1 ) {
640 $end_forward = $k - 1;
641 } elseif ( $absy - $bottoml2 >= $M ) {
642 $start_forward = $k + 1;
643 $value_to_add_forward = 0;
647 $start_diag = max( $value_to_add_backward + $start_backward, -$d );
648 $end_diag = min( $end_backward, $d );
649 $value_to_add_backward = 1 - $value_to_add_backward;
652 for ( $k = $start_diag; $k <= $end_diag; $k += 2 ) {
654 || ( $k != -$d && $V1[$limit_min_1 + $k] < $V1[$limit_plus_1 + $k] )
656 $x = $V1[$limit_min_1 + $k];
658 $x = $V1[$limit_plus_1 + $k] - 1;
661 $y = $x - $k - $delta;
664 while ( $x > 0 && $y > 0
665 &&
$from[$x + $bottoml1_min_1] ===
$to[$y + $bottoml2_min_1]
671 $V1[$limit + $k] = $x;
673 if ( $k >= -$delta - $d && $k <= $d - $delta
674 && $x <= $V0[$limit + $k + $delta]
676 $snake0 = $bottoml1 + $x;
677 $snake1 = $bottoml2 + $y;
684 $start_backward = $k + 1;
685 $value_to_add_backward = 0;
686 } elseif ( $y <= 0 && $end_backward > $k - 1 ) {
687 $end_backward = $k - 1;
700 $snake0 = $bottoml1 + $most_progress[0];
701 $snake1 = $bottoml2 + $most_progress[1];
703 wfDebug(
"Computing the LCS is too expensive. Using a heuristic." );
704 $this->heuristicUsed =
true;
725 if ( ( $M & 1 ) == ( $limit & 1 ) ) {
726 $forward_start_diag = max( -$M, -$limit );
728 $forward_start_diag = max( 1 - $M, -$limit );
731 $forward_end_diag = min( $N, $limit );
733 if ( ( $N & 1 ) == ( $limit & 1 ) ) {
734 $backward_start_diag = max( -$N, -$limit );
736 $backward_start_diag = max( 1 - $N, -$limit );
739 $backward_end_diag = -min( $M, $limit );
743 $max_progress = array_fill( 0, ceil( max( $forward_end_diag - $forward_start_diag,
744 $backward_end_diag - $backward_start_diag ) / 2 ), $temp );
749 for ( $k = $forward_start_diag; $k <= $forward_end_diag; $k += 2 ) {
750 $x = $V[0][$limit + $k];
752 if ( $x > $N || $y > $M ) {
757 if ( $progress > $max_progress[0][2] ) {
759 $max_progress[0][0] = $x;
760 $max_progress[0][1] = $y;
761 $max_progress[0][2] = $progress;
762 } elseif ( $progress == $max_progress[0][2] ) {
764 $max_progress[$num_progress][0] = $x;
765 $max_progress[$num_progress][1] = $y;
766 $max_progress[$num_progress][2] = $progress;
770 $max_progress_forward =
true;
775 for ( $k = $backward_start_diag; $k <= $backward_end_diag; $k += 2 ) {
776 $x = $V[1][$limit + $k];
777 $y = $x - $k - $delta;
778 if ( $x < 0 || $y < 0 ) {
782 $progress = $N - $x + $M - $y;
783 if ( $progress > $max_progress[0][2] ) {
785 $max_progress_forward =
false;
786 $max_progress[0][0] = $x;
787 $max_progress[0][1] = $y;
788 $max_progress[0][2] = $progress;
789 } elseif ( $progress == $max_progress[0][2] && !$max_progress_forward ) {
791 $max_progress[$num_progress][0] = $x;
792 $max_progress[$num_progress][1] = $y;
793 $max_progress[$num_progress][2] = $progress;
798 return $max_progress[(int)floor( $num_progress / 2 )];