@tanstack/db-ivm 0.1.6 → 0.1.8

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 (104) hide show
  1. package/dist/cjs/d2.cjs +1 -8
  2. package/dist/cjs/d2.cjs.map +1 -1
  3. package/dist/cjs/d2.d.cts +0 -1
  4. package/dist/cjs/indexes.cjs +243 -85
  5. package/dist/cjs/indexes.cjs.map +1 -1
  6. package/dist/cjs/indexes.d.cts +58 -7
  7. package/dist/cjs/operators/concat.cjs +0 -1
  8. package/dist/cjs/operators/concat.cjs.map +1 -1
  9. package/dist/cjs/operators/consolidate.cjs +0 -1
  10. package/dist/cjs/operators/consolidate.cjs.map +1 -1
  11. package/dist/cjs/operators/count.cjs +0 -1
  12. package/dist/cjs/operators/count.cjs.map +1 -1
  13. package/dist/cjs/operators/debug.cjs +0 -1
  14. package/dist/cjs/operators/debug.cjs.map +1 -1
  15. package/dist/cjs/operators/distinct.cjs +2 -3
  16. package/dist/cjs/operators/distinct.cjs.map +1 -1
  17. package/dist/cjs/operators/distinct.d.cts +6 -4
  18. package/dist/cjs/operators/filter.cjs +0 -1
  19. package/dist/cjs/operators/filter.cjs.map +1 -1
  20. package/dist/cjs/operators/join.cjs +0 -1
  21. package/dist/cjs/operators/join.cjs.map +1 -1
  22. package/dist/cjs/operators/map.cjs +0 -1
  23. package/dist/cjs/operators/map.cjs.map +1 -1
  24. package/dist/cjs/operators/negate.cjs +0 -1
  25. package/dist/cjs/operators/negate.cjs.map +1 -1
  26. package/dist/cjs/operators/output.cjs +0 -1
  27. package/dist/cjs/operators/output.cjs.map +1 -1
  28. package/dist/cjs/operators/reduce.cjs +0 -1
  29. package/dist/cjs/operators/reduce.cjs.map +1 -1
  30. package/dist/cjs/operators/tap.cjs +0 -1
  31. package/dist/cjs/operators/tap.cjs.map +1 -1
  32. package/dist/cjs/operators/topKWithFractionalIndex.cjs +0 -1
  33. package/dist/cjs/operators/topKWithFractionalIndex.cjs.map +1 -1
  34. package/dist/cjs/types.d.cts +0 -1
  35. package/dist/cjs/utils.cjs +0 -12
  36. package/dist/cjs/utils.cjs.map +1 -1
  37. package/dist/esm/d2.d.ts +0 -1
  38. package/dist/esm/d2.js +1 -8
  39. package/dist/esm/d2.js.map +1 -1
  40. package/dist/esm/indexes.d.ts +58 -7
  41. package/dist/esm/indexes.js +243 -85
  42. package/dist/esm/indexes.js.map +1 -1
  43. package/dist/esm/operators/concat.js +0 -1
  44. package/dist/esm/operators/concat.js.map +1 -1
  45. package/dist/esm/operators/consolidate.js +0 -1
  46. package/dist/esm/operators/consolidate.js.map +1 -1
  47. package/dist/esm/operators/count.js +0 -1
  48. package/dist/esm/operators/count.js.map +1 -1
  49. package/dist/esm/operators/debug.js +0 -1
  50. package/dist/esm/operators/debug.js.map +1 -1
  51. package/dist/esm/operators/distinct.d.ts +6 -4
  52. package/dist/esm/operators/distinct.js +2 -3
  53. package/dist/esm/operators/distinct.js.map +1 -1
  54. package/dist/esm/operators/filter.js +0 -1
  55. package/dist/esm/operators/filter.js.map +1 -1
  56. package/dist/esm/operators/join.js +0 -1
  57. package/dist/esm/operators/join.js.map +1 -1
  58. package/dist/esm/operators/map.js +0 -1
  59. package/dist/esm/operators/map.js.map +1 -1
  60. package/dist/esm/operators/negate.js +0 -1
  61. package/dist/esm/operators/negate.js.map +1 -1
  62. package/dist/esm/operators/output.js +0 -1
  63. package/dist/esm/operators/output.js.map +1 -1
  64. package/dist/esm/operators/reduce.js +0 -1
  65. package/dist/esm/operators/reduce.js.map +1 -1
  66. package/dist/esm/operators/tap.js +0 -1
  67. package/dist/esm/operators/tap.js.map +1 -1
  68. package/dist/esm/operators/topKWithFractionalIndex.js +0 -1
  69. package/dist/esm/operators/topKWithFractionalIndex.js.map +1 -1
  70. package/dist/esm/types.d.ts +0 -1
  71. package/dist/esm/utils.js +1 -13
  72. package/dist/esm/utils.js.map +1 -1
  73. package/package.json +1 -1
  74. package/src/d2.ts +0 -7
  75. package/src/indexes.ts +365 -90
  76. package/src/operators/concat.ts +0 -1
  77. package/src/operators/consolidate.ts +0 -1
  78. package/src/operators/count.ts +0 -1
  79. package/src/operators/debug.ts +0 -1
  80. package/src/operators/distinct.ts +13 -9
  81. package/src/operators/filter.ts +0 -1
  82. package/src/operators/join.ts +0 -1
  83. package/src/operators/map.ts +0 -1
  84. package/src/operators/negate.ts +0 -1
  85. package/src/operators/output.ts +0 -1
  86. package/src/operators/reduce.ts +0 -1
  87. package/src/operators/tap.ts +0 -1
  88. package/src/operators/topKWithFractionalIndex.ts +0 -1
  89. package/src/operators/topKWithFractionalIndexBTree.ts +0 -1
  90. package/src/types.ts +0 -1
  91. package/dist/cjs/hashIndex.cjs +0 -83
  92. package/dist/cjs/hashIndex.cjs.map +0 -1
  93. package/dist/cjs/hashIndex.d.cts +0 -25
  94. package/dist/cjs/valueIndex.cjs +0 -74
  95. package/dist/cjs/valueIndex.cjs.map +0 -1
  96. package/dist/cjs/valueIndex.d.cts +0 -17
  97. package/dist/esm/hashIndex.d.ts +0 -25
  98. package/dist/esm/hashIndex.js +0 -83
  99. package/dist/esm/hashIndex.js.map +0 -1
  100. package/dist/esm/valueIndex.d.ts +0 -17
  101. package/dist/esm/valueIndex.js +0 -74
  102. package/dist/esm/valueIndex.js.map +0 -1
  103. package/src/hashIndex.ts +0 -94
  104. package/src/valueIndex.ts +0 -78
package/dist/cjs/d2.cjs CHANGED
@@ -15,13 +15,12 @@ var __privateWrapper = (obj, member, setter, getter) => ({
15
15
  return __privateGet(obj, member, getter);
16
16
  }
17
17
  });
18
- var _streams, _operators, _nextOperatorId, _finalized, _D2_instances, checkNotFinalized_fn, _graph, _writer;
18
+ var _operators, _nextOperatorId, _finalized, _D2_instances, checkNotFinalized_fn, _graph, _writer;
19
19
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
20
20
  const graph = require("./graph.cjs");
21
21
  class D2 {
22
22
  constructor() {
23
23
  __privateAdd(this, _D2_instances);
24
- __privateAdd(this, _streams, []);
25
24
  __privateAdd(this, _operators, []);
26
25
  __privateAdd(this, _nextOperatorId, 0);
27
26
  __privateAdd(this, _finalized, false);
@@ -34,17 +33,12 @@ class D2 {
34
33
  __privateMethod(this, _D2_instances, checkNotFinalized_fn).call(this);
35
34
  const writer = new graph.DifferenceStreamWriter();
36
35
  const streamBuilder = new RootStreamBuilder(this, writer);
37
- __privateGet(this, _streams).push(streamBuilder.connectReader());
38
36
  return streamBuilder;
39
37
  }
40
38
  addOperator(operator) {
41
39
  __privateMethod(this, _D2_instances, checkNotFinalized_fn).call(this);
42
40
  __privateGet(this, _operators).push(operator);
43
41
  }
44
- addStream(stream) {
45
- __privateMethod(this, _D2_instances, checkNotFinalized_fn).call(this);
46
- __privateGet(this, _streams).push(stream);
47
- }
48
42
  finalize() {
49
43
  __privateMethod(this, _D2_instances, checkNotFinalized_fn).call(this);
50
44
  __privateSet(this, _finalized, true);
@@ -66,7 +60,6 @@ class D2 {
66
60
  }
67
61
  }
68
62
  }
69
- _streams = new WeakMap();
70
63
  _operators = new WeakMap();
71
64
  _nextOperatorId = new WeakMap();
72
65
  _finalized = new WeakMap();
@@ -1 +1 @@
1
- {"version":3,"file":"d2.cjs","sources":["../../src/d2.ts"],"sourcesContent":["import { DifferenceStreamWriter } from \"./graph.js\"\nimport type {\n BinaryOperator,\n DifferenceStreamReader,\n UnaryOperator,\n} from \"./graph.js\"\nimport type { MultiSet, MultiSetArray } from \"./multiset.js\"\nimport type { ID2, IStreamBuilder, PipedOperator } from \"./types.js\"\n\nexport class D2 implements ID2 {\n #streams: Array<DifferenceStreamReader<any>> = []\n #operators: Array<UnaryOperator<any> | BinaryOperator<any>> = []\n #nextOperatorId = 0\n #finalized = false\n\n constructor() {}\n\n #checkNotFinalized(): void {\n if (this.#finalized) {\n throw new Error(`Graph already finalized`)\n }\n }\n\n getNextOperatorId(): number {\n this.#checkNotFinalized()\n return this.#nextOperatorId++\n }\n\n newInput<T>(): RootStreamBuilder<T> {\n this.#checkNotFinalized()\n const writer = new DifferenceStreamWriter<T>()\n // Use the root stream builder that exposes the sendData and sendFrontier methods\n const streamBuilder = new RootStreamBuilder<T>(this, writer)\n this.#streams.push(streamBuilder.connectReader())\n return streamBuilder\n }\n\n addOperator(operator: UnaryOperator<any> | BinaryOperator<any>): void {\n this.#checkNotFinalized()\n this.#operators.push(operator)\n }\n\n addStream(stream: DifferenceStreamReader<any>): void {\n this.#checkNotFinalized()\n this.#streams.push(stream)\n }\n\n finalize() {\n this.#checkNotFinalized()\n this.#finalized = true\n }\n\n step(): void {\n if (!this.#finalized) {\n throw new Error(`Graph not finalized`)\n }\n for (const op of this.#operators) {\n op.run()\n }\n }\n\n pendingWork(): boolean {\n return this.#operators.some((op) => op.hasPendingWork())\n }\n\n run(): void {\n while (this.pendingWork()) {\n this.step()\n }\n }\n}\n\nexport class StreamBuilder<T> implements IStreamBuilder<T> {\n #graph: ID2\n #writer: DifferenceStreamWriter<T>\n\n constructor(graph: ID2, writer: DifferenceStreamWriter<T>) {\n this.#graph = graph\n this.#writer = writer\n }\n\n connectReader(): DifferenceStreamReader<T> {\n return this.#writer.newReader()\n }\n\n get writer(): DifferenceStreamWriter<T> {\n return this.#writer\n }\n\n get graph(): ID2 {\n return this.#graph\n }\n\n // Don't judge, this is the only way to type this function.\n // rxjs has very similar code to type its pipe function\n // https://github.com/ReactiveX/rxjs/blob/master/packages/rxjs/src/internal/util/pipe.ts\n // We go to 20 operators deep, because surly that's enough for anyone...\n // A user can always split the pipe into multiple pipes to get around this.\n pipe<O>(o1: PipedOperator<T, O>): IStreamBuilder<O>\n // prettier-ignore\n pipe<T2, O>(o1: PipedOperator<T, T2>, o2: PipedOperator<T2, O>): IStreamBuilder<O>\n // prettier-ignore\n pipe<T2, T3, O>(o1: PipedOperator<T, T2>, o2: PipedOperator<T2, T3>, o3: PipedOperator<T3, O>): IStreamBuilder<O>\n // prettier-ignore\n pipe<T2, T3, T4, O>(o1: PipedOperator<T, T2>, o2: PipedOperator<T2, T3>, o3: PipedOperator<T3, T4>, o4: PipedOperator<T4, O>): IStreamBuilder<O>\n // prettier-ignore\n pipe<T2, T3, T4, T5, O>(o1: PipedOperator<T, T2>, o2: PipedOperator<T2, T3>, o3: PipedOperator<T3, T4>, o4: PipedOperator<T4, T5>, o5: PipedOperator<T5, O>): IStreamBuilder<O>\n // prettier-ignore\n pipe<T2, T3, T4, T5, T6, O>(o1: PipedOperator<T, T2>, o2: PipedOperator<T2, T3>, o3: PipedOperator<T3, T4>, o4: PipedOperator<T4, T5>, o5: PipedOperator<T5, T6>, o6: PipedOperator<T6, O>): IStreamBuilder<O>\n // prettier-ignore\n pipe<T2, T3, T4, T5, T6, T7, O>(o1: PipedOperator<T, T2>, o2: PipedOperator<T2, T3>, o3: PipedOperator<T3, T4>, o4: PipedOperator<T4, T5>, o5: PipedOperator<T5, T6>, o6: PipedOperator<T6, T7>, o7: PipedOperator<T7, O>): IStreamBuilder<O>\n // prettier-ignore\n pipe<T2, T3, T4, T5, T6, T7, T8, O>(o1: PipedOperator<T, T2>, o2: PipedOperator<T2, T3>, o3: PipedOperator<T3, T4>, o4: PipedOperator<T4, T5>, o5: PipedOperator<T5, T6>, o6: PipedOperator<T6, T7>, o7: PipedOperator<T7, T8>, o8: PipedOperator<T8, O>): IStreamBuilder<O>\n // prettier-ignore\n pipe<T2, T3, T4, T5, T6, T7, T8, T9, O>(o1: PipedOperator<T, T2>, o2: PipedOperator<T2, T3>, o3: PipedOperator<T3, T4>, o4: PipedOperator<T4, T5>, o5: PipedOperator<T5, T6>, o6: PipedOperator<T6, T7>, o7: PipedOperator<T7, T8>, o8: PipedOperator<T8, T9>, o9: PipedOperator<T9, O>): IStreamBuilder<O>\n // prettier-ignore\n pipe<T2, T3, T4, T5, T6, T7, T8, T9, T10, O>(o1: PipedOperator<T, T2>, o2: PipedOperator<T2, T3>, o3: PipedOperator<T3, T4>, o4: PipedOperator<T4, T5>, o5: PipedOperator<T5, T6>, o6: PipedOperator<T6, T7>, o7: PipedOperator<T7, T8>, o8: PipedOperator<T8, T9>, o9: PipedOperator<T9, T10>, o10: PipedOperator<T10, O>): IStreamBuilder<O>\n // prettier-ignore\n pipe<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, O>(o1: PipedOperator<T, T2>, o2: PipedOperator<T2, T3>, o3: PipedOperator<T3, T4>, o4: PipedOperator<T4, T5>, o5: PipedOperator<T5, T6>, o6: PipedOperator<T6, T7>, o7: PipedOperator<T7, T8>, o8: PipedOperator<T8, T9>, o9: PipedOperator<T9, T10>, o10: PipedOperator<T10, T11>, o11: PipedOperator<T11, O>): IStreamBuilder<O>\n // prettier-ignore\n pipe<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, O>(o1: PipedOperator<T, T2>, o2: PipedOperator<T2, T3>, o3: PipedOperator<T3, T4>, o4: PipedOperator<T4, T5>, o5: PipedOperator<T5, T6>, o6: PipedOperator<T6, T7>, o7: PipedOperator<T7, T8>, o8: PipedOperator<T8, T9>, o9: PipedOperator<T9, T10>, o10: PipedOperator<T10, T11>, o11: PipedOperator<T11, T12>, o12: PipedOperator<T12, O>): IStreamBuilder<O>\n // prettier-ignore\n pipe<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, O>(o1: PipedOperator<T, T2>, o2: PipedOperator<T2, T3>, o3: PipedOperator<T3, T4>, o4: PipedOperator<T4, T5>, o5: PipedOperator<T5, T6>, o6: PipedOperator<T6, T7>, o7: PipedOperator<T7, T8>, o8: PipedOperator<T8, T9>, o9: PipedOperator<T9, T10>, o10: PipedOperator<T10, T11>, o11: PipedOperator<T11, T12>, o12: PipedOperator<T12, T13>, o13: PipedOperator<T13, O>): IStreamBuilder<O>\n // prettier-ignore\n pipe<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, O>(o1: PipedOperator<T, T2>, o2: PipedOperator<T2, T3>, o3: PipedOperator<T3, T4>, o4: PipedOperator<T4, T5>, o5: PipedOperator<T5, T6>, o6: PipedOperator<T6, T7>, o7: PipedOperator<T7, T8>, o8: PipedOperator<T8, T9>, o9: PipedOperator<T9, T10>, o10: PipedOperator<T10, T11>, o11: PipedOperator<T11, T12>, o12: PipedOperator<T12, T13>, o13: PipedOperator<T13, T14>, o14: PipedOperator<T14, O>): IStreamBuilder<O>\n // prettier-ignore\n pipe<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, O>(o1: PipedOperator<T, T2>, o2: PipedOperator<T2, T3>, o3: PipedOperator<T3, T4>, o4: PipedOperator<T4, T5>, o5: PipedOperator<T5, T6>, o6: PipedOperator<T6, T7>, o7: PipedOperator<T7, T8>, o8: PipedOperator<T8, T9>, o9: PipedOperator<T9, T10>, o10: PipedOperator<T10, T11>, o11: PipedOperator<T11, T12>, o12: PipedOperator<T12, T13>, o13: PipedOperator<T13, T14>, o14: PipedOperator<T14, T15>, o15: PipedOperator<T15, O>): IStreamBuilder<O>\n // prettier-ignore\n pipe<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, O>(o1: PipedOperator<T, T2>, o2: PipedOperator<T2, T3>, o3: PipedOperator<T3, T4>, o4: PipedOperator<T4, T5>, o5: PipedOperator<T5, T6>, o6: PipedOperator<T6, T7>, o7: PipedOperator<T7, T8>, o8: PipedOperator<T8, T9>, o9: PipedOperator<T9, T10>, o10: PipedOperator<T10, T11>, o11: PipedOperator<T11, T12>, o12: PipedOperator<T12, T13>, o13: PipedOperator<T13, T14>, o14: PipedOperator<T14, T15>, o15: PipedOperator<T15, T16>, o16: PipedOperator<T16, O>): IStreamBuilder<O>\n // prettier-ignore\n pipe<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, O>(o1: PipedOperator<T, T2>, o2: PipedOperator<T2, T3>, o3: PipedOperator<T3, T4>, o4: PipedOperator<T4, T5>, o5: PipedOperator<T5, T6>, o6: PipedOperator<T6, T7>, o7: PipedOperator<T7, T8>, o8: PipedOperator<T8, T9>, o9: PipedOperator<T9, T10>, o10: PipedOperator<T10, T11>, o11: PipedOperator<T11, T12>, o12: PipedOperator<T12, T13>, o13: PipedOperator<T13, T14>, o14: PipedOperator<T14, T15>, o15: PipedOperator<T15, T16>, o16: PipedOperator<T16, T17>, o17: PipedOperator<T17, O>): IStreamBuilder<O>\n // prettier-ignore\n pipe<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, O>(o1: PipedOperator<T, T2>, o2: PipedOperator<T2, T3>, o3: PipedOperator<T3, T4>, o4: PipedOperator<T4, T5>, o5: PipedOperator<T5, T6>, o6: PipedOperator<T6, T7>, o7: PipedOperator<T7, T8>, o8: PipedOperator<T8, T9>, o9: PipedOperator<T9, T10>, o10: PipedOperator<T10, T11>, o11: PipedOperator<T11, T12>, o12: PipedOperator<T12, T13>, o13: PipedOperator<T13, T14>, o14: PipedOperator<T14, T15>, o15: PipedOperator<T15, T16>, o16: PipedOperator<T16, T17>, o17: PipedOperator<T17, T18>, o18: PipedOperator<T18, O>): IStreamBuilder<O>\n // prettier-ignore\n pipe<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, O>(o1: PipedOperator<T, T2>, o2: PipedOperator<T2, T3>, o3: PipedOperator<T3, T4>, o4: PipedOperator<T4, T5>, o5: PipedOperator<T5, T6>, o6: PipedOperator<T6, T7>, o7: PipedOperator<T7, T8>, o8: PipedOperator<T8, T9>, o9: PipedOperator<T9, T10>, o10: PipedOperator<T10, T11>, o11: PipedOperator<T11, T12>, o12: PipedOperator<T12, T13>, o13: PipedOperator<T13, T14>, o14: PipedOperator<T14, T15>, o15: PipedOperator<T15, T16>, o16: PipedOperator<T16, T17>, o17: PipedOperator<T17, T18>, o18: PipedOperator<T18, T19>, o19: PipedOperator<T19, O>): IStreamBuilder<O>\n // prettier-ignore\n pipe<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, O>(o1: PipedOperator<T, T2>, o2: PipedOperator<T2, T3>, o3: PipedOperator<T3, T4>, o4: PipedOperator<T4, T5>, o5: PipedOperator<T5, T6>, o6: PipedOperator<T6, T7>, o7: PipedOperator<T7, T8>, o8: PipedOperator<T8, T9>, o9: PipedOperator<T9, T10>, o10: PipedOperator<T10, T11>, o11: PipedOperator<T11, T12>, o12: PipedOperator<T12, T13>, o13: PipedOperator<T13, T14>, o14: PipedOperator<T14, T15>, o15: PipedOperator<T15, T16>, o16: PipedOperator<T16, T17>, o17: PipedOperator<T17, T18>, o18: PipedOperator<T18, T19>, o19: PipedOperator<T19, T20>, o20: PipedOperator<T20, O>): IStreamBuilder<O>\n\n pipe(...operators: Array<PipedOperator<any, any>>): IStreamBuilder<any> {\n return operators.reduce((stream, operator) => {\n return operator(stream)\n }, this as IStreamBuilder<any>)\n }\n}\n\nexport class RootStreamBuilder<T> extends StreamBuilder<T> {\n sendData(collection: MultiSet<T> | MultiSetArray<T>): void {\n this.writer.sendData(collection)\n }\n}\n"],"names":["DifferenceStreamWriter","graph"],"mappings":";;;;;;;;;;;;;;;;;;;;AASO,MAAM,GAAkB;AAAA,EAM7B,cAAc;AANT;AACL,iCAA+C,CAAA;AAC/C,mCAA8D,CAAA;AAC9D,wCAAkB;AAClB,mCAAa;AAAA,EAEE;AAAA,EAQf,oBAA4B;AAC1B,0BAAK,qCAAL;AACA,WAAO,uBAAK,iBAAL;AAAA,EACT;AAAA,EAEA,WAAoC;AAClC,0BAAK,qCAAL;AACA,UAAM,SAAS,IAAIA,6BAAA;AAEnB,UAAM,gBAAgB,IAAI,kBAAqB,MAAM,MAAM;AAC3D,uBAAK,UAAS,KAAK,cAAc,cAAA,CAAe;AAChD,WAAO;AAAA,EACT;AAAA,EAEA,YAAY,UAA0D;AACpE,0BAAK,qCAAL;AACA,uBAAK,YAAW,KAAK,QAAQ;AAAA,EAC/B;AAAA,EAEA,UAAU,QAA2C;AACnD,0BAAK,qCAAL;AACA,uBAAK,UAAS,KAAK,MAAM;AAAA,EAC3B;AAAA,EAEA,WAAW;AACT,0BAAK,qCAAL;AACA,uBAAK,YAAa;AAAA,EACpB;AAAA,EAEA,OAAa;AACX,QAAI,CAAC,mBAAK,aAAY;AACpB,YAAM,IAAI,MAAM,qBAAqB;AAAA,IACvC;AACA,eAAW,MAAM,mBAAK,aAAY;AAChC,SAAG,IAAA;AAAA,IACL;AAAA,EACF;AAAA,EAEA,cAAuB;AACrB,WAAO,mBAAK,YAAW,KAAK,CAAC,OAAO,GAAG,gBAAgB;AAAA,EACzD;AAAA,EAEA,MAAY;AACV,WAAO,KAAK,eAAe;AACzB,WAAK,KAAA;AAAA,IACP;AAAA,EACF;AACF;AA5DE;AACA;AACA;AACA;AAJK;AAQL,uBAAA,WAA2B;AACzB,MAAI,mBAAK,aAAY;AACnB,UAAM,IAAI,MAAM,yBAAyB;AAAA,EAC3C;AACF;AAmDK,MAAM,cAA8C;AAAA,EAIzD,YAAYC,QAAY,QAAmC;AAH3D;AACA;AAGE,uBAAK,QAASA;AACd,uBAAK,SAAU;AAAA,EACjB;AAAA,EAEA,gBAA2C;AACzC,WAAO,mBAAK,SAAQ,UAAA;AAAA,EACtB;AAAA,EAEA,IAAI,SAAoC;AACtC,WAAO,mBAAK;AAAA,EACd;AAAA,EAEA,IAAI,QAAa;AACf,WAAO,mBAAK;AAAA,EACd;AAAA,EA+CA,QAAQ,WAAgE;AACtE,WAAO,UAAU,OAAO,CAAC,QAAQ,aAAa;AAC5C,aAAO,SAAS,MAAM;AAAA,IACxB,GAAG,IAA2B;AAAA,EAChC;AACF;AAtEE;AACA;AAuEK,MAAM,0BAA6B,cAAiB;AAAA,EACzD,SAAS,YAAkD;AACzD,SAAK,OAAO,SAAS,UAAU;AAAA,EACjC;AACF;;;;"}
1
+ {"version":3,"file":"d2.cjs","sources":["../../src/d2.ts"],"sourcesContent":["import { DifferenceStreamWriter } from \"./graph.js\"\nimport type {\n BinaryOperator,\n DifferenceStreamReader,\n UnaryOperator,\n} from \"./graph.js\"\nimport type { MultiSet, MultiSetArray } from \"./multiset.js\"\nimport type { ID2, IStreamBuilder, PipedOperator } from \"./types.js\"\n\nexport class D2 implements ID2 {\n #operators: Array<UnaryOperator<any> | BinaryOperator<any>> = []\n #nextOperatorId = 0\n #finalized = false\n\n constructor() {}\n\n #checkNotFinalized(): void {\n if (this.#finalized) {\n throw new Error(`Graph already finalized`)\n }\n }\n\n getNextOperatorId(): number {\n this.#checkNotFinalized()\n return this.#nextOperatorId++\n }\n\n newInput<T>(): RootStreamBuilder<T> {\n this.#checkNotFinalized()\n const writer = new DifferenceStreamWriter<T>()\n // Use the root stream builder that exposes the sendData and sendFrontier methods\n const streamBuilder = new RootStreamBuilder<T>(this, writer)\n return streamBuilder\n }\n\n addOperator(operator: UnaryOperator<any> | BinaryOperator<any>): void {\n this.#checkNotFinalized()\n this.#operators.push(operator)\n }\n\n finalize() {\n this.#checkNotFinalized()\n this.#finalized = true\n }\n\n step(): void {\n if (!this.#finalized) {\n throw new Error(`Graph not finalized`)\n }\n for (const op of this.#operators) {\n op.run()\n }\n }\n\n pendingWork(): boolean {\n return this.#operators.some((op) => op.hasPendingWork())\n }\n\n run(): void {\n while (this.pendingWork()) {\n this.step()\n }\n }\n}\n\nexport class StreamBuilder<T> implements IStreamBuilder<T> {\n #graph: ID2\n #writer: DifferenceStreamWriter<T>\n\n constructor(graph: ID2, writer: DifferenceStreamWriter<T>) {\n this.#graph = graph\n this.#writer = writer\n }\n\n connectReader(): DifferenceStreamReader<T> {\n return this.#writer.newReader()\n }\n\n get writer(): DifferenceStreamWriter<T> {\n return this.#writer\n }\n\n get graph(): ID2 {\n return this.#graph\n }\n\n // Don't judge, this is the only way to type this function.\n // rxjs has very similar code to type its pipe function\n // https://github.com/ReactiveX/rxjs/blob/master/packages/rxjs/src/internal/util/pipe.ts\n // We go to 20 operators deep, because surly that's enough for anyone...\n // A user can always split the pipe into multiple pipes to get around this.\n pipe<O>(o1: PipedOperator<T, O>): IStreamBuilder<O>\n // prettier-ignore\n pipe<T2, O>(o1: PipedOperator<T, T2>, o2: PipedOperator<T2, O>): IStreamBuilder<O>\n // prettier-ignore\n pipe<T2, T3, O>(o1: PipedOperator<T, T2>, o2: PipedOperator<T2, T3>, o3: PipedOperator<T3, O>): IStreamBuilder<O>\n // prettier-ignore\n pipe<T2, T3, T4, O>(o1: PipedOperator<T, T2>, o2: PipedOperator<T2, T3>, o3: PipedOperator<T3, T4>, o4: PipedOperator<T4, O>): IStreamBuilder<O>\n // prettier-ignore\n pipe<T2, T3, T4, T5, O>(o1: PipedOperator<T, T2>, o2: PipedOperator<T2, T3>, o3: PipedOperator<T3, T4>, o4: PipedOperator<T4, T5>, o5: PipedOperator<T5, O>): IStreamBuilder<O>\n // prettier-ignore\n pipe<T2, T3, T4, T5, T6, O>(o1: PipedOperator<T, T2>, o2: PipedOperator<T2, T3>, o3: PipedOperator<T3, T4>, o4: PipedOperator<T4, T5>, o5: PipedOperator<T5, T6>, o6: PipedOperator<T6, O>): IStreamBuilder<O>\n // prettier-ignore\n pipe<T2, T3, T4, T5, T6, T7, O>(o1: PipedOperator<T, T2>, o2: PipedOperator<T2, T3>, o3: PipedOperator<T3, T4>, o4: PipedOperator<T4, T5>, o5: PipedOperator<T5, T6>, o6: PipedOperator<T6, T7>, o7: PipedOperator<T7, O>): IStreamBuilder<O>\n // prettier-ignore\n pipe<T2, T3, T4, T5, T6, T7, T8, O>(o1: PipedOperator<T, T2>, o2: PipedOperator<T2, T3>, o3: PipedOperator<T3, T4>, o4: PipedOperator<T4, T5>, o5: PipedOperator<T5, T6>, o6: PipedOperator<T6, T7>, o7: PipedOperator<T7, T8>, o8: PipedOperator<T8, O>): IStreamBuilder<O>\n // prettier-ignore\n pipe<T2, T3, T4, T5, T6, T7, T8, T9, O>(o1: PipedOperator<T, T2>, o2: PipedOperator<T2, T3>, o3: PipedOperator<T3, T4>, o4: PipedOperator<T4, T5>, o5: PipedOperator<T5, T6>, o6: PipedOperator<T6, T7>, o7: PipedOperator<T7, T8>, o8: PipedOperator<T8, T9>, o9: PipedOperator<T9, O>): IStreamBuilder<O>\n // prettier-ignore\n pipe<T2, T3, T4, T5, T6, T7, T8, T9, T10, O>(o1: PipedOperator<T, T2>, o2: PipedOperator<T2, T3>, o3: PipedOperator<T3, T4>, o4: PipedOperator<T4, T5>, o5: PipedOperator<T5, T6>, o6: PipedOperator<T6, T7>, o7: PipedOperator<T7, T8>, o8: PipedOperator<T8, T9>, o9: PipedOperator<T9, T10>, o10: PipedOperator<T10, O>): IStreamBuilder<O>\n // prettier-ignore\n pipe<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, O>(o1: PipedOperator<T, T2>, o2: PipedOperator<T2, T3>, o3: PipedOperator<T3, T4>, o4: PipedOperator<T4, T5>, o5: PipedOperator<T5, T6>, o6: PipedOperator<T6, T7>, o7: PipedOperator<T7, T8>, o8: PipedOperator<T8, T9>, o9: PipedOperator<T9, T10>, o10: PipedOperator<T10, T11>, o11: PipedOperator<T11, O>): IStreamBuilder<O>\n // prettier-ignore\n pipe<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, O>(o1: PipedOperator<T, T2>, o2: PipedOperator<T2, T3>, o3: PipedOperator<T3, T4>, o4: PipedOperator<T4, T5>, o5: PipedOperator<T5, T6>, o6: PipedOperator<T6, T7>, o7: PipedOperator<T7, T8>, o8: PipedOperator<T8, T9>, o9: PipedOperator<T9, T10>, o10: PipedOperator<T10, T11>, o11: PipedOperator<T11, T12>, o12: PipedOperator<T12, O>): IStreamBuilder<O>\n // prettier-ignore\n pipe<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, O>(o1: PipedOperator<T, T2>, o2: PipedOperator<T2, T3>, o3: PipedOperator<T3, T4>, o4: PipedOperator<T4, T5>, o5: PipedOperator<T5, T6>, o6: PipedOperator<T6, T7>, o7: PipedOperator<T7, T8>, o8: PipedOperator<T8, T9>, o9: PipedOperator<T9, T10>, o10: PipedOperator<T10, T11>, o11: PipedOperator<T11, T12>, o12: PipedOperator<T12, T13>, o13: PipedOperator<T13, O>): IStreamBuilder<O>\n // prettier-ignore\n pipe<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, O>(o1: PipedOperator<T, T2>, o2: PipedOperator<T2, T3>, o3: PipedOperator<T3, T4>, o4: PipedOperator<T4, T5>, o5: PipedOperator<T5, T6>, o6: PipedOperator<T6, T7>, o7: PipedOperator<T7, T8>, o8: PipedOperator<T8, T9>, o9: PipedOperator<T9, T10>, o10: PipedOperator<T10, T11>, o11: PipedOperator<T11, T12>, o12: PipedOperator<T12, T13>, o13: PipedOperator<T13, T14>, o14: PipedOperator<T14, O>): IStreamBuilder<O>\n // prettier-ignore\n pipe<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, O>(o1: PipedOperator<T, T2>, o2: PipedOperator<T2, T3>, o3: PipedOperator<T3, T4>, o4: PipedOperator<T4, T5>, o5: PipedOperator<T5, T6>, o6: PipedOperator<T6, T7>, o7: PipedOperator<T7, T8>, o8: PipedOperator<T8, T9>, o9: PipedOperator<T9, T10>, o10: PipedOperator<T10, T11>, o11: PipedOperator<T11, T12>, o12: PipedOperator<T12, T13>, o13: PipedOperator<T13, T14>, o14: PipedOperator<T14, T15>, o15: PipedOperator<T15, O>): IStreamBuilder<O>\n // prettier-ignore\n pipe<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, O>(o1: PipedOperator<T, T2>, o2: PipedOperator<T2, T3>, o3: PipedOperator<T3, T4>, o4: PipedOperator<T4, T5>, o5: PipedOperator<T5, T6>, o6: PipedOperator<T6, T7>, o7: PipedOperator<T7, T8>, o8: PipedOperator<T8, T9>, o9: PipedOperator<T9, T10>, o10: PipedOperator<T10, T11>, o11: PipedOperator<T11, T12>, o12: PipedOperator<T12, T13>, o13: PipedOperator<T13, T14>, o14: PipedOperator<T14, T15>, o15: PipedOperator<T15, T16>, o16: PipedOperator<T16, O>): IStreamBuilder<O>\n // prettier-ignore\n pipe<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, O>(o1: PipedOperator<T, T2>, o2: PipedOperator<T2, T3>, o3: PipedOperator<T3, T4>, o4: PipedOperator<T4, T5>, o5: PipedOperator<T5, T6>, o6: PipedOperator<T6, T7>, o7: PipedOperator<T7, T8>, o8: PipedOperator<T8, T9>, o9: PipedOperator<T9, T10>, o10: PipedOperator<T10, T11>, o11: PipedOperator<T11, T12>, o12: PipedOperator<T12, T13>, o13: PipedOperator<T13, T14>, o14: PipedOperator<T14, T15>, o15: PipedOperator<T15, T16>, o16: PipedOperator<T16, T17>, o17: PipedOperator<T17, O>): IStreamBuilder<O>\n // prettier-ignore\n pipe<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, O>(o1: PipedOperator<T, T2>, o2: PipedOperator<T2, T3>, o3: PipedOperator<T3, T4>, o4: PipedOperator<T4, T5>, o5: PipedOperator<T5, T6>, o6: PipedOperator<T6, T7>, o7: PipedOperator<T7, T8>, o8: PipedOperator<T8, T9>, o9: PipedOperator<T9, T10>, o10: PipedOperator<T10, T11>, o11: PipedOperator<T11, T12>, o12: PipedOperator<T12, T13>, o13: PipedOperator<T13, T14>, o14: PipedOperator<T14, T15>, o15: PipedOperator<T15, T16>, o16: PipedOperator<T16, T17>, o17: PipedOperator<T17, T18>, o18: PipedOperator<T18, O>): IStreamBuilder<O>\n // prettier-ignore\n pipe<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, O>(o1: PipedOperator<T, T2>, o2: PipedOperator<T2, T3>, o3: PipedOperator<T3, T4>, o4: PipedOperator<T4, T5>, o5: PipedOperator<T5, T6>, o6: PipedOperator<T6, T7>, o7: PipedOperator<T7, T8>, o8: PipedOperator<T8, T9>, o9: PipedOperator<T9, T10>, o10: PipedOperator<T10, T11>, o11: PipedOperator<T11, T12>, o12: PipedOperator<T12, T13>, o13: PipedOperator<T13, T14>, o14: PipedOperator<T14, T15>, o15: PipedOperator<T15, T16>, o16: PipedOperator<T16, T17>, o17: PipedOperator<T17, T18>, o18: PipedOperator<T18, T19>, o19: PipedOperator<T19, O>): IStreamBuilder<O>\n // prettier-ignore\n pipe<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, O>(o1: PipedOperator<T, T2>, o2: PipedOperator<T2, T3>, o3: PipedOperator<T3, T4>, o4: PipedOperator<T4, T5>, o5: PipedOperator<T5, T6>, o6: PipedOperator<T6, T7>, o7: PipedOperator<T7, T8>, o8: PipedOperator<T8, T9>, o9: PipedOperator<T9, T10>, o10: PipedOperator<T10, T11>, o11: PipedOperator<T11, T12>, o12: PipedOperator<T12, T13>, o13: PipedOperator<T13, T14>, o14: PipedOperator<T14, T15>, o15: PipedOperator<T15, T16>, o16: PipedOperator<T16, T17>, o17: PipedOperator<T17, T18>, o18: PipedOperator<T18, T19>, o19: PipedOperator<T19, T20>, o20: PipedOperator<T20, O>): IStreamBuilder<O>\n\n pipe(...operators: Array<PipedOperator<any, any>>): IStreamBuilder<any> {\n return operators.reduce((stream, operator) => {\n return operator(stream)\n }, this as IStreamBuilder<any>)\n }\n}\n\nexport class RootStreamBuilder<T> extends StreamBuilder<T> {\n sendData(collection: MultiSet<T> | MultiSetArray<T>): void {\n this.writer.sendData(collection)\n }\n}\n"],"names":["DifferenceStreamWriter","graph"],"mappings":";;;;;;;;;;;;;;;;;;;;AASO,MAAM,GAAkB;AAAA,EAK7B,cAAc;AALT;AACL,mCAA8D,CAAA;AAC9D,wCAAkB;AAClB,mCAAa;AAAA,EAEE;AAAA,EAQf,oBAA4B;AAC1B,0BAAK,qCAAL;AACA,WAAO,uBAAK,iBAAL;AAAA,EACT;AAAA,EAEA,WAAoC;AAClC,0BAAK,qCAAL;AACA,UAAM,SAAS,IAAIA,6BAAA;AAEnB,UAAM,gBAAgB,IAAI,kBAAq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7 +7,6 @@ export declare class D2 implements ID2 {
7
7
  getNextOperatorId(): number;
8
8
  newInput<T>(): RootStreamBuilder<T>;
9
9
  addOperator(operator: UnaryOperator<any> | BinaryOperator<any>): void;
10
- addStream(stream: DifferenceStreamReader<any>): void;
11
10
  finalize(): void;
12
11
  step(): void;
13
12
  pendingWork(): boolean;
@@ -7,98 +7,228 @@ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read fr
7
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
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
9
  var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
10
- var _valueIndex, _hashIndex, _Index_instances, entries_fn, entriesIterators_fn;
10
+ var _inner, _Index_instances, handleSingleValueTransition_fn;
11
11
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
12
12
  const multiset = require("./multiset.cjs");
13
- const hashIndex = require("./hashIndex.cjs");
14
- const valueIndex = require("./valueIndex.cjs");
15
- const utils = require("./utils.cjs");
13
+ const hash = require("./hashing/hash.cjs");
14
+ const NO_PREFIX = Symbol(`NO_PREFIX`);
15
+ class PrefixMap extends Map {
16
+ /**
17
+ * Add a value to the PrefixMap. Returns true if the map becomes empty after the operation.
18
+ */
19
+ addValue(value, multiplicity) {
20
+ if (multiplicity === 0) return this.size === 0;
21
+ const prefix = getPrefix(value);
22
+ const valueMapOrSingleValue = this.get(prefix);
23
+ if (isSingleValue(valueMapOrSingleValue)) {
24
+ const [currentValue, currentMultiplicity] = valueMapOrSingleValue;
25
+ const currentPrefix = getPrefix(currentValue);
26
+ if (currentPrefix !== prefix) {
27
+ throw new Error(`Mismatching prefixes, this should never happen`);
28
+ }
29
+ if (currentValue === value || hash.hash(currentValue) === hash.hash(value)) {
30
+ const newMultiplicity = currentMultiplicity + multiplicity;
31
+ if (newMultiplicity === 0) {
32
+ this.delete(prefix);
33
+ } else {
34
+ this.set(prefix, [value, newMultiplicity]);
35
+ }
36
+ } else {
37
+ const valueMap = new ValueMap();
38
+ valueMap.set(hash.hash(currentValue), valueMapOrSingleValue);
39
+ valueMap.set(hash.hash(value), [value, multiplicity]);
40
+ this.set(prefix, valueMap);
41
+ }
42
+ } else if (valueMapOrSingleValue === void 0) {
43
+ this.set(prefix, [value, multiplicity]);
44
+ } else {
45
+ const isEmpty = valueMapOrSingleValue.addValue(value, multiplicity);
46
+ if (isEmpty) {
47
+ this.delete(prefix);
48
+ }
49
+ }
50
+ return this.size === 0;
51
+ }
52
+ }
53
+ class ValueMap extends Map {
54
+ /**
55
+ * Add a value to the ValueMap. Returns true if the map becomes empty after the operation.
56
+ * @param value - The full value to store
57
+ * @param multiplicity - The multiplicity to add
58
+ * @param hashKey - Optional hash key to use instead of hashing the full value (used when in PrefixMap context)
59
+ */
60
+ addValue(value, multiplicity) {
61
+ if (multiplicity === 0) return this.size === 0;
62
+ const key = hash.hash(value);
63
+ const currentValue = this.get(key);
64
+ if (currentValue) {
65
+ const [, currentMultiplicity] = currentValue;
66
+ const newMultiplicity = currentMultiplicity + multiplicity;
67
+ if (newMultiplicity === 0) {
68
+ this.delete(key);
69
+ } else {
70
+ this.set(key, [value, newMultiplicity]);
71
+ }
72
+ } else {
73
+ this.set(key, [value, multiplicity]);
74
+ }
75
+ return this.size === 0;
76
+ }
77
+ }
16
78
  class Index {
17
79
  constructor() {
18
80
  __privateAdd(this, _Index_instances);
19
81
  /*
20
- * This is a hybrid Index that composes a ValueIndex and a HashIndex.
21
- * Keys that have only one value are stored in the ValueIndex.
22
- * Keys that have multiple values are stored in the HashIndex, the hash distinguishes between the values.
23
- * This reduces the amount of hashes we need to compute since often times only a small portion of the keys are updated
24
- * so we don't have to hash the keys that are never updated.
25
- *
26
- * Note: The `valueIndex` and `hashIndex` have disjoint keys.
27
- * When a key that has only one value gets a new distinct value,
28
- * it is added to the `hashIndex` and removed from the `valueIndex` and vice versa.
82
+ * This index maintains a nested map of keys -> (value, multiplicities), where:
83
+ * - initially the values are stored against the key as a single value tuple
84
+ * - when a key gets additional values, the values are stored against the key in a
85
+ * prefix map
86
+ * - the prefix is extract where possible from values that are structured as
87
+ * [rowPrimaryKey, rowValue], as they are in the Tanstack DB query pipeline.
88
+ * - only when there are multiple values for a given prefix do we fall back to a
89
+ * hash to identify identical values, storing them in a third level value map.
29
90
  */
30
- __privateAdd(this, _valueIndex);
31
- __privateAdd(this, _hashIndex);
32
- __privateSet(this, _valueIndex, new valueIndex.ValueIndex());
33
- __privateSet(this, _hashIndex, new hashIndex.HashIndex());
91
+ __privateAdd(this, _inner);
92
+ __privateSet(this, _inner, /* @__PURE__ */ new Map());
34
93
  }
94
+ /**
95
+ * This method returns a string representation of the index.
96
+ * @param indent - Whether to indent the string representation.
97
+ * @returns A string representation of the index.
98
+ */
35
99
  toString(indent = false) {
36
- return `Index(
37
- ${__privateGet(this, _valueIndex).toString(indent)},
38
- ${__privateGet(this, _hashIndex).toString(indent)}
39
- )`;
100
+ return `Index(${JSON.stringify(
101
+ [...this.entries()],
102
+ void 0,
103
+ indent ? 2 : void 0
104
+ )})`;
105
+ }
106
+ /**
107
+ * The size of the index.
108
+ */
109
+ get size() {
110
+ return __privateGet(this, _inner).size;
40
111
  }
112
+ /**
113
+ * This method checks if the index has a given key.
114
+ * @param key - The key to check.
115
+ * @returns True if the index has the key, false otherwise.
116
+ */
117
+ has(key) {
118
+ return __privateGet(this, _inner).has(key);
119
+ }
120
+ /**
121
+ * This method returns all values for a given key.
122
+ * @param key - The key to get the values for.
123
+ * @returns An array of value tuples [value, multiplicity].
124
+ */
41
125
  get(key) {
42
- if (__privateGet(this, _valueIndex).has(key)) {
43
- return [__privateGet(this, _valueIndex).get(key)];
44
- }
45
- return __privateGet(this, _hashIndex).get(key);
126
+ return [...this.getIterator(key)];
46
127
  }
47
- getMultiplicity(key, value) {
48
- if (__privateGet(this, _valueIndex).has(key)) {
49
- return __privateGet(this, _valueIndex).getMultiplicity(key);
128
+ /**
129
+ * This method returns an iterator over all values for a given key.
130
+ * @param key - The key to get the values for.
131
+ * @returns An iterator of value tuples [value, multiplicity].
132
+ */
133
+ *getIterator(key) {
134
+ const mapOrSingleValue = __privateGet(this, _inner).get(key);
135
+ if (isSingleValue(mapOrSingleValue)) {
136
+ yield mapOrSingleValue;
137
+ } else if (mapOrSingleValue === void 0) {
138
+ return;
139
+ } else if (mapOrSingleValue instanceof ValueMap) {
140
+ for (const valueTuple of mapOrSingleValue.values()) {
141
+ yield valueTuple;
142
+ }
143
+ } else {
144
+ for (const singleValueOrValueMap of mapOrSingleValue.values()) {
145
+ if (isSingleValue(singleValueOrValueMap)) {
146
+ yield singleValueOrValueMap;
147
+ } else {
148
+ for (const valueTuple of singleValueOrValueMap.values()) {
149
+ yield valueTuple;
150
+ }
151
+ }
152
+ }
50
153
  }
51
- return __privateGet(this, _hashIndex).getMultiplicity(key, value);
52
154
  }
53
- has(key) {
54
- return __privateGet(this, _valueIndex).has(key) || __privateGet(this, _hashIndex).has(key);
155
+ /**
156
+ * This returns an iterator that iterates over all key-value pairs.
157
+ * @returns An iterable of all key-value pairs (and their multiplicities) in the index.
158
+ */
159
+ *entries() {
160
+ for (const key of __privateGet(this, _inner).keys()) {
161
+ for (const valueTuple of this.getIterator(key)) {
162
+ yield [key, valueTuple];
163
+ }
164
+ }
55
165
  }
56
- get size() {
57
- return __privateGet(this, _valueIndex).size + __privateGet(this, _hashIndex).size;
58
- }
59
- addValue(key, value) {
60
- const containedInValueIndex = __privateGet(this, _valueIndex).has(key);
61
- const containedInHashIndex = __privateGet(this, _hashIndex).has(key);
62
- if (containedInHashIndex && containedInValueIndex) {
63
- throw new Error(
64
- `Key ${key} is contained in both the value index and the hash index. This should never happen because they should have disjoint keysets.`
65
- );
166
+ /**
167
+ * This method only iterates over the keys and not over the values.
168
+ * Hence, it is more efficient than the `#entries` method.
169
+ * It returns an iterator that you can use if you need to iterate over the values for a given key.
170
+ * @returns An iterator of all *keys* in the index and their corresponding value iterator.
171
+ */
172
+ *entriesIterators() {
173
+ for (const key of __privateGet(this, _inner).keys()) {
174
+ yield [key, this.getIterator(key)];
66
175
  }
67
- if (!containedInValueIndex && !containedInHashIndex) {
68
- __privateGet(this, _valueIndex).addValue(key, value);
176
+ }
177
+ /**
178
+ * This method adds a value to the index.
179
+ * @param key - The key to add the value to.
180
+ * @param valueTuple - The value tuple [value, multiplicity] to add to the index.
181
+ */
182
+ addValue(key, valueTuple) {
183
+ const [value, multiplicity] = valueTuple;
184
+ if (multiplicity === 0) return;
185
+ const mapOrSingleValue = __privateGet(this, _inner).get(key);
186
+ if (mapOrSingleValue === void 0) {
187
+ __privateGet(this, _inner).set(key, valueTuple);
69
188
  return;
70
189
  }
71
- if (containedInValueIndex) {
72
- try {
73
- __privateGet(this, _valueIndex).addValue(key, value);
74
- } catch {
75
- const existingValue = __privateGet(this, _valueIndex).get(key);
76
- __privateGet(this, _valueIndex).delete(key);
77
- __privateGet(this, _hashIndex).addValue(key, existingValue);
78
- __privateGet(this, _hashIndex).addValue(key, value);
79
- }
190
+ if (isSingleValue(mapOrSingleValue)) {
191
+ __privateMethod(this, _Index_instances, handleSingleValueTransition_fn).call(this, key, mapOrSingleValue, value, multiplicity);
80
192
  return;
81
193
  }
82
- if (containedInHashIndex) {
83
- const singleRemainingValue = __privateGet(this, _hashIndex).addValue(key, value);
84
- if (singleRemainingValue) {
85
- __privateGet(this, _hashIndex).delete(key);
86
- __privateGet(this, _valueIndex).addValue(key, singleRemainingValue);
194
+ if (mapOrSingleValue instanceof ValueMap) {
195
+ const prefix = getPrefix(value);
196
+ if (prefix !== NO_PREFIX) {
197
+ const prefixMap = new PrefixMap();
198
+ prefixMap.set(NO_PREFIX, mapOrSingleValue);
199
+ prefixMap.set(prefix, valueTuple);
200
+ __privateGet(this, _inner).set(key, prefixMap);
201
+ } else {
202
+ const isEmpty = mapOrSingleValue.addValue(value, multiplicity);
203
+ if (isEmpty) {
204
+ __privateGet(this, _inner).delete(key);
205
+ }
206
+ }
207
+ } else {
208
+ const isEmpty = mapOrSingleValue.addValue(value, multiplicity);
209
+ if (isEmpty) {
210
+ __privateGet(this, _inner).delete(key);
87
211
  }
88
- return;
89
212
  }
90
213
  }
214
+ /**
215
+ * This method appends another index to the current index.
216
+ * @param other - The index to append to the current index.
217
+ */
91
218
  append(other) {
92
- var _a;
93
- for (const [key, value] of __privateMethod(_a = other, _Index_instances, entries_fn).call(_a)) {
219
+ for (const [key, value] of other.entries()) {
94
220
  this.addValue(key, value);
95
221
  }
96
222
  }
223
+ /**
224
+ * This method joins two indexes.
225
+ * @param other - The index to join with the current index.
226
+ * @returns A multiset of the joined values.
227
+ */
97
228
  join(other) {
98
- var _a;
99
229
  const result = [];
100
230
  if (this.size <= other.size) {
101
- for (const [key, valueIt] of __privateMethod(this, _Index_instances, entriesIterators_fn).call(this)) {
231
+ for (const [key, valueIt] of this.entriesIterators()) {
102
232
  if (!other.has(key)) continue;
103
233
  const otherValues = other.get(key);
104
234
  for (const [val1, mul1] of valueIt) {
@@ -110,7 +240,7 @@ class Index {
110
240
  }
111
241
  }
112
242
  } else {
113
- for (const [key, otherValueIt] of __privateMethod(_a = other, _Index_instances, entriesIterators_fn).call(_a)) {
243
+ for (const [key, otherValueIt] of other.entriesIterators()) {
114
244
  if (!this.has(key)) continue;
115
245
  const values = this.get(key);
116
246
  for (const [val2, mul2] of otherValueIt) {
@@ -125,32 +255,60 @@ class Index {
125
255
  return new multiset.MultiSet(result);
126
256
  }
127
257
  }
128
- _valueIndex = new WeakMap();
129
- _hashIndex = new WeakMap();
258
+ _inner = new WeakMap();
130
259
  _Index_instances = new WeakSet();
131
260
  /**
132
- * This returns an iterator that iterates over all key-value pairs.
133
- * @returns An iterable of all key-value pairs (and their multiplicities) in the index.
261
+ * Handle the transition from a single value to either a ValueMap or PrefixMap
134
262
  */
135
- entries_fn = function() {
136
- return utils.concatIterable(
137
- __privateGet(this, _valueIndex).entries(),
138
- __privateGet(this, _hashIndex).entriesIterator()
139
- );
140
- };
141
- entriesIterators_fn = function* () {
142
- for (const [key, [value, multiplicity]] of __privateGet(this, _valueIndex).entries()) {
143
- yield [key, /* @__PURE__ */ new Map([[value, multiplicity]])];
144
- }
145
- for (const [key, valueMap] of __privateGet(this, _hashIndex).entries()) {
146
- yield [
147
- key,
148
- utils.mapIterable(valueMap, ([_hash, [value, multiplicity]]) => [
149
- value,
150
- multiplicity
151
- ])
152
- ];
263
+ handleSingleValueTransition_fn = function(key, currentSingleValue, newValue, multiplicity) {
264
+ const [currentValue, currentMultiplicity] = currentSingleValue;
265
+ if (currentValue === newValue) {
266
+ const newMultiplicity = currentMultiplicity + multiplicity;
267
+ if (newMultiplicity === 0) {
268
+ __privateGet(this, _inner).delete(key);
269
+ } else {
270
+ __privateGet(this, _inner).set(key, [newValue, newMultiplicity]);
271
+ }
272
+ return;
273
+ }
274
+ const newPrefix = getPrefix(newValue);
275
+ const currentPrefix = getPrefix(currentValue);
276
+ if (currentPrefix === newPrefix && (currentValue === newValue || hash.hash(currentValue) === hash.hash(newValue))) {
277
+ const newMultiplicity = currentMultiplicity + multiplicity;
278
+ if (newMultiplicity === 0) {
279
+ __privateGet(this, _inner).delete(key);
280
+ } else {
281
+ __privateGet(this, _inner).set(key, [newValue, newMultiplicity]);
282
+ }
283
+ return;
284
+ }
285
+ if (currentPrefix === NO_PREFIX && newPrefix === NO_PREFIX) {
286
+ const valueMap = new ValueMap();
287
+ valueMap.set(hash.hash(currentValue), currentSingleValue);
288
+ valueMap.set(hash.hash(newValue), [newValue, multiplicity]);
289
+ __privateGet(this, _inner).set(key, valueMap);
290
+ } else {
291
+ const prefixMap = new PrefixMap();
292
+ if (currentPrefix === newPrefix) {
293
+ const valueMap = new ValueMap();
294
+ valueMap.set(hash.hash(currentValue), currentSingleValue);
295
+ valueMap.set(hash.hash(newValue), [newValue, multiplicity]);
296
+ prefixMap.set(currentPrefix, valueMap);
297
+ } else {
298
+ prefixMap.set(currentPrefix, currentSingleValue);
299
+ prefixMap.set(newPrefix, [newValue, multiplicity]);
300
+ }
301
+ __privateGet(this, _inner).set(key, prefixMap);
153
302
  }
154
303
  };
304
+ function getPrefix(value) {
305
+ if (Array.isArray(value) && (typeof value[0] === `string` || typeof value[0] === `number` || typeof value[0] === `bigint`)) {
306
+ return value[0];
307
+ }
308
+ return NO_PREFIX;
309
+ }
310
+ function isSingleValue(value) {
311
+ return Array.isArray(value);
312
+ }
155
313
  exports.Index = Index;
156
314
  //# sourceMappingURL=indexes.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"indexes.cjs","sources":["../../src/indexes.ts"],"sourcesContent":["import { MultiSet } from \"./multiset.js\"\nimport { HashIndex } from \"./hashIndex.js\"\nimport { ValueIndex } from \"./valueIndex.js\"\nimport { concatIterable, mapIterable } from \"./utils.js\"\n\n/**\n * A map from a difference collection trace's keys -> (value, multiplicities) that changed.\n * Used in operations like join and reduce where the operation needs to\n * exploit the key-value structure of the data to run efficiently.\n */\nexport class Index<K, V> {\n /*\n * This is a hybrid Index that composes a ValueIndex and a HashIndex.\n * Keys that have only one value are stored in the ValueIndex.\n * Keys that have multiple values are stored in the HashIndex, the hash distinguishes between the values.\n * This reduces the amount of hashes we need to compute since often times only a small portion of the keys are updated\n * so we don't have to hash the keys that are never updated.\n *\n * Note: The `valueIndex` and `hashIndex` have disjoint keys.\n * When a key that has only one value gets a new distinct value,\n * it is added to the `hashIndex` and removed from the `valueIndex` and vice versa.\n */\n #valueIndex: ValueIndex<K, V>\n #hashIndex: HashIndex<K, V>\n\n constructor() {\n this.#valueIndex = new ValueIndex<K, V>()\n this.#hashIndex = new HashIndex<K, V>()\n }\n\n toString(indent = false): string {\n return `Index(\\n ${this.#valueIndex.toString(indent)},\\n ${this.#hashIndex.toString(indent)}\\n)`\n }\n\n get(key: K): Array<[V, number]> {\n if (this.#valueIndex.has(key)) {\n return [this.#valueIndex.get(key)!]\n }\n return this.#hashIndex.get(key)\n }\n\n getMultiplicity(key: K, value: V): number {\n if (this.#valueIndex.has(key)) {\n return this.#valueIndex.getMultiplicity(key)\n }\n return this.#hashIndex.getMultiplicity(key, value)\n }\n\n /**\n * This returns an iterator that iterates over all key-value pairs.\n * @returns An iterable of all key-value pairs (and their multiplicities) in the index.\n */\n #entries(): Iterable<[K, [V, number]]> {\n return concatIterable(\n this.#valueIndex.entries(),\n this.#hashIndex.entriesIterator()\n )\n }\n\n /**\n * This method only iterates over the keys and not over the values.\n * Hence, it is more efficient than the `#entries` method.\n * It returns an iterator that you can use if you need to iterate over the values for a given key.\n * @returns An iterator of all *keys* in the index and their corresponding value iterator.\n */\n *#entriesIterators(): Iterable<[K, Iterable<[V, number]>]> {\n for (const [key, [value, multiplicity]] of this.#valueIndex.entries()) {\n yield [key, new Map<V, number>([[value, multiplicity]])]\n }\n for (const [key, valueMap] of this.#hashIndex.entries()) {\n yield [\n key,\n mapIterable(valueMap, ([_hash, [value, multiplicity]]) => [\n value,\n multiplicity,\n ]),\n ]\n }\n }\n\n has(key: K): boolean {\n return this.#valueIndex.has(key) || this.#hashIndex.has(key)\n }\n\n get size(): number {\n return this.#valueIndex.size + this.#hashIndex.size\n }\n\n addValue(key: K, value: [V, number]): void {\n const containedInValueIndex = this.#valueIndex.has(key)\n const containedInHashIndex = this.#hashIndex.has(key)\n\n if (containedInHashIndex && containedInValueIndex) {\n throw new Error(\n `Key ${key} is contained in both the value index and the hash index. This should never happen because they should have disjoint keysets.`\n )\n }\n\n if (!containedInValueIndex && !containedInHashIndex) {\n // This is the first time we see the key\n // Add it to the value index\n this.#valueIndex.addValue(key, value)\n return\n }\n\n if (containedInValueIndex) {\n // This key is already in the value index\n // It could be that it's the same value or a different one\n // If it's a different value we will need to remove the key from the value index\n // and add the key and its two values to the hash index\n try {\n this.#valueIndex.addValue(key, value)\n } catch {\n // This is a different value, need to move the key to the hash index\n const existingValue = this.#valueIndex.get(key)!\n this.#valueIndex.delete(key)\n this.#hashIndex.addValue(key, existingValue)\n this.#hashIndex.addValue(key, value)\n }\n return\n }\n\n if (containedInHashIndex) {\n // This key is already in the hash index so it already has two or more values.\n // However, this new value and multiplicity could cause an existing value to be removed\n // and lead to the key having only a single value in which case we need to move it back to the value index\n const singleRemainingValue = this.#hashIndex.addValue(key, value)\n if (singleRemainingValue) {\n // The key only has a single remaining value so we need to move it back to the value index\n this.#hashIndex.delete(key)\n this.#valueIndex.addValue(key, singleRemainingValue)\n }\n return\n }\n }\n\n append(other: Index<K, V>): void {\n for (const [key, value] of other.#entries()) {\n this.addValue(key, value)\n }\n }\n\n join<V2>(other: Index<K, V2>): MultiSet<[K, [V, V2]]> {\n const result: Array<[[K, [V, V2]], number]> = []\n\n // We want to iterate over the smaller of the two indexes to reduce the\n // number of operations we need to do.\n if (this.size <= other.size) {\n for (const [key, valueIt] of this.#entriesIterators()) {\n if (!other.has(key)) continue\n const otherValues = other.get(key)\n for (const [val1, mul1] of valueIt) {\n for (const [val2, mul2] of otherValues) {\n if (mul1 !== 0 && mul2 !== 0) {\n result.push([[key, [val1, val2]], mul1 * mul2])\n }\n }\n }\n }\n } else {\n for (const [key, otherValueIt] of other.#entriesIterators()) {\n if (!this.has(key)) continue\n const values = this.get(key)\n for (const [val2, mul2] of otherValueIt) {\n for (const [val1, mul1] of values) {\n if (mul1 !== 0 && mul2 !== 0) {\n result.push([[key, [val1, val2]], mul1 * mul2])\n }\n }\n }\n }\n }\n\n return new MultiSet(result)\n }\n}\n"],"names":["ValueIndex","HashIndex","MultiSet","concatIterable","mapIterable"],"mappings":";;;;;;;;;;;;;;;AAUO,MAAM,MAAY;AAAA,EAevB,cAAc;AAfT;AAYL;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA;AAGE,uBAAK,aAAc,IAAIA,sBAAA;AACvB,uBAAK,YAAa,IAAIC,oBAAA;AAAA,EACxB;AAAA,EAEA,SAAS,SAAS,OAAe;AAC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YAChD;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAEA,WAAO,IAAIC,SAAAA,SAAS,MAAM;AAAA,EAC5B;AACF;AAzJE;AACA;AAbK;AAAA;AAAA;AAAA;AAAA;AA0CL,aAAA,WAAuC;AACrC,SAAOC,MAAAA;AAAAA,IACL,mBAAK,aAAY,QAAA;AAAA,IACjB,mBAAK,YAAW,gBAAA;AAAA,EAAgB;AAEpC;AAQC,sBAAA,aAA0D;AACzD,aAAW,CAAC,KAAK,CAAC,OAAO,YAAY,CAAC,KAAK,mBAAK,aAAY,WAAW;AACrE,UAAM,CAAC,KAAK,oBAAI,IAAe,CAAC,CAAC,OAAO,YAAY,CAAC,CAAC,CAAC;AAAA,EACzD;AACA,aAAW,CAAC,KAAK,QAAQ,KAAK,mBAAK,YAAW,WAAW;AACvD,UAAM;AAAA,MACJ;AAAA,MACAC,MAAAA,YAAY,UAAU,CAAC,CAAC,OAAO,CAAC,OAAO,YAAY,CAAC,MAAM;AAAA,QACxD;AAAA,QACA;AAAA,MAAA,CACD;AAAA,IAAA;AAAA,EAEL;AACF;;"}
1
+ {"version":3,"file":"indexes.cjs","sources":["../../src/indexes.ts"],"sourcesContent":["/**\n * # Optimized Index Data Structure\n *\n * Multi-level index that adapts storage strategy based on data patterns to minimize memory\n * usage, eliminate wasteful lookups, and avoid hashing whenever possible.\n *\n * ## Storage Strategy\n *\n * **Single value**: `IndexMap['key'] → [value, multiplicity]` (no hashing needed)\n *\n * **Multiple unprefixed values**: Direct ValueMap (avoids NO_PREFIX lookup)\n * ```\n * IndexMap['key'] → ValueMap { hash(value1) → [value1, mult1], ... }\n * ```\n *\n * **Values with prefixes**: PrefixMap uses prefix keys directly (no hashing)\n * ```\n * IndexMap['key'] → PrefixMap { 'prefix1' → [value1, mult1], NO_PREFIX → ValueMap{...} }\n * ```\n *\n * **Multiple values per prefix**: ValueMap within PrefixMap (hash only suffixes)\n * ```\n * PrefixMap['prefix'] → ValueMap { hash(suffix1) → [full_value1, mult1], ... }\n * ```\n *\n * ## Dynamic Evolution\n *\n * Structure automatically evolves as data is added:\n * - Single → ValueMap (when both values unprefixed)\n * - Single → PrefixMap (when at least one prefixed)\n * - ValueMap → PrefixMap (adding prefixed value to unprefixed)\n *\n * Prefixes extracted from array values: `['prefix', 'suffix']` → prefix='prefix'\n */\n\nimport { MultiSet } from \"./multiset.js\"\nimport { hash } from \"./hashing/index.js\"\nimport type { Hash } from \"./hashing/index.js\"\n\n// We use a symbol to represent the absence of a prefix, unprefixed values a stored\n// against this key.\nconst NO_PREFIX = Symbol(`NO_PREFIX`)\ntype NO_PREFIX = typeof NO_PREFIX\n\n// A single value is a tuple of the value and the multiplicity.\ntype SingleValue<TValue> = [TValue, number]\n\n// Base map type for the index. Stores single values, prefix maps, or value maps against a key.\ntype IndexMap<TKey, TValue, TPrefix> = Map<\n TKey,\n SingleValue<TValue> | PrefixMap<TValue, TPrefix> | ValueMap<TValue>\n>\n\n// Second level map type for the index, stores single values or value maps against a prefix.\nclass PrefixMap<TValue, TPrefix> extends Map<\n TPrefix | NO_PREFIX,\n SingleValue<TValue> | ValueMap<TValue>\n> {\n /**\n * Add a value to the PrefixMap. Returns true if the map becomes empty after the operation.\n */\n addValue(value: TValue, multiplicity: number): boolean {\n if (multiplicity === 0) return this.size === 0\n\n const prefix = getPrefix<TValue, TPrefix>(value)\n const valueMapOrSingleValue = this.get(prefix)\n\n if (isSingleValue(valueMapOrSingleValue)) {\n const [currentValue, currentMultiplicity] = valueMapOrSingleValue\n const currentPrefix = getPrefix<TValue, TPrefix>(currentValue)\n\n if (currentPrefix !== prefix) {\n throw new Error(`Mismatching prefixes, this should never happen`)\n }\n\n if (currentValue === value || hash(currentValue) === hash(value)) {\n // Same value, update multiplicity\n const newMultiplicity = currentMultiplicity + multiplicity\n if (newMultiplicity === 0) {\n this.delete(prefix)\n } else {\n this.set(prefix, [value, newMultiplicity])\n }\n } else {\n // Different suffixes, need to create ValueMap\n const valueMap = new ValueMap<TValue>()\n valueMap.set(hash(currentValue), valueMapOrSingleValue)\n valueMap.set(hash(value), [value, multiplicity])\n this.set(prefix, valueMap)\n }\n } else if (valueMapOrSingleValue === undefined) {\n // No existing value for this prefix\n this.set(prefix, [value, multiplicity])\n } else {\n // Existing ValueMap\n const isEmpty = valueMapOrSingleValue.addValue(value, multiplicity)\n if (isEmpty) {\n this.delete(prefix)\n }\n }\n\n return this.size === 0\n }\n}\n\n// Third level map type for the index, stores single values or value maps against a hash.\nclass ValueMap<TValue> extends Map<Hash, [TValue, number]> {\n /**\n * Add a value to the ValueMap. Returns true if the map becomes empty after the operation.\n * @param value - The full value to store\n * @param multiplicity - The multiplicity to add\n * @param hashKey - Optional hash key to use instead of hashing the full value (used when in PrefixMap context)\n */\n addValue(value: TValue, multiplicity: number): boolean {\n if (multiplicity === 0) return this.size === 0\n\n const key = hash(value)\n const currentValue = this.get(key)\n\n if (currentValue) {\n const [, currentMultiplicity] = currentValue\n const newMultiplicity = currentMultiplicity + multiplicity\n if (newMultiplicity === 0) {\n this.delete(key)\n } else {\n this.set(key, [value, newMultiplicity])\n }\n } else {\n this.set(key, [value, multiplicity])\n }\n\n return this.size === 0\n }\n}\n\n/**\n * A map from a difference collection trace's keys -> (value, multiplicities) that changed.\n * Used in operations like join and reduce where the operation needs to\n * exploit the key-value structure of the data to run efficiently.\n */\nexport class Index<TKey, TValue, TPrefix = any> {\n /*\n * This index maintains a nested map of keys -> (value, multiplicities), where:\n * - initially the values are stored against the key as a single value tuple\n * - when a key gets additional values, the values are stored against the key in a\n * prefix map\n * - the prefix is extract where possible from values that are structured as\n * [rowPrimaryKey, rowValue], as they are in the Tanstack DB query pipeline.\n * - only when there are multiple values for a given prefix do we fall back to a\n * hash to identify identical values, storing them in a third level value map.\n */\n #inner: IndexMap<TKey, TValue, TPrefix>\n\n constructor() {\n this.#inner = new Map()\n }\n\n /**\n * This method returns a string representation of the index.\n * @param indent - Whether to indent the string representation.\n * @returns A string representation of the index.\n */\n toString(indent = false): string {\n return `Index(${JSON.stringify(\n [...this.entries()],\n undefined,\n indent ? 2 : undefined\n )})`\n }\n\n /**\n * The size of the index.\n */\n get size(): number {\n return this.#inner.size\n }\n\n /**\n * This method checks if the index has a given key.\n * @param key - The key to check.\n * @returns True if the index has the key, false otherwise.\n */\n has(key: TKey): boolean {\n return this.#inner.has(key)\n }\n\n /**\n * This method returns all values for a given key.\n * @param key - The key to get the values for.\n * @returns An array of value tuples [value, multiplicity].\n */\n get(key: TKey): Array<[TValue, number]> {\n return [...this.getIterator(key)]\n }\n\n /**\n * This method returns an iterator over all values for a given key.\n * @param key - The key to get the values for.\n * @returns An iterator of value tuples [value, multiplicity].\n */\n *getIterator(key: TKey): Iterable<[TValue, number]> {\n const mapOrSingleValue = this.#inner.get(key)\n if (isSingleValue(mapOrSingleValue)) {\n yield mapOrSingleValue\n } else if (mapOrSingleValue === undefined) {\n return\n } else if (mapOrSingleValue instanceof ValueMap) {\n // Direct ValueMap - all values have NO_PREFIX\n for (const valueTuple of mapOrSingleValue.values()) {\n yield valueTuple\n }\n } else {\n // PrefixMap - iterate through all prefixes\n for (const singleValueOrValueMap of mapOrSingleValue.values()) {\n if (isSingleValue(singleValueOrValueMap)) {\n yield singleValueOrValueMap\n } else {\n for (const valueTuple of singleValueOrValueMap.values()) {\n yield valueTuple\n }\n }\n }\n }\n }\n\n /**\n * This returns an iterator that iterates over all key-value pairs.\n * @returns An iterable of all key-value pairs (and their multiplicities) in the index.\n */\n *entries(): Iterable<[TKey, [TValue, number]]> {\n for (const key of this.#inner.keys()) {\n for (const valueTuple of this.getIterator(key)) {\n yield [key, valueTuple]\n }\n }\n }\n\n /**\n * This method only iterates over the keys and not over the values.\n * Hence, it is more efficient than the `#entries` method.\n * It returns an iterator that you can use if you need to iterate over the values for a given key.\n * @returns An iterator of all *keys* in the index and their corresponding value iterator.\n */\n *entriesIterators(): Iterable<[TKey, Iterable<[TValue, number]>]> {\n for (const key of this.#inner.keys()) {\n yield [key, this.getIterator(key)]\n }\n }\n\n /**\n * This method adds a value to the index.\n * @param key - The key to add the value to.\n * @param valueTuple - The value tuple [value, multiplicity] to add to the index.\n */\n addValue(key: TKey, valueTuple: SingleValue<TValue>) {\n const [value, multiplicity] = valueTuple\n // If the multiplicity is 0, do nothing\n if (multiplicity === 0) return\n\n const mapOrSingleValue = this.#inner.get(key)\n\n if (mapOrSingleValue === undefined) {\n // First value for this key\n this.#inner.set(key, valueTuple)\n return\n }\n\n if (isSingleValue(mapOrSingleValue)) {\n // Handle transition from single value to map\n this.#handleSingleValueTransition(\n key,\n mapOrSingleValue,\n value,\n multiplicity\n )\n return\n }\n\n if (mapOrSingleValue instanceof ValueMap) {\n // Handle existing ValueMap\n const prefix = getPrefix<TValue, TPrefix>(value)\n if (prefix !== NO_PREFIX) {\n // Convert ValueMap to PrefixMap since we have a prefixed value\n const prefixMap = new PrefixMap<TValue, TPrefix>()\n prefixMap.set(NO_PREFIX, mapOrSingleValue)\n prefixMap.set(prefix, valueTuple)\n this.#inner.set(key, prefixMap)\n } else {\n // Add to existing ValueMap\n const isEmpty = mapOrSingleValue.addValue(value, multiplicity)\n if (isEmpty) {\n this.#inner.delete(key)\n }\n }\n } else {\n // Handle existing PrefixMap\n const isEmpty = mapOrSingleValue.addValue(value, multiplicity)\n if (isEmpty) {\n this.#inner.delete(key)\n }\n }\n }\n\n /**\n * Handle the transition from a single value to either a ValueMap or PrefixMap\n */\n #handleSingleValueTransition(\n key: TKey,\n currentSingleValue: SingleValue<TValue>,\n newValue: TValue,\n multiplicity: number\n ) {\n const [currentValue, currentMultiplicity] = currentSingleValue\n\n // Check for exact same value (reference equality)\n if (currentValue === newValue) {\n const newMultiplicity = currentMultiplicity + multiplicity\n if (newMultiplicity === 0) {\n this.#inner.delete(key)\n } else {\n this.#inner.set(key, [newValue, newMultiplicity])\n }\n return\n }\n\n // Get prefixes for both values\n const newPrefix = getPrefix<TValue, TPrefix>(newValue)\n const currentPrefix = getPrefix<TValue, TPrefix>(currentValue)\n\n // Check if they're the same value by prefix/suffix comparison\n if (\n currentPrefix === newPrefix &&\n (currentValue === newValue || hash(currentValue) === hash(newValue))\n ) {\n const newMultiplicity = currentMultiplicity + multiplicity\n if (newMultiplicity === 0) {\n this.#inner.delete(key)\n } else {\n this.#inner.set(key, [newValue, newMultiplicity])\n }\n return\n }\n\n // Different values - choose appropriate map type\n if (currentPrefix === NO_PREFIX && newPrefix === NO_PREFIX) {\n // Both have NO_PREFIX, use ValueMap directly\n const valueMap = new ValueMap<TValue>()\n valueMap.set(hash(currentValue), currentSingleValue)\n valueMap.set(hash(newValue), [newValue, multiplicity])\n this.#inner.set(key, valueMap)\n } else {\n // At least one has a prefix, use PrefixMap\n const prefixMap = new PrefixMap<TValue, TPrefix>()\n\n if (currentPrefix === newPrefix) {\n // Same prefix, different suffixes - need ValueMap within PrefixMap\n const valueMap = new ValueMap<TValue>()\n valueMap.set(hash(currentValue), currentSingleValue)\n valueMap.set(hash(newValue), [newValue, multiplicity])\n prefixMap.set(currentPrefix, valueMap)\n } else {\n // Different prefixes - store as separate single values\n prefixMap.set(currentPrefix, currentSingleValue)\n prefixMap.set(newPrefix, [newValue, multiplicity])\n }\n\n this.#inner.set(key, prefixMap)\n }\n }\n\n /**\n * This method appends another index to the current index.\n * @param other - The index to append to the current index.\n */\n append(other: Index<TKey, TValue>): void {\n for (const [key, value] of other.entries()) {\n this.addValue(key, value)\n }\n }\n\n /**\n * This method joins two indexes.\n * @param other - The index to join with the current index.\n * @returns A multiset of the joined values.\n */\n join<TValue2>(\n other: Index<TKey, TValue2>\n ): MultiSet<[TKey, [TValue, TValue2]]> {\n const result: Array<[[TKey, [TValue, TValue2]], number]> = []\n // We want to iterate over the smaller of the two indexes to reduce the\n // number of operations we need to do.\n if (this.size <= other.size) {\n for (const [key, valueIt] of this.entriesIterators()) {\n if (!other.has(key)) continue\n const otherValues = other.get(key)\n for (const [val1, mul1] of valueIt) {\n for (const [val2, mul2] of otherValues) {\n if (mul1 !== 0 && mul2 !== 0) {\n result.push([[key, [val1, val2]], mul1 * mul2])\n }\n }\n }\n }\n } else {\n for (const [key, otherValueIt] of other.entriesIterators()) {\n if (!this.has(key)) continue\n const values = this.get(key)\n for (const [val2, mul2] of otherValueIt) {\n for (const [val1, mul1] of values) {\n if (mul1 !== 0 && mul2 !== 0) {\n result.push([[key, [val1, val2]], mul1 * mul2])\n }\n }\n }\n }\n }\n\n return new MultiSet(result)\n }\n}\n\n/**\n * This function extracts the prefix from a value.\n * @param value - The value to extract the prefix from.\n * @returns The prefix and the suffix.\n */\nfunction getPrefix<TValue, TPrefix>(value: TValue): TPrefix | NO_PREFIX {\n // If the value is an array and the first element is a string or number, then the\n // first element is the prefix. This is used to distinguish between values without\n // the need for hashing unless there are multiple values for the same prefix.\n if (\n Array.isArray(value) &&\n (typeof value[0] === `string` ||\n typeof value[0] === `number` ||\n typeof value[0] === `bigint`)\n ) {\n return value[0] as TPrefix\n }\n return NO_PREFIX\n}\n\n/**\n * This function checks if a value is a single value.\n * @param value - The value to check.\n * @returns True if the value is a single value, false otherwise.\n */\nfunction isSingleValue<TValue>(\n value: SingleValue<TValue> | unknown\n): value is SingleValue<TValue> {\n return Array.isArray(value)\n}\n"],"names":["hash","MultiSet"],"mappings":";;;;;;;;;;;;;AAyCA,MAAM,YAAY,OAAO,WAAW;AAapC,MAAM,kBAAmC,IAGvC;AAAA;AAAA;AAAA;AAAA,EAIA,SAAS,OAAe,cAA+B;AACrD,QAAI,iBAAiB,EAAG,QAAO,KAAK,SAAS;AAE7C,UAAM,SAAS,UAA2B,KAAK;AAC/C,UAAM,wBAAwB,KAAK,IAAI,MAAM;AAE7C,QAAI,cAAc,qBAAqB,GAAG;AACxC,YAAM,CAAC,cAAc,mBAAmB,IAAI;AAC5C,YAAM,gBAAgB,UAA2B,YAAY;AAE7D,UAAI,kBAAkB,QAAQ;AAC5B,cAAM,IAAI,MAAM,gDAAgD;AAAA,MAClE;AAEA,UAAI,iBAAiB,SAASA,KAAAA,KAAK,YA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