what-the-bank/sources/com/google/common/graph/DirectedGraphConnections.java

559 lines
23 KiB
Java

package com.google.common.graph;
import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.collect.AbstractIterator;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterators;
import com.google.common.collect.UnmodifiableIterator;
import com.google.common.graph.ElementOrder;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public final class DirectedGraphConnections<N, V> implements GraphConnections<N, V> {
private static final Object PRED = new Object();
private final Map<N, Object> adjacentNodeValues;
private final List<NodeConnection<N>> orderedNodeConnections;
private int predecessorCount;
private int successorCount;
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public static final class PredAndSucc {
private final Object successorValue;
PredAndSucc(Object obj) {
this.successorValue = obj;
}
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public static abstract class NodeConnection<N> {
final N node;
NodeConnection(N n) {
this.node = (N) Preconditions.checkNotNull(n);
}
/* loaded from: classes2.dex */
static final class Pred<N> extends NodeConnection<N> {
Pred(N n) {
super(n);
}
public final boolean equals(Object obj) {
if (obj instanceof Pred) {
return this.node.equals(((Pred) obj).node);
}
return false;
}
public final int hashCode() {
return Pred.class.hashCode() + this.node.hashCode();
}
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public static final class Succ<N> extends NodeConnection<N> {
Succ(N n) {
super(n);
}
public final boolean equals(Object obj) {
if (obj instanceof Succ) {
return this.node.equals(((Succ) obj).node);
}
return false;
}
public final int hashCode() {
return Succ.class.hashCode() + this.node.hashCode();
}
}
}
private DirectedGraphConnections(Map<N, Object> map, List<NodeConnection<N>> list, int i, int i2) {
this.adjacentNodeValues = (Map) Preconditions.checkNotNull(map);
this.orderedNodeConnections = list;
this.predecessorCount = Graphs.checkNonNegative(i);
this.successorCount = Graphs.checkNonNegative(i2);
Preconditions.checkState(i <= map.size() && i2 <= map.size());
}
/* renamed from: com.google.common.graph.DirectedGraphConnections$8, reason: invalid class name */
/* loaded from: classes2.dex */
static /* synthetic */ class AnonymousClass8 {
static final int[] $SwitchMap$com$google$common$graph$ElementOrder$Type;
static {
int[] iArr = new int[ElementOrder.Type.values().length];
$SwitchMap$com$google$common$graph$ElementOrder$Type = iArr;
try {
iArr[ElementOrder.Type.UNORDERED.ordinal()] = 1;
} catch (NoSuchFieldError unused) {
}
try {
$SwitchMap$com$google$common$graph$ElementOrder$Type[ElementOrder.Type.STABLE.ordinal()] = 2;
} catch (NoSuchFieldError unused2) {
}
}
}
/* JADX INFO: Access modifiers changed from: package-private */
public static <N, V> DirectedGraphConnections<N, V> of(ElementOrder<N> elementOrder) {
ArrayList arrayList;
int i = AnonymousClass8.$SwitchMap$com$google$common$graph$ElementOrder$Type[elementOrder.type().ordinal()];
if (i == 1) {
arrayList = null;
} else if (i == 2) {
arrayList = new ArrayList();
} else {
throw new AssertionError(elementOrder.type());
}
return new DirectedGraphConnections<>(new HashMap(4, 1.0f), arrayList, 0, 0);
}
/* JADX INFO: Access modifiers changed from: package-private */
/* JADX WARN: Multi-variable type inference failed */
public static <N, V> DirectedGraphConnections<N, V> ofImmutable(N n, Iterable<EndpointPair<N>> iterable, Function<N, V> function) {
Preconditions.checkNotNull(n);
Preconditions.checkNotNull(function);
HashMap hashMap = new HashMap();
ImmutableList.Builder builder = ImmutableList.builder();
int i = 0;
int i2 = 0;
for (EndpointPair<N> endpointPair : iterable) {
if (endpointPair.nodeU().equals(n) && endpointPair.nodeV().equals(n)) {
hashMap.put(n, new PredAndSucc(function.apply(n)));
builder.add((ImmutableList.Builder) new NodeConnection.Pred(n));
builder.add((ImmutableList.Builder) new NodeConnection.Succ(n));
i++;
} else if (endpointPair.nodeV().equals(n)) {
N nodeU = endpointPair.nodeU();
Object put = hashMap.put(nodeU, PRED);
if (put != null) {
hashMap.put(nodeU, new PredAndSucc(put));
}
builder.add((ImmutableList.Builder) new NodeConnection.Pred(nodeU));
i++;
} else {
Preconditions.checkArgument(endpointPair.nodeU().equals(n));
N nodeV = endpointPair.nodeV();
V apply = function.apply(nodeV);
Object put2 = hashMap.put(nodeV, apply);
if (put2 != null) {
Preconditions.checkArgument(put2 == PRED);
hashMap.put(nodeV, new PredAndSucc(apply));
}
builder.add((ImmutableList.Builder) new NodeConnection.Succ(nodeV));
}
i2++;
}
return new DirectedGraphConnections<>(hashMap, builder.build(), i, i2);
}
@Override // com.google.common.graph.GraphConnections
public final Set<N> adjacentNodes() {
if (this.orderedNodeConnections == null) {
return Collections.unmodifiableSet(this.adjacentNodeValues.keySet());
}
return new AbstractSet<N>(this) { // from class: com.google.common.graph.DirectedGraphConnections.1
final DirectedGraphConnections this$0;
{
this.this$0 = this;
}
@Override // java.util.AbstractCollection, java.util.Collection, java.lang.Iterable, java.util.Set
public UnmodifiableIterator<N> iterator() {
return new AbstractIterator<N>(this, this.this$0.orderedNodeConnections.iterator(), new HashSet()) { // from class: com.google.common.graph.DirectedGraphConnections.1.1
final Iterator val$nodeConnections;
final Set val$seenNodes;
{
this.val$nodeConnections = r2;
this.val$seenNodes = r3;
}
@Override // com.google.common.collect.AbstractIterator
public N computeNext() {
while (this.val$nodeConnections.hasNext()) {
NodeConnection nodeConnection = (NodeConnection) this.val$nodeConnections.next();
if (this.val$seenNodes.add(nodeConnection.node)) {
return nodeConnection.node;
}
}
return endOfData();
}
};
}
@Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
public int size() {
return this.this$0.adjacentNodeValues.size();
}
@Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
public boolean contains(Object obj) {
return this.this$0.adjacentNodeValues.containsKey(obj);
}
};
}
@Override // com.google.common.graph.GraphConnections
public final Set<N> predecessors() {
return new AbstractSet<N>(this) { // from class: com.google.common.graph.DirectedGraphConnections.2
final DirectedGraphConnections this$0;
{
this.this$0 = this;
}
@Override // java.util.AbstractCollection, java.util.Collection, java.lang.Iterable, java.util.Set
public UnmodifiableIterator<N> iterator() {
if (this.this$0.orderedNodeConnections == null) {
return new AbstractIterator<N>(this, this.this$0.adjacentNodeValues.entrySet().iterator()) { // from class: com.google.common.graph.DirectedGraphConnections.2.1
final Iterator val$entries;
{
this.val$entries = r2;
}
@Override // com.google.common.collect.AbstractIterator
public N computeNext() {
while (this.val$entries.hasNext()) {
Map.Entry entry = (Map.Entry) this.val$entries.next();
if (DirectedGraphConnections.isPredecessor(entry.getValue())) {
return (N) entry.getKey();
}
}
return endOfData();
}
};
}
return new AbstractIterator<N>(this, this.this$0.orderedNodeConnections.iterator()) { // from class: com.google.common.graph.DirectedGraphConnections.2.2
final Iterator val$nodeConnections;
{
this.val$nodeConnections = r2;
}
@Override // com.google.common.collect.AbstractIterator
public N computeNext() {
while (this.val$nodeConnections.hasNext()) {
NodeConnection nodeConnection = (NodeConnection) this.val$nodeConnections.next();
if (nodeConnection instanceof NodeConnection.Pred) {
return nodeConnection.node;
}
}
return endOfData();
}
};
}
@Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
public int size() {
return this.this$0.predecessorCount;
}
@Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
public boolean contains(Object obj) {
return DirectedGraphConnections.isPredecessor(this.this$0.adjacentNodeValues.get(obj));
}
};
}
@Override // com.google.common.graph.GraphConnections
public final Set<N> successors() {
return new AbstractSet<N>(this) { // from class: com.google.common.graph.DirectedGraphConnections.3
final DirectedGraphConnections this$0;
{
this.this$0 = this;
}
@Override // java.util.AbstractCollection, java.util.Collection, java.lang.Iterable, java.util.Set
public UnmodifiableIterator<N> iterator() {
if (this.this$0.orderedNodeConnections == null) {
return new AbstractIterator<N>(this, this.this$0.adjacentNodeValues.entrySet().iterator()) { // from class: com.google.common.graph.DirectedGraphConnections.3.1
final Iterator val$entries;
{
this.val$entries = r2;
}
@Override // com.google.common.collect.AbstractIterator
public N computeNext() {
while (this.val$entries.hasNext()) {
Map.Entry entry = (Map.Entry) this.val$entries.next();
if (DirectedGraphConnections.isSuccessor(entry.getValue())) {
return (N) entry.getKey();
}
}
return endOfData();
}
};
}
return new AbstractIterator<N>(this, this.this$0.orderedNodeConnections.iterator()) { // from class: com.google.common.graph.DirectedGraphConnections.3.2
final Iterator val$nodeConnections;
{
this.val$nodeConnections = r2;
}
@Override // com.google.common.collect.AbstractIterator
public N computeNext() {
while (this.val$nodeConnections.hasNext()) {
NodeConnection nodeConnection = (NodeConnection) this.val$nodeConnections.next();
if (nodeConnection instanceof NodeConnection.Succ) {
return nodeConnection.node;
}
}
return endOfData();
}
};
}
@Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
public int size() {
return this.this$0.successorCount;
}
@Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
public boolean contains(Object obj) {
return DirectedGraphConnections.isSuccessor(this.this$0.adjacentNodeValues.get(obj));
}
};
}
@Override // com.google.common.graph.GraphConnections
public final Iterator<EndpointPair<N>> incidentEdgeIterator(N n) {
Iterator transform;
Preconditions.checkNotNull(n);
List<NodeConnection<N>> list = this.orderedNodeConnections;
if (list == null) {
transform = Iterators.concat(Iterators.transform(predecessors().iterator(), new Function<N, EndpointPair<N>>(this, n) { // from class: com.google.common.graph.DirectedGraphConnections.4
final Object val$thisNode;
{
this.val$thisNode = n;
}
@Override // com.google.common.base.Function
public /* bridge */ /* synthetic */ Object apply(Object obj) {
return apply((AnonymousClass4) obj);
}
@Override // com.google.common.base.Function
public EndpointPair<N> apply(N n2) {
return EndpointPair.ordered(n2, this.val$thisNode);
}
}), Iterators.transform(successors().iterator(), new Function<N, EndpointPair<N>>(this, n) { // from class: com.google.common.graph.DirectedGraphConnections.5
final Object val$thisNode;
{
this.val$thisNode = n;
}
@Override // com.google.common.base.Function
public /* bridge */ /* synthetic */ Object apply(Object obj) {
return apply((AnonymousClass5) obj);
}
@Override // com.google.common.base.Function
public EndpointPair<N> apply(N n2) {
return EndpointPair.ordered(this.val$thisNode, n2);
}
}));
} else {
transform = Iterators.transform(list.iterator(), new Function<NodeConnection<N>, EndpointPair<N>>(this, n) { // from class: com.google.common.graph.DirectedGraphConnections.6
final Object val$thisNode;
{
this.val$thisNode = n;
}
@Override // com.google.common.base.Function
public EndpointPair<N> apply(NodeConnection<N> nodeConnection) {
if (nodeConnection instanceof NodeConnection.Succ) {
return EndpointPair.ordered(this.val$thisNode, nodeConnection.node);
}
return EndpointPair.ordered(nodeConnection.node, this.val$thisNode);
}
});
}
return new AbstractIterator<EndpointPair<N>>(this, transform, new AtomicBoolean(false)) { // from class: com.google.common.graph.DirectedGraphConnections.7
final AtomicBoolean val$alreadySeenSelfLoop;
final Iterator val$resultWithDoubleSelfLoop;
{
this.val$resultWithDoubleSelfLoop = transform;
this.val$alreadySeenSelfLoop = r3;
}
/* JADX INFO: Access modifiers changed from: protected */
@Override // com.google.common.collect.AbstractIterator
public EndpointPair<N> computeNext() {
while (this.val$resultWithDoubleSelfLoop.hasNext()) {
EndpointPair<N> endpointPair = (EndpointPair) this.val$resultWithDoubleSelfLoop.next();
if (!endpointPair.nodeU().equals(endpointPair.nodeV()) || !this.val$alreadySeenSelfLoop.getAndSet(true)) {
return endpointPair;
}
}
return endOfData();
}
};
}
/* JADX WARN: Multi-variable type inference failed */
@Override // com.google.common.graph.GraphConnections
public final V value(N n) {
Preconditions.checkNotNull(n);
V v = (V) this.adjacentNodeValues.get(n);
if (v == PRED) {
return null;
}
return v instanceof PredAndSucc ? (V) ((PredAndSucc) v).successorValue : v;
}
@Override // com.google.common.graph.GraphConnections
public final void removePredecessor(N n) {
Preconditions.checkNotNull(n);
Object obj = this.adjacentNodeValues.get(n);
if (obj == PRED) {
this.adjacentNodeValues.remove(n);
} else if (!(obj instanceof PredAndSucc)) {
return;
} else {
this.adjacentNodeValues.put(n, ((PredAndSucc) obj).successorValue);
}
int i = this.predecessorCount - 1;
this.predecessorCount = i;
Graphs.checkNonNegative(i);
List<NodeConnection<N>> list = this.orderedNodeConnections;
if (list != null) {
list.remove(new NodeConnection.Pred(n));
}
}
@Override // com.google.common.graph.GraphConnections
public final V removeSuccessor(Object obj) {
Object obj2;
Preconditions.checkNotNull(obj);
Object obj3 = (V) this.adjacentNodeValues.get(obj);
if (obj3 == null || obj3 == (obj2 = PRED)) {
obj3 = (V) null;
} else if (obj3 instanceof PredAndSucc) {
this.adjacentNodeValues.put(obj, obj2);
obj3 = (V) ((PredAndSucc) obj3).successorValue;
} else {
this.adjacentNodeValues.remove(obj);
}
if (obj3 != null) {
int i = this.successorCount - 1;
this.successorCount = i;
Graphs.checkNonNegative(i);
List<NodeConnection<N>> list = this.orderedNodeConnections;
if (list != null) {
list.remove(new NodeConnection.Succ(obj));
}
}
return (V) obj3;
}
@Override // com.google.common.graph.GraphConnections
public final void addPredecessor(N n, V v) {
Map<N, Object> map = this.adjacentNodeValues;
Object obj = PRED;
Object put = map.put(n, obj);
if (put != null) {
if (put instanceof PredAndSucc) {
this.adjacentNodeValues.put(n, put);
return;
} else if (put == obj) {
return;
} else {
this.adjacentNodeValues.put(n, new PredAndSucc(put));
}
}
int i = this.predecessorCount + 1;
this.predecessorCount = i;
Graphs.checkPositive(i);
List<NodeConnection<N>> list = this.orderedNodeConnections;
if (list != null) {
list.add(new NodeConnection.Pred(n));
}
}
/* JADX WARN: Removed duplicated region for block: B:7:0x002f */
@Override // com.google.common.graph.GraphConnections
/*
Code decompiled incorrectly, please refer to instructions dump.
To view partially-correct add '--show-bad-code' argument
*/
public final V addSuccessor(N r4, V r5) {
/*
r3 = this;
java.util.Map<N, java.lang.Object> r0 = r3.adjacentNodeValues
java.lang.Object r0 = r0.put(r4, r5)
if (r0 != 0) goto L9
goto L2c
L9:
boolean r1 = r0 instanceof com.google.common.graph.DirectedGraphConnections.PredAndSucc
if (r1 == 0) goto L1e
java.util.Map<N, java.lang.Object> r1 = r3.adjacentNodeValues
com.google.common.graph.DirectedGraphConnections$PredAndSucc r2 = new com.google.common.graph.DirectedGraphConnections$PredAndSucc
r2.<init>(r5)
r1.put(r4, r2)
com.google.common.graph.DirectedGraphConnections$PredAndSucc r0 = (com.google.common.graph.DirectedGraphConnections.PredAndSucc) r0
java.lang.Object r0 = com.google.common.graph.DirectedGraphConnections.PredAndSucc.access$600(r0)
goto L2d
L1e:
java.lang.Object r1 = com.google.common.graph.DirectedGraphConnections.PRED
if (r0 != r1) goto L2d
java.util.Map<N, java.lang.Object> r0 = r3.adjacentNodeValues
com.google.common.graph.DirectedGraphConnections$PredAndSucc r1 = new com.google.common.graph.DirectedGraphConnections$PredAndSucc
r1.<init>(r5)
r0.put(r4, r1)
L2c:
r0 = 0
L2d:
if (r0 != 0) goto L44
int r5 = r3.successorCount
int r5 = r5 + 1
r3.successorCount = r5
com.google.common.graph.Graphs.checkPositive(r5)
java.util.List<com.google.common.graph.DirectedGraphConnections$NodeConnection<N>> r5 = r3.orderedNodeConnections
if (r5 == 0) goto L44
com.google.common.graph.DirectedGraphConnections$NodeConnection$Succ r1 = new com.google.common.graph.DirectedGraphConnections$NodeConnection$Succ
r1.<init>(r4)
r5.add(r1)
L44:
return r0
*/
throw new UnsupportedOperationException("Method not decompiled: com.google.common.graph.DirectedGraphConnections.addSuccessor(java.lang.Object, java.lang.Object):java.lang.Object");
}
/* JADX INFO: Access modifiers changed from: private */
public static boolean isPredecessor(Object obj) {
return obj == PRED || (obj instanceof PredAndSucc);
}
/* JADX INFO: Access modifiers changed from: private */
public static boolean isSuccessor(Object obj) {
return (obj == PRED || obj == null) ? false : true;
}
}