package c.d;

import java.util.ConcurrentModificationException;
import java.util.Map;

/* JADX INFO: loaded from: classes.dex */
public class h<K, V> {

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static Object[] f5146b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public static int f5147c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static Object[] f5148d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static int f5149e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public int[] f5150f = d.f5116a;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public Object[] f5151g = d.f5118c;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public int f5152h = 0;

    public static void b(int[] iArr, Object[] objArr, int i2) {
        if (iArr.length == 8) {
            synchronized (h.class) {
                if (f5149e < 10) {
                    objArr[0] = f5148d;
                    objArr[1] = iArr;
                    for (int i3 = (i2 << 1) - 1; i3 >= 2; i3--) {
                        objArr[i3] = null;
                    }
                    f5148d = objArr;
                    f5149e++;
                }
            }
            return;
        }
        if (iArr.length == 4) {
            synchronized (h.class) {
                if (f5147c < 10) {
                    objArr[0] = f5146b;
                    objArr[1] = iArr;
                    for (int i4 = (i2 << 1) - 1; i4 >= 2; i4--) {
                        objArr[i4] = null;
                    }
                    f5146b = objArr;
                    f5147c++;
                }
            }
        }
    }

    public final void a(int i2) {
        if (i2 == 8) {
            synchronized (h.class) {
                Object[] objArr = f5148d;
                if (objArr != null) {
                    this.f5151g = objArr;
                    f5148d = (Object[]) objArr[0];
                    this.f5150f = (int[]) objArr[1];
                    objArr[1] = null;
                    objArr[0] = null;
                    f5149e--;
                    return;
                }
            }
        } else if (i2 == 4) {
            synchronized (h.class) {
                Object[] objArr2 = f5146b;
                if (objArr2 != null) {
                    this.f5151g = objArr2;
                    f5146b = (Object[]) objArr2[0];
                    this.f5150f = (int[]) objArr2[1];
                    objArr2[1] = null;
                    objArr2[0] = null;
                    f5147c--;
                    return;
                }
            }
        }
        this.f5150f = new int[i2];
        this.f5151g = new Object[i2 << 1];
    }

    public int c(Object obj, int i2) {
        int i3 = this.f5152h;
        if (i3 == 0) {
            return -1;
        }
        try {
            int iA = d.a(this.f5150f, i3, i2);
            if (iA < 0 || obj.equals(this.f5151g[iA << 1])) {
                return iA;
            }
            int i4 = iA + 1;
            while (i4 < i3 && this.f5150f[i4] == i2) {
                if (obj.equals(this.f5151g[i4 << 1])) {
                    return i4;
                }
                i4++;
            }
            for (int i5 = iA - 1; i5 >= 0 && this.f5150f[i5] == i2; i5--) {
                if (obj.equals(this.f5151g[i5 << 1])) {
                    return i5;
                }
            }
            return ~i4;
        } catch (ArrayIndexOutOfBoundsException unused) {
            throw new ConcurrentModificationException();
        }
    }

    public void clear() {
        int i2 = this.f5152h;
        if (i2 > 0) {
            int[] iArr = this.f5150f;
            Object[] objArr = this.f5151g;
            this.f5150f = d.f5116a;
            this.f5151g = d.f5118c;
            this.f5152h = 0;
            b(iArr, objArr, i2);
        }
        if (this.f5152h > 0) {
            throw new ConcurrentModificationException();
        }
    }

    public boolean containsKey(Object obj) {
        return d(obj) >= 0;
    }

    public boolean containsValue(Object obj) {
        return f(obj) >= 0;
    }

    public int d(Object obj) {
        return obj == null ? e() : c(obj, obj.hashCode());
    }

    public int e() {
        int i2 = this.f5152h;
        if (i2 == 0) {
            return -1;
        }
        try {
            int iA = d.a(this.f5150f, i2, 0);
            if (iA < 0 || this.f5151g[iA << 1] == null) {
                return iA;
            }
            int i3 = iA + 1;
            while (i3 < i2 && this.f5150f[i3] == 0) {
                if (this.f5151g[i3 << 1] == null) {
                    return i3;
                }
                i3++;
            }
            for (int i4 = iA - 1; i4 >= 0 && this.f5150f[i4] == 0; i4--) {
                if (this.f5151g[i4 << 1] == null) {
                    return i4;
                }
            }
            return ~i3;
        } catch (ArrayIndexOutOfBoundsException unused) {
            throw new ConcurrentModificationException();
        }
    }

    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof h) {
            h hVar = (h) obj;
            if (this.f5152h != hVar.f5152h) {
                return false;
            }
            for (int i2 = 0; i2 < this.f5152h; i2++) {
                try {
                    K kG = g(i2);
                    V vJ = j(i2);
                    Object obj2 = hVar.get(kG);
                    if (vJ == null) {
                        if (obj2 != null || !hVar.containsKey(kG)) {
                            return false;
                        }
                    } else if (!vJ.equals(obj2)) {
                        return false;
                    }
                } catch (ClassCastException | NullPointerException unused) {
                    return false;
                }
            }
            return true;
        }
        if (obj instanceof Map) {
            Map map = (Map) obj;
            if (this.f5152h != map.size()) {
                return false;
            }
            for (int i3 = 0; i3 < this.f5152h; i3++) {
                try {
                    K kG2 = g(i3);
                    V vJ2 = j(i3);
                    Object obj3 = map.get(kG2);
                    if (vJ2 == null) {
                        if (obj3 != null || !map.containsKey(kG2)) {
                            return false;
                        }
                    } else if (!vJ2.equals(obj3)) {
                        return false;
                    }
                } catch (ClassCastException | NullPointerException unused2) {
                }
            }
            return true;
        }
        return false;
    }

    public int f(Object obj) {
        int i2 = this.f5152h * 2;
        Object[] objArr = this.f5151g;
        if (obj == null) {
            for (int i3 = 1; i3 < i2; i3 += 2) {
                if (objArr[i3] == null) {
                    return i3 >> 1;
                }
            }
            return -1;
        }
        for (int i4 = 1; i4 < i2; i4 += 2) {
            if (obj.equals(objArr[i4])) {
                return i4 >> 1;
            }
        }
        return -1;
    }

    public K g(int i2) {
        return (K) this.f5151g[i2 << 1];
    }

    public V get(Object obj) {
        return getOrDefault(obj, null);
    }

    public V getOrDefault(Object obj, V v) {
        int iD = d(obj);
        return iD >= 0 ? (V) this.f5151g[(iD << 1) + 1] : v;
    }

    public V h(int i2) {
        Object[] objArr = this.f5151g;
        int i3 = i2 << 1;
        V v = (V) objArr[i3 + 1];
        int i4 = this.f5152h;
        int i5 = 0;
        if (i4 <= 1) {
            b(this.f5150f, objArr, i4);
            this.f5150f = d.f5116a;
            this.f5151g = d.f5118c;
        } else {
            int i6 = i4 - 1;
            int[] iArr = this.f5150f;
            if (iArr.length <= 8 || i4 >= iArr.length / 3) {
                if (i2 < i6) {
                    int i7 = i2 + 1;
                    int i8 = i6 - i2;
                    System.arraycopy(iArr, i7, iArr, i2, i8);
                    Object[] objArr2 = this.f5151g;
                    System.arraycopy(objArr2, i7 << 1, objArr2, i3, i8 << 1);
                }
                Object[] objArr3 = this.f5151g;
                int i9 = i6 << 1;
                objArr3[i9] = null;
                objArr3[i9 + 1] = null;
            } else {
                a(i4 > 8 ? i4 + (i4 >> 1) : 8);
                if (i4 != this.f5152h) {
                    throw new ConcurrentModificationException();
                }
                if (i2 > 0) {
                    System.arraycopy(iArr, 0, this.f5150f, 0, i2);
                    System.arraycopy(objArr, 0, this.f5151g, 0, i3);
                }
                if (i2 < i6) {
                    int i10 = i2 + 1;
                    int i11 = i6 - i2;
                    System.arraycopy(iArr, i10, this.f5150f, i2, i11);
                    System.arraycopy(objArr, i10 << 1, this.f5151g, i3, i11 << 1);
                }
            }
            i5 = i6;
        }
        if (i4 != this.f5152h) {
            throw new ConcurrentModificationException();
        }
        this.f5152h = i5;
        return v;
    }

    public int hashCode() {
        int[] iArr = this.f5150f;
        Object[] objArr = this.f5151g;
        int i2 = this.f5152h;
        int i3 = 1;
        int i4 = 0;
        int iHashCode = 0;
        while (i4 < i2) {
            Object obj = objArr[i3];
            iHashCode += (obj == null ? 0 : obj.hashCode()) ^ iArr[i4];
            i4++;
            i3 += 2;
        }
        return iHashCode;
    }

    public V i(int i2, V v) {
        int i3 = (i2 << 1) + 1;
        Object[] objArr = this.f5151g;
        V v2 = (V) objArr[i3];
        objArr[i3] = v;
        return v2;
    }

    public boolean isEmpty() {
        return this.f5152h <= 0;
    }

    public V j(int i2) {
        return (V) this.f5151g[(i2 << 1) + 1];
    }

    public V put(K k, V v) {
        int i2;
        int iC;
        int i3 = this.f5152h;
        if (k == null) {
            iC = e();
            i2 = 0;
        } else {
            int iHashCode = k.hashCode();
            i2 = iHashCode;
            iC = c(k, iHashCode);
        }
        if (iC >= 0) {
            int i4 = (iC << 1) + 1;
            Object[] objArr = this.f5151g;
            V v2 = (V) objArr[i4];
            objArr[i4] = v;
            return v2;
        }
        int i5 = ~iC;
        int[] iArr = this.f5150f;
        if (i3 >= iArr.length) {
            int i6 = 4;
            if (i3 >= 8) {
                i6 = (i3 >> 1) + i3;
            } else if (i3 >= 4) {
                i6 = 8;
            }
            Object[] objArr2 = this.f5151g;
            a(i6);
            if (i3 != this.f5152h) {
                throw new ConcurrentModificationException();
            }
            int[] iArr2 = this.f5150f;
            if (iArr2.length > 0) {
                System.arraycopy(iArr, 0, iArr2, 0, iArr.length);
                System.arraycopy(objArr2, 0, this.f5151g, 0, objArr2.length);
            }
            b(iArr, objArr2, i3);
        }
        if (i5 < i3) {
            int[] iArr3 = this.f5150f;
            int i7 = i5 + 1;
            System.arraycopy(iArr3, i5, iArr3, i7, i3 - i5);
            Object[] objArr3 = this.f5151g;
            System.arraycopy(objArr3, i5 << 1, objArr3, i7 << 1, (this.f5152h - i5) << 1);
        }
        int i8 = this.f5152h;
        if (i3 == i8) {
            int[] iArr4 = this.f5150f;
            if (i5 < iArr4.length) {
                iArr4[i5] = i2;
                Object[] objArr4 = this.f5151g;
                int i9 = i5 << 1;
                objArr4[i9] = k;
                objArr4[i9 + 1] = v;
                this.f5152h = i8 + 1;
                return null;
            }
        }
        throw new ConcurrentModificationException();
    }

    public V putIfAbsent(K k, V v) {
        V orDefault = getOrDefault(k, null);
        return orDefault == null ? put(k, v) : orDefault;
    }

    public V remove(Object obj) {
        int iD = d(obj);
        if (iD >= 0) {
            return h(iD);
        }
        return null;
    }

    public boolean remove(Object obj, Object obj2) {
        int iD = d(obj);
        if (iD < 0) {
            return false;
        }
        V vJ = j(iD);
        if (obj2 != vJ && (obj2 == null || !obj2.equals(vJ))) {
            return false;
        }
        h(iD);
        return true;
    }

    public V replace(K k, V v) {
        int iD = d(k);
        if (iD >= 0) {
            return i(iD, v);
        }
        return null;
    }

    public boolean replace(K k, V v, V v2) {
        int iD = d(k);
        if (iD < 0) {
            return false;
        }
        V vJ = j(iD);
        if (vJ != v && (v == null || !v.equals(vJ))) {
            return false;
        }
        i(iD, v2);
        return true;
    }

    public int size() {
        return this.f5152h;
    }

    public String toString() {
        if (isEmpty()) {
            return "{}";
        }
        StringBuilder sb = new StringBuilder(this.f5152h * 28);
        sb.append('{');
        for (int i2 = 0; i2 < this.f5152h; i2++) {
            if (i2 > 0) {
                sb.append(", ");
            }
            K kG = g(i2);
            if (kG != this) {
                sb.append(kG);
            } else {
                sb.append("(this Map)");
            }
            sb.append('=');
            V vJ = j(i2);
            if (vJ != this) {
                sb.append(vJ);
            } else {
                sb.append("(this Map)");
            }
        }
        sb.append('}');
        return sb.toString();
    }
}
