|
| 1 | +package de.dlyt.yanndroid.oneui.sesl.coordinatorlayout; |
| 2 | + |
| 3 | +import androidx.annotation.NonNull; |
| 4 | +import androidx.annotation.Nullable; |
| 5 | +import androidx.collection.SimpleArrayMap; |
| 6 | +import androidx.core.util.Pools; |
| 7 | + |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.HashSet; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +public final class DirectedAcyclicGraph<T> { |
| 13 | + private final Pools.Pool<ArrayList<T>> mListPool = new Pools.SimplePool<>(10); |
| 14 | + private final SimpleArrayMap<T, ArrayList<T>> mGraph = new SimpleArrayMap<>(); |
| 15 | + private final ArrayList<T> mSortResult = new ArrayList<>(); |
| 16 | + private final HashSet<T> mSortTmpMarked = new HashSet<>(); |
| 17 | + |
| 18 | + public void addNode(@NonNull T node) { |
| 19 | + if (!mGraph.containsKey(node)) { |
| 20 | + mGraph.put(node, null); |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + public boolean contains(@NonNull T node) { |
| 25 | + return mGraph.containsKey(node); |
| 26 | + } |
| 27 | + |
| 28 | + public void addEdge(@NonNull T node, @NonNull T incomingEdge) { |
| 29 | + if (!mGraph.containsKey(node) || !mGraph.containsKey(incomingEdge)) { |
| 30 | + throw new IllegalArgumentException("All nodes must be present in the graph before being added as an edge"); |
| 31 | + } |
| 32 | + |
| 33 | + ArrayList<T> edges = mGraph.get(node); |
| 34 | + if (edges == null) { |
| 35 | + edges = getEmptyList(); |
| 36 | + mGraph.put(node, edges); |
| 37 | + } |
| 38 | + edges.add(incomingEdge); |
| 39 | + } |
| 40 | + |
| 41 | + @Nullable |
| 42 | + public List<T> getIncomingEdges(@NonNull T node) { |
| 43 | + return mGraph.get(node); |
| 44 | + } |
| 45 | + |
| 46 | + @Nullable |
| 47 | + public List<T> getOutgoingEdges(@NonNull T node) { |
| 48 | + ArrayList<T> result = null; |
| 49 | + for (int i = 0, size = mGraph.size(); i < size; i++) { |
| 50 | + ArrayList<T> edges = mGraph.valueAt(i); |
| 51 | + if (edges != null && edges.contains(node)) { |
| 52 | + if (result == null) { |
| 53 | + result = new ArrayList<>(); |
| 54 | + } |
| 55 | + result.add(mGraph.keyAt(i)); |
| 56 | + } |
| 57 | + } |
| 58 | + return result; |
| 59 | + } |
| 60 | + |
| 61 | + public boolean hasOutgoingEdges(@NonNull T node) { |
| 62 | + for (int i = 0, size = mGraph.size(); i < size; i++) { |
| 63 | + ArrayList<T> edges = mGraph.valueAt(i); |
| 64 | + if (edges != null && edges.contains(node)) { |
| 65 | + return true; |
| 66 | + } |
| 67 | + } |
| 68 | + return false; |
| 69 | + } |
| 70 | + |
| 71 | + public void clear() { |
| 72 | + for (int i = 0, size = mGraph.size(); i < size; i++) { |
| 73 | + ArrayList<T> edges = mGraph.valueAt(i); |
| 74 | + if (edges != null) { |
| 75 | + poolList(edges); |
| 76 | + } |
| 77 | + } |
| 78 | + mGraph.clear(); |
| 79 | + } |
| 80 | + |
| 81 | + @NonNull |
| 82 | + public ArrayList<T> getSortedList() { |
| 83 | + mSortResult.clear(); |
| 84 | + mSortTmpMarked.clear(); |
| 85 | + |
| 86 | + for (int i = 0, size = mGraph.size(); i < size; i++) { |
| 87 | + dfs(mGraph.keyAt(i), mSortResult, mSortTmpMarked); |
| 88 | + } |
| 89 | + |
| 90 | + return mSortResult; |
| 91 | + } |
| 92 | + |
| 93 | + private void dfs(final T node, final ArrayList<T> result, final HashSet<T> tmpMarked) { |
| 94 | + if (result.contains(node)) { |
| 95 | + return; |
| 96 | + } |
| 97 | + if (tmpMarked.contains(node)) { |
| 98 | + throw new RuntimeException("This graph contains cyclic dependencies"); |
| 99 | + } |
| 100 | + tmpMarked.add(node); |
| 101 | + final ArrayList<T> edges = mGraph.get(node); |
| 102 | + if (edges != null) { |
| 103 | + for (int i = 0, size = edges.size(); i < size; i++) { |
| 104 | + dfs(edges.get(i), result, tmpMarked); |
| 105 | + } |
| 106 | + } |
| 107 | + tmpMarked.remove(node); |
| 108 | + result.add(node); |
| 109 | + } |
| 110 | + |
| 111 | + int size() { |
| 112 | + return mGraph.size(); |
| 113 | + } |
| 114 | + |
| 115 | + @NonNull |
| 116 | + private ArrayList<T> getEmptyList() { |
| 117 | + ArrayList<T> list = mListPool.acquire(); |
| 118 | + if (list == null) { |
| 119 | + list = new ArrayList<>(); |
| 120 | + } |
| 121 | + return list; |
| 122 | + } |
| 123 | + |
| 124 | + private void poolList(@NonNull ArrayList<T> list) { |
| 125 | + list.clear(); |
| 126 | + mListPool.release(list); |
| 127 | + } |
| 128 | +} |
0 commit comments