package androidx.coordinatorlayout.widget;

import androidx.core.util.e;
import androidx.core.util.f;
import j.g;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/* JADX INFO: compiled from: DirectedAcyclicGraph.java */
/* JADX INFO: loaded from: classes.dex */
public final class a<T> {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private final e<ArrayList<T>> f1593a = new f(10);

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private final g<T, ArrayList<T>> f1594b = new g<>();

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private final ArrayList<T> f1595c = new ArrayList<>();

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private final HashSet<T> f1596d = new HashSet<>();

    private void e(T t2, ArrayList<T> arrayList, HashSet<T> hashSet) {
        if (arrayList.contains(t2)) {
            return;
        }
        if (hashSet.contains(t2)) {
            throw new RuntimeException("This graph contains cyclic dependencies");
        }
        hashSet.add(t2);
        ArrayList<T> arrayList2 = this.f1594b.get(t2);
        if (arrayList2 != null) {
            int size = arrayList2.size();
            for (int i3 = 0; i3 < size; i3++) {
                e(arrayList2.get(i3), arrayList, hashSet);
            }
        }
        hashSet.remove(t2);
        arrayList.add(t2);
    }

    private ArrayList<T> f() {
        ArrayList<T> arrayListB = this.f1593a.b();
        return arrayListB == null ? new ArrayList<>() : arrayListB;
    }

    private void k(ArrayList<T> arrayList) {
        arrayList.clear();
        this.f1593a.a(arrayList);
    }

    public void a(T t2, T t3) {
        if (!this.f1594b.containsKey(t2) || !this.f1594b.containsKey(t3)) {
            throw new IllegalArgumentException("All nodes must be present in the graph before being added as an edge");
        }
        ArrayList<T> arrayListF = this.f1594b.get(t2);
        if (arrayListF == null) {
            arrayListF = f();
            this.f1594b.put(t2, arrayListF);
        }
        arrayListF.add(t3);
    }

    public void b(T t2) {
        if (this.f1594b.containsKey(t2)) {
            return;
        }
        this.f1594b.put(t2, null);
    }

    public void c() {
        int size = this.f1594b.size();
        for (int i3 = 0; i3 < size; i3++) {
            ArrayList<T> arrayListM = this.f1594b.m(i3);
            if (arrayListM != null) {
                k(arrayListM);
            }
        }
        this.f1594b.clear();
    }

    public boolean d(T t2) {
        return this.f1594b.containsKey(t2);
    }

    public List g(T t2) {
        return this.f1594b.get(t2);
    }

    public List<T> h(T t2) {
        int size = this.f1594b.size();
        ArrayList arrayList = null;
        for (int i3 = 0; i3 < size; i3++) {
            ArrayList<T> arrayListM = this.f1594b.m(i3);
            if (arrayListM != null && arrayListM.contains(t2)) {
                if (arrayList == null) {
                    arrayList = new ArrayList();
                }
                arrayList.add(this.f1594b.i(i3));
            }
        }
        return arrayList;
    }

    public ArrayList<T> i() {
        this.f1595c.clear();
        this.f1596d.clear();
        int size = this.f1594b.size();
        for (int i3 = 0; i3 < size; i3++) {
            e(this.f1594b.i(i3), this.f1595c, this.f1596d);
        }
        return this.f1595c;
    }

    public boolean j(T t2) {
        int size = this.f1594b.size();
        for (int i3 = 0; i3 < size; i3++) {
            ArrayList<T> arrayListM = this.f1594b.m(i3);
            if (arrayListM != null && arrayListM.contains(t2)) {
                return true;
            }
        }
        return false;
    }
}
