ADD week 5
This commit is contained in:
		| @@ -0,0 +1,122 @@ | ||||
| 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<T> { | ||||
|     private final Pools.Pool<ArrayList<T>> mListPool = new Pools.SimplePool(10); | ||||
|     private final SimpleArrayMap<T, ArrayList<T>> mGraph = new SimpleArrayMap<>(); | ||||
|     private final ArrayList<T> mSortResult = new ArrayList<>(); | ||||
|     private final HashSet<T> 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<T> 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<T> getOutgoingEdges(T t) { | ||||
|         int size = this.mGraph.size(); | ||||
|         ArrayList arrayList = null; | ||||
|         for (int i = 0; i < size; i++) { | ||||
|             ArrayList<T> 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.size(); | ||||
|         for (int i = 0; i < size; i++) { | ||||
|             ArrayList<T> valueAt = this.mGraph.valueAt(i); | ||||
|             if (valueAt != null && valueAt.contains(t)) { | ||||
|                 return true; | ||||
|             } | ||||
|         } | ||||
|         return false; | ||||
|     } | ||||
|  | ||||
|     public void clear() { | ||||
|         int size = this.mGraph.size(); | ||||
|         for (int i = 0; i < size; i++) { | ||||
|             ArrayList<T> valueAt = this.mGraph.valueAt(i); | ||||
|             if (valueAt != null) { | ||||
|                 poolList(valueAt); | ||||
|             } | ||||
|         } | ||||
|         this.mGraph.clear(); | ||||
|     } | ||||
|  | ||||
|     public ArrayList<T> getSortedList() { | ||||
|         this.mSortResult.clear(); | ||||
|         this.mSortTmpMarked.clear(); | ||||
|         int size = this.mGraph.size(); | ||||
|         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<T> arrayList, HashSet<T> hashSet) { | ||||
|         if (arrayList.contains(t)) { | ||||
|             return; | ||||
|         } | ||||
|         if (hashSet.contains(t)) { | ||||
|             throw new RuntimeException("This graph contains cyclic dependencies"); | ||||
|         } | ||||
|         hashSet.add(t); | ||||
|         ArrayList<T> 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.size(); | ||||
|     } | ||||
|  | ||||
|     private ArrayList<T> getEmptyList() { | ||||
|         ArrayList<T> acquire = this.mListPool.acquire(); | ||||
|         return acquire == null ? new ArrayList<>() : acquire; | ||||
|     } | ||||
|  | ||||
|     private void poolList(ArrayList<T> arrayList) { | ||||
|         arrayList.clear(); | ||||
|         this.mListPool.release(arrayList); | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user