package uk;

import java.io.Serializable;
import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.Comparator;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;

/* loaded from: classes3.dex */
public final class g extends AbstractMap implements Serializable {

    /* renamed from: w, reason: collision with root package name */
    private static final Comparator f37281w = new a();

    /* renamed from: p, reason: collision with root package name */
    Comparator f37282p;

    /* renamed from: q, reason: collision with root package name */
    e f37283q;

    /* renamed from: r, reason: collision with root package name */
    int f37284r;

    /* renamed from: s, reason: collision with root package name */
    int f37285s;

    /* renamed from: t, reason: collision with root package name */
    final e f37286t;

    /* renamed from: u, reason: collision with root package name */
    private b f37287u;

    /* renamed from: v, reason: collision with root package name */
    private c f37288v;

    /* loaded from: classes3.dex */
    class a implements Comparator {
        a() {
        }

        @Override // java.util.Comparator
        /* renamed from: a, reason: merged with bridge method [inline-methods] */
        public int compare(Comparable comparable, Comparable comparable2) {
            return comparable.compareTo(comparable2);
        }
    }

    /* loaded from: classes3.dex */
    class b extends AbstractSet {

        /* loaded from: classes3.dex */
        class a extends d {
            a() {
                super();
            }

            @Override // java.util.Iterator
            /* renamed from: b, reason: merged with bridge method [inline-methods] */
            public Map.Entry next() {
                return a();
            }
        }

        b() {
        }

        @Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
        public void clear() {
            g.this.clear();
        }

        @Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
        public boolean contains(Object obj) {
            return (obj instanceof Map.Entry) && g.this.c((Map.Entry) obj) != null;
        }

        @Override // java.util.AbstractCollection, java.util.Collection, java.lang.Iterable, java.util.Set
        public Iterator iterator() {
            return new a();
        }

        @Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
        public boolean remove(Object obj) {
            e c10;
            if (!(obj instanceof Map.Entry) || (c10 = g.this.c((Map.Entry) obj)) == null) {
                return false;
            }
            g.this.f(c10, true);
            return true;
        }

        @Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
        public int size() {
            return g.this.f37284r;
        }
    }

    /* loaded from: classes3.dex */
    final class c extends AbstractSet {

        /* loaded from: classes3.dex */
        class a extends d {
            a() {
                super();
            }

            @Override // java.util.Iterator
            public Object next() {
                return a().f37302u;
            }
        }

        c() {
        }

        @Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
        public void clear() {
            g.this.clear();
        }

        @Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
        public boolean contains(Object obj) {
            return g.this.containsKey(obj);
        }

        @Override // java.util.AbstractCollection, java.util.Collection, java.lang.Iterable, java.util.Set
        public Iterator iterator() {
            return new a();
        }

        @Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
        public boolean remove(Object obj) {
            return g.this.g(obj) != null;
        }

        @Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
        public int size() {
            return g.this.f37284r;
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes3.dex */
    public abstract class d implements Iterator {

        /* renamed from: p, reason: collision with root package name */
        e f37293p;

        /* renamed from: q, reason: collision with root package name */
        e f37294q = null;

        /* renamed from: r, reason: collision with root package name */
        int f37295r;

        d() {
            this.f37293p = g.this.f37286t.f37300s;
            this.f37295r = g.this.f37285s;
        }

        final e a() {
            e eVar = this.f37293p;
            g gVar = g.this;
            if (eVar == gVar.f37286t) {
                throw new NoSuchElementException();
            }
            if (gVar.f37285s != this.f37295r) {
                throw new ConcurrentModificationException();
            }
            this.f37293p = eVar.f37300s;
            this.f37294q = eVar;
            return eVar;
        }

        @Override // java.util.Iterator
        public final boolean hasNext() {
            return this.f37293p != g.this.f37286t;
        }

        @Override // java.util.Iterator
        public final void remove() {
            e eVar = this.f37294q;
            if (eVar == null) {
                throw new IllegalStateException();
            }
            g.this.f(eVar, true);
            this.f37294q = null;
            this.f37295r = g.this.f37285s;
        }
    }

    public g() {
        this(f37281w);
    }

    private boolean a(Object obj, Object obj2) {
        return obj == obj2 || (obj != null && obj.equals(obj2));
    }

    private void e(e eVar, boolean z10) {
        while (eVar != null) {
            e eVar2 = eVar.f37298q;
            e eVar3 = eVar.f37299r;
            int i10 = eVar2 != null ? eVar2.f37304w : 0;
            int i11 = eVar3 != null ? eVar3.f37304w : 0;
            int i12 = i10 - i11;
            if (i12 == -2) {
                e eVar4 = eVar3.f37298q;
                e eVar5 = eVar3.f37299r;
                int i13 = (eVar4 != null ? eVar4.f37304w : 0) - (eVar5 != null ? eVar5.f37304w : 0);
                if (i13 == -1 || (i13 == 0 && !z10)) {
                    j(eVar);
                } else {
                    m(eVar3);
                    j(eVar);
                }
                if (z10) {
                    return;
                }
            } else if (i12 == 2) {
                e eVar6 = eVar2.f37298q;
                e eVar7 = eVar2.f37299r;
                int i14 = (eVar6 != null ? eVar6.f37304w : 0) - (eVar7 != null ? eVar7.f37304w : 0);
                if (i14 == 1 || (i14 == 0 && !z10)) {
                    m(eVar);
                } else {
                    j(eVar2);
                    m(eVar);
                }
                if (z10) {
                    return;
                }
            } else if (i12 == 0) {
                eVar.f37304w = i10 + 1;
                if (z10) {
                    return;
                }
            } else {
                eVar.f37304w = Math.max(i10, i11) + 1;
                if (!z10) {
                    return;
                }
            }
            eVar = eVar.f37297p;
        }
    }

    private void h(e eVar, e eVar2) {
        e eVar3 = eVar.f37297p;
        eVar.f37297p = null;
        if (eVar2 != null) {
            eVar2.f37297p = eVar3;
        }
        if (eVar3 == null) {
            this.f37283q = eVar2;
        } else if (eVar3.f37298q == eVar) {
            eVar3.f37298q = eVar2;
        } else {
            eVar3.f37299r = eVar2;
        }
    }

    private void j(e eVar) {
        e eVar2 = eVar.f37298q;
        e eVar3 = eVar.f37299r;
        e eVar4 = eVar3.f37298q;
        e eVar5 = eVar3.f37299r;
        eVar.f37299r = eVar4;
        if (eVar4 != null) {
            eVar4.f37297p = eVar;
        }
        h(eVar, eVar3);
        eVar3.f37298q = eVar;
        eVar.f37297p = eVar3;
        int max = Math.max(eVar2 != null ? eVar2.f37304w : 0, eVar4 != null ? eVar4.f37304w : 0) + 1;
        eVar.f37304w = max;
        eVar3.f37304w = Math.max(max, eVar5 != null ? eVar5.f37304w : 0) + 1;
    }

    private void m(e eVar) {
        e eVar2 = eVar.f37298q;
        e eVar3 = eVar.f37299r;
        e eVar4 = eVar2.f37298q;
        e eVar5 = eVar2.f37299r;
        eVar.f37298q = eVar5;
        if (eVar5 != null) {
            eVar5.f37297p = eVar;
        }
        h(eVar, eVar2);
        eVar2.f37299r = eVar;
        eVar.f37297p = eVar2;
        int max = Math.max(eVar3 != null ? eVar3.f37304w : 0, eVar5 != null ? eVar5.f37304w : 0) + 1;
        eVar.f37304w = max;
        eVar2.f37304w = Math.max(max, eVar4 != null ? eVar4.f37304w : 0) + 1;
    }

    e b(Object obj, boolean z10) {
        int i10;
        e eVar;
        Comparator comparator = this.f37282p;
        e eVar2 = this.f37283q;
        if (eVar2 != null) {
            Comparable comparable = comparator == f37281w ? (Comparable) obj : null;
            while (true) {
                i10 = comparable != null ? comparable.compareTo(eVar2.f37302u) : comparator.compare(obj, eVar2.f37302u);
                if (i10 == 0) {
                    return eVar2;
                }
                e eVar3 = i10 < 0 ? eVar2.f37298q : eVar2.f37299r;
                if (eVar3 == null) {
                    break;
                }
                eVar2 = eVar3;
            }
        } else {
            i10 = 0;
        }
        if (!z10) {
            return null;
        }
        e eVar4 = this.f37286t;
        if (eVar2 != null) {
            eVar = new e(eVar2, obj, eVar4, eVar4.f37301t);
            if (i10 < 0) {
                eVar2.f37298q = eVar;
            } else {
                eVar2.f37299r = eVar;
            }
            e(eVar2, true);
        } else {
            if (comparator == f37281w && !(obj instanceof Comparable)) {
                throw new ClassCastException(obj.getClass().getName() + " is not Comparable");
            }
            eVar = new e(eVar2, obj, eVar4, eVar4.f37301t);
            this.f37283q = eVar;
        }
        this.f37284r++;
        this.f37285s++;
        return eVar;
    }

    e c(Map.Entry entry) {
        e d10 = d(entry.getKey());
        if (d10 != null && a(d10.f37303v, entry.getValue())) {
            return d10;
        }
        return null;
    }

    @Override // java.util.AbstractMap, java.util.Map
    public void clear() {
        this.f37283q = null;
        this.f37284r = 0;
        this.f37285s++;
        e eVar = this.f37286t;
        eVar.f37301t = eVar;
        eVar.f37300s = eVar;
    }

    @Override // java.util.AbstractMap, java.util.Map
    public boolean containsKey(Object obj) {
        return d(obj) != null;
    }

    e d(Object obj) {
        if (obj == null) {
            return null;
        }
        try {
            return b(obj, false);
        } catch (ClassCastException unused) {
            return null;
        }
    }

    @Override // java.util.AbstractMap, java.util.Map
    public Set entrySet() {
        b bVar = this.f37287u;
        if (bVar != null) {
            return bVar;
        }
        b bVar2 = new b();
        this.f37287u = bVar2;
        return bVar2;
    }

    void f(e eVar, boolean z10) {
        int i10;
        if (z10) {
            e eVar2 = eVar.f37301t;
            eVar2.f37300s = eVar.f37300s;
            eVar.f37300s.f37301t = eVar2;
        }
        e eVar3 = eVar.f37298q;
        e eVar4 = eVar.f37299r;
        e eVar5 = eVar.f37297p;
        int i11 = 0;
        if (eVar3 == null || eVar4 == null) {
            if (eVar3 != null) {
                h(eVar, eVar3);
                eVar.f37298q = null;
            } else if (eVar4 != null) {
                h(eVar, eVar4);
                eVar.f37299r = null;
            } else {
                h(eVar, null);
            }
            e(eVar5, false);
            this.f37284r--;
            this.f37285s++;
            return;
        }
        e b10 = eVar3.f37304w > eVar4.f37304w ? eVar3.b() : eVar4.a();
        f(b10, false);
        e eVar6 = eVar.f37298q;
        if (eVar6 != null) {
            i10 = eVar6.f37304w;
            b10.f37298q = eVar6;
            eVar6.f37297p = b10;
            eVar.f37298q = null;
        } else {
            i10 = 0;
        }
        e eVar7 = eVar.f37299r;
        if (eVar7 != null) {
            i11 = eVar7.f37304w;
            b10.f37299r = eVar7;
            eVar7.f37297p = b10;
            eVar.f37299r = null;
        }
        b10.f37304w = Math.max(i10, i11) + 1;
        h(eVar, b10);
    }

    e g(Object obj) {
        e d10 = d(obj);
        if (d10 != null) {
            f(d10, true);
        }
        return d10;
    }

    @Override // java.util.AbstractMap, java.util.Map
    public Object get(Object obj) {
        e d10 = d(obj);
        if (d10 != null) {
            return d10.f37303v;
        }
        return null;
    }

    @Override // java.util.AbstractMap, java.util.Map
    public Set keySet() {
        c cVar = this.f37288v;
        if (cVar != null) {
            return cVar;
        }
        c cVar2 = new c();
        this.f37288v = cVar2;
        return cVar2;
    }

    @Override // java.util.AbstractMap, java.util.Map
    public Object put(Object obj, Object obj2) {
        if (obj == null) {
            throw new NullPointerException("key == null");
        }
        e b10 = b(obj, true);
        Object obj3 = b10.f37303v;
        b10.f37303v = obj2;
        return obj3;
    }

    @Override // java.util.AbstractMap, java.util.Map
    public Object remove(Object obj) {
        e g10 = g(obj);
        if (g10 != null) {
            return g10.f37303v;
        }
        return null;
    }

    @Override // java.util.AbstractMap, java.util.Map
    public int size() {
        return this.f37284r;
    }

    public g(Comparator comparator) {
        this.f37284r = 0;
        this.f37285s = 0;
        this.f37286t = new e();
        this.f37282p = comparator == null ? f37281w : comparator;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes3.dex */
    public static final class e implements Map.Entry {

        /* renamed from: p, reason: collision with root package name */
        e f37297p;

        /* renamed from: q, reason: collision with root package name */
        e f37298q;

        /* renamed from: r, reason: collision with root package name */
        e f37299r;

        /* renamed from: s, reason: collision with root package name */
        e f37300s;

        /* renamed from: t, reason: collision with root package name */
        e f37301t;

        /* renamed from: u, reason: collision with root package name */
        final Object f37302u;

        /* renamed from: v, reason: collision with root package name */
        Object f37303v;

        /* renamed from: w, reason: collision with root package name */
        int f37304w;

        e() {
            this.f37302u = null;
            this.f37301t = this;
            this.f37300s = this;
        }

        public e a() {
            e eVar = this.f37298q;
            while (true) {
                e eVar2 = eVar;
                e eVar3 = this;
                this = eVar2;
                if (this == null) {
                    return eVar3;
                }
                eVar = this.f37298q;
            }
        }

        public e b() {
            e eVar = this.f37299r;
            while (true) {
                e eVar2 = eVar;
                e eVar3 = this;
                this = eVar2;
                if (this == null) {
                    return eVar3;
                }
                eVar = this.f37299r;
            }
        }

        @Override // java.util.Map.Entry
        public boolean equals(Object obj) {
            if (!(obj instanceof Map.Entry)) {
                return false;
            }
            Map.Entry entry = (Map.Entry) obj;
            Object obj2 = this.f37302u;
            if (obj2 == null) {
                if (entry.getKey() != null) {
                    return false;
                }
            } else if (!obj2.equals(entry.getKey())) {
                return false;
            }
            Object obj3 = this.f37303v;
            if (obj3 == null) {
                if (entry.getValue() != null) {
                    return false;
                }
            } else if (!obj3.equals(entry.getValue())) {
                return false;
            }
            return true;
        }

        @Override // java.util.Map.Entry
        public Object getKey() {
            return this.f37302u;
        }

        @Override // java.util.Map.Entry
        public Object getValue() {
            return this.f37303v;
        }

        @Override // java.util.Map.Entry
        public int hashCode() {
            Object obj = this.f37302u;
            int hashCode = obj == null ? 0 : obj.hashCode();
            Object obj2 = this.f37303v;
            return hashCode ^ (obj2 != null ? obj2.hashCode() : 0);
        }

        @Override // java.util.Map.Entry
        public Object setValue(Object obj) {
            Object obj2 = this.f37303v;
            this.f37303v = obj;
            return obj2;
        }

        public String toString() {
            return this.f37302u + "=" + this.f37303v;
        }

        e(e eVar, Object obj, e eVar2, e eVar3) {
            this.f37297p = eVar;
            this.f37302u = obj;
            this.f37304w = 1;
            this.f37300s = eVar2;
            this.f37301t = eVar3;
            eVar3.f37300s = this;
            eVar2.f37301t = this;
        }
    }
}
