Open History to see the last commit
1import { DifferenceStreamWriter, LinearUnaryOperator } from "../graph.js"
2import { StreamBuilder } from "../d2.js"
3import type { IStreamBuilder, PipedOperator } from "../types.js"
4import type { MultiSet } from "../multiset.js"
5
6/**
7 * Operator that negates the multiplicities in the input stream
8 */
9export class NegateOperator<T> extends LinearUnaryOperator<T, T> {
10 inner(collection: MultiSet<T>): MultiSet<T> {
11 return collection.negate()
12 }
13}
14
15/**
16 * Negates the multiplicities in the input stream
17 */
18export function negate<T>(): PipedOperator<T, T> {
19 return (stream: IStreamBuilder<T>): IStreamBuilder<T> => {
20 const output = new StreamBuilder<T>(
21 stream.graph,
22 new DifferenceStreamWriter<T>()
23 )
24 const operator = new NegateOperator<T>(
25 stream.graph.getNextOperatorId(),
26 stream.connectReader(),
27 output.writer
28 )
29 stream.graph.addOperator(operator)
30 return output
31 }
32}