@rimbu/graph
Version:
Immutable Graph data structures for TypeScript
57 lines • 2.29 kB
JavaScript
import { Reducer } from '@rimbu/stream';
import { isEmptyStreamSourceInstance } from '@rimbu/stream/custom';
import { ValuedGraphBuilder, ValuedGraphEmpty, ValuedGraphNonEmpty, } from '@rimbu/graph/custom';
export class ValuedGraphContext {
constructor(isDirected, typeTag, linkMapContext, linkConnectionsContext) {
this.isDirected = isDirected;
this.typeTag = typeTag;
this.linkMapContext = linkMapContext;
this.linkConnectionsContext = linkConnectionsContext;
this.empty = () => {
return this._empty;
};
this.from = (...sources) => {
let builder = this.builder();
let i = -1;
const length = sources.length;
while (++i < length) {
const source = sources[i];
if (isEmptyStreamSourceInstance(source))
continue;
if (builder.isEmpty &&
this.isNonEmptyInstance(source) &&
source.context === this) {
if (i === length - 1)
return source;
builder = source.toBuilder();
continue;
}
builder.addGraphElements(source);
}
return builder.build();
};
this.of = (...values) => {
return this.from(values).assumeNonEmpty();
};
this.builder = () => {
return new ValuedGraphBuilder(this.isDirected, this);
};
this.reducer = (source) => {
return Reducer.create(() => undefined === source ? this.builder() : this.from(source).toBuilder(), (builder, entry) => {
builder.addGraphElement(entry);
return builder;
}, (builder) => builder.build());
};
this._empty = Object.freeze(new ValuedGraphEmpty(isDirected, this));
}
isNonEmptyInstance(source) {
return source instanceof ValuedGraphNonEmpty;
}
createBuilder(source) {
return new ValuedGraphBuilder(this.isDirected, this, source);
}
createNonEmpty(linkMap, connectionSize) {
return new ValuedGraphNonEmpty(this.isDirected, this, linkMap, connectionSize);
}
}
//# sourceMappingURL=context.mjs.map