86 public function diff( $from_lines, $to_lines ) {
95 $n_from = count( $from_lines );
96 $n_to = count( $to_lines );
100 while ( $xi < $n_from || $yi < $n_to ) {
101 assert( $yi < $n_to || $this->removed[$xi] );
102 assert( $xi < $n_from || $this->added[$yi] );
106 while ( $xi < $n_from && $yi < $n_to
107 && !$this->removed[$xi] && !$this->added[$yi]
109 $copy[] = $from_lines[$xi++];
118 while ( $xi < $n_from && $this->removed[$xi] ) {
119 $delete[] = $from_lines[$xi++];
123 while ( $yi < $n_to && $this->added[$yi] ) {
124 $add[] = $to_lines[$yi++];
127 if ( $delete && $add ) {
129 } elseif ( $delete ) {
168 assert( count(
$lines ) == count( $changed ) );
170 $other_len = count( $other_changed );
184 while ( $j < $other_len && $other_changed[$j] ) {
188 while ( $i < $len && !$changed[$i] ) {
189 assert( $j < $other_len && !$other_changed[$j] );
192 while ( $j < $other_len && $other_changed[$j] ) {
204 while ( ++$i < $len && $changed[$i] ) {
213 $runlength = $i - $start;
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] ) {
227 while ( $other_changed[--$j] ) {
230 assert( $j >= 0 && !$other_changed[$j] );
238 $corresponding = $j < $other_len ? $i : $len;
248 $changed[$start++] =
false;
250 while ( $i < $len && $changed[$i] ) {
254 assert( $j < $other_len && !$other_changed[$j] );
256 if ( $j < $other_len && $other_changed[$j] ) {
258 while ( $j < $other_len && $other_changed[$j] ) {
263 }
while ( $runlength != $i - $start );
269 while ( $corresponding < $i ) {
270 $changed[--$start] = 1;
273 while ( $other_changed[--$j] ) {
276 assert( $j >= 0 && !$other_changed[$j] );
291 $this->heuristicUsed =
false;
295 $added =
$n > 0 ? array_fill( 0,
$n,
true ) : [];
308 while ( $i + $j <=
$m && $i + $j <=
$n &&
$from[
$m - $j] ===
$to[
$n - $j] ) {
314 $this->from = $newFromIndex = $this->to = $newToIndex = [];
318 foreach (
$from as $key ) {
319 $shared[$key] =
false;
322 foreach (
$to as $index => &$el ) {
323 if ( array_key_exists( $el, $shared ) ) {
327 $newToIndex[] = $index;
330 foreach (
$from as $index => &$el ) {
331 if ( $shared[$el] ) {
334 $newFromIndex[] = $index;
340 $this->m = count( $this->from );
341 $this->
n = count( $this->to );
343 if ( $this->bailoutComplexity > 0 && $this->m * $this->
n > $this->bailoutComplexity ) {
347 $this->removed = $this->m > 0 ? array_fill( 0, $this->m,
true ) : [];
348 $this->added = $this->
n > 0 ? array_fill( 0, $this->
n,
true ) : [];
350 if ( $this->m == 0 || $this->
n == 0 ) {
353 $this->maxDifferences = ceil( ( $this->m + $this->
n ) / 2.0 );
354 if ( $this->m * $this->
n > $this->tooLong ) {
356 $this->maxDifferences = floor( $this->maxDifferences ** ( $this->powLimit - 1.0 ) );
357 wfDebug(
"Limiting max number of differences to $this->maxDifferences\n" );
364 $max = min( $this->m, $this->
n );
365 for ( $forwardBound = 0; $forwardBound < $max
366 && $this->from[$forwardBound] === $this->to[$forwardBound];
369 $this->removed[$forwardBound] = $this->added[$forwardBound] =
false;
372 $backBoundL1 = $this->m - 1;
373 $backBoundL2 = $this->
n - 1;
375 while ( $backBoundL1 >= $forwardBound && $backBoundL2 >= $forwardBound
376 && $this->from[$backBoundL1] === $this->to[$backBoundL2]
378 $this->removed[$backBoundL1--] = $this->added[$backBoundL2--] =
false;
381 $temp = array_fill( 0, $this->m + $this->
n + 1, 0 );
382 $V = [ $temp, $temp ];
383 $snake = [ 0, 0, 0 ];
385 $this->length = $forwardBound + $this->m - $backBoundL1 - 1
399 $this->length += $i + $j - 1;
401 foreach ( $this->removed as $key => &$removed_elem ) {
402 if ( !$removed_elem ) {
403 $removed[$newFromIndex[$key]] =
false;
406 foreach ( $this->added as $key => &$added_elem ) {
407 if ( !$added_elem ) {
408 $added[$newToIndex[$key]] =
false;
450 private function lcs_rec( $bottoml1, $topl1, $bottoml2, $topl2, &$V, &$snake ) {
452 if ( $bottoml1 > $topl1 || $bottoml2 > $topl2 ) {
457 $topl2, $V, $snake );
461 list( $startx, $starty, $len ) = $snake;
464 for ( $i = 0; $i < $len; ++$i ) {
465 $this->removed[$startx + $i] = $this->added[$starty + $i] =
false;
470 + $this->
lcs_rec( $bottoml1, $startx - 1, $bottoml2,
471 $starty - 1, $V, $snake )
472 + $this->
lcs_rec( $startx + $len, $topl1, $starty + $len,
473 $topl2, $V, $snake );
474 } elseif ( $d == 1 ) {
480 $max = min( $startx - $bottoml1, $starty - $bottoml2 );
481 for ( $i = 0; $i < $max; ++$i ) {
482 $this->removed[$bottoml1 + $i] =
483 $this->added[$bottoml2 + $i] =
false;
497 $snake0 = &$snake[0];
498 $snake1 = &$snake[1];
499 $snake2 = &$snake[2];
500 $bottoml1_min_1 = $bottoml1 - 1;
501 $bottoml2_min_1 = $bottoml2 - 1;
502 $N = $topl1 - $bottoml1_min_1;
503 $M = $topl2 - $bottoml2_min_1;
505 $maxabsx = $N + $bottoml1;
506 $maxabsy = $M + $bottoml2;
507 $limit = min( $this->maxDifferences, ceil( ( $N + $M ) / 2 ) );
512 $value_to_add_forward = 1;
514 $value_to_add_forward = 0;
518 $value_to_add_backward = 1;
520 $value_to_add_backward = 0;
523 $start_forward = -$M;
525 $start_backward = -$N;
528 $limit_min_1 = $limit - 1;
529 $limit_plus_1 = $limit + 1;
531 $V0[$limit_plus_1] = 0;
532 $V1[$limit_min_1] = $N;
533 $limit = min( $this->maxDifferences, ceil( ( $N + $M ) / 2 ) );
536 for ( $d = 0; $d <= $limit; ++$d ) {
537 $start_diag = max( $value_to_add_forward + $start_forward, -$d );
538 $end_diag = min( $end_forward, $d );
539 $value_to_add_forward = 1 - $value_to_add_forward;
542 for ( $k = $start_diag; $k <= $end_diag; $k += 2 ) {
543 if ( $k == -$d || ( $k < $d
544 && $V0[$limit_min_1 + $k] < $V0[$limit_plus_1 + $k] )
546 $x = $V0[$limit_plus_1 + $k];
548 $x = $V0[$limit_min_1 + $k] + 1;
551 $absx = $snake0 = $x + $bottoml1;
552 $absy = $snake1 = $x - $k + $bottoml2;
554 while ( $absx < $maxabsx && $absy < $maxabsy &&
$from[$absx] ===
$to[$absy] ) {
558 $x = $absx - $bottoml1;
560 $snake2 = $absx - $snake0;
561 $V0[$limit + $k] = $x;
562 if ( $k >= $delta - $d + 1 && $k <= $delta + $d - 1
563 && $x >= $V1[$limit + $k - $delta]
569 if ( $x >= $N && $end_forward > $k - 1 ) {
570 $end_forward = $k - 1;
571 } elseif ( $absy - $bottoml2 >= $M ) {
572 $start_forward = $k + 1;
573 $value_to_add_forward = 0;
577 $start_diag = max( $value_to_add_backward + $start_backward, -$d );
578 $end_diag = min( $end_backward, $d );
579 $value_to_add_backward = 1 - $value_to_add_backward;
582 for ( $k = $start_diag; $k <= $end_diag; $k += 2 ) {
584 || ( $k != -$d && $V1[$limit_min_1 + $k] < $V1[$limit_plus_1 + $k] )
586 $x = $V1[$limit_min_1 + $k];
588 $x = $V1[$limit_plus_1 + $k] - 1;
591 $y = $x - $k - $delta;
594 while ( $x > 0 && $y > 0
595 &&
$from[$x + $bottoml1_min_1] ===
$to[$y + $bottoml2_min_1]
601 $V1[$limit + $k] = $x;
605 $start_backward = $k + 1;
606 $value_to_add_backward = 0;
607 } elseif ( $y <= 0 && $end_backward > $k - 1 ) {
608 $end_backward = $k - 1;
613 for ( $d = 0; $d <= $limit; ++$d ) {
614 $start_diag = max( $value_to_add_forward + $start_forward, -$d );
615 $end_diag = min( $end_forward, $d );
616 $value_to_add_forward = 1 - $value_to_add_forward;
619 for ( $k = $start_diag; $k <= $end_diag; $k += 2 ) {
621 || ( $k < $d && $V0[$limit_min_1 + $k] < $V0[$limit_plus_1 + $k] )
623 $x = $V0[$limit_plus_1 + $k];
625 $x = $V0[$limit_min_1 + $k] + 1;
628 $absx = $snake0 = $x + $bottoml1;
629 $absy = $snake1 = $x - $k + $bottoml2;
631 while ( $absx < $maxabsx && $absy < $maxabsy &&
$from[$absx] ===
$to[$absy] ) {
635 $x = $absx - $bottoml1;
636 $snake2 = $absx - $snake0;
637 $V0[$limit + $k] = $x;
640 if ( $x >= $N && $end_forward > $k - 1 ) {
641 $end_forward = $k - 1;
642 } elseif ( $absy - $bottoml2 >= $M ) {
643 $start_forward = $k + 1;
644 $value_to_add_forward = 0;
648 $start_diag = max( $value_to_add_backward + $start_backward, -$d );
649 $end_diag = min( $end_backward, $d );
650 $value_to_add_backward = 1 - $value_to_add_backward;
653 for ( $k = $start_diag; $k <= $end_diag; $k += 2 ) {
655 || ( $k != -$d && $V1[$limit_min_1 + $k] < $V1[$limit_plus_1 + $k] )
657 $x = $V1[$limit_min_1 + $k];
659 $x = $V1[$limit_plus_1 + $k] - 1;
662 $y = $x - $k - $delta;
665 while ( $x > 0 && $y > 0
666 &&
$from[$x + $bottoml1_min_1] ===
$to[$y + $bottoml2_min_1]
672 $V1[$limit + $k] = $x;
674 if ( $k >= -$delta - $d && $k <= $d - $delta
675 && $x <= $V0[$limit + $k + $delta]
677 $snake0 = $bottoml1 + $x;
678 $snake1 = $bottoml2 + $y;
685 $start_backward = $k + 1;
686 $value_to_add_backward = 0;
687 } elseif ( $y <= 0 && $end_backward > $k - 1 ) {
688 $end_backward = $k - 1;
701 $snake0 = $bottoml1 + $most_progress[0];
702 $snake1 = $bottoml2 + $most_progress[1];
704 wfDebug(
"Computing the LCS is too expensive. Using a heuristic.\n" );
705 $this->heuristicUsed =
true;
719 if ( ( $M & 1 ) == ( $limit & 1 ) ) {
720 $forward_start_diag = max( -$M, -$limit );
722 $forward_start_diag = max( 1 - $M, -$limit );
725 $forward_end_diag = min( $N, $limit );
727 if ( ( $N & 1 ) == ( $limit & 1 ) ) {
728 $backward_start_diag = max( -$N, -$limit );
730 $backward_start_diag = max( 1 - $N, -$limit );
733 $backward_end_diag = -min( $M, $limit );
737 $max_progress = array_fill( 0, ceil( max( $forward_end_diag - $forward_start_diag,
738 $backward_end_diag - $backward_start_diag ) / 2 ), $temp );
743 for ( $k = $forward_start_diag; $k <= $forward_end_diag; $k += 2 ) {
744 $x = $V[0][$limit + $k];
746 if ( $x > $N || $y > $M ) {
751 if ( $progress > $max_progress[0][2] ) {
753 $max_progress[0][0] = $x;
754 $max_progress[0][1] = $y;
755 $max_progress[0][2] = $progress;
756 } elseif ( $progress == $max_progress[0][2] ) {
758 $max_progress[$num_progress][0] = $x;
759 $max_progress[$num_progress][1] = $y;
760 $max_progress[$num_progress][2] = $progress;
764 $max_progress_forward =
true;
769 for ( $k = $backward_start_diag; $k <= $backward_end_diag; $k += 2 ) {
770 $x = $V[1][$limit + $k];
771 $y = $x - $k - $delta;
772 if ( $x < 0 || $y < 0 ) {
776 $progress = $N - $x + $M - $y;
777 if ( $progress > $max_progress[0][2] ) {
779 $max_progress_forward =
false;
780 $max_progress[0][0] = $x;
781 $max_progress[0][1] = $y;
782 $max_progress[0][2] = $progress;
783 } elseif ( $progress == $max_progress[0][2] && !$max_progress_forward ) {
785 $max_progress[$num_progress][0] = $x;
786 $max_progress[$num_progress][1] = $y;
787 $max_progress[$num_progress][2] = $progress;
792 return $max_progress[(int)floor( $num_progress / 2 )];