UNPKG

@electric-sql/d2mini

Version:

D2Mini is a minimal implementation of Differential Dataflow for performing in-memory incremental view maintenance.

35 lines (32 loc) 1 kB
import { IStreamBuilder, PipedOperator } from '../types.js' import { DifferenceStreamWriter } from '../graph.js' import { StreamBuilder } from '../d2.js' import { LinearUnaryOperator } from '../graph.js' import { MultiSet } from '../multiset.js' /** * Operator that negates the multiplicities in the input stream */ export class NegateOperator<T> extends LinearUnaryOperator<T, T> { inner(collection: MultiSet<T>): MultiSet<T> { return collection.negate() } } /** * Negates the multiplicities in the input stream */ export function negate<T>(): PipedOperator<T, T> { return (stream: IStreamBuilder<T>): IStreamBuilder<T> => { const output = new StreamBuilder<T>( stream.graph, new DifferenceStreamWriter<T>(), ) const operator = new NegateOperator<T>( stream.graph.getNextOperatorId(), stream.connectReader(), output.writer, ) stream.graph.addOperator(operator) stream.graph.addStream(output.connectReader()) return output } }