package androidx.coordinatorlayout.widget; import androidx.collection.SimpleArrayMap; import androidx.core.util.Pools; import java.util.ArrayList; import java.util.HashSet; import java.util.List; /* loaded from: classes.dex */ public final class DirectedAcyclicGraph { private final Pools.Pool> mListPool = new Pools.SimplePool(10); private final SimpleArrayMap> mGraph = new SimpleArrayMap<>(); private final ArrayList mSortResult = new ArrayList<>(); private final HashSet mSortTmpMarked = new HashSet<>(); public void addNode(T t) { if (this.mGraph.containsKey(t)) { return; } this.mGraph.put(t, null); } public boolean contains(T t) { return this.mGraph.containsKey(t); } public void addEdge(T t, T t2) { if (!this.mGraph.containsKey(t) || !this.mGraph.containsKey(t2)) { throw new IllegalArgumentException("All nodes must be present in the graph before being added as an edge"); } ArrayList arrayList = this.mGraph.get(t); if (arrayList == null) { arrayList = getEmptyList(); this.mGraph.put(t, arrayList); } arrayList.add(t2); } public List getIncomingEdges(T t) { return this.mGraph.get(t); } public List getOutgoingEdges(T t) { int size = this.mGraph.getSize(); ArrayList arrayList = null; for (int i = 0; i < size; i++) { ArrayList valueAt = this.mGraph.valueAt(i); if (valueAt != null && valueAt.contains(t)) { if (arrayList == null) { arrayList = new ArrayList(); } arrayList.add(this.mGraph.keyAt(i)); } } return arrayList; } public boolean hasOutgoingEdges(T t) { int size = this.mGraph.getSize(); for (int i = 0; i < size; i++) { ArrayList valueAt = this.mGraph.valueAt(i); if (valueAt != null && valueAt.contains(t)) { return true; } } return false; } public void clear() { int size = this.mGraph.getSize(); for (int i = 0; i < size; i++) { ArrayList valueAt = this.mGraph.valueAt(i); if (valueAt != null) { poolList(valueAt); } } this.mGraph.clear(); } public ArrayList getSortedList() { this.mSortResult.clear(); this.mSortTmpMarked.clear(); int size = this.mGraph.getSize(); for (int i = 0; i < size; i++) { dfs(this.mGraph.keyAt(i), this.mSortResult, this.mSortTmpMarked); } return this.mSortResult; } private void dfs(T t, ArrayList arrayList, HashSet hashSet) { if (arrayList.contains(t)) { return; } if (hashSet.contains(t)) { throw new RuntimeException("This graph contains cyclic dependencies"); } hashSet.add(t); ArrayList arrayList2 = this.mGraph.get(t); if (arrayList2 != null) { int size = arrayList2.size(); for (int i = 0; i < size; i++) { dfs(arrayList2.get(i), arrayList, hashSet); } } hashSet.remove(t); arrayList.add(t); } int size() { return this.mGraph.getSize(); } private ArrayList getEmptyList() { ArrayList acquire = this.mListPool.acquire(); return acquire == null ? new ArrayList<>() : acquire; } private void poolList(ArrayList arrayList) { arrayList.clear(); this.mListPool.release(arrayList); } }