All files / chart-model/src fingerprint-delta.ts

89.67% Statements 304/339
75.19% Branches 197/262
95.45% Functions 42/44
89.78% Lines 290/323

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738                                                                                                                                              597x                 48472x       298551x       181277x       632x       3788x           74333x       60510x                           20x       21482x       23584x   3886x   3454x   4760x   4070x   3454x   3960x                           701x 701x                 701x 649x 649x 649x     649x     701x 649x 649x   649x                       597x     37892x 37892x 15246x 15246x 15246x 3311x 3311x     15246x 15246x   37892x                                   37892x 37892x 37892x     37892x 37892x 34954x   37892x 37892x         6341x     37892x 37892x 21912x 21912x 21912x     21912x 21912x     37892x 61784x 61784x 61784x 6264x   55520x 55520x               77432x 53289x 37892x 71170x 55522x   71170x 15650x           3935x           2132x             597x 597x                                             20198x 20198x                             20198x 20198x 20198x 79707x 79707x 8480x 8480x   71227x     71227x 855x 855x 20x   835x   855x   71227x 71227x 71227x 855x   70372x   81463x           81463x 81463x 81463x 81463x 48756x   32707x   81463x     20198x 1001x 1001x 5021x 3761x       20198x 20198x 32707x 32707x   20198x                   8480x 8480x 8480x 8480x 3758x 3758x 3758x 3758x             3758x     3758x   1326x 1326x 1326x   602x 602x 602x 602x 602x     636x 636x 636x   1194x 1194x 1194x       4722x   478x 478x 478x   478x               832x 832x 832x   832x               1033x 1033x 1033x   1033x               678x 678x 678x   678x   499x 499x 499x 499x 499x   499x   499x     6x 6x 6x   6x   492x 492x 492x   492x   690x 690x 690x   690x   14x                   22536x 22536x 2338x   20198x 20198x 14x     67678x 22536x 22536x 22536x 40320x 31746x               8574x 406x 8168x 5740x 5740x               2428x 701x 701x 701x             1727x 1727x 10362x 1573x         20184x     13768x 13768x 2421x   13768x 13768x 7349x 7349x 2421x   4928x     13768x       34122x 34122x 34122x 34122x 34122x     34122x 1243x   34122x 34122x             597x               102366x   102366x 102366x 16139x   102366x 102366x 102366x 102366x 145496x 145496x 145496x 16139x 16139x   129357x 129357x     102366x     102366x 66808x 66808x   102366x 102366x       62096x 62096x 40446x 109212x   109212x   62096x 596x 706x       62096x 2692x 4194x       62096x 824x 978x 154x 978x   62096x       31048x 31048x 31048x 31048x 28264x   31048x 13770x   31048x 31048x 186288x 2102x     31048x 1652x   31048x       11806x 21485x 104x 645x   160x 160x           5903x 5903x 5903x 61x   5903x     5903x 131x   5903x 69x   5903x     5903x 65x   5903x    
import { parseHDXLongNoteMode } from './hdx-long-note-mode.ts';
import { isChartPlayOptionDeclaration } from './chart-play-options.ts';
/**
 * Deltas of the chart fingerprint (`docs/fingerprint-spec.md`, section 3.6.2): what the lines one `#RANDOM` / `#SWITCH`
 * outcome applies change in the content outside control flow, and how deltas are applied, formatted and read back.
 */
import {
  normalizeChannel,
  normalizeObjectKey,
  type BeMusicEvent,
  type BeMusicJSON,
  type BMSControlFlowEntry,
  type BMSControlFlowHeaderEntry,
} from '@be-music/chart-source';
import { formatRational, type Rational } from './fingerprint-rational.ts';
import {
  bucketEvents,
  bucketOfChannel,
  compareMineItems,
  compareNoteItems,
  compareTimelineItems,
  computeChartItems,
  formatJudge,
  formatMine,
  formatNote,
  formatTimeline,
  formatTotal,
  HEADER_BUCKET,
  LENGTHS_BUCKET,
  MINES_BUCKET,
  parseJudge,
  parseMine,
  parseNote,
  parseRational,
  parseTimeline,
  parseTotal,
  type ChartItems,
  type LaneNotesCache,
  type FingerprintLength,
  type FingerprintTimelineType,
  type FlatItems,
  type FpTime,
  type HeaderItems,
  type MineItem,
  type NoteItem,
  type TimelineItem,
} from './fingerprint-items.ts';
import {
  type FingerprintBranch,
  type FingerprintDeltaLists,
  type FingerprintHeaderChange,
  canonicalizeJSONValue,
} from './fingerprint-document.ts';
 
// ---------------------------------------------------------------------------------------------------------------------
// Deltas: what one outcome of a factor changes in the content outside control flow
// ---------------------------------------------------------------------------------------------------------------------
 
export interface Keyed<T> {
  key: string;
  item: T;
}
 
interface DeltaLists {
  notes: Array<Keyed<NoteItem>>;
  mines: Array<Keyed<MineItem>>;
  timeline: Array<Keyed<TimelineItem>>;
  lengths: Array<Keyed<[number, Rational]>>;
}
 
type HeaderField = 'bpm' | 'baseBPM' | 'judge' | 'total' | 'player3' | 'playOptions';
export const HEADER_FIELDS: ReadonlyArray<HeaderField> = ['bpm', 'baseBPM', 'judge', 'total', 'player3', 'playOptions'];
 
export interface ItemDelta {
  add: DeltaLists;
  remove: DeltaLists;
  header: Partial<HeaderItems>;
}
 
export function emptyLists(): DeltaLists {
  return { notes: [], mines: [], timeline: [], lengths: [] };
}
 
export function timeKey(time: FpTime): string {
  return `${time.m}:${time.n}/${time.d}`;
}
 
function noteKey(note: NoteItem): string {
  return `${timeKey(note.t)}|${note.lane}|${note.end ? timeKey(note.end) : ''}|${note.ln ?? ''}|${note.fz ?? ''}`;
}
 
function mineKey(mine: MineItem): string {
  return `${timeKey(mine.t)}|${mine.lane}|${formatRational(mine.damage)}`;
}
 
function timelineKey(item: TimelineItem): string {
  return `${item.type}|${timeKey(item.t)}|${formatRational(item.v)}`;
}
 
// Field-by-field equality that implies equal keys, so `diffKeyedAgainstTop` can find the runs an outcome shares with the
// top without building a key for every item of the lane.
function sameTime(a: FpTime, b: FpTime): boolean {
  return a.m === b.m && a.n === b.n && a.d === b.d;
}
 
function sameNote(a: NoteItem, b: NoteItem): boolean {
  return (
    a.lane === b.lane &&
    sameTime(a.t, b.t) &&
    (a.end === undefined ? b.end === undefined : b.end !== undefined && sameTime(a.end, b.end)) &&
    a.ln === b.ln &&
    a.fz === b.fz
  );
}
 
function sameMine(a: MineItem, b: MineItem): boolean {
  return a.lane === b.lane && sameTime(a.t, b.t) && a.damage.n === b.damage.n && a.damage.d === b.damage.d;
}
 
function sameTimelineItem(a: TimelineItem, b: TimelineItem): boolean {
  return a.type === b.type && sameTime(a.t, b.t) && a.v.n === b.v.n && a.v.d === b.v.d;
}
 
export function headerFieldText(header: Partial<HeaderItems>, field: HeaderField): string {
  return canonicalizeJSONValue(formatHeaderField(header, field));
}
 
function formatHeaderField(header: Partial<HeaderItems>, field: HeaderField): unknown {
  switch (field) {
    case 'bpm':
      return header.bpm === undefined ? undefined : formatRational(header.bpm);
    case 'baseBPM':
      return header.baseBPM === undefined ? undefined : header.baseBPM === null ? null : formatRational(header.baseBPM);
    case 'judge':
      return header.judge === undefined ? undefined : formatJudge(header.judge);
    case 'total':
      return header.total === undefined ? undefined : formatTotal(header.total);
    case 'player3':
      return header.player3;
    case 'playOptions':
      return header.playOptions === undefined ? undefined : { ...header.playOptions };
    default:
      return undefined;
  }
}
 
/** Multiset difference `after - before` and `before - after` of two keyed lists. */
function diffKeyed<T>(
  before: ReadonlyArray<T>,
  after: ReadonlyArray<T>,
  key: (item: T) => string,
  add: Array<Keyed<T>>,
  remove: Array<Keyed<T>>,
): void {
  const counts = new Map<string, { item: T; count: number }>();
  for (const item of before) {
    const k = key(item);
    const entry = counts.get(k);
    if (entry) {
      entry.count -= 1;
    } else {
      counts.set(k, { item, count: -1 });
    }
  }
  for (const item of after) {
    const k = key(item);
    const entry = counts.get(k);
    Iif (entry) {
      entry.count += 1;
    } else {
      counts.set(k, { item, count: 1 });
    }
  }
  for (const [k, entry] of counts) {
    for (let index = 0; index < entry.count; index += 1) {
      add.push({ key: k, item: entry.item });
    }
    for (let index = 0; index > entry.count; index -= 1) {
      remove.push({ key: k, item: entry.item });
    }
  }
}
 
/** The keys of a top-level list and where each key first appears, worked out once per list. */
interface KeyedTop {
  keys: string[];
  first: Map<string, number>;
}
 
const KEYED_TOPS = new WeakMap<ReadonlyArray<unknown>, KeyedTop>();
 
function keyedTop<T>(list: ReadonlyArray<T>, key: (item: T) => string): KeyedTop {
  let keyed = KEYED_TOPS.get(list);
  if (keyed === undefined) {
    const keys = list.map(key);
    const first = new Map<string, number>();
    for (const [index, itemKey] of keys.entries()) {
      Eif (!first.has(itemKey)) {
        first.set(itemKey, index);
      }
    }
    keyed = { keys, first };
    KEYED_TOPS.set(list, keyed);
  }
  return keyed;
}
 
/**
 * `diffKeyed` against a top-level list that every outcome is diffed against: the top's keys are worked out once, and
 * the runs both lists share at their start and end (an outcome usually changes one stretch of a lane) cancel out, so
 * only the stretch between them is counted. The result is `diffKeyed`'s, in its order: keys the top holds by where the
 * top first holds them, then the outcome's own keys by where it first holds them, each with the item `diffKeyed` would
 * take (the top's first for a key the top holds).
 */
function diffKeyedAgainstTop<T>(
  before: ReadonlyArray<T>,
  after: ReadonlyArray<T>,
  key: (item: T) => string,
  same: (a: T, b: T) => boolean,
  add: Array<Keyed<T>>,
  remove: Array<Keyed<T>>,
): void {
  const top = keyedTop(before, key);
  const beforeLength = before.length;
  const afterLength = after.length;
  // The shared runs are found by `same`, which implies equal keys: a pair it misses has equal keys and cancels out in
  // the counts below, so only the outcome's changed stretch needs keys.
  let prefix = 0;
  while (prefix < beforeLength && prefix < afterLength && same(before[prefix]!, after[prefix]!)) {
    prefix += 1;
  }
  let suffix = 0;
  while (
    suffix < beforeLength - prefix &&
    suffix < afterLength - prefix &&
    same(before[beforeLength - 1 - suffix]!, after[afterLength - 1 - suffix]!)
  ) {
    suffix += 1;
  }
 
  const counts = new Map<string, { item: T; count: number; order: number }>();
  for (let index = prefix; index < beforeLength - suffix; index += 1) {
    const itemKey = top.keys[index]!;
    const entry = counts.get(itemKey);
    Iif (entry) {
      entry.count -= 1;
    } else {
      const first = top.first.get(itemKey)!;
      counts.set(itemKey, { item: before[first]!, count: -1, order: first });
    }
  }
  for (let index = prefix; index < afterLength - suffix; index += 1) {
    const itemKey = key(after[index]!);
    const entry = counts.get(itemKey);
    if (entry) {
      entry.count += 1;
    } else {
      const first = top.first.get(itemKey);
      counts.set(
        itemKey,
        first === undefined
          ? { item: after[index]!, count: 1, order: beforeLength + index }
          : { item: before[first]!, count: 1, order: first },
      );
    }
  }
  const changed = [...counts].filter(([, entry]) => entry.count !== 0);
  changed.sort((left, right) => left[1].order - right[1].order);
  for (const [itemKey, entry] of changed) {
    for (let index = 0; index < entry.count; index += 1) {
      add.push({ key: itemKey, item: entry.item });
    }
    for (let index = 0; index > entry.count; index -= 1) {
      remove.push({ key: itemKey, item: entry.item });
    }
  }
}
 
function isEmptyLists(lists: DeltaLists): boolean {
  return (
    lists.notes.length === 0 && lists.mines.length === 0 && lists.timeline.length === 0 && lists.lengths.length === 0
  );
}
 
export function isEmptyDelta(delta: ItemDelta): boolean {
  return isEmptyLists(delta.add) && isEmptyLists(delta.remove) && Object.keys(delta.header).length === 0;
}
 
// ---------------------------------------------------------------------------------------------------------------------
// Replaying captured lines onto the content outside control flow
// ---------------------------------------------------------------------------------------------------------------------
 
export const INDEXED_DEFINITION_PATTERN = /^(BPM|STOP|EXRANK|SCROLL|SPEED)([0-9A-Za-z]{2})$/;
const ALL_LANES = '*lanes';
 
export interface ReplayContext {
  /** Measures at or after this are past every included event of every outcome (spec 3.6.6). */
  measureLimit: number;
  top: BeMusicJSON;
  topBuckets: Map<string, BeMusicEvent[]>;
  topItems: ChartItems;
  base: 36 | 62;
  /** Lane notes shared by the outcomes that change only events ({@link LaneNotesCache}). */
  laneCache?: LaneNotesCache;
}
 
/**
 * The chart for one outcome, as the parser's `resolveControlFlow` builds it: the content outside control flow, then the
 * applied lines in file order (headers re-applied with the parser's rules, objects appended). Returns the buckets the
 * lines touch.
 */
function replayEntries(
  context: ReplayContext,
  entries: ReadonlyArray<BMSControlFlowEntry>,
  applied: ReadonlyArray<number>,
): { json: BeMusicJSON; buckets: Map<string, BeMusicEvent[]>; touched: Set<string> } {
  const { top, base } = context;
  const json: BeMusicJSON = {
    ...top,
    metadata: { ...top.metadata },
    resources: { ...top.resources, bpm: { ...top.resources.bpm }, stop: { ...top.resources.stop } },
    measures: top.measures.map((measure) => ({ index: measure.index, length: measure.length })),
    bms: {
      ...top.bms,
      lnObjs: [...(top.bms.lnObjs ?? [])],
      ...(top.bms.options !== undefined ? { options: [...top.bms.options] } : {}),
      exRank: { ...top.bms.exRank },
      scroll: { ...top.bms.scroll },
      speed: { ...top.bms.speed },
      stp: [...top.bms.stp],
    },
  };
  const touched = new Set<string>();
  const added = new Map<string, BeMusicEvent[]>();
  for (const index of applied) {
    const entry = entries[index]!;
    if (entry.kind === 'header') {
      applyFingerprintHeader(json, entry, base, touched);
      continue;
    }
    Iif (entry.kind !== 'object') {
      continue;
    }
    if (typeof entry.measureLength === 'number' && entry.measureLength > 0) {
      const existing = json.measures.find((measure) => measure.index === entry.measure);
      if (existing) {
        existing.length = entry.measureLength;
      } else {
        json.measures.push({ index: entry.measure, length: entry.measureLength });
      }
      touched.add(LENGTHS_BUCKET);
    }
    const channel = normalizeChannel(entry.channel);
    const bucket = bucketOfChannel(channel);
    if (bucket === undefined) {
      continue;
    }
    for (const event of entry.events) {
      // Optional fields are assigned, not spread, to keep the hidden classes of parsed events.
      const replayed: BeMusicEvent = {
        measure: entry.measure,
        channel,
        position: event.position,
        value: normalizeObjectKey(event.value, base),
      };
      if (event.bmsLineOrder !== undefined) replayed.bmsLineOrder = event.bmsLineOrder;
      Iif (event.bmson) replayed.bmson = event.bmson;
      const list = added.get(bucket);
      if (list) {
        list.push(replayed);
      } else {
        added.set(bucket, [replayed]);
      }
      touched.add(bucket);
    }
  }
  if (touched.has(ALL_LANES)) {
    touched.delete(ALL_LANES);
    for (const bucket of [...context.topBuckets.keys(), ...added.keys()]) {
      if (bucket.startsWith('lane:')) {
        touched.add(bucket);
      }
    }
  }
  const buckets = new Map(context.topBuckets);
  for (const [bucket, events] of added) {
    const merged = [...(buckets.get(bucket) ?? []), ...events];
    buckets.set(bucket, bucketEvents(merged).get(bucket) ?? []);
  }
  return { json, buckets, touched };
}
 
/** Re-applies a captured header line, limited to the headers the fingerprint reads, with the parser's rules. */
function applyFingerprintHeader(
  json: BeMusicJSON,
  entry: BMSControlFlowHeaderEntry,
  base: 36 | 62,
  touched: Set<string>,
): void {
  const { command, value } = entry;
  const numeric = Number.parseFloat(value);
  const indexed = INDEXED_DEFINITION_PATTERN.exec(command);
  if (indexed) {
    const directive = indexed[1]!;
    const keyRaw = base === 62 ? (entry.commandRaw ?? command).slice(directive.length) : indexed[2]!;
    const key = normalizeObjectKey(keyRaw, base);
    Iif (directive === 'EXRANK') {
      if (value.length > 0) {
        json.bms.exRank[key] = value;
        touched.add('tl:exrank');
      }
      return;
    }
    Iif (!Number.isFinite(numeric)) {
      return;
    }
    switch (directive) {
      case 'BPM':
        json.resources.bpm[key] = numeric;
        touched.add('tl:bpm');
        return;
      case 'STOP': {
        const units = Math.max(-0x80000000, Math.min(0x7fffffff, Number.parseInt(value, 10) || 0));
        Iif (units <= 0 && (json.resources.stop[key] ?? 0) > 0) return;
        json.resources.stop[key] = units;
        touched.add('tl:stop');
        return;
      }
      case 'SCROLL':
        json.bms.scroll[key] = numeric;
        touched.add('tl:scroll');
        return;
      default:
        json.bms.speed[key] = numeric;
        touched.add('tl:speed');
        return;
    }
  }
 
  switch (command) {
    case 'BPM':
      Eif (Number.isFinite(numeric) && numeric !== 0) {
        json.metadata.bpm = numeric;
        touched.add(HEADER_BUCKET);
      }
      return;
    case 'BASEBPM':
      if (Number.isFinite(numeric) && numeric > 0) {
        json.bms.baseBPM = numeric;
        touched.add(HEADER_BUCKET);
      }
      return;
    case 'RANK':
      Eif (Number.isFinite(numeric)) {
        json.metadata.rank = numeric;
        touched.add(HEADER_BUCKET);
      }
      return;
    case 'DEFEXRANK':
      if (Number.isFinite(numeric)) {
        json.bms.defExRank = numeric;
        touched.add(HEADER_BUCKET);
      }
      return;
    case 'TOTAL':
      Eif (Number.isFinite(numeric)) {
        json.metadata.total = numeric;
        touched.add(HEADER_BUCKET);
      }
      return;
    case 'PLAYER':
      if (Number.isFinite(numeric) && numeric > 0) {
        json.bms.player = Math.floor(numeric);
        touched.add(HEADER_BUCKET);
      }
      return;
    case 'LNTYPE':
      Eif (Number.isFinite(numeric) && numeric > 0) {
        json.bms.lnType = Math.floor(numeric);
        touched.add(ALL_LANES);
      }
      return;
    case 'OPTION': {
      const longNoteMode = parseHDXLongNoteMode(value) !== undefined;
      Eif (longNoteMode || isChartPlayOptionDeclaration(value)) {
        const declarations = (json.bms.options ??= json.bms.option ? [json.bms.option] : []);
        declarations.push(value);
        json.bms.option = value;
        // An HDX LONGMODE changes the long notes; a gameplay option, the header's `playOptions`.
        touched.add(longNoteMode ? ALL_LANES : HEADER_BUCKET);
      }
      return;
    }
    case 'LNMODE':
      Eif (Number.isFinite(numeric) && numeric > 0) {
        json.bms.lnMode = Math.floor(numeric);
        touched.add(ALL_LANES);
      }
      return;
    case 'LNOBJ':
      Eif (value.length > 0) {
        json.bms.lnObjs = [...(json.bms.lnObjs ?? []), normalizeObjectKey(value, base)];
        touched.add(ALL_LANES);
      }
      return;
    case 'STP':
      Eif (value.length > 0) {
        json.bms.stp.push(value);
        touched.add('tl:stp');
      }
      return;
    default:
      return;
  }
}
 
/** What the lines `applied` change in the content outside control flow. */
export function computeDelta(
  context: ReplayContext,
  entries: ReadonlyArray<BMSControlFlowEntry>,
  applied: ReadonlyArray<number>,
): ItemDelta {
  const delta: ItemDelta = { add: emptyLists(), remove: emptyLists(), header: {} };
  if (applied.length === 0) {
    return delta;
  }
  const { json, buckets, touched } = replayEntries(context, entries, applied);
  if (touched.size === 0) {
    return delta;
  }
  // The lane cache holds only for outcomes whose chart differs from the top-level one in its events.
  const eventsOnly = !touched.has(LENGTHS_BUCKET) && applied.every((index) => entries[index]!.kind !== 'header');
  const items = computeChartItems(json, buckets, touched, context.topItems, eventsOnly ? context.laneCache : undefined);
  const top = context.topItems;
  for (const bucket of touched) {
    if (bucket.startsWith('lane:')) {
      diffKeyedAgainstTop(
        top.lanes.get(bucket) ?? [],
        items.lanes.get(bucket) ?? [],
        noteKey,
        sameNote,
        delta.add.notes,
        delta.remove.notes,
      );
    } else if (bucket === MINES_BUCKET) {
      diffKeyedAgainstTop(top.mines, items.mines, mineKey, sameMine, delta.add.mines, delta.remove.mines);
    } else if (bucket.startsWith('tl:')) {
      const type = bucket.slice(3) as FingerprintTimelineType;
      diffKeyedAgainstTop(
        top.timeline.get(type) ?? [],
        items.timeline.get(type) ?? [],
        timelineKey,
        sameTimelineItem,
        delta.add.timeline,
        delta.remove.timeline,
      );
    } else if (bucket === LENGTHS_BUCKET) {
      const lengthKey = ([measure, length]: [number, Rational]): string => `${measure}|${formatRational(length)}`;
      const below = ([measure]: [number, Rational]): boolean => measure < context.measureLimit;
      diffKeyed(
        [...top.lengths.entries()].filter(below),
        [...items.lengths.entries()].filter(below),
        lengthKey,
        delta.add.lengths,
        delta.remove.lengths,
      );
    } else Eif (bucket === HEADER_BUCKET) {
      for (const field of HEADER_FIELDS) {
        if (headerFieldText(items.header, field) !== headerFieldText(top.header, field)) {
          (delta.header as Record<string, unknown>)[field] = items.header[field];
        }
      }
    }
  }
  return delta;
}
export function subtractKeyed<T>(list: ReadonlyArray<Keyed<T>>, remove: ReadonlyArray<Keyed<T>>): Array<Keyed<T>> {
  const counts = new Map<string, number>();
  for (const entry of remove) {
    counts.set(entry.key, (counts.get(entry.key) ?? 0) + 1);
  }
  const kept: Array<Keyed<T>> = [];
  for (const entry of list) {
    const count = counts.get(entry.key) ?? 0;
    if (count > 0) {
      counts.set(entry.key, count - 1);
    } else {
      kept.push(entry);
    }
  }
  return kept;
}
 
export function applyDelta(top: FlatItems, delta: ItemDelta): void {
  top.notes = applyKeyed(top.notes, noteKey, delta.remove.notes, delta.add.notes);
  top.mines = applyKeyed(top.mines, mineKey, delta.remove.mines, delta.add.mines);
  top.timeline = applyKeyed(top.timeline, timelineKey, delta.remove.timeline, delta.add.timeline);
  const lengths = new Map(top.lengths);
  for (const { item } of delta.remove.lengths) {
    lengths.delete(item[0]);
  }
  for (const { item } of delta.add.lengths) {
    lengths.set(item[0], item[1]);
  }
  top.lengths = lengths;
  top.header = { ...top.header, ...delta.header };
}
 
/**
 * The keys of a list `applyKeyed` returned, so the next delta applied to it (one per factor, `hoistCommon`) reuses them
 * instead of keying every item of the chart again. The lists are never changed in place.
 */
const APPLIED_KEYS = new WeakMap<ReadonlyArray<unknown>, readonly string[]>();
 
function applyKeyed<T>(
  list: ReadonlyArray<T>,
  key: (item: T) => string,
  remove: ReadonlyArray<Keyed<T>>,
  add: ReadonlyArray<Keyed<T>>,
): T[] {
  const keys = APPLIED_KEYS.get(list) ?? list.map(key);
  // `subtractKeyed`: each removed key takes out its earliest remaining item.
  const counts = new Map<string, number>();
  for (const entry of remove) {
    counts.set(entry.key, (counts.get(entry.key) ?? 0) + 1);
  }
  const items: T[] = [];
  const itemKeys: string[] = [];
  let removed = 0;
  for (let index = 0; index < list.length; index += 1) {
    const itemKey = keys[index]!;
    const count = counts.get(itemKey) ?? 0;
    if (count > 0) {
      counts.set(itemKey, count - 1);
      removed += 1;
    } else {
      items.push(list[index]!);
      itemKeys.push(itemKey);
    }
  }
  Iif (removed !== remove.length) {
    throw new Error('fingerprint: a removed item is not present');
  }
  for (const entry of add) {
    items.push(entry.item);
    itemKeys.push(entry.key);
  }
  APPLIED_KEYS.set(items, itemKeys);
  return items;
}
 
function formatDeltaLists(lists: DeltaLists): FingerprintDeltaLists {
  const formatted: FingerprintDeltaLists = {};
  if (lists.notes.length > 0) {
    formatted.notes = lists.notes
      .map((entry) => entry.item)
      .sort(compareNoteItems)
      .map((note) => formatNote(note, 'raw'));
  }
  if (lists.mines.length > 0) {
    formatted.mines = lists.mines
      .map((entry) => entry.item)
      .sort(compareMineItems)
      .map(formatMine);
  }
  if (lists.timeline.length > 0) {
    formatted.timeline = lists.timeline
      .map((entry) => entry.item)
      .sort(compareTimelineItems)
      .map(formatTimeline);
  }
  if (lists.lengths.length > 0) {
    formatted.lengths = lists.lengths
      .map((entry) => entry.item)
      .sort((left, right) => left[0] - right[0])
      .map(([measure, length]): FingerprintLength => [String(measure), formatRational(length)]);
  }
  return formatted;
}
 
export function formatDelta(delta: ItemDelta): Omit<FingerprintBranch, 'values' | 'next'> {
  const formatted: Omit<FingerprintBranch, 'values' | 'next'> = {};
  const add = formatDeltaLists(delta.add);
  const remove = formatDeltaLists(delta.remove);
  if (Object.keys(add).length > 0) {
    formatted.add = add;
  }
  if (Object.keys(remove).length > 0) {
    formatted.remove = remove;
  }
  const header: FingerprintHeaderChange = {};
  for (const field of HEADER_FIELDS) {
    if (field in delta.header) {
      (header as Record<string, unknown>)[field] = formatHeaderField(delta.header, field);
    }
  }
  if (Object.keys(header).length > 0) {
    formatted.header = header;
  }
  return formatted;
}
 
function parseDeltaLists(lists: FingerprintDeltaLists | undefined): DeltaLists {
  return {
    notes: (lists?.notes ?? []).map(parseNote).map((item) => ({ key: noteKey(item), item })),
    mines: (lists?.mines ?? []).map(parseMine).map((item) => ({ key: mineKey(item), item })),
    timeline: (lists?.timeline ?? []).map(parseTimeline).map((item) => ({ key: timelineKey(item), item })),
    lengths: (lists?.lengths ?? []).map(([measure, length]) => {
      const item: [number, Rational] = [Number(measure), parseRational(length)];
      return { key: `${item[0]}|${length}`, item };
    }),
  };
}
 
export function parseDelta(branch: FingerprintBranch): ItemDelta {
  const header: Partial<HeaderItems> = {};
  const change = branch.header ?? {};
  if (change.bpm !== undefined) {
    header.bpm = parseRational(change.bpm);
  }
  Iif (change.baseBPM !== undefined) {
    header.baseBPM = change.baseBPM === null ? null : parseRational(change.baseBPM);
  }
  if (change.judge !== undefined) {
    header.judge = parseJudge(change.judge);
  }
  if (change.total !== undefined) {
    header.total = parseTotal(change.total);
  }
  Iif (change.player3 !== undefined) {
    header.player3 = change.player3;
  }
  if (change.playOptions !== undefined) {
    header.playOptions = { ...change.playOptions };
  }
  return { add: parseDeltaLists(branch.add), remove: parseDeltaLists(branch.remove), header };
}