451 lines
11 KiB
JavaScript
451 lines
11 KiB
JavaScript
import {
|
||
filter_default,
|
||
forEach_default,
|
||
isUndefined_default,
|
||
keys_default,
|
||
reduce_default,
|
||
union_default,
|
||
values_default
|
||
} from "./chunk-ZZTYOBSU.mjs";
|
||
import {
|
||
isEmpty_default
|
||
} from "./chunk-PSZZOCOG.mjs";
|
||
import {
|
||
constant_default,
|
||
isFunction_default
|
||
} from "./chunk-PEQZQI46.mjs";
|
||
import {
|
||
__name
|
||
} from "./chunk-DLQEHMXD.mjs";
|
||
|
||
// ../../node_modules/.pnpm/dagre-d3-es@7.0.11/node_modules/dagre-d3-es/src/graphlib/graph.js
|
||
var DEFAULT_EDGE_NAME = "\0";
|
||
var GRAPH_NODE = "\0";
|
||
var EDGE_KEY_DELIM = "";
|
||
var Graph = class {
|
||
static {
|
||
__name(this, "Graph");
|
||
}
|
||
constructor(opts = {}) {
|
||
this._isDirected = Object.prototype.hasOwnProperty.call(opts, "directed") ? opts.directed : true;
|
||
this._isMultigraph = Object.prototype.hasOwnProperty.call(opts, "multigraph") ? opts.multigraph : false;
|
||
this._isCompound = Object.prototype.hasOwnProperty.call(opts, "compound") ? opts.compound : false;
|
||
this._label = void 0;
|
||
this._defaultNodeLabelFn = constant_default(void 0);
|
||
this._defaultEdgeLabelFn = constant_default(void 0);
|
||
this._nodes = {};
|
||
if (this._isCompound) {
|
||
this._parent = {};
|
||
this._children = {};
|
||
this._children[GRAPH_NODE] = {};
|
||
}
|
||
this._in = {};
|
||
this._preds = {};
|
||
this._out = {};
|
||
this._sucs = {};
|
||
this._edgeObjs = {};
|
||
this._edgeLabels = {};
|
||
}
|
||
/* === Graph functions ========= */
|
||
isDirected() {
|
||
return this._isDirected;
|
||
}
|
||
isMultigraph() {
|
||
return this._isMultigraph;
|
||
}
|
||
isCompound() {
|
||
return this._isCompound;
|
||
}
|
||
setGraph(label) {
|
||
this._label = label;
|
||
return this;
|
||
}
|
||
graph() {
|
||
return this._label;
|
||
}
|
||
/* === Node functions ========== */
|
||
setDefaultNodeLabel(newDefault) {
|
||
if (!isFunction_default(newDefault)) {
|
||
newDefault = constant_default(newDefault);
|
||
}
|
||
this._defaultNodeLabelFn = newDefault;
|
||
return this;
|
||
}
|
||
nodeCount() {
|
||
return this._nodeCount;
|
||
}
|
||
nodes() {
|
||
return keys_default(this._nodes);
|
||
}
|
||
sources() {
|
||
var self = this;
|
||
return filter_default(this.nodes(), function(v) {
|
||
return isEmpty_default(self._in[v]);
|
||
});
|
||
}
|
||
sinks() {
|
||
var self = this;
|
||
return filter_default(this.nodes(), function(v) {
|
||
return isEmpty_default(self._out[v]);
|
||
});
|
||
}
|
||
setNodes(vs, value) {
|
||
var args = arguments;
|
||
var self = this;
|
||
forEach_default(vs, function(v) {
|
||
if (args.length > 1) {
|
||
self.setNode(v, value);
|
||
} else {
|
||
self.setNode(v);
|
||
}
|
||
});
|
||
return this;
|
||
}
|
||
setNode(v, value) {
|
||
if (Object.prototype.hasOwnProperty.call(this._nodes, v)) {
|
||
if (arguments.length > 1) {
|
||
this._nodes[v] = value;
|
||
}
|
||
return this;
|
||
}
|
||
this._nodes[v] = arguments.length > 1 ? value : this._defaultNodeLabelFn(v);
|
||
if (this._isCompound) {
|
||
this._parent[v] = GRAPH_NODE;
|
||
this._children[v] = {};
|
||
this._children[GRAPH_NODE][v] = true;
|
||
}
|
||
this._in[v] = {};
|
||
this._preds[v] = {};
|
||
this._out[v] = {};
|
||
this._sucs[v] = {};
|
||
++this._nodeCount;
|
||
return this;
|
||
}
|
||
node(v) {
|
||
return this._nodes[v];
|
||
}
|
||
hasNode(v) {
|
||
return Object.prototype.hasOwnProperty.call(this._nodes, v);
|
||
}
|
||
removeNode(v) {
|
||
if (Object.prototype.hasOwnProperty.call(this._nodes, v)) {
|
||
var removeEdge = /* @__PURE__ */ __name((e) => this.removeEdge(this._edgeObjs[e]), "removeEdge");
|
||
delete this._nodes[v];
|
||
if (this._isCompound) {
|
||
this._removeFromParentsChildList(v);
|
||
delete this._parent[v];
|
||
forEach_default(this.children(v), (child) => {
|
||
this.setParent(child);
|
||
});
|
||
delete this._children[v];
|
||
}
|
||
forEach_default(keys_default(this._in[v]), removeEdge);
|
||
delete this._in[v];
|
||
delete this._preds[v];
|
||
forEach_default(keys_default(this._out[v]), removeEdge);
|
||
delete this._out[v];
|
||
delete this._sucs[v];
|
||
--this._nodeCount;
|
||
}
|
||
return this;
|
||
}
|
||
setParent(v, parent) {
|
||
if (!this._isCompound) {
|
||
throw new Error("Cannot set parent in a non-compound graph");
|
||
}
|
||
if (isUndefined_default(parent)) {
|
||
parent = GRAPH_NODE;
|
||
} else {
|
||
parent += "";
|
||
for (var ancestor = parent; !isUndefined_default(ancestor); ancestor = this.parent(ancestor)) {
|
||
if (ancestor === v) {
|
||
throw new Error("Setting " + parent + " as parent of " + v + " would create a cycle");
|
||
}
|
||
}
|
||
this.setNode(parent);
|
||
}
|
||
this.setNode(v);
|
||
this._removeFromParentsChildList(v);
|
||
this._parent[v] = parent;
|
||
this._children[parent][v] = true;
|
||
return this;
|
||
}
|
||
_removeFromParentsChildList(v) {
|
||
delete this._children[this._parent[v]][v];
|
||
}
|
||
parent(v) {
|
||
if (this._isCompound) {
|
||
var parent = this._parent[v];
|
||
if (parent !== GRAPH_NODE) {
|
||
return parent;
|
||
}
|
||
}
|
||
}
|
||
children(v) {
|
||
if (isUndefined_default(v)) {
|
||
v = GRAPH_NODE;
|
||
}
|
||
if (this._isCompound) {
|
||
var children = this._children[v];
|
||
if (children) {
|
||
return keys_default(children);
|
||
}
|
||
} else if (v === GRAPH_NODE) {
|
||
return this.nodes();
|
||
} else if (this.hasNode(v)) {
|
||
return [];
|
||
}
|
||
}
|
||
predecessors(v) {
|
||
var predsV = this._preds[v];
|
||
if (predsV) {
|
||
return keys_default(predsV);
|
||
}
|
||
}
|
||
successors(v) {
|
||
var sucsV = this._sucs[v];
|
||
if (sucsV) {
|
||
return keys_default(sucsV);
|
||
}
|
||
}
|
||
neighbors(v) {
|
||
var preds = this.predecessors(v);
|
||
if (preds) {
|
||
return union_default(preds, this.successors(v));
|
||
}
|
||
}
|
||
isLeaf(v) {
|
||
var neighbors;
|
||
if (this.isDirected()) {
|
||
neighbors = this.successors(v);
|
||
} else {
|
||
neighbors = this.neighbors(v);
|
||
}
|
||
return neighbors.length === 0;
|
||
}
|
||
filterNodes(filter) {
|
||
var copy = new this.constructor({
|
||
directed: this._isDirected,
|
||
multigraph: this._isMultigraph,
|
||
compound: this._isCompound
|
||
});
|
||
copy.setGraph(this.graph());
|
||
var self = this;
|
||
forEach_default(this._nodes, function(value, v) {
|
||
if (filter(v)) {
|
||
copy.setNode(v, value);
|
||
}
|
||
});
|
||
forEach_default(this._edgeObjs, function(e) {
|
||
if (copy.hasNode(e.v) && copy.hasNode(e.w)) {
|
||
copy.setEdge(e, self.edge(e));
|
||
}
|
||
});
|
||
var parents = {};
|
||
function findParent(v) {
|
||
var parent = self.parent(v);
|
||
if (parent === void 0 || copy.hasNode(parent)) {
|
||
parents[v] = parent;
|
||
return parent;
|
||
} else if (parent in parents) {
|
||
return parents[parent];
|
||
} else {
|
||
return findParent(parent);
|
||
}
|
||
}
|
||
__name(findParent, "findParent");
|
||
if (this._isCompound) {
|
||
forEach_default(copy.nodes(), function(v) {
|
||
copy.setParent(v, findParent(v));
|
||
});
|
||
}
|
||
return copy;
|
||
}
|
||
/* === Edge functions ========== */
|
||
setDefaultEdgeLabel(newDefault) {
|
||
if (!isFunction_default(newDefault)) {
|
||
newDefault = constant_default(newDefault);
|
||
}
|
||
this._defaultEdgeLabelFn = newDefault;
|
||
return this;
|
||
}
|
||
edgeCount() {
|
||
return this._edgeCount;
|
||
}
|
||
edges() {
|
||
return values_default(this._edgeObjs);
|
||
}
|
||
setPath(vs, value) {
|
||
var self = this;
|
||
var args = arguments;
|
||
reduce_default(vs, function(v, w) {
|
||
if (args.length > 1) {
|
||
self.setEdge(v, w, value);
|
||
} else {
|
||
self.setEdge(v, w);
|
||
}
|
||
return w;
|
||
});
|
||
return this;
|
||
}
|
||
/*
|
||
* setEdge(v, w, [value, [name]])
|
||
* setEdge({ v, w, [name] }, [value])
|
||
*/
|
||
setEdge() {
|
||
var v, w, name, value;
|
||
var valueSpecified = false;
|
||
var arg0 = arguments[0];
|
||
if (typeof arg0 === "object" && arg0 !== null && "v" in arg0) {
|
||
v = arg0.v;
|
||
w = arg0.w;
|
||
name = arg0.name;
|
||
if (arguments.length === 2) {
|
||
value = arguments[1];
|
||
valueSpecified = true;
|
||
}
|
||
} else {
|
||
v = arg0;
|
||
w = arguments[1];
|
||
name = arguments[3];
|
||
if (arguments.length > 2) {
|
||
value = arguments[2];
|
||
valueSpecified = true;
|
||
}
|
||
}
|
||
v = "" + v;
|
||
w = "" + w;
|
||
if (!isUndefined_default(name)) {
|
||
name = "" + name;
|
||
}
|
||
var e = edgeArgsToId(this._isDirected, v, w, name);
|
||
if (Object.prototype.hasOwnProperty.call(this._edgeLabels, e)) {
|
||
if (valueSpecified) {
|
||
this._edgeLabels[e] = value;
|
||
}
|
||
return this;
|
||
}
|
||
if (!isUndefined_default(name) && !this._isMultigraph) {
|
||
throw new Error("Cannot set a named edge when isMultigraph = false");
|
||
}
|
||
this.setNode(v);
|
||
this.setNode(w);
|
||
this._edgeLabels[e] = valueSpecified ? value : this._defaultEdgeLabelFn(v, w, name);
|
||
var edgeObj = edgeArgsToObj(this._isDirected, v, w, name);
|
||
v = edgeObj.v;
|
||
w = edgeObj.w;
|
||
Object.freeze(edgeObj);
|
||
this._edgeObjs[e] = edgeObj;
|
||
incrementOrInitEntry(this._preds[w], v);
|
||
incrementOrInitEntry(this._sucs[v], w);
|
||
this._in[w][e] = edgeObj;
|
||
this._out[v][e] = edgeObj;
|
||
this._edgeCount++;
|
||
return this;
|
||
}
|
||
edge(v, w, name) {
|
||
var e = arguments.length === 1 ? edgeObjToId(this._isDirected, arguments[0]) : edgeArgsToId(this._isDirected, v, w, name);
|
||
return this._edgeLabels[e];
|
||
}
|
||
hasEdge(v, w, name) {
|
||
var e = arguments.length === 1 ? edgeObjToId(this._isDirected, arguments[0]) : edgeArgsToId(this._isDirected, v, w, name);
|
||
return Object.prototype.hasOwnProperty.call(this._edgeLabels, e);
|
||
}
|
||
removeEdge(v, w, name) {
|
||
var e = arguments.length === 1 ? edgeObjToId(this._isDirected, arguments[0]) : edgeArgsToId(this._isDirected, v, w, name);
|
||
var edge = this._edgeObjs[e];
|
||
if (edge) {
|
||
v = edge.v;
|
||
w = edge.w;
|
||
delete this._edgeLabels[e];
|
||
delete this._edgeObjs[e];
|
||
decrementOrRemoveEntry(this._preds[w], v);
|
||
decrementOrRemoveEntry(this._sucs[v], w);
|
||
delete this._in[w][e];
|
||
delete this._out[v][e];
|
||
this._edgeCount--;
|
||
}
|
||
return this;
|
||
}
|
||
inEdges(v, u) {
|
||
var inV = this._in[v];
|
||
if (inV) {
|
||
var edges = values_default(inV);
|
||
if (!u) {
|
||
return edges;
|
||
}
|
||
return filter_default(edges, function(edge) {
|
||
return edge.v === u;
|
||
});
|
||
}
|
||
}
|
||
outEdges(v, w) {
|
||
var outV = this._out[v];
|
||
if (outV) {
|
||
var edges = values_default(outV);
|
||
if (!w) {
|
||
return edges;
|
||
}
|
||
return filter_default(edges, function(edge) {
|
||
return edge.w === w;
|
||
});
|
||
}
|
||
}
|
||
nodeEdges(v, w) {
|
||
var inEdges = this.inEdges(v, w);
|
||
if (inEdges) {
|
||
return inEdges.concat(this.outEdges(v, w));
|
||
}
|
||
}
|
||
};
|
||
Graph.prototype._nodeCount = 0;
|
||
Graph.prototype._edgeCount = 0;
|
||
function incrementOrInitEntry(map, k) {
|
||
if (map[k]) {
|
||
map[k]++;
|
||
} else {
|
||
map[k] = 1;
|
||
}
|
||
}
|
||
__name(incrementOrInitEntry, "incrementOrInitEntry");
|
||
function decrementOrRemoveEntry(map, k) {
|
||
if (!--map[k]) {
|
||
delete map[k];
|
||
}
|
||
}
|
||
__name(decrementOrRemoveEntry, "decrementOrRemoveEntry");
|
||
function edgeArgsToId(isDirected, v_, w_, name) {
|
||
var v = "" + v_;
|
||
var w = "" + w_;
|
||
if (!isDirected && v > w) {
|
||
var tmp = v;
|
||
v = w;
|
||
w = tmp;
|
||
}
|
||
return v + EDGE_KEY_DELIM + w + EDGE_KEY_DELIM + (isUndefined_default(name) ? DEFAULT_EDGE_NAME : name);
|
||
}
|
||
__name(edgeArgsToId, "edgeArgsToId");
|
||
function edgeArgsToObj(isDirected, v_, w_, name) {
|
||
var v = "" + v_;
|
||
var w = "" + w_;
|
||
if (!isDirected && v > w) {
|
||
var tmp = v;
|
||
v = w;
|
||
w = tmp;
|
||
}
|
||
var edgeObj = { v, w };
|
||
if (name) {
|
||
edgeObj.name = name;
|
||
}
|
||
return edgeObj;
|
||
}
|
||
__name(edgeArgsToObj, "edgeArgsToObj");
|
||
function edgeObjToId(isDirected, edgeObj) {
|
||
return edgeArgsToId(isDirected, edgeObj.v, edgeObj.w, edgeObj.name);
|
||
}
|
||
__name(edgeObjToId, "edgeObjToId");
|
||
|
||
export {
|
||
Graph
|
||
};
|