Open History to see the last commit
1import { UnaryOperator } from '../graph.js'
2import { unaryOperator } from '../unary-operator.js'
3import { hash } from '../hashing/index.js'
4import { equalHashValues } from '../hashing/hash.js'
5import { MultiSet } from '../multiset.js'
6import type { Hash } from '../hashing/index.js'
7import type {
8 DifferenceStreamReader,
9 DifferenceStreamWriter,
10} from '../graph.js'
11import type { KeyValue } from '../types.js'
12
13type GetValue<T> = T extends KeyValue<unknown, infer V> ? V : never
14
15// Keep the emitted row for its eventual retraction. Input representatives can
16// differ even when the selected identity is equal.
17type DistinctEntry<V> = [
18 identity: unknown,
19 row: KeyValue<number, V>,
20 multiplicity: number,
21 visible: boolean,
22]
23
24/**
25 * Operator that removes duplicates
26 */
27export class DistinctOperator<
28 T extends KeyValue<unknown, unknown>,
29> extends UnaryOperator<T, KeyValue<number, GetValue<T>>> {
30 #by: (value: T) => unknown
31 #values = new Map<Hash, Array<DistinctEntry<GetValue<T>>>>()
32 // Hash digests are unsigned. Negative keys give collisions a separate identity.
33 #nextCollisionKey = 0
34
35 constructor(
36 id: number,
37 input: DifferenceStreamReader<T>,
38 output: DifferenceStreamWriter<KeyValue<number, GetValue<T>>>,
39 by: (value: T) => unknown = (value: T) => value,
40 ) {
41 super(id, input, output)
42 this.#by = by
43 }
44
45 run(): void {
46 const updatedBuckets = new Set<Hash>()
47
48 // Compute the new multiplicity for each value
49 for (const message of this.inputMessages()) {
50 for (const [value, diff] of message.getInner()) {
51 const identity = this.#by(value)
52 const hashedValue = hash(identity)
53 let bucket = this.#values.get(hashedValue)
54 if (!bucket) this.#values.set(hashedValue, (bucket = []))
55 let entry = bucket.find(([candidate]) =>
56 equalHashValues(candidate, identity),
57 )
58 if (!entry) {
59 entry = [
60 identity,
61 [
62 bucket.length ? --this.#nextCollisionKey : hashedValue,
63 value[1] as GetValue<T>,
64 ],
65 0,
66 false,
67 ]
68 bucket.push(entry)
69 }
70 updatedBuckets.add(hashedValue)
71 entry[2] += diff
72 }
73 }
74
75 const result: Array<[KeyValue<number, GetValue<T>>, number]> = []
76
77 // Decide visibility and remove zero support only after the whole batch.
78 // Cancelling changes must retain the same entry and output key.
79 for (const hashedValue of updatedBuckets) {
80 const bucket = this.#values.get(hashedValue)!
81 let retained = 0
82 for (const entry of bucket) {
83 const [, row, multiplicity, wasVisible] = entry
84 const visible = multiplicity > 0
85 if (visible !== wasVisible) result.push([row, visible ? 1 : -1])
86 entry[3] = visible
87 if (multiplicity !== 0) bucket[retained++] = entry
88 }
89 bucket.length = retained
90 if (retained === 0) this.#values.delete(hashedValue)
91 }
92
93 if (result.length > 0) {
94 this.output.sendData(new MultiSet(result))
95 }
96 }
97}
98
99/**
100 * Removes duplicate values
101 */
102export function distinct<T extends KeyValue<unknown, unknown>>(
103 by?: (value: T) => unknown,
104) {
105 return unaryOperator(DistinctOperator<T>, by)
106}