Repository navigation
Expand file tree
/
Copy pathMyers.php
More file actions
332 lines (264 loc) · 8.63 KB
/
Copy pathMyers.php
File metadata and controls
332 lines (264 loc) · 8.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<?php
declare(strict_types=1);
namespace Pharaonic\Diff\Algorithm;
/**
* Myers' O(ND) shortest edit script, in linear space
*
* Tokens are compared with ===. Common prefixes and suffixes are stripped first, and inside
* every run of edits the deletions come before the insertions, as in Git.
*
* Like Git, a search that needs more than max(MIN_COST, √(N + M)) edits to find its middle snake
* stops there and splits at the furthest point it reached, so very different inputs stay fast;
* the script is then still valid but may not be the shortest.
*
* @internal
*/
final class Myers
{
private const MIN_COST = 256;
public const EQUAL = 0;
public const DELETE = 1;
public const INSERT = 2;
/**
* @var list<int|string>
*/
private array $a;
/**
* @var list<int|string>
*/
private array $b;
/**
* @param list<int|string> $a
* @param list<int|string> $b
*/
private function __construct(array $a, array $b)
{
$this->a = $a;
$this->b = $b;
}
/**
* The edits that turn $a into $b, as [operation, index in $a, index in $b]; the index of the
* side an edit does not touch is where it would be inserted.
*
* @param list<int|string> $a
* @param list<int|string> $b
*
* @return list<array{int, int, int}>
*/
public static function diff(array $a, array $b): array
{
$n = count($a);
$m = count($b);
$start = 0;
while ($start < $n && $start < $m && $a[$start] === $b[$start]) {
$start++;
}
$end = 0;
while ($end < $n - $start && $end < $m - $start && $a[$n - 1 - $end] === $b[$m - 1 - $end]) {
$end++;
}
$edits = [];
for ($i = 0; $i < $start; $i++) {
$edits[] = [self::EQUAL, $i, $i];
}
$myers = new self(array_slice($a, $start, $n - $start - $end), array_slice($b, $start, $m - $start - $end));
foreach ($myers->edits() as [$operation, $x, $y]) {
$edits[] = [$operation, $x + $start, $y + $start];
}
for ($i = $end; $i > 0; $i--) {
$edits[] = [self::EQUAL, $n - $i, $m - $i];
}
return self::deletionsFirst($edits);
}
/**
* @return list<array{int, int, int}>
*/
private function edits(): array
{
$n = count($this->a);
$m = count($this->b);
if ($n === 0 || $m === 0) {
$edits = [];
for ($x = 0; $x < $n; $x++) {
$edits[] = [self::DELETE, $x, 0];
}
for ($y = 0; $y < $m; $y++) {
$edits[] = [self::INSERT, $n, $y];
}
return $edits;
}
$path = $this->path(0, 0, $n, $m) ?? [[0, 0], [$n, $m]];
$edits = [];
for ($i = 1, $count = count($path); $i < $count; $i++) {
[$x1, $y1] = $path[$i - 1];
[$x2, $y2] = $path[$i];
[$x1, $y1] = $this->diagonal($x1, $y1, $x2, $y2, $edits);
if ($x2 - $x1 < $y2 - $y1) {
$edits[] = [self::INSERT, $x1, $y1];
$y1++;
} elseif ($x2 - $x1 > $y2 - $y1) {
$edits[] = [self::DELETE, $x1, $y1];
$x1++;
}
$this->diagonal($x1, $y1, $x2, $y2, $edits);
}
return $edits;
}
/**
* @param list<array{int, int, int}> $edits
*
* @return array{int, int}
*/
private function diagonal(int $x1, int $y1, int $x2, int $y2, array &$edits): array
{
while ($x1 < $x2 && $y1 < $y2 && $this->a[$x1] === $this->b[$y1]) {
$edits[] = [self::EQUAL, $x1, $y1];
$x1++;
$y1++;
}
return [$x1, $y1];
}
/**
* The points of the shortest path through a box, split at its middle snake
*
* @return list<array{int, int}>|null
*/
private function path(int $left, int $top, int $right, int $bottom): ?array
{
$snake = $this->midpoint($left, $top, $right, $bottom);
if ($snake === null) {
return null;
}
[$start, $finish] = $snake;
$head = $this->path($left, $top, $start[0], $start[1]);
$tail = $this->path($finish[0], $finish[1], $right, $bottom);
return [...($head ?? [$start]), ...($tail ?? [$finish])];
}
/**
* @return array{array{int, int}, array{int, int}}|null
*/
private function midpoint(int $left, int $top, int $right, int $bottom): ?array
{
$width = $right - $left;
$height = $bottom - $top;
$size = $width + $height;
if ($size === 0) {
return null;
}
$delta = $width - $height;
$max = intdiv($size + 1, 2);
$maxCost = max(self::MIN_COST, (int) sqrt($size));
$forward = [1 => $left];
$backward = [1 => $bottom];
for ($d = 0; $d <= $max; $d++) {
for ($k = $d; $k >= -$d; $k -= 2) {
$c = $k - $delta;
if ($k === -$d || ($k !== $d && $forward[$k - 1] < $forward[$k + 1])) {
$px = $x = $forward[$k + 1];
} else {
$px = $forward[$k - 1];
$x = $px + 1;
}
$y = $top + ($x - $left) - $k;
$py = ($d === 0 || $x !== $px) ? $y : $y - 1;
while ($x < $right && $y < $bottom && $this->a[$x] === $this->b[$y]) {
$x++;
$y++;
}
$forward[$k] = $x;
if ($delta % 2 !== 0 && $c >= -($d - 1) && $c <= $d - 1 && $y >= $backward[$c]) {
return [[$px, $py], [$x, $y]];
}
}
for ($c = $d; $c >= -$d; $c -= 2) {
$k = $c + $delta;
if ($c === -$d || ($c !== $d && $backward[$c - 1] > $backward[$c + 1])) {
$py = $y = $backward[$c + 1];
} else {
$py = $backward[$c - 1];
$y = $py - 1;
}
$x = $left + ($y - $top) + $k;
$px = ($d === 0 || $y !== $py) ? $x : $x + 1;
while ($x > $left && $y > $top && $this->a[$x - 1] === $this->b[$y - 1]) {
$x--;
$y--;
}
$backward[$c] = $y;
if ($delta % 2 === 0 && $k >= -$d && $k <= $d && $x <= $forward[$k]) {
return [[$x, $y], [$px, $py]];
}
}
if ($d >= $maxCost) {
$point = self::furthest($forward, $d, $left, $top, $right, $bottom);
return [$point, $point];
}
}
return null;
}
/**
* The point inside the box that the forward search reached furthest after $d edits
*
* @param array<int, int> $forward
*
* @return array{int, int}
*/
private static function furthest(array $forward, int $d, int $left, int $top, int $right, int $bottom): array
{
$best = [$left, $top];
for ($k = -$d; $k <= $d; $k += 2) {
$x = $forward[$k];
$y = $top + ($x - $left) - $k;
if ($x <= $right && $y >= $top && $y <= $bottom && $x + $y > $best[0] + $best[1]) {
$best = [$x, $y];
}
}
return $best;
}
/**
* Put the deletions of every run of edits before its insertions
*
* @param list<array{int, int, int}> $edits
*
* @return list<array{int, int, int}>
*/
private static function deletionsFirst(array $edits): array
{
$sorted = [];
$run = [];
foreach ($edits as $edit) {
if ($edit[0] === self::EQUAL) {
self::flush($run, $sorted);
$sorted[] = $edit;
} else {
$run[] = $edit;
}
}
self::flush($run, $sorted);
return $sorted;
}
/**
* @param list<array{int, int, int}> $run
* @param list<array{int, int, int}> $sorted
*/
private static function flush(array &$run, array &$sorted): void
{
if ($run === []) {
return;
}
[, $x, $y] = $run[0];
$insertions = [];
foreach ($run as [$operation, $oldIndex, $newIndex]) {
if ($operation === self::DELETE) {
$sorted[] = [self::DELETE, $oldIndex, $y];
$x++;
} else {
$insertions[] = $newIndex;
}
}
foreach ($insertions as $newIndex) {
$sorted[] = [self::INSERT, $x, $newIndex];
}
$run = [];
}
}