90 lines
3.3 KiB
Java
90 lines
3.3 KiB
Java
package com.google.common.graph;
|
|
|
|
import com.google.common.base.Preconditions;
|
|
import com.google.common.collect.AbstractIterator;
|
|
import com.google.common.collect.ImmutableSet;
|
|
import com.google.common.collect.Sets;
|
|
import java.util.Iterator;
|
|
import java.util.Set;
|
|
|
|
/* JADX INFO: Access modifiers changed from: package-private */
|
|
/* loaded from: classes2.dex */
|
|
public abstract class EndpointPairIterator<N> extends AbstractIterator<EndpointPair<N>> {
|
|
private final BaseGraph<N> graph;
|
|
protected N node;
|
|
private final Iterator<N> nodeIterator;
|
|
protected Iterator<N> successorIterator;
|
|
|
|
/* JADX INFO: Access modifiers changed from: package-private */
|
|
public static <N> EndpointPairIterator<N> of(BaseGraph<N> baseGraph) {
|
|
return baseGraph.isDirected() ? new Directed(baseGraph) : new Undirected(baseGraph);
|
|
}
|
|
|
|
private EndpointPairIterator(BaseGraph<N> baseGraph) {
|
|
this.node = null;
|
|
this.successorIterator = ImmutableSet.of().iterator();
|
|
this.graph = baseGraph;
|
|
this.nodeIterator = baseGraph.nodes().iterator();
|
|
}
|
|
|
|
protected final boolean advance() {
|
|
Preconditions.checkState(!this.successorIterator.hasNext());
|
|
if (!this.nodeIterator.hasNext()) {
|
|
return false;
|
|
}
|
|
N next = this.nodeIterator.next();
|
|
this.node = next;
|
|
this.successorIterator = this.graph.successors((BaseGraph<N>) next).iterator();
|
|
return true;
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: package-private */
|
|
/* loaded from: classes2.dex */
|
|
public static final class Directed<N> extends EndpointPairIterator<N> {
|
|
private Directed(BaseGraph<N> baseGraph) {
|
|
super(baseGraph);
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: protected */
|
|
@Override // com.google.common.collect.AbstractIterator
|
|
public final EndpointPair<N> computeNext() {
|
|
while (!this.successorIterator.hasNext()) {
|
|
if (!advance()) {
|
|
return endOfData();
|
|
}
|
|
}
|
|
return EndpointPair.ordered(this.node, this.successorIterator.next());
|
|
}
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: package-private */
|
|
/* loaded from: classes2.dex */
|
|
public static final class Undirected<N> extends EndpointPairIterator<N> {
|
|
private Set<N> visitedNodes;
|
|
|
|
private Undirected(BaseGraph<N> baseGraph) {
|
|
super(baseGraph);
|
|
this.visitedNodes = Sets.newHashSetWithExpectedSize(baseGraph.nodes().size());
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: protected */
|
|
@Override // com.google.common.collect.AbstractIterator
|
|
public final EndpointPair<N> computeNext() {
|
|
while (true) {
|
|
if (this.successorIterator.hasNext()) {
|
|
N next = this.successorIterator.next();
|
|
if (!this.visitedNodes.contains(next)) {
|
|
return EndpointPair.unordered(this.node, next);
|
|
}
|
|
} else {
|
|
this.visitedNodes.add(this.node);
|
|
if (!advance()) {
|
|
this.visitedNodes = null;
|
|
return endOfData();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|