PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / 2.8.6
Kubio AI Page Builder v2.8.6
2.8.6 2.8.5 2.8.4 2.8.3 2.8.2 2.8.1 trunk 1.0.0 1.0.1 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.9.0 2.0.0 2.1.1 2.1.2 2.1.3 2.2.0 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.5 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.6.1 2.6.2 2.6.3 2.6.5 2.6.6 2.6.7 2.7.0 2.7.1 2.7.2 2.7.3 2.8.0
kubio / vendor / sebastian / diff / src / MemoryEfficientLongestCommonSubsequenceCalculator.php
kubio / vendor / sebastian / diff / src Last commit date
Exception 1 year ago Output 1 year ago Chunk.php 1 year ago Diff.php 1 year ago Differ.php 1 year ago Line.php 1 year ago LongestCommonSubsequenceCalculator.php 1 year ago MemoryEfficientLongestCommonSubsequenceCalculator.php 1 year ago Parser.php 1 year ago TimeEfficientLongestCommonSubsequenceCalculator.php 1 year ago
MemoryEfficientLongestCommonSubsequenceCalculator.php
94 lines
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
12 use function array_fill;
13 use function array_merge;
14 use function array_reverse;
15 use function array_slice;
16 use function count;
17 use function in_array;
18 use function max;
19
20 final class MemoryEfficientLongestCommonSubsequenceCalculator implements LongestCommonSubsequenceCalculator
21 {
22 /**
23 * {@inheritdoc}
24 */
25 public function calculate(array $from, array $to): array
26 {
27 $cFrom = count($from);
28 $cTo = count($to);
29
30 if ($cFrom === 0) {
31 return [];
32 }
33
34 if ($cFrom === 1) {
35 if (in_array($from[0], $to, true)) {
36 return [$from[0]];
37 }
38
39 return [];
40 }
41
42 $i = (int) ($cFrom / 2);
43 $fromStart = array_slice($from, 0, $i);
44 $fromEnd = array_slice($from, $i);
45 $llB = $this->length($fromStart, $to);
46 $llE = $this->length(array_reverse($fromEnd), array_reverse($to));
47 $jMax = 0;
48 $max = 0;
49
50 for ($j = 0; $j <= $cTo; $j++) {
51 $m = $llB[$j] + $llE[$cTo - $j];
52
53 if ($m >= $max) {
54 $max = $m;
55 $jMax = $j;
56 }
57 }
58
59 $toStart = array_slice($to, 0, $jMax);
60 $toEnd = array_slice($to, $jMax);
61
62 return array_merge(
63 $this->calculate($fromStart, $toStart),
64 $this->calculate($fromEnd, $toEnd)
65 );
66 }
67
68 private function length(array $from, array $to): array
69 {
70 $current = array_fill(0, count($to) + 1, 0);
71 $cFrom = count($from);
72 $cTo = count($to);
73
74 for ($i = 0; $i < $cFrom; $i++) {
75 $prev = $current;
76
77 for ($j = 0; $j < $cTo; $j++) {
78 if ($from[$i] === $to[$j]) {
79 $current[$j + 1] = $prev[$j] + 1;
80 } else {
81 // don't use max() to avoid function call overhead
82 if ($current[$j] > $prev[$j + 1]) {
83 $current[$j + 1] = $current[$j];
84 } else {
85 $current[$j + 1] = $prev[$j + 1];
86 }
87 }
88 }
89 }
90
91 return $current;
92 }
93 }
94