UNPKG

@rimbu/graph

Version:

Immutable Graph data structures for TypeScript

72 lines 2.37 kB
import { Token } from '@rimbu/base'; import { OptLazy, OptLazyOr } from '@rimbu/common'; import { Stream } from '@rimbu/stream'; import { GraphEmptyBase } from '../../common/index.mjs'; export class ValuedGraphEmpty extends GraphEmptyBase { constructor(isDirected, context) { super(); this.isDirected = isDirected; this.context = context; } get linkMap() { return this.context.linkMapContext.empty(); } getValue(node1, node2, otherwise) { return OptLazy(otherwise); } getConnectionsFrom() { return this.context.linkConnectionsContext.empty(); } addNode(node) { return this.context.createNonEmpty(this.linkMap.context.of([ node, this.context.linkConnectionsContext.empty(), ]), 0); } addNodes(nodes) { const emptyConnections = this.context.linkConnectionsContext.empty(); const linkMap = this.context.linkMapContext.from(Stream.from(nodes).map((node) => [node, emptyConnections])); if (!linkMap.nonEmpty()) return this; return this.context.createNonEmpty(linkMap, 0); } connect(node1, node2, value) { const linkMap = this.context.linkMapContext.of([ node1, this.context.linkConnectionsContext.of([node2, value]), ]); if (node1 === node2) return this.context.createNonEmpty(linkMap, 1); const linkConnections = this.isDirected ? this.context.linkConnectionsContext.empty() : this.context.linkConnectionsContext.of([node1, value]); return this.context.createNonEmpty(linkMap.set(node2, linkConnections), 1); } connectAll(links) { return this.context.from(links); } modifyAt(node1, node2, options) { if (undefined === options.ifNew) return this; const newValue = OptLazyOr(options.ifNew, Token); if (Token === newValue) return this; return this.connect(node1, node2, newValue); } mapValues() { return this; } toString() { return `${this.context.typeTag}()`; } toJSON() { return { dataType: this.context.typeTag, value: [], }; } toBuilder() { return this.context.builder(); } } //# sourceMappingURL=empty.mjs.map