@itwin/ecschema-metadata
Version:
ECObjects core concepts in typescript
84 lines • 2.54 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
// Following https://github.com/dagrejs/graphlib/blob/master/lib/graph.js
/** @internal */
export class Graph {
_edgeKeyDelim = "\x01";
_label = "";
_nodeCount = 0;
_edgeCount = 0;
_nodes;
_edgeObjs;
_edgeLabels;
_outEdges;
constructor() {
this._nodes = {};
this._edgeObjs = {};
this._edgeLabels = {};
this._outEdges = {};
}
setGraph = (label) => {
this._label = label;
return this;
};
graph = () => {
return this._label;
};
nodeCount = () => {
return this._nodeCount;
};
nodes = () => {
return Object.keys(this._nodes);
};
setNode = (nodeKey, nodeValue) => {
if (nodeKey in this._nodes) {
this._nodes[nodeKey] = nodeValue;
return;
}
this._nodes[nodeKey] = nodeValue;
this._outEdges[nodeKey] = {};
++this._nodeCount;
};
node = (nodeKey) => {
return this._nodes[nodeKey];
};
hasNode = (nodeKey) => {
return nodeKey in this._nodes;
};
edgeCount = () => {
return this._edgeCount;
};
edges = () => {
return Object.values(this._edgeObjs);
};
setEdge = (v, w, value) => {
const edgeId = v + this._edgeKeyDelim + w + this._edgeKeyDelim;
if (edgeId in this._edgeLabels) {
// this._edgeLabels[edgeId] = value;
// Update exponent, specific to this graph's use case
this._edgeLabels[edgeId].exponent += value.exponent;
return;
}
this._edgeLabels[edgeId] = value;
const edgeObj = {
v,
w,
};
this._edgeObjs[edgeId] = edgeObj;
// setNode should have ran first, so this.outEdges[v] shouldn't be undefined
this._outEdges[v][edgeId] = edgeObj;
this._edgeCount++;
};
edge = (v, w) => {
const edgeId = v + this._edgeKeyDelim + w + this._edgeKeyDelim;
return this._edgeLabels[edgeId];
};
outEdges = (v) => {
const outV = this._outEdges[v];
const edges = Object.values(outV);
return edges;
};
}
//# sourceMappingURL=Graph.js.map