Open History to see the last commit
1import { MultiSet } from "./multiset.js"
2import { DefaultMap } from "./utils.js"
3
4/**
5 * A map from a difference collection trace's keys -> (value, multiplicities) that changed.
6 * Used in operations like join and reduce where the operation needs to
7 * exploit the key-value structure of the data to run efficiently.
8 */
9export class Index<K, V> {
10 #inner: DefaultMap<K, Map<V, number>>
11
12 constructor() {
13 this.#inner = new DefaultMap<K, Map<V, number>>(() => new Map<V, number>())
14 // #inner is a map of:
15 // {
16 // [key]: Map<V, number> // Direct value-to-multiplicity mapping
17 // }
18 }
19
20 toString(indent = false): string {
21 return `Index(${JSON.stringify(
22 [...this.#inner].map(([k, valueMap]) => [k, [...valueMap]]),
23 undefined,
24 indent ? ` ` : undefined
25 )})`
26 }
27
28 get(key: K): Array<[V, number]> {
29 const valueMap = this.#inner.get(key)
30 return [...valueMap.entries()]
31 }
32
33 getMultiplicity(key: K, value: V): number {
34 const valueMap = this.#inner.get(key)
35 return valueMap.get(value) ?? 0
36 }
37
38 entries() {
39 return this.#inner.entries()
40 }
41
42 keys() {
43 return this.#inner.keys()
44 }
45
46 has(key: K): boolean {
47 return this.#inner.has(key)
48 }
49
50 get size(): number {
51 return this.#inner.size
52 }
53
54 addValue(key: K, value: [V, number]): void {
55 const [val, multiplicity] = value
56 const valueMap = this.#inner.get(key)
57 const existingMultiplicity = valueMap.get(val) ?? 0
58 const newMultiplicity = existingMultiplicity + multiplicity
59
60 if (multiplicity !== 0) {
61 if (newMultiplicity === 0) {
62 valueMap.delete(val)
63 } else {
64 valueMap.set(val, newMultiplicity)
65 }
66 }
67 }
68
69 append(other: Index<K, V>): void {
70 for (const [key, otherValueMap] of other.entries()) {
71 const thisValueMap = this.#inner.get(key)
72 for (const [value, multiplicity] of otherValueMap.entries()) {
73 const existingMultiplicity = thisValueMap.get(value) ?? 0
74 const newMultiplicity = existingMultiplicity + multiplicity
75 if (newMultiplicity === 0) {
76 thisValueMap.delete(value)
77 } else {
78 thisValueMap.set(value, newMultiplicity)
79 }
80 }
81 }
82 }
83
84 join<V2>(other: Index<K, V2>): MultiSet<[K, [V, V2]]> {
85 const result: Array<[[K, [V, V2]], number]> = []
86
87 // We want to iterate over the smaller of the two indexes to reduce the
88 // number of operations we need to do.
89 if (this.size <= other.size) {
90 for (const [key, valueMap] of this.entries()) {
91 if (!other.has(key)) continue
92 const otherValues = other.get(key)
93 for (const [val1, mul1] of valueMap.entries()) {
94 for (const [val2, mul2] of otherValues) {
95 if (mul1 !== 0 && mul2 !== 0) {
96 result.push([[key, [val1, val2]], mul1 * mul2])
97 }
98 }
99 }
100 }
101 } else {
102 for (const [key, otherValueMap] of other.entries()) {
103 if (!this.has(key)) continue
104 const values = this.get(key)
105 for (const [val2, mul2] of otherValueMap.entries()) {
106 for (const [val1, mul1] of values) {
107 if (mul1 !== 0 && mul2 !== 0) {
108 result.push([[key, [val1, val2]], mul1 * mul2])
109 }
110 }
111 }
112 }
113 }
114
115 return new MultiSet(result)
116 }
117}