In this section
- Iterations — 40 Same Name Same Body Page render · galaxies-wafers / iterations/40-same-name-same-body/index.html lines 1–92
- Snsb render · galaxies-wafers / iterations/40-same-name-same-body/snsb.css lines 1–68
- Worker render · galaxies-wafers / iterations/40-same-name-same-body/worker.js lines 1–232
Iterations — 40 Same Name Same Body Page
A web page in Galaxies Wafers that reads inputs for name. Published at https://ventusltd.github.io/galaxies-wafers/iterations/40-same-name-same-body/.
rendergalaxies-wafers / iterations/40-same-name-same-body/index.html · lines 1–92 of 92 · Entry point
- What it does
- It draws its content. Side effects: changes the web page (DOM).
- Why it exists in the grid workflow
- README: "The page (`index.html`, `app.mjs`, `lib.mjs`) is inherited unchanged from testcode/202609151339."
- Inputs & outputs
- In: form fields (user input). Out: web page.
- Downstream impact
- It is itself an entry point, published at https://ventusltd.github.io/galaxies-wafers/iterations/40-same-name-same-body/.
- What it achieves
- The README says: The page (`index.html`, `app.mjs`, `lib.mjs`) is inherited unchanged from testcode/.
- Why it matters
- No other file in the estate was found to import it; it is an entry point.
- Application
- galaxies-wafers · iterations/40-same-name-same-body · page
- Uses
- Snsb (stylesheet), Galaxies Wafers — Iterations Page (link), Iterations — 40 Same Name Same Body (script src)
- Used by
- none found
- Produces
- none declared
- Defines here
- none
- Cannot conclude
- Whether this code runs, is used by a live app today, or is correct. This is a static reading.
- Evidence
- galaxies-wafers@5814793812add74605660fa821e6a39b8de80588 iterations/40-same-name-same-body/index.html:1-92 · blob 204887e4d3d2c6f9dc86b56dbbb1816e54829316 · GitHub at these lines
- State
- achieves: readme · context: readme_derived · stage: output · effects: dom · domain stage: unknown · users: none · canonical: unknown
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<meta name="color-scheme" content="dark">
<title>Same Name, Same Body?</title>
<link rel="icon" href="data:,">
<link rel="stylesheet" href="snsb.css">
</head>
<body>
<main>
<header>
<h1>SAME NAME, SAME BODY?</h1>
<p class="dim">Two kinds of repetition in the estate's code, kept apart. <b>Same name</b>: function families that carry one recorded name. <b>Same body</b>: families whose ordered sequence of permanent line numbers is identical. One line number is one distinct line of text, so an identical sequence is identical code in the same order. Everything below is counted in your browser from the number … [line shortened here: 432 characters; full line on GitHub]
</header>
<section class="layers" aria-label="Layers">
<label class="layer"><input type="checkbox" data-layer="compute"><span id="lbl-compute" data-base-label="Compare every family · families.json + lines.bin">Compare every family · families.json + lines.bin [WAIT]</span></label>
<label class="layer"><input type="checkbox" data-layer="ground"><span id="lbl-ground" data-base-label="Dark wafer ground · all-lines.bin">Dark wafer ground · all-lines.bin [WAIT]</span></label>
</section>
<div class="progress" aria-hidden="true"><div id="bar"></div></div>
<p id="phase" class="dim small">WAIT — nothing is fetched until you tick a layer. Tick <b>Compare every family</b> to begin.</p>
<h2>THE FOUR CASES</h2>
<div id="cases" class="cases">
<div class="case" data-c="0"><span class="sw c0"></span><span class="lab">same name, same body · true copies</span><span class="v" id="cv0">—</span><span class="track"><span class="fill c0" id="cf0"></span></span></div>
<div class="case" data-c="1"><span class="sw c1"></span><span class="lab">different names, same body · renamed copies</span><span class="v" id="cv1">—</span><span class="track"><span class="fill c1" id="cf1"></span></span></div>
<div class="case" data-c="2"><span class="sw c2"></span><span class="lab">same name, different bodies · a name hiding different code</span><span class="v" id="cv2">—</span><span class="track"><span class="fill c2" id="cf2"></span></span></div>
<div class="case" data-c="3"><span class="sw c3"></span><span class="lab">unique name, unique body</span><span class="v" id="cv3">—</span><span class="track"><span class="fill c3" id="cf3"></span></span></div>
<div class="case" data-c="4"><span class="sw c4"></span><span class="lab">no name recorded · "(anonymous)"</span><span class="v" id="cv4">—</span><span class="track"><span class="fill c4" id="cf4"></span></span></div>
</div>
<p class="dim small">Each family is counted once, in the first case that fits, in the order listed. "(anonymous)" is a placeholder for code with no name, so it cannot share a name and is counted apart. Bars are families, not verdicts.</p>
<p id="bodyline" class="summary">WAIT — tick Compare every family.</p>
<h2>THE EARLIER FINDING, CHECKED</h2>
<p class="dim small">An earlier estate note said one helper name had many distinct bodies across many sites while another had few bodies across hundreds of sites, and that ranking by how often a name appears is backwards for finding copies. Its figures are not typed here; this is what the data says now.</p>
<p id="finding" class="summary">WAIT — tick Compare every family.</p>
<h2>A NAME GROUP ON THE WAFER</h2>
<form id="pick" class="pick" autocomplete="off">
<input id="name" placeholder="a function name, exactly" aria-label="function name" autocapitalize="off" spellcheck="false">
<button type="submit">SHOW</button>
</form>
<div class="plane">
<canvas id="ground" aria-hidden="true"></canvas>
<canvas id="plane" aria-label="The wafer. Each distinct body of the selected name is lit in its own colour. Drag to pan, pinch or wheel to zoom, tap a lit line to read its body."></canvas>
<button type="button" id="fit" class="fit">fit</button>
<div id="drawn" class="drawn dim"></div>
</div>
<div id="legend" class="legend"><p class="dim small">Pick a name from a list below, or type one. Each distinct body gets its own colour.</p></div>
<div class="pager" id="legendpager" hidden>
<button type="button" id="lprev">◀ bodies</button><span id="lpage" class="dim small"></span><button type="button" id="lnext">bodies ▶</button>
</div>
<section id="card" class="card" aria-live="polite">
<p class="dim">Tap a body in the legend or a lit line on the wafer to read it.</p>
</section>
<h2>RANKED · TOP <span id="topn">40</span></h2>
<div class="seg" id="tabs" role="group" aria-label="Which ranking">
<button type="button" data-t="copied" aria-pressed="true">most copied bodies</button>
<button type="button" data-t="overloaded" aria-pressed="false">most overloaded names</button>
</div>
<p id="rankhow" class="dim small">WAIT — tick Compare every family.</p>
<ol id="rows" class="rows"></ol>
<div class="pager">
<button type="button" id="prev">◀ prev</button><span id="page" class="dim small">page — of —</span><button type="button" id="next">next ▶</button>
</div>
<details id="questions" class="questions">
<summary>Questions</summary>
<dl>
<dt>1. How does this help draw a system or a single-line diagram?</dt>
<dd id="q1">It draws no busbar, feeder, transformer, cable route, protection or earthing itself: none yet. What it guards is the drawing. A distance or voltage-drop function copied into two places and edited in only one will give two answers, and the same diagram drawn twice may disagree; a name shared by different bodies may hide different physics behind one word. Names in the estate that s … [line shortened here: 523 characters; full line on GitHub]
<dt>2. What is this code used for?</dt>
<dd id="q2">This page: <code>worker.js</code> <code>fnv</code> (hash to bucket), <code>sameSeq</code> (exact confirmation), <code>csr</code>, <code>intern</code>, <code>spearman</code>; drawn by <code>app.mjs</code>. It reads <code>families.json</code> fields <code>name</code>, <code>lineOffset</code>, <code>lineCount</code>, <code>files</code>, <code>repos</code>, <code>block</code>, <code> … [line shortened here: 653 characters; full line on GitHub]
<dt>3. Where does it lead next?</dt>
<dd id="q3">To a person reading two bodies side by side with the CODE button and deciding whether to consolidate them. Never automatically: identical text can serve different callers, and different text can be a deliberate fix. <span id="q3now">not yet known</span></dd>
</dl>
</details>
<p id="machine" class="machine small">Machine detail · not yet known: tick Compare every family.</p>
<footer class="dim small">
<p>Loading, as Grid Atlas does it: every layer starts unticked and unfetched; ticking fetches its files through one queue of at most 3 requests, each with a 15 s timeout; a file is fetched once and shared; a failed fetch is retried by ticking again; unticking hides and keeps the data. The comparison runs in a Web Worker, so the page scrolls while it counts. Each source is one whole file, so th … [line shortened here: 750 characters; full line on GitHub]
<p id="prov"></p>
<p><a href="../">every version</a></p>
</footer>
</main>
<script type="module" src="app.mjs"></script>
</body>
</html>Snsb
A style sheet in the iterations folder of Galaxies Wafers. Served with the page at https://ventusltd.github.io/galaxies-wafers/iterations/40-same-name-same-body/.
rendergalaxies-wafers / iterations/40-same-name-same-body/snsb.css · lines 1–68 of 68 · Entry point
- What it does
- Side effects: changes the web page (DOM).
- Downstream impact
- Used directly by 1 file: Iterations — 40 Same Name Same Body Page (galaxies-wafers, https://ventusltd.github.io/galaxies-wafers/iterations/40-same-name-same-body/). It is itself an entry point, published at https://ventusltd.github.io/galaxies-wafers/iterations/40-same-name-same-body/.
- What it achieves
- No statement of purpose was found in the file, a README line naming it, or its name.
- Why it matters
- Loaded by the 40 Same Name Same Body page at https://ventusltd.github.io/galaxies-wafers/iterations/40-same-name-same-body/.
- Application
- galaxies-wafers · iterations/40-same-name-same-body · style sheet
- Uses
- none found
- Used by
- loaded by Iterations — 40 Same Name Same Body Page (stylesheet)
- Produces
- none declared
- Defines here
- none
- Cannot conclude
- Whether this code runs, is used by a live app today, or is correct. This is a static reading.
- Evidence
- galaxies-wafers@5814793812add74605660fa821e6a39b8de80588 iterations/40-same-name-same-body/snsb.css:1-68 · blob e24aba0be5ca6f19e7a528b45e8a0ccf8d58b9c0 · GitHub at these lines
- State
- achieves: name · context: readme_derived · stage: output · effects: dom · domain stage: unknown · users: none · canonical: unknown
:root{--ink:#d8dee9;--dim:#8b93a7;--ground:#0b0d12;--edge:#1b2030;--lit:#ffd54a;--beam:#5ec8f2;
--c0:#5ec8f2;--c1:#ff7ad9;--c2:#ffd54a;--c3:#9aa3b8;--c4:#4a5163}
*{box-sizing:border-box}
html{-webkit-text-size-adjust:100%}
body{margin:0;background:var(--ground);color:var(--ink);font:13px/1.45 ui-monospace,SFMono-Regular,Menlo,Consolas,monospace;
padding:calc(env(safe-area-inset-top,0px) + .8rem) 1rem calc(env(safe-area-inset-bottom,0px) + 2rem)}
main{max-width:60rem;margin:0 auto}
h1{font-size:14px;letter-spacing:.18em;font-weight:600;margin:0 0 .35rem;color:#fff}
h2{font-size:11px;letter-spacing:.14em;font-weight:400;color:var(--dim);margin:1.3rem 0 .4rem}
p{margin:.35rem 0}
b{color:#fff}
.dim{color:var(--dim)} .small{font-size:11.5px}
a{color:var(--beam)}
code{color:var(--beam);overflow-wrap:anywhere}
.summary{border-left:3px solid var(--beam);padding:.4rem .6rem;background:#10131b;margin:.5rem 0;overflow-wrap:anywhere}
.layers{display:grid;gap:.2rem;margin:.6rem 0 .3rem}
.layer{display:flex;align-items:center;gap:.55rem;min-height:44px;padding:0 .2rem;border-bottom:1px solid var(--edge);cursor:pointer}
.layer input{width:20px;height:20px;accent-color:#d8dee9;flex:none}
.layer span{overflow-wrap:anywhere}
.progress{height:4px;background:#07080c;border:1px solid var(--edge);border-radius:2px;overflow:hidden}
#bar{height:100%;width:0;background:var(--beam);transition:width .15s linear}
.cases{display:grid;gap:.35rem}
.case{display:grid;grid-template-columns:12px 1fr auto;grid-template-rows:auto 8px;gap:.15rem .5rem;align-items:center}
.case .sw{width:12px;height:12px;border-radius:2px}
.case .lab{overflow-wrap:anywhere}
.case .v{color:#fff;font-variant-numeric:tabular-nums}
.case .track{grid-column:2/4;height:8px;background:#07080c;border:1px solid var(--edge);border-radius:2px;overflow:hidden}
.case .fill{display:block;height:100%;width:0}
.c0{background:var(--c0)}.c1{background:var(--c1)}.c2{background:var(--c2)}.c3{background:var(--c3)}.c4{background:var(--c4)}
.pick{display:flex;gap:.4rem;margin:.3rem 0}
.pick input{flex:1 1 0;min-width:0;padding:.55rem;background:#11151f;color:var(--ink);border:1px solid var(--edge);border-radius:6px;font:inherit;font-size:16px}
.pick button,.pager button,.seg button,.card button{font:inherit;color:var(--ink);background:#07080c;border:1px solid var(--edge);border-radius:.35rem;padding:.45rem .7rem;min-height:44px;cursor:pointer}
.pick button{color:var(--beam);border-color:var(--beam)}
.plane{position:relative;margin:.4rem -1rem 0;height:58vh;min-height:300px;max-height:760px;background:#030406;border-top:1px solid var(--edge);border-bottom:1px solid var(--edge)}
#ground,#plane{position:absolute;inset:0;display:block;width:100%;height:100%}
#ground{pointer-events:none}
#plane{touch-action:none;cursor:crosshair}
.fit{position:absolute;right:.6rem;top:.6rem;font:inherit;color:var(--ink);background:#0b0d12e6;border:1px solid var(--edge);border-radius:.35rem;min-width:44px;min-height:40px}
.drawn{position:absolute;left:.6rem;bottom:.35rem;right:.6rem;font-size:10.5px;pointer-events:none}
.legend{margin:.5rem 0 0}
.legend ul{list-style:none;margin:0;padding:0;display:grid;gap:.3rem;grid-template-columns:repeat(auto-fill,minmax(15rem,1fr))}
.legend button{all:unset;box-sizing:border-box;display:flex;gap:.5rem;align-items:center;width:100%;min-height:40px;padding:.3rem .5rem;border:1px solid var(--edge);border-radius:.35rem;background:#07080c;cursor:pointer;overflow-wrap:anywhere}
.legend button.on{border-color:#fff}
.legend .dot{width:12px;height:12px;border-radius:50%;flex:none;box-shadow:0 0 0 2px #000}
.pager{display:flex;align-items:center;justify-content:space-between;gap:.5rem;margin:.4rem 0}
.pager[hidden]{display:none}
.card{border:1px solid var(--edge);border-radius:.45rem;background:#10131b;padding:.55rem .7rem;margin:.8rem 0;overflow-wrap:anywhere}
.card h3{margin:0 0 .35rem;font-size:14px;color:#fff;font-weight:600}
.card dl{display:grid;grid-template-columns:minmax(6.5rem,auto) 1fr;gap:.25rem .7rem;margin:.4rem 0}
.card dt{color:var(--dim)} .card dd{margin:0}
.card .acts{display:flex;flex-wrap:wrap;gap:.4rem;margin-top:.5rem}
.card a.code{display:inline-flex;align-items:center;min-height:44px;padding:0 .9rem;border:1px solid var(--beam);border-radius:.35rem;color:var(--beam);text-decoration:none;letter-spacing:.1em}
.seg{display:flex;flex-wrap:wrap;gap:.35rem;margin:.4rem 0}
.seg button[aria-pressed=true]{border-color:#fff;box-shadow:inset 0 0 0 1px #fff}
.rows{list-style:none;margin:0;padding:0;display:grid;gap:.35rem}
.rows li[hidden]{display:none}
.rows button{all:unset;display:grid;grid-template-columns:2.2rem 1fr;gap:.1rem .5rem;box-sizing:border-box;width:100%;cursor:pointer;border:1px solid var(--edge);border-radius:.4rem;background:#07080c;padding:.45rem .55rem;min-height:44px;overflow-wrap:anywhere}
.rows button:focus-visible{outline:1px solid #fff}
.rows .r{color:var(--dim);grid-row:1/3}
.rows .t{color:#fff}
.rows .s{color:var(--dim);font-size:11.5px}
.questions{border:1px solid var(--edge);border-radius:.45rem;background:#10131b;padding:0 .65rem;margin:1.3rem 0 .5rem}
.questions summary{font-size:11px;letter-spacing:.14em;text-transform:uppercase;cursor:pointer;color:var(--beam);min-height:44px;display:flex;align-items:center}
.questions dt{color:#fff;margin-top:.5rem;font-size:12px}
.questions dd{margin:.2rem 0 .6rem;font-size:11.5px;overflow-wrap:anywhere}
.machine{border-left:3px solid var(--beam);padding:.35rem .6rem;background:#07080c;overflow-wrap:anywhere}
footer{margin-top:1.2rem;overflow-wrap:anywhere}
@media (min-width:900px){.plane{margin:.4rem 0 0;border:1px solid var(--edge);border-radius:.45rem;overflow:hidden}}Worker
A JavaScript file in the iterations folder of Galaxies Wafers that works with same seq (`sameSeq`). Served with the page at https://ventusltd.github.io/galaxies-wafers/iterations/40-same-name-same-body/.
rendergalaxies-wafers / iterations/40-same-name-same-body/worker.js · lines 1–232 of 232 · pattern intern (shared by 2 files)
- What it does
- It lists and searches its content. Defines 7 functions or classes, including fnv, sameSeq, csr, intern, ranks.
- Downstream impact
- Used directly by 1 file: Iterations — 40 Same Name Same Body (galaxies-wafers, https://ventusltd.github.io/galaxies-wafers/iterations/40-same-name-same-body/). Reached from 1 other entry point, including Iterations — 40 Same Name Same Body Page (galaxies-wafers, https://ventusltd.github.io/galaxies-wafers/iterations/40-same-name-same-body/).
- What it achieves
- It is built from the fnv step (`fnv`), the same seq helper (`sameSeq`) and the csr step (`csr`). Same Name, Same Body? — the arithmetic, in a Web Worker so the page stays live.
- Why it matters
- Imported by Iterations — 40 Same Name Same Body.
- Application
- galaxies-wafers · iterations/40-same-name-same-body · script
- Uses
- none found
- Used by
- referenced by Iterations — 40 Same Name Same Body (new URL import.meta.url)
- Produces
- fnv, sameSeq, csr, intern, ranks, spearman, post
- Defines here
- fnv, sameSeq, csr, intern, ranks, spearman, post
- Cannot conclude
- Whether this code runs, is used by a live app today, or is correct. This is a static reading.
- Evidence
- galaxies-wafers@5814793812add74605660fa821e6a39b8de80588 iterations/40-same-name-same-body/worker.js:1-232 · blob 54acf850a8fe623ae707ccee3a0506af46538e18 · GitHub at these lines
- State
- achieves: comment · context: readme_derived · stage: output · effects: none found · domain stage: unknown · users: none · canonical: unknown
/* Same Name, Same Body? — the arithmetic, in a Web Worker so the page stays live.
*
* Input: the text of families.json and the bytes of lines.bin, exactly as the
* wafer loads them. A family's body is its ordered line-key sequence,
* lines[lineOffset : lineOffset + lineCount]. Line keys are permanent and one key
* means one distinct line text, so two identical key sequences are two identical
* texts in the same order.
*
* SAME NAME: group families by their recorded name.
* SAME BODY: bucket families by a hash of the key sequence, then confirm every
* bucket member by exact comparison of the key arrays. A hash is never trusted
* on its own: a collision is counted and shown, not merged.
*
* Classic worker (no imports) so every browser that runs the page runs this.
*/
'use strict';
const ANON = '(anonymous)';
function fnv(lines, o, c) {
let h = 0x811c9dc5 ^ c;
for (let i = o; i < o + c; i++) {
const k = lines[i];
h ^= k & 0xff; h = Math.imul(h, 0x01000193);
h ^= (k >>> 8) & 0xff; h = Math.imul(h, 0x01000193);
h ^= (k >>> 16) & 0xff; h = Math.imul(h, 0x01000193);
h ^= k >>> 24; h = Math.imul(h, 0x01000193);
}
return h >>> 0;
}
function sameSeq(lines, o1, o2, c) {
for (let i = 0; i < c; i++) if (lines[o1 + i] !== lines[o2 + i]) return false;
return true;
}
/* compressed sparse rows from a group id per item */
function csr(groupOf, groups) {
const start = new Int32Array(groups + 1);
for (let i = 0; i < groupOf.length; i++) start[groupOf[i] + 1]++;
for (let g = 0; g < groups; g++) start[g + 1] += start[g];
const fill = start.slice(0, groups), list = new Int32Array(groupOf.length);
for (let i = 0; i < groupOf.length; i++) list[fill[groupOf[i]]++] = i;
return { start, list };
}
function intern(values) {
const table = [], id = new Map(), out = new Int32Array(values.length);
values.forEach((v, i) => {
const key = v === null || v === undefined ? '\u0000null' : String(v);
let x = id.get(key);
if (x === undefined) { x = table.length; id.set(key, x); table.push(v ?? null); }
out[i] = x;
});
return { table, out };
}
/* Spearman rank correlation, average ranks for ties */
function ranks(a) {
const idx = Array.from(a.keys()).sort((i, j) => a[i] - a[j]);
const r = new Float64Array(a.length);
for (let s = 0; s < idx.length;) {
let e = s; while (e + 1 < idx.length && a[idx[e + 1]] === a[idx[s]]) e++;
const avg = (s + e) / 2 + 1;
for (let t = s; t <= e; t++) r[idx[t]] = avg;
s = e + 1;
}
return r;
}
function spearman(x, y) {
if (x.length < 3) return null;
const rx = ranks(x), ry = ranks(y), n = x.length;
let mx = 0, my = 0; for (let i = 0; i < n; i++) { mx += rx[i]; my += ry[i]; }
mx /= n; my /= n;
let sxy = 0, sxx = 0, syy = 0;
for (let i = 0; i < n; i++) { const a = rx[i] - mx, b = ry[i] - my; sxy += a * b; sxx += a * a; syy += b * b; }
return sxx && syy ? sxy / Math.sqrt(sxx * syy) : null;
}
self.onmessage = e => {
const { text, buffer } = e.data;
const t0 = performance.now();
const post = (phase, done, total) => self.postMessage({ type: 'progress', phase, done, total });
try {
post('parsing families.json', 0, 1);
const fam = JSON.parse(text);
const lines = new Uint32Array(buffer);
const F = fam.length;
if (!Array.isArray(fam) || !F) throw new Error('families.json holds no families');
const tParsed = performance.now();
const off = new Int32Array(F), cnt = new Int32Array(F), n = new Int32Array(F);
const files = new Int32Array(F), repos = new Int32Array(F);
let beyond = 0;
for (let f = 0; f < F; f++) {
off[f] = fam[f].lineOffset; cnt[f] = fam[f].lineCount; n[f] = fam[f].n;
files[f] = typeof fam[f].files === 'number' ? fam[f].files : -1;
repos[f] = typeof fam[f].repos === 'number' ? fam[f].repos : -1;
if (off[f] < 0 || off[f] + cnt[f] > lines.length) beyond++;
}
if (beyond) throw new Error(`${beyond} families point past the end of lines.bin`);
/* SAME BODY: hash to bucket, exact compare to confirm */
const buckets = new Map(); /* hash -> body ids */
const rep = []; /* body id -> representative family */
const bodyOf = new Int32Array(F);
let compares = 0, collisions = 0;
for (let f = 0; f < F; f++) {
const h = fnv(lines, off[f], cnt[f]);
let cand = buckets.get(h), body = -1;
if (cand) {
for (const b of cand) {
const r = rep[b];
compares++;
if (cnt[r] === cnt[f] && sameSeq(lines, off[r], off[f], cnt[f])) { body = b; break; }
}
if (body < 0) collisions++;
} else { cand = []; buckets.set(h, cand); }
if (body < 0) { body = rep.length; rep.push(f); cand.push(body); }
bodyOf[f] = body;
if ((f & 511) === 0) post('comparing key sequences', f, F);
}
const B = rep.length;
const byBody = csr(bodyOf, B);
const tBodies = performance.now();
/* SAME NAME */
post('grouping by name', 0, 1);
const nm = intern(fam.map(x => x.name));
const nameOf = nm.out, names = nm.table, N = names.length;
const byName = csr(nameOf, N);
const kinds = intern(fam.map(x => x.kind)), blocks = intern(fam.map(x => x.block)), cats = intern(fam.map(x => x.category));
const noName = new Uint8Array(N);
names.forEach((s, i) => { noName[i] = s === null || s === ANON ? 1 : 0; });
/* distinct bodies per name (bodies in first-seen order) */
const nameBodies = new Int32Array(N);
const seen = new Int32Array(B).fill(-1);
for (let g = 0; g < N; g++) {
for (let i = byName.start[g]; i < byName.start[g + 1]; i++) {
const b = bodyOf[byName.list[i]];
if (seen[b] !== g) { seen[b] = g; nameBodies[g]++; }
}
}
/* four cases per family, exclusive, in this order of precedence:
0 same name same body · 1 different names same body · 2 same name different bodies · 3 unique
4 no name recorded (the placeholder "(anonymous)" is not a name, so it cannot share one) */
const cas = new Uint8Array(F);
const counts = [0, 0, 0, 0, 0];
for (let f = 0; f < F; f++) {
const b = bodyOf[f], g = nameOf[f];
let sameName = 0, diffName = 0;
for (let i = byBody.start[b]; i < byBody.start[b + 1]; i++) {
const o = byBody.list[i]; if (o === f) continue;
if (!noName[g] && nameOf[o] === g) sameName++; else diffName++;
}
let c;
if (noName[g] && sameName + diffName === 0) c = 4;
else if (sameName) c = 0;
else if (diffName) c = 1;
else if (nameBodies[g] > 1) c = 2;
else c = 3;
cas[f] = c; counts[c]++;
}
const nameCase = new Int32Array(N * 5);
for (let f = 0; f < F; f++) nameCase[nameOf[f] * 5 + cas[f]]++;
/* rankings */
const bodyFamilies = new Int32Array(B), bodyFiles = new Int32Array(B), bodyNames = new Int32Array(B);
for (let b = 0; b < B; b++) {
bodyFamilies[b] = byBody.start[b + 1] - byBody.start[b];
const s = new Set(); let fl = 0;
for (let i = byBody.start[b]; i < byBody.start[b + 1]; i++) { const f = byBody.list[i]; s.add(nameOf[f]); fl += Math.max(0, files[f]); }
bodyNames[b] = s.size; bodyFiles[b] = fl;
}
const TOP = 40;
const topBodies = Array.from({ length: B }, (_, b) => b)
.sort((a, b) => bodyFamilies[b] - bodyFamilies[a] || bodyFiles[b] - bodyFiles[a] || n[rep[a]] - n[rep[b]])
.slice(0, TOP);
const nameFiles = new Int32Array(N), nameMaxFiles = new Int32Array(N), nameMaxBody = new Int32Array(N).fill(-1);
for (let g = 0; g < N; g++) {
for (let i = byName.start[g]; i < byName.start[g + 1]; i++) {
const f = byName.list[i], fl = Math.max(0, files[f]);
nameFiles[g] += fl;
if (fl > nameMaxFiles[g]) { nameMaxFiles[g] = fl; nameMaxBody[g] = bodyOf[f]; }
}
}
const named = Array.from({ length: N }, (_, g) => g).filter(g => !noName[g]);
const byOverload = named.slice().sort((a, b) => nameBodies[b] - nameBodies[a] || nameFiles[b] - nameFiles[a] || (names[a] < names[b] ? -1 : 1));
const topNames = byOverload.slice(0, TOP);
const anonNames = Array.from({ length: N }, (_, g) => g).filter(g => noName[g]);
/* THE EARLIER FINDING, checked: does ranking names by how many families carry
them find the most copied code? Compare with ranking by the largest copy
count of any one body under that name (the pack's per-body files field). */
const byFamilies = named.slice().sort((a, b) => (byName.start[b + 1] - byName.start[b]) - (byName.start[a + 1] - byName.start[a]) || (names[a] < names[b] ? -1 : 1));
const byMaxCopy = named.slice().sort((a, b) => nameMaxFiles[b] - nameMaxFiles[a] || (names[a] < names[b] ? -1 : 1));
const topFamSet = new Set(byFamilies.slice(0, TOP));
const overlap = byMaxCopy.slice(0, TOP).filter(g => topFamSet.has(g)).length;
const multi = named.filter(g => byName.start[g + 1] - byName.start[g] > 1);
const rho = spearman(multi.map(g => nameBodies[g]), multi.map(g => nameMaxFiles[g]));
const rhoPerBody = spearman(multi.map(g => nameBodies[g]), multi.map(g => nameFiles[g] / nameBodies[g]));
let famEqBodies = true;
for (const g of named) if (nameBodies[g] !== byName.start[g + 1] - byName.start[g]) { famEqBodies = false; break; }
/* the named group with the most sites per body, among names with more than one body */
let densest = -1;
for (const g of multi) if (nameBodies[g] > 1 && (densest < 0 || nameFiles[g] / nameBodies[g] > nameFiles[densest] / nameBodies[densest])) densest = g;
const sharedBodies = Array.from({ length: B }, (_, b) => b).filter(b => bodyFamilies[b] > 1).length;
const tEnd = performance.now();
const msg = {
type: 'done',
F, B, N, L: lines.length,
off, cnt, n, files, repos, bodyOf, nameOf, names, noName,
kind: kinds.out, kinds: kinds.table, block: blocks.out, blocks: blocks.table, cat: cats.out, cats: cats.table,
bodyStart: byBody.start, bodyList: byBody.list, nameStart: byName.start, nameList: byName.list,
nameBodies, nameFiles, nameMaxFiles, nameMaxBody, nameCase, cas, counts,
bodyFamilies, bodyFiles, bodyNames, rep: Int32Array.from(rep),
topBodies, topNames, anonNames,
finding: {
top: TOP, overlap, rho, rhoPerBody, famEqBodies, multiNames: multi.length, sharedBodies,
mostBodies: byOverload[0] ?? -1, mostFamilies: byFamilies[0] ?? -1, mostCopied: byMaxCopy[0] ?? -1, densest
},
stats: { compares, collisions, buckets: buckets.size },
ms: { parse: tParsed - t0, bodies: tBodies - tParsed, rest: tEnd - tBodies, total: tEnd - t0 }
};
self.postMessage(msg, [off.buffer, cnt.buffer, bodyOf.buffer, nameOf.buffer]);
} catch (err) {
self.postMessage({ type: 'error', message: (err && err.message) || String(err) });
}
};