TimeEfficientLongestCommonSubsequenceCalculator.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. final class TimeEfficientLongestCommonSubsequenceCalculator implements LongestCommonSubsequenceCalculator
  12. {
  13. /**
  14. * {@inheritdoc}
  15. */
  16. public function calculate(array $from, array $to): array
  17. {
  18. $common = [];
  19. $fromLength = \count($from);
  20. $toLength = \count($to);
  21. $width = $fromLength + 1;
  22. $matrix = new \SplFixedArray($width * ($toLength + 1));
  23. for ($i = 0; $i <= $fromLength; ++$i) {
  24. $matrix[$i] = 0;
  25. }
  26. for ($j = 0; $j <= $toLength; ++$j) {
  27. $matrix[$j * $width] = 0;
  28. }
  29. for ($i = 1; $i <= $fromLength; ++$i) {
  30. for ($j = 1; $j <= $toLength; ++$j) {
  31. $o = ($j * $width) + $i;
  32. $matrix[$o] = \max(
  33. $matrix[$o - 1],
  34. $matrix[$o - $width],
  35. $from[$i - 1] === $to[$j - 1] ? $matrix[$o - $width - 1] + 1 : 0
  36. );
  37. }
  38. }
  39. $i = $fromLength;
  40. $j = $toLength;
  41. while ($i > 0 && $j > 0) {
  42. if ($from[$i - 1] === $to[$j - 1]) {
  43. $common[] = $from[$i - 1];
  44. --$i;
  45. --$j;
  46. } else {
  47. $o = ($j * $width) + $i;
  48. if ($matrix[$o - $width] > $matrix[$o - 1]) {
  49. --$j;
  50. } else {
  51. --$i;
  52. }
  53. }
  54. }
  55. return \array_reverse($common);
  56. }
  57. }