@tanstack/db-ivm 0.1.8 → 0.1.10
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/cjs/d2.cjs +25 -49
- package/dist/cjs/d2.cjs.map +1 -1
- package/dist/cjs/graph.cjs +8 -20
- package/dist/cjs/graph.cjs.map +1 -1
- package/dist/cjs/indexes.cjs +118 -79
- package/dist/cjs/indexes.cjs.map +1 -1
- package/dist/cjs/indexes.d.cts +23 -0
- package/dist/cjs/multiset.cjs +106 -115
- package/dist/cjs/multiset.cjs.map +1 -1
- package/dist/cjs/multiset.d.cts +1 -0
- package/dist/cjs/operators/debug.cjs +5 -15
- package/dist/cjs/operators/debug.cjs.map +1 -1
- package/dist/cjs/operators/distinct.cjs +11 -22
- package/dist/cjs/operators/distinct.cjs.map +1 -1
- package/dist/cjs/operators/filter.cjs +3 -12
- package/dist/cjs/operators/filter.cjs.map +1 -1
- package/dist/cjs/operators/filterBy.cjs.map +1 -1
- package/dist/cjs/operators/filterBy.d.cts +1 -2
- package/dist/cjs/operators/groupBy.cjs +13 -12
- package/dist/cjs/operators/groupBy.cjs.map +1 -1
- package/dist/cjs/operators/groupBy.d.cts +7 -4
- package/dist/cjs/operators/join.cjs +104 -93
- package/dist/cjs/operators/join.cjs.map +1 -1
- package/dist/cjs/operators/join.d.cts +11 -8
- package/dist/cjs/operators/map.cjs +3 -12
- package/dist/cjs/operators/map.cjs.map +1 -1
- package/dist/cjs/operators/orderBy.cjs +10 -10
- package/dist/cjs/operators/orderBy.cjs.map +1 -1
- package/dist/cjs/operators/orderBy.d.cts +1 -2
- package/dist/cjs/operators/output.cjs +3 -12
- package/dist/cjs/operators/output.cjs.map +1 -1
- package/dist/cjs/operators/reduce.cjs +11 -22
- package/dist/cjs/operators/reduce.cjs.map +1 -1
- package/dist/cjs/operators/tap.cjs +3 -12
- package/dist/cjs/operators/tap.cjs.map +1 -1
- package/dist/cjs/operators/topK.cjs +4 -4
- package/dist/cjs/operators/topK.cjs.map +1 -1
- package/dist/cjs/operators/topK.d.cts +1 -2
- package/dist/cjs/operators/topKWithFractionalIndex.cjs +56 -75
- package/dist/cjs/operators/topKWithFractionalIndex.cjs.map +1 -1
- package/dist/esm/d2.js +25 -49
- package/dist/esm/d2.js.map +1 -1
- package/dist/esm/graph.js +8 -20
- package/dist/esm/graph.js.map +1 -1
- package/dist/esm/indexes.d.ts +23 -0
- package/dist/esm/indexes.js +118 -79
- package/dist/esm/indexes.js.map +1 -1
- package/dist/esm/multiset.d.ts +1 -0
- package/dist/esm/multiset.js +106 -115
- package/dist/esm/multiset.js.map +1 -1
- package/dist/esm/operators/debug.js +5 -15
- package/dist/esm/operators/debug.js.map +1 -1
- package/dist/esm/operators/distinct.js +11 -22
- package/dist/esm/operators/distinct.js.map +1 -1
- package/dist/esm/operators/filter.js +3 -12
- package/dist/esm/operators/filter.js.map +1 -1
- package/dist/esm/operators/filterBy.d.ts +1 -2
- package/dist/esm/operators/filterBy.js.map +1 -1
- package/dist/esm/operators/groupBy.d.ts +7 -4
- package/dist/esm/operators/groupBy.js +13 -12
- package/dist/esm/operators/groupBy.js.map +1 -1
- package/dist/esm/operators/join.d.ts +11 -8
- package/dist/esm/operators/join.js +104 -93
- package/dist/esm/operators/join.js.map +1 -1
- package/dist/esm/operators/map.js +3 -12
- package/dist/esm/operators/map.js.map +1 -1
- package/dist/esm/operators/orderBy.d.ts +1 -2
- package/dist/esm/operators/orderBy.js +10 -10
- package/dist/esm/operators/orderBy.js.map +1 -1
- package/dist/esm/operators/output.js +3 -12
- package/dist/esm/operators/output.js.map +1 -1
- package/dist/esm/operators/reduce.js +11 -22
- package/dist/esm/operators/reduce.js.map +1 -1
- package/dist/esm/operators/tap.js +3 -12
- package/dist/esm/operators/tap.js.map +1 -1
- package/dist/esm/operators/topK.d.ts +1 -2
- package/dist/esm/operators/topK.js +4 -4
- package/dist/esm/operators/topK.js.map +1 -1
- package/dist/esm/operators/topKWithFractionalIndex.js +56 -75
- package/dist/esm/operators/topKWithFractionalIndex.js.map +1 -1
- package/package.json +1 -1
- package/src/indexes.ts +54 -0
- package/src/multiset.ts +6 -0
- package/src/operators/filterBy.ts +1 -2
- package/src/operators/groupBy.ts +38 -18
- package/src/operators/join.ts +249 -139
- package/src/operators/orderBy.ts +1 -2
- package/src/operators/topK.ts +1 -2
|
@@ -1,26 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __typeError = (msg) => {
|
|
3
|
-
throw TypeError(msg);
|
|
4
|
-
};
|
|
5
|
-
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
6
|
-
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
7
|
-
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
8
|
-
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
|
|
9
|
-
var _f;
|
|
10
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
11
3
|
const graph = require("../graph.cjs");
|
|
12
4
|
const d2 = require("../d2.cjs");
|
|
13
5
|
class MapOperator extends graph.LinearUnaryOperator {
|
|
6
|
+
#f;
|
|
14
7
|
constructor(id, inputA, output, f) {
|
|
15
8
|
super(id, inputA, output);
|
|
16
|
-
|
|
17
|
-
__privateSet(this, _f, f);
|
|
9
|
+
this.#f = f;
|
|
18
10
|
}
|
|
19
11
|
inner(collection) {
|
|
20
|
-
return collection.map(
|
|
12
|
+
return collection.map(this.#f);
|
|
21
13
|
}
|
|
22
14
|
}
|
|
23
|
-
_f = new WeakMap();
|
|
24
15
|
function map(f) {
|
|
25
16
|
return (stream) => {
|
|
26
17
|
const output = new d2.StreamBuilder(
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"map.cjs","sources":["../../../src/operators/map.ts"],"sourcesContent":["import { DifferenceStreamWriter, LinearUnaryOperator } from \"../graph.js\"\nimport { StreamBuilder } from \"../d2.js\"\nimport type { IStreamBuilder, PipedOperator } from \"../types.js\"\nimport type { DifferenceStreamReader } from \"../graph.js\"\nimport type { MultiSet } from \"../multiset.js\"\n\n/**\n * Operator that applies a function to each element in the input stream\n */\nexport class MapOperator<T, U> extends LinearUnaryOperator<T, U> {\n #f: (data: T) => U\n\n constructor(\n id: number,\n inputA: DifferenceStreamReader<T>,\n output: DifferenceStreamWriter<U>,\n f: (data: T) => U\n ) {\n super(id, inputA, output)\n this.#f = f\n }\n\n inner(collection: MultiSet<T>): MultiSet<U> {\n return collection.map(this.#f)\n }\n}\n\n/**\n * Applies a function to each element in the input stream\n * @param f - The function to apply to each element\n */\nexport function map<T, O>(f: (data: T) => O): PipedOperator<T, O> {\n return (stream: IStreamBuilder<T>): IStreamBuilder<O> => {\n const output = new StreamBuilder<O>(\n stream.graph,\n new DifferenceStreamWriter<O>()\n )\n const operator = new MapOperator<T, O>(\n stream.graph.getNextOperatorId(),\n stream.connectReader(),\n output.writer,\n f\n )\n stream.graph.addOperator(operator)\n return output\n }\n}\n"],"names":["LinearUnaryOperator","StreamBuilder","DifferenceStreamWriter"],"mappings":"
|
|
1
|
+
{"version":3,"file":"map.cjs","sources":["../../../src/operators/map.ts"],"sourcesContent":["import { DifferenceStreamWriter, LinearUnaryOperator } from \"../graph.js\"\nimport { StreamBuilder } from \"../d2.js\"\nimport type { IStreamBuilder, PipedOperator } from \"../types.js\"\nimport type { DifferenceStreamReader } from \"../graph.js\"\nimport type { MultiSet } from \"../multiset.js\"\n\n/**\n * Operator that applies a function to each element in the input stream\n */\nexport class MapOperator<T, U> extends LinearUnaryOperator<T, U> {\n #f: (data: T) => U\n\n constructor(\n id: number,\n inputA: DifferenceStreamReader<T>,\n output: DifferenceStreamWriter<U>,\n f: (data: T) => U\n ) {\n super(id, inputA, output)\n this.#f = f\n }\n\n inner(collection: MultiSet<T>): MultiSet<U> {\n return collection.map(this.#f)\n }\n}\n\n/**\n * Applies a function to each element in the input stream\n * @param f - The function to apply to each element\n */\nexport function map<T, O>(f: (data: T) => O): PipedOperator<T, O> {\n return (stream: IStreamBuilder<T>): IStreamBuilder<O> => {\n const output = new StreamBuilder<O>(\n stream.graph,\n new DifferenceStreamWriter<O>()\n )\n const operator = new MapOperator<T, O>(\n stream.graph.getNextOperatorId(),\n stream.connectReader(),\n output.writer,\n f\n )\n stream.graph.addOperator(operator)\n return output\n }\n}\n"],"names":["LinearUnaryOperator","StreamBuilder","DifferenceStreamWriter"],"mappings":";;;;AASO,MAAM,oBAA0BA,MAAAA,oBAA0B;AAAA,EAC/D;AAAA,EAEA,YACE,IACA,QACA,QACA,GACA;AACA,UAAM,IAAI,QAAQ,MAAM;AACxB,SAAK,KAAK;AAAA,EACZ;AAAA,EAEA,MAAM,YAAsC;AAC1C,WAAO,WAAW,IAAI,KAAK,EAAE;AAAA,EAC/B;AACF;AAMO,SAAS,IAAU,GAAwC;AAChE,SAAO,CAAC,WAAiD;AACvD,UAAM,SAAS,IAAIC,GAAAA;AAAAA,MACjB,OAAO;AAAA,MACP,IAAIC,MAAAA,uBAAA;AAAA,IAA0B;AAEhC,UAAM,WAAW,IAAI;AAAA,MACnB,OAAO,MAAM,kBAAA;AAAA,MACb,OAAO,cAAA;AAAA,MACP,OAAO;AAAA,MACP;AAAA,IAAA;AAEF,WAAO,MAAM,YAAY,QAAQ;AACjC,WAAO;AAAA,EACT;AACF;;;"}
|
|
@@ -6,9 +6,9 @@ const map = require("./map.cjs");
|
|
|
6
6
|
const join = require("./join.cjs");
|
|
7
7
|
const consolidate = require("./consolidate.cjs");
|
|
8
8
|
function orderBy(valueExtractor, options) {
|
|
9
|
-
const limit =
|
|
10
|
-
const offset =
|
|
11
|
-
const comparator =
|
|
9
|
+
const limit = options?.limit ?? Infinity;
|
|
10
|
+
const offset = options?.offset ?? 0;
|
|
11
|
+
const comparator = options?.comparator ?? ((a, b) => {
|
|
12
12
|
if (a === b) return 0;
|
|
13
13
|
if (a < b) return -1;
|
|
14
14
|
return 1;
|
|
@@ -37,9 +37,9 @@ function orderBy(valueExtractor, options) {
|
|
|
37
37
|
};
|
|
38
38
|
}
|
|
39
39
|
function orderByWithIndex(valueExtractor, options) {
|
|
40
|
-
const limit =
|
|
41
|
-
const offset =
|
|
42
|
-
const comparator =
|
|
40
|
+
const limit = options?.limit ?? Infinity;
|
|
41
|
+
const offset = options?.offset ?? 0;
|
|
42
|
+
const comparator = options?.comparator ?? ((a, b) => {
|
|
43
43
|
if (a === b) return 0;
|
|
44
44
|
if (a < b) return -1;
|
|
45
45
|
return 1;
|
|
@@ -68,10 +68,10 @@ function orderByWithIndex(valueExtractor, options) {
|
|
|
68
68
|
};
|
|
69
69
|
}
|
|
70
70
|
function orderByWithFractionalIndexBase(topKFunction, valueExtractor, options) {
|
|
71
|
-
const limit =
|
|
72
|
-
const offset =
|
|
73
|
-
const setSizeCallback = options
|
|
74
|
-
const comparator =
|
|
71
|
+
const limit = options?.limit ?? Infinity;
|
|
72
|
+
const offset = options?.offset ?? 0;
|
|
73
|
+
const setSizeCallback = options?.setSizeCallback;
|
|
74
|
+
const comparator = options?.comparator ?? ((a, b) => {
|
|
75
75
|
if (a === b) return 0;
|
|
76
76
|
if (a < b) return -1;
|
|
77
77
|
return 1;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"orderBy.cjs","sources":["../../../src/operators/orderBy.ts"],"sourcesContent":["import { topK, topKWithIndex } from \"./topK.js\"\nimport { topKWithFractionalIndex } from \"./topKWithFractionalIndex.js\"\nimport { map } from \"./map.js\"\nimport { innerJoin } from \"./join.js\"\nimport { consolidate } from \"./consolidate.js\"\nimport type { KeyValue } from \"../types.js\"\
|
|
1
|
+
{"version":3,"file":"orderBy.cjs","sources":["../../../src/operators/orderBy.ts"],"sourcesContent":["import { topK, topKWithIndex } from \"./topK.js\"\nimport { topKWithFractionalIndex } from \"./topKWithFractionalIndex.js\"\nimport { map } from \"./map.js\"\nimport { innerJoin } from \"./join.js\"\nimport { consolidate } from \"./consolidate.js\"\nimport type { IStreamBuilder, KeyValue } from \"../types.js\"\n\nexport interface OrderByOptions<Ve> {\n comparator?: (a: Ve, b: Ve) => number\n limit?: number\n offset?: number\n}\n\ntype OrderByWithFractionalIndexOptions<Ve> = OrderByOptions<Ve> & {\n setSizeCallback?: (getSize: () => number) => void\n}\n\n/**\n * Orders the elements and limits the number of results, with optional offset\n * This requires a keyed stream, and uses the `topK` operator to order all the elements.\n *\n * @param valueExtractor - A function that extracts the value to order by from the element\n * @param options - An optional object containing comparator, limit and offset properties\n * @returns A piped operator that orders the elements and limits the number of results\n */\nexport function orderBy<T extends KeyValue<unknown, unknown>, Ve = unknown>(\n valueExtractor: (\n value: T extends KeyValue<unknown, infer V> ? V : never\n ) => Ve,\n options?: OrderByOptions<Ve>\n) {\n const limit = options?.limit ?? Infinity\n const offset = options?.offset ?? 0\n const comparator =\n options?.comparator ??\n ((a, b) => {\n // Default to JS like ordering\n if (a === b) return 0\n if (a < b) return -1\n return 1\n })\n\n return (stream: IStreamBuilder<T>): IStreamBuilder<T> => {\n type KeyType = T extends KeyValue<infer K, unknown> ? K : never\n\n return stream.pipe(\n map(\n ([key, value]) =>\n [\n null,\n [\n key,\n valueExtractor(\n value as T extends KeyValue<unknown, infer V> ? V : never\n ),\n ],\n ] as KeyValue<null, [KeyType, Ve]>\n ),\n topK((a, b) => comparator(a[1], b[1]), { limit, offset }),\n map(([_, [key]]) => [key, null] as KeyValue<KeyType, null>),\n innerJoin(stream),\n map(([key, value]) => {\n return [key, value[1]] as T\n }),\n consolidate()\n )\n }\n}\n\n/**\n * Orders the elements and limits the number of results, with optional offset and\n * annotates the value with the index.\n * This requires a keyed stream, and uses the `topKWithIndex` operator to order all the elements.\n *\n * @param valueExtractor - A function that extracts the value to order by from the element\n * @param options - An optional object containing comparator, limit and offset properties\n * @returns A piped operator that orders the elements and limits the number of results\n */\nexport function orderByWithIndex<\n T extends KeyValue<unknown, unknown>,\n Ve = unknown,\n>(\n valueExtractor: (\n value: T extends KeyValue<unknown, infer V> ? V : never\n ) => Ve,\n options?: OrderByOptions<Ve>\n) {\n const limit = options?.limit ?? Infinity\n const offset = options?.offset ?? 0\n const comparator =\n options?.comparator ??\n ((a, b) => {\n // Default to JS like ordering\n if (a === b) return 0\n if (a < b) return -1\n return 1\n })\n\n return (\n stream: IStreamBuilder<T>\n ): IStreamBuilder<\n KeyValue<\n T extends KeyValue<infer K, unknown> ? K : never,\n [T extends KeyValue<unknown, infer V> ? V : never, number]\n >\n > => {\n type KeyType = T extends KeyValue<infer K, unknown> ? K : never\n type ValueType = T extends KeyValue<unknown, infer V> ? V : never\n\n return stream.pipe(\n map(\n ([key, value]) =>\n [\n null,\n [\n key,\n valueExtractor(\n value as T extends KeyValue<unknown, infer V> ? V : never\n ),\n ],\n ] as KeyValue<null, [KeyType, Ve]>\n ),\n topKWithIndex((a, b) => comparator(a[1], b[1]), { limit, offset }),\n map(([_, [[key], index]]) => [key, index] as KeyValue<KeyType, number>),\n innerJoin(stream),\n map(([key, [index, value]]) => {\n return [key, [value, index]] as KeyValue<KeyType, [ValueType, number]>\n }),\n consolidate()\n )\n }\n}\n\nexport function orderByWithFractionalIndexBase<\n T extends KeyValue<unknown, unknown>,\n Ve = unknown,\n>(\n topKFunction: typeof topKWithFractionalIndex,\n valueExtractor: (\n value: T extends KeyValue<unknown, infer V> ? V : never\n ) => Ve,\n options?: OrderByWithFractionalIndexOptions<Ve>\n) {\n type KeyType = T extends KeyValue<infer K, unknown> ? K : never\n type ValueType = T extends KeyValue<unknown, infer V> ? V : never\n\n const limit = options?.limit ?? Infinity\n const offset = options?.offset ?? 0\n const setSizeCallback = options?.setSizeCallback\n const comparator =\n options?.comparator ??\n ((a, b) => {\n // Default to JS like ordering\n if (a === b) return 0\n if (a < b) return -1\n return 1\n })\n\n return (\n stream: IStreamBuilder<T>\n ): IStreamBuilder<[KeyType, [ValueType, string]]> => {\n return stream.pipe(\n topKFunction(\n (a: ValueType, b: ValueType) =>\n comparator(valueExtractor(a), valueExtractor(b)),\n {\n limit,\n offset,\n setSizeCallback,\n }\n ),\n consolidate()\n )\n }\n}\n\n/**\n * Orders the elements and limits the number of results, with optional offset and\n * annotates the value with a fractional index.\n * This requires a keyed stream, and uses the `topKWithFractionalIndex` operator to order all the elements.\n *\n * @param valueExtractor - A function that extracts the value to order by from the element\n * @param options - An optional object containing comparator, limit and offset properties\n * @returns A piped operator that orders the elements and limits the number of results\n */\nexport function orderByWithFractionalIndex<\n T extends KeyValue<unknown, unknown>,\n Ve = unknown,\n>(\n valueExtractor: (\n value: T extends KeyValue<unknown, infer V> ? V : never\n ) => Ve,\n options?: OrderByWithFractionalIndexOptions<Ve>\n) {\n return orderByWithFractionalIndexBase(\n topKWithFractionalIndex,\n valueExtractor,\n options\n )\n}\n"],"names":["map","topK","innerJoin","consolidate","topKWithIndex","topKWithFractionalIndex"],"mappings":";;;;;;;AAyBO,SAAS,QACd,gBAGA,SACA;AACA,QAAM,QAAQ,SAAS,SAAS;AAChC,QAAM,SAAS,SAAS,UAAU;AAClC,QAAM,aACJ,SAAS,eACR,CAAC,GAAG,MAAM;AAET,QAAI,MAAM,EAAG,QAAO;AACpB,QAAI,IAAI,EAAG,QAAO;AAClB,WAAO;AAAA,EACT;AAEF,SAAO,CAAC,WAAiD;AAGvD,WAAO,OAAO;AAAA,MACZA,IAAAA;AAAAA,QACE,CAAC,CAAC,KAAK,KAAK,MACV;AAAA,UACE;AAAA,UACA;AAAA,YACE;AAAA,YACA;AAAA,cACE;AAAA,YAAA;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAAA,MAEJC,KAAAA,KAAK,CAAC,GAAG,MAAM,WAAW,EAAE,CAAC,GAAG,EAAE,CAAC,CAAC,GAAG,EAAE,OAAO,QAAQ;AAAA,MACxDD,QAAI,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,IAAI,CAA4B;AAAA,MAC1DE,KAAAA,UAAU,MAAM;AAAA,MAChBF,IAAAA,IAAI,CAAC,CAAC,KAAK,KAAK,MAAM;AACpB,eAAO,CAAC,KAAK,MAAM,CAAC,CAAC;AAAA,MACvB,CAAC;AAAA,MACDG,YAAAA,YAAA;AAAA,IAAY;AAAA,EAEhB;AACF;AAWO,SAAS,iBAId,gBAGA,SACA;AACA,QAAM,QAAQ,SAAS,SAAS;AAChC,QAAM,SAAS,SAAS,UAAU;AAClC,QAAM,aACJ,SAAS,eACR,CAAC,GAAG,MAAM;AAET,QAAI,MAAM,EAAG,QAAO;AACpB,QAAI,IAAI,EAAG,QAAO;AAClB,WAAO;AAAA,EACT;AAEF,SAAO,CACL,WAMG;AAIH,WAAO,OAAO;AAAA,MACZH,IAAAA;AAAAA,QACE,CAAC,CAAC,KAAK,KAAK,MACV;AAAA,UACE;AAAA,UACA;AAAA,YACE;AAAA,YACA;AAAA,cACE;AAAA,YAAA;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAAA,MAEJI,KAAAA,cAAc,CAAC,GAAG,MAAM,WAAW,EAAE,CAAC,GAAG,EAAE,CAAC,CAAC,GAAG,EAAE,OAAO,QAAQ;AAAA,MACjEJ,IAAAA,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,GAAG,KAAK,CAAC,MAAM,CAAC,KAAK,KAAK,CAA8B;AAAA,MACtEE,KAAAA,UAAU,MAAM;AAAA,MAChBF,IAAAA,IAAI,CAAC,CAAC,KAAK,CAAC,OAAO,KAAK,CAAC,MAAM;AAC7B,eAAO,CAAC,KAAK,CAAC,OAAO,KAAK,CAAC;AAAA,MAC7B,CAAC;AAAA,MACDG,YAAAA,YAAA;AAAA,IAAY;AAAA,EAEhB;AACF;AAEO,SAAS,+BAId,cACA,gBAGA,SACA;AAIA,QAAM,QAAQ,SAAS,SAAS;AAChC,QAAM,SAAS,SAAS,UAAU;AAClC,QAAM,kBAAkB,SAAS;AACjC,QAAM,aACJ,SAAS,eACR,CAAC,GAAG,MAAM;AAET,QAAI,MAAM,EAAG,QAAO;AACpB,QAAI,IAAI,EAAG,QAAO;AAClB,WAAO;AAAA,EACT;AAEF,SAAO,CACL,WACmD;AACnD,WAAO,OAAO;AAAA,MACZ;AAAA,QACE,CAAC,GAAc,MACb,WAAW,eAAe,CAAC,GAAG,eAAe,CAAC,CAAC;AAAA,QACjD;AAAA,UACE;AAAA,UACA;AAAA,UACA;AAAA,QAAA;AAAA,MACF;AAAA,MAEFA,YAAAA,YAAA;AAAA,IAAY;AAAA,EAEhB;AACF;AAWO,SAAS,2BAId,gBAGA,SACA;AACA,SAAO;AAAA,IACLE,wBAAAA;AAAAA,IACA;AAAA,IACA;AAAA,EAAA;AAEJ;;;;;"}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { topKWithFractionalIndex } from './topKWithFractionalIndex.js';
|
|
2
|
-
import { KeyValue } from '../types.js';
|
|
3
|
-
import { IStreamBuilder } from '../types.cjs';
|
|
2
|
+
import { IStreamBuilder, KeyValue } from '../types.js';
|
|
4
3
|
export interface OrderByOptions<Ve> {
|
|
5
4
|
comparator?: (a: Ve, b: Ve) => number;
|
|
6
5
|
limit?: number;
|
|
@@ -1,29 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __typeError = (msg) => {
|
|
3
|
-
throw TypeError(msg);
|
|
4
|
-
};
|
|
5
|
-
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
6
|
-
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
7
|
-
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
8
|
-
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
|
|
9
|
-
var _fn;
|
|
10
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
11
3
|
const graph = require("../graph.cjs");
|
|
12
4
|
const d2 = require("../d2.cjs");
|
|
13
5
|
class OutputOperator extends graph.UnaryOperator {
|
|
6
|
+
#fn;
|
|
14
7
|
constructor(id, inputA, outputWriter, fn) {
|
|
15
8
|
super(id, inputA, outputWriter);
|
|
16
|
-
|
|
17
|
-
__privateSet(this, _fn, fn);
|
|
9
|
+
this.#fn = fn;
|
|
18
10
|
}
|
|
19
11
|
run() {
|
|
20
12
|
for (const message of this.inputMessages()) {
|
|
21
|
-
|
|
13
|
+
this.#fn(message);
|
|
22
14
|
this.output.sendData(message);
|
|
23
15
|
}
|
|
24
16
|
}
|
|
25
17
|
}
|
|
26
|
-
_fn = new WeakMap();
|
|
27
18
|
function output(fn) {
|
|
28
19
|
return (stream) => {
|
|
29
20
|
const outputStream = new d2.StreamBuilder(
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"output.cjs","sources":["../../../src/operators/output.ts"],"sourcesContent":["import { DifferenceStreamWriter, UnaryOperator } from \"../graph.js\"\nimport { StreamBuilder } from \"../d2.js\"\nimport type { IStreamBuilder, PipedOperator } from \"../types.js\"\nimport type { DifferenceStreamReader } from \"../graph.js\"\nimport type { MultiSet } from \"../multiset.js\"\n\n/**\n * Operator that outputs the messages in the stream\n */\nexport class OutputOperator<T> extends UnaryOperator<T> {\n #fn: (data: MultiSet<T>) => void\n\n constructor(\n id: number,\n inputA: DifferenceStreamReader<T>,\n outputWriter: DifferenceStreamWriter<T>,\n fn: (data: MultiSet<T>) => void\n ) {\n super(id, inputA, outputWriter)\n this.#fn = fn\n }\n\n run(): void {\n for (const message of this.inputMessages()) {\n this.#fn(message)\n this.output.sendData(message)\n }\n }\n}\n\n/**\n * Outputs the messages in the stream\n * @param fn - The function to call with each message\n */\nexport function output<T>(\n fn: (data: MultiSet<T>) => void\n): PipedOperator<T, T> {\n return (stream: IStreamBuilder<T>): IStreamBuilder<T> => {\n const outputStream = new StreamBuilder<T>(\n stream.graph,\n new DifferenceStreamWriter<T>()\n )\n const operator = new OutputOperator<T>(\n stream.graph.getNextOperatorId(),\n stream.connectReader(),\n outputStream.writer,\n fn\n )\n stream.graph.addOperator(operator)\n return outputStream\n }\n}\n"],"names":["UnaryOperator","StreamBuilder","DifferenceStreamWriter"],"mappings":"
|
|
1
|
+
{"version":3,"file":"output.cjs","sources":["../../../src/operators/output.ts"],"sourcesContent":["import { DifferenceStreamWriter, UnaryOperator } from \"../graph.js\"\nimport { StreamBuilder } from \"../d2.js\"\nimport type { IStreamBuilder, PipedOperator } from \"../types.js\"\nimport type { DifferenceStreamReader } from \"../graph.js\"\nimport type { MultiSet } from \"../multiset.js\"\n\n/**\n * Operator that outputs the messages in the stream\n */\nexport class OutputOperator<T> extends UnaryOperator<T> {\n #fn: (data: MultiSet<T>) => void\n\n constructor(\n id: number,\n inputA: DifferenceStreamReader<T>,\n outputWriter: DifferenceStreamWriter<T>,\n fn: (data: MultiSet<T>) => void\n ) {\n super(id, inputA, outputWriter)\n this.#fn = fn\n }\n\n run(): void {\n for (const message of this.inputMessages()) {\n this.#fn(message)\n this.output.sendData(message)\n }\n }\n}\n\n/**\n * Outputs the messages in the stream\n * @param fn - The function to call with each message\n */\nexport function output<T>(\n fn: (data: MultiSet<T>) => void\n): PipedOperator<T, T> {\n return (stream: IStreamBuilder<T>): IStreamBuilder<T> => {\n const outputStream = new StreamBuilder<T>(\n stream.graph,\n new DifferenceStreamWriter<T>()\n )\n const operator = new OutputOperator<T>(\n stream.graph.getNextOperatorId(),\n stream.connectReader(),\n outputStream.writer,\n fn\n )\n stream.graph.addOperator(operator)\n return outputStream\n }\n}\n"],"names":["UnaryOperator","StreamBuilder","DifferenceStreamWriter"],"mappings":";;;;AASO,MAAM,uBAA0BA,MAAAA,cAAiB;AAAA,EACtD;AAAA,EAEA,YACE,IACA,QACA,cACA,IACA;AACA,UAAM,IAAI,QAAQ,YAAY;AAC9B,SAAK,MAAM;AAAA,EACb;AAAA,EAEA,MAAY;AACV,eAAW,WAAW,KAAK,iBAAiB;AAC1C,WAAK,IAAI,OAAO;AAChB,WAAK,OAAO,SAAS,OAAO;AAAA,IAC9B;AAAA,EACF;AACF;AAMO,SAAS,OACd,IACqB;AACrB,SAAO,CAAC,WAAiD;AACvD,UAAM,eAAe,IAAIC,GAAAA;AAAAA,MACvB,OAAO;AAAA,MACP,IAAIC,MAAAA,uBAAA;AAAA,IAA0B;AAEhC,UAAM,WAAW,IAAI;AAAA,MACnB,OAAO,MAAM,kBAAA;AAAA,MACb,OAAO,cAAA;AAAA,MACP,aAAa;AAAA,MACb;AAAA,IAAA;AAEF,WAAO,MAAM,YAAY,QAAQ;AACjC,WAAO;AAAA,EACT;AACF;;;"}
|
|
@@ -1,39 +1,31 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __typeError = (msg) => {
|
|
3
|
-
throw TypeError(msg);
|
|
4
|
-
};
|
|
5
|
-
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
6
|
-
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
7
|
-
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
8
|
-
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
|
|
9
|
-
var _index, _indexOut, _f;
|
|
10
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
11
3
|
const graph = require("../graph.cjs");
|
|
12
4
|
const d2 = require("../d2.cjs");
|
|
13
5
|
const multiset = require("../multiset.cjs");
|
|
14
6
|
const indexes = require("../indexes.cjs");
|
|
15
7
|
class ReduceOperator extends graph.UnaryOperator {
|
|
8
|
+
#index = new indexes.Index();
|
|
9
|
+
#indexOut = new indexes.Index();
|
|
10
|
+
#f;
|
|
16
11
|
constructor(id, inputA, output, f) {
|
|
17
12
|
super(id, inputA, output);
|
|
18
|
-
|
|
19
|
-
__privateAdd(this, _indexOut, new indexes.Index());
|
|
20
|
-
__privateAdd(this, _f);
|
|
21
|
-
__privateSet(this, _f, f);
|
|
13
|
+
this.#f = f;
|
|
22
14
|
}
|
|
23
15
|
run() {
|
|
24
16
|
const keysTodo = /* @__PURE__ */ new Set();
|
|
25
17
|
for (const message of this.inputMessages()) {
|
|
26
18
|
for (const [item, multiplicity] of message.getInner()) {
|
|
27
19
|
const [key, value] = item;
|
|
28
|
-
|
|
20
|
+
this.#index.addValue(key, [value, multiplicity]);
|
|
29
21
|
keysTodo.add(key);
|
|
30
22
|
}
|
|
31
23
|
}
|
|
32
24
|
const result = [];
|
|
33
25
|
for (const key of keysTodo) {
|
|
34
|
-
const curr =
|
|
35
|
-
const currOut =
|
|
36
|
-
const out =
|
|
26
|
+
const curr = this.#index.get(key);
|
|
27
|
+
const currOut = this.#indexOut.get(key);
|
|
28
|
+
const out = this.#f(curr);
|
|
37
29
|
const newOutputMap = /* @__PURE__ */ new Map();
|
|
38
30
|
const oldOutputMap = /* @__PURE__ */ new Map();
|
|
39
31
|
for (const [value, multiplicity] of out) {
|
|
@@ -47,14 +39,14 @@ class ReduceOperator extends graph.UnaryOperator {
|
|
|
47
39
|
for (const [value, multiplicity] of oldOutputMap) {
|
|
48
40
|
if (!newOutputMap.has(value)) {
|
|
49
41
|
result.push([[key, value], -multiplicity]);
|
|
50
|
-
|
|
42
|
+
this.#indexOut.addValue(key, [value, -multiplicity]);
|
|
51
43
|
}
|
|
52
44
|
}
|
|
53
45
|
for (const [value, multiplicity] of newOutputMap) {
|
|
54
46
|
if (!oldOutputMap.has(value)) {
|
|
55
47
|
if (multiplicity !== 0) {
|
|
56
48
|
result.push([[key, value], multiplicity]);
|
|
57
|
-
|
|
49
|
+
this.#indexOut.addValue(key, [value, multiplicity]);
|
|
58
50
|
}
|
|
59
51
|
}
|
|
60
52
|
}
|
|
@@ -64,7 +56,7 @@ class ReduceOperator extends graph.UnaryOperator {
|
|
|
64
56
|
const delta = newMultiplicity - oldMultiplicity;
|
|
65
57
|
if (delta !== 0) {
|
|
66
58
|
result.push([[key, value], delta]);
|
|
67
|
-
|
|
59
|
+
this.#indexOut.addValue(key, [value, delta]);
|
|
68
60
|
}
|
|
69
61
|
}
|
|
70
62
|
}
|
|
@@ -74,9 +66,6 @@ class ReduceOperator extends graph.UnaryOperator {
|
|
|
74
66
|
}
|
|
75
67
|
}
|
|
76
68
|
}
|
|
77
|
-
_index = new WeakMap();
|
|
78
|
-
_indexOut = new WeakMap();
|
|
79
|
-
_f = new WeakMap();
|
|
80
69
|
function reduce(f) {
|
|
81
70
|
return (stream) => {
|
|
82
71
|
const output = new d2.StreamBuilder(
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"reduce.cjs","sources":["../../../src/operators/reduce.ts"],"sourcesContent":["import { DifferenceStreamWriter, UnaryOperator } from \"../graph.js\"\nimport { StreamBuilder } from \"../d2.js\"\nimport { MultiSet } from \"../multiset.js\"\nimport { Index } from \"../indexes.js\"\nimport type { DifferenceStreamReader } from \"../graph.js\"\nimport type { IStreamBuilder, KeyValue } from \"../types.js\"\n\n/**\n * Base operator for reduction operations (version-free)\n */\nexport class ReduceOperator<K, V1, V2> extends UnaryOperator<[K, V1], [K, V2]> {\n #index = new Index<K, V1>()\n #indexOut = new Index<K, V2>()\n #f: (values: Array<[V1, number]>) => Array<[V2, number]>\n\n constructor(\n id: number,\n inputA: DifferenceStreamReader<[K, V1]>,\n output: DifferenceStreamWriter<[K, V2]>,\n f: (values: Array<[V1, number]>) => Array<[V2, number]>\n ) {\n super(id, inputA, output)\n this.#f = f\n }\n\n run(): void {\n // Collect all input messages and update the index\n const keysTodo = new Set<K>()\n for (const message of this.inputMessages()) {\n for (const [item, multiplicity] of message.getInner()) {\n const [key, value] = item\n this.#index.addValue(key, [value, multiplicity])\n keysTodo.add(key)\n }\n }\n\n // For each key, compute the reduction and delta\n const result: Array<[[K, V2], number]> = []\n for (const key of keysTodo) {\n const curr = this.#index.get(key)\n const currOut = this.#indexOut.get(key)\n const out = this.#f(curr)\n\n // Create maps for current and previous outputs using values directly as keys\n const newOutputMap = new Map<V2, number>()\n const oldOutputMap = new Map<V2, number>()\n\n // Process new output\n for (const [value, multiplicity] of out) {\n const existing = newOutputMap.get(value) ?? 0\n newOutputMap.set(value, existing + multiplicity)\n }\n\n // Process previous output\n for (const [value, multiplicity] of currOut) {\n const existing = oldOutputMap.get(value) ?? 0\n oldOutputMap.set(value, existing + multiplicity)\n }\n\n // First, emit removals for old values that are no longer present\n for (const [value, multiplicity] of oldOutputMap) {\n if (!newOutputMap.has(value)) {\n // Remove the old value entirely\n result.push([[key, value], -multiplicity])\n this.#indexOut.addValue(key, [value, -multiplicity])\n }\n }\n\n // Then, emit additions for new values that are not present in old\n for (const [value, multiplicity] of newOutputMap) {\n if (!oldOutputMap.has(value)) {\n // Add the new value only if it has non-zero multiplicity\n if (multiplicity !== 0) {\n result.push([[key, value], multiplicity])\n this.#indexOut.addValue(key, [value, multiplicity])\n }\n }\n }\n\n // Finally, emit multiplicity changes for values that were present and are still present\n for (const [value, newMultiplicity] of newOutputMap) {\n const oldMultiplicity = oldOutputMap.get(value)\n if (oldMultiplicity !== undefined) {\n const delta = newMultiplicity - oldMultiplicity\n // Only emit actual changes, i.e. non-zero deltas\n if (delta !== 0) {\n result.push([[key, value], delta])\n this.#indexOut.addValue(key, [value, delta])\n }\n }\n }\n }\n\n if (result.length > 0) {\n this.output.sendData(new MultiSet(result))\n }\n }\n}\n\n/**\n * Reduces the elements in the stream by key (version-free)\n */\nexport function reduce<\n KType extends T extends KeyValue<infer K, infer _V> ? K : never,\n V1Type extends T extends KeyValue<KType, infer V> ? V : never,\n R,\n T,\n>(f: (values: Array<[V1Type, number]>) => Array<[R, number]>) {\n return (stream: IStreamBuilder<T>): IStreamBuilder<KeyValue<KType, R>> => {\n const output = new StreamBuilder<KeyValue<KType, R>>(\n stream.graph,\n new DifferenceStreamWriter<KeyValue<KType, R>>()\n )\n const operator = new ReduceOperator<KType, V1Type, R>(\n stream.graph.getNextOperatorId(),\n stream.connectReader() as DifferenceStreamReader<KeyValue<KType, V1Type>>,\n output.writer,\n f\n )\n stream.graph.addOperator(operator)\n return output\n }\n}\n"],"names":["UnaryOperator","Index","MultiSet","StreamBuilder","DifferenceStreamWriter"],"mappings":"
|
|
1
|
+
{"version":3,"file":"reduce.cjs","sources":["../../../src/operators/reduce.ts"],"sourcesContent":["import { DifferenceStreamWriter, UnaryOperator } from \"../graph.js\"\nimport { StreamBuilder } from \"../d2.js\"\nimport { MultiSet } from \"../multiset.js\"\nimport { Index } from \"../indexes.js\"\nimport type { DifferenceStreamReader } from \"../graph.js\"\nimport type { IStreamBuilder, KeyValue } from \"../types.js\"\n\n/**\n * Base operator for reduction operations (version-free)\n */\nexport class ReduceOperator<K, V1, V2> extends UnaryOperator<[K, V1], [K, V2]> {\n #index = new Index<K, V1>()\n #indexOut = new Index<K, V2>()\n #f: (values: Array<[V1, number]>) => Array<[V2, number]>\n\n constructor(\n id: number,\n inputA: DifferenceStreamReader<[K, V1]>,\n output: DifferenceStreamWriter<[K, V2]>,\n f: (values: Array<[V1, number]>) => Array<[V2, number]>\n ) {\n super(id, inputA, output)\n this.#f = f\n }\n\n run(): void {\n // Collect all input messages and update the index\n const keysTodo = new Set<K>()\n for (const message of this.inputMessages()) {\n for (const [item, multiplicity] of message.getInner()) {\n const [key, value] = item\n this.#index.addValue(key, [value, multiplicity])\n keysTodo.add(key)\n }\n }\n\n // For each key, compute the reduction and delta\n const result: Array<[[K, V2], number]> = []\n for (const key of keysTodo) {\n const curr = this.#index.get(key)\n const currOut = this.#indexOut.get(key)\n const out = this.#f(curr)\n\n // Create maps for current and previous outputs using values directly as keys\n const newOutputMap = new Map<V2, number>()\n const oldOutputMap = new Map<V2, number>()\n\n // Process new output\n for (const [value, multiplicity] of out) {\n const existing = newOutputMap.get(value) ?? 0\n newOutputMap.set(value, existing + multiplicity)\n }\n\n // Process previous output\n for (const [value, multiplicity] of currOut) {\n const existing = oldOutputMap.get(value) ?? 0\n oldOutputMap.set(value, existing + multiplicity)\n }\n\n // First, emit removals for old values that are no longer present\n for (const [value, multiplicity] of oldOutputMap) {\n if (!newOutputMap.has(value)) {\n // Remove the old value entirely\n result.push([[key, value], -multiplicity])\n this.#indexOut.addValue(key, [value, -multiplicity])\n }\n }\n\n // Then, emit additions for new values that are not present in old\n for (const [value, multiplicity] of newOutputMap) {\n if (!oldOutputMap.has(value)) {\n // Add the new value only if it has non-zero multiplicity\n if (multiplicity !== 0) {\n result.push([[key, value], multiplicity])\n this.#indexOut.addValue(key, [value, multiplicity])\n }\n }\n }\n\n // Finally, emit multiplicity changes for values that were present and are still present\n for (const [value, newMultiplicity] of newOutputMap) {\n const oldMultiplicity = oldOutputMap.get(value)\n if (oldMultiplicity !== undefined) {\n const delta = newMultiplicity - oldMultiplicity\n // Only emit actual changes, i.e. non-zero deltas\n if (delta !== 0) {\n result.push([[key, value], delta])\n this.#indexOut.addValue(key, [value, delta])\n }\n }\n }\n }\n\n if (result.length > 0) {\n this.output.sendData(new MultiSet(result))\n }\n }\n}\n\n/**\n * Reduces the elements in the stream by key (version-free)\n */\nexport function reduce<\n KType extends T extends KeyValue<infer K, infer _V> ? K : never,\n V1Type extends T extends KeyValue<KType, infer V> ? V : never,\n R,\n T,\n>(f: (values: Array<[V1Type, number]>) => Array<[R, number]>) {\n return (stream: IStreamBuilder<T>): IStreamBuilder<KeyValue<KType, R>> => {\n const output = new StreamBuilder<KeyValue<KType, R>>(\n stream.graph,\n new DifferenceStreamWriter<KeyValue<KType, R>>()\n )\n const operator = new ReduceOperator<KType, V1Type, R>(\n stream.graph.getNextOperatorId(),\n stream.connectReader() as DifferenceStreamReader<KeyValue<KType, V1Type>>,\n output.writer,\n f\n )\n stream.graph.addOperator(operator)\n return output\n }\n}\n"],"names":["UnaryOperator","Index","MultiSet","StreamBuilder","DifferenceStreamWriter"],"mappings":";;;;;;AAUO,MAAM,uBAAkCA,MAAAA,cAAgC;AAAA,EAC7E,SAAS,IAAIC,QAAAA,MAAA;AAAA,EACb,YAAY,IAAIA,QAAAA,MAAA;AAAA,EAChB;AAAA,EAEA,YACE,IACA,QACA,QACA,GACA;AACA,UAAM,IAAI,QAAQ,MAAM;AACxB,SAAK,KAAK;AAAA,EACZ;AAAA,EAEA,MAAY;AAEV,UAAM,+BAAe,IAAA;AACrB,eAAW,WAAW,KAAK,iBAAiB;AAC1C,iBAAW,CAAC,MAAM,YAAY,KAAK,QAAQ,YAAY;AACrD,cAAM,CAAC,KAAK,KAAK,IAAI;AACrB,aAAK,OAAO,SAAS,KAAK,CAAC,OAAO,YAAY,CAAC;AAC/C,iBAAS,IAAI,GAAG;AAAA,MAClB;AAAA,IACF;AAGA,UAAM,SAAmC,CAAA;AACzC,eAAW,OAAO,UAAU;AAC1B,YAAM,OAAO,KAAK,OAAO,IAAI,GAAG;AAChC,YAAM,UAAU,KAAK,UAAU,IAAI,GAAG;AACtC,YAAM,MAAM,KAAK,GAAG,IAAI;AAGxB,YAAM,mCAAmB,IAAA;AACzB,YAAM,mCAAmB,IAAA;AAGzB,iBAAW,CAAC,OAAO,YAAY,KAAK,KAAK;AACvC,cAAM,WAAW,aAAa,IAAI,KAAK,KAAK;AAC5C,qBAAa,IAAI,OAAO,WAAW,YAAY;AAAA,MACjD;AAGA,iBAAW,CAAC,OAAO,YAAY,KAAK,SAAS;AAC3C,cAAM,WAAW,aAAa,IAAI,KAAK,KAAK;AAC5C,qBAAa,IAAI,OAAO,WAAW,YAAY;AAAA,MACjD;AAGA,iBAAW,CAAC,OAAO,YAAY,KAAK,cAAc;AAChD,YAAI,CAAC,aAAa,IAAI,KAAK,GAAG;AAE5B,iBAAO,KAAK,CAAC,CAAC,KAAK,KAAK,GAAG,CAAC,YAAY,CAAC;AACzC,eAAK,UAAU,SAAS,KAAK,CAAC,OAAO,CAAC,YAAY,CAAC;AAAA,QACrD;AAAA,MACF;AAGA,iBAAW,CAAC,OAAO,YAAY,KAAK,cAAc;AAChD,YAAI,CAAC,aAAa,IAAI,KAAK,GAAG;AAE5B,cAAI,iBAAiB,GAAG;AACtB,mBAAO,KAAK,CAAC,CAAC,KAAK,KAAK,GAAG,YAAY,CAAC;AACxC,iBAAK,UAAU,SAAS,KAAK,CAAC,OAAO,YAAY,CAAC;AAAA,UACpD;AAAA,QACF;AAAA,MACF;AAGA,iBAAW,CAAC,OAAO,eAAe,KAAK,cAAc;AACnD,cAAM,kBAAkB,aAAa,IAAI,KAAK;AAC9C,YAAI,oBAAoB,QAAW;AACjC,gBAAM,QAAQ,kBAAkB;AAEhC,cAAI,UAAU,GAAG;AACf,mBAAO,KAAK,CAAC,CAAC,KAAK,KAAK,GAAG,KAAK,CAAC;AACjC,iBAAK,UAAU,SAAS,KAAK,CAAC,OAAO,KAAK,CAAC;AAAA,UAC7C;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAEA,QAAI,OAAO,SAAS,GAAG;AACrB,WAAK,OAAO,SAAS,IAAIC,SAAAA,SAAS,MAAM,CAAC;AAAA,IAC3C;AAAA,EACF;AACF;AAKO,SAAS,OAKd,GAA4D;AAC5D,SAAO,CAAC,WAAkE;AACxE,UAAM,SAAS,IAAIC,GAAAA;AAAAA,MACjB,OAAO;AAAA,MACP,IAAIC,MAAAA,uBAAA;AAAA,IAA2C;AAEjD,UAAM,WAAW,IAAI;AAAA,MACnB,OAAO,MAAM,kBAAA;AAAA,MACb,OAAO,cAAA;AAAA,MACP,OAAO;AAAA,MACP;AAAA,IAAA;AAEF,WAAO,MAAM,YAAY,QAAQ;AACjC,WAAO;AAAA,EACT;AACF;;;"}
|
|
@@ -1,27 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __typeError = (msg) => {
|
|
3
|
-
throw TypeError(msg);
|
|
4
|
-
};
|
|
5
|
-
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
6
|
-
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
7
|
-
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
8
|
-
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
|
|
9
|
-
var _f;
|
|
10
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
11
3
|
const graph = require("../graph.cjs");
|
|
12
4
|
const d2 = require("../d2.cjs");
|
|
13
5
|
class TapOperator extends graph.LinearUnaryOperator {
|
|
6
|
+
#f;
|
|
14
7
|
constructor(id, inputA, output, f) {
|
|
15
8
|
super(id, inputA, output);
|
|
16
|
-
|
|
17
|
-
__privateSet(this, _f, f);
|
|
9
|
+
this.#f = f;
|
|
18
10
|
}
|
|
19
11
|
inner(collection) {
|
|
20
|
-
|
|
12
|
+
this.#f(collection);
|
|
21
13
|
return collection;
|
|
22
14
|
}
|
|
23
15
|
}
|
|
24
|
-
_f = new WeakMap();
|
|
25
16
|
function tap(f) {
|
|
26
17
|
return (stream) => {
|
|
27
18
|
const output = new d2.StreamBuilder(
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tap.cjs","sources":["../../../src/operators/tap.ts"],"sourcesContent":["import { DifferenceStreamWriter, LinearUnaryOperator } from \"../graph.js\"\nimport { StreamBuilder } from \"../d2.js\"\nimport type { IStreamBuilder, PipedOperator } from \"../types.js\"\nimport type { DifferenceStreamReader } from \"../graph.js\"\nimport type { MultiSet } from \"../multiset.js\"\n\n/**\n * Operator that applies a function to each multi-set in the input stream\n */\nexport class TapOperator<T> extends LinearUnaryOperator<T, T> {\n #f: (data: MultiSet<T>) => void\n\n constructor(\n id: number,\n inputA: DifferenceStreamReader<T>,\n output: DifferenceStreamWriter<T>,\n f: (data: MultiSet<T>) => void\n ) {\n super(id, inputA, output)\n this.#f = f\n }\n\n inner(collection: MultiSet<T>): MultiSet<T> {\n this.#f(collection)\n return collection\n }\n}\n\n/**\n * Invokes a function for each multi-set in the input stream.\n * This operator doesn't modify the stream and is used to perform side effects.\n * @param f - The function to invoke on each multi-set\n * @returns The input stream\n */\nexport function tap<T>(f: (data: MultiSet<T>) => void): PipedOperator<T, T> {\n return (stream: IStreamBuilder<T>): IStreamBuilder<T> => {\n const output = new StreamBuilder<T>(\n stream.graph,\n new DifferenceStreamWriter<T>()\n )\n const operator = new TapOperator<T>(\n stream.graph.getNextOperatorId(),\n stream.connectReader(),\n output.writer,\n f\n )\n stream.graph.addOperator(operator)\n return output\n }\n}\n"],"names":["LinearUnaryOperator","StreamBuilder","DifferenceStreamWriter"],"mappings":"
|
|
1
|
+
{"version":3,"file":"tap.cjs","sources":["../../../src/operators/tap.ts"],"sourcesContent":["import { DifferenceStreamWriter, LinearUnaryOperator } from \"../graph.js\"\nimport { StreamBuilder } from \"../d2.js\"\nimport type { IStreamBuilder, PipedOperator } from \"../types.js\"\nimport type { DifferenceStreamReader } from \"../graph.js\"\nimport type { MultiSet } from \"../multiset.js\"\n\n/**\n * Operator that applies a function to each multi-set in the input stream\n */\nexport class TapOperator<T> extends LinearUnaryOperator<T, T> {\n #f: (data: MultiSet<T>) => void\n\n constructor(\n id: number,\n inputA: DifferenceStreamReader<T>,\n output: DifferenceStreamWriter<T>,\n f: (data: MultiSet<T>) => void\n ) {\n super(id, inputA, output)\n this.#f = f\n }\n\n inner(collection: MultiSet<T>): MultiSet<T> {\n this.#f(collection)\n return collection\n }\n}\n\n/**\n * Invokes a function for each multi-set in the input stream.\n * This operator doesn't modify the stream and is used to perform side effects.\n * @param f - The function to invoke on each multi-set\n * @returns The input stream\n */\nexport function tap<T>(f: (data: MultiSet<T>) => void): PipedOperator<T, T> {\n return (stream: IStreamBuilder<T>): IStreamBuilder<T> => {\n const output = new StreamBuilder<T>(\n stream.graph,\n new DifferenceStreamWriter<T>()\n )\n const operator = new TapOperator<T>(\n stream.graph.getNextOperatorId(),\n stream.connectReader(),\n output.writer,\n f\n )\n stream.graph.addOperator(operator)\n return output\n }\n}\n"],"names":["LinearUnaryOperator","StreamBuilder","DifferenceStreamWriter"],"mappings":";;;;AASO,MAAM,oBAAuBA,MAAAA,oBAA0B;AAAA,EAC5D;AAAA,EAEA,YACE,IACA,QACA,QACA,GACA;AACA,UAAM,IAAI,QAAQ,MAAM;AACxB,SAAK,KAAK;AAAA,EACZ;AAAA,EAEA,MAAM,YAAsC;AAC1C,SAAK,GAAG,UAAU;AAClB,WAAO;AAAA,EACT;AACF;AAQO,SAAS,IAAO,GAAqD;AAC1E,SAAO,CAAC,WAAiD;AACvD,UAAM,SAAS,IAAIC,GAAAA;AAAAA,MACjB,OAAO;AAAA,MACP,IAAIC,MAAAA,uBAAA;AAAA,IAA0B;AAEhC,UAAM,WAAW,IAAI;AAAA,MACnB,OAAO,MAAM,kBAAA;AAAA,MACb,OAAO,cAAA;AAAA,MACP,OAAO;AAAA,MACP;AAAA,IAAA;AAEF,WAAO,MAAM,YAAY,QAAQ;AACjC,WAAO;AAAA,EACT;AACF;;;"}
|
|
@@ -3,8 +3,8 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
3
3
|
const multiset = require("../multiset.cjs");
|
|
4
4
|
const reduce = require("./reduce.cjs");
|
|
5
5
|
function topK(comparator, options) {
|
|
6
|
-
const limit =
|
|
7
|
-
const offset =
|
|
6
|
+
const limit = options?.limit ?? Infinity;
|
|
7
|
+
const offset = options?.offset ?? 0;
|
|
8
8
|
return (stream) => {
|
|
9
9
|
const reduced = stream.pipe(
|
|
10
10
|
reduce.reduce((values) => {
|
|
@@ -17,8 +17,8 @@ function topK(comparator, options) {
|
|
|
17
17
|
};
|
|
18
18
|
}
|
|
19
19
|
function topKWithIndex(comparator, options) {
|
|
20
|
-
const limit =
|
|
21
|
-
const offset =
|
|
20
|
+
const limit = options?.limit ?? Infinity;
|
|
21
|
+
const offset = options?.offset ?? 0;
|
|
22
22
|
return (stream) => {
|
|
23
23
|
const reduced = stream.pipe(
|
|
24
24
|
reduce.reduce((values) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"topK.cjs","sources":["../../../src/operators/topK.ts"],"sourcesContent":["import { MultiSet } from \"../multiset.js\"\nimport { reduce } from \"./reduce.js\"\nimport type { IStreamBuilder, PipedOperator } from \"../types\"\
|
|
1
|
+
{"version":3,"file":"topK.cjs","sources":["../../../src/operators/topK.ts"],"sourcesContent":["import { MultiSet } from \"../multiset.js\"\nimport { reduce } from \"./reduce.js\"\nimport type { IStreamBuilder, KeyValue, PipedOperator } from \"../types\"\n\ninterface TopKOptions {\n limit?: number\n offset?: number\n}\n\n/**\n * Limits the number of results based on a comparator, with optional offset.\n * This works on a keyed stream, where the key is the first element of the tuple\n * The ordering is withing a key group, i.e. elements are sorted within a key group\n * and the limit + offset is applied to that sorted group.\n * To order the entire stream, key by the same value for all elements such as null.\n *\n * @param comparator - A function that compares two elements\n * @param options - An optional object containing limit and offset properties\n * @returns A piped operator that limits the number of results\n */\nexport function topK<\n KType extends T extends KeyValue<infer K, infer _V> ? K : never,\n V1Type extends T extends KeyValue<KType, infer V> ? V : never,\n T,\n>(\n comparator: (a: V1Type, b: V1Type) => number,\n options?: TopKOptions\n): PipedOperator<T, T> {\n const limit = options?.limit ?? Infinity\n const offset = options?.offset ?? 0\n\n return (stream: IStreamBuilder<T>): IStreamBuilder<T> => {\n const reduced = stream.pipe(\n reduce((values) => {\n // `values` is a list of tuples, first element is the value, second is the multiplicity\n const consolidated = new MultiSet(values).consolidate()\n const sortedValues = consolidated\n .getInner()\n .sort((a, b) => comparator(a[0] as V1Type, b[0] as V1Type))\n return sortedValues.slice(offset, offset + limit)\n })\n )\n return reduced as IStreamBuilder<T>\n }\n}\n\n/**\n * Limits the number of results based on a comparator, with optional offset.\n * This works on a keyed stream, where the key is the first element of the tuple\n * The ordering is withing a key group, i.e. elements are sorted within a key group\n * and the limit + offset is applied to that sorted group.\n * To order the entire stream, key by the same value for all elements such as null.\n * Adds the index of the element to the result as [key, [value, index]]\n *\n * @param comparator - A function that compares two elements\n * @param options - An optional object containing limit and offset properties\n * @returns A piped operator that orders the elements and limits the number of results\n */\nexport function topKWithIndex<\n KType extends T extends KeyValue<infer K, infer _V> ? K : never,\n V1Type extends T extends KeyValue<KType, infer V> ? V : never,\n T,\n>(\n comparator: (a: V1Type, b: V1Type) => number,\n options?: TopKOptions\n): PipedOperator<T, KeyValue<KType, [V1Type, number]>> {\n const limit = options?.limit ?? Infinity\n const offset = options?.offset ?? 0\n\n return (\n stream: IStreamBuilder<T>\n ): IStreamBuilder<KeyValue<KType, [V1Type, number]>> => {\n const reduced = stream.pipe(\n reduce<KType, V1Type, [V1Type, number], T>((values) => {\n // `values` is a list of tuples, first element is the value, second is the multiplicity\n const consolidated = new MultiSet(values).consolidate()\n let i = offset\n const sortedValues = consolidated\n .getInner()\n .sort((a, b) => comparator(a[0], b[0]))\n .slice(offset, offset + limit)\n .map(([value, multiplicity]): [[V1Type, number], number] => [\n [value, i++],\n multiplicity,\n ])\n return sortedValues\n })\n )\n return reduced\n }\n}\n"],"names":["reduce","MultiSet"],"mappings":";;;;AAoBO,SAAS,KAKd,YACA,SACqB;AACrB,QAAM,QAAQ,SAAS,SAAS;AAChC,QAAM,SAAS,SAAS,UAAU;AAElC,SAAO,CAAC,WAAiD;AACvD,UAAM,UAAU,OAAO;AAAA,MACrBA,OAAAA,OAAO,CAAC,WAAW;AAEjB,cAAM,eAAe,IAAIC,SAAAA,SAAS,MAAM,EAAE,YAAA;AAC1C,cAAM,eAAe,aAClB,SAAA,EACA,KAAK,CAAC,GAAG,MAAM,WAAW,EAAE,CAAC,GAAa,EAAE,CAAC,CAAW,CAAC;AAC5D,eAAO,aAAa,MAAM,QAAQ,SAAS,KAAK;AAAA,MAClD,CAAC;AAAA,IAAA;AAEH,WAAO;AAAA,EACT;AACF;AAcO,SAAS,cAKd,YACA,SACqD;AACrD,QAAM,QAAQ,SAAS,SAAS;AAChC,QAAM,SAAS,SAAS,UAAU;AAElC,SAAO,CACL,WACsD;AACtD,UAAM,UAAU,OAAO;AAAA,MACrBD,OAAAA,OAA2C,CAAC,WAAW;AAErD,cAAM,eAAe,IAAIC,SAAAA,SAAS,MAAM,EAAE,YAAA;AAC1C,YAAI,IAAI;AACR,cAAM,eAAe,aAClB,SAAA,EACA,KAAK,CAAC,GAAG,MAAM,WAAW,EAAE,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,EACrC,MAAM,QAAQ,SAAS,KAAK,EAC5B,IAAI,CAAC,CAAC,OAAO,YAAY,MAAkC;AAAA,UAC1D,CAAC,OAAO,GAAG;AAAA,UACX;AAAA,QAAA,CACD;AACH,eAAO;AAAA,MACT,CAAC;AAAA,IAAA;AAEH,WAAO;AAAA,EACT;AACF;;;"}
|
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __typeError = (msg) => {
|
|
3
|
-
throw TypeError(msg);
|
|
4
|
-
};
|
|
5
|
-
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
6
|
-
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
7
|
-
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
8
|
-
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
|
|
9
|
-
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
|
|
10
|
-
var _sortedValues, _comparator, _topKStart, _topKEnd, _TopKArray_instances, findIndex_fn, _index, _topK, _limit;
|
|
11
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
12
3
|
const fractionalIndexing = require("fractional-indexing");
|
|
13
4
|
const graph = require("../graph.cjs");
|
|
@@ -15,36 +6,35 @@ const d2 = require("../d2.cjs");
|
|
|
15
6
|
const multiset = require("../multiset.cjs");
|
|
16
7
|
const utils = require("../utils.cjs");
|
|
17
8
|
class TopKArray {
|
|
9
|
+
#sortedValues = [];
|
|
10
|
+
#comparator;
|
|
11
|
+
#topKStart;
|
|
12
|
+
#topKEnd;
|
|
18
13
|
constructor(offset, limit, comparator) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
__privateAdd(this, _topKStart);
|
|
23
|
-
__privateAdd(this, _topKEnd);
|
|
24
|
-
__privateSet(this, _topKStart, offset);
|
|
25
|
-
__privateSet(this, _topKEnd, offset + limit);
|
|
26
|
-
__privateSet(this, _comparator, comparator);
|
|
14
|
+
this.#topKStart = offset;
|
|
15
|
+
this.#topKEnd = offset + limit;
|
|
16
|
+
this.#comparator = comparator;
|
|
27
17
|
}
|
|
28
18
|
get size() {
|
|
29
|
-
const offset =
|
|
30
|
-
const limit =
|
|
31
|
-
const available =
|
|
19
|
+
const offset = this.#topKStart;
|
|
20
|
+
const limit = this.#topKEnd - this.#topKStart;
|
|
21
|
+
const available = this.#sortedValues.length - offset;
|
|
32
22
|
return Math.max(0, Math.min(limit, available));
|
|
33
23
|
}
|
|
34
24
|
insert(value) {
|
|
35
25
|
const result = { moveIn: null, moveOut: null };
|
|
36
|
-
const index =
|
|
37
|
-
const indexBefore = index === 0 ? null : getIndex(
|
|
38
|
-
const indexAfter = index ===
|
|
26
|
+
const index = this.#findIndex(value);
|
|
27
|
+
const indexBefore = index === 0 ? null : getIndex(this.#sortedValues[index - 1]);
|
|
28
|
+
const indexAfter = index === this.#sortedValues.length ? null : getIndex(this.#sortedValues[index]);
|
|
39
29
|
const fractionalIndex = fractionalIndexing.generateKeyBetween(indexBefore, indexAfter);
|
|
40
30
|
const val = indexedValue(value, fractionalIndex);
|
|
41
|
-
|
|
42
|
-
if (index <
|
|
43
|
-
const moveInIndex = Math.max(index,
|
|
44
|
-
if (moveInIndex <
|
|
45
|
-
result.moveIn =
|
|
46
|
-
if (
|
|
47
|
-
result.moveOut =
|
|
31
|
+
this.#sortedValues.splice(index, 0, val);
|
|
32
|
+
if (index < this.#topKEnd) {
|
|
33
|
+
const moveInIndex = Math.max(index, this.#topKStart);
|
|
34
|
+
if (moveInIndex < this.#sortedValues.length) {
|
|
35
|
+
result.moveIn = this.#sortedValues[moveInIndex];
|
|
36
|
+
if (this.#topKEnd < this.#sortedValues.length) {
|
|
37
|
+
result.moveOut = this.#sortedValues[this.#topKEnd];
|
|
48
38
|
}
|
|
49
39
|
}
|
|
50
40
|
}
|
|
@@ -58,54 +48,48 @@ class TopKArray {
|
|
|
58
48
|
*/
|
|
59
49
|
delete(value) {
|
|
60
50
|
const result = { moveIn: null, moveOut: null };
|
|
61
|
-
const index =
|
|
62
|
-
const [removedElem] =
|
|
63
|
-
if (index <
|
|
51
|
+
const index = this.#findIndex(value);
|
|
52
|
+
const [removedElem] = this.#sortedValues.splice(index, 1);
|
|
53
|
+
if (index < this.#topKEnd) {
|
|
64
54
|
result.moveOut = removedElem;
|
|
65
|
-
if (index <
|
|
66
|
-
const moveOutIndex =
|
|
67
|
-
if (moveOutIndex <
|
|
68
|
-
result.moveOut =
|
|
55
|
+
if (index < this.#topKStart) {
|
|
56
|
+
const moveOutIndex = this.#topKStart - 1;
|
|
57
|
+
if (moveOutIndex < this.#sortedValues.length) {
|
|
58
|
+
result.moveOut = this.#sortedValues[moveOutIndex];
|
|
69
59
|
} else {
|
|
70
60
|
result.moveOut = null;
|
|
71
61
|
}
|
|
72
62
|
}
|
|
73
|
-
const moveInIndex =
|
|
74
|
-
if (moveInIndex <
|
|
75
|
-
result.moveIn =
|
|
63
|
+
const moveInIndex = this.#topKEnd - 1;
|
|
64
|
+
if (moveInIndex < this.#sortedValues.length) {
|
|
65
|
+
result.moveIn = this.#sortedValues[moveInIndex];
|
|
76
66
|
}
|
|
77
67
|
}
|
|
78
68
|
return result;
|
|
79
69
|
}
|
|
70
|
+
// TODO: see if there is a way to refactor the code for insert and delete in the topK above
|
|
71
|
+
// because they are very similar, one is shifting the topK window to the left and the other is shifting it to the right
|
|
72
|
+
// so i have the feeling there is a common pattern here and we can implement both cases using that pattern
|
|
73
|
+
#findIndex(value) {
|
|
74
|
+
return utils.binarySearch(
|
|
75
|
+
this.#sortedValues,
|
|
76
|
+
indexedValue(value, ``),
|
|
77
|
+
(a, b) => this.#comparator(getValue(a), getValue(b))
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
80
|
}
|
|
81
|
-
_sortedValues = new WeakMap();
|
|
82
|
-
_comparator = new WeakMap();
|
|
83
|
-
_topKStart = new WeakMap();
|
|
84
|
-
_topKEnd = new WeakMap();
|
|
85
|
-
_TopKArray_instances = new WeakSet();
|
|
86
|
-
// TODO: see if there is a way to refactor the code for insert and delete in the topK above
|
|
87
|
-
// because they are very similar, one is shifting the topK window to the left and the other is shifting it to the right
|
|
88
|
-
// so i have the feeling there is a common pattern here and we can implement both cases using that pattern
|
|
89
|
-
findIndex_fn = function(value) {
|
|
90
|
-
return utils.binarySearch(
|
|
91
|
-
__privateGet(this, _sortedValues),
|
|
92
|
-
indexedValue(value, ``),
|
|
93
|
-
(a, b) => __privateGet(this, _comparator).call(this, getValue(a), getValue(b))
|
|
94
|
-
);
|
|
95
|
-
};
|
|
96
81
|
class TopKWithFractionalIndexOperator extends graph.UnaryOperator {
|
|
82
|
+
#index = /* @__PURE__ */ new Map();
|
|
83
|
+
// maps keys to their multiplicity
|
|
84
|
+
/**
|
|
85
|
+
* topK data structure that supports insertions and deletions
|
|
86
|
+
* and returns changes to the topK.
|
|
87
|
+
*/
|
|
88
|
+
#topK;
|
|
89
|
+
#limit;
|
|
97
90
|
constructor(id, inputA, output, comparator, options) {
|
|
98
|
-
var _a;
|
|
99
91
|
super(id, inputA, output);
|
|
100
|
-
|
|
101
|
-
// maps keys to their multiplicity
|
|
102
|
-
/**
|
|
103
|
-
* topK data structure that supports insertions and deletions
|
|
104
|
-
* and returns changes to the topK.
|
|
105
|
-
*/
|
|
106
|
-
__privateAdd(this, _topK);
|
|
107
|
-
__privateAdd(this, _limit);
|
|
108
|
-
__privateSet(this, _limit, options.limit ?? Infinity);
|
|
92
|
+
this.#limit = options.limit ?? Infinity;
|
|
109
93
|
const offset = options.offset ?? 0;
|
|
110
94
|
const compareTaggedValues = (a, b) => {
|
|
111
95
|
const valueComparison = comparator(getVal(a), getVal(b));
|
|
@@ -116,8 +100,8 @@ class TopKWithFractionalIndexOperator extends graph.UnaryOperator {
|
|
|
116
100
|
const tieBreakerB = getTag(b);
|
|
117
101
|
return tieBreakerA - tieBreakerB;
|
|
118
102
|
};
|
|
119
|
-
|
|
120
|
-
|
|
103
|
+
this.#topK = this.createTopK(offset, this.#limit, compareTaggedValues);
|
|
104
|
+
options.setSizeCallback?.(() => this.#topK.size);
|
|
121
105
|
}
|
|
122
106
|
createTopK(offset, limit, comparator) {
|
|
123
107
|
return new TopKArray(offset, limit, comparator);
|
|
@@ -142,10 +126,10 @@ class TopKWithFractionalIndexOperator extends graph.UnaryOperator {
|
|
|
142
126
|
};
|
|
143
127
|
if (oldMultiplicity <= 0 && newMultiplicity > 0) {
|
|
144
128
|
const taggedValue = tagValue(key, value);
|
|
145
|
-
res =
|
|
129
|
+
res = this.#topK.insert(taggedValue);
|
|
146
130
|
} else if (oldMultiplicity > 0 && newMultiplicity <= 0) {
|
|
147
131
|
const taggedValue = tagValue(key, value);
|
|
148
|
-
res =
|
|
132
|
+
res = this.#topK.delete(taggedValue);
|
|
149
133
|
} else ;
|
|
150
134
|
if (res.moveIn) {
|
|
151
135
|
const index = getIndex(res.moveIn);
|
|
@@ -164,22 +148,19 @@ class TopKWithFractionalIndexOperator extends graph.UnaryOperator {
|
|
|
164
148
|
return;
|
|
165
149
|
}
|
|
166
150
|
getMultiplicity(key) {
|
|
167
|
-
return
|
|
151
|
+
return this.#index.get(key) ?? 0;
|
|
168
152
|
}
|
|
169
153
|
addKey(key, multiplicity) {
|
|
170
154
|
const oldMultiplicity = this.getMultiplicity(key);
|
|
171
155
|
const newMultiplicity = oldMultiplicity + multiplicity;
|
|
172
156
|
if (newMultiplicity === 0) {
|
|
173
|
-
|
|
157
|
+
this.#index.delete(key);
|
|
174
158
|
} else {
|
|
175
|
-
|
|
159
|
+
this.#index.set(key, newMultiplicity);
|
|
176
160
|
}
|
|
177
161
|
return { oldMultiplicity, newMultiplicity };
|
|
178
162
|
}
|
|
179
163
|
}
|
|
180
|
-
_index = new WeakMap();
|
|
181
|
-
_topK = new WeakMap();
|
|
182
|
-
_limit = new WeakMap();
|
|
183
164
|
function topKWithFractionalIndex(comparator, options) {
|
|
184
165
|
const opts = options || {};
|
|
185
166
|
return (stream) => {
|