@rimbu/graph
Version:
Immutable Graph data structures for TypeScript
56 lines • 1.85 kB
JavaScript
import { Stream } from '@rimbu/stream';
import { GraphEmptyBase } from '../../common/index.mjs';
export class GraphEmpty extends GraphEmptyBase {
constructor(isDirected, context) {
super();
this.isDirected = isDirected;
this.context = context;
}
get linkMap() {
return this.context.linkMapContext.empty();
}
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) {
const linkMap = this.context.linkMapContext.of([
node1,
this.context.linkConnectionsContext.of(node2),
]);
if (node1 === node2)
return this.context.createNonEmpty(linkMap, 1);
const linkConnections = this.isDirected
? this.context.linkConnectionsContext.empty()
: this.context.linkConnectionsContext.of(node1);
return this.context.createNonEmpty(linkMap.set(node2, linkConnections), 1);
}
connectAll(links) {
return this.context.from(links);
}
toString() {
return `${this.context.typeTag}()`;
}
toJSON() {
return {
dataType: this.context.typeTag,
value: [],
};
}
toBuilder() {
return this.context.builder();
}
}
//# sourceMappingURL=empty.mjs.map