static <N, E> UndirectedMultiNetworkConnections<N, E> ofImmutable(Map<E, N> incidentEdges) { return new UndirectedMultiNetworkConnections<>(ImmutableMap.copyOf(incidentEdges)); }
@Override public void addInEdge(E edge, N node, boolean isSelfLoop) { if (!isSelfLoop) { addOutEdge(edge, node); } }
@Override public Set<N> adjacentNodes() { return Collections.unmodifiableSet(adjacentNodesMultiset().elementSet()); }
private Multiset<N> adjacentNodesMultiset() { Multiset<N> adjacentNodes = getReference(adjacentNodesReference); if (adjacentNodes == null) { adjacentNodes = HashMultiset.create(incidentEdgeMap.values()); adjacentNodesReference = new SoftReference<>(adjacentNodes); } return adjacentNodes; }
private NetworkConnections<N, E> newConnections() { return isDirected() ? allowsParallelEdges() ? DirectedMultiNetworkConnections.<N, E>of() : DirectedNetworkConnections.<N, E>of() : allowsParallelEdges() ? UndirectedMultiNetworkConnections.<N, E>of() : UndirectedNetworkConnections.<N, E>of(); } }
@Override public N removeInEdge(E edge, boolean isSelfLoop) { if (!isSelfLoop) { return removeOutEdge(edge); } return null; }
private static <N, E> NetworkConnections<N, E> connectionsOf(Network<N, E> network, N node) { if (network.isDirected()) { Map<E, N> inEdgeMap = Maps.asMap(network.inEdges(node), sourceNodeFn(network)); Map<E, N> outEdgeMap = Maps.asMap(network.outEdges(node), targetNodeFn(network)); int selfLoopCount = network.edgesConnecting(node, node).size(); return network.allowsParallelEdges() ? DirectedMultiNetworkConnections.ofImmutable(inEdgeMap, outEdgeMap, selfLoopCount) : DirectedNetworkConnections.ofImmutable(inEdgeMap, outEdgeMap, selfLoopCount); } else { Map<E, N> incidentEdgeMap = Maps.asMap(network.incidentEdges(node), adjacentNodeFn(network, node)); return network.allowsParallelEdges() ? UndirectedMultiNetworkConnections.ofImmutable(incidentEdgeMap) : UndirectedNetworkConnections.ofImmutable(incidentEdgeMap); } }
@Override public void addOutEdge(E edge, N node) { super.addOutEdge(edge, node); Multiset<N> adjacentNodes = getReference(adjacentNodesReference); if (adjacentNodes != null) { checkState(adjacentNodes.add(node)); } }
private NetworkConnections<N, E> newConnections() { return isDirected() ? allowsParallelEdges() ? DirectedMultiNetworkConnections.<N, E>of() : DirectedNetworkConnections.<N, E>of() : allowsParallelEdges() ? UndirectedMultiNetworkConnections.<N, E>of() : UndirectedNetworkConnections.<N, E>of(); } }
@Override public N removeInEdge(E edge, boolean isSelfLoop) { if (!isSelfLoop) { return removeOutEdge(edge); } return null; }
private static <N, E> NetworkConnections<N, E> connectionsOf(Network<N, E> network, N node) { if (network.isDirected()) { Map<E, N> inEdgeMap = Maps.asMap(network.inEdges(node), sourceNodeFn(network)); Map<E, N> outEdgeMap = Maps.asMap(network.outEdges(node), targetNodeFn(network)); int selfLoopCount = network.edgesConnecting(node, node).size(); return network.allowsParallelEdges() ? DirectedMultiNetworkConnections.ofImmutable(inEdgeMap, outEdgeMap, selfLoopCount) : DirectedNetworkConnections.ofImmutable(inEdgeMap, outEdgeMap, selfLoopCount); } else { Map<E, N> incidentEdgeMap = Maps.asMap(network.incidentEdges(node), adjacentNodeFn(network, node)); return network.allowsParallelEdges() ? UndirectedMultiNetworkConnections.ofImmutable(incidentEdgeMap) : UndirectedNetworkConnections.ofImmutable(incidentEdgeMap); } }
private Multiset<N> adjacentNodesMultiset() { Multiset<N> adjacentNodes = getReference(adjacentNodesReference); if (adjacentNodes == null) { adjacentNodes = HashMultiset.create(incidentEdgeMap.values()); adjacentNodesReference = new SoftReference<>(adjacentNodes); } return adjacentNodes; }
static <N, E> UndirectedMultiNetworkConnections<N, E> of() { return new UndirectedMultiNetworkConnections<>( new HashMap<E, N>(INNER_CAPACITY, INNER_LOAD_FACTOR)); }
@Override public void addInEdge(E edge, N node, boolean isSelfLoop) { if (!isSelfLoop) { addOutEdge(edge, node); } }
private NetworkConnections<N, E> newConnections() { return isDirected() ? allowsParallelEdges() ? DirectedMultiNetworkConnections.<N, E>of() : DirectedNetworkConnections.<N, E>of() : allowsParallelEdges() ? UndirectedMultiNetworkConnections.<N, E>of() : UndirectedNetworkConnections.<N, E>of(); } }
@Override public N removeInEdge(E edge, boolean isSelfLoop) { if (!isSelfLoop) { return removeOutEdge(edge); } return null; }
@Override public Set<N> adjacentNodes() { return Collections.unmodifiableSet(adjacentNodesMultiset().elementSet()); }
private static <N, E> NetworkConnections<N, E> connectionsOf(Network<N, E> network, N node) { if (network.isDirected()) { Map<E, N> inEdgeMap = Maps.asMap(network.inEdges(node), sourceNodeFn(network)); Map<E, N> outEdgeMap = Maps.asMap(network.outEdges(node), targetNodeFn(network)); int selfLoopCount = network.edgesConnecting(node, node).size(); return network.allowsParallelEdges() ? DirectedMultiNetworkConnections.ofImmutable(inEdgeMap, outEdgeMap, selfLoopCount) : DirectedNetworkConnections.ofImmutable(inEdgeMap, outEdgeMap, selfLoopCount); } else { Map<E, N> incidentEdgeMap = Maps.asMap(network.incidentEdges(node), adjacentNodeFn(network, node)); return network.allowsParallelEdges() ? UndirectedMultiNetworkConnections.ofImmutable(incidentEdgeMap) : UndirectedNetworkConnections.ofImmutable(incidentEdgeMap); } }
private Multiset<N> adjacentNodesMultiset() { Multiset<N> adjacentNodes = getReference(adjacentNodesReference); if (adjacentNodes == null) { adjacentNodes = HashMultiset.create(incidentEdgeMap.values()); adjacentNodesReference = new SoftReference<>(adjacentNodes); } return adjacentNodes; }
static <N, E> UndirectedMultiNetworkConnections<N, E> ofImmutable(Map<E, N> incidentEdges) { return new UndirectedMultiNetworkConnections<>(ImmutableMap.copyOf(incidentEdges)); }