package androidx.coordinatorlayout.widget;

import c.d.g;
import c.f.k.c;
import c.f.k.d;
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> {
    private final c<ArrayList<T>> a = new d(10);

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

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

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

    private void e(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.f598b.get(t);
        if (arrayList2 != null) {
            int size = arrayList2.size();
            for (int i2 = 0; i2 < size; i2++) {
                e(arrayList2.get(i2), arrayList, hashSet);
            }
        }
        hashSet.remove(t);
        arrayList.add(t);
    }

    private ArrayList<T> f() {
        ArrayList<T> arrayListAcquire = this.a.acquire();
        return arrayListAcquire == null ? new ArrayList<>() : arrayListAcquire;
    }

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

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

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

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

    public boolean d(T t) {
        return this.f598b.containsKey(t);
    }

    public List g(T t) {
        return this.f598b.get(t);
    }

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

    public ArrayList<T> i() {
        this.f599c.clear();
        this.f600d.clear();
        int size = this.f598b.size();
        for (int i2 = 0; i2 < size; i2++) {
            e(this.f598b.i(i2), this.f599c, this.f600d);
        }
        return this.f599c;
    }

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