| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
> |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
namespace Wikimedia\Diff; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
class DiffEngine { |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
private $from; |
| 53 |
|
| 54 |
private $to; |
| 55 |
|
| 56 |
private $m; |
| 57 |
|
| 58 |
private $n; |
| 59 |
|
| 60 |
|
| 61 |
private $tooLong; |
| 62 |
|
| 63 |
private $powLimit; |
| 64 |
|
| 65 |
|
| 66 |
protected $bailoutComplexity = 0; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
private $maxDifferences; |
| 71 |
|
| 72 |
private $lcsLengthCorrectedForHeuristic = false; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public $length; |
| 77 |
|
| 78 |
public $removed; |
| 79 |
|
| 80 |
public $added; |
| 81 |
|
| 82 |
public $heuristicUsed; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public function __construct( $tooLong = 2_000_000, $powLimit = 1.45 ) { |
| 89 |
$this->tooLong = $tooLong; |
| 90 |
$this->powLimit = $powLimit; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public function diff( $from_lines, $to_lines ) { |
| 103 |
|
| 104 |
$this->diffInternal( $from_lines, $to_lines ); |
| 105 |
|
| 106 |
|
| 107 |
$this->shiftBoundaries( $from_lines, $this->removed, $this->added ); |
| 108 |
$this->shiftBoundaries( $to_lines, $this->added, $this->removed ); |
| 109 |
|
| 110 |
|
| 111 |
$n_from = count( $from_lines ); |
| 112 |
$n_to = count( $to_lines ); |
| 113 |
|
| 114 |
$edits = []; |
| 115 |
$xi = $yi = 0; |
| 116 |
while ( $xi < $n_from || $yi < $n_to ) { |
| 117 |
assert( $yi < $n_to || $this->removed[$xi] ); |
| 118 |
assert( $xi < $n_from || $this->added[$yi] ); |
| 119 |
|
| 120 |
|
| 121 |
$copy = []; |
| 122 |
while ( $xi < $n_from && $yi < $n_to |
| 123 |
&& !$this->removed[$xi] && !$this->added[$yi] |
| 124 |
) { |
| 125 |
$copy[] = $from_lines[$xi++]; |
| 126 |
++$yi; |
| 127 |
} |
| 128 |
if ( $copy ) { |
| 129 |
$edits[] = new DiffOpCopy( $copy ); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
$delete = []; |
| 134 |
while ( $xi < $n_from && $this->removed[$xi] ) { |
| 135 |
$delete[] = $from_lines[$xi++]; |
| 136 |
} |
| 137 |
|
| 138 |
$add = []; |
| 139 |
while ( $yi < $n_to && $this->added[$yi] ) { |
| 140 |
$add[] = $to_lines[$yi++]; |
| 141 |
} |
| 142 |
|
| 143 |
if ( $delete && $add ) { |
| 144 |
$edits[] = new DiffOpChange( $delete, $add ); |
| 145 |
} elseif ( $delete ) { |
| 146 |
$edits[] = new DiffOpDelete( $delete ); |
| 147 |
} elseif ( $add ) { |
| 148 |
$edits[] = new DiffOpAdd( $add ); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
return $edits; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public function setBailoutComplexity( $value ) { |
| 160 |
$this->bailoutComplexity = $value; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
private function shiftBoundaries( array $lines, array &$changed, array $other_changed ) { |
| 181 |
$i = 0; |
| 182 |
$j = 0; |
| 183 |
|
| 184 |
assert( count( $lines ) == count( $changed ) ); |
| 185 |
$len = count( $lines ); |
| 186 |
$other_len = count( $other_changed ); |
| 187 |
|
| 188 |
while ( 1 ) { |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
while ( $j < $other_len && $other_changed[$j] ) { |
| 201 |
$j++; |
| 202 |
} |
| 203 |
|
| 204 |
while ( $i < $len && !$changed[$i] ) { |
| 205 |
assert( $j < $other_len && !$other_changed[$j] ); |
| 206 |
$i++; |
| 207 |
$j++; |
| 208 |
while ( $j < $other_len && $other_changed[$j] ) { |
| 209 |
$j++; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
if ( $i == $len ) { |
| 214 |
break; |
| 215 |
} |
| 216 |
|
| 217 |
$start = $i; |
| 218 |
|
| 219 |
|
| 220 |
while ( ++$i < $len && $changed[$i] ) { |
| 221 |
continue; |
| 222 |
} |
| 223 |
|
| 224 |
do { |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
$runlength = $i - $start; |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
while ( $start > 0 && $lines[$start - 1] == $lines[$i - 1] ) { |
| 237 |
$changed[--$start] = 1; |
| 238 |
$changed[--$i] = false; |
| 239 |
|
| 240 |
while ( $start > 0 && $changed[$start - 1] ) { |
| 241 |
$start--; |
| 242 |
} |
| 243 |
assert( $j > 0 ); |
| 244 |
while ( $other_changed[--$j] ) { |
| 245 |
continue; |
| 246 |
} |
| 247 |
assert( $j >= 0 && !$other_changed[$j] ); |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
$corresponding = $j < $other_len ? $i : $len; |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
while ( $i < $len && $lines[$start] == $lines[$i] ) { |
| 265 |
$changed[$start++] = false; |
| 266 |
$changed[$i++] = 1; |
| 267 |
while ( $i < $len && $changed[$i] ) { |
| 268 |
$i++; |
| 269 |
} |
| 270 |
|
| 271 |
assert( $j < $other_len && !$other_changed[$j] ); |
| 272 |
$j++; |
| 273 |
if ( $j < $other_len && $other_changed[$j] ) { |
| 274 |
$corresponding = $i; |
| 275 |
while ( $j < $other_len && $other_changed[$j] ) { |
| 276 |
$j++; |
| 277 |
} |
| 278 |
} |
| 279 |
} |
| 280 |
} while ( $runlength != $i - $start ); |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
while ( $corresponding < $i ) { |
| 287 |
$changed[--$start] = 1; |
| 288 |
$changed[--$i] = 0; |
| 289 |
assert( $j > 0 ); |
| 290 |
while ( $other_changed[--$j] ) { |
| 291 |
continue; |
| 292 |
} |
| 293 |
assert( $j >= 0 && !$other_changed[$j] ); |
| 294 |
} |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
protected function diffInternal( array $from, array $to ) { |
| 304 |
|
| 305 |
$m = count( $from ); |
| 306 |
$n = count( $to ); |
| 307 |
|
| 308 |
$this->heuristicUsed = false; |
| 309 |
|
| 310 |
|
| 311 |
$removed = $m > 0 ? array_fill( 0, $m, true ) : []; |
| 312 |
$added = $n > 0 ? array_fill( 0, $n, true ) : []; |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
$i = 0; |
| 317 |
while ( $i < $m && $i < $n && $from[$i] === $to[$i] ) { |
| 318 |
$removed[$i] = $added[$i] = false; |
| 319 |
unset( $from[$i], $to[$i] ); |
| 320 |
++$i; |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
$j = 1; |
| 325 |
while ( $i + $j <= $m && $i + $j <= $n && $from[$m - $j] === $to[$n - $j] ) { |
| 326 |
$removed[$m - $j] = $added[$n - $j] = false; |
| 327 |
unset( $from[$m - $j], $to[$n - $j] ); |
| 328 |
++$j; |
| 329 |
} |
| 330 |
|
| 331 |
$this->from = $newFromIndex = $this->to = $newToIndex = []; |
| 332 |
|
| 333 |
|
| 334 |
$shared = []; |
| 335 |
foreach ( $from as $key ) { |
| 336 |
$shared[$key] = false; |
| 337 |
} |
| 338 |
|
| 339 |
foreach ( $to as $index => &$el ) { |
| 340 |
if ( array_key_exists( $el, $shared ) ) { |
| 341 |
|
| 342 |
$this->to[] = $el; |
| 343 |
$shared[$el] = true; |
| 344 |
$newToIndex[] = $index; |
| 345 |
} |
| 346 |
} |
| 347 |
foreach ( $from as $index => &$el ) { |
| 348 |
if ( $shared[$el] ) { |
| 349 |
|
| 350 |
$this->from[] = $el; |
| 351 |
$newFromIndex[] = $index; |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
unset( $shared, $from, $to ); |
| 356 |
|
| 357 |
$this->m = count( $this->from ); |
| 358 |
$this->n = count( $this->to ); |
| 359 |
|
| 360 |
if ( $this->bailoutComplexity > 0 && $this->m * $this->n > $this->bailoutComplexity ) { |
| 361 |
throw new ComplexityException(); |
| 362 |
} |
| 363 |
|
| 364 |
$this->removed = $this->m > 0 ? array_fill( 0, $this->m, true ) : []; |
| 365 |
$this->added = $this->n > 0 ? array_fill( 0, $this->n, true ) : []; |
| 366 |
|
| 367 |
if ( $this->m == 0 || $this->n == 0 ) { |
| 368 |
$this->length = 0; |
| 369 |
} else { |
| 370 |
$this->maxDifferences = ceil( ( $this->m + $this->n ) / 2.0 ); |
| 371 |
if ( $this->m * $this->n > $this->tooLong ) { |
| 372 |
|
| 373 |
$this->maxDifferences = floor( $this->maxDifferences ** ( $this->powLimit - 1.0 ) ); |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
$max = min( $this->m, $this->n ); |
| 381 |
for ( $forwardBound = 0; $forwardBound < $max |
| 382 |
&& $this->from[$forwardBound] === $this->to[$forwardBound]; |
| 383 |
++$forwardBound |
| 384 |
) { |
| 385 |
$this->removed[$forwardBound] = $this->added[$forwardBound] = false; |
| 386 |
} |
| 387 |
|
| 388 |
$backBoundL1 = $this->m - 1; |
| 389 |
$backBoundL2 = $this->n - 1; |
| 390 |
|
| 391 |
while ( $backBoundL1 >= $forwardBound && $backBoundL2 >= $forwardBound |
| 392 |
&& $this->from[$backBoundL1] === $this->to[$backBoundL2] |
| 393 |
) { |
| 394 |
$this->removed[$backBoundL1--] = $this->added[$backBoundL2--] = false; |
| 395 |
} |
| 396 |
|
| 397 |
$temp = array_fill( 0, $this->m + $this->n + 1, 0 ); |
| 398 |
$V = [ $temp, $temp ]; |
| 399 |
$snake = [ 0, 0, 0 ]; |
| 400 |
|
| 401 |
$this->length = $forwardBound + $this->m - $backBoundL1 - 1 |
| 402 |
+ $this->lcs_rec( |
| 403 |
$forwardBound, |
| 404 |
$backBoundL1, |
| 405 |
$forwardBound, |
| 406 |
$backBoundL2, |
| 407 |
$V, |
| 408 |
$snake |
| 409 |
); |
| 410 |
} |
| 411 |
|
| 412 |
$this->m = $m; |
| 413 |
$this->n = $n; |
| 414 |
|
| 415 |
$this->length += $i + $j - 1; |
| 416 |
|
| 417 |
foreach ( $this->removed as $key => &$removed_elem ) { |
| 418 |
if ( !$removed_elem ) { |
| 419 |
$removed[$newFromIndex[$key]] = false; |
| 420 |
} |
| 421 |
} |
| 422 |
foreach ( $this->added as $key => &$added_elem ) { |
| 423 |
if ( !$added_elem ) { |
| 424 |
$added[$newToIndex[$key]] = false; |
| 425 |
} |
| 426 |
} |
| 427 |
$this->removed = $removed; |
| 428 |
$this->added = $added; |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
private function lcs_rec( $bottoml1, $topl1, $bottoml2, $topl2, &$V, &$snake ) { |
| 441 |
|
| 442 |
if ( $bottoml1 > $topl1 || $bottoml2 > $topl2 ) { |
| 443 |
return 0; |
| 444 |
} |
| 445 |
|
| 446 |
$d = $this->find_middle_snake( $bottoml1, $topl1, $bottoml2, |
| 447 |
$topl2, $V, $snake ); |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
[ $startx, $starty, $len ] = $snake; |
| 452 |
|
| 453 |
|
| 454 |
for ( $i = 0; $i < $len; ++$i ) { |
| 455 |
$this->removed[$startx + $i] = $this->added[$starty + $i] = false; |
| 456 |
} |
| 457 |
|
| 458 |
if ( $d > 1 ) { |
| 459 |
return $len |
| 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 ) { |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 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; |
| 474 |
} |
| 475 |
|
| 476 |
return $max + $len; |
| 477 |
} |
| 478 |
|
| 479 |
return $len; |
| 480 |
} |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
private function find_middle_snake( $bottoml1, $topl1, $bottoml2, $topl2, &$V, &$snake ) { |
| 492 |
$from = &$this->from; |
| 493 |
$to = &$this->to; |
| 494 |
$V0 = &$V[0]; |
| 495 |
$V1 = &$V[1]; |
| 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; |
| 503 |
$delta = $N - $M; |
| 504 |
$maxabsx = $N + $bottoml1; |
| 505 |
$maxabsy = $M + $bottoml2; |
| 506 |
$limit = min( $this->maxDifferences, ceil( ( $N + $M ) / 2 ) ); |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
if ( $M & 1 ) { |
| 511 |
$value_to_add_forward = 1; |
| 512 |
} else { |
| 513 |
$value_to_add_forward = 0; |
| 514 |
} |
| 515 |
|
| 516 |
if ( $N & 1 ) { |
| 517 |
$value_to_add_backward = 1; |
| 518 |
} else { |
| 519 |
$value_to_add_backward = 0; |
| 520 |
} |
| 521 |
|
| 522 |
$start_forward = -$M; |
| 523 |
$end_forward = $N; |
| 524 |
$start_backward = -$N; |
| 525 |
$end_backward = $M; |
| 526 |
|
| 527 |
$limit_min_1 = $limit - 1; |
| 528 |
$limit_plus_1 = $limit + 1; |
| 529 |
|
| 530 |
$V0[$limit_plus_1] = 0; |
| 531 |
$V1[$limit_min_1] = $N; |
| 532 |
$limit = min( $this->maxDifferences, ceil( ( $N + $M ) / 2 ) ); |
| 533 |
|
| 534 |
if ( $delta & 1 ) { |
| 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; |
| 539 |
|
| 540 |
|
| 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] ) |
| 544 |
) { |
| 545 |
$x = $V0[$limit_plus_1 + $k]; |
| 546 |
} else { |
| 547 |
$x = $V0[$limit_min_1 + $k] + 1; |
| 548 |
} |
| 549 |
|
| 550 |
$absx = $snake0 = $x + $bottoml1; |
| 551 |
$absy = $snake1 = $x - $k + $bottoml2; |
| 552 |
|
| 553 |
while ( $absx < $maxabsx && $absy < $maxabsy && $from[$absx] === $to[$absy] ) { |
| 554 |
++$absx; |
| 555 |
++$absy; |
| 556 |
} |
| 557 |
$x = $absx - $bottoml1; |
| 558 |
|
| 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] |
| 563 |
) { |
| 564 |
return 2 * $d - 1; |
| 565 |
} |
| 566 |
|
| 567 |
|
| 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; |
| 573 |
} |
| 574 |
} |
| 575 |
|
| 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; |
| 579 |
|
| 580 |
|
| 581 |
for ( $k = $start_diag; $k <= $end_diag; $k += 2 ) { |
| 582 |
if ( $k == $d |
| 583 |
|| ( $k != -$d && $V1[$limit_min_1 + $k] < $V1[$limit_plus_1 + $k] ) |
| 584 |
) { |
| 585 |
$x = $V1[$limit_min_1 + $k]; |
| 586 |
} else { |
| 587 |
$x = $V1[$limit_plus_1 + $k] - 1; |
| 588 |
} |
| 589 |
|
| 590 |
$y = $x - $k - $delta; |
| 591 |
|
| 592 |
$snake2 = 0; |
| 593 |
while ( $x > 0 && $y > 0 |
| 594 |
&& $from[$x + $bottoml1_min_1] === $to[$y + $bottoml2_min_1] |
| 595 |
) { |
| 596 |
--$x; |
| 597 |
--$y; |
| 598 |
++$snake2; |
| 599 |
} |
| 600 |
$V1[$limit + $k] = $x; |
| 601 |
|
| 602 |
|
| 603 |
if ( $x <= 0 ) { |
| 604 |
$start_backward = $k + 1; |
| 605 |
$value_to_add_backward = 0; |
| 606 |
} elseif ( $y <= 0 && $end_backward > $k - 1 ) { |
| 607 |
$end_backward = $k - 1; |
| 608 |
} |
| 609 |
} |
| 610 |
} |
| 611 |
} else { |
| 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; |
| 616 |
|
| 617 |
|
| 618 |
for ( $k = $start_diag; $k <= $end_diag; $k += 2 ) { |
| 619 |
if ( $k == -$d |
| 620 |
|| ( $k < $d && $V0[$limit_min_1 + $k] < $V0[$limit_plus_1 + $k] ) |
| 621 |
) { |
| 622 |
$x = $V0[$limit_plus_1 + $k]; |
| 623 |
} else { |
| 624 |
$x = $V0[$limit_min_1 + $k] + 1; |
| 625 |
} |
| 626 |
|
| 627 |
$absx = $snake0 = $x + $bottoml1; |
| 628 |
$absy = $snake1 = $x - $k + $bottoml2; |
| 629 |
|
| 630 |
while ( $absx < $maxabsx && $absy < $maxabsy && $from[$absx] === $to[$absy] ) { |
| 631 |
++$absx; |
| 632 |
++$absy; |
| 633 |
} |
| 634 |
$x = $absx - $bottoml1; |
| 635 |
$snake2 = $absx - $snake0; |
| 636 |
$V0[$limit + $k] = $x; |
| 637 |
|
| 638 |
|
| 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; |
| 644 |
} |
| 645 |
} |
| 646 |
|
| 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; |
| 650 |
|
| 651 |
|
| 652 |
for ( $k = $start_diag; $k <= $end_diag; $k += 2 ) { |
| 653 |
if ( $k == $d |
| 654 |
|| ( $k != -$d && $V1[$limit_min_1 + $k] < $V1[$limit_plus_1 + $k] ) |
| 655 |
) { |
| 656 |
$x = $V1[$limit_min_1 + $k]; |
| 657 |
} else { |
| 658 |
$x = $V1[$limit_plus_1 + $k] - 1; |
| 659 |
} |
| 660 |
|
| 661 |
$y = $x - $k - $delta; |
| 662 |
|
| 663 |
$snake2 = 0; |
| 664 |
while ( $x > 0 && $y > 0 |
| 665 |
&& $from[$x + $bottoml1_min_1] === $to[$y + $bottoml2_min_1] |
| 666 |
) { |
| 667 |
--$x; |
| 668 |
--$y; |
| 669 |
++$snake2; |
| 670 |
} |
| 671 |
$V1[$limit + $k] = $x; |
| 672 |
|
| 673 |
if ( $k >= -$delta - $d && $k <= $d - $delta |
| 674 |
&& $x <= $V0[$limit + $k + $delta] |
| 675 |
) { |
| 676 |
$snake0 = $bottoml1 + $x; |
| 677 |
$snake1 = $bottoml2 + $y; |
| 678 |
|
| 679 |
return 2 * $d; |
| 680 |
} |
| 681 |
|
| 682 |
|
| 683 |
if ( $x <= 0 ) { |
| 684 |
$start_backward = $k + 1; |
| 685 |
$value_to_add_backward = 0; |
| 686 |
} elseif ( $y <= 0 && $end_backward > $k - 1 ) { |
| 687 |
$end_backward = $k - 1; |
| 688 |
} |
| 689 |
} |
| 690 |
} |
| 691 |
} |
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
$most_progress = self::findMostProgress( $M, $N, $limit, $V ); |
| 699 |
|
| 700 |
$snake0 = $bottoml1 + $most_progress[0]; |
| 701 |
$snake1 = $bottoml2 + $most_progress[1]; |
| 702 |
$snake2 = 0; |
| 703 |
|
| 704 |
$this->heuristicUsed = true; |
| 705 |
|
| 706 |
return 5; |
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
} |
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
private static function findMostProgress( $M, $N, $limit, $V ) { |
| 723 |
$delta = $N - $M; |
| 724 |
|
| 725 |
if ( ( $M & 1 ) == ( $limit & 1 ) ) { |
| 726 |
$forward_start_diag = max( -$M, -$limit ); |
| 727 |
} else { |
| 728 |
$forward_start_diag = max( 1 - $M, -$limit ); |
| 729 |
} |
| 730 |
|
| 731 |
$forward_end_diag = min( $N, $limit ); |
| 732 |
|
| 733 |
if ( ( $N & 1 ) == ( $limit & 1 ) ) { |
| 734 |
$backward_start_diag = max( -$N, -$limit ); |
| 735 |
} else { |
| 736 |
$backward_start_diag = max( 1 - $N, -$limit ); |
| 737 |
} |
| 738 |
|
| 739 |
$backward_end_diag = -min( $M, $limit ); |
| 740 |
|
| 741 |
$temp = [ 0, 0, 0 ]; |
| 742 |
|
| 743 |
$max_progress = array_fill( 0, ceil( max( $forward_end_diag - $forward_start_diag, |
| 744 |
$backward_end_diag - $backward_start_diag ) / 2 ), $temp ); |
| 745 |
$num_progress = 0; |
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
for ( $k = $forward_start_diag; $k <= $forward_end_diag; $k += 2 ) { |
| 750 |
$x = $V[0][$limit + $k]; |
| 751 |
$y = $x - $k; |
| 752 |
if ( $x > $N || $y > $M ) { |
| 753 |
continue; |
| 754 |
} |
| 755 |
|
| 756 |
$progress = $x + $y; |
| 757 |
if ( $progress > $max_progress[0][2] ) { |
| 758 |
$num_progress = 0; |
| 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] ) { |
| 763 |
++$num_progress; |
| 764 |
$max_progress[$num_progress][0] = $x; |
| 765 |
$max_progress[$num_progress][1] = $y; |
| 766 |
$max_progress[$num_progress][2] = $progress; |
| 767 |
} |
| 768 |
} |
| 769 |
|
| 770 |
$max_progress_forward = true; |
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 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 ) { |
| 779 |
continue; |
| 780 |
} |
| 781 |
|
| 782 |
$progress = $N - $x + $M - $y; |
| 783 |
if ( $progress > $max_progress[0][2] ) { |
| 784 |
$num_progress = 0; |
| 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 ) { |
| 790 |
++$num_progress; |
| 791 |
$max_progress[$num_progress][0] = $x; |
| 792 |
$max_progress[$num_progress][1] = $y; |
| 793 |
$max_progress[$num_progress][2] = $progress; |
| 794 |
} |
| 795 |
} |
| 796 |
|
| 797 |
|
| 798 |
return $max_progress[(int)floor( $num_progress / 2 )]; |
| 799 |
} |
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
public function getLcsLength() { |
| 805 |
if ( $this->heuristicUsed && !$this->lcsLengthCorrectedForHeuristic ) { |
| 806 |
$this->lcsLengthCorrectedForHeuristic = true; |
| 807 |
$this->length = $this->m - array_sum( $this->added ); |
| 808 |
} |
| 809 |
|
| 810 |
return $this->length; |
| 811 |
} |
| 812 |
|
| 813 |
} |
| 814 |
|
| 815 |
|
| 816 |
class_alias( DiffEngine::class, 'DiffEngine' ); |