@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
149 lines • 5.83 kB
JavaScript
/**
* One compaction round's sorted changes (undefined deletes) merged into the AVL in one descent, with
* join-based rebalancing (senpi#4272, RULING-47). Changed nodes stay drafts until the final tree is
* made bottom-up, so each node of the result is built, owned and registered for commit exactly once;
* a node replaced inside the round is never built at all.
*/
export class CompactPagedBatch {
constructor(tree) {
this.changes = [];
this.tree = tree;
}
/** Returns an owned root; the caller keeps `root` live until this settles. */
async apply(root, changes) {
this.changes = [...changes].sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0));
return this.build(await this.merge(root, 0, this.changes.length));
}
async merge(sub, from, to) {
if (from === to)
return sub;
if (!sub)
return this.fresh(from, to);
const node = await this.open(sub);
let split = from;
while (split < to && this.changes[split][0] < node.name)
split++;
const hit = split < to && this.changes[split][0] === node.name;
const left = await this.merge(node.left, from, split);
const right = await this.merge(node.right, hit ? split + 1 : split, to);
if (!hit && left === node.left && right === node.right)
return sub;
const value = hit ? this.changes[split][1] : node.value;
if (value === undefined)
return this.concat(left, right);
node.value = value;
return this.join(left, node, right);
}
/** A perfectly balanced subtree of the inserts in an empty position; deletes of absent keys are no-ops. */
fresh(from, to) {
const rows = [];
for (const [name, value] of this.changes.slice(from, to))
if (value !== undefined)
rows.push([name, value]);
const grow = (lo, hi) => {
if (lo === hi)
return undefined;
const mid = (lo + hi) >>> 1;
const [name, value] = rows[mid];
const left = grow(lo, mid);
return this.fix({ key: undefined, name, value, left, right: grow(mid + 1, hi), height: 0, size: 0 });
};
return grow(0, rows.length);
}
/** AVL join: descends the taller side's spine to a height match, rebalancing once per level. */
async join(left, node, right) {
const lh = left?.height ?? 0;
const rh = right?.height ?? 0;
if (left && lh > rh + 1) {
const top = await this.open(left);
top.right = await this.join(top.right, node, right);
return this.balance(top);
}
if (right && rh > lh + 1) {
const top = await this.open(right);
top.left = await this.join(left, node, top.left);
return this.balance(top);
}
node.left = left;
node.right = right;
return this.fix(node);
}
async concat(left, right) {
if (!left)
return right;
if (!right)
return left;
const [min, rest] = await this.takeMin(right);
return this.join(left, min, rest);
}
async takeMin(sub) {
const node = await this.open(sub);
if (!node.left)
return [node, node.right];
const [min, rest] = await this.takeMin(node.left);
node.left = rest;
return [min, await this.balance(node)];
}
/** One single or double rotation restores a node whose children differ in height by two. */
async balance(node) {
const leftHeavy = (node.left?.height ?? 0) > (node.right?.height ?? 0);
const side = leftHeavy ? "left" : "right";
const other = leftHeavy ? "right" : "left";
const heavy = node[side];
if (!heavy || heavy.height - (node[other]?.height ?? 0) < 2)
return this.fix(node);
const child = await this.open(heavy);
const inner = child[other];
if (inner && inner.height > (child[side]?.height ?? 0)) {
const pivot = await this.open(inner);
child[other] = pivot[side];
node[side] = pivot[other];
pivot[side] = this.fix(child);
pivot[other] = this.fix(node);
return this.fix(pivot);
}
node[side] = inner;
child[other] = this.fix(node);
return this.fix(child);
}
fix(node) {
node.height = 1 + Math.max(node.left?.height ?? 0, node.right?.height ?? 0);
node.size = 1 + (node.left?.size ?? 0) + (node.right?.size ?? 0);
return node;
}
async open(sub) {
if (!("ref" in sub))
return sub;
const { node, name } = await this.tree.read(sub);
const { key, value, left, right } = node;
return { key, name, value, left, right, height: sub.height, size: sub.size };
}
/** Makes each draft once, children first; every result and intermediate is owned exactly once. */
async build(sub) {
if (!sub || "ref" in sub) {
this.tree.adjust(sub, 1);
return sub;
}
const left = await this.build(sub.left);
try {
const right = await this.build(sub.right);
try {
const key = sub.key ?? this.tree.own(sub.name);
try {
return await this.tree.make({ key, value: sub.value, left, right });
}
finally {
if (!sub.key)
this.tree.release(key);
}
}
finally {
this.tree.adjust(right, -1);
}
}
finally {
this.tree.adjust(left, -1);
}
}
}
//# sourceMappingURL=compact-paged-batch.js.map