Mercurial > hg > isophonics-drupal-site
comparison vendor/sebastian/diff/src/LCS/MemoryEfficientLongestCommonSubsequenceImplementation.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. |
23 * | 23 * |
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 $cFrom = count($from); | 28 $cFrom = \count($from); |
29 $cTo = count($to); | 29 $cTo = \count($to); |
30 | 30 |
31 if ($cFrom == 0) { | 31 if ($cFrom === 0) { |
32 return array(); | 32 return array(); |
33 } elseif ($cFrom == 1) { | 33 } |
34 if (in_array($from[0], $to)) { | 34 |
35 if ($cFrom === 1) { | |
36 if (\in_array($from[0], $to, true)) { | |
35 return array($from[0]); | 37 return array($from[0]); |
36 } else { | |
37 return array(); | |
38 } | |
39 } else { | |
40 $i = intval($cFrom / 2); | |
41 $fromStart = array_slice($from, 0, $i); | |
42 $fromEnd = array_slice($from, $i); | |
43 $llB = $this->length($fromStart, $to); | |
44 $llE = $this->length(array_reverse($fromEnd), array_reverse($to)); | |
45 $jMax = 0; | |
46 $max = 0; | |
47 | |
48 for ($j = 0; $j <= $cTo; $j++) { | |
49 $m = $llB[$j] + $llE[$cTo - $j]; | |
50 | |
51 if ($m >= $max) { | |
52 $max = $m; | |
53 $jMax = $j; | |
54 } | |
55 } | 38 } |
56 | 39 |
57 $toStart = array_slice($to, 0, $jMax); | 40 return array(); |
58 $toEnd = array_slice($to, $jMax); | 41 } |
59 | 42 |
60 return array_merge( | 43 $i = (int) ($cFrom / 2); |
61 $this->calculate($fromStart, $toStart), | 44 $fromStart = \array_slice($from, 0, $i); |
62 $this->calculate($fromEnd, $toEnd) | 45 $fromEnd = \array_slice($from, $i); |
63 ); | 46 $llB = $this->length($fromStart, $to); |
47 $llE = $this->length(\array_reverse($fromEnd), \array_reverse($to)); | |
48 $jMax = 0; | |
49 $max = 0; | |
50 | |
51 for ($j = 0; $j <= $cTo; $j++) { | |
52 $m = $llB[$j] + $llE[$cTo - $j]; | |
53 | |
54 if ($m >= $max) { | |
55 $max = $m; | |
56 $jMax = $j; | |
57 } | |
64 } | 58 } |
59 | |
60 $toStart = \array_slice($to, 0, $jMax); | |
61 $toEnd = \array_slice($to, $jMax); | |
62 | |
63 return \array_merge( | |
64 $this->calculate($fromStart, $toStart), | |
65 $this->calculate($fromEnd, $toEnd) | |
66 ); | |
65 } | 67 } |
66 | 68 |
67 /** | 69 /** |
68 * @param array $from | 70 * @param array $from |
69 * @param array $to | 71 * @param array $to |
70 * | 72 * |
71 * @return array | 73 * @return array |
72 */ | 74 */ |
73 private function length(array $from, array $to) | 75 private function length(array $from, array $to) |
74 { | 76 { |
75 $current = array_fill(0, count($to) + 1, 0); | 77 $current = \array_fill(0, \count($to) + 1, 0); |
76 $cFrom = count($from); | 78 $cFrom = \count($from); |
77 $cTo = count($to); | 79 $cTo = \count($to); |
78 | 80 |
79 for ($i = 0; $i < $cFrom; $i++) { | 81 for ($i = 0; $i < $cFrom; $i++) { |
80 $prev = $current; | 82 $prev = $current; |
81 | 83 |
82 for ($j = 0; $j < $cTo; $j++) { | 84 for ($j = 0; $j < $cTo; $j++) { |
83 if ($from[$i] == $to[$j]) { | 85 if ($from[$i] === $to[$j]) { |
84 $current[$j + 1] = $prev[$j] + 1; | 86 $current[$j + 1] = $prev[$j] + 1; |
85 } else { | 87 } else { |
86 $current[$j + 1] = max($current[$j], $prev[$j + 1]); | 88 $current[$j + 1] = \max($current[$j], $prev[$j + 1]); |
87 } | 89 } |
88 } | 90 } |
89 } | 91 } |
90 | 92 |
91 return $current; | 93 return $current; |