Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

negate.ts

33 lines982 Bytes

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 }
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 stream.graph.addStream(output.connectReader())
31 return output
32 }