private static <N, E> Map<E, N> getEdgeToReferenceNode(Network<N, E> network) { // ImmutableMap.Builder maintains the order of the elements as inserted, so the map will have // whatever ordering the network's edges do, so ImmutableSortedMap is unnecessary even if the // input edges are sorted. ImmutableMap.Builder<E, N> edgeToReferenceNode = ImmutableMap.builder(); for (E edge : network.edges()) { edgeToReferenceNode.put(edge, network.incidentNodes(edge).nodeU()); } return edgeToReferenceNode.build(); }
@Override public V apply(EndpointPair<N> edge) { return graph.edgeValueOrDefault(edge.nodeU(), edge.nodeV(), null); } };
@SuppressWarnings("unchecked") @Override public boolean contains(@Nullable Object obj) { if (!(obj instanceof EndpointPair)) { return false; } EndpointPair<?> endpointPair = (EndpointPair<?>) obj; return isOrderingCompatible(endpointPair) && nodes().contains(endpointPair.nodeU()) && successors((N) endpointPair.nodeU()).contains(endpointPair.nodeV()); } };
@Override public Set<E> adjacentEdges(E edge) { EndpointPair<N> endpointPair = incidentNodes(edge); // Verifies that edge is in this network. Set<E> endpointPairIncidentEdges = Sets.union(incidentEdges(endpointPair.nodeU()), incidentEdges(endpointPair.nodeV())); return Sets.difference(endpointPairIncidentEdges, ImmutableSet.of(edge)); }
@Override public boolean hasEdgeConnecting(EndpointPair<N> endpoints) { checkNotNull(endpoints); if (!isOrderingCompatible(endpoints)) { return false; } N nodeU = endpoints.nodeU(); N nodeV = endpoints.nodeV(); return nodes().contains(nodeU) && successors(nodeU).contains(nodeV); }
@Override public boolean hasEdgeConnecting(EndpointPair<N> endpoints) { checkNotNull(endpoints); return isOrderingCompatible(endpoints) && hasEdgeConnecting_internal(endpoints.nodeU(), endpoints.nodeV()); }
@Override public Set<E> edgesConnecting(EndpointPair<N> endpoints) { validateEndpoints(endpoints); return edgesConnecting(endpoints.nodeU(), endpoints.nodeV()); }
@Override public boolean putEdge(EndpointPair<N> endpoints) { validateEndpoints(endpoints); return putEdge(endpoints.nodeU(), endpoints.nodeV()); }
@Override public Optional<E> edgeConnecting(EndpointPair<N> endpoints) { validateEndpoints(endpoints); return edgeConnecting(endpoints.nodeU(), endpoints.nodeV()); }
@Override public boolean removeEdge(EndpointPair<N> endpoints) { validateEndpoints(endpoints); return removeEdge(endpoints.nodeU(), endpoints.nodeV()); } }
@Override public EndpointPair<N> incidentNodes(E edge) { EndpointPair<N> endpointPair = delegate().incidentNodes(edge); return EndpointPair.of(network, endpointPair.nodeV(), endpointPair.nodeU()); // transpose }
@Override @CanIgnoreReturnValue public V putEdgeValue(EndpointPair<N> endpoints, V value) { validateEndpoints(endpoints); return putEdgeValue(endpoints.nodeU(), endpoints.nodeV(), value); }
@Override public @Nullable E edgeConnectingOrNull(EndpointPair<N> endpoints) { validateEndpoints(endpoints); return edgeConnectingOrNull(endpoints.nodeU(), endpoints.nodeV()); }
@Override @CanIgnoreReturnValue public V removeEdge(EndpointPair<N> endpoints) { validateEndpoints(endpoints); return removeEdge(endpoints.nodeU(), endpoints.nodeV()); }
@Override @CanIgnoreReturnValue public boolean addEdge(EndpointPair<N> endpoints, E edge) { validateEndpoints(endpoints); return addEdge(endpoints.nodeU(), endpoints.nodeV(), edge); }
@Override public @Nullable V edgeValueOrDefault(EndpointPair<N> endpoints, @Nullable V defaultValue) { validateEndpoints(endpoints); return edgeValueOrDefault_internal(endpoints.nodeU(), endpoints.nodeV(), defaultValue); }
/** Creates a mutable copy of {@code graph} with the same nodes and edges. */ public static <N> MutableGraph<N> copyOf(Graph<N> graph) { MutableGraph<N> copy = GraphBuilder.from(graph).expectedNodeCount(graph.nodes().size()).build(); for (N node : graph.nodes()) { copy.addNode(node); } for (EndpointPair<N> edge : graph.edges()) { copy.putEdge(edge.nodeU(), edge.nodeV()); } return copy; }
@Test public void testSelfLoop() { EndpointPair<String> unordered = EndpointPair.unordered("node", "node"); assertThat(unordered.isOrdered()).isFalse(); assertThat(unordered).containsExactly("node", "node"); assertThat(unordered.nodeU()).isEqualTo("node"); assertThat(unordered.nodeV()).isEqualTo("node"); assertThat(unordered.adjacentNode("node")).isEqualTo("node"); assertThat(unordered.toString()).isEqualTo("[node, node]"); }
protected boolean addEdge(EndpointPair<Integer> endpoints, String e) { network.addNode(endpoints.nodeU()); network.addNode(endpoints.nodeV()); return network.addEdge(endpoints, e); }
@Test public void incidentNodes_selfLoop() { addEdge(N1, N1, E11); assertThat(network.incidentNodes(E11).nodeU()).isEqualTo(N1); assertThat(network.incidentNodes(E11).nodeV()).isEqualTo(N1); }