@tanstack/db-ivm 0.0.0 → 0.1.1

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.
Files changed (157) hide show
  1. package/LICENSE +21 -0
  2. package/dist/cjs/d2.cjs +111 -0
  3. package/dist/cjs/d2.cjs.map +1 -0
  4. package/dist/cjs/d2.d.cts +45 -0
  5. package/dist/cjs/graph.cjs +90 -0
  6. package/dist/cjs/graph.cjs.map +1 -0
  7. package/dist/cjs/graph.d.cts +57 -0
  8. package/dist/cjs/index.cjs +72 -0
  9. package/dist/cjs/index.cjs.map +1 -0
  10. package/dist/cjs/index.d.cts +4 -0
  11. package/dist/cjs/indexes.cjs +104 -0
  12. package/dist/cjs/indexes.cjs.map +1 -0
  13. package/dist/cjs/indexes.d.cts +20 -0
  14. package/dist/cjs/multiset.cjs +173 -0
  15. package/dist/cjs/multiset.cjs.map +1 -0
  16. package/dist/cjs/multiset.d.cts +36 -0
  17. package/dist/cjs/operators/concat.cjs +37 -0
  18. package/dist/cjs/operators/concat.cjs.map +1 -0
  19. package/dist/cjs/operators/concat.d.cts +13 -0
  20. package/dist/cjs/operators/consolidate.cjs +40 -0
  21. package/dist/cjs/operators/consolidate.cjs.map +1 -0
  22. package/dist/cjs/operators/consolidate.d.cts +12 -0
  23. package/dist/cjs/operators/count.cjs +36 -0
  24. package/dist/cjs/operators/count.cjs.map +1 -0
  25. package/dist/cjs/operators/count.d.cts +13 -0
  26. package/dist/cjs/operators/debug.cjs +50 -0
  27. package/dist/cjs/operators/debug.cjs.map +1 -0
  28. package/dist/cjs/operators/debug.d.cts +16 -0
  29. package/dist/cjs/operators/distinct.cjs +78 -0
  30. package/dist/cjs/operators/distinct.cjs.map +1 -0
  31. package/dist/cjs/operators/distinct.d.cts +14 -0
  32. package/dist/cjs/operators/filter.cjs +43 -0
  33. package/dist/cjs/operators/filter.cjs.map +1 -0
  34. package/dist/cjs/operators/filter.d.cts +16 -0
  35. package/dist/cjs/operators/filterBy.cjs +19 -0
  36. package/dist/cjs/operators/filterBy.cjs.map +1 -0
  37. package/dist/cjs/operators/filterBy.d.cts +10 -0
  38. package/dist/cjs/operators/groupBy.cjs +225 -0
  39. package/dist/cjs/operators/groupBy.cjs.map +1 -0
  40. package/dist/cjs/operators/groupBy.d.cts +68 -0
  41. package/dist/cjs/operators/index.d.cts +18 -0
  42. package/dist/cjs/operators/join.cjs +145 -0
  43. package/dist/cjs/operators/join.cjs.map +1 -0
  44. package/dist/cjs/operators/join.d.cts +48 -0
  45. package/dist/cjs/operators/keying.cjs +16 -0
  46. package/dist/cjs/operators/keying.cjs.map +1 -0
  47. package/dist/cjs/operators/keying.d.cts +19 -0
  48. package/dist/cjs/operators/map.cjs +43 -0
  49. package/dist/cjs/operators/map.cjs.map +1 -0
  50. package/dist/cjs/operators/map.d.cts +16 -0
  51. package/dist/cjs/operators/negate.cjs +28 -0
  52. package/dist/cjs/operators/negate.cjs.map +1 -0
  53. package/dist/cjs/operators/negate.d.cts +13 -0
  54. package/dist/cjs/operators/orderBy.cjs +102 -0
  55. package/dist/cjs/operators/orderBy.cjs.map +1 -0
  56. package/dist/cjs/operators/orderBy.d.cts +38 -0
  57. package/dist/cjs/operators/orderByBTree.d.cts +3 -0
  58. package/dist/cjs/operators/output.cjs +46 -0
  59. package/dist/cjs/operators/output.cjs.map +1 -0
  60. package/dist/cjs/operators/output.d.cts +16 -0
  61. package/dist/cjs/operators/pipe.cjs +9 -0
  62. package/dist/cjs/operators/pipe.cjs.map +1 -0
  63. package/dist/cjs/operators/pipe.d.cts +21 -0
  64. package/dist/cjs/operators/reduce.cjs +99 -0
  65. package/dist/cjs/operators/reduce.cjs.map +1 -0
  66. package/dist/cjs/operators/reduce.d.cts +14 -0
  67. package/dist/cjs/operators/topK.cjs +39 -0
  68. package/dist/cjs/operators/topK.cjs.map +1 -0
  69. package/dist/cjs/operators/topK.d.cts +32 -0
  70. package/dist/cjs/operators/topKWithFractionalIndex.cjs +218 -0
  71. package/dist/cjs/operators/topKWithFractionalIndex.cjs.map +1 -0
  72. package/dist/cjs/operators/topKWithFractionalIndex.d.cts +58 -0
  73. package/dist/cjs/operators/topKWithFractionalIndexBTree.d.cts +27 -0
  74. package/dist/cjs/types.d.cts +30 -0
  75. package/dist/cjs/utils.cjs +125 -0
  76. package/dist/cjs/utils.cjs.map +1 -0
  77. package/dist/cjs/utils.d.cts +40 -0
  78. package/dist/esm/d2.d.ts +45 -0
  79. package/dist/esm/d2.js +111 -0
  80. package/dist/esm/d2.js.map +1 -0
  81. package/dist/esm/graph.d.ts +57 -0
  82. package/dist/esm/graph.js +90 -0
  83. package/dist/esm/graph.js.map +1 -0
  84. package/dist/esm/index.d.ts +4 -0
  85. package/dist/esm/index.js +72 -0
  86. package/dist/esm/index.js.map +1 -0
  87. package/dist/esm/indexes.d.ts +20 -0
  88. package/dist/esm/indexes.js +104 -0
  89. package/dist/esm/indexes.js.map +1 -0
  90. package/dist/esm/multiset.d.ts +36 -0
  91. package/dist/esm/multiset.js +173 -0
  92. package/dist/esm/multiset.js.map +1 -0
  93. package/dist/esm/operators/concat.d.ts +13 -0
  94. package/dist/esm/operators/concat.js +37 -0
  95. package/dist/esm/operators/concat.js.map +1 -0
  96. package/dist/esm/operators/consolidate.d.ts +12 -0
  97. package/dist/esm/operators/consolidate.js +40 -0
  98. package/dist/esm/operators/consolidate.js.map +1 -0
  99. package/dist/esm/operators/count.d.ts +13 -0
  100. package/dist/esm/operators/count.js +36 -0
  101. package/dist/esm/operators/count.js.map +1 -0
  102. package/dist/esm/operators/debug.d.ts +16 -0
  103. package/dist/esm/operators/debug.js +50 -0
  104. package/dist/esm/operators/debug.js.map +1 -0
  105. package/dist/esm/operators/distinct.d.ts +14 -0
  106. package/dist/esm/operators/distinct.js +78 -0
  107. package/dist/esm/operators/distinct.js.map +1 -0
  108. package/dist/esm/operators/filter.d.ts +16 -0
  109. package/dist/esm/operators/filter.js +43 -0
  110. package/dist/esm/operators/filter.js.map +1 -0
  111. package/dist/esm/operators/filterBy.d.ts +10 -0
  112. package/dist/esm/operators/filterBy.js +19 -0
  113. package/dist/esm/operators/filterBy.js.map +1 -0
  114. package/dist/esm/operators/groupBy.d.ts +68 -0
  115. package/dist/esm/operators/groupBy.js +225 -0
  116. package/dist/esm/operators/groupBy.js.map +1 -0
  117. package/dist/esm/operators/index.d.ts +18 -0
  118. package/dist/esm/operators/join.d.ts +48 -0
  119. package/dist/esm/operators/join.js +145 -0
  120. package/dist/esm/operators/join.js.map +1 -0
  121. package/dist/esm/operators/keying.d.ts +19 -0
  122. package/dist/esm/operators/keying.js +16 -0
  123. package/dist/esm/operators/keying.js.map +1 -0
  124. package/dist/esm/operators/map.d.ts +16 -0
  125. package/dist/esm/operators/map.js +43 -0
  126. package/dist/esm/operators/map.js.map +1 -0
  127. package/dist/esm/operators/negate.d.ts +13 -0
  128. package/dist/esm/operators/negate.js +28 -0
  129. package/dist/esm/operators/negate.js.map +1 -0
  130. package/dist/esm/operators/orderBy.d.ts +38 -0
  131. package/dist/esm/operators/orderBy.js +102 -0
  132. package/dist/esm/operators/orderBy.js.map +1 -0
  133. package/dist/esm/operators/orderByBTree.d.ts +3 -0
  134. package/dist/esm/operators/output.d.ts +16 -0
  135. package/dist/esm/operators/output.js +46 -0
  136. package/dist/esm/operators/output.js.map +1 -0
  137. package/dist/esm/operators/pipe.d.ts +21 -0
  138. package/dist/esm/operators/pipe.js +9 -0
  139. package/dist/esm/operators/pipe.js.map +1 -0
  140. package/dist/esm/operators/reduce.d.ts +14 -0
  141. package/dist/esm/operators/reduce.js +99 -0
  142. package/dist/esm/operators/reduce.js.map +1 -0
  143. package/dist/esm/operators/topK.d.ts +32 -0
  144. package/dist/esm/operators/topK.js +39 -0
  145. package/dist/esm/operators/topK.js.map +1 -0
  146. package/dist/esm/operators/topKWithFractionalIndex.d.ts +58 -0
  147. package/dist/esm/operators/topKWithFractionalIndex.js +218 -0
  148. package/dist/esm/operators/topKWithFractionalIndex.js.map +1 -0
  149. package/dist/esm/operators/topKWithFractionalIndexBTree.d.ts +27 -0
  150. package/dist/esm/types.d.ts +30 -0
  151. package/dist/esm/utils.d.ts +40 -0
  152. package/dist/esm/utils.js +125 -0
  153. package/dist/esm/utils.js.map +1 -0
  154. package/package.json +6 -7
  155. package/src/operators/orderBy.ts +11 -29
  156. package/src/operators/topKWithFractionalIndex.ts +69 -57
  157. package/src/operators/topKWithFractionalIndexBTree.ts +14 -19
@@ -0,0 +1,173 @@
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 _inner, _MultiSet_instances, consolidateKeyed_fn, consolidateUnkeyed_fn;
11
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
12
+ const utils = require("./utils.cjs");
13
+ const _MultiSet = class _MultiSet {
14
+ constructor(data = []) {
15
+ __privateAdd(this, _MultiSet_instances);
16
+ __privateAdd(this, _inner);
17
+ __privateSet(this, _inner, data);
18
+ }
19
+ toString(indent = false) {
20
+ return `MultiSet(${JSON.stringify(__privateGet(this, _inner), null, indent ? 2 : void 0)})`;
21
+ }
22
+ toJSON() {
23
+ return JSON.stringify(Array.from(this.getInner()));
24
+ }
25
+ static fromJSON(json) {
26
+ return new _MultiSet(JSON.parse(json));
27
+ }
28
+ /**
29
+ * Apply a function to all records in the collection.
30
+ */
31
+ map(f) {
32
+ return new _MultiSet(
33
+ __privateGet(this, _inner).map(([data, multiplicity]) => [f(data), multiplicity])
34
+ );
35
+ }
36
+ /**
37
+ * Filter out records for which a function f(record) evaluates to False.
38
+ */
39
+ filter(f) {
40
+ return new _MultiSet(__privateGet(this, _inner).filter(([data, _]) => f(data)));
41
+ }
42
+ /**
43
+ * Negate all multiplicities in the collection.
44
+ */
45
+ negate() {
46
+ return new _MultiSet(
47
+ __privateGet(this, _inner).map(([data, multiplicity]) => [data, -multiplicity])
48
+ );
49
+ }
50
+ /**
51
+ * Concatenate two collections together.
52
+ */
53
+ concat(other) {
54
+ const out = [];
55
+ utils.chunkedArrayPush(out, __privateGet(this, _inner));
56
+ utils.chunkedArrayPush(out, other.getInner());
57
+ return new _MultiSet(out);
58
+ }
59
+ /**
60
+ * Produce as output a collection that is logically equivalent to the input
61
+ * but which combines identical instances of the same record into one
62
+ * (record, multiplicity) pair.
63
+ */
64
+ consolidate() {
65
+ var _a;
66
+ if (__privateGet(this, _inner).length > 0) {
67
+ const firstItem = (_a = __privateGet(this, _inner)[0]) == null ? void 0 : _a[0];
68
+ if (Array.isArray(firstItem) && firstItem.length === 2) {
69
+ return __privateMethod(this, _MultiSet_instances, consolidateKeyed_fn).call(this);
70
+ }
71
+ }
72
+ return __privateMethod(this, _MultiSet_instances, consolidateUnkeyed_fn).call(this);
73
+ }
74
+ extend(other) {
75
+ const otherArray = other instanceof _MultiSet ? other.getInner() : other;
76
+ utils.chunkedArrayPush(__privateGet(this, _inner), otherArray);
77
+ }
78
+ getInner() {
79
+ return __privateGet(this, _inner);
80
+ }
81
+ };
82
+ _inner = new WeakMap();
83
+ _MultiSet_instances = new WeakSet();
84
+ /**
85
+ * Private method for consolidating keyed multisets where keys are strings/numbers
86
+ * and values are compared by reference equality.
87
+ *
88
+ * This method provides significant performance improvements over the hash-based approach
89
+ * by using WeakMap for object reference tracking and avoiding expensive serialization.
90
+ *
91
+ * Special handling for join operations: When values are tuples of length 2 (common in joins),
92
+ * we unpack them and compare each element individually to maintain proper equality semantics.
93
+ */
94
+ consolidateKeyed_fn = function() {
95
+ const consolidated = /* @__PURE__ */ new Map();
96
+ const values = /* @__PURE__ */ new Map();
97
+ const getTupleId = (tuple) => {
98
+ if (tuple.length !== 2) {
99
+ throw new Error(`Expected tuple of length 2`);
100
+ }
101
+ const [first, second] = tuple;
102
+ return `${utils.globalObjectIdGenerator.getStringId(first)}|${utils.globalObjectIdGenerator.getStringId(second)}`;
103
+ };
104
+ for (const [data, multiplicity] of __privateGet(this, _inner)) {
105
+ if (!Array.isArray(data) || data.length !== 2) {
106
+ return __privateMethod(this, _MultiSet_instances, consolidateUnkeyed_fn).call(this);
107
+ }
108
+ const [key, value] = data;
109
+ if (typeof key !== `string` && typeof key !== `number`) {
110
+ return __privateMethod(this, _MultiSet_instances, consolidateUnkeyed_fn).call(this);
111
+ }
112
+ let valueId;
113
+ if (Array.isArray(value) && value.length === 2) {
114
+ valueId = getTupleId(value);
115
+ } else {
116
+ valueId = utils.globalObjectIdGenerator.getStringId(value);
117
+ }
118
+ const compositeKey = key + `|` + valueId;
119
+ consolidated.set(
120
+ compositeKey,
121
+ (consolidated.get(compositeKey) || 0) + multiplicity
122
+ );
123
+ if (!values.has(compositeKey)) {
124
+ values.set(compositeKey, data);
125
+ }
126
+ }
127
+ const result = [];
128
+ for (const [compositeKey, multiplicity] of consolidated) {
129
+ if (multiplicity !== 0) {
130
+ result.push([values.get(compositeKey), multiplicity]);
131
+ }
132
+ }
133
+ return new _MultiSet(result);
134
+ };
135
+ /**
136
+ * Private method for consolidating unkeyed multisets using the original approach.
137
+ */
138
+ consolidateUnkeyed_fn = function() {
139
+ const consolidated = new utils.DefaultMap(() => 0);
140
+ const values = /* @__PURE__ */ new Map();
141
+ let hasString = false;
142
+ let hasNumber = false;
143
+ let hasOther = false;
144
+ for (const [data, _] of __privateGet(this, _inner)) {
145
+ if (typeof data === `string`) {
146
+ hasString = true;
147
+ } else if (typeof data === `number`) {
148
+ hasNumber = true;
149
+ } else {
150
+ hasOther = true;
151
+ break;
152
+ }
153
+ }
154
+ const requireJson = hasOther || hasString && hasNumber;
155
+ for (const [data, multiplicity] of __privateGet(this, _inner)) {
156
+ const key = requireJson ? utils.hash(data) : data;
157
+ if (requireJson && !values.has(key)) {
158
+ values.set(key, data);
159
+ }
160
+ consolidated.update(key, (count) => count + multiplicity);
161
+ }
162
+ const result = [];
163
+ for (const [key, multiplicity] of consolidated.entries()) {
164
+ if (multiplicity !== 0) {
165
+ const parsedKey = requireJson ? values.get(key) : key;
166
+ result.push([parsedKey, multiplicity]);
167
+ }
168
+ }
169
+ return new _MultiSet(result);
170
+ };
171
+ let MultiSet = _MultiSet;
172
+ exports.MultiSet = MultiSet;
173
+ //# sourceMappingURL=multiset.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"multiset.cjs","sources":["../../src/multiset.ts"],"sourcesContent":["import {\n DefaultMap,\n chunkedArrayPush,\n globalObjectIdGenerator,\n hash,\n} from \"./utils.js\"\n\nexport type MultiSetArray<T> = Array<[T, number]>\nexport type KeyedData<T> = [key: string, value: T]\n\n/**\n * A multiset of data.\n */\nexport class MultiSet<T> {\n #inner: MultiSetArray<T>\n\n constructor(data: MultiSetArray<T> = []) {\n this.#inner = data\n }\n\n toString(indent = false): string {\n return `MultiSet(${JSON.stringify(this.#inner, null, indent ? 2 : undefined)})`\n }\n\n toJSON(): string {\n return JSON.stringify(Array.from(this.getInner()))\n }\n\n static fromJSON<U>(json: string): MultiSet<U> {\n return new MultiSet(JSON.parse(json))\n }\n\n /**\n * Apply a function to all records in the collection.\n */\n map<U>(f: (data: T) => U): MultiSet<U> {\n return new MultiSet(\n this.#inner.map(([data, multiplicity]) => [f(data), multiplicity])\n )\n }\n\n /**\n * Filter out records for which a function f(record) evaluates to False.\n */\n filter(f: (data: T) => boolean): MultiSet<T> {\n return new MultiSet(this.#inner.filter(([data, _]) => f(data)))\n }\n\n /**\n * Negate all multiplicities in the collection.\n */\n negate(): MultiSet<T> {\n return new MultiSet(\n this.#inner.map(([data, multiplicity]) => [data, -multiplicity])\n )\n }\n\n /**\n * Concatenate two collections together.\n */\n concat(other: MultiSet<T>): MultiSet<T> {\n const out: MultiSetArray<T> = []\n chunkedArrayPush(out, this.#inner)\n chunkedArrayPush(out, other.getInner())\n return new MultiSet(out)\n }\n\n /**\n * Produce as output a collection that is logically equivalent to the input\n * but which combines identical instances of the same record into one\n * (record, multiplicity) pair.\n */\n consolidate(): MultiSet<T> {\n // Check if this looks like a keyed multiset (first item is a tuple of length 2)\n if (this.#inner.length > 0) {\n const firstItem = this.#inner[0]?.[0]\n if (Array.isArray(firstItem) && firstItem.length === 2) {\n return this.#consolidateKeyed()\n }\n }\n\n // Fall back to original method for unkeyed data\n return this.#consolidateUnkeyed()\n }\n\n /**\n * Private method for consolidating keyed multisets where keys are strings/numbers\n * and values are compared by reference equality.\n *\n * This method provides significant performance improvements over the hash-based approach\n * by using WeakMap for object reference tracking and avoiding expensive serialization.\n *\n * Special handling for join operations: When values are tuples of length 2 (common in joins),\n * we unpack them and compare each element individually to maintain proper equality semantics.\n */\n #consolidateKeyed(): MultiSet<T> {\n const consolidated = new Map<string, number>()\n const values = new Map<string, T>()\n\n // Use global object ID generator for consistent reference equality\n\n /**\n * Special handler for tuples (arrays of length 2) commonly produced by join operations.\n * Unpacks the tuple and generates an ID based on both elements to ensure proper\n * consolidation of join results like ['A', null] and [null, 'X'].\n */\n const getTupleId = (tuple: Array<any>): string => {\n if (tuple.length !== 2) {\n throw new Error(`Expected tuple of length 2`)\n }\n const [first, second] = tuple\n return `${globalObjectIdGenerator.getStringId(first)}|${globalObjectIdGenerator.getStringId(second)}`\n }\n\n // Process each item in the multiset\n for (const [data, multiplicity] of this.#inner) {\n // Verify this is still a keyed item (should be [key, value] pair)\n if (!Array.isArray(data) || data.length !== 2) {\n // Found non-keyed item, fall back to unkeyed consolidation\n return this.#consolidateUnkeyed()\n }\n\n const [key, value] = data\n\n // Verify key is string or number as expected for keyed multisets\n if (typeof key !== `string` && typeof key !== `number`) {\n // Found non-string/number key, fall back to unkeyed consolidation\n return this.#consolidateUnkeyed()\n }\n\n // Generate value ID with special handling for join tuples\n let valueId: string\n if (Array.isArray(value) && value.length === 2) {\n // Special case: value is a tuple from join operations\n valueId = getTupleId(value)\n } else {\n // Regular case: use reference/value equality\n valueId = globalObjectIdGenerator.getStringId(value)\n }\n\n // Create composite key and consolidate\n const compositeKey = key + `|` + valueId\n consolidated.set(\n compositeKey,\n (consolidated.get(compositeKey) || 0) + multiplicity\n )\n\n // Store the original data for the first occurrence\n if (!values.has(compositeKey)) {\n values.set(compositeKey, data as T)\n }\n }\n\n // Build result array, filtering out zero multiplicities\n const result: MultiSetArray<T> = []\n for (const [compositeKey, multiplicity] of consolidated) {\n if (multiplicity !== 0) {\n result.push([values.get(compositeKey)!, multiplicity])\n }\n }\n\n return new MultiSet(result)\n }\n\n /**\n * Private method for consolidating unkeyed multisets using the original approach.\n */\n #consolidateUnkeyed(): MultiSet<T> {\n const consolidated = new DefaultMap<string | number, number>(() => 0)\n const values = new Map<string, any>()\n\n let hasString = false\n let hasNumber = false\n let hasOther = false\n for (const [data, _] of this.#inner) {\n if (typeof data === `string`) {\n hasString = true\n } else if (typeof data === `number`) {\n hasNumber = true\n } else {\n hasOther = true\n break\n }\n }\n\n const requireJson = hasOther || (hasString && hasNumber)\n\n for (const [data, multiplicity] of this.#inner) {\n const key = requireJson ? hash(data) : (data as string | number)\n if (requireJson && !values.has(key as string)) {\n values.set(key as string, data)\n }\n consolidated.update(key, (count) => count + multiplicity)\n }\n\n const result: MultiSetArray<T> = []\n for (const [key, multiplicity] of consolidated.entries()) {\n if (multiplicity !== 0) {\n const parsedKey = requireJson ? values.get(key as string) : key\n result.push([parsedKey as T, multiplicity])\n }\n }\n\n return new MultiSet(result)\n }\n\n extend(other: MultiSet<T> | MultiSetArray<T>): void {\n const otherArray = other instanceof MultiSet ? other.getInner() : other\n chunkedArrayPush(this.#inner, otherArray)\n }\n\n getInner(): MultiSetArray<T> {\n return this.#inner\n }\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B,YAAY,CAAC;AAAA,IAC5C;AAAA,EACF;AAEA,SAAO,IAAI,UAAS,MAAM;AAC5B;AA/LK,IAAM,WAAN;;"}
@@ -0,0 +1,36 @@
1
+ export type MultiSetArray<T> = Array<[T, number]>;
2
+ export type KeyedData<T> = [key: string, value: T];
3
+ /**
4
+ * A multiset of data.
5
+ */
6
+ export declare class MultiSet<T> {
7
+ #private;
8
+ constructor(data?: MultiSetArray<T>);
9
+ toString(indent?: boolean): string;
10
+ toJSON(): string;
11
+ static fromJSON<U>(json: string): MultiSet<U>;
12
+ /**
13
+ * Apply a function to all records in the collection.
14
+ */
15
+ map<U>(f: (data: T) => U): MultiSet<U>;
16
+ /**
17
+ * Filter out records for which a function f(record) evaluates to False.
18
+ */
19
+ filter(f: (data: T) => boolean): MultiSet<T>;
20
+ /**
21
+ * Negate all multiplicities in the collection.
22
+ */
23
+ negate(): MultiSet<T>;
24
+ /**
25
+ * Concatenate two collections together.
26
+ */
27
+ concat(other: MultiSet<T>): MultiSet<T>;
28
+ /**
29
+ * Produce as output a collection that is logically equivalent to the input
30
+ * but which combines identical instances of the same record into one
31
+ * (record, multiplicity) pair.
32
+ */
33
+ consolidate(): MultiSet<T>;
34
+ extend(other: MultiSet<T> | MultiSetArray<T>): void;
35
+ getInner(): MultiSetArray<T>;
36
+ }
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const graph = require("../graph.cjs");
4
+ const d2 = require("../d2.cjs");
5
+ class ConcatOperator extends graph.BinaryOperator {
6
+ run() {
7
+ for (const message of this.inputAMessages()) {
8
+ this.output.sendData(message);
9
+ }
10
+ for (const message of this.inputBMessages()) {
11
+ this.output.sendData(message);
12
+ }
13
+ }
14
+ }
15
+ function concat(other) {
16
+ return (stream) => {
17
+ if (stream.graph !== other.graph) {
18
+ throw new Error(`Cannot concat streams from different graphs`);
19
+ }
20
+ const output = new d2.StreamBuilder(
21
+ stream.graph,
22
+ new graph.DifferenceStreamWriter()
23
+ );
24
+ const operator = new ConcatOperator(
25
+ stream.graph.getNextOperatorId(),
26
+ stream.connectReader(),
27
+ other.connectReader(),
28
+ output.writer
29
+ );
30
+ stream.graph.addOperator(operator);
31
+ stream.graph.addStream(output.connectReader());
32
+ return output;
33
+ };
34
+ }
35
+ exports.ConcatOperator = ConcatOperator;
36
+ exports.concat = concat;
37
+ //# sourceMappingURL=concat.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"concat.cjs","sources":["../../../src/operators/concat.ts"],"sourcesContent":["import { BinaryOperator, DifferenceStreamWriter } from \"../graph.js\"\nimport { StreamBuilder } from \"../d2.js\"\nimport type { IStreamBuilder, PipedOperator } from \"../types.js\"\n\n/**\n * Operator that concatenates two input streams\n */\nexport class ConcatOperator<T, T2> extends BinaryOperator<T | T2> {\n run(): void {\n for (const message of this.inputAMessages()) {\n this.output.sendData(message)\n }\n\n for (const message of this.inputBMessages()) {\n this.output.sendData(message)\n }\n }\n}\n\n/**\n * Concatenates two input streams\n * @param other - The other stream to concatenate\n */\nexport function concat<T, T2>(\n other: IStreamBuilder<T2>\n): PipedOperator<T, T | T2> {\n return (stream: IStreamBuilder<T | T2>): IStreamBuilder<T | T2> => {\n if (stream.graph !== other.graph) {\n throw new Error(`Cannot concat streams from different graphs`)\n }\n const output = new StreamBuilder<T | T2>(\n stream.graph,\n new DifferenceStreamWriter<T | T2>()\n )\n const operator = new ConcatOperator<T, T2>(\n stream.graph.getNextOperatorId(),\n stream.connectReader(),\n other.connectReader(),\n output.writer\n )\n stream.graph.addOperator(operator)\n stream.graph.addStream(output.connectReader())\n return output\n }\n}\n"],"names":["BinaryOperator","StreamBuilder","DifferenceStreamWriter"],"mappings":";;;;AAOO,MAAM,uBAA8BA,MAAAA,eAAuB;AAAA,EAChE,MAAY;AACV,eAAW,WAAW,KAAK,kBAAkB;AAC3C,WAAK,OAAO,SAAS,OAAO;AAAA,IAC9B;AAEA,eAAW,WAAW,KAAK,kBAAkB;AAC3C,WAAK,OAAO,SAAS,OAAO;AAAA,IAC9B;AAAA,EACF;AACF;AAMO,SAAS,OACd,OAC0B;AAC1B,SAAO,CAAC,WAA2D;AACjE,QAAI,OAAO,UAAU,MAAM,OAAO;AAChC,YAAM,IAAI,MAAM,6CAA6C;AAAA,IAC/D;AACA,UAAM,SAAS,IAAIC,GAAAA;AAAAA,MACjB,OAAO;AAAA,MACP,IAAIC,MAAAA,uBAAA;AAAA,IAA+B;AAErC,UAAM,WAAW,IAAI;AAAA,MACnB,OAAO,MAAM,kBAAA;AAAA,MACb,OAAO,cAAA;AAAA,MACP,MAAM,cAAA;AAAA,MACN,OAAO;AAAA,IAAA;AAET,WAAO,MAAM,YAAY,QAAQ;AACjC,WAAO,MAAM,UAAU,OAAO,cAAA,CAAe;AAC7C,WAAO;AAAA,EACT;AACF;;;"}
@@ -0,0 +1,13 @@
1
+ import { BinaryOperator } from '../graph.js';
2
+ import { IStreamBuilder, PipedOperator } from '../types.js';
3
+ /**
4
+ * Operator that concatenates two input streams
5
+ */
6
+ export declare class ConcatOperator<T, T2> extends BinaryOperator<T | T2> {
7
+ run(): void;
8
+ }
9
+ /**
10
+ * Concatenates two input streams
11
+ * @param other - The other stream to concatenate
12
+ */
13
+ export declare function concat<T, T2>(other: IStreamBuilder<T2>): PipedOperator<T, T | T2>;
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const graph = require("../graph.cjs");
4
+ const d2 = require("../d2.cjs");
5
+ const multiset = require("../multiset.cjs");
6
+ class ConsolidateOperator extends graph.UnaryOperator {
7
+ run() {
8
+ const messages = this.inputMessages();
9
+ if (messages.length === 0) {
10
+ return;
11
+ }
12
+ const combined = new multiset.MultiSet();
13
+ for (const message of messages) {
14
+ combined.extend(message);
15
+ }
16
+ const consolidated = combined.consolidate();
17
+ if (consolidated.getInner().length > 0) {
18
+ this.output.sendData(consolidated);
19
+ }
20
+ }
21
+ }
22
+ function consolidate() {
23
+ return (stream) => {
24
+ const output = new d2.StreamBuilder(
25
+ stream.graph,
26
+ new graph.DifferenceStreamWriter()
27
+ );
28
+ const operator = new ConsolidateOperator(
29
+ stream.graph.getNextOperatorId(),
30
+ stream.connectReader(),
31
+ output.writer
32
+ );
33
+ stream.graph.addOperator(operator);
34
+ stream.graph.addStream(output.connectReader());
35
+ return output;
36
+ };
37
+ }
38
+ exports.ConsolidateOperator = ConsolidateOperator;
39
+ exports.consolidate = consolidate;
40
+ //# sourceMappingURL=consolidate.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"consolidate.cjs","sources":["../../../src/operators/consolidate.ts"],"sourcesContent":["import { DifferenceStreamWriter, UnaryOperator } from \"../graph.js\"\nimport { StreamBuilder } from \"../d2.js\"\nimport { MultiSet } from \"../multiset.js\"\nimport type { IStreamBuilder, PipedOperator } from \"../types.js\"\n\n/**\n * Operator that consolidates collections\n */\nexport class ConsolidateOperator<T> extends UnaryOperator<T> {\n run(): void {\n const messages = this.inputMessages()\n if (messages.length === 0) {\n return\n }\n\n // Combine all messages into a single MultiSet\n const combined = new MultiSet<T>()\n for (const message of messages) {\n combined.extend(message)\n }\n\n // Consolidate the combined MultiSet\n const consolidated = combined.consolidate()\n\n // Only send if there are results\n if (consolidated.getInner().length > 0) {\n this.output.sendData(consolidated)\n }\n }\n}\n\n/**\n * Consolidates the elements in the stream\n */\nexport function consolidate<T>(): 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 ConsolidateOperator<T>(\n stream.graph.getNextOperatorId(),\n stream.connectReader(),\n output.writer\n )\n stream.graph.addOperator(operator)\n stream.graph.addStream(output.connectReader())\n return output\n }\n}\n"],"names":["UnaryOperator","MultiSet","StreamBuilder","DifferenceStreamWriter"],"mappings":";;;;;AAQO,MAAM,4BAA+BA,MAAAA,cAAiB;AAAA,EAC3D,MAAY;AACV,UAAM,WAAW,KAAK,cAAA;AACtB,QAAI,SAAS,WAAW,GAAG;AACzB;AAAA,IACF;AAGA,UAAM,WAAW,IAAIC,kBAAA;AACrB,eAAW,WAAW,UAAU;AAC9B,eAAS,OAAO,OAAO;AAAA,IACzB;AAGA,UAAM,eAAe,SAAS,YAAA;AAG9B,QAAI,aAAa,WAAW,SAAS,GAAG;AACtC,WAAK,OAAO,SAAS,YAAY;AAAA,IACnC;AAAA,EACF;AACF;AAKO,SAAS,cAAsC;AACpD,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,IAAA;AAET,WAAO,MAAM,YAAY,QAAQ;AACjC,WAAO,MAAM,UAAU,OAAO,cAAA,CAAe;AAC7C,WAAO;AAAA,EACT;AACF;;;"}
@@ -0,0 +1,12 @@
1
+ import { UnaryOperator } from '../graph.js';
2
+ import { PipedOperator } from '../types.js';
3
+ /**
4
+ * Operator that consolidates collections
5
+ */
6
+ export declare class ConsolidateOperator<T> extends UnaryOperator<T> {
7
+ run(): void;
8
+ }
9
+ /**
10
+ * Consolidates the elements in the stream
11
+ */
12
+ export declare function consolidate<T>(): PipedOperator<T, T>;
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const graph = require("../graph.cjs");
4
+ const d2 = require("../d2.cjs");
5
+ const reduce = require("./reduce.cjs");
6
+ class CountOperator extends reduce.ReduceOperator {
7
+ constructor(id, inputA, output) {
8
+ const countInner = (vals) => {
9
+ let totalCount = 0;
10
+ for (const [_, diff] of vals) {
11
+ totalCount += diff;
12
+ }
13
+ return [[totalCount, 1]];
14
+ };
15
+ super(id, inputA, output, countInner);
16
+ }
17
+ }
18
+ function count() {
19
+ return (stream) => {
20
+ const output = new d2.StreamBuilder(
21
+ stream.graph,
22
+ new graph.DifferenceStreamWriter()
23
+ );
24
+ const operator = new CountOperator(
25
+ stream.graph.getNextOperatorId(),
26
+ stream.connectReader(),
27
+ output.writer
28
+ );
29
+ stream.graph.addOperator(operator);
30
+ stream.graph.addStream(output.connectReader());
31
+ return output;
32
+ };
33
+ }
34
+ exports.CountOperator = CountOperator;
35
+ exports.count = count;
36
+ //# sourceMappingURL=count.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"count.cjs","sources":["../../../src/operators/count.ts"],"sourcesContent":["import { DifferenceStreamWriter } from \"../graph.js\"\nimport { StreamBuilder } from \"../d2.js\"\nimport { ReduceOperator } from \"./reduce.js\"\nimport type { DifferenceStreamReader } from \"../graph.js\"\nimport type { IStreamBuilder, KeyValue } from \"../types.js\"\n\n/**\n * Operator that counts elements by key (version-free)\n */\nexport class CountOperator<K, V> extends ReduceOperator<K, V, number> {\n constructor(\n id: number,\n inputA: DifferenceStreamReader<[K, V]>,\n output: DifferenceStreamWriter<[K, number]>\n ) {\n const countInner = (vals: Array<[V, number]>): Array<[number, number]> => {\n let totalCount = 0\n for (const [_, diff] of vals) {\n totalCount += diff\n }\n return [[totalCount, 1]]\n }\n\n super(id, inputA, output, countInner)\n }\n}\n\n/**\n * Counts the number of elements by key (version-free)\n */\nexport function count<\n KType extends T extends KeyValue<infer K, infer _V> ? K : never,\n VType extends T extends KeyValue<KType, infer V> ? V : never,\n T,\n>() {\n return (\n stream: IStreamBuilder<T>\n ): IStreamBuilder<KeyValue<KType, number>> => {\n const output = new StreamBuilder<KeyValue<KType, number>>(\n stream.graph,\n new DifferenceStreamWriter<KeyValue<KType, number>>()\n )\n const operator = new CountOperator<KType, VType>(\n stream.graph.getNextOperatorId(),\n stream.connectReader() as DifferenceStreamReader<KeyValue<KType, VType>>,\n output.writer\n )\n stream.graph.addOperator(operator)\n stream.graph.addStream(output.connectReader())\n return output\n }\n}\n"],"names":["ReduceOperator","StreamBuilder","DifferenceStreamWriter"],"mappings":";;;;;AASO,MAAM,sBAA4BA,OAAAA,eAA6B;AAAA,EACpE,YACE,IACA,QACA,QACA;AACA,UAAM,aAAa,CAAC,SAAsD;AACxE,UAAI,aAAa;AACjB,iBAAW,CAAC,GAAG,IAAI,KAAK,MAAM;AAC5B,sBAAc;AAAA,MAChB;AACA,aAAO,CAAC,CAAC,YAAY,CAAC,CAAC;AAAA,IACzB;AAEA,UAAM,IAAI,QAAQ,QAAQ,UAAU;AAAA,EACtC;AACF;AAKO,SAAS,QAIZ;AACF,SAAO,CACL,WAC4C;AAC5C,UAAM,SAAS,IAAIC,GAAAA;AAAAA,MACjB,OAAO;AAAA,MACP,IAAIC,MAAAA,uBAAA;AAAA,IAAgD;AAEtD,UAAM,WAAW,IAAI;AAAA,MACnB,OAAO,MAAM,kBAAA;AAAA,MACb,OAAO,cAAA;AAAA,MACP,OAAO;AAAA,IAAA;AAET,WAAO,MAAM,YAAY,QAAQ;AACjC,WAAO,MAAM,UAAU,OAAO,cAAA,CAAe;AAC7C,WAAO;AAAA,EACT;AACF;;;"}
@@ -0,0 +1,13 @@
1
+ import { DifferenceStreamWriter, DifferenceStreamReader } from '../graph.js';
2
+ import { ReduceOperator } from './reduce.js';
3
+ import { IStreamBuilder, KeyValue } from '../types.js';
4
+ /**
5
+ * Operator that counts elements by key (version-free)
6
+ */
7
+ export declare class CountOperator<K, V> extends ReduceOperator<K, V, number> {
8
+ constructor(id: number, inputA: DifferenceStreamReader<[K, V]>, output: DifferenceStreamWriter<[K, number]>);
9
+ }
10
+ /**
11
+ * Counts the number of elements by key (version-free)
12
+ */
13
+ export declare function count<KType extends T extends KeyValue<infer K, infer _V> ? K : never, VType extends T extends KeyValue<KType, infer V> ? V : never, T>(): (stream: IStreamBuilder<T>) => IStreamBuilder<KeyValue<KType, number>>;
@@ -0,0 +1,50 @@
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 _name, _indent;
10
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
11
+ const graph = require("../graph.cjs");
12
+ const d2 = require("../d2.cjs");
13
+ class DebugOperator extends graph.UnaryOperator {
14
+ constructor(id, inputA, output, name, indent = false) {
15
+ super(id, inputA, output);
16
+ __privateAdd(this, _name);
17
+ __privateAdd(this, _indent);
18
+ __privateSet(this, _name, name);
19
+ __privateSet(this, _indent, indent);
20
+ }
21
+ run() {
22
+ for (const message of this.inputMessages()) {
23
+ console.log(`debug ${__privateGet(this, _name)} data: ${message.toString(__privateGet(this, _indent))}`);
24
+ this.output.sendData(message);
25
+ }
26
+ }
27
+ }
28
+ _name = new WeakMap();
29
+ _indent = new WeakMap();
30
+ function debug(name, indent = false) {
31
+ return (stream) => {
32
+ const output = new d2.StreamBuilder(
33
+ stream.graph,
34
+ new graph.DifferenceStreamWriter()
35
+ );
36
+ const operator = new DebugOperator(
37
+ stream.graph.getNextOperatorId(),
38
+ stream.connectReader(),
39
+ output.writer,
40
+ name,
41
+ indent
42
+ );
43
+ stream.graph.addOperator(operator);
44
+ stream.graph.addStream(output.connectReader());
45
+ return output;
46
+ };
47
+ }
48
+ exports.DebugOperator = DebugOperator;
49
+ exports.debug = debug;
50
+ //# sourceMappingURL=debug.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"debug.cjs","sources":["../../../src/operators/debug.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\"\n\n/**\n * Operator that logs debug information about the stream\n */\nexport class DebugOperator<T> extends UnaryOperator<T> {\n #name: string\n #indent: boolean\n\n constructor(\n id: number,\n inputA: DifferenceStreamReader<T>,\n output: DifferenceStreamWriter<T>,\n name: string,\n indent: boolean = false\n ) {\n super(id, inputA, output)\n this.#name = name\n this.#indent = indent\n }\n\n run(): void {\n for (const message of this.inputMessages()) {\n console.log(`debug ${this.#name} data: ${message.toString(this.#indent)}`)\n this.output.sendData(message)\n }\n }\n}\n\n/**\n * Logs debug information about the stream using console.log\n * @param name - The name to prefix debug messages with\n * @param indent - Whether to indent the debug output\n */\nexport function debug<T>(\n name: string,\n indent: boolean = false\n): 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 DebugOperator<T>(\n stream.graph.getNextOperatorId(),\n stream.connectReader(),\n output.writer,\n name,\n indent\n )\n stream.graph.addOperator(operator)\n stream.graph.addStream(output.connectReader())\n return output\n }\n}\n"],"names":["UnaryOperator","StreamBuilder","DifferenceStreamWriter"],"mappings":";;;;;;;;;;;;AAQO,MAAM,sBAAyBA,MAAAA,cAAiB;AAAA,EAIrD,YACE,IACA,QACA,QACA,MACA,SAAkB,OAClB;AACA,UAAM,IAAI,QAAQ,MAAM;AAV1B;AACA;AAUE,uBAAK,OAAQ;AACb,uBAAK,SAAU;AAAA,EACjB;AAAA,EAEA,MAAY;AACV,eAAW,WAAW,KAAK,iBAAiB;AAC1C,cAAQ,IAAI,SAAS,mBAAK,MAAK,UAAU,QAAQ,SAAS,mBAAK,QAAO,CAAC,EAAE;AACzE,WAAK,OAAO,SAAS,OAAO;AAAA,IAC9B;AAAA,EACF;AACF;AArBE;AACA;AA2BK,SAAS,MACd,MACA,SAAkB,OACG;AACrB,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,MACA;AAAA,IAAA;AAEF,WAAO,MAAM,YAAY,QAAQ;AACjC,WAAO,MAAM,UAAU,OAAO,cAAA,CAAe;AAC7C,WAAO;AAAA,EACT;AACF;;;"}
@@ -0,0 +1,16 @@
1
+ import { DifferenceStreamWriter, UnaryOperator, DifferenceStreamReader } from '../graph.js';
2
+ import { PipedOperator } from '../types.js';
3
+ /**
4
+ * Operator that logs debug information about the stream
5
+ */
6
+ export declare class DebugOperator<T> extends UnaryOperator<T> {
7
+ #private;
8
+ constructor(id: number, inputA: DifferenceStreamReader<T>, output: DifferenceStreamWriter<T>, name: string, indent?: boolean);
9
+ run(): void;
10
+ }
11
+ /**
12
+ * Logs debug information about the stream using console.log
13
+ * @param name - The name to prefix debug messages with
14
+ * @param indent - Whether to indent the debug output
15
+ */
16
+ export declare function debug<T>(name: string, indent?: boolean): PipedOperator<T, T>;
@@ -0,0 +1,78 @@
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 _by, _values;
10
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
11
+ const graph = require("../graph.cjs");
12
+ const d2 = require("../d2.cjs");
13
+ const utils = require("../utils.cjs");
14
+ const multiset = require("../multiset.cjs");
15
+ class DistinctOperator extends graph.UnaryOperator {
16
+ // keeps track of the number of times each value has been seen
17
+ constructor(id, input, output, by = (value) => value) {
18
+ super(id, input, output);
19
+ __privateAdd(this, _by);
20
+ __privateAdd(this, _values);
21
+ __privateSet(this, _by, by);
22
+ __privateSet(this, _values, /* @__PURE__ */ new Map());
23
+ }
24
+ run() {
25
+ var _a;
26
+ const updatedValues = /* @__PURE__ */ new Map();
27
+ for (const message of this.inputMessages()) {
28
+ for (const [value, diff] of message.getInner()) {
29
+ const hashedValue = utils.hash(__privateGet(this, _by).call(this, value));
30
+ const oldMultiplicity = ((_a = updatedValues.get(hashedValue)) == null ? void 0 : _a[0]) ?? __privateGet(this, _values).get(hashedValue) ?? 0;
31
+ const newMultiplicity = oldMultiplicity + diff;
32
+ updatedValues.set(hashedValue, [newMultiplicity, value]);
33
+ }
34
+ }
35
+ const result = [];
36
+ for (const [
37
+ hashedValue,
38
+ [newMultiplicity, value]
39
+ ] of updatedValues.entries()) {
40
+ const oldMultiplicity = __privateGet(this, _values).get(hashedValue) ?? 0;
41
+ if (newMultiplicity === 0) {
42
+ __privateGet(this, _values).delete(hashedValue);
43
+ } else {
44
+ __privateGet(this, _values).set(hashedValue, newMultiplicity);
45
+ }
46
+ if (oldMultiplicity <= 0 && newMultiplicity > 0) {
47
+ result.push([value, 1]);
48
+ } else if (oldMultiplicity > 0 && newMultiplicity <= 0) {
49
+ result.push([value, -1]);
50
+ }
51
+ }
52
+ if (result.length > 0) {
53
+ this.output.sendData(new multiset.MultiSet(result));
54
+ }
55
+ }
56
+ }
57
+ _by = new WeakMap();
58
+ _values = new WeakMap();
59
+ function distinct(by = (value) => value) {
60
+ return (stream) => {
61
+ const output = new d2.StreamBuilder(
62
+ stream.graph,
63
+ new graph.DifferenceStreamWriter()
64
+ );
65
+ const operator = new DistinctOperator(
66
+ stream.graph.getNextOperatorId(),
67
+ stream.connectReader(),
68
+ output.writer,
69
+ by
70
+ );
71
+ stream.graph.addOperator(operator);
72
+ stream.graph.addStream(output.connectReader());
73
+ return output;
74
+ };
75
+ }
76
+ exports.DistinctOperator = DistinctOperator;
77
+ exports.distinct = distinct;
78
+ //# sourceMappingURL=distinct.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"distinct.cjs","sources":["../../../src/operators/distinct.ts"],"sourcesContent":["import { DifferenceStreamWriter, UnaryOperator } from \"../graph.js\"\nimport { StreamBuilder } from \"../d2.js\"\nimport { hash } from \"../utils.js\"\nimport { MultiSet } from \"../multiset.js\"\nimport type { DifferenceStreamReader } from \"../graph.js\"\nimport type { IStreamBuilder } from \"../types.js\"\n\ntype HashedValue = string\ntype Multiplicity = number\n\n/**\n * Operator that removes duplicates\n */\nexport class DistinctOperator<T> extends UnaryOperator<T> {\n #by: (value: T) => any\n #values: Map<HashedValue, Multiplicity> // keeps track of the number of times each value has been seen\n\n constructor(\n id: number,\n input: DifferenceStreamReader<T>,\n output: DifferenceStreamWriter<T>,\n by: (value: T) => any = (value: T) => value\n ) {\n super(id, input, output)\n this.#by = by\n this.#values = new Map()\n }\n\n run(): void {\n const updatedValues = new Map<HashedValue, [Multiplicity, T]>()\n\n // Compute the new multiplicity for each value\n for (const message of this.inputMessages()) {\n for (const [value, diff] of message.getInner()) {\n const hashedValue = hash(this.#by(value))\n\n const oldMultiplicity =\n updatedValues.get(hashedValue)?.[0] ??\n this.#values.get(hashedValue) ??\n 0\n const newMultiplicity = oldMultiplicity + diff\n\n updatedValues.set(hashedValue, [newMultiplicity, value])\n }\n }\n\n const result: Array<[T, number]> = []\n\n // Check which values became visible or disappeared\n for (const [\n hashedValue,\n [newMultiplicity, value],\n ] of updatedValues.entries()) {\n const oldMultiplicity = this.#values.get(hashedValue) ?? 0\n\n if (newMultiplicity === 0) {\n this.#values.delete(hashedValue)\n } else {\n this.#values.set(hashedValue, newMultiplicity)\n }\n\n if (oldMultiplicity <= 0 && newMultiplicity > 0) {\n // The value wasn't present in the stream\n // but with this change it is now present in the stream\n result.push([value, 1])\n } else if (oldMultiplicity > 0 && newMultiplicity <= 0) {\n // The value was present in the stream\n // but with this change it is no longer present in the stream\n result.push([value, -1])\n }\n }\n\n if (result.length > 0) {\n this.output.sendData(new MultiSet(result))\n }\n }\n}\n\n/**\n * Removes duplicate values\n */\nexport function distinct<T>(by: (value: T) => any = (value: T) => value) {\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 DistinctOperator<T>(\n stream.graph.getNextOperatorId(),\n stream.connectReader(),\n output.writer,\n by\n )\n stream.graph.addOperator(operator)\n stream.graph.addStream(output.connectReader())\n return output\n }\n}\n"],"names":["UnaryOperator","hash","MultiSet","StreamBuilder","DifferenceStreamWriter"],"mappings":";;;;;;;;;;;;;;AAaO,MAAM,yBAA4BA,MAAAA,cAAiB;AAAA;AAAA,EAIxD,YACE,IACA,OACA,QACA,KAAwB,CAAC,UAAa,OACtC;AACA,UAAM,IAAI,OAAO,MAAM;AATzB;AACA;AASE,uBAAK,KAAM;AACX,uBAAK,6BAAc,IAAA;AAAA,EACrB;AAAA,EAEA,MAAY;;AACV,UAAM,oCAAoB,IAAA;AAG1B,eAAW,WAAW,KAAK,iBAAiB;AAC1C,iBAAW,CAAC,OAAO,IAAI,KAAK,QAAQ,YAAY;AAC9C,cAAM,cAAcC,MAAAA,KAAK,mBAAK,KAAL,WAAS,MAAM;AAExC,cAAM,oBACJ,mBAAc,IAAI,WAAW,MAA7B,mBAAiC,OACjC,mBAAK,SAAQ,IAAI,WAAW,KAC5B;AACF,cAAM,kBAAkB,kBAAkB;AAE1C,sBAAc,IAAI,aAAa,CAAC,iBAAiB,KAAK,CAAC;AAAA,MACzD;AAAA,IACF;AAEA,UAAM,SAA6B,CAAA;AAGnC,eAAW;AAAA,MACT;AAAA,MACA,CAAC,iBAAiB,KAAK;AAAA,IAAA,KACpB,cAAc,WAAW;AAC5B,YAAM,kBAAkB,mBAAK,SAAQ,IAAI,WAAW,KAAK;AAEzD,UAAI,oBAAoB,GAAG;AACzB,2BAAK,SAAQ,OAAO,WAAW;AAAA,MACjC,OAAO;AACL,2BAAK,SAAQ,IAAI,aAAa,eAAe;AAAA,MAC/C;AAEA,UAAI,mBAAmB,KAAK,kBAAkB,GAAG;AAG/C,eAAO,KAAK,CAAC,OAAO,CAAC,CAAC;AAAA,MACxB,WAAW,kBAAkB,KAAK,mBAAmB,GAAG;AAGtD,eAAO,KAAK,CAAC,OAAO,EAAE,CAAC;AAAA,MACzB;AAAA,IACF;AAEA,QAAI,OAAO,SAAS,GAAG;AACrB,WAAK,OAAO,SAAS,IAAIC,SAAAA,SAAS,MAAM,CAAC;AAAA,IAC3C;AAAA,EACF;AACF;AA9DE;AACA;AAkEK,SAAS,SAAY,KAAwB,CAAC,UAAa,OAAO;AACvE,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,MAAM,UAAU,OAAO,cAAA,CAAe;AAC7C,WAAO;AAAA,EACT;AACF;;;"}
@@ -0,0 +1,14 @@
1
+ import { DifferenceStreamWriter, UnaryOperator, DifferenceStreamReader } from '../graph.js';
2
+ import { IStreamBuilder } from '../types.js';
3
+ /**
4
+ * Operator that removes duplicates
5
+ */
6
+ export declare class DistinctOperator<T> extends UnaryOperator<T> {
7
+ #private;
8
+ constructor(id: number, input: DifferenceStreamReader<T>, output: DifferenceStreamWriter<T>, by?: (value: T) => any);
9
+ run(): void;
10
+ }
11
+ /**
12
+ * Removes duplicate values
13
+ */
14
+ export declare function distinct<T>(by?: (value: T) => any): (stream: IStreamBuilder<T>) => IStreamBuilder<T>;
@@ -0,0 +1,43 @@
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
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
11
+ const graph = require("../graph.cjs");
12
+ const d2 = require("../d2.cjs");
13
+ class FilterOperator extends graph.LinearUnaryOperator {
14
+ constructor(id, inputA, output, f) {
15
+ super(id, inputA, output);
16
+ __privateAdd(this, _f);
17
+ __privateSet(this, _f, f);
18
+ }
19
+ inner(collection) {
20
+ return collection.filter(__privateGet(this, _f));
21
+ }
22
+ }
23
+ _f = new WeakMap();
24
+ function filter(f) {
25
+ return (stream) => {
26
+ const output = new d2.StreamBuilder(
27
+ stream.graph,
28
+ new graph.DifferenceStreamWriter()
29
+ );
30
+ const operator = new FilterOperator(
31
+ stream.graph.getNextOperatorId(),
32
+ stream.connectReader(),
33
+ output.writer,
34
+ f
35
+ );
36
+ stream.graph.addOperator(operator);
37
+ stream.graph.addStream(output.connectReader());
38
+ return output;
39
+ };
40
+ }
41
+ exports.FilterOperator = FilterOperator;
42
+ exports.filter = filter;
43
+ //# sourceMappingURL=filter.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"filter.cjs","sources":["../../../src/operators/filter.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 filters elements from the input stream\n */\nexport class FilterOperator<T> extends LinearUnaryOperator<T, T> {\n #f: (data: T) => boolean\n\n constructor(\n id: number,\n inputA: DifferenceStreamReader<T>,\n output: DifferenceStreamWriter<T>,\n f: (data: T) => boolean\n ) {\n super(id, inputA, output)\n this.#f = f\n }\n\n inner(collection: MultiSet<T>): MultiSet<T> {\n return collection.filter(this.#f)\n }\n}\n\n/**\n * Filters elements from the input stream\n * @param f - The predicate to filter elements\n */\nexport function filter<T>(f: (data: T) => boolean): 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 FilterOperator<T>(\n stream.graph.getNextOperatorId(),\n stream.connectReader(),\n output.writer,\n f\n )\n stream.graph.addOperator(operator)\n stream.graph.addStream(output.connectReader())\n return output\n }\n}\n"],"names":["LinearUnaryOperator","StreamBuilder","DifferenceStreamWriter"],"mappings":";;;;;;;;;;;;AASO,MAAM,uBAA0BA,MAAAA,oBAA0B;AAAA,EAG/D,YACE,IACA,QACA,QACA,GACA;AACA,UAAM,IAAI,QAAQ,MAAM;AAR1B;AASE,uBAAK,IAAK;AAAA,EACZ;AAAA,EAEA,MAAM,YAAsC;AAC1C,WAAO,WAAW,OAAO,mBAAK,GAAE;AAAA,EAClC;AACF;AAfE;AAqBK,SAAS,OAAU,GAA8C;AACtE,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,MAAM,UAAU,OAAO,cAAA,CAAe;AAC7C,WAAO;AAAA,EACT;AACF;;;"}