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.UnmodifiableIterator; import com.google.errorprone.annotations.DoNotMock; import java.util.ArrayDeque; import java.util.Deque; import java.util.HashSet; import java.util.Iterator; import java.util.Objects; import javax.annotation.CheckForNull; @DoNotMock("Call forGraph or forTree, passing a lambda or a Graph with the desired edges (built with GraphBuilder)") @ElementTypesAreNonnullByDefault /* loaded from: classes3.dex */ public abstract class Traverser { private final SuccessorsFunction successorFunction; /* JADX INFO: Access modifiers changed from: private */ /* loaded from: classes3.dex */ public enum InsertionOrder { FRONT { // from class: com.google.common.graph.Traverser.InsertionOrder.1 @Override // com.google.common.graph.Traverser.InsertionOrder void insertInto(Deque deque, T t) { deque.addFirst(t); } }, BACK { // from class: com.google.common.graph.Traverser.InsertionOrder.2 @Override // com.google.common.graph.Traverser.InsertionOrder void insertInto(Deque deque, T t) { deque.addLast(t); } }; abstract void insertInto(Deque deque, T t); } abstract Traversal newTraversal(); private Traverser(SuccessorsFunction successorsFunction) { this.successorFunction = (SuccessorsFunction) Preconditions.checkNotNull(successorsFunction); } public static Traverser forGraph(final SuccessorsFunction successorsFunction) { return new Traverser(successorsFunction) { // from class: com.google.common.graph.Traverser.1 @Override // com.google.common.graph.Traverser Traversal newTraversal() { return Traversal.inGraph(successorsFunction); } }; } public static Traverser forTree(final SuccessorsFunction successorsFunction) { if (successorsFunction instanceof BaseGraph) { Preconditions.checkArgument(((BaseGraph) successorsFunction).isDirected(), "Undirected graphs can never be trees."); } if (successorsFunction instanceof Network) { Preconditions.checkArgument(((Network) successorsFunction).isDirected(), "Undirected networks can never be trees."); } return new Traverser(successorsFunction) { // from class: com.google.common.graph.Traverser.2 @Override // com.google.common.graph.Traverser Traversal newTraversal() { return Traversal.inTree(successorsFunction); } }; } public final Iterable breadthFirst(N n) { return breadthFirst((Iterable) ImmutableSet.of(n)); } public final Iterable breadthFirst(Iterable iterable) { final ImmutableSet validate = validate(iterable); return new Iterable() { // from class: com.google.common.graph.Traverser.3 @Override // java.lang.Iterable public Iterator iterator() { return Traverser.this.newTraversal().breadthFirst(validate.iterator()); } }; } public final Iterable depthFirstPreOrder(N n) { return depthFirstPreOrder((Iterable) ImmutableSet.of(n)); } public final Iterable depthFirstPreOrder(Iterable iterable) { final ImmutableSet validate = validate(iterable); return new Iterable() { // from class: com.google.common.graph.Traverser.4 @Override // java.lang.Iterable public Iterator iterator() { return Traverser.this.newTraversal().preOrder(validate.iterator()); } }; } public final Iterable depthFirstPostOrder(N n) { return depthFirstPostOrder((Iterable) ImmutableSet.of(n)); } public final Iterable depthFirstPostOrder(Iterable iterable) { final ImmutableSet validate = validate(iterable); return new Iterable() { // from class: com.google.common.graph.Traverser.5 @Override // java.lang.Iterable public Iterator iterator() { return Traverser.this.newTraversal().postOrder(validate.iterator()); } }; } private ImmutableSet validate(Iterable iterable) { ImmutableSet copyOf = ImmutableSet.copyOf(iterable); UnmodifiableIterator it = copyOf.iterator(); while (it.hasNext()) { this.successorFunction.successors(it.next()); } return copyOf; } /* loaded from: classes3.dex */ private static abstract class Traversal { final SuccessorsFunction successorFunction; @CheckForNull abstract N visitNext(Deque> deque); Traversal(SuccessorsFunction successorsFunction) { this.successorFunction = successorsFunction; } static Traversal inGraph(SuccessorsFunction successorsFunction) { final HashSet hashSet = new HashSet(); return new Traversal(successorsFunction) { // from class: com.google.common.graph.Traverser.Traversal.1 @Override // com.google.common.graph.Traverser.Traversal @CheckForNull N visitNext(Deque> deque) { Iterator first = deque.getFirst(); while (first.hasNext()) { N next = first.next(); Objects.requireNonNull(next); if (hashSet.add(next)) { return next; } } deque.removeFirst(); return null; } }; } static Traversal inTree(SuccessorsFunction successorsFunction) { return new Traversal(successorsFunction) { // from class: com.google.common.graph.Traverser.Traversal.2 @Override // com.google.common.graph.Traverser.Traversal @CheckForNull N visitNext(Deque> deque) { Iterator first = deque.getFirst(); if (first.hasNext()) { return (N) Preconditions.checkNotNull(first.next()); } deque.removeFirst(); return null; } }; } final Iterator breadthFirst(Iterator it) { return topDown(it, InsertionOrder.BACK); } final Iterator preOrder(Iterator it) { return topDown(it, InsertionOrder.FRONT); } private Iterator topDown(Iterator it, final InsertionOrder insertionOrder) { final ArrayDeque arrayDeque = new ArrayDeque(); arrayDeque.add(it); return new AbstractIterator() { // from class: com.google.common.graph.Traverser.Traversal.3 @Override // com.google.common.collect.AbstractIterator @CheckForNull protected N computeNext() { do { N n = (N) Traversal.this.visitNext(arrayDeque); if (n != null) { Iterator it2 = Traversal.this.successorFunction.successors(n).iterator(); if (it2.hasNext()) { insertionOrder.insertInto(arrayDeque, it2); } return n; } } while (!arrayDeque.isEmpty()); return endOfData(); } }; } final Iterator postOrder(Iterator it) { final ArrayDeque arrayDeque = new ArrayDeque(); final ArrayDeque arrayDeque2 = new ArrayDeque(); arrayDeque2.add(it); return new AbstractIterator() { // from class: com.google.common.graph.Traverser.Traversal.4 @Override // com.google.common.collect.AbstractIterator @CheckForNull protected N computeNext() { while (true) { N n = (N) Traversal.this.visitNext(arrayDeque2); if (n != null) { Iterator it2 = Traversal.this.successorFunction.successors(n).iterator(); if (!it2.hasNext()) { return n; } arrayDeque2.addFirst(it2); arrayDeque.push(n); } else { if (!arrayDeque.isEmpty()) { return (N) arrayDeque.pop(); } return endOfData(); } } } }; } } }