/** * The object heap: arrays, grids and drawing objects, `compiled-program.md` * 3.2. * * Everything here exists to serve one requirement, section 6.2's: after a * restore, two cells that held the same array still hold the same array, and a * `push` through one is visible through the other. A heap addressed by integer * gives that for nothing, because a restore puts integers back and the graph * was never taken apart. * * **Rollback is an undo journal rather than a copy.** Section 6.2 permits a * full copy, copy on write, an undo journal or nothing at all, and requires the * semantics rather than the representation. A full copy of the heap at every * bar is quadratic in a study that keeps a drawing object per bar, which is a * study people write, so the journal takes a copy of an object the first time a * bar touches it and of nothing else. A bar that changes nothing costs nothing. * * **What nothing can reach is dropped at a checkpoint.** A study that builds an * array every bar and keeps none of them would otherwise grow for as long as * the chart is open, and refusing it at a ceiling would be the wrong answer: * the objects are rubbish, not a program asking for too much. The sweep runs * only once the heap has grown past what the last one found live, so its cost * is spread over the allocations that made it necessary. */ import type { Value } from './value.js'; /** The kinds of object a reference can point at. */ export type ObjectKind = 'array' | 'table' | 'line' | 'label' | 'box' | 'polyline'; /** One cell a script wrote into a grid during this bar. */ export interface GridCell { readonly row: number; readonly col: number; readonly text: Value; readonly textColor: Value; readonly bgColor: Value; readonly align: Value; } export interface ArrayObject { readonly kind: 'array'; items: Value[]; } export interface TableObject { readonly kind: 'table'; readonly rows: number; readonly cols: number; /** * The cells written this bar, in write order. * * Not in a checkpoint and not rolled back: step 3 of every execution empties * the buffers, so a re-executed bar rebuilds them from nothing. */ cells: GridCell[]; } export interface DrawingObject { readonly kind: 'line' | 'label' | 'box' | 'polyline'; props: Record; deleted: boolean; /** The bar it was deleted on, for OS4005's message. */ deletedAt: number; } export type HeapObject = ArrayObject | TableObject | DrawingObject; export declare function isDrawing(object: HeapObject): object is DrawingObject; /** What a caller must offer the sweep: every value a live root holds. */ export type RootWalk = (visit: (value: Value) => void) => void; export declare class Heap { private readonly objects; private readonly free; /** Drawing object ids in creation order, which is the order a host draws them. */ private drawingIds; /** A copy of each object taken before the first change this bar made to it. */ private readonly taken; /** Ids allocated during this bar, oldest first. */ private created; /** The drawing roster's length when this bar began. */ private drawingsAtBarStart; private liveAtSweep; private sinceSweep; private elements; /** * Drawing objects created and not yet deleted, kept as a running count. * * Counted rather than measured because two things ask for it on the hot * path: `draw.count()` on every bar and the object ceiling on every * creation. Walking the roster for each would make a study that holds ten * thousand objects quadratic in the number it holds, which is the study the * ceiling exists for. The count is adjusted by the same journal that undoes * everything else, so a rolled back creation is a rolled back count. */ private drawingsLive; /** Objects alive right now, which is what a heap ceiling counts. */ liveCount(): number; /** Drawing objects the script has created and not deleted. */ drawingCount(): number; /** Total array elements held. */ elementCount(): number; get(id: number): HeapObject | undefined; /** The object a reference names, or nothing when the value is not one. */ deref(value: Value): HeapObject | undefined; allocate(object: HeapObject): number; /** * Delete one drawing object, which is the only thing that ends its life. * * Here rather than in the library call so that the roster, the count and the * journal cannot disagree: a caller that set the flag itself would leave the * count saying one thing and the objects another, and the first symptom * would be a ceiling refusing a script that had deleted everything. * * Deleting twice does nothing and is not an error. The script asked for the * object to be gone and it is gone; only a change to a deleted object is * OS4005. */ deleteDrawing(id: number, bar: number): void; /** * Announce a change to an object before making it. * * The copy is taken once per bar per object: the journal only has to restore * the state the bar started in, so the second change to the same object has * nothing to record. */ touch(id: number): void; /** Called after an array's length changes, so the ceiling sees it. */ countElements(delta: number): void; /** The drawing objects a host should see, in creation order. */ drawings(): readonly { readonly id: number; readonly object: DrawingObject; }[]; /** Undo every change this bar made, which is section 6.3 for the heap. */ rollback(): void; /** Accept every change this bar made. The journal starts again empty. */ commit(): void; /** Drop what nothing can reach. Only at a checkpoint, and only when it pays. */ sweep(roots: RootWalk): void; } //# sourceMappingURL=heap.d.ts.map