81 public function diff( $from_lines, $to_lines ) {
90 $n_from = count( $from_lines );
91 $n_to = count( $to_lines );
95 while ( $xi < $n_from || $yi < $n_to ) {
96 assert( $yi < $n_to || $this->removed[$xi] );
97 assert( $xi < $n_from || $this->added[$yi] );
101 while ( $xi < $n_from && $yi < $n_to
102 && !$this->removed[$xi] && !$this->added[$yi]
104 $copy[] = $from_lines[$xi++];
113 while ( $xi < $n_from && $this->removed[$xi] ) {
114 $delete[] = $from_lines[$xi++];
118 while ( $yi < $n_to && $this->added[$yi] ) {
119 $add[] = $to_lines[$yi++];
122 if ( $delete && $add ) {
124 } elseif ( $delete ) {
163 assert( count(
$lines ) == count( $changed ) );
165 $other_len = count( $other_changed );
179 while ( $j < $other_len && $other_changed[$j] ) {
183 while ( $i < $len && !$changed[$i] ) {
184 assert( $j < $other_len && !$other_changed[$j] );
187 while ( $j < $other_len && $other_changed[$j] ) {
199 while ( ++$i < $len && $changed[$i] ) {
208 $runlength = $i - $start;
215 while ( $start > 0 &&
$lines[$start - 1] ==
$lines[$i - 1] ) {
216 $changed[--$start] = 1;
217 $changed[--$i] =
false;
218 while ( $start > 0 && $changed[$start - 1] ) {
222 while ( $other_changed[--$j] ) {
225 assert( $j >= 0 && !$other_changed[$j] );
233 $corresponding = $j < $other_len ? $i : $len;
243 $changed[$start++] =
false;
245 while ( $i < $len && $changed[$i] ) {
249 assert( $j < $other_len && !$other_changed[$j] );
251 if ( $j < $other_len && $other_changed[$j] ) {
253 while ( $j < $other_len && $other_changed[$j] ) {
258 }
while ( $runlength != $i - $start );
264 while ( $corresponding < $i ) {
265 $changed[--$start] = 1;
268 while ( $other_changed[--$j] ) {
271 assert( $j >= 0 && !$other_changed[$j] );
286 $this->heuristicUsed =
false;
290 $added =
$n > 0 ? array_fill( 0,
$n,
true ) : [];
303 while ( $i + $j <=
$m && $i + $j <=
$n &&
$from[
$m - $j] ===
$to[
$n - $j] ) {
309 $this->from = $newFromIndex = $this->to = $newToIndex = [];
313 foreach (
$from as $key ) {
314 $shared[$key] =
false;
317 foreach (
$to as $index => &$el ) {
318 if ( array_key_exists( $el, $shared ) ) {
322 $newToIndex[] = $index;
325 foreach (
$from as $index => &$el ) {
326 if ( $shared[$el] ) {
329 $newFromIndex[] = $index;
335 $this->m = count( $this->from );
336 $this->n = count( $this->to );
338 if ( $this->bailoutComplexity > 0 && $this->m * $this->n > $this->bailoutComplexity ) {
342 $this->removed = $this->m > 0 ? array_fill( 0, $this->m,
true ) : [];
343 $this->added = $this->n > 0 ? array_fill( 0, $this->n,
true ) : [];
345 if ( $this->m == 0 || $this->n == 0 ) {
348 $this->maxDifferences = ceil( ( $this->m + $this->n ) / 2.0 );
349 if ( $this->m * $this->n > $this->tooLong ) {
351 $this->maxDifferences = floor( pow( $this->maxDifferences, $this->powLimit - 1.0 ) );
352 wfDebug(
"Limiting max number of differences to $this->maxDifferences\n" );
359 $max = min( $this->m, $this->n );
360 for ( $forwardBound = 0; $forwardBound < $max
361 && $this->from[$forwardBound] === $this->to[$forwardBound];
364 $this->removed[$forwardBound] = $this->added[$forwardBound] =
false;
367 $backBoundL1 = $this->m - 1;
368 $backBoundL2 = $this->n - 1;
370 while ( $backBoundL1 >= $forwardBound && $backBoundL2 >= $forwardBound
371 && $this->from[$backBoundL1] === $this->to[$backBoundL2]
373 $this->removed[$backBoundL1--] = $this->added[$backBoundL2--] =
false;
376 $temp = array_fill( 0, $this->m + $this->n + 1, 0 );
377 $V = [ $temp, $temp ];
378 $snake = [ 0, 0, 0 ];
380 $this->length = $forwardBound + $this->m - $backBoundL1 - 1
394 $this->length += $i + $j - 1;
396 foreach ( $this->removed as $key => &$removed_elem ) {
397 if ( !$removed_elem ) {
398 $removed[$newFromIndex[$key]] =
false;
401 foreach ( $this->added as $key => &$added_elem ) {
402 if ( !$added_elem ) {
403 $added[$newToIndex[$key]] =
false;
445 private function lcs_rec( $bottoml1, $topl1, $bottoml2, $topl2, &$V, &$snake ) {
447 if ( $bottoml1 > $topl1 || $bottoml2 > $topl2 ) {
452 $topl2, $V, $snake );
461 for ( $i = 0; $i < $len; ++$i ) {
462 $this->removed[$startx + $i] = $this->added[$starty + $i] =
false;
467 + $this->
lcs_rec( $bottoml1, $startx - 1, $bottoml2,
468 $starty - 1, $V, $snake )
469 + $this->
lcs_rec( $startx + $len, $topl1, $starty + $len,
470 $topl2, $V, $snake );
471 } elseif ( $d == 1 ) {
477 $max = min( $startx - $bottoml1, $starty - $bottoml2 );
478 for ( $i = 0; $i < $max; ++$i ) {
479 $this->removed[$bottoml1 + $i] =
480 $this->added[$bottoml2 + $i] =
false;
494 $snake0 = &$snake[0];
495 $snake1 = &$snake[1];
496 $snake2 = &$snake[2];
497 $bottoml1_min_1 = $bottoml1 - 1;
498 $bottoml2_min_1 = $bottoml2 - 1;
499 $N = $topl1 - $bottoml1_min_1;
500 $M = $topl2 - $bottoml2_min_1;
502 $maxabsx = $N + $bottoml1;
503 $maxabsy = $M + $bottoml2;
504 $limit = min( $this->maxDifferences, ceil( ( $N + $M ) / 2 ) );
508 if ( ( $M & 1 ) == 1 ) {
509 $value_to_add_forward = 1;
511 $value_to_add_forward = 0;
514 if ( ( $N & 1 ) == 1 ) {
515 $value_to_add_backward = 1;
517 $value_to_add_backward = 0;
520 $start_forward = -$M;
522 $start_backward = -$N;
525 $limit_min_1 = $limit - 1;
526 $limit_plus_1 = $limit + 1;
528 $V0[$limit_plus_1] = 0;
529 $V1[$limit_min_1] = $N;
530 $limit = min( $this->maxDifferences, ceil( ( $N + $M ) / 2 ) );
532 if ( ( $delta & 1 ) == 1 ) {
533 for ( $d = 0; $d <= $limit; ++$d ) {
534 $start_diag = max( $value_to_add_forward + $start_forward, -$d );
535 $end_diag = min( $end_forward, $d );
536 $value_to_add_forward = 1 - $value_to_add_forward;
539 for ( $k = $start_diag; $k <= $end_diag; $k += 2 ) {
540 if ( $k == -$d || ( $k < $d
541 && $V0[$limit_min_1 + $k] < $V0[$limit_plus_1 + $k] )
543 $x = $V0[$limit_plus_1 + $k];
545 $x = $V0[$limit_min_1 + $k] + 1;
548 $absx = $snake0 = $x + $bottoml1;
549 $absy = $snake1 = $x - $k + $bottoml2;
551 while ( $absx < $maxabsx && $absy < $maxabsy &&
$from[$absx] ===
$to[$absy] ) {
555 $x = $absx - $bottoml1;
557 $snake2 = $absx - $snake0;
558 $V0[$limit + $k] = $x;
559 if ( $k >= $delta - $d + 1 && $k <= $delta + $d - 1
560 && $x >= $V1[$limit + $k - $delta]
566 if ( $x >= $N && $end_forward > $k - 1 ) {
567 $end_forward = $k - 1;
568 } elseif ( $absy - $bottoml2 >= $M ) {
569 $start_forward = $k + 1;
570 $value_to_add_forward = 0;
574 $start_diag = max( $value_to_add_backward + $start_backward, -$d );
575 $end_diag = min( $end_backward, $d );
576 $value_to_add_backward = 1 - $value_to_add_backward;
579 for ( $k = $start_diag; $k <= $end_diag; $k += 2 ) {
581 || ( $k != -$d && $V1[$limit_min_1 + $k] < $V1[$limit_plus_1 + $k] )
583 $x = $V1[$limit_min_1 + $k];
585 $x = $V1[$limit_plus_1 + $k] - 1;
588 $y = $x - $k - $delta;
591 while ( $x > 0 && $y > 0
592 &&
$from[$x + $bottoml1_min_1] ===
$to[$y + $bottoml2_min_1]
598 $V1[$limit + $k] = $x;
602 $start_backward = $k + 1;
603 $value_to_add_backward = 0;
604 } elseif ( $y <= 0 && $end_backward > $k - 1 ) {
605 $end_backward = $k - 1;
610 for ( $d = 0; $d <= $limit; ++$d ) {
611 $start_diag = max( $value_to_add_forward + $start_forward, -$d );
612 $end_diag = min( $end_forward, $d );
613 $value_to_add_forward = 1 - $value_to_add_forward;
616 for ( $k = $start_diag; $k <= $end_diag; $k += 2 ) {
618 || ( $k < $d && $V0[$limit_min_1 + $k] < $V0[$limit_plus_1 + $k] )
620 $x = $V0[$limit_plus_1 + $k];
622 $x = $V0[$limit_min_1 + $k] + 1;
625 $absx = $snake0 = $x + $bottoml1;
626 $absy = $snake1 = $x - $k + $bottoml2;
628 while ( $absx < $maxabsx && $absy < $maxabsy &&
$from[$absx] ===
$to[$absy] ) {
632 $x = $absx - $bottoml1;
633 $snake2 = $absx - $snake0;
634 $V0[$limit + $k] = $x;
637 if ( $x >= $N && $end_forward > $k - 1 ) {
638 $end_forward = $k - 1;
639 } elseif ( $absy - $bottoml2 >= $M ) {
640 $start_forward = $k + 1;
641 $value_to_add_forward = 0;
645 $start_diag = max( $value_to_add_backward + $start_backward, -$d );
646 $end_diag = min( $end_backward, $d );
647 $value_to_add_backward = 1 - $value_to_add_backward;
650 for ( $k = $start_diag; $k <= $end_diag; $k += 2 ) {
652 || ( $k != -$d && $V1[$limit_min_1 + $k] < $V1[$limit_plus_1 + $k] )
654 $x = $V1[$limit_min_1 + $k];
656 $x = $V1[$limit_plus_1 + $k] - 1;
659 $y = $x - $k - $delta;
662 while ( $x > 0 && $y > 0
663 &&
$from[$x + $bottoml1_min_1] ===
$to[$y + $bottoml2_min_1]
669 $V1[$limit + $k] = $x;
671 if ( $k >= -$delta - $d && $k <= $d - $delta
672 && $x <= $V0[$limit + $k + $delta]
674 $snake0 = $bottoml1 + $x;
675 $snake1 = $bottoml2 + $y;
682 $start_backward = $k + 1;
683 $value_to_add_backward = 0;
684 } elseif ( $y <= 0 && $end_backward > $k - 1 ) {
685 $end_backward = $k - 1;
698 $snake0 = $bottoml1 + $most_progress[0];
699 $snake1 = $bottoml2 + $most_progress[1];
701 wfDebug(
"Computing the LCS is too expensive. Using a heuristic.\n" );
702 $this->heuristicUsed =
true;
716 if ( ( $M & 1 ) == ( $limit & 1 ) ) {
717 $forward_start_diag = max( -$M, -$limit );
719 $forward_start_diag = max( 1 - $M, -$limit );
722 $forward_end_diag = min( $N, $limit );
724 if ( ( $N & 1 ) == ( $limit & 1 ) ) {
725 $backward_start_diag = max( -$N, -$limit );
727 $backward_start_diag = max( 1 - $N, -$limit );
730 $backward_end_diag = -min( $M, $limit );
734 $max_progress = array_fill( 0, ceil( max( $forward_end_diag - $forward_start_diag,
735 $backward_end_diag - $backward_start_diag ) / 2 ), $temp );
740 for ( $k = $forward_start_diag; $k <= $forward_end_diag; $k += 2 ) {
741 $x = $V[0][$limit + $k];
743 if ( $x > $N || $y > $M ) {
748 if ( $progress > $max_progress[0][2] ) {
750 $max_progress[0][0] = $x;
751 $max_progress[0][1] = $y;
752 $max_progress[0][2] = $progress;
753 } elseif ( $progress == $max_progress[0][2] ) {
755 $max_progress[$num_progress][0] = $x;
756 $max_progress[$num_progress][1] = $y;
757 $max_progress[$num_progress][2] = $progress;
761 $max_progress_forward =
true;
766 for ( $k = $backward_start_diag; $k <= $backward_end_diag; $k += 2 ) {
767 $x = $V[1][$limit + $k];
768 $y = $x - $k - $delta;
769 if ( $x < 0 || $y < 0 ) {
773 $progress = $N - $x + $M - $y;
774 if ( $progress > $max_progress[0][2] ) {
776 $max_progress_forward =
false;
777 $max_progress[0][0] = $x;
778 $max_progress[0][1] = $y;
779 $max_progress[0][2] = $progress;
780 } elseif ( $progress == $max_progress[0][2] && !$max_progress_forward ) {
782 $max_progress[$num_progress][0] = $x;
783 $max_progress[$num_progress][1] = $y;
784 $max_progress[$num_progress][2] = $progress;
789 return $max_progress[(int)floor( $num_progress / 2 )];