UNPKG

graph-builder

Version:

A graph builder library for modeling abstract graph structures.

117 lines 4.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const AbstractValueGraph_1 = require("./AbstractValueGraph"); const GraphConstants_1 = require("./GraphConstants"); const ImmutableSet_1 = require("../collect/ImmutableSet"); /* * Copyright (C) 2016 The Guava Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * Modifications (C) 2019 Ben Sorohan */ /** * Configurable implementation of {@link ValueGraph} that supports the options supplied by {@link * AbstractGraphBuilder}. * * <p>This class maintains a map of nodes to {@link GraphConnections}. * * <p>Collection-returning accessors return unmodifiable views: the view returned will reflect * changes to the graph (if the graph is mutable) but may not be modified by the user. * * <p>The time complexity of all collection-returning accessors is O(1), since views are returned. */ class ConfigurableValueGraph extends AbstractValueGraph_1.AbstractValueGraph { /** * Constructs a graph with the properties specified in `builder`, initialized with the given * node map. */ constructor(builder, nodeConnections, edgeCount) { super(); this.isDirectedValue = builder.directedValue; this.allowsSelfLoopsValue = builder.allowsSelfLoopsValue; this.nodeOrderValue = builder.nodeOrderValue; // @todo: Prefer the heavier "MapRetrievalCache" for nodes if lookup is expensive. this.nodeConnections = // (nodeConnections instanceof TreeMap) // ? new MapRetrievalCache<N, GraphConnections<N, V>>(nodeConnections) : new Map(nodeConnections); if (edgeCount < 0) { throw new Error('Negative edge count'); } this.edgeCountValue = edgeCount; } /** Constructs a graph with the properties specified in `builder`. */ static from(builder) { return new ConfigurableValueGraph(builder, builder.nodeOrderValue.createMap(builder.expectedNodeCountValue || GraphConstants_1.GraphConstants.DEFAULT_NODE_COUNT), 0); } nodes() { return ImmutableSet_1.ImmutableSet.fromIterable(this.nodeConnections.keys()); } isDirected() { return this.isDirectedValue; } allowsSelfLoops() { return this.allowsSelfLoopsValue; } nodeOrder() { return this.nodeOrderValue; } adjacentNodes(node) { return this.checkedConnections(node).adjacentNodes(); } predecessors(node) { return this.checkedConnections(node).predecessors(); } successors(node) { return this.checkedConnections(node).successors(); } hasEdge(nodeU, nodeV) { return this.hasEdge_internal(nodeU, nodeV); } hasEdgeConnectingEndpoints(endpoints) { return this.isOrderingCompatible(endpoints) && this.hasEdge_internal(endpoints.nodeU, endpoints.nodeV); } edgeValueOrDefault(nodeU, nodeV, defaultValue) { return this.edgeValueOrDefault_internal(nodeU, nodeV, defaultValue); } edgeValueConnectingEndpointsOrDefault(endpoints, defaultValue) { this.validateEndpoints(endpoints); return this.edgeValueOrDefault_internal(endpoints.nodeU, endpoints.nodeV, defaultValue); } edgeCount() { return this.edgeCountValue; } checkedConnections(node) { const connections = this.nodeConnections.get(node); if (connections === undefined) { throw new Error("Node " + node + " is not an element of this graph."); } return connections; } containsNode(node) { return this.nodeConnections.has(node); } hasEdge_internal(nodeU, nodeV) { const connectionsU = this.nodeConnections.get(nodeU); return (connectionsU !== undefined) && connectionsU.successors().has(nodeV); } edgeValueOrDefault_internal(nodeU, nodeV, defaultValue) { const connectionsU = this.nodeConnections.get(nodeU); const value = (connectionsU === undefined) ? undefined : connectionsU.value(nodeV); return value === undefined ? defaultValue : value; } } exports.ConfigurableValueGraph = ConfigurableValueGraph; //# sourceMappingURL=ConfigurableValueGraph.js.map