TimeEfficientLongestCommonSubsequenceCalculator.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of sebastian/diff.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace SebastianBergmann\Diff;
  11. use function array_reverse;
  12. use function count;
  13. use function max;
  14. use SplFixedArray;
  15. final class TimeEfficientLongestCommonSubsequenceCalculator implements LongestCommonSubsequenceCalculator
  16. {
  17. /**
  18. * @inheritDoc
  19. */
  20. public function calculate(array $from, array $to): array
  21. {
  22. $common = [];
  23. $fromLength = count($from);
  24. $toLength = count($to);
  25. $width = $fromLength + 1;
  26. $matrix = new SplFixedArray($width * ($toLength + 1));
  27. for ($i = 0; $i <= $fromLength; $i++) {
  28. $matrix[$i] = 0;
  29. }
  30. for ($j = 0; $j <= $toLength; $j++) {
  31. $matrix[$j * $width] = 0;
  32. }
  33. for ($i = 1; $i <= $fromLength; $i++) {
  34. for ($j = 1; $j <= $toLength; $j++) {
  35. $o = ($j * $width) + $i;
  36. // don't use max() to avoid function call overhead
  37. $firstOrLast = $from[$i - 1] === $to[$j - 1] ? $matrix[$o - $width - 1] + 1 : 0;
  38. if ($matrix[$o - 1] > $matrix[$o - $width]) {
  39. if ($firstOrLast > $matrix[$o - 1]) {
  40. $matrix[$o] = $firstOrLast;
  41. } else {
  42. $matrix[$o] = $matrix[$o - 1];
  43. }
  44. } else {
  45. if ($firstOrLast > $matrix[$o - $width]) {
  46. $matrix[$o] = $firstOrLast;
  47. } else {
  48. $matrix[$o] = $matrix[$o - $width];
  49. }
  50. }
  51. }
  52. }
  53. $i = $fromLength;
  54. $j = $toLength;
  55. while ($i > 0 && $j > 0) {
  56. if ($from[$i - 1] === $to[$j - 1]) {
  57. $common[] = $from[$i - 1];
  58. $i--;
  59. $j--;
  60. } else {
  61. $o = ($j * $width) + $i;
  62. if ($matrix[$o - $width] > $matrix[$o - 1]) {
  63. $j--;
  64. } else {
  65. $i--;
  66. }
  67. }
  68. }
  69. return array_reverse($common);
  70. }
  71. }