56 private $maxDifferences;
58 private $lcsLengthCorrectedForHeuristic =
false;
74 public function __construct( $tooLong = 2_000_000, $powLimit = 1.45 ) {
75 $this->tooLong = $tooLong;
76 $this->powLimit = $powLimit;
88 public function diff( $from_lines, $to_lines ) {
93 $this->shiftBoundaries( $from_lines, $this->removed, $this->added );
94 $this->shiftBoundaries( $to_lines, $this->added, $this->removed );
97 $n_from = count( $from_lines );
98 $n_to = count( $to_lines );
102 while ( $xi < $n_from || $yi < $n_to ) {
103 assert( $yi < $n_to || $this->removed[$xi] );
104 assert( $xi < $n_from || $this->added[$yi] );
108 while ( $xi < $n_from && $yi < $n_to
109 && !$this->removed[$xi] && !$this->added[$yi]
111 $copy[] = $from_lines[$xi++];
120 while ( $xi < $n_from && $this->removed[$xi] ) {
121 $delete[] = $from_lines[$xi++];
125 while ( $yi < $n_to && $this->added[$yi] ) {
126 $add[] = $to_lines[$yi++];
129 if ( $delete && $add ) {
131 } elseif ( $delete ) {
146 $this->bailoutComplexity = $value;
166 private function shiftBoundaries( array $lines, array &$changed, array $other_changed ) {
170 assert( count( $lines ) == count( $changed ) );
171 $len = count( $lines );
172 $other_len = count( $other_changed );
186 while ( $j < $other_len && $other_changed[$j] ) {
190 while ( $i < $len && !$changed[$i] ) {
191 assert( $j < $other_len && !$other_changed[$j] );
194 while ( $j < $other_len && $other_changed[$j] ) {
206 while ( ++$i < $len && $changed[$i] ) {
215 $runlength = $i - $start;
222 while ( $start > 0 && $lines[$start - 1] == $lines[$i - 1] ) {
223 $changed[--$start] = 1;
224 $changed[--$i] =
false;
226 while ( $start > 0 && $changed[$start - 1] ) {
230 while ( $other_changed[--$j] ) {
233 assert( $j >= 0 && !$other_changed[$j] );
241 $corresponding = $j < $other_len ? $i : $len;
250 while ( $i < $len && $lines[$start] == $lines[$i] ) {
251 $changed[$start++] =
false;
253 while ( $i < $len && $changed[$i] ) {
257 assert( $j < $other_len && !$other_changed[$j] );
259 if ( $j < $other_len && $other_changed[$j] ) {
261 while ( $j < $other_len && $other_changed[$j] ) {
266 }
while ( $runlength != $i - $start );
272 while ( $corresponding < $i ) {
273 $changed[--$start] = 1;
276 while ( $other_changed[--$j] ) {
279 assert( $j >= 0 && !$other_changed[$j] );
294 $this->heuristicUsed =
false;
297 $removed = $m > 0 ? array_fill( 0, $m,
true ) : [];
298 $added = $n > 0 ? array_fill( 0, $n,
true ) : [];
303 while ( $i < $m && $i < $n && $from[$i] === $to[$i] ) {
305 unset( $from[$i], $to[$i] );
311 while ( $i + $j <= $m && $i + $j <= $n && $from[$m - $j] === $to[$n - $j] ) {
313 unset( $from[$m - $j], $to[$n - $j] );
317 $this->from = $newFromIndex = $this->to = $newToIndex = [];
321 foreach ( $from as $key ) {
322 $shared[$key] =
false;
325 foreach ( $to as $index => &$el ) {
326 if ( array_key_exists( $el, $shared ) ) {
330 $newToIndex[] = $index;
333 foreach ( $from as $index => &$el ) {
334 if ( $shared[$el] ) {
337 $newFromIndex[] = $index;
341 unset( $shared, $from, $to );
343 $this->m = count( $this->from );
344 $this->n = count( $this->to );
346 if ( $this->bailoutComplexity > 0 && $this->m * $this->n > $this->bailoutComplexity ) {
350 $this->removed = $this->m > 0 ? array_fill( 0, $this->m,
true ) : [];
351 $this->added = $this->n > 0 ? array_fill( 0, $this->n,
true ) : [];
353 if ( $this->m == 0 || $this->n == 0 ) {
356 $this->maxDifferences = ceil( ( $this->m + $this->n ) / 2.0 );
357 if ( $this->m * $this->n > $this->tooLong ) {
359 $this->maxDifferences = floor( $this->maxDifferences ** ( $this->powLimit - 1.0 ) );
366 $max = min( $this->m, $this->n );
367 for ( $forwardBound = 0; $forwardBound < $max
368 && $this->from[$forwardBound] === $this->to[$forwardBound];
371 $this->removed[$forwardBound] = $this->added[$forwardBound] =
false;
374 $backBoundL1 = $this->m - 1;
375 $backBoundL2 = $this->n - 1;
377 while ( $backBoundL1 >= $forwardBound && $backBoundL2 >= $forwardBound
378 && $this->from[$backBoundL1] === $this->to[$backBoundL2]
380 $this->removed[$backBoundL1--] = $this->added[$backBoundL2--] =
false;
383 $temp = array_fill( 0, $this->m + $this->n + 1, 0 );
384 $V = [ $temp, $temp ];
385 $snake = [ 0, 0, 0 ];
387 $this->length = $forwardBound + $this->m - $backBoundL1 - 1
401 $this->length += $i + $j - 1;
403 foreach ( $this->removed as $key => &$removed_elem ) {
404 if ( !$removed_elem ) {
405 $removed[$newFromIndex[$key]] =
false;
408 foreach ( $this->added as $key => &$added_elem ) {
409 if ( !$added_elem ) {
410 $added[$newToIndex[$key]] =
false;
426 private function lcs_rec( $bottoml1, $topl1, $bottoml2, $topl2, &$V, &$snake ) {
428 if ( $bottoml1 > $topl1 || $bottoml2 > $topl2 ) {
432 $d = $this->find_middle_snake( $bottoml1, $topl1, $bottoml2,
433 $topl2, $V, $snake );
437 [ $startx, $starty, $len ] = $snake;
440 for ( $i = 0; $i < $len; ++$i ) {
441 $this->removed[$startx + $i] = $this->added[$starty + $i] =
false;
446 + $this->lcs_rec( $bottoml1, $startx - 1, $bottoml2,
447 $starty - 1, $V, $snake )
448 + $this->lcs_rec( $startx + $len, $topl1, $starty + $len,
449 $topl2, $V, $snake );
450 } elseif ( $d == 1 ) {
456 $max = min( $startx - $bottoml1, $starty - $bottoml2 );
457 for ( $i = 0; $i < $max; ++$i ) {
458 $this->removed[$bottoml1 + $i] =
459 $this->added[$bottoml2 + $i] =
false;
477 private function find_middle_snake( $bottoml1, $topl1, $bottoml2, $topl2, &$V, &$snake ) {
478 $from = &$this->from;
482 $snake0 = &$snake[0];
483 $snake1 = &$snake[1];
484 $snake2 = &$snake[2];
485 $bottoml1_min_1 = $bottoml1 - 1;
486 $bottoml2_min_1 = $bottoml2 - 1;
487 $N = $topl1 - $bottoml1_min_1;
488 $M = $topl2 - $bottoml2_min_1;
490 $maxabsx = $N + $bottoml1;
491 $maxabsy = $M + $bottoml2;
492 $limit = min( $this->maxDifferences, ceil( ( $N + $M ) / 2 ) );
497 $value_to_add_forward = 1;
499 $value_to_add_forward = 0;
503 $value_to_add_backward = 1;
505 $value_to_add_backward = 0;
508 $start_forward = -$M;
510 $start_backward = -$N;
513 $limit_min_1 = $limit - 1;
514 $limit_plus_1 = $limit + 1;
516 $V0[$limit_plus_1] = 0;
517 $V1[$limit_min_1] = $N;
518 $limit = min( $this->maxDifferences, ceil( ( $N + $M ) / 2 ) );
521 for ( $d = 0; $d <= $limit; ++$d ) {
522 $start_diag = max( $value_to_add_forward + $start_forward, -$d );
523 $end_diag = min( $end_forward, $d );
524 $value_to_add_forward = 1 - $value_to_add_forward;
527 for ( $k = $start_diag; $k <= $end_diag; $k += 2 ) {
528 if ( $k == -$d || ( $k < $d
529 && $V0[$limit_min_1 + $k] < $V0[$limit_plus_1 + $k] )
531 $x = $V0[$limit_plus_1 + $k];
533 $x = $V0[$limit_min_1 + $k] + 1;
536 $absx = $snake0 = $x + $bottoml1;
537 $absy = $snake1 = $x - $k + $bottoml2;
539 while ( $absx < $maxabsx && $absy < $maxabsy && $from[$absx] === $to[$absy] ) {
543 $x = $absx - $bottoml1;
545 $snake2 = $absx - $snake0;
546 $V0[$limit + $k] = $x;
547 if ( $k >= $delta - $d + 1 && $k <= $delta + $d - 1
548 && $x >= $V1[$limit + $k - $delta]
554 if ( $x >= $N && $end_forward > $k - 1 ) {
555 $end_forward = $k - 1;
556 } elseif ( $absy - $bottoml2 >= $M ) {
557 $start_forward = $k + 1;
558 $value_to_add_forward = 0;
562 $start_diag = max( $value_to_add_backward + $start_backward, -$d );
563 $end_diag = min( $end_backward, $d );
564 $value_to_add_backward = 1 - $value_to_add_backward;
567 for ( $k = $start_diag; $k <= $end_diag; $k += 2 ) {
569 || ( $k != -$d && $V1[$limit_min_1 + $k] < $V1[$limit_plus_1 + $k] )
571 $x = $V1[$limit_min_1 + $k];
573 $x = $V1[$limit_plus_1 + $k] - 1;
576 $y = $x - $k - $delta;
579 while ( $x > 0 && $y > 0
580 && $from[$x + $bottoml1_min_1] === $to[$y + $bottoml2_min_1]
586 $V1[$limit + $k] = $x;
590 $start_backward = $k + 1;
591 $value_to_add_backward = 0;
592 } elseif ( $y <= 0 && $end_backward > $k - 1 ) {
593 $end_backward = $k - 1;
598 for ( $d = 0; $d <= $limit; ++$d ) {
599 $start_diag = max( $value_to_add_forward + $start_forward, -$d );
600 $end_diag = min( $end_forward, $d );
601 $value_to_add_forward = 1 - $value_to_add_forward;
604 for ( $k = $start_diag; $k <= $end_diag; $k += 2 ) {
606 || ( $k < $d && $V0[$limit_min_1 + $k] < $V0[$limit_plus_1 + $k] )
608 $x = $V0[$limit_plus_1 + $k];
610 $x = $V0[$limit_min_1 + $k] + 1;
613 $absx = $snake0 = $x + $bottoml1;
614 $absy = $snake1 = $x - $k + $bottoml2;
616 while ( $absx < $maxabsx && $absy < $maxabsy && $from[$absx] === $to[$absy] ) {
620 $x = $absx - $bottoml1;
621 $snake2 = $absx - $snake0;
622 $V0[$limit + $k] = $x;
625 if ( $x >= $N && $end_forward > $k - 1 ) {
626 $end_forward = $k - 1;
627 } elseif ( $absy - $bottoml2 >= $M ) {
628 $start_forward = $k + 1;
629 $value_to_add_forward = 0;
633 $start_diag = max( $value_to_add_backward + $start_backward, -$d );
634 $end_diag = min( $end_backward, $d );
635 $value_to_add_backward = 1 - $value_to_add_backward;
638 for ( $k = $start_diag; $k <= $end_diag; $k += 2 ) {
640 || ( $k != -$d && $V1[$limit_min_1 + $k] < $V1[$limit_plus_1 + $k] )
642 $x = $V1[$limit_min_1 + $k];
644 $x = $V1[$limit_plus_1 + $k] - 1;
647 $y = $x - $k - $delta;
650 while ( $x > 0 && $y > 0
651 && $from[$x + $bottoml1_min_1] === $to[$y + $bottoml2_min_1]
657 $V1[$limit + $k] = $x;
659 if ( $k >= -$delta - $d && $k <= $d - $delta
660 && $x <= $V0[$limit + $k + $delta]
662 $snake0 = $bottoml1 + $x;
663 $snake1 = $bottoml2 + $y;
670 $start_backward = $k + 1;
671 $value_to_add_backward = 0;
672 } elseif ( $y <= 0 && $end_backward > $k - 1 ) {
673 $end_backward = $k - 1;
684 $most_progress = self::findMostProgress( $M, $N, $limit, $V );
686 $snake0 = $bottoml1 + $most_progress[0];
687 $snake1 = $bottoml2 + $most_progress[1];
690 $this->heuristicUsed =
true;
708 private static function findMostProgress( $M, $N, $limit, $V ) {
711 if ( ( $M & 1 ) == ( $limit & 1 ) ) {
712 $forward_start_diag = max( -$M, -$limit );
714 $forward_start_diag = max( 1 - $M, -$limit );
717 $forward_end_diag = min( $N, $limit );
719 if ( ( $N & 1 ) == ( $limit & 1 ) ) {
720 $backward_start_diag = max( -$N, -$limit );
722 $backward_start_diag = max( 1 - $N, -$limit );
725 $backward_end_diag = -min( $M, $limit );
729 $max_progress = array_fill( 0, ceil( max( $forward_end_diag - $forward_start_diag,
730 $backward_end_diag - $backward_start_diag ) / 2 ), $temp );
735 for ( $k = $forward_start_diag; $k <= $forward_end_diag; $k += 2 ) {
736 $x = $V[0][$limit + $k];
738 if ( $x > $N || $y > $M ) {
743 if ( $progress > $max_progress[0][2] ) {
745 $max_progress[0][0] = $x;
746 $max_progress[0][1] = $y;
747 $max_progress[0][2] = $progress;
748 } elseif ( $progress == $max_progress[0][2] ) {
750 $max_progress[$num_progress][0] = $x;
751 $max_progress[$num_progress][1] = $y;
752 $max_progress[$num_progress][2] = $progress;
756 $max_progress_forward =
true;
761 for ( $k = $backward_start_diag; $k <= $backward_end_diag; $k += 2 ) {
762 $x = $V[1][$limit + $k];
763 $y = $x - $k - $delta;
764 if ( $x < 0 || $y < 0 ) {
768 $progress = $N - $x + $M - $y;
769 if ( $progress > $max_progress[0][2] ) {
771 $max_progress_forward =
false;
772 $max_progress[0][0] = $x;
773 $max_progress[0][1] = $y;
774 $max_progress[0][2] = $progress;
775 } elseif ( $progress == $max_progress[0][2] && !$max_progress_forward ) {
777 $max_progress[$num_progress][0] = $x;
778 $max_progress[$num_progress][1] = $y;
779 $max_progress[$num_progress][2] = $progress;
784 return $max_progress[(int)floor( $num_progress / 2 )];
791 if ( $this->heuristicUsed && !$this->lcsLengthCorrectedForHeuristic ) {
792 $this->lcsLengthCorrectedForHeuristic =
true;
793 $this->length = $this->m - array_sum( $this->added );