302 lines
12 KiB
Java
302 lines
12 KiB
Java
|
package com.google.common.graph;
|
||
|
|
||
|
import com.google.common.base.Function;
|
||
|
import com.google.common.base.Preconditions;
|
||
|
import com.google.common.base.Predicate;
|
||
|
import com.google.common.collect.ImmutableSet;
|
||
|
import com.google.common.collect.Iterators;
|
||
|
import com.google.common.collect.Maps;
|
||
|
import com.google.common.collect.Sets;
|
||
|
import com.google.common.math.IntMath;
|
||
|
import java.util.AbstractSet;
|
||
|
import java.util.Collections;
|
||
|
import java.util.Iterator;
|
||
|
import java.util.Map;
|
||
|
import java.util.Set;
|
||
|
|
||
|
/* loaded from: classes2.dex */
|
||
|
public abstract class AbstractNetwork<N, E> implements Network<N, E> {
|
||
|
|
||
|
/* JADX INFO: Access modifiers changed from: package-private */
|
||
|
/* renamed from: com.google.common.graph.AbstractNetwork$1, reason: invalid class name */
|
||
|
/* loaded from: classes2.dex */
|
||
|
public class AnonymousClass1 extends AbstractGraph<N> {
|
||
|
final AbstractNetwork this$0;
|
||
|
|
||
|
AnonymousClass1(AbstractNetwork abstractNetwork) {
|
||
|
this.this$0 = abstractNetwork;
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.graph.PredecessorsFunction
|
||
|
public /* bridge */ /* synthetic */ Iterable predecessors(Object obj) {
|
||
|
return predecessors((AnonymousClass1) obj);
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.graph.SuccessorsFunction
|
||
|
public /* bridge */ /* synthetic */ Iterable successors(Object obj) {
|
||
|
return successors((AnonymousClass1) obj);
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.graph.BaseGraph, com.google.common.graph.Graph
|
||
|
public Set<N> nodes() {
|
||
|
return this.this$0.nodes();
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.graph.AbstractGraph, com.google.common.graph.AbstractBaseGraph, com.google.common.graph.BaseGraph
|
||
|
public Set<EndpointPair<N>> edges() {
|
||
|
if (this.this$0.allowsParallelEdges()) {
|
||
|
return super.edges();
|
||
|
}
|
||
|
return new C00381(this);
|
||
|
}
|
||
|
|
||
|
/* JADX INFO: Access modifiers changed from: package-private */
|
||
|
/* renamed from: com.google.common.graph.AbstractNetwork$1$1, reason: invalid class name and collision with other inner class name */
|
||
|
/* loaded from: classes2.dex */
|
||
|
public class C00381 extends AbstractSet<EndpointPair<N>> {
|
||
|
final AnonymousClass1 this$1;
|
||
|
|
||
|
C00381(AnonymousClass1 anonymousClass1) {
|
||
|
this.this$1 = anonymousClass1;
|
||
|
}
|
||
|
|
||
|
@Override // java.util.AbstractCollection, java.util.Collection, java.lang.Iterable, java.util.Set
|
||
|
public Iterator<EndpointPair<N>> iterator() {
|
||
|
return Iterators.transform(this.this$1.this$0.edges().iterator(), new Function<E, EndpointPair<N>>(this) { // from class: com.google.common.graph.AbstractNetwork.1.1.1
|
||
|
final C00381 this$2;
|
||
|
|
||
|
{
|
||
|
this.this$2 = this;
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.base.Function
|
||
|
public /* bridge */ /* synthetic */ Object apply(Object obj) {
|
||
|
return apply((C00391) obj);
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.base.Function
|
||
|
public EndpointPair<N> apply(E e) {
|
||
|
return this.this$2.this$1.this$0.incidentNodes(e);
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
|
@Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
|
||
|
public int size() {
|
||
|
return this.this$1.this$0.edges().size();
|
||
|
}
|
||
|
|
||
|
/* JADX WARN: Multi-variable type inference failed */
|
||
|
@Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
|
||
|
public boolean contains(Object obj) {
|
||
|
if (!(obj instanceof EndpointPair)) {
|
||
|
return false;
|
||
|
}
|
||
|
EndpointPair<?> endpointPair = (EndpointPair) obj;
|
||
|
return this.this$1.isOrderingCompatible(endpointPair) && this.this$1.nodes().contains(endpointPair.nodeU()) && this.this$1.successors((AnonymousClass1) endpointPair.nodeU()).contains(endpointPair.nodeV());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.graph.BaseGraph, com.google.common.graph.Graph
|
||
|
public ElementOrder<N> nodeOrder() {
|
||
|
return this.this$0.nodeOrder();
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.graph.AbstractGraph, com.google.common.graph.AbstractBaseGraph, com.google.common.graph.BaseGraph
|
||
|
public ElementOrder<N> incidentEdgeOrder() {
|
||
|
return ElementOrder.unordered();
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.graph.BaseGraph, com.google.common.graph.Graph
|
||
|
public boolean isDirected() {
|
||
|
return this.this$0.isDirected();
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.graph.BaseGraph, com.google.common.graph.Graph
|
||
|
public boolean allowsSelfLoops() {
|
||
|
return this.this$0.allowsSelfLoops();
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.graph.BaseGraph, com.google.common.graph.Graph
|
||
|
public Set<N> adjacentNodes(N n) {
|
||
|
return this.this$0.adjacentNodes(n);
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.graph.BaseGraph, com.google.common.graph.PredecessorsFunction
|
||
|
public Set<N> predecessors(N n) {
|
||
|
return this.this$0.predecessors((AbstractNetwork) n);
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.graph.BaseGraph, com.google.common.graph.SuccessorsFunction
|
||
|
public Set<N> successors(N n) {
|
||
|
return this.this$0.successors((AbstractNetwork) n);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.graph.Network
|
||
|
public Graph<N> asGraph() {
|
||
|
return new AnonymousClass1(this);
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.graph.Network
|
||
|
public int degree(N n) {
|
||
|
if (isDirected()) {
|
||
|
return IntMath.saturatedAdd(inEdges(n).size(), outEdges(n).size());
|
||
|
}
|
||
|
return IntMath.saturatedAdd(incidentEdges(n).size(), edgesConnecting(n, n).size());
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.graph.Network
|
||
|
public int inDegree(N n) {
|
||
|
return isDirected() ? inEdges(n).size() : degree(n);
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.graph.Network
|
||
|
public int outDegree(N n) {
|
||
|
return isDirected() ? outEdges(n).size() : degree(n);
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.graph.Network
|
||
|
public Set<E> adjacentEdges(E e) {
|
||
|
EndpointPair<N> incidentNodes = incidentNodes(e);
|
||
|
return Sets.difference(Sets.union(incidentEdges(incidentNodes.nodeU()), incidentEdges(incidentNodes.nodeV())), ImmutableSet.of((Object) e));
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.graph.Network
|
||
|
public Set<E> edgesConnecting(N n, N n2) {
|
||
|
Set<E> outEdges = outEdges(n);
|
||
|
Set<E> inEdges = inEdges(n2);
|
||
|
if (outEdges.size() <= inEdges.size()) {
|
||
|
return Collections.unmodifiableSet(Sets.filter(outEdges, connectedPredicate(n, n2)));
|
||
|
}
|
||
|
return Collections.unmodifiableSet(Sets.filter(inEdges, connectedPredicate(n2, n)));
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.graph.Network
|
||
|
public Set<E> edgesConnecting(EndpointPair<N> endpointPair) {
|
||
|
validateEndpoints(endpointPair);
|
||
|
return edgesConnecting(endpointPair.nodeU(), endpointPair.nodeV());
|
||
|
}
|
||
|
|
||
|
private Predicate<E> connectedPredicate(N n, N n2) {
|
||
|
return new Predicate<E>(this, n, n2) { // from class: com.google.common.graph.AbstractNetwork.2
|
||
|
final AbstractNetwork this$0;
|
||
|
final Object val$nodePresent;
|
||
|
final Object val$nodeToCheck;
|
||
|
|
||
|
{
|
||
|
this.this$0 = this;
|
||
|
this.val$nodePresent = n;
|
||
|
this.val$nodeToCheck = n2;
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.base.Predicate
|
||
|
public boolean apply(E e) {
|
||
|
return this.this$0.incidentNodes(e).adjacentNode(this.val$nodePresent).equals(this.val$nodeToCheck);
|
||
|
}
|
||
|
};
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.graph.Network
|
||
|
public E edgeConnectingOrNull(N n, N n2) {
|
||
|
Set<E> edgesConnecting = edgesConnecting(n, n2);
|
||
|
int size = edgesConnecting.size();
|
||
|
if (size == 0) {
|
||
|
return null;
|
||
|
}
|
||
|
if (size == 1) {
|
||
|
return edgesConnecting.iterator().next();
|
||
|
}
|
||
|
throw new IllegalArgumentException(String.format("Cannot call edgeConnecting() when parallel edges exist between %s and %s. Consider calling edgesConnecting() instead.", n, n2));
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.graph.Network
|
||
|
public E edgeConnectingOrNull(EndpointPair<N> endpointPair) {
|
||
|
validateEndpoints(endpointPair);
|
||
|
return edgeConnectingOrNull(endpointPair.nodeU(), endpointPair.nodeV());
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.graph.Network
|
||
|
public boolean hasEdgeConnecting(N n, N n2) {
|
||
|
Preconditions.checkNotNull(n);
|
||
|
Preconditions.checkNotNull(n2);
|
||
|
return nodes().contains(n) && successors((AbstractNetwork<N, E>) n).contains(n2);
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.graph.Network
|
||
|
public boolean hasEdgeConnecting(EndpointPair<N> endpointPair) {
|
||
|
Preconditions.checkNotNull(endpointPair);
|
||
|
if (isOrderingCompatible(endpointPair)) {
|
||
|
return hasEdgeConnecting(endpointPair.nodeU(), endpointPair.nodeV());
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
/* JADX INFO: Access modifiers changed from: protected */
|
||
|
public final void validateEndpoints(EndpointPair<?> endpointPair) {
|
||
|
Preconditions.checkNotNull(endpointPair);
|
||
|
Preconditions.checkArgument(isOrderingCompatible(endpointPair), "Mismatch: unordered endpoints cannot be used with directed graphs");
|
||
|
}
|
||
|
|
||
|
protected final boolean isOrderingCompatible(EndpointPair<?> endpointPair) {
|
||
|
return endpointPair.isOrdered() || !isDirected();
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.graph.Network
|
||
|
public final boolean equals(Object obj) {
|
||
|
if (obj == this) {
|
||
|
return true;
|
||
|
}
|
||
|
if (!(obj instanceof Network)) {
|
||
|
return false;
|
||
|
}
|
||
|
Network network = (Network) obj;
|
||
|
return isDirected() == network.isDirected() && nodes().equals(network.nodes()) && edgeIncidentNodesMap(this).equals(edgeIncidentNodesMap(network));
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.graph.Network
|
||
|
public final int hashCode() {
|
||
|
return edgeIncidentNodesMap(this).hashCode();
|
||
|
}
|
||
|
|
||
|
public String toString() {
|
||
|
boolean isDirected = isDirected();
|
||
|
boolean allowsParallelEdges = allowsParallelEdges();
|
||
|
boolean allowsSelfLoops = allowsSelfLoops();
|
||
|
String valueOf = String.valueOf(nodes());
|
||
|
String valueOf2 = String.valueOf(edgeIncidentNodesMap(this));
|
||
|
StringBuilder sb = new StringBuilder(String.valueOf(valueOf).length() + 87 + String.valueOf(valueOf2).length());
|
||
|
sb.append("isDirected: ");
|
||
|
sb.append(isDirected);
|
||
|
sb.append(", allowsParallelEdges: ");
|
||
|
sb.append(allowsParallelEdges);
|
||
|
sb.append(", allowsSelfLoops: ");
|
||
|
sb.append(allowsSelfLoops);
|
||
|
sb.append(", nodes: ");
|
||
|
sb.append(valueOf);
|
||
|
sb.append(", edges: ");
|
||
|
sb.append(valueOf2);
|
||
|
return sb.toString();
|
||
|
}
|
||
|
|
||
|
private static <N, E> Map<E, EndpointPair<N>> edgeIncidentNodesMap(Network<N, E> network) {
|
||
|
return Maps.asMap(network.edges(), new Function<E, EndpointPair<N>>(network) { // from class: com.google.common.graph.AbstractNetwork.3
|
||
|
final Network val$network;
|
||
|
|
||
|
{
|
||
|
this.val$network = network;
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.base.Function
|
||
|
public /* bridge */ /* synthetic */ Object apply(Object obj) {
|
||
|
return apply((AnonymousClass3) obj);
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.base.Function
|
||
|
public EndpointPair<N> apply(E e) {
|
||
|
return this.val$network.incidentNodes(e);
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
}
|