@hviana/sema 0.1.4 → 0.1.5

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 (122) hide show
  1. package/dist/example/demo.js +25 -25
  2. package/dist/example/train_base.d.ts +21 -19
  3. package/dist/example/train_base.js +1952 -1684
  4. package/dist/src/alphabet.d.ts +3 -3
  5. package/dist/src/alphabet.js +27 -24
  6. package/dist/src/alu/src/alu.d.ts +194 -169
  7. package/dist/src/alu/src/alu.js +414 -374
  8. package/dist/src/alu/src/expr.d.ts +43 -36
  9. package/dist/src/alu/src/expr.js +278 -252
  10. package/dist/src/alu/src/index.d.ts +98 -9
  11. package/dist/src/alu/src/index.js +63 -9
  12. package/dist/src/alu/src/kernel-arith.d.ts +5 -1
  13. package/dist/src/alu/src/kernel-arith.js +285 -195
  14. package/dist/src/alu/src/kernel-bits.d.ts +5 -1
  15. package/dist/src/alu/src/kernel-bits.js +115 -81
  16. package/dist/src/alu/src/kernel-logic.js +63 -33
  17. package/dist/src/alu/src/kernel-nd.js +195 -155
  18. package/dist/src/alu/src/kernel-numeric.d.ts +80 -16
  19. package/dist/src/alu/src/kernel-numeric.js +331 -259
  20. package/dist/src/alu/src/operation.d.ts +132 -112
  21. package/dist/src/alu/src/operation.js +163 -149
  22. package/dist/src/alu/src/parser.d.ts +175 -168
  23. package/dist/src/alu/src/parser.js +421 -382
  24. package/dist/src/alu/src/resonance.d.ts +25 -16
  25. package/dist/src/alu/src/resonance.js +56 -49
  26. package/dist/src/alu/src/text.d.ts +7 -3
  27. package/dist/src/alu/src/text.js +37 -31
  28. package/dist/src/alu/src/value.d.ts +14 -14
  29. package/dist/src/alu/src/value.js +160 -150
  30. package/dist/src/alu/test/alu.test.js +656 -543
  31. package/dist/src/bytes.d.ts +5 -1
  32. package/dist/src/bytes.js +40 -33
  33. package/dist/src/config.d.ts +102 -102
  34. package/dist/src/config.js +83 -83
  35. package/dist/src/derive/src/deduction.d.ts +62 -60
  36. package/dist/src/derive/src/deduction.js +109 -103
  37. package/dist/src/derive/src/index.d.ts +7 -1
  38. package/dist/src/derive/src/priority-queue.d.ts +8 -8
  39. package/dist/src/derive/src/priority-queue.js +61 -57
  40. package/dist/src/derive/src/rewrite.d.ts +18 -15
  41. package/dist/src/derive/src/rewrite.js +67 -59
  42. package/dist/src/derive/src/trie.d.ts +56 -56
  43. package/dist/src/derive/src/trie.js +179 -175
  44. package/dist/src/derive/test/derive.test.js +100 -96
  45. package/dist/src/extension.d.ts +16 -13
  46. package/dist/src/geometry.d.ts +42 -17
  47. package/dist/src/geometry.js +266 -235
  48. package/dist/src/index.d.ts +17 -2
  49. package/dist/src/index.js +7 -1
  50. package/dist/src/ingest-cache.d.ts +30 -25
  51. package/dist/src/ingest-cache.js +127 -108
  52. package/dist/src/mind/articulation.d.ts +5 -1
  53. package/dist/src/mind/articulation.js +112 -77
  54. package/dist/src/mind/attention.d.ts +108 -40
  55. package/dist/src/mind/attention.js +871 -749
  56. package/dist/src/mind/canonical.d.ts +19 -4
  57. package/dist/src/mind/canonical.js +31 -27
  58. package/dist/src/mind/graph-search.d.ts +225 -201
  59. package/dist/src/mind/graph-search.js +821 -742
  60. package/dist/src/mind/index.d.ts +10 -2
  61. package/dist/src/mind/junction.d.ts +58 -31
  62. package/dist/src/mind/junction.js +237 -172
  63. package/dist/src/mind/learning.d.ts +52 -16
  64. package/dist/src/mind/learning.js +165 -143
  65. package/dist/src/mind/match.d.ts +69 -20
  66. package/dist/src/mind/match.js +318 -259
  67. package/dist/src/mind/mechanisms/alu.js +16 -16
  68. package/dist/src/mind/mechanisms/cast.d.ts +13 -9
  69. package/dist/src/mind/mechanisms/cast.js +456 -363
  70. package/dist/src/mind/mechanisms/confluence.d.ts +12 -8
  71. package/dist/src/mind/mechanisms/confluence.js +183 -152
  72. package/dist/src/mind/mechanisms/cover.d.ts +8 -2
  73. package/dist/src/mind/mechanisms/cover.js +210 -148
  74. package/dist/src/mind/mechanisms/extraction.d.ts +34 -8
  75. package/dist/src/mind/mechanisms/extraction.js +288 -234
  76. package/dist/src/mind/mechanisms/recall.d.ts +10 -6
  77. package/dist/src/mind/mechanisms/recall.js +185 -126
  78. package/dist/src/mind/mind.d.ts +154 -129
  79. package/dist/src/mind/mind.js +288 -258
  80. package/dist/src/mind/pipeline-mechanism.d.ts +124 -112
  81. package/dist/src/mind/pipeline-mechanism.js +172 -161
  82. package/dist/src/mind/pipeline.d.ts +14 -4
  83. package/dist/src/mind/pipeline.js +189 -125
  84. package/dist/src/mind/primitives.d.ts +32 -8
  85. package/dist/src/mind/primitives.js +117 -99
  86. package/dist/src/mind/rationale.d.ts +98 -86
  87. package/dist/src/mind/rationale.js +121 -113
  88. package/dist/src/mind/reasoning.d.ts +13 -2
  89. package/dist/src/mind/reasoning.js +166 -129
  90. package/dist/src/mind/recognition.d.ts +4 -1
  91. package/dist/src/mind/recognition.js +208 -183
  92. package/dist/src/mind/resonance.d.ts +22 -5
  93. package/dist/src/mind/resonance.js +0 -0
  94. package/dist/src/mind/trace.d.ts +25 -6
  95. package/dist/src/mind/trace.js +58 -50
  96. package/dist/src/mind/traverse.d.ts +58 -16
  97. package/dist/src/mind/traverse.js +357 -304
  98. package/dist/src/mind/types.d.ts +127 -120
  99. package/dist/src/mind/types.js +69 -60
  100. package/dist/src/rabitq-hnsw/src/database.d.ts +179 -177
  101. package/dist/src/rabitq-hnsw/src/database.js +359 -342
  102. package/dist/src/rabitq-hnsw/src/heap.d.ts +12 -12
  103. package/dist/src/rabitq-hnsw/src/heap.js +80 -75
  104. package/dist/src/rabitq-hnsw/src/hnsw.d.ts +103 -103
  105. package/dist/src/rabitq-hnsw/src/hnsw.js +454 -428
  106. package/dist/src/rabitq-hnsw/src/index.d.ts +6 -1
  107. package/dist/src/rabitq-hnsw/src/prng.d.ts +10 -10
  108. package/dist/src/rabitq-hnsw/src/prng.js +27 -25
  109. package/dist/src/rabitq-hnsw/src/rabitq.d.ts +69 -69
  110. package/dist/src/rabitq-hnsw/src/rabitq.js +262 -246
  111. package/dist/src/rabitq-hnsw/src/store.d.ts +132 -132
  112. package/dist/src/rabitq-hnsw/src/store.js +843 -752
  113. package/dist/src/rabitq-hnsw/test/hnsw.test.js +1054 -805
  114. package/dist/src/sema.d.ts +13 -9
  115. package/dist/src/sema.js +40 -26
  116. package/dist/src/store-sqlite.d.ts +166 -144
  117. package/dist/src/store-sqlite.js +660 -578
  118. package/dist/src/store.d.ts +630 -593
  119. package/dist/src/store.js +1581 -1440
  120. package/dist/src/vec.d.ts +9 -5
  121. package/dist/src/vec.js +73 -61
  122. package/package.json +2 -2
@@ -43,83 +43,85 @@
43
43
  * derives `conclusion` at an additional `cost`.
44
44
  */
45
45
  export interface Rule<I> {
46
- premises: readonly I[];
47
- conclusion: I;
48
- /** Local (edge) cost added on top of the premises' costs. Non-negative. */
49
- cost: number;
50
- /** The combinator this rule's firing uses at its conclusion:
51
- * • `"min"` (default, omitted) — Knuth/A* proper: the conclusion's cost is
52
- * the CHEAPEST of any rule that reaches it, every other route discarded.
53
- * The shortest-path monoid (min, +) that makes the search admissible and
54
- * output-sensitive.
55
- * • `"sum"` — evidence pooling: EVERY firing of a sum rule contributes its
56
- * cost to the SAME conclusion (accumulated in {@link
57
- * DeductionSystem.pool}), instead of competing to be the one cheapest
58
- * route. The (+, +) monoid a consensus vote needs — several
59
- * independent premises corroborating one conclusion — kept deliberately
60
- * OUT of the min-cost chart: a pooled conclusion is never relaxed into
61
- * `g`, never enters the agenda, and is never itself a premise — it is a
62
- * terminal aggregate the caller reads out of `pool` once the search is
63
- * done. */
64
- combine?: "min" | "sum";
46
+ premises: readonly I[];
47
+ conclusion: I;
48
+ /** Local (edge) cost added on top of the premises' costs. Non-negative. */
49
+ cost: number;
50
+ /** The combinator this rule's firing uses at its conclusion:
51
+ * • `"min"` (default, omitted) — Knuth/A* proper: the conclusion's cost is
52
+ * the CHEAPEST of any rule that reaches it, every other route discarded.
53
+ * The shortest-path monoid (min, +) that makes the search admissible and
54
+ * output-sensitive.
55
+ * • `"sum"` — evidence pooling: EVERY firing of a sum rule contributes its
56
+ * cost to the SAME conclusion (accumulated in {@link
57
+ * DeductionSystem.pool}), instead of competing to be the one cheapest
58
+ * route. The (+, +) monoid a consensus vote needs — several
59
+ * independent premises corroborating one conclusion — kept deliberately
60
+ * OUT of the min-cost chart: a pooled conclusion is never relaxed into
61
+ * `g`, never enters the agenda, and is never itself a premise — it is a
62
+ * terminal aggregate the caller reads out of `pool` once the search is
63
+ * done. */
64
+ combine?: "min" | "sum";
65
65
  }
66
66
  /** One rule's contribution to a pooled (`combine: "sum"`) conclusion — the
67
67
  * firing rule and the already-finalised derivations of its premises, so a
68
68
  * caller can render each contribution exactly as it would a min-cost step. */
69
69
  export interface PooledContribution<I> {
70
- rule: Rule<I>;
71
- premises: Array<Derivation<I>>;
70
+ rule: Rule<I>;
71
+ premises: Array<Derivation<I>>;
72
72
  }
73
73
  /** The running aggregate at one pooled conclusion: every sum-mode rule that
74
74
  * has fired for it, accumulated. */
75
75
  export interface PooledConclusion<I> {
76
- item: I;
77
- cost: number;
78
- contributions: Array<PooledContribution<I>>;
76
+ item: I;
77
+ cost: number;
78
+ contributions: Array<PooledContribution<I>>;
79
79
  }
80
80
  /** The problem the solver is given: items, rules, a goal, and a heuristic. */
81
81
  export interface DeductionSystem<I> {
82
- /** Canonical key for chart memoization (the item's boundary signature). */
83
- key(item: I): string;
84
- /** Axioms: the atomic items and their base costs (the search's seeds). */
85
- axioms(): Iterable<{
86
- item: I;
87
- cost: number;
88
- }>;
89
- /**
90
- * Lazily generate the demanded rules that have `item` among their premises.
91
- * Called once, when `item` is finalised. `costOf` returns the finalised cost
92
- * of any item (Infinity if not yet known) — use it for backward-demand /
93
- * boundary filtering, e.g. drop a rule whose other premises are still open or
94
- * whose conclusion can no longer beat the best goal.
95
- */
96
- rules(item: I, costOf: (other: I) => number): Iterable<Rule<I>>;
97
- /** Whether `item` satisfies the goal. The first finalised goal wins. */
98
- isGoal(item: I): boolean;
99
- /** Admissible, consistent lower bound on the cost from `item` to a goal. */
100
- heuristic?(item: I): number;
101
- /** Present only on a system that fires `combine: "sum"` rules — supplied
102
- * empty, populated in place as the search runs, read back once it returns
103
- * (typically `null`: a pooling system has no goal to reach, it exhausts its
104
- * axioms instead — see {@link lightestDerivation}). Absent on every
105
- * ordinary min-cost system, which is what keeps pooling a zero-cost opt-in:
106
- * `relax` only takes the pooling branch when a rule declares `combine:
107
- * "sum"` AND this map is present. */
108
- pool?: Map<string, PooledConclusion<I>>;
82
+ /** Canonical key for chart memoization (the item's boundary signature). */
83
+ key(item: I): string;
84
+ /** Axioms: the atomic items and their base costs (the search's seeds). */
85
+ axioms(): Iterable<{
86
+ item: I;
87
+ cost: number;
88
+ }>;
89
+ /**
90
+ * Lazily generate the demanded rules that have `item` among their premises.
91
+ * Called once, when `item` is finalised. `costOf` returns the finalised cost
92
+ * of any item (Infinity if not yet known) — use it for backward-demand /
93
+ * boundary filtering, e.g. drop a rule whose other premises are still open or
94
+ * whose conclusion can no longer beat the best goal.
95
+ */
96
+ rules(item: I, costOf: (other: I) => number): Iterable<Rule<I>>;
97
+ /** Whether `item` satisfies the goal. The first finalised goal wins. */
98
+ isGoal(item: I): boolean;
99
+ /** Admissible, consistent lower bound on the cost from `item` to a goal. */
100
+ heuristic?(item: I): number;
101
+ /** Present only on a system that fires `combine: "sum"` rules — supplied
102
+ * empty, populated in place as the search runs, read back once it returns
103
+ * (typically `null`: a pooling system has no goal to reach, it exhausts its
104
+ * axioms instead — see {@link lightestDerivation}). Absent on every
105
+ * ordinary min-cost system, which is what keeps pooling a zero-cost opt-in:
106
+ * `relax` only takes the pooling branch when a rule declares `combine:
107
+ * "sum"` AND this map is present. */
108
+ pool?: Map<string, PooledConclusion<I>>;
109
109
  }
110
110
  /** A node of the reconstructed derivation tree. */
111
111
  export interface Derivation<I> {
112
- /** The derived item. */
113
- item: I;
114
- /** This item's minimum derivation cost (its g value). */
115
- cost: number;
116
- /** The rule that produced it, or null if it is an axiom. */
117
- rule: Rule<I> | null;
118
- /** Derivations of the rule's premises (empty for an axiom). */
119
- premises: Array<Derivation<I>>;
112
+ /** The derived item. */
113
+ item: I;
114
+ /** This item's minimum derivation cost (its g value). */
115
+ cost: number;
116
+ /** The rule that produced it, or null if it is an axiom. */
117
+ rule: Rule<I> | null;
118
+ /** Derivations of the rule's premises (empty for an axiom). */
119
+ premises: Array<Derivation<I>>;
120
120
  }
121
121
  /**
122
122
  * Find a lightest derivation of a goal item, or `null` if none exists.
123
123
  * `cost` on the returned root is the total derivation cost.
124
124
  */
125
- export declare function lightestDerivation<I>(system: DeductionSystem<I>): Derivation<I> | null;
125
+ export declare function lightestDerivation<I>(
126
+ system: DeductionSystem<I>,
127
+ ): Derivation<I> | null;
@@ -44,112 +44,118 @@ import { MinHeap } from "./priority-queue.js";
44
44
  * `cost` on the returned root is the total derivation cost.
45
45
  */
46
46
  export function lightestDerivation(system) {
47
- const g = new Map(); // best known cost per item
48
- const proof = new Map(); // producing rule per item
49
- const items = new Map(); // key → the item it stands for
50
- const hCache = new Map();
51
- const agenda = new MinHeap();
52
- const heuristic = system.heuristic;
53
- const h = (item, key) => {
54
- if (!heuristic)
55
- return 0;
56
- let v = hCache.get(key);
57
- if (v === undefined) {
58
- v = heuristic(item);
59
- hCache.set(key, v);
60
- }
61
- return v;
62
- };
63
- const costOf = (item) => g.get(system.key(item)) ?? Infinity;
64
- const relax = (item, cost, rule) => {
65
- const key = system.key(item);
66
- if (rule?.combine === "sum" && system.pool) {
67
- // Evidence pooling: accumulate this firing rather than compete for the
68
- // cheapest see {@link Rule.combine}. The premises are already
69
- // finalised (the caller only relaxes a rule once every premise's cost
70
- // is known), so their derivations can be read back immediately.
71
- const premises = rule.premises.map((p) => reconstruct(p, system, g, proof));
72
- const prior = system.pool.get(key);
73
- system.pool.set(key, {
74
- item,
75
- cost: (prior?.cost ?? 0) + cost,
76
- contributions: [...(prior?.contributions ?? []), { rule, premises }],
77
- });
78
- return;
79
- }
80
- const current = g.get(key);
81
- if (current === undefined || cost < current) {
82
- g.set(key, cost);
83
- proof.set(key, rule);
84
- items.set(key, item);
85
- agenda.push(cost + h(item, key), { key, g: cost });
86
- }
87
- };
88
- for (const { item, cost } of system.axioms())
89
- relax(item, cost, null);
90
- while (agenda.size > 0) {
91
- const { value } = agenda.pop();
92
- const key = value.key;
93
- // Lazy deletion: an entry is stale if a cheaper derivation has since been
94
- // recorded for the same item.
95
- if (value.g !== g.get(key))
96
- continue;
97
- const item = items.get(key);
98
- if (system.isGoal(item)) {
99
- return reconstruct(item, system, g, proof);
100
- }
101
- for (const rule of system.rules(item, costOf)) {
102
- let sum = rule.cost;
103
- let ready = true;
104
- for (const p of rule.premises) {
105
- const pc = g.get(system.key(p));
106
- if (pc === undefined) {
107
- ready = false;
108
- break;
109
- }
110
- sum += pc;
111
- }
112
- if (ready)
113
- relax(rule.conclusion, sum, rule);
47
+ const g = new Map(); // best known cost per item
48
+ const proof = new Map(); // producing rule per item
49
+ const items = new Map(); // key → the item it stands for
50
+ const hCache = new Map();
51
+ const agenda = new MinHeap();
52
+ const heuristic = system.heuristic;
53
+ const h = (item, key) => {
54
+ if (!heuristic) {
55
+ return 0;
56
+ }
57
+ let v = hCache.get(key);
58
+ if (v === undefined) {
59
+ v = heuristic(item);
60
+ hCache.set(key, v);
61
+ }
62
+ return v;
63
+ };
64
+ const costOf = (item) => g.get(system.key(item)) ?? Infinity;
65
+ const relax = (item, cost, rule) => {
66
+ const key = system.key(item);
67
+ if (rule?.combine === "sum" && system.pool) {
68
+ // Evidence pooling: accumulate this firing rather than compete for the
69
+ // cheapest see {@link Rule.combine}. The premises are already
70
+ // finalised (the caller only relaxes a rule once every premise's cost
71
+ // is known), so their derivations can be read back immediately.
72
+ const premises = rule.premises.map((p) =>
73
+ reconstruct(p, system, g, proof)
74
+ );
75
+ const prior = system.pool.get(key);
76
+ system.pool.set(key, {
77
+ item,
78
+ cost: (prior?.cost ?? 0) + cost,
79
+ contributions: [...(prior?.contributions ?? []), { rule, premises }],
80
+ });
81
+ return;
82
+ }
83
+ const current = g.get(key);
84
+ if (current === undefined || cost < current) {
85
+ g.set(key, cost);
86
+ proof.set(key, rule);
87
+ items.set(key, item);
88
+ agenda.push(cost + h(item, key), { key, g: cost });
89
+ }
90
+ };
91
+ for (const { item, cost } of system.axioms()) {
92
+ relax(item, cost, null);
93
+ }
94
+ while (agenda.size > 0) {
95
+ const { value } = agenda.pop();
96
+ const key = value.key;
97
+ // Lazy deletion: an entry is stale if a cheaper derivation has since been
98
+ // recorded for the same item.
99
+ if (value.g !== g.get(key)) {
100
+ continue;
101
+ }
102
+ const item = items.get(key);
103
+ if (system.isGoal(item)) {
104
+ return reconstruct(item, system, g, proof);
105
+ }
106
+ for (const rule of system.rules(item, costOf)) {
107
+ let sum = rule.cost;
108
+ let ready = true;
109
+ for (const p of rule.premises) {
110
+ const pc = g.get(system.key(p));
111
+ if (pc === undefined) {
112
+ ready = false;
113
+ break;
114
114
  }
115
+ sum += pc;
116
+ }
117
+ if (ready) {
118
+ relax(rule.conclusion, sum, rule);
119
+ }
115
120
  }
116
- return null;
121
+ }
122
+ return null;
117
123
  }
118
124
  function reconstruct(item, system, g, proof) {
119
- // Iterative post-order over the derivation hypergraph. In the rewrite
120
- // search every rule has one premise, so the derivation is a chain whose
121
- // length equals the number of frontier edges — which, with long inputs,
122
- // can exceed the call stack. Multi-premise rules (the test-suite bridge
123
- // case) are handled by the same explicit stack.
124
- const done = new Map();
125
- const stack = [item];
126
- while (stack.length > 0) {
127
- const cur = stack[stack.length - 1]; // peek
128
- const key = system.key(cur);
129
- if (done.has(key)) {
130
- stack.pop();
131
- continue;
132
- }
133
- const rule = proof.get(key) ?? null;
134
- const premises = rule?.premises ?? [];
135
- // Push any unresolved premises (rightmost first → leftmost resolves first).
136
- let pending = false;
137
- for (let i = premises.length - 1; i >= 0; i--) {
138
- if (!done.has(system.key(premises[i]))) {
139
- stack.push(premises[i]);
140
- pending = true;
141
- }
142
- }
143
- if (!pending) {
144
- stack.pop(); // this item
145
- const kids = premises.map((p) => done.get(system.key(p)));
146
- done.set(key, {
147
- item: cur,
148
- cost: g.get(key),
149
- rule,
150
- premises: kids,
151
- });
152
- }
125
+ // Iterative post-order over the derivation hypergraph. In the rewrite
126
+ // search every rule has one premise, so the derivation is a chain whose
127
+ // length equals the number of frontier edges — which, with long inputs,
128
+ // can exceed the call stack. Multi-premise rules (the test-suite bridge
129
+ // case) are handled by the same explicit stack.
130
+ const done = new Map();
131
+ const stack = [item];
132
+ while (stack.length > 0) {
133
+ const cur = stack[stack.length - 1]; // peek
134
+ const key = system.key(cur);
135
+ if (done.has(key)) {
136
+ stack.pop();
137
+ continue;
138
+ }
139
+ const rule = proof.get(key) ?? null;
140
+ const premises = rule?.premises ?? [];
141
+ // Push any unresolved premises (rightmost first → leftmost resolves first).
142
+ let pending = false;
143
+ for (let i = premises.length - 1; i >= 0; i--) {
144
+ if (!done.has(system.key(premises[i]))) {
145
+ stack.push(premises[i]);
146
+ pending = true;
147
+ }
148
+ }
149
+ if (!pending) {
150
+ stack.pop(); // this item
151
+ const kids = premises.map((p) => done.get(system.key(p)));
152
+ done.set(key, {
153
+ item: cur,
154
+ cost: g.get(key),
155
+ rule,
156
+ premises: kids,
157
+ });
153
158
  }
154
- return done.get(system.key(item));
159
+ }
160
+ return done.get(system.key(item));
155
161
  }
@@ -1,5 +1,11 @@
1
1
  export { lightestDerivation } from "./deduction.js";
2
- export type { DeductionSystem, Derivation, PooledConclusion, PooledContribution, Rule, } from "./deduction.js";
2
+ export type {
3
+ DeductionSystem,
4
+ Derivation,
5
+ PooledConclusion,
6
+ PooledContribution,
7
+ Rule,
8
+ } from "./deduction.js";
3
9
  export { coverSequence } from "./rewrite.js";
4
10
  export type { CandidateSpan, Cover } from "./rewrite.js";
5
11
  export { Trie } from "./trie.js";
@@ -9,12 +9,12 @@
9
9
  * of objects) keep the hot path allocation-free.
10
10
  */
11
11
  export declare class MinHeap<T> {
12
- private readonly keys;
13
- private readonly vals;
14
- get size(): number;
15
- push(priority: number, value: T): void;
16
- pop(): {
17
- priority: number;
18
- value: T;
19
- } | undefined;
12
+ private readonly keys;
13
+ private readonly vals;
14
+ get size(): number;
15
+ push(priority: number, value: T): void;
16
+ pop(): {
17
+ priority: number;
18
+ value: T;
19
+ } | undefined;
20
20
  }
@@ -9,65 +9,69 @@
9
9
  * of objects) keep the hot path allocation-free.
10
10
  */
11
11
  export class MinHeap {
12
- keys = [];
13
- vals = [];
14
- get size() {
15
- return this.keys.length;
12
+ keys = [];
13
+ vals = [];
14
+ get size() {
15
+ return this.keys.length;
16
+ }
17
+ push(priority, value) {
18
+ const keys = this.keys;
19
+ const vals = this.vals;
20
+ let i = keys.length;
21
+ keys.push(priority);
22
+ vals.push(value);
23
+ while (i > 0) {
24
+ const parent = (i - 1) >> 1;
25
+ if (keys[i] < keys[parent]) {
26
+ const tk = keys[i];
27
+ keys[i] = keys[parent];
28
+ keys[parent] = tk;
29
+ const tv = vals[i];
30
+ vals[i] = vals[parent];
31
+ vals[parent] = tv;
32
+ i = parent;
33
+ } else {
34
+ break;
35
+ }
16
36
  }
17
- push(priority, value) {
18
- const keys = this.keys;
19
- const vals = this.vals;
20
- let i = keys.length;
21
- keys.push(priority);
22
- vals.push(value);
23
- while (i > 0) {
24
- const parent = (i - 1) >> 1;
25
- if (keys[i] < keys[parent]) {
26
- const tk = keys[i];
27
- keys[i] = keys[parent];
28
- keys[parent] = tk;
29
- const tv = vals[i];
30
- vals[i] = vals[parent];
31
- vals[parent] = tv;
32
- i = parent;
33
- }
34
- else
35
- break;
36
- }
37
+ }
38
+ pop() {
39
+ const keys = this.keys;
40
+ const vals = this.vals;
41
+ const n = keys.length;
42
+ if (n === 0) {
43
+ return undefined;
37
44
  }
38
- pop() {
39
- const keys = this.keys;
40
- const vals = this.vals;
41
- const n = keys.length;
42
- if (n === 0)
43
- return undefined;
44
- const top = { priority: keys[0], value: vals[0] };
45
- const lastKey = keys.pop();
46
- const lastVal = vals.pop();
47
- const m = keys.length;
48
- if (m > 0) {
49
- keys[0] = lastKey;
50
- vals[0] = lastVal;
51
- let i = 0;
52
- for (;;) {
53
- const left = 2 * i + 1;
54
- const right = left + 1;
55
- let best = i;
56
- if (left < m && keys[left] < keys[best])
57
- best = left;
58
- if (right < m && keys[right] < keys[best])
59
- best = right;
60
- if (best === i)
61
- break;
62
- const tk = keys[i];
63
- keys[i] = keys[best];
64
- keys[best] = tk;
65
- const tv = vals[i];
66
- vals[i] = vals[best];
67
- vals[best] = tv;
68
- i = best;
69
- }
45
+ const top = { priority: keys[0], value: vals[0] };
46
+ const lastKey = keys.pop();
47
+ const lastVal = vals.pop();
48
+ const m = keys.length;
49
+ if (m > 0) {
50
+ keys[0] = lastKey;
51
+ vals[0] = lastVal;
52
+ let i = 0;
53
+ for (;;) {
54
+ const left = 2 * i + 1;
55
+ const right = left + 1;
56
+ let best = i;
57
+ if (left < m && keys[left] < keys[best]) {
58
+ best = left;
59
+ }
60
+ if (right < m && keys[right] < keys[best]) {
61
+ best = right;
62
+ }
63
+ if (best === i) {
64
+ break;
70
65
  }
71
- return top;
66
+ const tk = keys[i];
67
+ keys[i] = keys[best];
68
+ keys[best] = tk;
69
+ const tv = vals[i];
70
+ vals[i] = vals[best];
71
+ vals[best] = tv;
72
+ i = best;
73
+ }
72
74
  }
75
+ return top;
76
+ }
73
77
  }
@@ -21,22 +21,22 @@
21
21
  */
22
22
  /** A candidate span over a sequence, carrying caller payload. */
23
23
  export interface CandidateSpan<P> {
24
- /** Start offset (inclusive). */
25
- start: number;
26
- /** End offset (exclusive); must be > start. */
27
- end: number;
28
- /** Relative cost of using this span (default 1). Lower wins ties. */
29
- weight?: number;
30
- /** Caller data, returned on the chosen spans. */
31
- payload: P;
24
+ /** Start offset (inclusive). */
25
+ start: number;
26
+ /** End offset (exclusive); must be > start. */
27
+ end: number;
28
+ /** Relative cost of using this span (default 1). Lower wins ties. */
29
+ weight?: number;
30
+ /** Caller data, returned on the chosen spans. */
31
+ payload: P;
32
32
  }
33
33
  export interface Cover<P> {
34
- /** Chosen non-overlapping spans, left to right. */
35
- spans: Array<CandidateSpan<P>>;
36
- /** Symbols covered by the chosen spans. */
37
- covered: number;
38
- /** Symbols left uncovered. */
39
- uncovered: number;
34
+ /** Chosen non-overlapping spans, left to right. */
35
+ spans: Array<CandidateSpan<P>>;
36
+ /** Symbols covered by the chosen spans. */
37
+ covered: number;
38
+ /** Symbols left uncovered. */
39
+ uncovered: number;
40
40
  }
41
41
  /**
42
42
  * The lightest non-overlapping cover of `[0, length)` drawn from `candidates`.
@@ -53,4 +53,7 @@ export interface Cover<P> {
53
53
  * items, each finalised once, with one "skip" edge plus the candidates that
54
54
  * start there. No quadratic span structure.
55
55
  */
56
- export declare function coverSequence<P>(length: number, candidates: ReadonlyArray<CandidateSpan<P>>): Cover<P>;
56
+ export declare function coverSequence<P>(
57
+ length: number,
58
+ candidates: ReadonlyArray<CandidateSpan<P>>,
59
+ ): Cover<P>;