UNPKG

@tanstack/db-ivm

Version:

Incremental View Maintenance for TanStack DB based on Differential Dataflow

40 lines (39 loc) 1.31 kB
import { MultiSet } from "../multiset.js"; import { reduce } from "./reduce.js"; function topK(comparator, options) { const limit = (options == null ? void 0 : options.limit) ?? Infinity; const offset = (options == null ? void 0 : options.offset) ?? 0; return (stream) => { const reduced = stream.pipe( reduce((values) => { const consolidated = new MultiSet(values).consolidate(); const sortedValues = consolidated.getInner().sort((a, b) => comparator(a[0], b[0])); return sortedValues.slice(offset, offset + limit); }) ); return reduced; }; } function topKWithIndex(comparator, options) { const limit = (options == null ? void 0 : options.limit) ?? Infinity; const offset = (options == null ? void 0 : options.offset) ?? 0; return (stream) => { const reduced = stream.pipe( reduce((values) => { const consolidated = new MultiSet(values).consolidate(); let i = offset; const sortedValues = consolidated.getInner().sort((a, b) => comparator(a[0], b[0])).slice(offset, offset + limit).map(([value, multiplicity]) => [ [value, i++], multiplicity ]); return sortedValues; }) ); return reduced; }; } export { topK, topKWithIndex }; //# sourceMappingURL=topK.js.map