/** * Copyright 2026 aphrody-code * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * @module bxc/queue/frontier/heap * * Indexed binary heap used by the crawl frontier. * * Every node stores its own position (`heapIndex`), which makes `remove()` and * `update()` (increase-key / decrease-key) O(log n) without lazy tombstones. * A node may belong to at most one heap at a time because the index slot is * shared. */ /** A value that can live in an {@link IndexedHeap}. `-1` means "not in a heap". */ export interface HeapNode { heapIndex: number; } /** * Binary heap ordered by a strict "comes before" predicate. * `before(a, b) === true` means `a` is popped before `b`. */ export class IndexedHeap { #items: T[] = []; readonly #before: (a: T, b: T) => boolean; constructor(before: (a: T, b: T) => boolean) { this.#before = before; } get size(): number { return this.#items.length; } /** Highest-priority node, or `undefined` when empty. */ peek(): T | undefined { return this.#items[0]; } /** Whether `node` currently sits in this heap. */ has(node: T): boolean { const i = node.heapIndex; return i >= 0 && i < this.#items.length && this.#items[i] === node; } push(node: T): void { const i = this.#items.length; this.#items.push(node); node.heapIndex = i; this.#siftUp(i); } /** * Push many nodes at once. When the batch is large relative to the heap, * append everything and rebuild in O(n) (Floyd) instead of n·log n pushes. */ pushAll(nodes: readonly T[]): void { if (nodes.length === 0) return; if (nodes.length < 16 || nodes.length < this.#items.length) { for (const node of nodes) this.push(node); return; } const items = this.#items; for (const node of nodes) { node.heapIndex = items.length; items.push(node); } for (let i = (items.length >> 1) - 1; i >= 0; i--) this.#siftDown(i); } pop(): T | undefined { const items = this.#items; if (items.length === 0) return undefined; const top = items[0]; const last = items.pop() as T; if (items.length > 0) { items[0] = last; last.heapIndex = 0; this.#siftDown(0); } top.heapIndex = -1; return top; } /** Remove an arbitrary node. Returns `false` when it was not in the heap. */ remove(node: T): boolean { if (!this.has(node)) return false; const items = this.#items; const i = node.heapIndex; const last = items.pop() as T; if (last !== node) { items[i] = last; last.heapIndex = i; this.#siftUp(i); this.#siftDown(last.heapIndex); } node.heapIndex = -1; return true; } /** Restore heap order after the key of `node` changed in either direction. */ update(node: T): void { if (!this.has(node)) return; this.#siftUp(node.heapIndex); this.#siftDown(node.heapIndex); } /** Re-establish heap order for every node (after a bulk key change). */ rebuild(): void { const items = this.#items; for (let i = (items.length >> 1) - 1; i >= 0; i--) this.#siftDown(i); } clear(): void { for (const node of this.#items) node.heapIndex = -1; this.#items = []; } /** Unordered snapshot of the nodes (do not mutate keys while iterating). */ values(): readonly T[] { return this.#items; } #siftUp(start: number): void { const items = this.#items; const node = items[start]; let i = start; while (i > 0) { const parentIndex = (i - 1) >> 1; const parent = items[parentIndex]; if (!this.#before(node, parent)) break; items[i] = parent; parent.heapIndex = i; i = parentIndex; } items[i] = node; node.heapIndex = i; } #siftDown(start: number): void { const items = this.#items; const length = items.length; const node = items[start]; let i = start; while (true) { const left = 2 * i + 1; if (left >= length) break; const right = left + 1; let child = left; if (right < length && this.#before(items[right], items[left])) child = right; if (!this.#before(items[child], node)) break; items[i] = items[child]; items[i].heapIndex = i; i = child; } items[i] = node; node.heapIndex = i; } }