summaryrefslogtreecommitdiff
path: root/testing/diff.ts
diff options
context:
space:
mode:
authorKitson Kelly <me@kitsonkelly.com>2019-03-05 11:53:35 +1100
committerRyan Dahl <ry@tinyclouds.org>2019-03-04 19:53:35 -0500
commit17663c12326dd1053f89a3bd741807f139973dae (patch)
tree4588e84042b0155e11d7f5442ae38a6007922585 /testing/diff.ts
parent9f33cd28963a72d8fea0b1e99bb61ca9bec21a94 (diff)
Add eslint for linting (denoland/deno_std#235)
Original: https://github.com/denoland/deno_std/commit/c0390ade3de4d825423c9f0f5e1aa56ffd509942
Diffstat (limited to 'testing/diff.ts')
-rw-r--r--testing/diff.ts97
1 files changed, 50 insertions, 47 deletions
diff --git a/testing/diff.ts b/testing/diff.ts
index a1385b88a..4fab75e4a 100644
--- a/testing/diff.ts
+++ b/testing/diff.ts
@@ -15,7 +15,7 @@ const REMOVED = 1;
const COMMON = 2;
const ADDED = 3;
-function createCommon<T>(A: T[], B: T[], reverse?: boolean) {
+function createCommon<T>(A: T[], B: T[], reverse?: boolean): T[] {
const common = [];
if (A.length === 0 || B.length === 0) return [];
for (let i = 0; i < Math.min(A.length, B.length); i += 1) {
@@ -30,13 +30,55 @@ function createCommon<T>(A: T[], B: T[], reverse?: boolean) {
return common;
}
-export default function diff<T>(A: T[], B: T[]): DiffResult<T>[] {
+export default function diff<T>(A: T[], B: T[]): Array<DiffResult<T>> {
+ const prefixCommon = createCommon(A, B);
+ const suffixCommon = createCommon(
+ A.slice(prefixCommon.length),
+ B.slice(prefixCommon.length),
+ true
+ ).reverse();
+ A = suffixCommon.length
+ ? A.slice(prefixCommon.length, -suffixCommon.length)
+ : A.slice(prefixCommon.length);
+ B = suffixCommon.length
+ ? B.slice(prefixCommon.length, -suffixCommon.length)
+ : B.slice(prefixCommon.length);
+ const swapped = B.length > A.length;
+ [A, B] = swapped ? [B, A] : [A, B];
+ const M = A.length;
+ const N = B.length;
+ if (!M && !N && !suffixCommon.length && !prefixCommon.length) return [];
+ if (!N) {
+ return [
+ ...prefixCommon.map(c => ({ type: "common" as DiffType, value: c })),
+ ...A.map(a => ({
+ type: (swapped ? "added" : "removed") as DiffType,
+ value: a
+ })),
+ ...suffixCommon.map(c => ({ type: "common" as DiffType, value: c }))
+ ];
+ }
+ const offset = N;
+ const delta = M - N;
+ const size = M + N + 1;
+ const fp = new Array(size).fill({ y: -1 });
+ // INFO: This buffer is used to save memory and improve performance.
+ // The first half is used to save route and last half is used to save diff type.
+ // This is because, when I kept new uint8array area to save type, performance worsened.
+ const routes = new Uint32Array((M * N + size + 1) * 2);
+ const diffTypesPtrOffset = routes.length / 2;
+ let ptr = 0;
+ let p = -1;
+
function backTrace<T>(
A: T[],
B: T[],
current: FarthestPoint,
swapped: boolean
- ) {
+ ): Array<{
+ type: DiffType;
+ value: T;
+ }> {
const M = A.length;
const N = B.length;
const result = [];
@@ -74,8 +116,7 @@ export default function diff<T>(A: T[], B: T[]): DiffResult<T>[] {
slide: FarthestPoint,
down: FarthestPoint,
k: number,
- M: number,
- N: number
+ M: number
): FarthestPoint {
if (slide && slide.y === -1 && (down && down.y === -1))
return { y: 0, id: 0 };
@@ -102,14 +143,14 @@ export default function diff<T>(A: T[], B: T[]): DiffResult<T>[] {
k: number,
slide: FarthestPoint,
down: FarthestPoint,
- offset: number,
+ _offset: number,
A: T[],
B: T[]
- ) {
+ ): FarthestPoint {
const M = A.length;
const N = B.length;
- if (k < -N || M < k) return { y: -1 };
- const fp = createFP(slide, down, k, M, N);
+ if (k < -N || M < k) return { y: -1, id: -1 };
+ const fp = createFP(slide, down, k, M);
while (fp.y + k < M && fp.y < N && A[fp.y + k] === B[fp.y]) {
const prev = fp.id;
ptr++;
@@ -121,44 +162,6 @@ export default function diff<T>(A: T[], B: T[]): DiffResult<T>[] {
return fp;
}
- const prefixCommon = createCommon(A, B);
- const suffixCommon = createCommon(
- A.slice(prefixCommon.length),
- B.slice(prefixCommon.length),
- true
- ).reverse();
- A = suffixCommon.length
- ? A.slice(prefixCommon.length, -suffixCommon.length)
- : A.slice(prefixCommon.length);
- B = suffixCommon.length
- ? B.slice(prefixCommon.length, -suffixCommon.length)
- : B.slice(prefixCommon.length);
- const swapped = B.length > A.length;
- [A, B] = swapped ? [B, A] : [A, B];
- const M = A.length;
- const N = B.length;
- if (!M && !N && !suffixCommon.length && !prefixCommon.length) return [];
- if (!N) {
- return [
- ...prefixCommon.map(c => ({ type: "common" as DiffType, value: c })),
- ...A.map(a => ({
- type: (swapped ? "added" : "removed") as DiffType,
- value: a
- })),
- ...suffixCommon.map(c => ({ type: "common" as DiffType, value: c }))
- ];
- }
- const offset = N;
- const delta = M - N;
- const size = M + N + 1;
- const fp = new Array(size).fill({ y: -1 });
- // INFO: This buffer is used to save memory and improve performance.
- // The first half is used to save route and last half is used to save diff type.
- // This is because, when I kept new uint8array area to save type, performance worsened.
- const routes = new Uint32Array((M * N + size + 1) * 2);
- const diffTypesPtrOffset = routes.length / 2;
- let ptr = 0;
- let p = -1;
while (fp[delta + offset].y < N) {
p = p + 1;
for (let k = -p; k < delta; ++k) {