@pilates/core 1.0.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/dist/algorithm/cache.d.ts +7 -19
  2. package/dist/algorithm/cache.d.ts.map +1 -1
  3. package/dist/algorithm/cache.js +31 -27
  4. package/dist/algorithm/cache.js.map +1 -1
  5. package/dist/algorithm/index.d.ts +31 -0
  6. package/dist/algorithm/index.d.ts.map +1 -1
  7. package/dist/algorithm/index.js +105 -13
  8. package/dist/algorithm/index.js.map +1 -1
  9. package/dist/algorithm/round.d.ts +13 -0
  10. package/dist/algorithm/round.d.ts.map +1 -1
  11. package/dist/algorithm/round.js +33 -16
  12. package/dist/algorithm/round.js.map +1 -1
  13. package/dist/algorithm/spineless/classify.d.ts +47 -0
  14. package/dist/algorithm/spineless/classify.d.ts.map +1 -0
  15. package/dist/algorithm/spineless/classify.js +109 -0
  16. package/dist/algorithm/spineless/classify.js.map +1 -0
  17. package/dist/algorithm/spineless/field-id-pool.d.ts +28 -0
  18. package/dist/algorithm/spineless/field-id-pool.d.ts.map +1 -0
  19. package/dist/algorithm/spineless/field-id-pool.js +35 -0
  20. package/dist/algorithm/spineless/field-id-pool.js.map +1 -0
  21. package/dist/algorithm/spineless/flex-grammar.d.ts +394 -0
  22. package/dist/algorithm/spineless/flex-grammar.d.ts.map +1 -0
  23. package/dist/algorithm/spineless/flex-grammar.js +2509 -0
  24. package/dist/algorithm/spineless/flex-grammar.js.map +1 -0
  25. package/dist/algorithm/spineless/grammar.d.ts +156 -0
  26. package/dist/algorithm/spineless/grammar.d.ts.map +1 -0
  27. package/dist/algorithm/spineless/grammar.js +145 -0
  28. package/dist/algorithm/spineless/grammar.js.map +1 -0
  29. package/dist/algorithm/spineless/layout.d.ts +167 -0
  30. package/dist/algorithm/spineless/layout.d.ts.map +1 -0
  31. package/dist/algorithm/spineless/layout.js +893 -0
  32. package/dist/algorithm/spineless/layout.js.map +1 -0
  33. package/dist/algorithm/spineless/order-maintenance.bench.d.ts +25 -0
  34. package/dist/algorithm/spineless/order-maintenance.bench.d.ts.map +1 -0
  35. package/dist/algorithm/spineless/order-maintenance.bench.js +78 -0
  36. package/dist/algorithm/spineless/order-maintenance.bench.js.map +1 -0
  37. package/dist/algorithm/spineless/order-maintenance.d.ts +201 -0
  38. package/dist/algorithm/spineless/order-maintenance.d.ts.map +1 -0
  39. package/dist/algorithm/spineless/order-maintenance.js +300 -0
  40. package/dist/algorithm/spineless/order-maintenance.js.map +1 -0
  41. package/dist/algorithm/spineless/priority-queue.bench.d.ts +17 -0
  42. package/dist/algorithm/spineless/priority-queue.bench.d.ts.map +1 -0
  43. package/dist/algorithm/spineless/priority-queue.bench.js +57 -0
  44. package/dist/algorithm/spineless/priority-queue.bench.js.map +1 -0
  45. package/dist/algorithm/spineless/priority-queue.d.ts +73 -0
  46. package/dist/algorithm/spineless/priority-queue.d.ts.map +1 -0
  47. package/dist/algorithm/spineless/priority-queue.js +149 -0
  48. package/dist/algorithm/spineless/priority-queue.js.map +1 -0
  49. package/dist/algorithm/spineless/runtime.d.ts +292 -0
  50. package/dist/algorithm/spineless/runtime.d.ts.map +1 -0
  51. package/dist/algorithm/spineless/runtime.js +609 -0
  52. package/dist/algorithm/spineless/runtime.js.map +1 -0
  53. package/dist/algorithm/spineless/style-dirty.d.ts +65 -0
  54. package/dist/algorithm/spineless/style-dirty.d.ts.map +1 -0
  55. package/dist/algorithm/spineless/style-dirty.js +75 -0
  56. package/dist/algorithm/spineless/style-dirty.js.map +1 -0
  57. package/dist/dirty-flags.d.ts +30 -0
  58. package/dist/dirty-flags.d.ts.map +1 -0
  59. package/dist/dirty-flags.js +35 -0
  60. package/dist/dirty-flags.js.map +1 -0
  61. package/dist/index.d.ts +4 -1
  62. package/dist/index.d.ts.map +1 -1
  63. package/dist/index.js +9 -1
  64. package/dist/index.js.map +1 -1
  65. package/dist/inspect.d.ts +27 -0
  66. package/dist/inspect.d.ts.map +1 -0
  67. package/dist/inspect.js +61 -0
  68. package/dist/inspect.js.map +1 -0
  69. package/dist/layout-pool.d.ts +49 -0
  70. package/dist/layout-pool.d.ts.map +1 -0
  71. package/dist/layout-pool.js +75 -0
  72. package/dist/layout-pool.js.map +1 -0
  73. package/dist/node.d.ts +20 -3
  74. package/dist/node.d.ts.map +1 -1
  75. package/dist/node.js +63 -42
  76. package/dist/node.js.map +1 -1
  77. package/package.json +1 -1
@@ -0,0 +1,292 @@
1
+ /**
2
+ * Spineless Traversal runtime — incremental driver for an attribute
3
+ * grammar (Kirisame, Wang, Panchekha — PLDI 2025).
4
+ *
5
+ * Combines the three foundational primitives:
6
+ * - `BenderOrderMaintenance` (OM) — assigns a stable, totally-
7
+ * ordered timestamp to every field at first computation. Topo
8
+ * order at init time => OM order forever, even after relabel.
9
+ * - `OmPriorityQueue<Field>` — keyed on the field's OM node.
10
+ * Popping the minimum returns the next field to recompute in
11
+ * topological order, without any explicit DAG walks.
12
+ * - `Grammar` — declarative `field -> rule(deps, compute)` map.
13
+ * The runtime queries it on every recompute.
14
+ *
15
+ * ## Phases
16
+ *
17
+ * **Init** (`init()`): one full pass over the grammar from the
18
+ * supplied root fields. DFS in topological order; for each field,
19
+ * allocate an OM node (`om.insertAfter(prev)`), record the
20
+ * reverse-deps edges so dependents can be scheduled later, then run
21
+ * the field's compute and cache the value. After init, every
22
+ * reachable field has a (timestamp, value, dependents-list) triple
23
+ * in the runtime's storage.
24
+ *
25
+ * **Recompute** (`markDirty(field)` + `recompute()`): callers mark
26
+ * the fields whose inputs changed. Each dirty field is enqueued
27
+ * (`pq.push(field, omNode)`). `recompute()` loops: pop the
28
+ * OM-minimum field, re-run its rule. If the new value differs from
29
+ * the cached one, persist it and push every dependent. Process
30
+ * stops when the queue is empty.
31
+ *
32
+ * **Termination** rests on the dependency graph being acyclic: each
33
+ * field's value is a function of finitely many others, so the
34
+ * worklist reaches the DAG's unique fixpoint in finitely many
35
+ * steps. When the OM order is a true topological order — as it is
36
+ * after `init` and pure-additive `graft` — every field recomputes
37
+ * exactly once (a dependent always pops after its deps), giving the
38
+ * O(affected) Spineless bound. After a `rebindRule` an existing
39
+ * field may gain a dependency on a later-OM field; recompute stays
40
+ * correct and terminating, but such a field may recompute a bounded
41
+ * number of extra times. OM order is thus a performance property,
42
+ * not a correctness one.
43
+ *
44
+ * **Value preservation under no-op recompute:** if a field's rule
45
+ * produces the same value as before, its dependents are NOT
46
+ * scheduled. This is the key "skip work" property of Spineless.
47
+ *
48
+ * **Graft** (`graft(additions, newRoots)`): incremental structural
49
+ * growth (phase 5c). New fields whose topological position is at the
50
+ * tail — they may read existing fields, but no existing field reads
51
+ * them and no existing rule changes — are spliced in without a
52
+ * rebuild: each gets an OM node appended after the current tail, its
53
+ * reverse-dependency edges recorded, and its value computed once.
54
+ * This is exactly the shape of appending a child to a parent in the
55
+ * "simple" regime.
56
+ *
57
+ * **Detach** (`detach(fields)`): the inverse of `graft` — drops a
58
+ * removed subtree's fields, freeing their OM nodes and pruning the
59
+ * reverse-dependency edges into surviving fields. Valid when the
60
+ * removed set is closed under "is read by" (nothing outside reads
61
+ * in), which holds for removing a last child in the simple regime.
62
+ * Surviving leaf-input fields the removed set was the sole reader of
63
+ * are dropped too — the caller passes only the subtree's own fields.
64
+ *
65
+ * **Rebind** (`rebindRule(field, newRule)`): replace an existing
66
+ * field's rule — used when a structural change rewrites a surviving
67
+ * field (e.g. appending into a flex-distributing parent grows every
68
+ * sibling's flex-distribution dependency set). The reverse-
69
+ * dependency edges are updated to the new dep set and the field is
70
+ * marked dirty. New deps may have a later OM than the field — see
71
+ * the termination note above.
72
+ *
73
+ * ## What this runtime does NOT cover
74
+ *
75
+ * - Regime-changing structural mutation. `graft` only adds pure
76
+ * topological-tail fields; appending into a flex-distributing /
77
+ * justified / wrapping parent also rewrites existing siblings'
78
+ * rules, and removal / direction flips re-key subtrees — later
79
+ * phase-5c slices.
80
+ * - Differential mode against the imperative algorithm. The
81
+ * correctness oracle for the runtime is the `TopoInterpreter`
82
+ * running over the same grammar — once both agree, the grammar's
83
+ * existing differential coverage carries through.
84
+ *
85
+ * @internal
86
+ */
87
+ import type { Field, FieldRule, Grammar } from './grammar.js';
88
+ import { type OrderMaintenance } from './order-maintenance.js';
89
+ /**
90
+ * @internal
91
+ */
92
+ export declare class SpinelessRuntime {
93
+ private readonly rootFields;
94
+ private readonly om;
95
+ private readonly pq;
96
+ /**
97
+ * External Grammar map reference — kept so that callers holding the
98
+ * same map reference (e.g. layout.ts's `output.grammar`) see mutations
99
+ * made by `graft` / `rebindRule` / `detach`. All internal HOT-PATH
100
+ * reads use `rulesArr` (O(1) array index) instead.
101
+ */
102
+ private readonly grammar;
103
+ /**
104
+ * Field rules indexed by field.id — the fast-path mirror of `grammar`.
105
+ * Updated in lockstep with every `grammar.set` / `grammar.delete`.
106
+ * Plain array; auto-grows on out-of-range assignment (JS semantics).
107
+ */
108
+ private rulesArr;
109
+ /**
110
+ * Fast path: numeric field values indexed by field.id.
111
+ * valuePresent[id] === 1 means the value is stored here as a number.
112
+ */
113
+ private valuesArr;
114
+ /**
115
+ * Presence / storage-kind bitset, indexed by field.id:
116
+ * 0 = absent (not yet computed or detached)
117
+ * 1 = computed, value is a number stored in valuesArr[id]
118
+ * 2 = computed, value is a non-number object stored in valuesMap
119
+ */
120
+ private valuePresent;
121
+ /**
122
+ * Fallback for non-number field values (e.g. Field<MainAxisDistribution>).
123
+ * Only populated when valuePresent[id] === 2.
124
+ */
125
+ private readonly valuesMap;
126
+ /**
127
+ * OM nodes indexed by field.id — replaces the former `omNodes: Map<Field, OMNode>`.
128
+ * Plain array; auto-grows on out-of-range assignment (JS semantics).
129
+ * undefined slot = field not integrated (or detached).
130
+ */
131
+ private omNodesArr;
132
+ /**
133
+ * Authoritative list of all fields ever registered with this runtime
134
+ * (in integration order). Used for iteration in `markAllDirty` — slots
135
+ * whose `omNodesArr[field.id]` is `undefined` (detached) are skipped.
136
+ * Dead entries are never removed; the skip is O(1) per slot.
137
+ */
138
+ private readonly fieldRoster;
139
+ /**
140
+ * Reverse-dependency lists indexed by field.id — replaces the former
141
+ * `dependents: Map<Field, Field[]>`. `dependentsArr[id]` is the array
142
+ * of fields that read field-id. undefined = no dependents recorded yet
143
+ * (or field detached). Plain array; auto-grows on assignment.
144
+ */
145
+ private dependentsArr;
146
+ /** The OM node at the topological tail — where `graft` appends. */
147
+ private lastOm;
148
+ private initDone;
149
+ /**
150
+ * Recompute counters — for observability (phase 9). Plain integer
151
+ * fields, bumped on the existing `integrate` / `recompute` loops,
152
+ * so they cost nothing measurable and need no enable flag.
153
+ */
154
+ readonly stats: {
155
+ /** Fields integrated so far — the `init` pass plus every `graft`. */
156
+ initFields: number;
157
+ /** Fields popped from the PQ by the most recent `recompute()`. */
158
+ recomputeVisited: number;
159
+ /** Of those, Fields whose value actually changed. */
160
+ recomputeChanged: number;
161
+ /** Cumulative Fields visited across every `recompute()` since init. */
162
+ totalVisited: number;
163
+ };
164
+ constructor(grammar: Grammar, rootFields: ReadonlyArray<Field<unknown>>, om?: OrderMaintenance);
165
+ /**
166
+ * Grow `valuesArr` and `valuePresent` so that `id` is a valid index.
167
+ * Doubles capacity until sufficient, copying forward (LayoutPool pattern).
168
+ */
169
+ private ensureFieldCapacity;
170
+ /**
171
+ * Walk the grammar in topological order, allocate an OM node per
172
+ * field, cache initial values, and record reverse-dependents. Must
173
+ * be called once before any `evaluate` / `markDirty` / `recompute`.
174
+ */
175
+ init(): void;
176
+ /**
177
+ * Integrate new fields into an already-`init`ed runtime without a
178
+ * rebuild (phase 5c). `additions` holds the rules for the new
179
+ * fields only — it throws if any field is already present, since
180
+ * redefining an existing field is a rule *change*, not a graft.
181
+ * `newRoots` are the new fields to start the topological DFS from
182
+ * (their existing-field dependencies are reached as boundaries).
183
+ *
184
+ * Correct **iff** the new fields are pure topological-tail
185
+ * additions: no existing field reads a new field, and no existing
186
+ * rule needed to change. The caller guarantees this — it holds by
187
+ * construction when appending a last child to a parent in the
188
+ * simple regime (no flex distribution, default `justify`, no
189
+ * wrap). The new fields are computed with correct inputs during
190
+ * the graft, so no `markDirty` / `recompute` is needed afterward.
191
+ */
192
+ graft(additions: Grammar, newRoots: ReadonlyArray<Field<unknown>>): void;
193
+ /**
194
+ * Remove fields from an already-`init`ed runtime without a rebuild
195
+ * (phase 5c) — the inverse of `graft`. `fields` is the exact set
196
+ * to drop (a removed subtree's fields). For each: its OM node is
197
+ * freed, its cached value and reverse-dependency list dropped, its
198
+ * rule deleted from the grammar, and it is pruned from the
199
+ * reverse-dependency list of every field it read.
200
+ *
201
+ * Throws if any removed field still has a dependent *outside* the
202
+ * removed set — detaching it would dangle that edge. The caller
203
+ * guarantees a clean cut: it holds by construction when removing a
204
+ * last child from a parent in the simple regime (nothing outside
205
+ * that subtree reads into it). No `recompute` is needed afterward —
206
+ * removing a topological-tail subtree changes no surviving field.
207
+ *
208
+ * After the cut, any surviving leaf-input field the removed set was
209
+ * the sole reader of is **orphaned** — nothing reads it and the
210
+ * grammar cannot re-read it without a fresh build. Such orphans are
211
+ * dropped too (a leaf has no dependencies, so this cannot cascade).
212
+ * That makes the caller's removed set just the subtree's own
213
+ * fields — orphan input fields, e.g. the previous last child's
214
+ * now-unread main-end margin, need not be enumerated.
215
+ *
216
+ * @returns The set of every field actually removed — both the
217
+ * explicit `fields` argument AND any orphan-cleaned surviving deps.
218
+ * Callers can use this to maintain a `Set<Field>` index in O(|dropped|)
219
+ * rather than scanning all tracked fields.
220
+ */
221
+ detach(fields: Iterable<Field<unknown>>): Set<Field<unknown>>;
222
+ /**
223
+ * Replace the rule of an already-integrated field (phase 5c) — for
224
+ * a structural change that rewrites a *surviving* field rather than
225
+ * adding or removing one. Appending a child into a flex-distributing
226
+ * parent, for instance, grows every existing sibling's
227
+ * flex-distribution dependency set.
228
+ *
229
+ * The reverse-dependency edges are repaired to match the new dep
230
+ * set (the field is dropped from the lists of deps it no longer
231
+ * reads and added to the lists of deps it now reads), the new rule
232
+ * is installed, and the field is marked dirty so the next
233
+ * `recompute()` re-runs it. Every new dependency must already be
234
+ * integrated. The field keeps its OM node; if a new dependency has
235
+ * a later OM, recompute stays correct (see the termination note on
236
+ * the class) at the cost of a bounded number of extra recomputes.
237
+ */
238
+ rebindRule(field: Field<unknown>, newRule: FieldRule<unknown>): void;
239
+ /**
240
+ * Topological DFS shared by `init` and `graft`. For every
241
+ * not-yet-integrated field reachable from `roots`: recurse into
242
+ * deps, record reverse-dependency edges, allocate an OM node after
243
+ * the current tail, run the rule, and cache the value. Fields that
244
+ * already have an OM node are boundaries — visited, edge recorded,
245
+ * not re-walked.
246
+ */
247
+ private integrate;
248
+ /**
249
+ * Read the current cached value of a field. Throws if the field
250
+ * wasn't reachable from any root during `init` (so no cache entry
251
+ * exists).
252
+ */
253
+ evaluate<T>(field: Field<T>): T;
254
+ /**
255
+ * Whether `field` is currently tracked by the runtime — integrated
256
+ * and not since detached. A caller holding a possibly-stale field
257
+ * reference (e.g. an input field orphaned by a `detach`) can check
258
+ * this before `markDirty`.
259
+ */
260
+ isTracked(field: Field<unknown>): boolean;
261
+ /**
262
+ * Mark every field tracked by this runtime as dirty. Useful as an
263
+ * escape hatch when callers don't have fine-grained style-mutation
264
+ * wiring yet: mutate styles, call `markAllDirty()`, then
265
+ * `recompute()`. Each field re-runs its rule once, but the "skip
266
+ * dependents when value unchanged" property still applies — so
267
+ * cost is one compute per field plus propagation only along
268
+ * actually-changed values, rather than a full re-init.
269
+ */
270
+ markAllDirty(): void;
271
+ /**
272
+ * Mark a field as dirty. Its rule will be re-run on the next
273
+ * `recompute()`. If the new value differs from the cached one,
274
+ * dependents are scheduled in turn.
275
+ *
276
+ * Duplicate calls are a no-op (the priority queue dedupes via its
277
+ * internal membership set).
278
+ */
279
+ markDirty(field: Field<unknown>): void;
280
+ /**
281
+ * Process all dirty fields. Pops in OM (= topological) order; runs
282
+ * each field's rule. If the result differs from the cached value,
283
+ * persists it and pushes every dependent so it gets re-run later in
284
+ * this same pass.
285
+ *
286
+ * Returns every field whose value actually changed — the caller
287
+ * uses it to scope an incremental write-back to the moved nodes.
288
+ */
289
+ recompute(): Array<Field<unknown>>;
290
+ private runCompute;
291
+ }
292
+ //# sourceMappingURL=runtime.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../../../src/algorithm/spineless/runtime.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqFG;AAGH,OAAO,KAAK,EAAE,KAAK,EAAE,SAAS,EAAE,OAAO,EAAU,MAAM,cAAc,CAAC;AACtE,OAAO,EAAuC,KAAK,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAsBpG;;GAEG;AACH,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAgC;IAC3D,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAmB;IACtC,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAkC;IAErD;;;;;OAKG;IACH,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAU;IAElC;;;;OAIG;IACH,OAAO,CAAC,QAAQ,CAA0C;IAE1D;;;OAGG;IACH,OAAO,CAAC,SAAS,CAAe;IAChC;;;;;OAKG;IACH,OAAO,CAAC,YAAY,CAAa;IACjC;;;OAGG;IACH,OAAO,CAAC,QAAQ,CAAC,SAAS,CAA2C;IACrE;;;;OAIG;IACH,OAAO,CAAC,UAAU,CAA8B;IAChD;;;;;OAKG;IACH,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAwB;IACpD;;;;;OAKG;IACH,OAAO,CAAC,aAAa,CAAwC;IAE7D,mEAAmE;IACnE,OAAO,CAAC,MAAM,CAAuB;IAErC,OAAO,CAAC,QAAQ,CAAS;IAEzB;;;;OAIG;IACH,QAAQ,CAAC,KAAK;QACZ,qEAAqE;;QAErE,kEAAkE;;QAElE,qDAAqD;;QAErD,uEAAuE;;MAEvE;gBAGA,OAAO,EAAE,OAAO,EAChB,UAAU,EAAE,aAAa,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,EACzC,EAAE,GAAE,gBAA+C;IAkBrD;;;OAGG;IACH,OAAO,CAAC,mBAAmB;IAY3B;;;;OAIG;IACH,IAAI,IAAI,IAAI;IAKZ;;;;;;;;;;;;;;;OAeG;IACH,KAAK,CAAC,SAAS,EAAE,OAAO,EAAE,QAAQ,EAAE,aAAa,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,GAAG,IAAI;IAgBxE;;;;;;;;;;;;;;;;;;;;;;;;;;;OA2BG;IACH,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,GAAG,GAAG,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IAoF7D;;;;;;;;;;;;;;;OAeG;IACH,UAAU,CAAC,KAAK,EAAE,KAAK,CAAC,OAAO,CAAC,EAAE,OAAO,EAAE,SAAS,CAAC,OAAO,CAAC,GAAG,IAAI;IA2CpE;;;;;;;OAOG;IACH,OAAO,CAAC,SAAS;IAyDjB;;;;OAIG;IACH,QAAQ,CAAC,CAAC,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC;IAY/B;;;;;OAKG;IACH,SAAS,CAAC,KAAK,EAAE,KAAK,CAAC,OAAO,CAAC,GAAG,OAAO;IAIzC;;;;;;;;OAQG;IACH,YAAY,IAAI,IAAI;IAWpB;;;;;;;OAOG;IACH,SAAS,CAAC,KAAK,EAAE,KAAK,CAAC,OAAO,CAAC,GAAG,IAAI;IAatC;;;;;;;;OAQG;IACH,SAAS,IAAI,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IAuClC,OAAO,CAAC,UAAU;CA6BnB"}