@tanstack/db-ivm 0.1.1 → 0.1.3

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 (92) hide show
  1. package/dist/cjs/hashIndex.cjs +83 -0
  2. package/dist/cjs/hashIndex.cjs.map +1 -0
  3. package/dist/cjs/hashIndex.d.cts +25 -0
  4. package/dist/cjs/hashing/hash.cjs +122 -0
  5. package/dist/cjs/hashing/hash.cjs.map +1 -0
  6. package/dist/cjs/hashing/hash.d.cts +1 -0
  7. package/dist/cjs/hashing/index.d.cts +2 -0
  8. package/dist/cjs/hashing/murmur.cjs +103 -0
  9. package/dist/cjs/hashing/murmur.cjs.map +1 -0
  10. package/dist/cjs/hashing/murmur.d.cts +26 -0
  11. package/dist/cjs/index.cjs +3 -0
  12. package/dist/cjs/index.cjs.map +1 -1
  13. package/dist/cjs/indexes.cjs +97 -45
  14. package/dist/cjs/indexes.cjs.map +1 -1
  15. package/dist/cjs/indexes.d.cts +0 -2
  16. package/dist/cjs/multiset.cjs +2 -1
  17. package/dist/cjs/multiset.cjs.map +1 -1
  18. package/dist/cjs/operators/distinct.cjs +2 -2
  19. package/dist/cjs/operators/distinct.cjs.map +1 -1
  20. package/dist/cjs/operators/groupBy.cjs +5 -4
  21. package/dist/cjs/operators/groupBy.cjs.map +1 -1
  22. package/dist/cjs/operators/groupBy.d.cts +1 -1
  23. package/dist/cjs/operators/index.d.cts +1 -0
  24. package/dist/cjs/operators/orderBy.cjs +3 -1
  25. package/dist/cjs/operators/orderBy.cjs.map +1 -1
  26. package/dist/cjs/operators/orderBy.d.cts +6 -2
  27. package/dist/cjs/operators/tap.cjs +46 -0
  28. package/dist/cjs/operators/tap.cjs.map +1 -0
  29. package/dist/cjs/operators/tap.d.cts +18 -0
  30. package/dist/cjs/operators/topKWithFractionalIndex.cjs +13 -3
  31. package/dist/cjs/operators/topKWithFractionalIndex.cjs.map +1 -1
  32. package/dist/cjs/operators/topKWithFractionalIndex.d.cts +2 -0
  33. package/dist/cjs/utils.cjs +13 -33
  34. package/dist/cjs/utils.cjs.map +1 -1
  35. package/dist/cjs/utils.d.cts +7 -4
  36. package/dist/cjs/valueIndex.cjs +74 -0
  37. package/dist/cjs/valueIndex.cjs.map +1 -0
  38. package/dist/cjs/valueIndex.d.cts +17 -0
  39. package/dist/esm/hashIndex.d.ts +25 -0
  40. package/dist/esm/hashIndex.js +83 -0
  41. package/dist/esm/hashIndex.js.map +1 -0
  42. package/dist/esm/hashing/hash.d.ts +1 -0
  43. package/dist/esm/hashing/hash.js +122 -0
  44. package/dist/esm/hashing/hash.js.map +1 -0
  45. package/dist/esm/hashing/index.d.ts +2 -0
  46. package/dist/esm/hashing/murmur.d.ts +26 -0
  47. package/dist/esm/hashing/murmur.js +103 -0
  48. package/dist/esm/hashing/murmur.js.map +1 -0
  49. package/dist/esm/index.js +3 -0
  50. package/dist/esm/index.js.map +1 -1
  51. package/dist/esm/indexes.d.ts +0 -2
  52. package/dist/esm/indexes.js +98 -46
  53. package/dist/esm/indexes.js.map +1 -1
  54. package/dist/esm/multiset.js +2 -1
  55. package/dist/esm/multiset.js.map +1 -1
  56. package/dist/esm/operators/distinct.js +1 -1
  57. package/dist/esm/operators/distinct.js.map +1 -1
  58. package/dist/esm/operators/groupBy.d.ts +1 -1
  59. package/dist/esm/operators/groupBy.js +5 -4
  60. package/dist/esm/operators/groupBy.js.map +1 -1
  61. package/dist/esm/operators/index.d.ts +1 -0
  62. package/dist/esm/operators/orderBy.d.ts +6 -2
  63. package/dist/esm/operators/orderBy.js +3 -1
  64. package/dist/esm/operators/orderBy.js.map +1 -1
  65. package/dist/esm/operators/tap.d.ts +18 -0
  66. package/dist/esm/operators/tap.js +46 -0
  67. package/dist/esm/operators/tap.js.map +1 -0
  68. package/dist/esm/operators/topKWithFractionalIndex.d.ts +2 -0
  69. package/dist/esm/operators/topKWithFractionalIndex.js +13 -3
  70. package/dist/esm/operators/topKWithFractionalIndex.js.map +1 -1
  71. package/dist/esm/utils.d.ts +7 -4
  72. package/dist/esm/utils.js +13 -33
  73. package/dist/esm/utils.js.map +1 -1
  74. package/dist/esm/valueIndex.d.ts +17 -0
  75. package/dist/esm/valueIndex.js +74 -0
  76. package/dist/esm/valueIndex.js.map +1 -0
  77. package/package.json +1 -3
  78. package/src/hashIndex.ts +94 -0
  79. package/src/hashing/hash.ts +157 -0
  80. package/src/hashing/index.ts +2 -0
  81. package/src/hashing/murmur.ts +128 -0
  82. package/src/indexes.ts +106 -47
  83. package/src/multiset.ts +1 -1
  84. package/src/operators/distinct.ts +4 -4
  85. package/src/operators/groupBy.ts +7 -4
  86. package/src/operators/index.ts +1 -0
  87. package/src/operators/orderBy.ts +8 -2
  88. package/src/operators/tap.ts +53 -0
  89. package/src/operators/topKWithFractionalIndex.ts +14 -2
  90. package/src/operators/topKWithFractionalIndexBTree.ts +7 -0
  91. package/src/utils.ts +31 -48
  92. package/src/valueIndex.ts +78 -0
@@ -1 +1 @@
1
- {"version":3,"file":"topKWithFractionalIndex.cjs","sources":["../../../src/operators/topKWithFractionalIndex.ts"],"sourcesContent":["import { generateKeyBetween } from \"fractional-indexing\"\nimport { DifferenceStreamWriter, UnaryOperator } from \"../graph.js\"\nimport { StreamBuilder } from \"../d2.js\"\nimport { MultiSet } from \"../multiset.js\"\nimport { binarySearch, globalObjectIdGenerator } from \"../utils.js\"\nimport type { DifferenceStreamReader } from \"../graph.js\"\nimport type { IStreamBuilder, PipedOperator } from \"../types.js\"\n\nexport interface TopKWithFractionalIndexOptions {\n limit?: number\n offset?: number\n}\n\nexport type TopKChanges<V> = {\n /** Indicates which element moves into the topK (if any) */\n moveIn: IndexedValue<V> | null\n /** Indicates which element moves out of the topK (if any) */\n moveOut: IndexedValue<V> | null\n}\n\n/**\n * A topK data structure that supports insertions and deletions\n * and returns changes to the topK.\n */\nexport interface TopK<V> {\n insert: (value: V) => TopKChanges<V>\n delete: (value: V) => TopKChanges<V>\n}\n\n/**\n * Implementation of a topK data structure.\n * Uses a sorted array internally to store the values and keeps a topK window over that array.\n * Inserts and deletes are O(n) operations because worst case an element is inserted/deleted\n * at the start of the array which causes all the elements to shift to the right/left.\n */\nclass TopKArray<V> implements TopK<V> {\n #sortedValues: Array<IndexedValue<V>> = []\n #comparator: (a: V, b: V) => number\n #topKStart: number\n #topKEnd: number\n\n constructor(\n offset: number,\n limit: number,\n comparator: (a: V, b: V) => number\n ) {\n this.#topKStart = offset\n this.#topKEnd = offset + limit\n this.#comparator = comparator\n }\n\n insert(value: V): TopKChanges<V> {\n const result: TopKChanges<V> = { moveIn: null, moveOut: null }\n\n // Lookup insert position\n const index = this.#findIndex(value)\n // Generate fractional index based on the fractional indices of the elements before and after it\n const indexBefore =\n index === 0 ? null : getIndex(this.#sortedValues[index - 1]!)\n const indexAfter =\n index === this.#sortedValues.length\n ? null\n : getIndex(this.#sortedValues[index]!)\n const fractionalIndex = generateKeyBetween(indexBefore, indexAfter)\n\n // Insert the value at the correct position\n const val = indexedValue(value, fractionalIndex)\n // Splice is O(n) where n = all elements in the collection (i.e. n >= k) !\n this.#sortedValues.splice(index, 0, val)\n\n // Check if the topK changed\n if (index < this.#topKEnd) {\n // The inserted element is either before the top K or within the top K\n // If it is before the top K then it moves the element that was right before the topK into the topK\n // If it is within the top K then the inserted element moves into the top K\n // In both cases the last element of the old top K now moves out of the top K\n const moveInIndex = Math.max(index, this.#topKStart)\n if (moveInIndex < this.#sortedValues.length) {\n // We actually have a topK\n // because in some cases there may not be enough elements in the array to reach the start of the topK\n // e.g. [1, 2, 3] with K = 2 and offset = 3 does not have a topK\n result.moveIn = this.#sortedValues[moveInIndex]!\n\n // We need to remove the element that falls out of the top K\n // The element that falls out of the top K has shifted one to the right\n // because of the element we inserted, so we find it at index topKEnd\n if (this.#topKEnd < this.#sortedValues.length) {\n result.moveOut = this.#sortedValues[this.#topKEnd]!\n }\n }\n }\n\n return result\n }\n\n /**\n * Deletes a value that may or may not be in the topK.\n * IMPORTANT: this assumes that the value is present in the collection\n * if it's not the case it will remove the element\n * that is on the position where the provided `value` would be.\n */\n delete(value: V): TopKChanges<V> {\n const result: TopKChanges<V> = { moveIn: null, moveOut: null }\n\n // Lookup delete position\n const index = this.#findIndex(value)\n // Remove the value at that position\n const [removedElem] = this.#sortedValues.splice(index, 1)\n\n // Check if the topK changed\n if (index < this.#topKEnd) {\n // The removed element is either before the top K or within the top K\n // If it is before the top K then the first element of the topK moves out of the topK\n // If it is within the top K then the removed element moves out of the topK\n result.moveOut = removedElem!\n if (index < this.#topKStart) {\n // The removed element is before the topK\n // so actually, the first element of the topK moves out of the topK\n // and not the element that we removed\n // The first element of the topK is now at index topKStart - 1\n // since we removed an element before the topK\n const moveOutIndex = this.#topKStart - 1\n if (moveOutIndex < this.#sortedValues.length) {\n result.moveOut = this.#sortedValues[moveOutIndex]!\n } else {\n // No value is moving out of the topK\n // because there are no elements in the topK\n result.moveOut = null\n }\n }\n\n // Since we removed an element that was before or in the topK\n // the first element after the topK moved one position to the left\n // and thus falls into the topK now\n const moveInIndex = this.#topKEnd - 1\n if (moveInIndex < this.#sortedValues.length) {\n result.moveIn = this.#sortedValues[moveInIndex]!\n }\n }\n\n return result\n }\n\n // TODO: see if there is a way to refactor the code for insert and delete in the topK above\n // because they are very similar, one is shifting the topK window to the left and the other is shifting it to the right\n // so i have the feeling there is a common pattern here and we can implement both cases using that pattern\n\n #findIndex(value: V): number {\n return binarySearch(this.#sortedValues, indexedValue(value, ``), (a, b) =>\n this.#comparator(getValue(a), getValue(b))\n )\n }\n}\n\n/**\n * Operator for fractional indexed topK operations\n * This operator maintains fractional indices for sorted elements\n * and only updates indices when elements move position\n */\nexport class TopKWithFractionalIndexOperator<K, T> extends UnaryOperator<\n [K, T],\n [K, IndexedValue<T>]\n> {\n #index: Map<K, number> = new Map() // maps keys to their multiplicity\n\n /**\n * topK data structure that supports insertions and deletions\n * and returns changes to the topK.\n */\n #topK: TopK<TaggedValue<K, T>>\n\n constructor(\n id: number,\n inputA: DifferenceStreamReader<[K, T]>,\n output: DifferenceStreamWriter<[K, IndexedValue<T>]>,\n comparator: (a: T, b: T) => number,\n options: TopKWithFractionalIndexOptions\n ) {\n super(id, inputA, output)\n const limit = options.limit ?? Infinity\n const offset = options.offset ?? 0\n const compareTaggedValues = (\n a: TaggedValue<K, T>,\n b: TaggedValue<K, T>\n ) => {\n // First compare on the value\n const valueComparison = comparator(getVal(a), getVal(b))\n if (valueComparison !== 0) {\n return valueComparison\n }\n // If the values are equal, compare on the tag (object identity)\n const tieBreakerA = getTag(a)\n const tieBreakerB = getTag(b)\n return tieBreakerA - tieBreakerB\n }\n this.#topK = this.createTopK(offset, limit, compareTaggedValues)\n }\n\n protected createTopK(\n offset: number,\n limit: number,\n comparator: (a: TaggedValue<K, T>, b: TaggedValue<K, T>) => number\n ): TopK<TaggedValue<K, T>> {\n return new TopKArray(offset, limit, comparator)\n }\n\n run(): void {\n const result: Array<[[K, IndexedValue<T>], number]> = []\n for (const message of this.inputMessages()) {\n for (const [item, multiplicity] of message.getInner()) {\n const [key, value] = item\n this.processElement(key, value, multiplicity, result)\n }\n }\n\n if (result.length > 0) {\n this.output.sendData(new MultiSet(result))\n }\n }\n\n processElement(\n key: K,\n value: T,\n multiplicity: number,\n result: Array<[[K, IndexedValue<T>], number]>\n ): void {\n const { oldMultiplicity, newMultiplicity } = this.addKey(key, multiplicity)\n\n let res: TopKChanges<TaggedValue<K, T>> = {\n moveIn: null,\n moveOut: null,\n }\n if (oldMultiplicity <= 0 && newMultiplicity > 0) {\n // The value was invisible but should now be visible\n // Need to insert it into the array of sorted values\n const taggedValue = tagValue(key, value)\n res = this.#topK.insert(taggedValue)\n } else if (oldMultiplicity > 0 && newMultiplicity <= 0) {\n // The value was visible but should now be invisible\n // Need to remove it from the array of sorted values\n const taggedValue = tagValue(key, value)\n res = this.#topK.delete(taggedValue)\n } else {\n // The value was invisible and it remains invisible\n // or it was visible and remains visible\n // so it doesn't affect the topK\n }\n\n if (res.moveIn) {\n const index = getIndex(res.moveIn)\n const taggedValue = getValue(res.moveIn)\n const k = getKey(taggedValue)\n const val = getVal(taggedValue)\n result.push([[k, [val, index]], 1])\n }\n\n if (res.moveOut) {\n const index = getIndex(res.moveOut)\n const taggedValue = getValue(res.moveOut)\n const k = getKey(taggedValue)\n const val = getVal(taggedValue)\n result.push([[k, [val, index]], -1])\n }\n\n return\n }\n\n private getMultiplicity(key: K): number {\n return this.#index.get(key) ?? 0\n }\n\n private addKey(\n key: K,\n multiplicity: number\n ): { oldMultiplicity: number; newMultiplicity: number } {\n const oldMultiplicity = this.getMultiplicity(key)\n const newMultiplicity = oldMultiplicity + multiplicity\n if (newMultiplicity === 0) {\n this.#index.delete(key)\n } else {\n this.#index.set(key, newMultiplicity)\n }\n return { oldMultiplicity, newMultiplicity }\n }\n}\n\n/**\n * Limits the number of results based on a comparator, with optional offset.\n * Uses fractional indexing to minimize the number of changes when elements move positions.\n * Each element is assigned a fractional index that is lexicographically sortable.\n * When elements move, only the indices of the moved elements are updated, not all elements.\n *\n * @param comparator - A function that compares two elements\n * @param options - An optional object containing limit and offset properties\n * @returns A piped operator that orders the elements and limits the number of results\n */\nexport function topKWithFractionalIndex<KType, T>(\n comparator: (a: T, b: T) => number,\n options?: TopKWithFractionalIndexOptions\n): PipedOperator<[KType, T], [KType, IndexedValue<T>]> {\n const opts = options || {}\n\n return (\n stream: IStreamBuilder<[KType, T]>\n ): IStreamBuilder<[KType, IndexedValue<T>]> => {\n const output = new StreamBuilder<[KType, IndexedValue<T>]>(\n stream.graph,\n new DifferenceStreamWriter<[KType, IndexedValue<T>]>()\n )\n const operator = new TopKWithFractionalIndexOperator<KType, T>(\n stream.graph.getNextOperatorId(),\n stream.connectReader(),\n output.writer,\n comparator,\n opts\n )\n stream.graph.addOperator(operator)\n stream.graph.addStream(output.connectReader())\n return output\n }\n}\n\n// Abstraction for fractionally indexed values\nexport type FractionalIndex = string\nexport type IndexedValue<V> = [V, FractionalIndex]\n\nexport function indexedValue<V>(\n value: V,\n index: FractionalIndex\n): IndexedValue<V> {\n return [value, index]\n}\n\nexport function getValue<V>(indexedVal: IndexedValue<V>): V {\n return indexedVal[0]\n}\n\nexport function getIndex<V>(indexedVal: IndexedValue<V>): FractionalIndex {\n return indexedVal[1]\n}\n\nexport type Tag = number\nexport type TaggedValue<K, V> = [K, V, Tag]\n\nfunction tagValue<K, V>(key: K, value: V): TaggedValue<K, V> {\n return [key, value, globalObjectIdGenerator.getId(key)]\n}\n\nfunction getKey<K, V>(tieBreakerTaggedValue: TaggedValue<K, V>): K {\n return tieBreakerTaggedValue[0]\n}\n\nfunction getVal<K, V>(tieBreakerTaggedValue: TaggedValue<K, V>): V {\n return tieBreakerTaggedValue[1]\n}\n\nfunction getTag<K, V>(tieBreakerTaggedValue: TaggedValue<K, V>): Tag {\n return tieBreakerTaggedValue[2]\n}\n"],"names":["generateKeyBetween","binarySearch","UnaryOperator","MultiSet","StreamBuilder","DifferenceStreamWriter","globalObjectIdGenerator"],"mappings":";;;;;;;;;;;;;;;;AAmCA,MAAM,UAAgC;AAAA,EAMpC,YACE,QACA,OACA,YACA;AAVJ;AACE,sCAAwC,CAAA;AACxC;AACA;AACA;AAOE,uBAAK,YAAa;AAClB,uBAAK,UAAW,SAAS;AACzB,uBAAK,aAAc;AAAA,EACrB;AAAA,EAEA,OAAO,OAA0B;AAC/B,UAAM,SAAyB,EAAE,QAAQ,MAAM,SAAS,KAAA;AAGxD,UAAM,QAAQ,sBAAK,oCAAL,WAAgB;AAE9B,UAAM,cACJ,UAAU,IAAI,OAAO,SAAS,mBAAK,eAAc,QAAQ,CAAC,CAAE;AAC9D,UAAM,aACJ,UAAU,mBAAK,eAAc,SACzB,OACA,SAAS,mBAAK,eAAc,KAAK,CAAE;AACzC,UAAM,kBAAkBA,mBAAAA,mBAAmB,aAAa,UAAU;AAGlE,UAAM,MAAM,aAAa,OAAO,eAAe;AAE/C,uBAAK,eAAc,OAAO,OAAO,GAAG,GAAG;AAGvC,QAAI,QAAQ,mBAAK,WAA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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;AAMO,SAAS,aACd,OACA,OACiB;AACjB,SAAO,CAAC,OAAO,KAAK;AACtB;AAEO,SAAS,SAAY,YAAgC;AAC1D,SAAO,WAAW,CAAC;AACrB;AAEO,SAAS,SAAY,YAA8C;AACxE,SAAO,WAAW,CAAC;AACrB;AAKA,SAAS,SAAe,KAAQ,OAA6B;AAC3D,SAAO,CAAC,KAAK,OAAOC,MAAAA,wBAAwB,MAAM,GAAG,CAAC;AACxD;AAEA,SAAS,OAAa,uBAA6C;AACjE,SAAO,sBAAsB,CAAC;AAChC;AAEA,SAAS,OAAa,uBAA6C;AACjE,SAAO,sBAAsB,CAAC;AAChC;AAEA,SAAS,OAAa,uBAA+C;AACnE,SAAO,sBAAsB,CAAC;AAChC;;;;;;"}
1
+ {"version":3,"file":"topKWithFractionalIndex.cjs","sources":["../../../src/operators/topKWithFractionalIndex.ts"],"sourcesContent":["import { generateKeyBetween } from \"fractional-indexing\"\nimport { DifferenceStreamWriter, UnaryOperator } from \"../graph.js\"\nimport { StreamBuilder } from \"../d2.js\"\nimport { MultiSet } from \"../multiset.js\"\nimport { binarySearch, globalObjectIdGenerator } from \"../utils.js\"\nimport type { DifferenceStreamReader } from \"../graph.js\"\nimport type { IStreamBuilder, PipedOperator } from \"../types.js\"\n\nexport interface TopKWithFractionalIndexOptions {\n limit?: number\n offset?: number\n setSizeCallback?: (getSize: () => number) => void\n}\n\nexport type TopKChanges<V> = {\n /** Indicates which element moves into the topK (if any) */\n moveIn: IndexedValue<V> | null\n /** Indicates which element moves out of the topK (if any) */\n moveOut: IndexedValue<V> | null\n}\n\n/**\n * A topK data structure that supports insertions and deletions\n * and returns changes to the topK.\n */\nexport interface TopK<V> {\n size: number\n insert: (value: V) => TopKChanges<V>\n delete: (value: V) => TopKChanges<V>\n}\n\n/**\n * Implementation of a topK data structure.\n * Uses a sorted array internally to store the values and keeps a topK window over that array.\n * Inserts and deletes are O(n) operations because worst case an element is inserted/deleted\n * at the start of the array which causes all the elements to shift to the right/left.\n */\nclass TopKArray<V> implements TopK<V> {\n #sortedValues: Array<IndexedValue<V>> = []\n #comparator: (a: V, b: V) => number\n #topKStart: number\n #topKEnd: number\n\n constructor(\n offset: number,\n limit: number,\n comparator: (a: V, b: V) => number\n ) {\n this.#topKStart = offset\n this.#topKEnd = offset + limit\n this.#comparator = comparator\n }\n\n get size(): number {\n const offset = this.#topKStart\n const limit = this.#topKEnd - this.#topKStart\n const available = this.#sortedValues.length - offset\n return Math.max(0, Math.min(limit, available))\n }\n\n insert(value: V): TopKChanges<V> {\n const result: TopKChanges<V> = { moveIn: null, moveOut: null }\n\n // Lookup insert position\n const index = this.#findIndex(value)\n // Generate fractional index based on the fractional indices of the elements before and after it\n const indexBefore =\n index === 0 ? null : getIndex(this.#sortedValues[index - 1]!)\n const indexAfter =\n index === this.#sortedValues.length\n ? null\n : getIndex(this.#sortedValues[index]!)\n const fractionalIndex = generateKeyBetween(indexBefore, indexAfter)\n\n // Insert the value at the correct position\n const val = indexedValue(value, fractionalIndex)\n // Splice is O(n) where n = all elements in the collection (i.e. n >= k) !\n this.#sortedValues.splice(index, 0, val)\n\n // Check if the topK changed\n if (index < this.#topKEnd) {\n // The inserted element is either before the top K or within the top K\n // If it is before the top K then it moves the element that was right before the topK into the topK\n // If it is within the top K then the inserted element moves into the top K\n // In both cases the last element of the old top K now moves out of the top K\n const moveInIndex = Math.max(index, this.#topKStart)\n if (moveInIndex < this.#sortedValues.length) {\n // We actually have a topK\n // because in some cases there may not be enough elements in the array to reach the start of the topK\n // e.g. [1, 2, 3] with K = 2 and offset = 3 does not have a topK\n result.moveIn = this.#sortedValues[moveInIndex]!\n\n // We need to remove the element that falls out of the top K\n // The element that falls out of the top K has shifted one to the right\n // because of the element we inserted, so we find it at index topKEnd\n if (this.#topKEnd < this.#sortedValues.length) {\n result.moveOut = this.#sortedValues[this.#topKEnd]!\n }\n }\n }\n\n return result\n }\n\n /**\n * Deletes a value that may or may not be in the topK.\n * IMPORTANT: this assumes that the value is present in the collection\n * if it's not the case it will remove the element\n * that is on the position where the provided `value` would be.\n */\n delete(value: V): TopKChanges<V> {\n const result: TopKChanges<V> = { moveIn: null, moveOut: null }\n\n // Lookup delete position\n const index = this.#findIndex(value)\n // Remove the value at that position\n const [removedElem] = this.#sortedValues.splice(index, 1)\n\n // Check if the topK changed\n if (index < this.#topKEnd) {\n // The removed element is either before the top K or within the top K\n // If it is before the top K then the first element of the topK moves out of the topK\n // If it is within the top K then the removed element moves out of the topK\n result.moveOut = removedElem!\n if (index < this.#topKStart) {\n // The removed element is before the topK\n // so actually, the first element of the topK moves out of the topK\n // and not the element that we removed\n // The first element of the topK is now at index topKStart - 1\n // since we removed an element before the topK\n const moveOutIndex = this.#topKStart - 1\n if (moveOutIndex < this.#sortedValues.length) {\n result.moveOut = this.#sortedValues[moveOutIndex]!\n } else {\n // No value is moving out of the topK\n // because there are no elements in the topK\n result.moveOut = null\n }\n }\n\n // Since we removed an element that was before or in the topK\n // the first element after the topK moved one position to the left\n // and thus falls into the topK now\n const moveInIndex = this.#topKEnd - 1\n if (moveInIndex < this.#sortedValues.length) {\n result.moveIn = this.#sortedValues[moveInIndex]!\n }\n }\n\n return result\n }\n\n // TODO: see if there is a way to refactor the code for insert and delete in the topK above\n // because they are very similar, one is shifting the topK window to the left and the other is shifting it to the right\n // so i have the feeling there is a common pattern here and we can implement both cases using that pattern\n\n #findIndex(value: V): number {\n return binarySearch(this.#sortedValues, indexedValue(value, ``), (a, b) =>\n this.#comparator(getValue(a), getValue(b))\n )\n }\n}\n\n/**\n * Operator for fractional indexed topK operations\n * This operator maintains fractional indices for sorted elements\n * and only updates indices when elements move position\n */\nexport class TopKWithFractionalIndexOperator<K, T> extends UnaryOperator<\n [K, T],\n [K, IndexedValue<T>]\n> {\n #index: Map<K, number> = new Map() // maps keys to their multiplicity\n\n /**\n * topK data structure that supports insertions and deletions\n * and returns changes to the topK.\n */\n #topK: TopK<TaggedValue<K, T>>\n\n #limit: number\n\n constructor(\n id: number,\n inputA: DifferenceStreamReader<[K, T]>,\n output: DifferenceStreamWriter<[K, IndexedValue<T>]>,\n comparator: (a: T, b: T) => number,\n options: TopKWithFractionalIndexOptions\n ) {\n super(id, inputA, output)\n this.#limit = options.limit ?? Infinity\n const offset = options.offset ?? 0\n const compareTaggedValues = (\n a: TaggedValue<K, T>,\n b: TaggedValue<K, T>\n ) => {\n // First compare on the value\n const valueComparison = comparator(getVal(a), getVal(b))\n if (valueComparison !== 0) {\n return valueComparison\n }\n // If the values are equal, compare on the tag (object identity)\n const tieBreakerA = getTag(a)\n const tieBreakerB = getTag(b)\n return tieBreakerA - tieBreakerB\n }\n this.#topK = this.createTopK(offset, this.#limit, compareTaggedValues)\n options.setSizeCallback?.(() => this.#topK.size)\n }\n\n protected createTopK(\n offset: number,\n limit: number,\n comparator: (a: TaggedValue<K, T>, b: TaggedValue<K, T>) => number\n ): TopK<TaggedValue<K, T>> {\n return new TopKArray(offset, limit, comparator)\n }\n\n run(): void {\n const result: Array<[[K, IndexedValue<T>], number]> = []\n for (const message of this.inputMessages()) {\n for (const [item, multiplicity] of message.getInner()) {\n const [key, value] = item\n this.processElement(key, value, multiplicity, result)\n }\n }\n\n if (result.length > 0) {\n this.output.sendData(new MultiSet(result))\n }\n }\n\n processElement(\n key: K,\n value: T,\n multiplicity: number,\n result: Array<[[K, IndexedValue<T>], number]>\n ): void {\n const { oldMultiplicity, newMultiplicity } = this.addKey(key, multiplicity)\n\n let res: TopKChanges<TaggedValue<K, T>> = {\n moveIn: null,\n moveOut: null,\n }\n if (oldMultiplicity <= 0 && newMultiplicity > 0) {\n // The value was invisible but should now be visible\n // Need to insert it into the array of sorted values\n const taggedValue = tagValue(key, value)\n res = this.#topK.insert(taggedValue)\n } else if (oldMultiplicity > 0 && newMultiplicity <= 0) {\n // The value was visible but should now be invisible\n // Need to remove it from the array of sorted values\n const taggedValue = tagValue(key, value)\n res = this.#topK.delete(taggedValue)\n } else {\n // The value was invisible and it remains invisible\n // or it was visible and remains visible\n // so it doesn't affect the topK\n }\n\n if (res.moveIn) {\n const index = getIndex(res.moveIn)\n const taggedValue = getValue(res.moveIn)\n const k = getKey(taggedValue)\n const val = getVal(taggedValue)\n result.push([[k, [val, index]], 1])\n }\n\n if (res.moveOut) {\n const index = getIndex(res.moveOut)\n const taggedValue = getValue(res.moveOut)\n const k = getKey(taggedValue)\n const val = getVal(taggedValue)\n result.push([[k, [val, index]], -1])\n }\n\n return\n }\n\n private getMultiplicity(key: K): number {\n return this.#index.get(key) ?? 0\n }\n\n private addKey(\n key: K,\n multiplicity: number\n ): { oldMultiplicity: number; newMultiplicity: number } {\n const oldMultiplicity = this.getMultiplicity(key)\n const newMultiplicity = oldMultiplicity + multiplicity\n if (newMultiplicity === 0) {\n this.#index.delete(key)\n } else {\n this.#index.set(key, newMultiplicity)\n }\n return { oldMultiplicity, newMultiplicity }\n }\n}\n\n/**\n * Limits the number of results based on a comparator, with optional offset.\n * Uses fractional indexing to minimize the number of changes when elements move positions.\n * Each element is assigned a fractional index that is lexicographically sortable.\n * When elements move, only the indices of the moved elements are updated, not all elements.\n *\n * @param comparator - A function that compares two elements\n * @param options - An optional object containing limit and offset properties\n * @returns A piped operator that orders the elements and limits the number of results\n */\nexport function topKWithFractionalIndex<KType, T>(\n comparator: (a: T, b: T) => number,\n options?: TopKWithFractionalIndexOptions\n): PipedOperator<[KType, T], [KType, IndexedValue<T>]> {\n const opts = options || {}\n\n return (\n stream: IStreamBuilder<[KType, T]>\n ): IStreamBuilder<[KType, IndexedValue<T>]> => {\n const output = new StreamBuilder<[KType, IndexedValue<T>]>(\n stream.graph,\n new DifferenceStreamWriter<[KType, IndexedValue<T>]>()\n )\n const operator = new TopKWithFractionalIndexOperator<KType, T>(\n stream.graph.getNextOperatorId(),\n stream.connectReader(),\n output.writer,\n comparator,\n opts\n )\n stream.graph.addOperator(operator)\n stream.graph.addStream(output.connectReader())\n return output\n }\n}\n\n// Abstraction for fractionally indexed values\nexport type FractionalIndex = string\nexport type IndexedValue<V> = [V, FractionalIndex]\n\nexport function indexedValue<V>(\n value: V,\n index: FractionalIndex\n): IndexedValue<V> {\n return [value, index]\n}\n\nexport function getValue<V>(indexedVal: IndexedValue<V>): V {\n return indexedVal[0]\n}\n\nexport function getIndex<V>(indexedVal: IndexedValue<V>): FractionalIndex {\n return indexedVal[1]\n}\n\nexport type Tag = number\nexport type TaggedValue<K, V> = [K, V, Tag]\n\nfunction tagValue<K, V>(key: K, value: V): TaggedValue<K, V> {\n return [key, value, globalObjectIdGenerator.getId(key)]\n}\n\nfunction getKey<K, V>(tieBreakerTaggedValue: TaggedValue<K, V>): K {\n return tieBreakerTaggedValue[0]\n}\n\nfunction getVal<K, V>(tieBreakerTaggedValue: TaggedValue<K, V>): V {\n return tieBreakerTaggedValue[1]\n}\n\nfunction getTag<K, V>(tieBreakerTaggedValue: TaggedValue<K, V>): Tag {\n return tieBreakerTaggedValue[2]\n}\n"],"names":["generateKeyBetween","binarySearch","UnaryOperator","MultiSet","StreamBuilder","DifferenceStreamWriter","globalObjectIdGenerator"],"mappings":";;;;;;;;;;;;;;;;AAqCA,MAAM,UAAgC;AAAA,EAMpC,YACE,QACA,OACA,YACA;AAVJ;AACE,sCAAwC,CAAA;AACxC;AACA;AACA;AAOE,uBAAK,YAAa;AAClB,uBAAK,UAAW,SAAS;AACzB,uBAAK,aAAc;AAAA,EACrB;AAAA,EAEA,IAAI,OAAe;AACjB,UAAM,SAAS,mBAAK;AACpB,UAAM,QAAQ,mBAAK,YAAW,mBAAK;AACnC,UAAM,YAAY,mBAAK,eAAc,SAAS;AAC9C,WAAO,KAAK,IAAI,GAAG,KAAK,IAAI,OAAO,SAAS,CAAC;AAAA,EAC/C;AAAA,EAEA,OAAO,OAA0B;AAC/B,UAAM,SAAyB,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AAkB;AAC1C,QAAI,oBAAoB,GAAG;AACzB,yBAAK,QAAO,OAAO,GAAG;AAAA,IACxB,OAAO;AACL,yBAAK,QAAO,IAAI,KAAK,eAAe;AAAA,IACtC;AACA,WAAO,EAAE,iBAAiB,gBAAA;AAAA,EAC5B;AACF;AA5HE;AAMA;AAEA;AAgIK,SAAS,wBACd,YACA,SACqD;AACrD,QAAM,OAAO,WAAW,CAAA;AAExB,SAAO,CACL,WAC6C;AAC7C,UAAM,SAAS,IAAIC,GAAAA;AAAAA,MACjB,OAAO;AAAA,MACP,IAAIC,MAAAA,uBAAA;AAAA,IAAiD;AAEvD,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;AAMO,SAAS,aACd,OACA,OACiB;AACjB,SAAO,CAAC,OAAO,KAAK;AACtB;AAEO,SAAS,SAAY,YAAgC;AAC1D,SAAO,WAAW,CAAC;AACrB;AAEO,SAAS,SAAY,YAA8C;AACxE,SAAO,WAAW,CAAC;AACrB;AAKA,SAAS,SAAe,KAAQ,OAA6B;AAC3D,SAAO,CAAC,KAAK,OAAOC,MAAAA,wBAAwB,MAAM,GAAG,CAAC;AACxD;AAEA,SAAS,OAAa,uBAA6C;AACjE,SAAO,sBAAsB,CAAC;AAChC;AAEA,SAAS,OAAa,uBAA6C;AACjE,SAAO,sBAAsB,CAAC;AAChC;AAEA,SAAS,OAAa,uBAA+C;AACnE,SAAO,sBAAsB,CAAC;AAChC;;;;;;"}
@@ -3,6 +3,7 @@ import { PipedOperator } from '../types.js';
3
3
  export interface TopKWithFractionalIndexOptions {
4
4
  limit?: number;
5
5
  offset?: number;
6
+ setSizeCallback?: (getSize: () => number) => void;
6
7
  }
7
8
  export type TopKChanges<V> = {
8
9
  /** Indicates which element moves into the topK (if any) */
@@ -15,6 +16,7 @@ export type TopKChanges<V> = {
15
16
  * and returns changes to the topK.
16
17
  */
17
18
  export interface TopK<V> {
19
+ size: number;
18
20
  insert: (value: V) => TopKChanges<V>;
19
21
  delete: (value: V) => TopKChanges<V>;
20
22
  }
@@ -1,6 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const murmurhash = require("murmurhash-js");
4
3
  class DefaultMap extends Map {
5
4
  constructor(defaultValue, entries) {
6
5
  super(entries);
@@ -8,7 +7,7 @@ class DefaultMap extends Map {
8
7
  }
9
8
  get(key) {
10
9
  if (!this.has(key)) {
11
- this.set(key, this.defaultValue());
10
+ return this.defaultValue();
12
11
  }
13
12
  return super.get(key);
14
13
  }
@@ -33,36 +32,6 @@ function chunkedArrayPush(array, other) {
33
32
  }
34
33
  }
35
34
  }
36
- const hashCache = /* @__PURE__ */ new WeakMap();
37
- function hashReplacer(_key, value) {
38
- if (typeof value === `bigint`) {
39
- return String(value);
40
- } else if (typeof value === `symbol`) {
41
- return String(value);
42
- } else if (typeof value === `function`) {
43
- return String(value);
44
- } else if (value === void 0) {
45
- return `undefined`;
46
- } else if (value instanceof Map) {
47
- return `Map(${JSON.stringify(Array.from(value.entries()), hashReplacer)})`;
48
- } else if (value instanceof Set) {
49
- return `Set(${JSON.stringify(Array.from(value.values()), hashReplacer)})`;
50
- }
51
- return value;
52
- }
53
- function hash(data) {
54
- if (data === null || data === void 0 || typeof data !== `object` && typeof data !== `function`) {
55
- const serialized2 = JSON.stringify(data, hashReplacer);
56
- return murmurhash.murmur3(serialized2).toString(16);
57
- }
58
- if (hashCache.has(data)) {
59
- return hashCache.get(data);
60
- }
61
- const serialized = JSON.stringify(data, hashReplacer);
62
- const hashValue = murmurhash.murmur3(serialized).toString(16);
63
- hashCache.set(data, hashValue);
64
- return hashValue;
65
- }
66
35
  function binarySearch(array, value, comparator) {
67
36
  let low = 0;
68
37
  let high = array.length;
@@ -116,10 +85,21 @@ class ObjectIdGenerator {
116
85
  }
117
86
  }
118
87
  const globalObjectIdGenerator = new ObjectIdGenerator();
88
+ function* concatIterable(...iterables) {
89
+ for (const iterable of iterables) {
90
+ yield* iterable;
91
+ }
92
+ }
93
+ function* mapIterable(it, fn) {
94
+ for (const t of it) {
95
+ yield fn(t);
96
+ }
97
+ }
119
98
  exports.DefaultMap = DefaultMap;
120
99
  exports.ObjectIdGenerator = ObjectIdGenerator;
121
100
  exports.binarySearch = binarySearch;
122
101
  exports.chunkedArrayPush = chunkedArrayPush;
102
+ exports.concatIterable = concatIterable;
123
103
  exports.globalObjectIdGenerator = globalObjectIdGenerator;
124
- exports.hash = hash;
104
+ exports.mapIterable = mapIterable;
125
105
  //# sourceMappingURL=utils.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.cjs","sources":["../../src/utils.ts"],"sourcesContent":["import murmurhash from \"murmurhash-js\"\n\n/**\n * A map that returns a default value for keys that are not present.\n */\nexport class DefaultMap<K, V> extends Map<K, V> {\n constructor(\n private defaultValue: () => V,\n entries?: Iterable<[K, V]>\n ) {\n super(entries)\n }\n\n get(key: K): V {\n if (!this.has(key)) {\n this.set(key, this.defaultValue())\n }\n return super.get(key)!\n }\n\n /**\n * Update the value for a key using a function.\n */\n update(key: K, updater: (value: V) => V): V {\n const value = this.get(key)\n const newValue = updater(value)\n this.set(key, newValue)\n return newValue\n }\n}\n\n// JS engines have various limits on how many args can be passed to a function\n// with a spread operator, so we need to split the operation into chunks\n// 32767 is the max for Chrome 14, all others are higher\n// TODO: investigate the performance of this and other approaches\nconst chunkSize = 30000\nexport function chunkedArrayPush(array: Array<unknown>, other: Array<unknown>) {\n if (other.length <= chunkSize) {\n array.push(...other)\n } else {\n for (let i = 0; i < other.length; i += chunkSize) {\n const chunk = other.slice(i, i + chunkSize)\n array.push(...chunk)\n }\n }\n}\n\nconst hashCache = new WeakMap()\n\n/**\n * Replacer function for JSON.stringify that converts unsupported types to strings\n */\nfunction hashReplacer(_key: string, value: any): any {\n if (typeof value === `bigint`) {\n return String(value)\n } else if (typeof value === `symbol`) {\n return String(value)\n } else if (typeof value === `function`) {\n return String(value)\n } else if (value === undefined) {\n return `undefined`\n } else if (value instanceof Map) {\n return `Map(${JSON.stringify(Array.from(value.entries()), hashReplacer)})`\n } else if (value instanceof Set) {\n return `Set(${JSON.stringify(Array.from(value.values()), hashReplacer)})`\n }\n return value\n}\n\n/**\n * A hash method that caches the hash of a value in a week map\n */\nexport function hash(data: any): string {\n if (\n data === null ||\n data === undefined ||\n (typeof data !== `object` && typeof data !== `function`)\n ) {\n // Can't be cached in the weak map because it's not an object\n const serialized = JSON.stringify(data, hashReplacer)\n return murmurhash.murmur3(serialized).toString(16)\n }\n\n if (hashCache.has(data)) {\n return hashCache.get(data)\n }\n\n const serialized = JSON.stringify(data, hashReplacer)\n const hashValue = murmurhash.murmur3(serialized).toString(16)\n hashCache.set(data, hashValue)\n return hashValue\n}\n\nexport function binarySearch<T>(\n array: Array<T>,\n value: T,\n comparator: (a: T, b: T) => number\n): number {\n let low = 0\n let high = array.length\n while (low < high) {\n const mid = Math.floor((low + high) / 2)\n const comparison = comparator(array[mid]!, value)\n if (comparison < 0) {\n low = mid + 1\n } else if (comparison > 0) {\n high = mid\n } else {\n return mid\n }\n }\n return low\n}\n\n/**\n * Utility for generating unique IDs for objects and values.\n * Uses WeakMap for object reference tracking and consistent hashing for primitives.\n */\nexport class ObjectIdGenerator {\n private objectIds = new WeakMap<object, number>()\n private nextId = 0\n\n /**\n * Get a unique identifier for any value.\n * - Objects: Uses WeakMap for reference-based identity\n * - Primitives: Uses consistent string-based hashing\n */\n getId(value: any): number {\n // For primitives, use a simple hash of their string representation\n if (typeof value !== `object` || value === null) {\n const str = String(value)\n let hashValue = 0\n for (let i = 0; i < str.length; i++) {\n const char = str.charCodeAt(i)\n hashValue = (hashValue << 5) - hashValue + char\n hashValue = hashValue & hashValue // Convert to 32-bit integer\n }\n return hashValue\n }\n\n // For objects, use WeakMap to assign unique IDs\n if (!this.objectIds.has(value)) {\n this.objectIds.set(value, this.nextId++)\n }\n return this.objectIds.get(value)!\n }\n\n /**\n * Get a string representation of the ID for use in composite keys.\n */\n getStringId(value: any): string {\n if (value === null) return `null`\n if (value === undefined) return `undefined`\n if (typeof value !== `object`) return `str_${String(value)}`\n\n return `obj_${this.getId(value)}`\n }\n}\n\n/**\n * Global instance for cases where a shared object ID space is needed.\n */\nexport const globalObjectIdGenerator = new ObjectIdGenerator()\n"],"names":["serialized"],"mappings":";;;AAKO,MAAM,mBAAyB,IAAU;AAAA,EAC9C,YACU,cACR,SACA;AACA,UAAM,OAAO;AAHL,SAAA,eAAA;AAAA,EAIV;AAAA,EAEA,IAAI,KAAW;AACb,QAAI,CAAC,KAAK,IAAI,GAAG,GAAG;AAClB,WAAK,IAAI,KAAK,KAAK,aAAA,CAAc;AAAA,IACnC;AACA,WAAO,MAAM,IAAI,GAAG;AAAA,EACtB;AAAA;AAAA;AAAA;AAAA,EAKA,OAAO,KAAQ,SAA6B;AAC1C,UAAM,QAAQ,KAAK,IAAI,GAAG;AAC1B,UAAM,WAAW,QAAQ,KAAK;AAC9B,SAAK,IAAI,KAAK,QAAQ;AACtB,WAAO;AAAA,EACT;AACF;AAMA,MAAM,YAAY;AACX,SAAS,iBAAiB,OAAuB,OAAuB;AAC7E,MAAI,MAAM,UAAU,WAAW;AAC7B,UAAM,KAAK,GAAG,KAAK;AAAA,EACrB,OAAO;AACL,aAAS,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK,WAAW;AAChD,YAAM,QAAQ,MAAM,MAAM,GAAG,IAAI,SAAS;AAC1C,YAAM,KAAK,GAAG,KAAK;AAAA,IACrB;AAAA,EACF;AACF;AAEA,MAAM,gCAAgB,QAAA;AAKtB,SAAS,aAAa,MAAc,OAAiB;AACnD,MAAI,OAAO,UAAU,UAAU;AAC7B,WAAO,OAAO,KAAK;AAAA,EACrB,WAAW,OA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B,SAAQ,SAAS;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOjB,MAAM,OAAoB;AAExB,QAAI,OAAO,UAAU,YAAY,UAAU,MAAM;AAC/C,YAAM,MAAM,OAAO,KAAK;AACxB,UAAI,YAAY;AAChB,eAAS,IAAI,GAAG,IAAI,IAAI,QAAQ,KAAK;AACnC,cAAM,OAAO,IAAI,WAAW,CAAC;AAC7B,qBAAa,aAAa,KAAK,YAAY;AAC3C,oBAAY,YAAY;AAAA,MAC1B;AACA,aAAO;AAAA,IACT;AAGA,QAAI,CAAC,KAAK,UAAU,IAAI,KAAK,GAAG;AAC9B,WAAK,UAAU,IAAI,OAAO,KAAK,QAAQ;AAAA,IACzC;AACA,WAAO,KAAK,UAAU,IAAI,KAAK;AAAA,EACjC;AAAA;AAAA;AAAA;AAAA,EAKA,YAAY,OAAoB;AAC9B,QAAI,UAAU,KAAM,QAAO;AAC3B,QAAI,UAAU,OAAW,QAAO;AAChC,QAAI,OAAO,UAAU,iBAAiB,OAAO,OAAO,KAAK,CAAC;AAE1D,WAAO,OAAO,KAAK,MAAM,KAAK,CAAC;AAAA,EACjC;AACF;AAKO,MAAM,0BAA0B,IAAI,kBAAA;;;;;;;"}
1
+ {"version":3,"file":"utils.cjs","sources":["../../src/utils.ts"],"sourcesContent":["/**\n * Simple assertion function for runtime checks.\n * Throws an error if the condition is false.\n */\nexport function assert(\n condition: unknown,\n message?: string\n): asserts condition {\n if (!condition) {\n throw new Error(message || `Assertion failed`)\n }\n}\n\n/**\n * A map that returns a default value for keys that are not present.\n */\nexport class DefaultMap<K, V> extends Map<K, V> {\n constructor(\n private defaultValue: () => V,\n entries?: Iterable<[K, V]>\n ) {\n super(entries)\n }\n\n get(key: K): V {\n if (!this.has(key)) {\n // this.set(key, this.defaultValue())\n return this.defaultValue()\n }\n return super.get(key)!\n }\n\n /**\n * Update the value for a key using a function.\n */\n update(key: K, updater: (value: V) => V): V {\n const value = this.get(key)\n const newValue = updater(value)\n this.set(key, newValue)\n return newValue\n }\n}\n\n// JS engines have various limits on how many args can be passed to a function\n// with a spread operator, so we need to split the operation into chunks\n// 32767 is the max for Chrome 14, all others are higher\n// TODO: investigate the performance of this and other approaches\nconst chunkSize = 30000\nexport function chunkedArrayPush(array: Array<unknown>, other: Array<unknown>) {\n if (other.length <= chunkSize) {\n array.push(...other)\n } else {\n for (let i = 0; i < other.length; i += chunkSize) {\n const chunk = other.slice(i, i + chunkSize)\n array.push(...chunk)\n }\n }\n}\n\nexport function binarySearch<T>(\n array: Array<T>,\n value: T,\n comparator: (a: T, b: T) => number\n): number {\n let low = 0\n let high = array.length\n while (low < high) {\n const mid = Math.floor((low + high) / 2)\n const comparison = comparator(array[mid]!, value)\n if (comparison < 0) {\n low = mid + 1\n } else if (comparison > 0) {\n high = mid\n } else {\n return mid\n }\n }\n return low\n}\n\n/**\n * Utility for generating unique IDs for objects and values.\n * Uses WeakMap for object reference tracking and consistent hashing for primitives.\n */\nexport class ObjectIdGenerator {\n private objectIds = new WeakMap<object, number>()\n private nextId = 0\n\n /**\n * Get a unique identifier for any value.\n * - Objects: Uses WeakMap for reference-based identity\n * - Primitives: Uses consistent string-based hashing\n */\n getId(value: any): number {\n // For primitives, use a simple hash of their string representation\n if (typeof value !== `object` || value === null) {\n const str = String(value)\n let hashValue = 0\n for (let i = 0; i < str.length; i++) {\n const char = str.charCodeAt(i)\n hashValue = (hashValue << 5) - hashValue + char\n hashValue = hashValue & hashValue // Convert to 32-bit integer\n }\n return hashValue\n }\n\n // For objects, use WeakMap to assign unique IDs\n if (!this.objectIds.has(value)) {\n this.objectIds.set(value, this.nextId++)\n }\n return this.objectIds.get(value)!\n }\n\n /**\n * Get a string representation of the ID for use in composite keys.\n */\n getStringId(value: any): string {\n if (value === null) return `null`\n if (value === undefined) return `undefined`\n if (typeof value !== `object`) return `str_${String(value)}`\n\n return `obj_${this.getId(value)}`\n }\n}\n\n/**\n * Global instance for cases where a shared object ID space is needed.\n */\nexport const globalObjectIdGenerator = new ObjectIdGenerator()\n\nexport function* concatIterable<T>(\n ...iterables: Array<Iterable<T>>\n): Iterable<T> {\n for (const iterable of iterables) {\n yield* iterable\n }\n}\n\nexport function* mapIterable<T, U>(\n it: Iterable<T>,\n fn: (t: T) => U\n): Iterable<U> {\n for (const t of it) {\n yield fn(t)\n }\n}\n"],"names":[],"mappings":";;AAgBO,MAAM,mBAAyB,IAAU;AAAA,EAC9C,YACU,cACR,SACA;AACA,UAAM,OAAO;AAHL,SAAA,eAAA;AAAA,EAIV;AAAA,EAEA,IAAI,KAAW;AACb,QAAI,CAAC,KAAK,IAAI,GAAG,GAAG;AAElB,aAAO,KAAK,aAAA;AAAA,IACd;AACA,WAAO,MAAM,IAAI,GAAG;AAAA,EACtB;AAAA;AAAA;AAAA;AAAA,EAKA,OAAO,KAAQ,SAA6B;AAC1C,UAAM,QAAQ,KAAK,IAAI,GAAG;AAC1B,UAAM,WAAW,QAAQ,KAAK;AAC9B,SAAK,IAAI,KAAK,QAAQ;AACtB,WAAO;AAAA,EACT;AACF;AAMA,MAAM,YAAY;AACX,SAAS,iBAAiB,OAAuB,OAAuB;AAC7E,MAAI,MAAM,UAAU,WAAW;AAC7B,UAAM,KAAK,GAAG,KAAK;AAAA,EACrB,OAAO;AACL,aAAS,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK,WAAW;AAChD,YAAM,QAAQ,MAAM,MAAM,GAAG,IAAI,SAAS;AAC1C,YAAM,KAAK,GAAG,KAAK;AAAA,IACrB;AAAA,EACF;AACF;AAEO,SAAS,aACd,OACA,OACA,YACQ;AACR,MAAI,MAAM;AACV,MAAI,OAAO,MAAM;AACjB,SAAO,MAAM,MAAM;AACjB,UAAM,MAAM,KAAK,OAAO,MAAM,QAAQ,CAAC;AACvC,UAAM,aAAa,WAAW,MAAM,GAAG,GAAI,KAAK;AAChD,QAAI,aAAa,GAAG;AAClB,YAAM,MAAM;AAAA,IACd,WAAW,aAAa,GAAG;AACzB,aAAO;AAAA,IACT,OAAO;AACL,aAAO;AAAA,IACT;AAAA,EACF;AACA,SAAO;AACT;AAMO,MAAM,kBAAkB;AAAA,EAAxB,cAAA;AACL,SAAQ,gCAAgB,QAAA;AACxB,SAAQ,SAAS;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOjB,MAAM,OAAoB;AAExB,QAAI,OAAO,UAAU,YAAY,UAAU,MAAM;AAC/C,YAAM,MAAM,OAAO,KAAK;AACxB,UAAI,YAAY;AAChB,eAAS,IAAI,GAAG,IAAI,IAAI,QAAQ,KAAK;AACnC,cAAM,OAAO,IAAI,WAAW,CAAC;AAC7B,qBAAa,aAAa,KAAK,YAAY;AAC3C,oBAAY,YAAY;AAAA,MAC1B;AACA,aAAO;AAAA,IACT;AAGA,QAAI,CAAC,KAAK,UAAU,IAAI,KAAK,GAAG;AAC9B,WAAK,UAAU,IAAI,OAAO,KAAK,QAAQ;AAAA,IACzC;AACA,WAAO,KAAK,UAAU,IAAI,KAAK;AAAA,EACjC;AAAA;AAAA;AAAA;AAAA,EAKA,YAAY,OAAoB;AAC9B,QAAI,UAAU,KAAM,QAAO;AAC3B,QAAI,UAAU,OAAW,QAAO;AAChC,QAAI,OAAO,UAAU,iBAAiB,OAAO,OAAO,KAAK,CAAC;AAE1D,WAAO,OAAO,KAAK,MAAM,KAAK,CAAC;AAAA,EACjC;AACF;AAKO,MAAM,0BAA0B,IAAI,kBAAA;AAEpC,UAAU,kBACZ,WACU;AACb,aAAW,YAAY,WAAW;AAChC,WAAO;AAAA,EACT;AACF;AAEO,UAAU,YACf,IACA,IACa;AACb,aAAW,KAAK,IAAI;AAClB,UAAM,GAAG,CAAC;AAAA,EACZ;AACF;;;;;;;;"}
@@ -1,3 +1,8 @@
1
+ /**
2
+ * Simple assertion function for runtime checks.
3
+ * Throws an error if the condition is false.
4
+ */
5
+ export declare function assert(condition: unknown, message?: string): asserts condition;
1
6
  /**
2
7
  * A map that returns a default value for keys that are not present.
3
8
  */
@@ -11,10 +16,6 @@ export declare class DefaultMap<K, V> extends Map<K, V> {
11
16
  update(key: K, updater: (value: V) => V): V;
12
17
  }
13
18
  export declare function chunkedArrayPush(array: Array<unknown>, other: Array<unknown>): void;
14
- /**
15
- * A hash method that caches the hash of a value in a week map
16
- */
17
- export declare function hash(data: any): string;
18
19
  export declare function binarySearch<T>(array: Array<T>, value: T, comparator: (a: T, b: T) => number): number;
19
20
  /**
20
21
  * Utility for generating unique IDs for objects and values.
@@ -38,3 +39,5 @@ export declare class ObjectIdGenerator {
38
39
  * Global instance for cases where a shared object ID space is needed.
39
40
  */
40
41
  export declare const globalObjectIdGenerator: ObjectIdGenerator;
42
+ export declare function concatIterable<T>(...iterables: Array<Iterable<T>>): Iterable<T>;
43
+ export declare function mapIterable<T, U>(it: Iterable<T>, fn: (t: T) => U): Iterable<U>;
@@ -0,0 +1,74 @@
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, _ValueIndex_instances, setMultiplicity_fn;
11
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
12
+ const hash = require("./hashing/hash.cjs");
13
+ class ValueIndex {
14
+ // Maps key to the value and its multiplicity
15
+ constructor() {
16
+ __privateAdd(this, _ValueIndex_instances);
17
+ __privateAdd(this, _inner);
18
+ __privateSet(this, _inner, /* @__PURE__ */ new Map());
19
+ }
20
+ toString(indent = false) {
21
+ return `ValueIndex(${JSON.stringify(
22
+ [...__privateGet(this, _inner).entries()],
23
+ void 0,
24
+ indent ? 2 : void 0
25
+ )})`;
26
+ }
27
+ get(key) {
28
+ return __privateGet(this, _inner).get(key);
29
+ }
30
+ getMultiplicity(key) {
31
+ var _a;
32
+ return ((_a = this.get(key)) == null ? void 0 : _a[1]) ?? 0;
33
+ }
34
+ entries() {
35
+ return __privateGet(this, _inner).entries();
36
+ }
37
+ has(key) {
38
+ return __privateGet(this, _inner).has(key);
39
+ }
40
+ delete(key) {
41
+ __privateGet(this, _inner).delete(key);
42
+ }
43
+ get size() {
44
+ return __privateGet(this, _inner).size;
45
+ }
46
+ addValue(key, v) {
47
+ const [value, multiplicity] = v;
48
+ if (multiplicity === 0) {
49
+ return;
50
+ }
51
+ if (this.has(key)) {
52
+ const [currValue, currMultiplicity] = this.get(key);
53
+ if (hash.hash(value) === hash.hash(currValue)) {
54
+ __privateMethod(this, _ValueIndex_instances, setMultiplicity_fn).call(this, key, value, currMultiplicity + multiplicity);
55
+ return;
56
+ }
57
+ throw new Error(
58
+ `Cannot add value for key ${key} because it already exists in ValueIndex with a different value`
59
+ );
60
+ }
61
+ __privateGet(this, _inner).set(key, [value, multiplicity]);
62
+ }
63
+ }
64
+ _inner = new WeakMap();
65
+ _ValueIndex_instances = new WeakSet();
66
+ setMultiplicity_fn = function(key, value, multiplicity) {
67
+ if (multiplicity === 0) {
68
+ __privateGet(this, _inner).delete(key);
69
+ } else {
70
+ __privateGet(this, _inner).set(key, [value, multiplicity]);
71
+ }
72
+ };
73
+ exports.ValueIndex = ValueIndex;
74
+ //# sourceMappingURL=valueIndex.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"valueIndex.cjs","sources":["../../src/valueIndex.ts"],"sourcesContent":["import { hash } from \"./hashing/index.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 ValueIndex<K, V> {\n #inner: Map<K, [V, number]> // Maps key to the value and its multiplicity\n\n constructor() {\n this.#inner = new Map()\n }\n\n toString(indent = false): string {\n return `ValueIndex(${JSON.stringify(\n [...this.#inner.entries()],\n undefined,\n indent ? 2 : undefined\n )})`\n }\n\n get(key: K): [V, number] | undefined {\n return this.#inner.get(key)\n }\n\n getMultiplicity(key: K): number {\n return this.get(key)?.[1] ?? 0\n }\n\n entries() {\n return this.#inner.entries()\n }\n\n has(key: K): boolean {\n return this.#inner.has(key)\n }\n\n delete(key: K): void {\n this.#inner.delete(key)\n }\n\n get size(): number {\n return this.#inner.size\n }\n\n addValue(key: K, v: [V, number]): void {\n const [value, multiplicity] = v\n\n if (multiplicity === 0) {\n return\n }\n\n if (this.has(key)) {\n const [currValue, currMultiplicity] = this.get(key)!\n if (hash(value) === hash(currValue)) {\n // Update the multiplicity\n this.#setMultiplicity(key, value, currMultiplicity + multiplicity)\n return\n }\n // Different value, not allowed.\n // ValueIndex only supports one value per key.\n throw new Error(\n `Cannot add value for key ${key} because it already exists in ValueIndex with a different value`\n )\n }\n\n this.#inner.set(key, [value, multiplicity])\n }\n\n #setMultiplicity(key: K, value: V, multiplicity: number): void {\n if (multiplicity === 0) {\n this.#inner.delete(key)\n } else {\n this.#inner.set(key, [value, multiplicity])\n }\n }\n}\n"],"names":["hash"],"mappings":";;;;;;;;;;;;AAOO,MAAM,WAAiB;AAAA;AAAA,EAG5B,cAAc;AAHT;AACL;AAGE,uBAAK,4BAAa,IAAA;AAAA,EACpB;AAAA,EAEA,SAAS,SAAS,OAAe;AAC/B,WAAO,cAAc,KAAK;AAAA,MACxB,CAAC,GAAG,mBAAK,QAAO,SAAS;AAAA,MACzB;AAAA,MACA,SAAS,IAAI;AAAA,IAAA,CACd;AAAA,EACH;AAAA,EAEA,IAAI,KAAiC;AACnC,WAAO,mBAAK,QAAO,IAAI,GAAG;AAAA,EAC5B;AAAA,EAEA,gBAAgB,KAAgB;;AAC9B,aAAO,UAAK,IAAI,GAAG,MAAZ,mBAAgB,OAAM;AAAA,EAC/B;AAAA,EAEA,UAAU;AACR,WAAO,mBAAK,QAAO,QAAA;AAAA,EACrB;AAAA,EAEA,IAAI,KAAiB;AACnB,WAAO,mBAAK,QAAO,IAAI,GAAG;AAAA,EAC5B;AAAA,EAEA,OAAO,KAAc;AACnB,uBAAK,QAAO,OAAO,GAAG;AAAA,EACxB;AAAA,EAEA,IAAI,OAAe;AACjB,WAAO,mBAAK,QAAO;AAAA,EACrB;AAAA,EAEA,SAAS,KAAQ,GAAsB;AACrC,UAAM,CAAC,OAAO,YAAY,IAAI;AAE9B,QAAI,iBAAiB,GAAG;AACtB;AAAA,IACF;AAEA,QAAI,KAAK,IAAI,GAAG,GAAG;AACjB,YAAM,CAAC,WAAW,gBAAgB,IAAI,KAAK,IAAI,GAAG;AAClD,UAAIA,KAAAA,KAAK,KAAK,MAAMA,KAAAA,KAAK,SAAS,GAAG;AAEnC,8BAAK,2CAAL,WAAsB,KAAK,OAAO,mBAAmB;AACrD;AAAA,MACF;AAGA,YAAM,IAAI;AAAA,QACR,4BAA4B,GAAG;AAAA,MAAA;AAAA,IAEnC;AAEA,uBAAK,QAAO,IAAI,KAAK,CAAC,OAAO,YAAY,CAAC;AAAA,EAC5C;AASF;AArEE;AADK;AA+DL,qBAAA,SAAiB,KAAQ,OAAU,cAA4B;AAC7D,MAAI,iBAAiB,GAAG;AACtB,uBAAK,QAAO,OAAO,GAAG;AAAA,EACxB,OAAO;AACL,uBAAK,QAAO,IAAI,KAAK,CAAC,OAAO,YAAY,CAAC;AAAA,EAC5C;AACF;;"}
@@ -0,0 +1,17 @@
1
+ /**
2
+ * A map from a difference collection trace's keys -> (value, multiplicities) that changed.
3
+ * Used in operations like join and reduce where the operation needs to
4
+ * exploit the key-value structure of the data to run efficiently.
5
+ */
6
+ export declare class ValueIndex<K, V> {
7
+ #private;
8
+ constructor();
9
+ toString(indent?: boolean): string;
10
+ get(key: K): [V, number] | undefined;
11
+ getMultiplicity(key: K): number;
12
+ entries(): MapIterator<[K, [V, number]]>;
13
+ has(key: K): boolean;
14
+ delete(key: K): void;
15
+ get size(): number;
16
+ addValue(key: K, v: [V, number]): void;
17
+ }
@@ -0,0 +1,25 @@
1
+ import { DefaultMap } from './utils.js';
2
+ /**
3
+ * A map from a difference collection trace's keys -> (value, multiplicities) that changed.
4
+ * Used in operations like join and reduce where the operation needs to
5
+ * exploit the key-value structure of the data to run efficiently.
6
+ */
7
+ export declare class HashIndex<K, V> {
8
+ #private;
9
+ constructor();
10
+ toString(indent?: boolean): string;
11
+ get(key: K): Array<[V, number]>;
12
+ getMultiplicity(key: K, value: V): number;
13
+ entries(): MapIterator<[K, DefaultMap<number, [V, number]>]>;
14
+ entriesIterator(): Generator<[K, [V, number]]>;
15
+ has(key: K): boolean;
16
+ delete(key: K): void;
17
+ get size(): number;
18
+ /**
19
+ * Adds a value to the index and does not return anything
20
+ * except if the addition caused the value to be removed
21
+ * and the key to be left with only a single value.
22
+ * In that case, we return the single remaining value.
23
+ */
24
+ addValue(key: K, value: [V, number]): [V, number] | void;
25
+ }
@@ -0,0 +1,83 @@
1
+ var __typeError = (msg) => {
2
+ throw TypeError(msg);
3
+ };
4
+ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
5
+ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
6
+ 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);
7
+ var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
8
+ var _inner;
9
+ import { DefaultMap } from "./utils.js";
10
+ import { hash } from "./hashing/hash.js";
11
+ class HashIndex {
12
+ constructor() {
13
+ __privateAdd(this, _inner);
14
+ __privateSet(this, _inner, new DefaultMap(
15
+ () => new DefaultMap(() => [void 0, 0])
16
+ ));
17
+ }
18
+ toString(indent = false) {
19
+ return `HashIndex(${JSON.stringify(
20
+ [...__privateGet(this, _inner)].map(([k, valueMap]) => [k, [...valueMap]]),
21
+ void 0,
22
+ indent ? 2 : void 0
23
+ )})`;
24
+ }
25
+ get(key) {
26
+ const valueMap = __privateGet(this, _inner).get(key);
27
+ return [...valueMap.values()];
28
+ }
29
+ getMultiplicity(key, value) {
30
+ const valueMap = __privateGet(this, _inner).get(key);
31
+ const valueHash = hash(value);
32
+ const [, multiplicity] = valueMap.get(valueHash);
33
+ return multiplicity;
34
+ }
35
+ entries() {
36
+ return __privateGet(this, _inner).entries();
37
+ }
38
+ *entriesIterator() {
39
+ for (const [key, valueMap] of __privateGet(this, _inner).entries()) {
40
+ for (const [_valueHash, [value, multiplicity]] of valueMap.entries()) {
41
+ yield [key, [value, multiplicity]];
42
+ }
43
+ }
44
+ }
45
+ has(key) {
46
+ return __privateGet(this, _inner).has(key);
47
+ }
48
+ delete(key) {
49
+ __privateGet(this, _inner).delete(key);
50
+ }
51
+ get size() {
52
+ return __privateGet(this, _inner).size;
53
+ }
54
+ /**
55
+ * Adds a value to the index and does not return anything
56
+ * except if the addition caused the value to be removed
57
+ * and the key to be left with only a single value.
58
+ * In that case, we return the single remaining value.
59
+ */
60
+ addValue(key, value) {
61
+ const [val, multiplicity] = value;
62
+ const valueMap = __privateGet(this, _inner).get(key);
63
+ const valueHash = hash(val);
64
+ const [, existingMultiplicity] = valueMap.get(valueHash);
65
+ const newMultiplicity = existingMultiplicity + multiplicity;
66
+ if (multiplicity !== 0) {
67
+ if (newMultiplicity === 0) {
68
+ valueMap.delete(valueHash);
69
+ if (valueMap.size === 1) {
70
+ return valueMap.entries().next().value[1];
71
+ }
72
+ } else {
73
+ valueMap.set(valueHash, [val, newMultiplicity]);
74
+ }
75
+ }
76
+ __privateGet(this, _inner).set(key, valueMap);
77
+ }
78
+ }
79
+ _inner = new WeakMap();
80
+ export {
81
+ HashIndex
82
+ };
83
+ //# sourceMappingURL=hashIndex.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hashIndex.js","sources":["../../src/hashIndex.ts"],"sourcesContent":["import { DefaultMap } from \"./utils.js\"\nimport { hash } from \"./hashing/index.js\"\nimport type { Hash } from \"./hashing/index.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 HashIndex<K, V> {\n #inner: DefaultMap<K, DefaultMap<Hash, [V, number]>>\n\n constructor() {\n this.#inner = new DefaultMap<K, DefaultMap<Hash, [V, number]>>(\n () => new DefaultMap<Hash, [V, number]>(() => [undefined as any as V, 0])\n )\n // #inner is as map of:\n // {\n // [key]: {\n // [hash(value)]: [value, multiplicity]\n // }\n // }\n }\n\n toString(indent = false): string {\n return `HashIndex(${JSON.stringify(\n [...this.#inner].map(([k, valueMap]) => [k, [...valueMap]]),\n undefined,\n indent ? 2 : undefined\n )})`\n }\n\n get(key: K): Array<[V, number]> {\n const valueMap = this.#inner.get(key)\n return [...valueMap.values()]\n }\n\n getMultiplicity(key: K, value: V): number {\n const valueMap = this.#inner.get(key)\n const valueHash = hash(value)\n const [, multiplicity] = valueMap.get(valueHash)\n return multiplicity\n }\n\n entries() {\n return this.#inner.entries()\n }\n\n *entriesIterator(): Generator<[K, [V, number]]> {\n for (const [key, valueMap] of this.#inner.entries()) {\n for (const [_valueHash, [value, multiplicity]] of valueMap.entries()) {\n yield [key, [value, multiplicity]]\n }\n }\n }\n\n has(key: K): boolean {\n return this.#inner.has(key)\n }\n\n delete(key: K): void {\n this.#inner.delete(key)\n }\n\n get size(): number {\n return this.#inner.size\n }\n\n /**\n * Adds a value to the index and does not return anything\n * except if the addition caused the value to be removed\n * and the key to be left with only a single value.\n * In that case, we return the single remaining value.\n */\n addValue(key: K, value: [V, number]): [V, number] | void {\n const [val, multiplicity] = value\n const valueMap = this.#inner.get(key)\n const valueHash = hash(val)\n const [, existingMultiplicity] = valueMap.get(valueHash)\n const newMultiplicity = existingMultiplicity + multiplicity\n if (multiplicity !== 0) {\n if (newMultiplicity === 0) {\n valueMap.delete(valueHash)\n if (valueMap.size === 1) {\n // Signal that the key only has a single remaining value\n return valueMap.entries().next().value![1]\n }\n } else {\n valueMap.set(valueHash, [val, newMultiplicity])\n }\n }\n this.#inner.set(key, valueMap)\n }\n}\n"],"names":[],"mappings":";;;;;;;;;;AASO,MAAM,UAAgB;AAAA,EAG3B,cAAc;AAFd;AAGE,uBAAK,QAAS,IAAI;AAAA,MAChB,MAAM,IAAI,WAA8B,MAAM,CAAC,QAAuB,CAAC,CAAC;AAAA,IAAA;AAAA,EAQ5E;AAAA,EAEA,SAAS,SAAS,OAAe;AAC/B,WAAO,aAAa,KAAK;AAAA,MACvB,CAAC,GAAG,mBAAK,OAAM,EAAE,IAAI,CAAC,CAAC,GAAG,QAAQ,MAAM,CAAC,GAAG,CAAC,GAAG,QAAQ,CAAC,CAAC;AAAA,MAC1D;AAAA,MACA,SAAS,IAAI;AAAA,IAAA,CACd;AAAA,EACH;AAAA,EAEA,IAAI,KAA4B;AAC9B,UAAM,WAAW,mBAAK,QAAO,IAAI,GAAG;AACpC,WAAO,CAAC,GAAG,SAAS,QAAQ;AAAA,EAC9B;AAAA,EAEA,gBAAgB,KAAQ,OAAkB;AACxC,UAAM,WAAW,mBAAK,QAAO,IAAI,GAAG;AACpC,UAAM,YAAY,KAAK,KAAK;AAC5B,UAAM,CAAA,EAAG,YAAY,IAAI,SAAS,IAAI,SAAS;AAC/C,WAAO;AAAA,EACT;AAAA,EAEA,UAAU;AACR,WAAO,mBAAK,QAAO,QAAA;AAAA,EACrB;AAAA,EAEA,CAAC,kBAA+C;AAC9C,eAAW,CAAC,KAAK,QAAQ,KAAK,mBAAK,QAAO,WAAW;AACnD,iBAAW,CAAC,YAAY,CAAC,OAAO,YAAY,CAAC,KAAK,SAAS,WAAW;AACpE,cAAM,CAAC,KAAK,CAAC,OAAO,YAAY,CAAC;AAAA,MACnC;AAAA,IACF;AAAA,EACF;AAAA,EAEA,IAAI,KAAiB;AACnB,WAAO,mBAAK,QAAO,IAAI,GAAG;AAAA,EAC5B;AAAA,EAEA,OAAO,KAAc;AACnB,uBAAK,QAAO,OAAO,GAAG;AAAA,EACxB;AAAA,EAEA,IAAI,OAAe;AACjB,WAAO,mBAAK,QAAO;AAAA,EACrB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,SAAS,KAAQ,OAAwC;AACvD,UAAM,CAAC,KAAK,YAAY,IAAI;AAC5B,UAAM,WAAW,mBAAK,QAAO,IAAI,GAAG;AACpC,UAAM,YAAY,KAAK,GAAG;AAC1B,UAAM,CAAA,EAAG,oBAAoB,IAAI,SAAS,IAAI,SAAS;AACvD,UAAM,kBAAkB,uBAAuB;AAC/C,QAAI,iBAAiB,GAAG;AACtB,UAAI,oBAAoB,GAAG;AACzB,iBAAS,OAAO,SAAS;AACzB,YAAI,SAAS,SAAS,GAAG;AAEvB,iBAAO,SAAS,QAAA,EAAU,KAAA,EAAO,MAAO,CAAC;AAAA,QAC3C;AAAA,MACF,OAAO;AACL,iBAAS,IAAI,WAAW,CAAC,KAAK,eAAe,CAAC;AAAA,MAChD;AAAA,IACF;AACA,uBAAK,QAAO,IAAI,KAAK,QAAQ;AAAA,EAC/B;AACF;AAnFE;"}
@@ -0,0 +1 @@
1
+ export declare function hash(input: any): number;
@@ -0,0 +1,122 @@
1
+ import { MurmurHashStream, randomHash } from "./murmur.js";
2
+ const TRUE = randomHash();
3
+ const FALSE = randomHash();
4
+ const NULL = randomHash();
5
+ const UNDEFINED = randomHash();
6
+ const KEY = randomHash();
7
+ const FUNCTIONS = randomHash();
8
+ const DATE_MARKER = randomHash();
9
+ const OBJECT_MARKER = randomHash();
10
+ const ARRAY_MARKER = randomHash();
11
+ const MAP_MARKER = randomHash();
12
+ const SET_MARKER = randomHash();
13
+ const hashCache = /* @__PURE__ */ new WeakMap();
14
+ function hash(input) {
15
+ const hasher = new MurmurHashStream();
16
+ updateHasher(hasher, input);
17
+ return hasher.digest();
18
+ }
19
+ function hashObject(input) {
20
+ const cachedHash = hashCache.get(input);
21
+ if (cachedHash !== void 0) {
22
+ return cachedHash;
23
+ }
24
+ let valueHash;
25
+ if (input instanceof Date) {
26
+ valueHash = hashDate(input);
27
+ } else {
28
+ let plainObjectInput = input;
29
+ let marker = OBJECT_MARKER;
30
+ if (input instanceof Array) {
31
+ marker = ARRAY_MARKER;
32
+ }
33
+ if (input instanceof Map) {
34
+ marker = MAP_MARKER;
35
+ plainObjectInput = [...input.entries()];
36
+ }
37
+ if (input instanceof Set) {
38
+ marker = SET_MARKER;
39
+ plainObjectInput = [...input.entries()];
40
+ }
41
+ if (input instanceof Buffer || input instanceof Uint8Array || input instanceof File) {
42
+ return cachedReferenceHash(input);
43
+ }
44
+ valueHash = hashPlainObject(plainObjectInput, marker);
45
+ }
46
+ hashCache.set(input, valueHash);
47
+ return valueHash;
48
+ }
49
+ function hashDate(input) {
50
+ const hasher = new MurmurHashStream();
51
+ hasher.update(DATE_MARKER);
52
+ hasher.update(input.getTime());
53
+ return hasher.digest();
54
+ }
55
+ function hashPlainObject(input, marker) {
56
+ const hasher = new MurmurHashStream();
57
+ hasher.update(marker);
58
+ const keys = Object.keys(input);
59
+ keys.sort(keySort);
60
+ for (const key of keys) {
61
+ hasher.update(KEY);
62
+ hasher.update(key);
63
+ updateHasher(hasher, input[key]);
64
+ }
65
+ return hasher.digest();
66
+ }
67
+ function updateHasher(hasher, input) {
68
+ if (input === null) {
69
+ hasher.update(NULL);
70
+ return;
71
+ }
72
+ switch (typeof input) {
73
+ case `undefined`:
74
+ hasher.update(UNDEFINED);
75
+ return;
76
+ case `boolean`:
77
+ hasher.update(input ? TRUE : FALSE);
78
+ return;
79
+ case `number`:
80
+ hasher.update(isNaN(input) ? NaN : input === 0 ? 0 : input);
81
+ return;
82
+ case `bigint`:
83
+ case `string`:
84
+ case `symbol`:
85
+ hasher.update(input);
86
+ return;
87
+ case `object`:
88
+ hasher.update(getCachedHash(input));
89
+ return;
90
+ case `function`:
91
+ hasher.update(cachedReferenceHash(input));
92
+ return;
93
+ default:
94
+ console.warn(
95
+ `Ignored input during hashing because it is of type ${typeof input} which is not supported`
96
+ );
97
+ }
98
+ }
99
+ function getCachedHash(input) {
100
+ let valueHash = hashCache.get(input);
101
+ if (valueHash === void 0) {
102
+ valueHash = hashObject(input);
103
+ }
104
+ return valueHash;
105
+ }
106
+ let nextRefId = 1;
107
+ function cachedReferenceHash(fn) {
108
+ let valueHash = hashCache.get(fn);
109
+ if (valueHash === void 0) {
110
+ valueHash = nextRefId ^ FUNCTIONS;
111
+ nextRefId++;
112
+ hashCache.set(fn, valueHash);
113
+ }
114
+ return valueHash;
115
+ }
116
+ function keySort(a, b) {
117
+ return a.localeCompare(b);
118
+ }
119
+ export {
120
+ hash
121
+ };
122
+ //# sourceMappingURL=hash.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hash.js","sources":["../../../src/hashing/hash.ts"],"sourcesContent":["import { MurmurHashStream, randomHash } from \"./murmur.js\"\nimport type { Hasher } from \"./murmur.js\"\n\n/*\n * Implementation of structural hashing based on the Composites polyfill implementation:\n * https://github.com/tc39/proposal-composites\n */\n\nconst TRUE = randomHash()\nconst FALSE = randomHash()\nconst NULL = randomHash()\nconst UNDEFINED = randomHash()\nconst KEY = randomHash()\nconst FUNCTIONS = randomHash()\nconst DATE_MARKER = randomHash()\nconst OBJECT_MARKER = randomHash()\nconst ARRAY_MARKER = randomHash()\nconst MAP_MARKER = randomHash()\nconst SET_MARKER = randomHash()\n\nconst hashCache = new WeakMap<object, number>()\n\nexport function hash(input: any): number {\n const hasher = new MurmurHashStream()\n updateHasher(hasher, input)\n return hasher.digest()\n}\n\nfunction hashObject(input: object): number {\n const cachedHash = hashCache.get(input)\n if (cachedHash !== undefined) {\n return cachedHash\n }\n\n let valueHash: number | undefined\n if (input instanceof Date) {\n valueHash = hashDate(input)\n } else {\n let plainObjectInput = input\n let marker = OBJECT_MARKER\n\n if (input instanceof Array) {\n marker = ARRAY_MARKER\n }\n\n if (input instanceof Map) {\n marker = MAP_MARKER\n plainObjectInput = [...input.entries()]\n }\n\n if (input instanceof Set) {\n marker = SET_MARKER\n plainObjectInput = [...input.entries()]\n }\n\n if (\n input instanceof Buffer ||\n input instanceof Uint8Array ||\n input instanceof File\n ) {\n // Deeply hashing these objects would be too costly\n // but we also don't want to ignore them\n // so we track them by reference and cache them in a weak map\n return cachedReferenceHash(input)\n }\n\n valueHash = hashPlainObject(plainObjectInput, marker)\n }\n\n hashCache.set(input, valueHash)\n return valueHash\n}\n\nfunction hashDate(input: Date): number {\n const hasher = new MurmurHashStream()\n hasher.update(DATE_MARKER)\n hasher.update(input.getTime())\n return hasher.digest()\n}\n\nfunction hashPlainObject(input: object, marker: number): number {\n const hasher = new MurmurHashStream()\n\n // Mark the type of the input\n hasher.update(marker)\n const keys = Object.keys(input)\n keys.sort(keySort)\n for (const key of keys) {\n hasher.update(KEY)\n hasher.update(key)\n updateHasher(hasher, input[key as keyof typeof input])\n }\n\n return hasher.digest()\n}\n\nfunction updateHasher(hasher: Hasher, input: unknown): void {\n if (input === null) {\n hasher.update(NULL)\n return\n }\n switch (typeof input) {\n case `undefined`:\n hasher.update(UNDEFINED)\n return\n case `boolean`:\n hasher.update(input ? TRUE : FALSE)\n return\n case `number`:\n // Normalize NaNs and -0\n hasher.update(isNaN(input) ? NaN : input === 0 ? 0 : input)\n return\n case `bigint`:\n case `string`:\n case `symbol`:\n hasher.update(input)\n return\n case `object`:\n hasher.update(getCachedHash(input))\n return\n case `function`:\n // Functions are assigned a globally unique ID\n // and that ID is cached in the weak map\n hasher.update(cachedReferenceHash(input))\n return\n default:\n console.warn(\n `Ignored input during hashing because it is of type ${typeof input} which is not supported`\n )\n }\n}\n\nfunction getCachedHash(input: object): number {\n let valueHash = hashCache.get(input)\n if (valueHash === undefined) {\n valueHash = hashObject(input)\n }\n return valueHash\n}\n\nlet nextRefId = 1\nfunction cachedReferenceHash(fn: object): number {\n let valueHash = hashCache.get(fn)\n if (valueHash === undefined) {\n valueHash = nextRefId ^ FUNCTIONS\n nextRefId++\n hashCache.set(fn, valueHash)\n }\n return valueHash\n}\n\n/**\n * Strings sorted lexicographically.\n */\nfunction keySort(a: string, b: string): number {\n return a.localeCompare(b)\n}\n"],"names":[],"mappings":";AAQA,MAAM,OAAO,WAAA;AACb,MAAM,QAAQ,WAAA;AACd,MAAM,OAAO,WAAA;AACb,MAAM,YAAY,WAAA;AAClB,MAAM,MAAM,WAAA;AACZ,MAAM,YAAY,WAAA;AAClB,MAAM,cAAc,WAAA;AACpB,MAAM,gBAAgB,WAAA;AACtB,MAAM,eAAe,WAAA;AACrB,MAAM,aAAa,WAAA;AACnB,MAAM,aAAa,WAAA;AAEnB,MAAM,gCAAgB,QAAA;AAEf,SAAS,KAAK,OAAoB;AACvC,QAAM,SAAS,IAAI,iBAAA;AACnB,eAAa,QAAQ,KAAK;AAC1B,SAAO,OAAO,OAAA;AAChB;AAEA,SAAS,WAAW,OAAuB;AACzC,QAAM,aAAa,UAAU,IAAI,KAAK;AACtC,MAAI,eAAe,QAAW;AAC5B,WAAO;AAAA,EACT;AAEA,MAAI;AACJ,MAAI,iBAAiB,MAAM;AACzB,gBAAY,SAAS,KAAK;AAAA,EAC5B,OAAO;AACL,QAAI,mBAAmB;AACvB,QAAI,SAAS;AAEb,QAAI,iBAAiB,OAAO;AAC1B,eAAS;AAAA,IACX;AAEA,QAAI,iBAAiB,KAAK;AACxB,eAAS;AACT,yBAAmB,CAAC,GAAG,MAAM,SAAS;AAAA,IACxC;AAEA,QAAI,iBAAiB,KAAK;AACxB,eAAS;AACT,yBAAmB,CAAC,GAAG,MAAM,SAAS;AAAA,IACxC;AAEA,QACE,iBAAiB,UACjB,iBAAiB,cACjB,iBAAiB,MACjB;AAIA,aAAO,oBAAoB,KAAK;AAAA,IAClC;AAEA,gBAAY,gBAAgB,kBAAkB,MAAM;AA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;AACA,SAAO;AACT;AAEA,IAAI,YAAY;AAChB,SAAS,oBAAoB,IAAoB;AAC/C,MAAI,YAAY,UAAU,IAAI,EAAE;AAChC,MAAI,cAAc,QAAW;AAC3B,gBAAY,YAAY;AACxB;AACA,cAAU,IAAI,IAAI,SAAS;AAAA,EAC7B;AACA,SAAO;AACT;AAKA,SAAS,QAAQ,GAAW,GAAmB;AAC7C,SAAO,EAAE,cAAc,CAAC;AAC1B;"}
@@ -0,0 +1,2 @@
1
+ export { hash } from './hash.js';
2
+ export type { Hash, Hasher } from './murmur.js';
@@ -0,0 +1,26 @@
1
+ export type Hash = number;
2
+ export declare function randomHash(): number;
3
+ export interface Hasher {
4
+ update: (val: symbol | string | number | bigint) => void;
5
+ digest: () => number;
6
+ }
7
+ /**
8
+ * This implementation of Murmur hash uses a random initial seed and random markers.
9
+ * This means that hashes aren't deterministic across app restarts.
10
+ * This is intentional in the composites polyfill to be resistent to hash-flooding attacks
11
+ * where malicious users would precompute lots of different objects whose hashes collide with each other.
12
+ *
13
+ * Currently, for ts/db-ivm this is fine because we don't persist client state.
14
+ * However, when we will introduce persistence we will either need to store the seeds or remove the randomness
15
+ * to ensure deterministic hashes across app restarts.
16
+ */
17
+ export declare class MurmurHashStream implements Hasher {
18
+ private hash;
19
+ private length;
20
+ private carry;
21
+ private carryBytes;
22
+ private _mix;
23
+ private _writeByte;
24
+ update(chunk: symbol | string | number | bigint): void;
25
+ digest(): number;
26
+ }