comparison vendor/sebastian/diff/src/LCS/TimeEfficientLongestCommonSubsequenceImplementation.php @ 12:7a779792577d

Update Drupal core to v8.4.5 (via Composer)
author Chris Cannam
date Fri, 23 Feb 2018 15:52:07 +0000
parents 4c8ae668cc8c
children
comparison
equal deleted inserted replaced
11:bfffd8d7479a 12:7a779792577d
1 <?php 1 <?php
2 /* 2 /*
3 * This file is part of the Diff package. 3 * This file is part of sebastian/diff.
4 * 4 *
5 * (c) Sebastian Bergmann <sebastian@phpunit.de> 5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6 * 6 *
7 * For the full copyright and license information, please view the LICENSE 7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code. 8 * file that was distributed with this source code.
24 * @return array 24 * @return array
25 */ 25 */
26 public function calculate(array $from, array $to) 26 public function calculate(array $from, array $to)
27 { 27 {
28 $common = array(); 28 $common = array();
29 $fromLength = count($from); 29 $fromLength = \count($from);
30 $toLength = count($to); 30 $toLength = \count($to);
31 $width = $fromLength + 1; 31 $width = $fromLength + 1;
32 $matrix = new \SplFixedArray($width * ($toLength + 1)); 32 $matrix = new \SplFixedArray($width * ($toLength + 1));
33 33
34 for ($i = 0; $i <= $fromLength; ++$i) { 34 for ($i = 0; $i <= $fromLength; ++$i) {
35 $matrix[$i] = 0; 35 $matrix[$i] = 0;
40 } 40 }
41 41
42 for ($i = 1; $i <= $fromLength; ++$i) { 42 for ($i = 1; $i <= $fromLength; ++$i) {
43 for ($j = 1; $j <= $toLength; ++$j) { 43 for ($j = 1; $j <= $toLength; ++$j) {
44 $o = ($j * $width) + $i; 44 $o = ($j * $width) + $i;
45 $matrix[$o] = max( 45 $matrix[$o] = \max(
46 $matrix[$o - 1], 46 $matrix[$o - 1],
47 $matrix[$o - $width], 47 $matrix[$o - $width],
48 $from[$i - 1] === $to[$j - 1] ? $matrix[$o - $width - 1] + 1 : 0 48 $from[$i - 1] === $to[$j - 1] ? $matrix[$o - $width - 1] + 1 : 0
49 ); 49 );
50 } 50 }
52 52
53 $i = $fromLength; 53 $i = $fromLength;
54 $j = $toLength; 54 $j = $toLength;
55 55
56 while ($i > 0 && $j > 0) { 56 while ($i > 0 && $j > 0) {
57 if ($from[$i-1] === $to[$j-1]) { 57 if ($from[$i - 1] === $to[$j - 1]) {
58 $common[] = $from[$i-1]; 58 $common[] = $from[$i - 1];
59 --$i; 59 --$i;
60 --$j; 60 --$j;
61 } else { 61 } else {
62 $o = ($j * $width) + $i; 62 $o = ($j * $width) + $i;
63
63 if ($matrix[$o - $width] > $matrix[$o - 1]) { 64 if ($matrix[$o - $width] > $matrix[$o - 1]) {
64 --$j; 65 --$j;
65 } else { 66 } else {
66 --$i; 67 --$i;
67 } 68 }
68 } 69 }
69 } 70 }
70 71
71 return array_reverse($common); 72 return \array_reverse($common);
72 } 73 }
73 } 74 }