package a.b;

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

/* JADX INFO: compiled from: SimpleArrayMap.java */
/* JADX INFO: loaded from: classes.dex */
public class f<K, V> {

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    static Object[] f138c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    static int f139d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    static Object[] f140e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    static int f141f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    int[] f142g;
    Object[] h;
    int i;

    public f() {
        this.f142g = b.f108a;
        this.h = b.f110c;
        this.i = 0;
    }

    private void b(int i) {
        if (i == 8) {
            synchronized (a.class) {
                Object[] objArr = f140e;
                if (objArr != null) {
                    this.h = objArr;
                    f140e = (Object[]) objArr[0];
                    this.f142g = (int[]) objArr[1];
                    objArr[1] = null;
                    objArr[0] = null;
                    f141f--;
                    return;
                }
            }
        } else if (i == 4) {
            synchronized (a.class) {
                Object[] objArr2 = f138c;
                if (objArr2 != null) {
                    this.h = objArr2;
                    f138c = (Object[]) objArr2[0];
                    this.f142g = (int[]) objArr2[1];
                    objArr2[1] = null;
                    objArr2[0] = null;
                    f139d--;
                    return;
                }
            }
        }
        this.f142g = new int[i];
        this.h = new Object[i << 1];
    }

    private static int c(int[] iArr, int i, int i2) {
        try {
            return b.a(iArr, i, i2);
        } catch (ArrayIndexOutOfBoundsException unused) {
            throw new ConcurrentModificationException();
        }
    }

    private static void e(int[] iArr, Object[] objArr, int i) {
        if (iArr.length == 8) {
            synchronized (a.class) {
                if (f141f < 10) {
                    objArr[0] = f140e;
                    objArr[1] = iArr;
                    for (int i2 = (i << 1) - 1; i2 >= 2; i2--) {
                        objArr[i2] = null;
                    }
                    f140e = objArr;
                    f141f++;
                }
            }
            return;
        }
        if (iArr.length == 4) {
            synchronized (a.class) {
                if (f139d < 10) {
                    objArr[0] = f138c;
                    objArr[1] = iArr;
                    for (int i3 = (i << 1) - 1; i3 >= 2; i3--) {
                        objArr[i3] = null;
                    }
                    f138c = objArr;
                    f139d++;
                }
            }
        }
    }

    public void clear() {
        int i = this.i;
        if (i > 0) {
            int[] iArr = this.f142g;
            Object[] objArr = this.h;
            this.f142g = b.f108a;
            this.h = b.f110c;
            this.i = 0;
            e(iArr, objArr, i);
        }
        if (this.i > 0) {
            throw new ConcurrentModificationException();
        }
    }

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

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

    public void d(int i) {
        int i2 = this.i;
        int[] iArr = this.f142g;
        if (iArr.length < i) {
            Object[] objArr = this.h;
            b(i);
            if (this.i > 0) {
                System.arraycopy(iArr, 0, this.f142g, 0, i2);
                System.arraycopy(objArr, 0, this.h, 0, i2 << 1);
            }
            e(iArr, objArr, i2);
        }
        if (this.i != i2) {
            throw new ConcurrentModificationException();
        }
    }

    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof f) {
            f fVar = (f) obj;
            if (size() != fVar.size()) {
                return false;
            }
            for (int i = 0; i < this.i; i++) {
                try {
                    K kJ = j(i);
                    V vM = m(i);
                    Object obj2 = fVar.get(kJ);
                    if (vM == null) {
                        if (obj2 != null || !fVar.containsKey(kJ)) {
                            return false;
                        }
                    } else if (!vM.equals(obj2)) {
                        return false;
                    }
                } catch (ClassCastException | NullPointerException unused) {
                    return false;
                }
            }
            return true;
        }
        if (obj instanceof Map) {
            Map map = (Map) obj;
            if (size() != map.size()) {
                return false;
            }
            for (int i2 = 0; i2 < this.i; i2++) {
                try {
                    K kJ2 = j(i2);
                    V vM2 = m(i2);
                    Object obj3 = map.get(kJ2);
                    if (vM2 == null) {
                        if (obj3 != null || !map.containsKey(kJ2)) {
                            return false;
                        }
                    } else if (!vM2.equals(obj3)) {
                        return false;
                    }
                } catch (ClassCastException | NullPointerException unused2) {
                }
            }
            return true;
        }
        return false;
    }

    int f(Object obj, int i) {
        int i2 = this.i;
        if (i2 == 0) {
            return -1;
        }
        int iC = c(this.f142g, i2, i);
        if (iC < 0 || obj.equals(this.h[iC << 1])) {
            return iC;
        }
        int i3 = iC + 1;
        while (i3 < i2 && this.f142g[i3] == i) {
            if (obj.equals(this.h[i3 << 1])) {
                return i3;
            }
            i3++;
        }
        for (int i4 = iC - 1; i4 >= 0 && this.f142g[i4] == i; i4--) {
            if (obj.equals(this.h[i4 << 1])) {
                return i4;
            }
        }
        return ~i3;
    }

    public int g(Object obj) {
        return obj == null ? h() : f(obj, obj.hashCode());
    }

    public V get(Object obj) {
        int iG = g(obj);
        if (iG >= 0) {
            return (V) this.h[(iG << 1) + 1];
        }
        return null;
    }

    int h() {
        int i = this.i;
        if (i == 0) {
            return -1;
        }
        int iC = c(this.f142g, i, 0);
        if (iC < 0 || this.h[iC << 1] == null) {
            return iC;
        }
        int i2 = iC + 1;
        while (i2 < i && this.f142g[i2] == 0) {
            if (this.h[i2 << 1] == null) {
                return i2;
            }
            i2++;
        }
        for (int i3 = iC - 1; i3 >= 0 && this.f142g[i3] == 0; i3--) {
            if (this.h[i3 << 1] == null) {
                return i3;
            }
        }
        return ~i2;
    }

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

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

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

    public K j(int i) {
        return (K) this.h[i << 1];
    }

    public V k(int i) {
        Object[] objArr = this.h;
        int i2 = i << 1;
        V v = (V) objArr[i2 + 1];
        int i3 = this.i;
        int i4 = 0;
        if (i3 <= 1) {
            e(this.f142g, objArr, i3);
            this.f142g = b.f108a;
            this.h = b.f110c;
        } else {
            int i5 = i3 - 1;
            int[] iArr = this.f142g;
            if (iArr.length <= 8 || i3 >= iArr.length / 3) {
                if (i < i5) {
                    int i6 = i + 1;
                    int i7 = i5 - i;
                    System.arraycopy(iArr, i6, iArr, i, i7);
                    Object[] objArr2 = this.h;
                    System.arraycopy(objArr2, i6 << 1, objArr2, i2, i7 << 1);
                }
                Object[] objArr3 = this.h;
                int i8 = i5 << 1;
                objArr3[i8] = null;
                objArr3[i8 + 1] = null;
            } else {
                b(i3 > 8 ? i3 + (i3 >> 1) : 8);
                if (i3 != this.i) {
                    throw new ConcurrentModificationException();
                }
                if (i > 0) {
                    System.arraycopy(iArr, 0, this.f142g, 0, i);
                    System.arraycopy(objArr, 0, this.h, 0, i2);
                }
                if (i < i5) {
                    int i9 = i + 1;
                    int i10 = i5 - i;
                    System.arraycopy(iArr, i9, this.f142g, i, i10);
                    System.arraycopy(objArr, i9 << 1, this.h, i2, i10 << 1);
                }
            }
            i4 = i5;
        }
        if (i3 != this.i) {
            throw new ConcurrentModificationException();
        }
        this.i = i4;
        return v;
    }

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

    public V m(int i) {
        return (V) this.h[(i << 1) + 1];
    }

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

    public V remove(Object obj) {
        int iG = g(obj);
        if (iG >= 0) {
            return k(iG);
        }
        return null;
    }

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

    public String toString() {
        if (isEmpty()) {
            return "{}";
        }
        StringBuilder sb = new StringBuilder(this.i * 28);
        sb.append('{');
        for (int i = 0; i < this.i; i++) {
            if (i > 0) {
                sb.append(", ");
            }
            K kJ = j(i);
            if (kJ != this) {
                sb.append(kJ);
            } else {
                sb.append("(this Map)");
            }
            sb.append('=');
            V vM = m(i);
            if (vM != this) {
                sb.append(vM);
            } else {
                sb.append("(this Map)");
            }
        }
        sb.append('}');
        return sb.toString();
    }

    public f(int i) {
        if (i == 0) {
            this.f142g = b.f108a;
            this.h = b.f110c;
        } else {
            b(i);
        }
        this.i = 0;
    }
}
