@hviana/sema 0.1.3 → 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.
- package/dist/example/demo.js +25 -25
- package/dist/example/train_base.d.ts +21 -19
- package/dist/example/train_base.js +1952 -1684
- package/dist/src/alphabet.d.ts +3 -3
- package/dist/src/alphabet.js +27 -24
- package/dist/src/alu/src/alu.d.ts +194 -169
- package/dist/src/alu/src/alu.js +414 -374
- package/dist/src/alu/src/expr.d.ts +43 -36
- package/dist/src/alu/src/expr.js +278 -252
- package/dist/src/alu/src/index.d.ts +98 -9
- package/dist/src/alu/src/index.js +63 -9
- package/dist/src/alu/src/kernel-arith.d.ts +5 -1
- package/dist/src/alu/src/kernel-arith.js +285 -195
- package/dist/src/alu/src/kernel-bits.d.ts +5 -1
- package/dist/src/alu/src/kernel-bits.js +115 -81
- package/dist/src/alu/src/kernel-logic.js +63 -33
- package/dist/src/alu/src/kernel-nd.js +195 -155
- package/dist/src/alu/src/kernel-numeric.d.ts +80 -16
- package/dist/src/alu/src/kernel-numeric.js +331 -259
- package/dist/src/alu/src/operation.d.ts +132 -112
- package/dist/src/alu/src/operation.js +163 -149
- package/dist/src/alu/src/parser.d.ts +175 -168
- package/dist/src/alu/src/parser.js +421 -382
- package/dist/src/alu/src/resonance.d.ts +25 -16
- package/dist/src/alu/src/resonance.js +56 -49
- package/dist/src/alu/src/text.d.ts +7 -3
- package/dist/src/alu/src/text.js +37 -31
- package/dist/src/alu/src/value.d.ts +14 -14
- package/dist/src/alu/src/value.js +160 -150
- package/dist/src/alu/test/alu.test.js +656 -543
- package/dist/src/bytes.d.ts +5 -1
- package/dist/src/bytes.js +40 -33
- package/dist/src/config.d.ts +102 -102
- package/dist/src/config.js +83 -83
- package/dist/src/derive/src/deduction.d.ts +62 -60
- package/dist/src/derive/src/deduction.js +109 -103
- package/dist/src/derive/src/index.d.ts +7 -1
- package/dist/src/derive/src/priority-queue.d.ts +8 -8
- package/dist/src/derive/src/priority-queue.js +61 -57
- package/dist/src/derive/src/rewrite.d.ts +18 -15
- package/dist/src/derive/src/rewrite.js +67 -59
- package/dist/src/derive/src/trie.d.ts +56 -56
- package/dist/src/derive/src/trie.js +179 -175
- package/dist/src/derive/test/derive.test.js +100 -96
- package/dist/src/extension.d.ts +16 -13
- package/dist/src/geometry.d.ts +42 -17
- package/dist/src/geometry.js +266 -235
- package/dist/src/index.d.ts +17 -2
- package/dist/src/index.js +7 -1
- package/dist/src/ingest-cache.d.ts +30 -25
- package/dist/src/ingest-cache.js +127 -108
- package/dist/src/mind/articulation.d.ts +5 -1
- package/dist/src/mind/articulation.js +112 -77
- package/dist/src/mind/attention.d.ts +108 -40
- package/dist/src/mind/attention.js +871 -749
- package/dist/src/mind/canonical.d.ts +19 -4
- package/dist/src/mind/canonical.js +31 -27
- package/dist/src/mind/graph-search.d.ts +225 -201
- package/dist/src/mind/graph-search.js +821 -742
- package/dist/src/mind/index.d.ts +10 -2
- package/dist/src/mind/junction.d.ts +58 -31
- package/dist/src/mind/junction.js +237 -172
- package/dist/src/mind/learning.d.ts +52 -16
- package/dist/src/mind/learning.js +165 -143
- package/dist/src/mind/match.d.ts +69 -20
- package/dist/src/mind/match.js +318 -259
- package/dist/src/mind/mechanisms/alu.js +16 -16
- package/dist/src/mind/mechanisms/cast.d.ts +13 -9
- package/dist/src/mind/mechanisms/cast.js +456 -363
- package/dist/src/mind/mechanisms/confluence.d.ts +12 -8
- package/dist/src/mind/mechanisms/confluence.js +183 -152
- package/dist/src/mind/mechanisms/cover.d.ts +8 -2
- package/dist/src/mind/mechanisms/cover.js +210 -148
- package/dist/src/mind/mechanisms/extraction.d.ts +34 -8
- package/dist/src/mind/mechanisms/extraction.js +288 -234
- package/dist/src/mind/mechanisms/recall.d.ts +10 -6
- package/dist/src/mind/mechanisms/recall.js +185 -126
- package/dist/src/mind/mind.d.ts +154 -129
- package/dist/src/mind/mind.js +288 -258
- package/dist/src/mind/pipeline-mechanism.d.ts +124 -112
- package/dist/src/mind/pipeline-mechanism.js +172 -161
- package/dist/src/mind/pipeline.d.ts +14 -4
- package/dist/src/mind/pipeline.js +189 -125
- package/dist/src/mind/primitives.d.ts +32 -8
- package/dist/src/mind/primitives.js +117 -99
- package/dist/src/mind/rationale.d.ts +98 -86
- package/dist/src/mind/rationale.js +121 -113
- package/dist/src/mind/reasoning.d.ts +13 -2
- package/dist/src/mind/reasoning.js +166 -129
- package/dist/src/mind/recognition.d.ts +4 -1
- package/dist/src/mind/recognition.js +208 -183
- package/dist/src/mind/resonance.d.ts +22 -5
- package/dist/src/mind/resonance.js +0 -0
- package/dist/src/mind/trace.d.ts +25 -6
- package/dist/src/mind/trace.js +58 -50
- package/dist/src/mind/traverse.d.ts +58 -16
- package/dist/src/mind/traverse.js +357 -304
- package/dist/src/mind/types.d.ts +127 -120
- package/dist/src/mind/types.js +69 -60
- package/dist/src/rabitq-hnsw/src/database.d.ts +179 -177
- package/dist/src/rabitq-hnsw/src/database.js +359 -342
- package/dist/src/rabitq-hnsw/src/heap.d.ts +12 -12
- package/dist/src/rabitq-hnsw/src/heap.js +80 -75
- package/dist/src/rabitq-hnsw/src/hnsw.d.ts +103 -103
- package/dist/src/rabitq-hnsw/src/hnsw.js +454 -428
- package/dist/src/rabitq-hnsw/src/index.d.ts +6 -1
- package/dist/src/rabitq-hnsw/src/prng.d.ts +10 -10
- package/dist/src/rabitq-hnsw/src/prng.js +27 -25
- package/dist/src/rabitq-hnsw/src/rabitq.d.ts +69 -69
- package/dist/src/rabitq-hnsw/src/rabitq.js +262 -246
- package/dist/src/rabitq-hnsw/src/store.d.ts +132 -132
- package/dist/src/rabitq-hnsw/src/store.js +843 -752
- package/dist/src/rabitq-hnsw/test/hnsw.test.js +1054 -805
- package/dist/src/sema.d.ts +35 -0
- package/dist/src/sema.js +77 -0
- package/dist/src/store-sqlite.d.ts +166 -144
- package/dist/src/store-sqlite.js +660 -578
- package/dist/src/store.d.ts +630 -593
- package/dist/src/store.js +1581 -1440
- package/dist/src/vec.d.ts +9 -5
- package/dist/src/vec.js +73 -61
- package/package.json +2 -2
- package/src/sema.ts +102 -0
|
@@ -43,83 +43,85 @@
|
|
|
43
43
|
* derives `conclusion` at an additional `cost`.
|
|
44
44
|
*/
|
|
45
45
|
export interface Rule<I> {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
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
|
-
|
|
71
|
-
|
|
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
|
-
|
|
77
|
-
|
|
78
|
-
|
|
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
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
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
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
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>(
|
|
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
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
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
|
-
|
|
121
|
+
}
|
|
122
|
+
return null;
|
|
117
123
|
}
|
|
118
124
|
function reconstruct(item, system, g, proof) {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
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
|
-
|
|
159
|
+
}
|
|
160
|
+
return done.get(system.key(item));
|
|
155
161
|
}
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
export { lightestDerivation } from "./deduction.js";
|
|
2
|
-
export type {
|
|
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
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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
|
-
|
|
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
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
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>(
|
|
56
|
+
export declare function coverSequence<P>(
|
|
57
|
+
length: number,
|
|
58
|
+
candidates: ReadonlyArray<CandidateSpan<P>>,
|
|
59
|
+
): Cover<P>;
|