-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathutils.js
More file actions
396 lines (375 loc) · 8.01 KB
/
Copy pathutils.js
File metadata and controls
396 lines (375 loc) · 8.01 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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
'use strict';
/* AUTOMATICALLY IMPORTED, DO NOT MODIFY */
const append = (get, parent, children, start, end, before) => {
if ((end - start) < 2)
parent.insertBefore(get(children[start], 1), before);
else {
const fragment = parent.ownerDocument.createDocumentFragment();
while (start < end)
fragment.appendChild(get(children[start++], 1));
parent.insertBefore(fragment, before);
}
};
exports.append = append;
const eqeq = (a, b) => a == b;
exports.eqeq = eqeq;
const identity = O => O;
exports.identity = identity;
const indexOf = (
moreNodes,
moreStart,
moreEnd,
lessNodes,
lessStart,
lessEnd,
compare
) => {
const length = lessEnd - lessStart;
/* istanbul ignore if */
if (length < 1)
return -1;
while ((moreEnd - moreStart) >= length) {
let m = moreStart;
let l = lessStart;
while (
m < moreEnd &&
l < lessEnd &&
compare(moreNodes[m], lessNodes[l])
) {
m++;
l++;
}
if (l === lessEnd)
return moreStart;
moreStart = m + 1;
}
return -1;
};
exports.indexOf = indexOf;
const isReversed = (
futureNodes,
futureEnd,
currentNodes,
currentStart,
currentEnd,
compare
) => {
while (
currentStart < currentEnd &&
compare(
currentNodes[currentStart],
futureNodes[futureEnd - 1]
)) {
currentStart++;
futureEnd--;
};
return futureEnd === 0;
};
exports.isReversed = isReversed;
const next = (get, list, i, length, before) => i < length ?
get(list[i], 0) :
(0 < i ?
get(list[i - 1], -0).nextSibling :
before);
exports.next = next;
const remove = (get, parent, children, start, end) => {
if ((end - start) < 2)
parent.removeChild(get(children[start], -1));
else {
const range = parent.ownerDocument.createRange();
range.setStartBefore(get(children[start], -1));
range.setEndAfter(get(children[end - 1], -1));
range.deleteContents();
}
};
exports.remove = remove;
// - - - - - - - - - - - - - - - - - - -
// diff related constants and utilities
// - - - - - - - - - - - - - - - - - - -
const DELETION = -1;
const INSERTION = 1;
const SKIP = 0;
const SKIP_OND = 50;
/* istanbul ignore next */
const Rel = typeof Map === 'undefined' ?
function () {
const k = [], v = [];
return {
has: value => -1 < k.indexOf(value),
get: value => v[k.indexOf(value)],
set: value => {
const i = k.indexOf(value);
v[i < 0 ? (k.push(value) - 1) : i] = value;
}
};
} :
Map
;
const HS = (
futureNodes,
futureStart,
futureEnd,
futureChanges,
currentNodes,
currentStart,
currentEnd,
currentChanges
) => {
let k = 0;
/* istanbul ignore next */
let minLen = futureChanges < currentChanges ? futureChanges : currentChanges;
const link = Array(minLen++);
const tresh = Array(minLen);
tresh[0] = -1;
for (let i = 1; i < minLen; i++)
tresh[i] = currentEnd;
const keymap = new Rel;
for (let i = currentStart; i < currentEnd; i++)
keymap.set(currentNodes[i], i);
for (let i = futureStart; i < futureEnd; i++) {
const idxInOld = keymap.get(futureNodes[i]);
if (idxInOld != null) {
k = findK(tresh, minLen, idxInOld);
/* istanbul ignore else */
if (-1 < k) {
tresh[k] = idxInOld;
link[k] = {
newi: i,
oldi: idxInOld,
prev: link[k - 1]
};
}
}
}
k = --minLen;
--currentEnd;
while (tresh[k] > currentEnd) --k;
minLen = currentChanges + futureChanges - k;
const diff = Array(minLen);
let ptr = link[k];
--futureEnd;
while (ptr) {
const {newi, oldi} = ptr;
while (futureEnd > newi) {
diff[--minLen] = INSERTION;
--futureEnd;
}
while (currentEnd > oldi) {
diff[--minLen] = DELETION;
--currentEnd;
}
diff[--minLen] = SKIP;
--futureEnd;
--currentEnd;
ptr = ptr.prev;
}
while (futureEnd >= futureStart) {
diff[--minLen] = INSERTION;
--futureEnd;
}
while (currentEnd >= currentStart) {
diff[--minLen] = DELETION;
--currentEnd;
}
return diff;
};
// this is pretty much the same petit-dom code without the delete map part
// https://github.com/yelouafi/petit-dom/blob/bd6f5c919b5ae5297be01612c524c40be45f14a7/src/vdom.js#L556-L561
const OND = (
futureNodes,
futureStart,
rows,
currentNodes,
currentStart,
cols,
compare
) => {
const length = rows + cols;
const v = [];
let d, k, r, c, pv, cv, pd;
outer: for (d = 0; d <= length; d++) {
/* istanbul ignore if */
if (d > SKIP_OND)
return null;
pd = d - 1;
/* istanbul ignore next */
pv = d ? v[d - 1] : [0, 0];
cv = v[d] = [];
for (k = -d; k <= d; k += 2) {
if (k === -d || (k !== d && pv[pd + k - 1] < pv[pd + k + 1])) {
c = pv[pd + k + 1];
} else {
c = pv[pd + k - 1] + 1;
}
r = c - k;
while (
c < cols &&
r < rows &&
compare(
currentNodes[currentStart + c],
futureNodes[futureStart + r]
)
) {
c++;
r++;
}
if (c === cols && r === rows) {
break outer;
}
cv[d + k] = c;
}
}
const diff = Array(d / 2 + length / 2);
let diffIdx = diff.length - 1;
for (d = v.length - 1; d >= 0; d--) {
while (
c > 0 &&
r > 0 &&
compare(
currentNodes[currentStart + c - 1],
futureNodes[futureStart + r - 1]
)
) {
// diagonal edge = equality
diff[diffIdx--] = SKIP;
c--;
r--;
}
if (!d)
break;
pd = d - 1;
/* istanbul ignore next */
pv = d ? v[d - 1] : [0, 0];
k = c - r;
if (k === -d || (k !== d && pv[pd + k - 1] < pv[pd + k + 1])) {
// vertical edge = insertion
r--;
diff[diffIdx--] = INSERTION;
} else {
// horizontal edge = deletion
c--;
diff[diffIdx--] = DELETION;
}
}
return diff;
};
const applyDiff = (
diff,
get,
parentNode,
futureNodes,
futureStart,
currentNodes,
currentStart,
currentLength,
before
) => {
const live = new Rel;
const length = diff.length;
let currentIndex = currentStart;
let i = 0;
while (i < length) {
switch (diff[i++]) {
case SKIP:
futureStart++;
currentIndex++;
break;
case INSERTION:
// TODO: bulk appends for sequential nodes
live.set(futureNodes[futureStart], 1);
append(
get,
parentNode,
futureNodes,
futureStart++,
futureStart,
currentIndex < currentLength ?
get(currentNodes[currentIndex], 1) :
before
);
break;
case DELETION:
currentIndex++;
break;
}
}
i = 0;
while (i < length) {
switch (diff[i++]) {
case SKIP:
currentStart++;
break;
case DELETION:
// TODO: bulk removes for sequential nodes
if (live.has(currentNodes[currentStart]))
currentStart++;
else
remove(
get,
parentNode,
currentNodes,
currentStart++,
currentStart
);
break;
}
}
};
const findK = (ktr, length, j) => {
let lo = 1;
let hi = length;
while (lo < hi) {
var mid = ((lo + hi) / 2) >>> 0;
if (j < ktr[mid])
hi = mid;
else
lo = mid + 1;
}
return lo;
}
const smartDiff = (
get,
parentNode,
futureNodes,
futureStart,
futureEnd,
futureChanges,
currentNodes,
currentStart,
currentEnd,
currentChanges,
currentLength,
compare,
before
) => {
applyDiff(
OND(
futureNodes,
futureStart,
futureChanges,
currentNodes,
currentStart,
currentChanges,
compare
) ||
HS(
futureNodes,
futureStart,
futureEnd,
futureChanges,
currentNodes,
currentStart,
currentEnd,
currentChanges
),
get,
parentNode,
futureNodes,
futureStart,
currentNodes,
currentStart,
currentLength,
before
);
};
exports.smartDiff = smartDiff;