- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/src/engine/view.ts?version=127&format=json
597 linesFirst import 75% · codex-capacity-chair 18% · codex-capacity-policy 7%Last change codex-capacity-chair in version 127, 11 versions changed it
You're reading version 127. The newest is 318, and 34 versions since then changed this file.
1// Projection: the files a closed set of atoms produces. A pure function of the
2// set. Each file is a tree of lines hanging off its start node; linearizing it
3// depth-first, children in descending priority, gives the file's sequence. A
4// line hangs off the node it was written after, unless a move in the set gave
5// it another parent (#28): then the newest such move decides.
6//
7// A laid-out file is kept as arrays of line numbers (the graph's n), not as a
8// record per line (#46): a record, its id and two index entries cost about
9// 370 bytes a line, and the view cache holds up to 200,000 lines. Records are
10// made when a caller asks for them, and the latest asked for is kept.
12import type { Graph, RelocationInfo } from "./graph.ts";
13import { isStartNode } from "./keys.ts";
14import { StableSet } from "./state-source.ts";
15import { joinLines } from "./text.ts";
16import { type AtomId, type AtomSet, EngineError, type LineId, type NodeId } from "./types.ts";
18/** A line in a file's full sequence, tombstones included. */
19export interface SeqLine {
20 readonly id: LineId;
21 readonly text: string;
22 /** The atom that wrote it. */
23 readonly atom: AtomId;
24 /** Atoms in the set that delete it, by seq. Empty when it is visible. */
25 readonly deletedBy: readonly AtomId[];
26 readonly visible: boolean;
27}
29/** A visible line of a projected file. */
30export interface VisibleLine {
31 readonly id: LineId;
32 readonly text: string;
33 readonly atom: AtomId;
34}
36/** A file's attributes in a view. */
37export interface FileAttrs {
38 /** Kept with no lines (an empty file). */
39 readonly exists: boolean;
40 readonly exec: boolean;
41 /** The target, when the file is a symbolic link. */
42 readonly link: string | null;
43 /** The SHA-256 of its bytes, when the file is binary. */
44 readonly blob: string | null;
45}
47const NO_ATTRS: FileAttrs = { exists: false, exec: false, link: null, blob: null };
48const NONE: readonly never[] = Object.freeze([]);
50interface FileState {
51 readonly attrs: FileAttrs;
52 /** Full history is retained only when a sequence caller explicitly requests it. */
53 readonly complete: boolean;
54 /** The file's sequence, tombstones included: each line's n. */
55 readonly seq: Int32Array;
56 /** Its visible lines' n, in order. */
57 readonly vis: Int32Array;
58 /** For each deleted position in seq: the seqs of the atoms in the set that delete it, by priority. */
59 readonly deletedBy: ReadonlyMap<number, readonly number[]>;
60 /** Positions by n, built the first time they're asked for. */
61 seqIndex?: Map<number, number>;
62 visIndex?: Map<number, number>;
63}
65interface Child {
66 readonly n: number;
67 /** 1 when the child's atom deleted the parent (the child replaces it), else 0. A moved line is 0. */
68 readonly replaces: number;
69 readonly lamport: number;
70 readonly seq: number;
71 readonly k: number;
72}
74const EMPTY_INTS = new Int32Array(0);
75const NO_FILE: FileState = { attrs: NO_ATTRS, complete: true, seq: EMPTY_INTS, vis: EMPTY_INTS, deletedBy: new Map() };
77// Approximate retained-container costs for View.weight(): reference slots,
78// object headers, and map/set entries. These are cache-budget units, not V8
79// heap measurements. Referenced Graph strings and records are not charged.
80const ARRAY_HEADER_BYTES = 24;
81const OBJECT_BYTES = 32;
82const MAP_HEADER_BYTES = 32;
83const MAP_ENTRY_BYTES = 40;
84const SET_ENTRY_BYTES = 32;
85const REFERENCE_BYTES = 8;
86const FILE_LOCATION_MEMO_ENTRIES = 256;
88/**
89 * The projection of a closed set. Construct once, then ask for files, lines
90 * and sequences. Throws `not-closed` if some atom's dependency is missing.
91 * Each file is laid out the first time it is asked for, so a view of a large
92 * set costs little until files are read.
93 */
94export class View {
95 readonly graph: Graph;
96 /** The set, made from #in the first time it's asked for: a view keeps a byte per atom, not a copy of the set (#46). */
97 #set: AtomSet | undefined;
98 readonly #stable: StableSet<AtomId> | undefined;
99 /** Fixed-size memo for immutable indexed membership, independent of history size. */
100 #membershipSeq = new Float64Array(256).fill(-1);
101 #membershipValue = new Uint8Array(256);
102 #files = new Map<string, FileState>();
103 /** Every file the set wrote in, sorted; worked out the first time it's asked for. */
104 #knownList: string[] | undefined;
105 /** 1 for each atom (by seq) in the set. */
106 #in: Uint8Array;
107 /** Atoms in the set that move lines. */
108 #movers: AtomId[] | undefined;
109 #hasMoves = false;
110 /** Each moved line's relocation that counts in this set; built on first use. */
111 #relocs: Map<LineId, RelocationInfo> | undefined;
112 /** The same, by n, with the new parent's code. */
113 #relocsN: Map<number, { readonly r: RelocationInfo; readonly parent: number }> | undefined;
114 /** Bounded memo: ancestry can span the entire history even for one visible line. */
115 #fileOf = new Map<number, string>();
116 /** The latest records asked for: one file's lines, one file's sequence, one file's text. */
117 #linesCache: { path: string; lines: readonly VisibleLine[] } | undefined;
118 #seqCache: { path: string; seq: readonly SeqLine[] } | undefined;
119 #textCache: { path: string; text: string } | undefined;
121 constructor(graph: Graph, set: Iterable<AtomId>) {
122 this.graph = graph;
123 if (set instanceof StableSet) {
124 this.#stable = set;
125 this.#set = set;
126 this.#in = new Uint8Array(0);
127 for (const id of set) {
128 const atom = graph.atom(id);
129 for (const dependency of atom.deps) {
130 graph.atom(dependency);
131 if (!set.has(dependency))
132 throw new EngineError("not-closed", `set is not closed: ${id} depends on ${dependency}`);
133 }
134 }
135 for (const id of graph.movers()) {
136 if (set.has(id)) {
137 this.#hasMoves = true;
138 break;
139 }
140 }
141 this.#relocs = this.#hasMoves ? undefined : new Map();
142 this.#relocsN = this.#hasMoves ? undefined : new Map();
143 return;
144 }
145 const inSet = new Uint8Array(graph.size);
146 for (const id of set) {
147 const atom = graph.atom(id);
148 inSet[atom.seq] = 1;
149 }
150 // Validate one atom at a time. Keeping all decoded atoms here defeats
151 // storage-backed Graph reads even though the view only needs membership.
152 for (let seq = 0; seq < inSet.length; seq++) {
153 if (inSet[seq] !== 1) continue;
154 const atom = graph.atom(graph.idOfSeq(seq));
155 for (const d of atom.deps) {
156 if (inSet[graph.atom(d).seq] !== 1)
157 throw new EngineError("not-closed", `set is not closed: ${atom.id} depends on ${d}`);
158 }
159 }
160 this.graph = graph;
161 this.#in = inSet;
162 this.#movers = graph.movers().filter((id) => inSet[graph.atom(id).seq] === 1);
163 this.#hasMoves = this.#movers.length > 0;
164 this.#relocs = this.#movers.length === 0 ? new Map() : undefined;
165 this.#relocsN = this.#movers.length === 0 ? new Map() : undefined;
166 }
168 /** The set of atoms this view projects. */
169 get set(): AtomSet {
170 if (!this.#set) {
171 const out = new Set<AtomId>();
172 for (let seq = 0; seq < this.#in.length; seq++) if (this.#in[seq] === 1) out.add(this.graph.idOfSeq(seq));
173 this.#set = out;
174 }
175 return this.#set;
176 }
178 /** Whether the atom with this seq is in the set. */
179 #has(seq: number): boolean {
180 if (this.#stable) {
181 const slot = seq % this.#membershipSeq.length;
182 if (this.#membershipSeq[slot] === seq) return this.#membershipValue[slot] === 1;
183 const present = this.#stable.has(this.graph.idOfSeq(seq));
184 this.#membershipSeq[slot] = seq;
185 this.#membershipValue[slot] = present ? 1 : 0;
186 return present;
187 }
188 return seq < this.#in.length && this.#in[seq] === 1;
189 }
191 /**
192 * The relocation that counts for each moved line: the newest (by priority)
193 * in the set. Two moves made apart can loop lines into each other, so no
194 * start node reaches them; then the oldest relocation on the loop is
195 * ignored, and the next newest of that line counts, until none loop.
196 */
197 #relocations(): Map<LineId, RelocationInfo> {
198 if (this.#relocs) return this.#relocs;
199 const graph = this.graph;
200 const stronger = (a: RelocationInfo, b: RelocationInfo): boolean => graph.comparePriority(a.atom, b.atom) > 0;
201 // Persistent reads decode new objects. Cycle suppression must identify the
202 // logical move, not one particular decoded JavaScript object.
203 const identity = (r: RelocationInfo): string => JSON.stringify([r.atom, r.k]);
204 const ignored = new Set<string>();
205 const pick = (line: LineId): RelocationInfo | undefined => {
206 let best: RelocationInfo | undefined;
207 for (const r of graph.eachRelocationOf(line)) {
208 if (this.#has(graph.atom(r.atom).seq) && !ignored.has(identity(r)) && (!best || stronger(r, best))) best = r;
209 }
210 return best;
211 };
212 const relocs = new Map<LineId, RelocationInfo>();
213 for (const id of this.#eachMover()) {
214 for (const m of graph.atom(id).moves!) {
215 if (!relocs.has(m.line)) relocs.set(m.line, pick(m.line)!);
216 }
217 }
218 const parentOf = (line: LineId): NodeId => relocs.get(line)?.parent ?? graph.lineParent(line)!;
219 const reaches = new Set<LineId>();
220 for (const line of [...relocs.keys()]) {
221 for (;;) {
222 const path: LineId[] = [];
223 const onPath = new Map<LineId, number>();
224 let cur: NodeId = line;
225 let loopAt = -1;
226 while (!isStartNode(cur) && !reaches.has(cur)) {
227 const at = onPath.get(cur);
228 if (at !== undefined) {
229 loopAt = at;
230 break;
231 }
232 onPath.set(cur, path.length);
233 path.push(cur);
234 cur = parentOf(cur);
235 }
236 if (loopAt < 0) {
237 for (const l of path) reaches.add(l);
238 break;
239 }
240 let weakest: RelocationInfo | undefined;
241 for (const l of path.slice(loopAt)) {
242 const r = relocs.get(l);
243 if (r && (!weakest || stronger(weakest, r))) weakest = r;
244 }
245 ignored.add(identity(weakest!));
246 const next = pick(weakest!.line);
247 if (next) relocs.set(weakest!.line, next);
248 else relocs.delete(weakest!.line);
249 }
250 }
251 this.#relocs = relocs;
252 return relocs;
253 }
255 /** The relocations by n, with each new parent's code. */
256 #relocationsN(): Map<number, { readonly r: RelocationInfo; readonly parent: number }> {
257 if (this.#relocsN) return this.#relocsN;
258 const out = new Map<number, { readonly r: RelocationInfo; readonly parent: number }>();
259 for (const [line, r] of this.#relocations())
260 out.set(this.graph.lineN(line), { r, parent: this.graph.codeOf(r.parent)! });
261 this.#relocsN = out;
262 return out;
263 }
265 /** The node a line hangs off in this set: where it was written, or where a move put it. */
266 parentOf(line: LineId): NodeId {
267 const r = this.#hasMoves ? this.#relocations().get(line) : undefined;
268 if (r) return r.parent;
269 const parent = this.graph.lineParent(line);
270 if (parent === undefined) throw new EngineError("unknown-line", `no line ${line}`);
271 return parent;
272 }
274 /** Whether any atom in the set moves lines. */
275 hasMoves(): boolean {
276 return this.#hasMoves;
277 }
278 *#eachMover(): IterableIterator<AtomId> {
279 if (this.#movers) yield* this.#movers;
280 else for (const id of this.graph.movers()) if (this.#stable!.has(id)) yield id;
281 }
283 /** The file a line is in, in this set: the one its chain of parents reaches. */
284 fileOf(line: LineId): string {
285 const n = this.graph.lineN(line);
286 if (n < 0) throw new EngineError("unknown-line", `no line ${line}`);
287 return this.#fileOfN(n);
288 }
290 #fileOfN(n: number): string {
291 const graph = this.graph;
292 if (!this.#hasMoves) return graph.writtenIn(n);
293 const relocs = this.#relocationsN();
294 // Keep only a fixed tail of the walk for path compression. The selected
295 // relocation graph is acyclic, so traversal needs no history-sized stack.
296 const path: number[] = [];
297 let visited = 0;
298 let cur = n;
299 let file: string | undefined;
300 while (file === undefined) {
301 if (cur < 0) file = graph.nodeOf(cur).slice(1);
302 else {
303 file = this.#fileOf.get(cur);
304 if (file === undefined) {
305 path[visited++ % FILE_LOCATION_MEMO_ENTRIES] = cur;
306 cur = relocs.get(cur)?.parent ?? graph.parentCodeOf(cur);
307 }
308 }
309 }
310 for (const l of path) {
311 if (!this.#fileOf.has(l) && this.#fileOf.size >= FILE_LOCATION_MEMO_ENTRIES)
312 this.#fileOf.delete(this.#fileOf.keys().next().value!);
313 this.#fileOf.set(l, file);
314 }
315 return file;
316 }
318 /** The lines hanging off a node in this set, in layout order. */
319 #children(code: number): Child[] {
320 const graph = this.graph;
321 const relocs = this.#hasMoves ? this.#relocationsN() : undefined;
322 const ns: number[] = [];
323 graph.childrenN(code, ns);
324 const out: Child[] = [];
325 for (const n of ns) {
326 const seq = graph.writerSeqOf(n);
327 if (!this.#has(seq) || relocs?.has(n)) continue;
328 out.push({ n, replaces: graph.replacesN(n) ? 1 : 0, lamport: graph.lamportOfSeq(seq), seq, k: graph.kOf(n) });
329 }
330 if (relocs) {
331 for (const r of graph.eachRelocatedToCode(code)) {
332 const n = graph.lineN(r.line);
333 const selected = relocs.get(n)?.r;
334 if (!selected || selected.atom !== r.atom || selected.k !== r.k) continue;
335 const seq = graph.atom(r.atom).seq;
336 out.push({ n, replaces: 0, lamport: graph.lamportOfSeq(seq), seq, k: r.k });
337 }
338 }
339 // A line's replacements (runs of atoms that deleted it) first, then the
340 // rest; each group in descending priority (lamport, then seq); lines of
341 // one atom keep k order. A replacement already outranks every insertion
342 // after the line that reads it (it depends on them, or conflicts with
343 // them); the grouping also puts it above a `before` insertion, which
344 // doesn't read the line above, so the new text lands in the replaced
345 // line's place. A moved line ranks like an insertion by the moving atom.
346 if (out.length > 1)
347 out.sort((a, b) => b.replaces - a.replaces || b.lamport - a.lamport || b.seq - a.seq || a.k - b.k);
348 return out;
349 }
351 /** Lay out one file: its line tree from the set's atoms, linearized. */
352 #layout(path: string, complete: boolean): FileState {
353 const graph = this.graph;
354 // For each attribute, the highest-priority setter in the set wins.
355 const setters = new Map<string, { id: AtomId; value: string | null }>();
356 for (const id of graph.eachAtomIn(path)) {
357 if (!this.#has(graph.atom(id).seq)) continue;
358 for (const a of graph.atom(id).attrs ?? []) {
359 if (a.file !== path) continue;
360 const best = setters.get(a.name);
361 if (!best || graph.comparePriority(best.id, id) < 0) setters.set(a.name, { id, value: a.value });
362 }
363 }
365 const seq: number[] = [];
366 const vis: number[] = [];
367 const deletedBy = new Map<number, readonly number[]>();
368 // Depth-first, iteratively (a file of n lines can be a chain n deep).
369 const stack: number[] = [];
370 const pushChildren = (code: number): void => {
371 const list = this.#children(code);
372 for (let i = list.length - 1; i >= 0; i--) stack.push(list[i]!.n);
373 };
374 const start = graph.startCodeOf(path);
375 if (start !== undefined) pushChildren(start);
376 const priority = (a: number, b: number) => graph.lamportOfSeq(a) - graph.lamportOfSeq(b) || a - b;
377 while (stack.length > 0) {
378 const n = stack.pop()!;
379 const deleters = graph.deleterSeqsOf(n);
380 const by = deleters.length === 0 ? NONE : deleters.filter((d) => this.#has(d));
381 if (by.length > 0) {
382 if (complete) deletedBy.set(seq.length, by.length > 1 ? [...by].sort(priority) : by);
383 } else vis.push(n);
384 if (complete) seq.push(n);
385 pushChildren(n);
386 }
387 const attrs: FileAttrs = {
388 exists: setters.get("exists")?.value === "1",
389 exec: setters.get("exec")?.value === "1",
390 link: setters.get("link")?.value ?? null,
391 blob: setters.get("blob")?.value ?? null,
392 };
393 return { attrs, complete, seq: Int32Array.from(seq), vis: Int32Array.from(vis), deletedBy };
394 }
396 get #known(): string[] {
397 if (!this.#knownList) {
398 const known = new Set<string>();
399 if (this.#stable) {
400 for (const id of this.#stable) for (const file of this.graph.filesOf(id)) known.add(file);
401 } else {
402 for (let seq = 0; seq < this.#in.length; seq++)
403 if (this.#in[seq] === 1) for (const file of this.graph.filesOf(this.graph.idOfSeq(seq))) known.add(file);
404 }
405 this.#knownList = [...known].sort();
406 }
407 return this.#knownList;
408 }
410 /** Files with at least one visible line, sorted. */
411 paths(): string[] {
412 if (this.#stable) {
413 const paths: string[] = [];
414 for (const path of this.graph.knownFiles()) {
415 if (this.hasFile(path)) paths.push(path);
416 else this.#files.delete(path);
417 }
418 return paths.sort();
419 }
420 return this.#known.filter((p) => this.hasFile(p));
421 }
423 /** Every file the set has ever written a line in, including fully deleted ones. */
424 knownPaths(): string[] {
425 return [...this.#known];
426 }
428 /** Whether the file exists in this projection (has a visible line). */
429 hasFile(path: string): boolean {
430 const f = this.#file(path);
431 return f.vis.length > 0 || f.attrs.exists || f.attrs.link !== null || f.attrs.blob !== null;
432 }
434 /** A file's attributes: kept while empty, executable, a link's target. */
435 attrs(path: string): FileAttrs {
436 return this.#file(path).attrs;
437 }
439 /** The projected text of a file ("" if it has no visible lines). */
440 text(path: string): string {
441 if (this.#textCache?.path === path) return this.#textCache.text;
442 const vis = this.#file(path).vis;
443 const lines: string[] = new Array(vis.length);
444 for (let i = 0; i < vis.length; i++) lines[i] = this.graph.textOf(vis[i]!);
445 const text = joinLines(lines);
446 this.#textCache = { path, text };
447 return text;
448 }
450 /** Whether a file reads the same here and in `other` (of the same graph), compared line by line without joining either (#46). */
451 sameText(path: string, other: View): boolean {
452 const a = this.#file(path).vis;
453 const b = other.#file(path).vis;
454 if (a.length !== b.length) return false;
455 for (let i = 0; i < a.length; i++)
456 if (a[i] !== b[i] && this.graph.textOf(a[i]!) !== this.graph.textOf(b[i]!)) return false;
457 return true;
458 }
460 /** Visible lines with their ids. */
461 lines(path: string): readonly VisibleLine[] {
462 if (this.#linesCache?.path === path) return this.#linesCache.lines;
463 const graph = this.graph;
464 const vis = this.#file(path).vis;
465 const lines: VisibleLine[] = new Array(vis.length);
466 for (let i = 0; i < vis.length; i++) {
467 const n = vis[i]!;
468 lines[i] = { id: graph.lineIdOf(n), text: graph.textOf(n), atom: graph.idOfSeq(graph.writerSeqOf(n)) };
469 }
470 this.#linesCache = { path, lines };
471 return lines;
472 }
474 /** The full linearized sequence of a file, tombstones included. */
475 sequence(path: string): readonly SeqLine[] {
476 if (this.#seqCache?.path === path) return this.#seqCache.seq;
477 const graph = this.graph;
478 const f = this.#file(path, true);
479 const out: SeqLine[] = new Array(f.seq.length);
480 for (let i = 0; i < f.seq.length; i++) {
481 const n = f.seq[i]!;
482 const by = f.deletedBy.get(i);
483 out[i] = {
484 id: graph.lineIdOf(n),
485 text: graph.textOf(n),
486 atom: graph.idOfSeq(graph.writerSeqOf(n)),
487 deletedBy: by ? by.map((s) => graph.idOfSeq(s)) : NONE,
488 visible: by === undefined,
489 };
490 }
491 this.#seqCache = { path, seq: out };
492 return out;
493 }
495 /** Index of a line in `sequence(path)`, or -1. */
496 sequenceIndex(path: string, line: LineId): number {
497 const f = this.#file(path, true);
498 if (!f.seqIndex) {
499 f.seqIndex = new Map();
500 for (let i = 0; i < f.seq.length; i++) f.seqIndex.set(f.seq[i]!, i);
501 }
502 return f.seqIndex.get(this.graph.lineN(line)) ?? -1;
503 }
505 /** Index of a line in `lines(path)` (0-based visible line number), or -1 if not visible. */
506 lineNumber(path: string, line: LineId): number {
507 const f = this.#file(path);
508 if (!f.visIndex) {
509 f.visIndex = new Map();
510 for (let i = 0; i < f.vis.length; i++) f.visIndex.set(f.vis[i]!, i);
511 }
512 return f.visIndex.get(this.graph.lineN(line)) ?? -1;
513 }
515 /** All projected text files, path -> text, in path order. Binary files are in `blobs()`. */
516 files(): Record<string, string> {
517 const out: Record<string, string> = {};
518 for (const path of this.paths()) if (this.#file(path).attrs.blob === null) out[path] = this.text(path);
519 return out;
520 }
522 /** Binary files, path -> the SHA-256 of their bytes, in path order. */
523 blobs(): Record<string, string> {
524 const out: Record<string, string> = {};
525 for (const path of this.paths()) {
526 const blob = this.#file(path).attrs.blob;
527 if (blob !== null) out[path] = blob;
528 }
529 return out;
530 }
532 /**
533 * About how many bytes of retained state this view costs (#46). This uses
534 * approximate ownership costs for containers and wrappers, not V8 heap
535 * measurements. Graph-owned strings and records count only as references.
536 * This method only inspects already-created state: it does not lay out files,
537 * populate lazy caches, enumerate the graph, or count temporary traversal
538 * containers and shared empty singleton payloads as retained view state.
539 */
540 weight(): number {
541 let n = this.#in.byteLength;
542 n += 2 * ARRAY_HEADER_BYTES + this.#membershipSeq.byteLength + this.#membershipValue.byteLength;
543 if (this.#movers) n += ARRAY_HEADER_BYTES + REFERENCE_BYTES * this.#movers.length;
544 n += MAP_HEADER_BYTES; // #files exists from construction.
545 n += MAP_HEADER_BYTES; // #fileOf exists from construction.
546 if (this.#stable) n += this.#stable.retainedBytes;
547 else if (this.#set) n += OBJECT_BYTES + SET_ENTRY_BYTES * this.#set.size;
548 if (this.#knownList) n += ARRAY_HEADER_BYTES + REFERENCE_BYTES * this.#knownList.length;
549 if (this.#relocs) n += MAP_HEADER_BYTES + MAP_ENTRY_BYTES * this.#relocs.size;
550 if (this.#relocsN) n += MAP_HEADER_BYTES + (MAP_ENTRY_BYTES + OBJECT_BYTES) * this.#relocsN.size;
551 n += MAP_ENTRY_BYTES * this.#fileOf.size;
552 for (const f of this.#files.values()) {
553 n += MAP_ENTRY_BYTES;
554 if (f !== NO_FILE) {
555 // FileState and FileAttrs are per laid-out file; typed-array
556 // headers are separate from their payload byte lengths below.
557 n += OBJECT_BYTES + OBJECT_BYTES + 2 * ARRAY_HEADER_BYTES;
558 n += f.seq.byteLength + f.vis.byteLength;
559 n += MAP_HEADER_BYTES + (MAP_ENTRY_BYTES + ARRAY_HEADER_BYTES) * f.deletedBy.size;
560 for (const deleters of f.deletedBy.values()) n += REFERENCE_BYTES * deleters.length;
561 if (f.seqIndex) n += MAP_HEADER_BYTES + MAP_ENTRY_BYTES * f.seqIndex.size;
562 if (f.visIndex) n += MAP_HEADER_BYTES + MAP_ENTRY_BYTES * f.visIndex.size;
563 }
564 }
565 if (this.#linesCache)
566 n += OBJECT_BYTES + REFERENCE_BYTES + ARRAY_HEADER_BYTES + 100 * this.#linesCache.lines.length;
567 if (this.#seqCache) {
568 n += OBJECT_BYTES + REFERENCE_BYTES + ARRAY_HEADER_BYTES + 120 * this.#seqCache.seq.length;
569 for (const line of this.#seqCache.seq)
570 if (line.deletedBy !== NONE) n += ARRAY_HEADER_BYTES + REFERENCE_BYTES * line.deletedBy.length;
571 }
572 if (this.#textCache) n += OBJECT_BYTES + REFERENCE_BYTES + ARRAY_HEADER_BYTES + 2 * this.#textCache.text.length;
573 return n;
574 }
576 #file(path: string, complete = false): FileState {
577 let state = this.#files.get(path);
578 if (!state || (complete && !state.complete)) {
579 state = this.graph.hasAtomsIn(path) ? this.#layout(path, complete) : NO_FILE;
580 this.#files.set(path, state);
581 }
582 return state;
583 }
584}
586/** The files of a closed set, path -> text. */
587export function project(graph: Graph, set: Iterable<AtomId>): Record<string, string> {
588 return new View(graph, set).files();
589}
591/** Where a node sits in a file's sequence: the index just after it (0 for a start node). */
592export function indexAfter(view: View, path: string, node: NodeId): number {
593 if (isStartNode(node)) return 0;
594 const i = view.sequenceIndex(path, node);
595 if (i < 0) throw new EngineError("not-in-view", `line ${node} is not in ${path} in this view`);
596 return i + 1;
597}