|
| 1 | +"use strict"; |
| 2 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 | +exports.Graph = void 0; |
| 4 | +var invariant_js_1 = require("./invariant.js"); |
| 5 | +var Graph = /** @class */ (function () { |
| 6 | + function Graph() { |
| 7 | + /** |
| 8 | + * Contains all the nodes added to the graph. |
| 9 | + */ |
| 10 | + this.nodes = new Set(); |
| 11 | + /** |
| 12 | + * The adjacency list of the graph. |
| 13 | + */ |
| 14 | + this.edges = new Map(); |
| 15 | + /** |
| 16 | + * The weights of edges. |
| 17 | + * |
| 18 | + * Map<SourceNode, Map<TargetNode, EdgeWeight>> |
| 19 | + */ |
| 20 | + this.edgeWeights = new Map(); |
| 21 | + /** |
| 22 | + * Arbitrary properties of edges. |
| 23 | + * Map<SourceNode, Map<TargetNode, EdgeProperties>> |
| 24 | + */ |
| 25 | + this.edgeProperties = new Map(); |
| 26 | + } |
| 27 | + /** |
| 28 | + * Adds a node to the graph. |
| 29 | + * If node was already added, this function does nothing. |
| 30 | + * If node was not already added, this function sets up an empty adjacency list. |
| 31 | + */ |
| 32 | + Graph.prototype.addNode = function (node) { |
| 33 | + if (!this.nodes.has(node)) { |
| 34 | + this.nodes.add(node); |
| 35 | + } |
| 36 | + if (!this.edges.has(node)) { |
| 37 | + this.edges.set(node, new Set()); |
| 38 | + } |
| 39 | + return this; |
| 40 | + }; |
| 41 | + /** |
| 42 | + * Removes a node from the graph. |
| 43 | + * Also removes incoming and outgoing edges. |
| 44 | + */ |
| 45 | + Graph.prototype.removeNode = function (node) { |
| 46 | + // Remove outgoing edges (and signal that the node no longer exists). |
| 47 | + this.edges.delete(node); |
| 48 | + this.nodes.delete(node); |
| 49 | + // Remove ingoing edges |
| 50 | + for (var _i = 0, _a = this.edges.values(); _i < _a.length; _i++) { |
| 51 | + var adjacentNodes = _a[_i]; |
| 52 | + adjacentNodes.delete(node); |
| 53 | + } |
| 54 | + return this; |
| 55 | + }; |
| 56 | + /** |
| 57 | + * Gets the adjacent nodes set for the given node. |
| 58 | + */ |
| 59 | + Graph.prototype.adjacent = function (node) { |
| 60 | + return this.edges.get(node); |
| 61 | + }; |
| 62 | + /** |
| 63 | + * Sets the weight of the given edge. |
| 64 | + */ |
| 65 | + Graph.prototype.setEdgeWeight = function (source, target, weight) { |
| 66 | + if (!this.edgeWeights.has(source)) { |
| 67 | + this.edgeWeights.set(source, new Map()); |
| 68 | + } |
| 69 | + var weights = this.edgeWeights.get(source); |
| 70 | + (0, invariant_js_1.invariant)(weights); |
| 71 | + weights.set(target, weight); |
| 72 | + return this; |
| 73 | + }; |
| 74 | + /** |
| 75 | + * Gets the weight of the given edge or `1` if not set. |
| 76 | + */ |
| 77 | + Graph.prototype.getEdgeWeight = function (source, target) { |
| 78 | + var _a, _b; |
| 79 | + return (_b = (_a = this.edgeWeights.get(source)) === null || _a === void 0 ? void 0 : _a.get(target)) !== null && _b !== void 0 ? _b : 1; |
| 80 | + }; |
| 81 | + /** |
| 82 | + * Set the properties of the given edge. |
| 83 | + */ |
| 84 | + Graph.prototype.setEdgeProperties = function (source, target, props) { |
| 85 | + if (!this.edgeProperties.has(source)) { |
| 86 | + this.edgeProperties.set(source, new Map()); |
| 87 | + } |
| 88 | + var propsHolder = this.edgeProperties.get(source); |
| 89 | + (0, invariant_js_1.invariant)(propsHolder); |
| 90 | + propsHolder.set(target, props); |
| 91 | + return this; |
| 92 | + }; |
| 93 | + /** |
| 94 | + * Get the properties of the given edge or undefined if none are set. |
| 95 | + */ |
| 96 | + Graph.prototype.getEdgeProperties = function (source, target) { |
| 97 | + var _a; |
| 98 | + return (_a = this.edgeProperties.get(source)) === null || _a === void 0 ? void 0 : _a.get(target); |
| 99 | + }; |
| 100 | + /** |
| 101 | + * Adds an edge from the `source` node to `target` node. |
| 102 | + * This method will create the nodes if they were not already added. |
| 103 | + */ |
| 104 | + Graph.prototype.addEdge = function (source, target) { |
| 105 | + var opts = []; |
| 106 | + for (var _i = 2; _i < arguments.length; _i++) { |
| 107 | + opts[_i - 2] = arguments[_i]; |
| 108 | + } |
| 109 | + var weight = opts[0], linkProps = opts[1]; |
| 110 | + this.addNode(source); |
| 111 | + this.addNode(target); |
| 112 | + var adjacentNodes = this.adjacent(source); |
| 113 | + (0, invariant_js_1.invariant)(adjacentNodes); |
| 114 | + adjacentNodes.add(target); |
| 115 | + if (weight !== undefined) { |
| 116 | + this.setEdgeWeight(source, target, weight); |
| 117 | + } |
| 118 | + if (linkProps !== undefined) { |
| 119 | + this.setEdgeProperties(source, target, linkProps); |
| 120 | + } |
| 121 | + return this; |
| 122 | + }; |
| 123 | + /** |
| 124 | + * Removes the edge from the `source` node to `target` node. |
| 125 | + * Does not remove the nodes themselves. |
| 126 | + * Does nothing if the edge does not exist. |
| 127 | + */ |
| 128 | + Graph.prototype.removeEdge = function (source, target) { |
| 129 | + var _a, _b; |
| 130 | + (_a = this.edges.get(source)) === null || _a === void 0 ? void 0 : _a.delete(target); |
| 131 | + (_b = this.edgeProperties.get(source)) === null || _b === void 0 ? void 0 : _b.delete(target); |
| 132 | + return this; |
| 133 | + }; |
| 134 | + /** |
| 135 | + * Returns true if there is an edge from the `source` node to `target` node.. |
| 136 | + */ |
| 137 | + Graph.prototype.hasEdge = function (source, target) { |
| 138 | + var _a, _b; |
| 139 | + return (_b = (_a = this.edges.get(source)) === null || _a === void 0 ? void 0 : _a.has(target)) !== null && _b !== void 0 ? _b : false; |
| 140 | + }; |
| 141 | + return Graph; |
| 142 | +}()); |
| 143 | +exports.Graph = Graph; |
0 commit comments