package androidx.collection;

import com.google.android.gms.measurement.internal.u1;
import java.util.Arrays;
import java.util.ConcurrentModificationException;
import java.util.Map;

/* JADX INFO: loaded from: classes.dex */
public class s {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public int[] f966a;

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

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

    public s() {
        this(0);
    }

    public final int a(Object obj) {
        int i10 = this.f968c * 2;
        Object[] objArr = this.f967b;
        if (obj == null) {
            for (int i11 = 1; i11 < i10; i11 += 2) {
                if (objArr[i11] == null) {
                    return i11 >> 1;
                }
            }
            return -1;
        }
        for (int i12 = 1; i12 < i10; i12 += 2) {
            if (nb.g.c(obj, objArr[i12])) {
                return i12 >> 1;
            }
        }
        return -1;
    }

    public final void b(int i10) {
        int i11 = this.f968c;
        int[] iArr = this.f966a;
        if (iArr.length < i10) {
            int[] iArrCopyOf = Arrays.copyOf(iArr, i10);
            nb.g.h(iArrCopyOf, "copyOf(this, newSize)");
            this.f966a = iArrCopyOf;
            Object[] objArrCopyOf = Arrays.copyOf(this.f967b, i10 * 2);
            nb.g.h(objArrCopyOf, "copyOf(this, newSize)");
            this.f967b = objArrCopyOf;
        }
        if (this.f968c != i11) {
            throw new ConcurrentModificationException();
        }
    }

    public void clear() {
        if (this.f968c > 0) {
            this.f966a = t.a.f32765a;
            this.f967b = t.a.f32767c;
            this.f968c = 0;
        }
        if (this.f968c > 0) {
            throw new ConcurrentModificationException();
        }
    }

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

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

    public final int d(int i10, Object obj) {
        int i11 = this.f968c;
        if (i11 == 0) {
            return -1;
        }
        int iA = t.a.a(i11, i10, this.f966a);
        if (iA < 0 || nb.g.c(obj, this.f967b[iA << 1])) {
            return iA;
        }
        int i12 = iA + 1;
        while (i12 < i11 && this.f966a[i12] == i10) {
            if (nb.g.c(obj, this.f967b[i12 << 1])) {
                return i12;
            }
            i12++;
        }
        for (int i13 = iA - 1; i13 >= 0 && this.f966a[i13] == i10; i13--) {
            if (nb.g.c(obj, this.f967b[i13 << 1])) {
                return i13;
            }
        }
        return ~i12;
    }

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

    public final boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        try {
            if (obj instanceof s) {
                int i10 = this.f968c;
                if (i10 != ((s) obj).f968c) {
                    return false;
                }
                s sVar = (s) obj;
                for (int i11 = 0; i11 < i10; i11++) {
                    Object objI = i(i11);
                    Object objM = m(i11);
                    Object obj2 = sVar.get(objI);
                    if (objM == null) {
                        if (obj2 != null || !sVar.containsKey(objI)) {
                            return false;
                        }
                    } else if (!nb.g.c(objM, obj2)) {
                        return false;
                    }
                }
                return true;
            }
            if (!(obj instanceof Map) || this.f968c != ((Map) obj).size()) {
                return false;
            }
            int i12 = this.f968c;
            for (int i13 = 0; i13 < i12; i13++) {
                Object objI2 = i(i13);
                Object objM2 = m(i13);
                Object obj3 = ((Map) obj).get(objI2);
                if (objM2 == null) {
                    if (obj3 != null || !((Map) obj).containsKey(objI2)) {
                        return false;
                    }
                } else if (!nb.g.c(objM2, obj3)) {
                    return false;
                }
            }
            return true;
        } catch (ClassCastException | NullPointerException unused) {
        }
        return false;
    }

    public final int g() {
        int i10 = this.f968c;
        if (i10 == 0) {
            return -1;
        }
        int iA = t.a.a(i10, 0, this.f966a);
        if (iA < 0 || this.f967b[iA << 1] == null) {
            return iA;
        }
        int i11 = iA + 1;
        while (i11 < i10 && this.f966a[i11] == 0) {
            if (this.f967b[i11 << 1] == null) {
                return i11;
            }
            i11++;
        }
        for (int i12 = iA - 1; i12 >= 0 && this.f966a[i12] == 0; i12--) {
            if (this.f967b[i12 << 1] == null) {
                return i12;
            }
        }
        return ~i11;
    }

    public Object get(Object obj) {
        int iE = e(obj);
        if (iE >= 0) {
            return this.f967b[(iE << 1) + 1];
        }
        return null;
    }

    public final Object getOrDefault(Object obj, Object obj2) {
        int iE = e(obj);
        return iE >= 0 ? this.f967b[(iE << 1) + 1] : obj2;
    }

    public int hashCode() {
        int[] iArr = this.f966a;
        Object[] objArr = this.f967b;
        int i10 = this.f968c;
        int i11 = 1;
        int i12 = 0;
        int iHashCode = 0;
        while (i12 < i10) {
            Object obj = objArr[i11];
            iHashCode += (obj != null ? obj.hashCode() : 0) ^ iArr[i12];
            i12++;
            i11 += 2;
        }
        return iHashCode;
    }

    public final Object i(int i10) {
        if (i10 < 0 || i10 >= this.f968c) {
            throw new IllegalArgumentException(u1.g("Expected index to be within 0..size()-1, but was ", i10).toString());
        }
        return this.f967b[i10 << 1];
    }

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

    public void j(s sVar) {
        nb.g.i(sVar, "map");
        int i10 = sVar.f968c;
        b(this.f968c + i10);
        if (this.f968c != 0) {
            for (int i11 = 0; i11 < i10; i11++) {
                put(sVar.i(i11), sVar.m(i11));
            }
        } else if (i10 > 0) {
            kotlin.collections.q.P(0, 0, sVar.f966a, this.f966a, i10);
            kotlin.collections.q.R(sVar.f967b, 0, this.f967b, 0, i10 << 1);
            this.f968c = i10;
        }
    }

    public Object k(int i10) {
        int i11;
        if (i10 < 0 || i10 >= (i11 = this.f968c)) {
            throw new IllegalArgumentException(u1.g("Expected index to be within 0..size()-1, but was ", i10).toString());
        }
        Object[] objArr = this.f967b;
        int i12 = i10 << 1;
        Object obj = objArr[i12 + 1];
        if (i11 <= 1) {
            clear();
        } else {
            int i13 = i11 - 1;
            int[] iArr = this.f966a;
            if (iArr.length <= 8 || i11 >= iArr.length / 3) {
                if (i10 < i13) {
                    int i14 = i10 + 1;
                    kotlin.collections.q.P(i10, i14, iArr, iArr, i11);
                    Object[] objArr2 = this.f967b;
                    kotlin.collections.q.R(objArr2, i12, objArr2, i14 << 1, i11 << 1);
                }
                Object[] objArr3 = this.f967b;
                int i15 = i13 << 1;
                objArr3[i15] = null;
                objArr3[i15 + 1] = null;
            } else {
                int i16 = i11 > 8 ? i11 + (i11 >> 1) : 8;
                int[] iArrCopyOf = Arrays.copyOf(iArr, i16);
                nb.g.h(iArrCopyOf, "copyOf(this, newSize)");
                this.f966a = iArrCopyOf;
                Object[] objArrCopyOf = Arrays.copyOf(this.f967b, i16 << 1);
                nb.g.h(objArrCopyOf, "copyOf(this, newSize)");
                this.f967b = objArrCopyOf;
                if (i11 != this.f968c) {
                    throw new ConcurrentModificationException();
                }
                if (i10 > 0) {
                    kotlin.collections.q.P(0, 0, iArr, this.f966a, i10);
                    kotlin.collections.q.R(objArr, 0, this.f967b, 0, i12);
                }
                if (i10 < i13) {
                    int i17 = i10 + 1;
                    kotlin.collections.q.P(i10, i17, iArr, this.f966a, i11);
                    kotlin.collections.q.R(objArr, i12, this.f967b, i17 << 1, i11 << 1);
                }
            }
            if (i11 != this.f968c) {
                throw new ConcurrentModificationException();
            }
            this.f968c = i13;
        }
        return obj;
    }

    public Object l(int i10, Object obj) {
        if (i10 < 0 || i10 >= this.f968c) {
            throw new IllegalArgumentException(u1.g("Expected index to be within 0..size()-1, but was ", i10).toString());
        }
        int i11 = (i10 << 1) + 1;
        Object[] objArr = this.f967b;
        Object obj2 = objArr[i11];
        objArr[i11] = obj;
        return obj2;
    }

    public final Object m(int i10) {
        if (i10 < 0 || i10 >= this.f968c) {
            throw new IllegalArgumentException(u1.g("Expected index to be within 0..size()-1, but was ", i10).toString());
        }
        return this.f967b[(i10 << 1) + 1];
    }

    public Object put(Object obj, Object obj2) {
        int i10 = this.f968c;
        int iHashCode = obj != null ? obj.hashCode() : 0;
        int iD = obj != null ? d(iHashCode, obj) : g();
        if (iD >= 0) {
            int i11 = (iD << 1) + 1;
            Object[] objArr = this.f967b;
            Object obj3 = objArr[i11];
            objArr[i11] = obj2;
            return obj3;
        }
        int i12 = ~iD;
        int[] iArr = this.f966a;
        if (i10 >= iArr.length) {
            int i13 = 8;
            if (i10 >= 8) {
                i13 = (i10 >> 1) + i10;
            } else if (i10 < 4) {
                i13 = 4;
            }
            int[] iArrCopyOf = Arrays.copyOf(iArr, i13);
            nb.g.h(iArrCopyOf, "copyOf(this, newSize)");
            this.f966a = iArrCopyOf;
            Object[] objArrCopyOf = Arrays.copyOf(this.f967b, i13 << 1);
            nb.g.h(objArrCopyOf, "copyOf(this, newSize)");
            this.f967b = objArrCopyOf;
            if (i10 != this.f968c) {
                throw new ConcurrentModificationException();
            }
        }
        if (i12 < i10) {
            int[] iArr2 = this.f966a;
            int i14 = i12 + 1;
            kotlin.collections.q.P(i14, i12, iArr2, iArr2, i10);
            Object[] objArr2 = this.f967b;
            kotlin.collections.q.R(objArr2, i14 << 1, objArr2, i12 << 1, this.f968c << 1);
        }
        int i15 = this.f968c;
        if (i10 == i15) {
            int[] iArr3 = this.f966a;
            if (i12 < iArr3.length) {
                iArr3[i12] = iHashCode;
                Object[] objArr3 = this.f967b;
                int i16 = i12 << 1;
                objArr3[i16] = obj;
                objArr3[i16 + 1] = obj2;
                this.f968c = i15 + 1;
                return null;
            }
        }
        throw new ConcurrentModificationException();
    }

    public final Object putIfAbsent(Object obj, Object obj2) {
        Object obj3 = get(obj);
        return obj3 == null ? put(obj, obj2) : obj3;
    }

    public Object remove(Object obj) {
        int iE = e(obj);
        if (iE >= 0) {
            return k(iE);
        }
        return null;
    }

    public final Object replace(Object obj, Object obj2) {
        int iE = e(obj);
        if (iE >= 0) {
            return l(iE, obj2);
        }
        return null;
    }

    public final int size() {
        return this.f968c;
    }

    public final String toString() {
        if (isEmpty()) {
            return "{}";
        }
        StringBuilder sb = new StringBuilder(this.f968c * 28);
        sb.append('{');
        int i10 = this.f968c;
        for (int i11 = 0; i11 < i10; i11++) {
            if (i11 > 0) {
                sb.append(", ");
            }
            Object objI = i(i11);
            if (objI != sb) {
                sb.append(objI);
            } else {
                sb.append("(this Map)");
            }
            sb.append('=');
            Object objM = m(i11);
            if (objM != sb) {
                sb.append(objM);
            } else {
                sb.append("(this Map)");
            }
        }
        sb.append('}');
        String string = sb.toString();
        nb.g.h(string, "StringBuilder(capacity).…builderAction).toString()");
        return string;
    }

    public s(int i10) {
        this.f966a = i10 == 0 ? t.a.f32765a : new int[i10];
        this.f967b = i10 == 0 ? t.a.f32767c : new Object[i10 << 1];
    }

    public final boolean remove(Object obj, Object obj2) {
        int iE = e(obj);
        if (iE < 0 || !nb.g.c(obj2, m(iE))) {
            return false;
        }
        k(iE);
        return true;
    }

    public final boolean replace(Object obj, Object obj2, Object obj3) {
        int iE = e(obj);
        if (iE < 0 || !nb.g.c(obj2, m(iE))) {
            return false;
        }
        l(iE, obj3);
        return true;
    }

    public s(s sVar) {
        this(0);
        if (sVar != null) {
            j(sVar);
        }
    }
}
