/** * Specifies the expected number of nodes in the graph. * * @throws IllegalArgumentException if {@code expectedNodeCount} is negative */ public ValueGraphBuilder<N, V> expectedNodeCount(int expectedNodeCount) { this.expectedNodeCount = Optional.of(checkNonNegative(expectedNodeCount)); return this; }
/** * Specifies the expected number of nodes in the network. * * @throws IllegalArgumentException if {@code expectedNodeCount} is negative */ public NetworkBuilder<N, E> expectedNodeCount(int expectedNodeCount) { this.expectedNodeCount = Optional.of(checkNonNegative(expectedNodeCount)); return this; }
/** * Specifies the expected number of edges in the network. * * @throws IllegalArgumentException if {@code expectedEdgeCount} is negative */ public NetworkBuilder<N, E> expectedEdgeCount(int expectedEdgeCount) { this.expectedEdgeCount = Optional.of(checkNonNegative(expectedEdgeCount)); return this; }
/** * Specifies the expected number of nodes in the graph. * * @throws IllegalArgumentException if {@code expectedNodeCount} is negative */ public GraphBuilder<N> expectedNodeCount(int expectedNodeCount) { this.expectedNodeCount = Optional.of(checkNonNegative(expectedNodeCount)); return this; }
@SuppressWarnings("unchecked") @Override public void removePredecessor(N node) { Object previousValue = adjacentNodeValues.get(node); if (previousValue == PRED) { adjacentNodeValues.remove(node); checkNonNegative(--predecessorCount); } else if (previousValue instanceof PredAndSucc) { adjacentNodeValues.put((N) node, ((PredAndSucc) previousValue).successorValue); checkNonNegative(--predecessorCount); } }
private DirectedGraphConnections( Map<N, Object> adjacentNodeValues, int predecessorCount, int successorCount) { this.adjacentNodeValues = checkNotNull(adjacentNodeValues); this.predecessorCount = checkNonNegative(predecessorCount); this.successorCount = checkNonNegative(successorCount); checkState( predecessorCount <= adjacentNodeValues.size() && successorCount <= adjacentNodeValues.size()); }
@Override public N removeInEdge(E edge, boolean isSelfLoop) { if (isSelfLoop) { checkNonNegative(--selfLoopCount); } N previousNode = inEdgeMap.remove(edge); return checkNotNull(previousNode); }
@SuppressWarnings("unchecked") @Override public V removeSuccessor(Object node) { Object previousValue = adjacentNodeValues.get(node); if (previousValue == null || previousValue == PRED) { return null; } else if (previousValue instanceof PredAndSucc) { adjacentNodeValues.put((N) node, PRED); checkNonNegative(--successorCount); return (V) ((PredAndSucc) previousValue).successorValue; } else { // successor adjacentNodeValues.remove(node); checkNonNegative(--successorCount); return (V) previousValue; } }
protected AbstractDirectedNetworkConnections( Map<E, N> inEdgeMap, Map<E, N> outEdgeMap, int selfLoopCount) { this.inEdgeMap = checkNotNull(inEdgeMap); this.outEdgeMap = checkNotNull(outEdgeMap); this.selfLoopCount = checkNonNegative(selfLoopCount); checkState(selfLoopCount <= inEdgeMap.size() && selfLoopCount <= outEdgeMap.size()); }
/** * Specifies the expected number of nodes in the graph. * * @throws IllegalArgumentException if {@code expectedNodeCount} is negative */ public GraphBuilder<N> expectedNodeCount(int expectedNodeCount) { this.expectedNodeCount = Optional.of(checkNonNegative(expectedNodeCount)); return this; }
/** * Specifies the expected number of nodes in the graph. * * @throws IllegalArgumentException if {@code expectedNodeCount} is negative */ public ValueGraphBuilder<N, V> expectedNodeCount(int expectedNodeCount) { this.expectedNodeCount = Optional.of(checkNonNegative(expectedNodeCount)); return this; }
/** * Specifies the expected number of nodes in the network. * * @throws IllegalArgumentException if {@code expectedNodeCount} is negative */ public NetworkBuilder<N, E> expectedNodeCount(int expectedNodeCount) { this.expectedNodeCount = Optional.of(checkNonNegative(expectedNodeCount)); return this; }
/** * Specifies the expected number of edges in the network. * * @throws IllegalArgumentException if {@code expectedEdgeCount} is negative */ public NetworkBuilder<N, E> expectedEdgeCount(int expectedEdgeCount) { this.expectedEdgeCount = Optional.of(checkNonNegative(expectedEdgeCount)); return this; }
@SuppressWarnings("unchecked") @Override public void removePredecessor(N node) { Object previousValue = adjacentNodeValues.get(node); if (previousValue == PRED) { adjacentNodeValues.remove(node); checkNonNegative(--predecessorCount); } else if (previousValue instanceof PredAndSucc) { adjacentNodeValues.put((N) node, ((PredAndSucc) previousValue).successorValue); checkNonNegative(--predecessorCount); } }
private DirectedGraphConnections( Map<N, Object> adjacentNodeValues, int predecessorCount, int successorCount) { this.adjacentNodeValues = checkNotNull(adjacentNodeValues); this.predecessorCount = checkNonNegative(predecessorCount); this.successorCount = checkNonNegative(successorCount); checkState( predecessorCount <= adjacentNodeValues.size() && successorCount <= adjacentNodeValues.size()); }
@Override public N removeInEdge(E edge, boolean isSelfLoop) { if (isSelfLoop) { checkNonNegative(--selfLoopCount); } N previousNode = inEdgeMap.remove(edge); return checkNotNull(previousNode); }
protected AbstractDirectedNetworkConnections( Map<E, N> inEdgeMap, Map<E, N> outEdgeMap, int selfLoopCount) { this.inEdgeMap = checkNotNull(inEdgeMap); this.outEdgeMap = checkNotNull(outEdgeMap); this.selfLoopCount = checkNonNegative(selfLoopCount); checkState(selfLoopCount <= inEdgeMap.size() && selfLoopCount <= outEdgeMap.size()); }
@Override @CanIgnoreReturnValue public V removeEdge(N nodeU, N nodeV) { checkNotNull(nodeU, "nodeU"); checkNotNull(nodeV, "nodeV"); GraphConnections<N, V> connectionsU = nodeConnections.get(nodeU); GraphConnections<N, V> connectionsV = nodeConnections.get(nodeV); if (connectionsU == null || connectionsV == null) { return null; } V previousValue = connectionsU.removeSuccessor(nodeV); if (previousValue != null) { connectionsV.removePredecessor(nodeU); checkNonNegative(--edgeCount); } return previousValue; }
/** * Constructs a graph with the properties specified in {@code builder}, initialized with the given * node map. */ ConfigurableValueGraph( AbstractGraphBuilder<? super N> builder, Map<N, GraphConnections<N, V>> nodeConnections, long edgeCount) { this.isDirected = builder.directed; this.allowsSelfLoops = builder.allowsSelfLoops; this.nodeOrder = builder.nodeOrder.cast(); // Prefer the heavier "MapRetrievalCache" for nodes if lookup is expensive. this.nodeConnections = (nodeConnections instanceof TreeMap) ? new MapRetrievalCache<N, GraphConnections<N, V>>(nodeConnections) : new MapIteratorCache<N, GraphConnections<N, V>>(nodeConnections); this.edgeCount = checkNonNegative(edgeCount); }
@Override @CanIgnoreReturnValue public boolean removeNode(N node) { checkNotNull(node, "node"); GraphConnections<N, V> connections = nodeConnections.get(node); if (connections == null) { return false; } if (allowsSelfLoops()) { // Remove self-loop (if any) first, so we don't get CME while removing incident edges. if (connections.removeSuccessor(node) != null) { connections.removePredecessor(node); --edgeCount; } } for (N successor : connections.successors()) { nodeConnections.getWithoutCaching(successor).removePredecessor(node); --edgeCount; } if (isDirected()) { // In undirected graphs, the successor and predecessor sets are equal. for (N predecessor : connections.predecessors()) { checkState(nodeConnections.getWithoutCaching(predecessor).removeSuccessor(node) != null); --edgeCount; } } nodeConnections.remove(node); checkNonNegative(edgeCount); return true; }