| 520 | // as a patience diff finds them: what a module declares or imports is mostly |
| 521 | // named once, so the runs between them stay short however long the module |
| 522 | const anchorsOf = ( |
| 523 | base: string[], |
| 524 | other: string[], |
| 525 | b0: number, |
| 526 | b1: number, |
| 527 | o0: number, |
| 528 | o1: number |
| 529 | ): [at: number, otherAt: number][] => { |
| 530 | // its index in each run, -1 once it's there twice |
| 531 | const found = new Map<string, [at: number, otherAt: number]>() |
| 532 | for (let i = b0; i < b1; i++) { |
| 533 | const known = found.get(base[i]) |
| 534 | if (known) known[0] = -1 |
| 535 | else found.set(base[i], [i, -2]) |
| 536 | } |
| 537 | for (let i = o0; i < o1; i++) { |
| 538 | const known = found.get(other[i]) |
| 539 | if (known) known[1] = known[1] === -2 ? i : -1 |
| 540 | } |
| 541 | const pairs = [...found.values()].filter( |
| 542 | ([at, otherAt]) => at >= 0 && otherAt >= 0 |
| 543 | ) |
| 544 | // the longest run of them rising in the other list, already rising in the base |
| 545 | const tails: number[] = [] |
| 546 | const previous: number[] = [] |
| 547 | for (const [i, [, otherAt]] of pairs.entries()) { |
| 548 | let [low, high] = [0, tails.length] |
| 549 | while (low < high) { |
| 550 | const middle = (low + high) >> 1 |
| 551 | if (pairs[tails[middle]][1] < otherAt) low = middle + 1 |
| 552 | else high = middle |
| 553 | } |
| 554 | previous[i] = low ? tails[low - 1] : -1 |
| 555 | tails[low] = i |
| 556 | } |
| 557 | const anchors: [number, number][] = [] |
| 558 | for ( |
| 559 | let i = tails.length ? tails[tails.length - 1] : -1; |
| 560 | i >= 0; |
| 561 | i = previous[i] |
| 562 | ) |
| 563 | anchors.push(pairs[i]) |
| 564 | return anchors.reverse() |
| 565 | } |
| 566 | |
| 567 | // the hunks of two short runs, by their longest common subsequence |
| 568 | const compareEach = ( |