package androidx.recyclerview.widget;

import defpackage.v80;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;

/* JADX INFO: loaded from: classes.dex */
public class t {
    public static final Comparator a = new a();

    public class a implements Comparator<d> {
        @Override // java.util.Comparator
        public final int compare(d dVar, d dVar2) {
            return dVar.a - dVar2.a;
        }
    }

    public static abstract class b {
        public abstract boolean a(int i, int i2);

        public abstract boolean b(int i, int i2);

        public Object c(int i, int i2) {
            return null;
        }

        public abstract int d();

        public abstract int e();
    }

    public static class c {
        public final int[] a;
        public final int b;

        public c(int i) {
            int[] iArr = new int[i];
            this.a = iArr;
            this.b = iArr.length / 2;
        }

        public final int a(int i) {
            return this.a[i + this.b];
        }
    }

    public static class d {
        public final int a;
        public final int b;
        public final int c;

        public d(int i, int i2, int i3) {
            this.a = i;
            this.b = i2;
            this.c = i3;
        }
    }

    public static class e {
        public final ArrayList a;
        public final int[] b;
        public final int[] c;
        public final b d;
        public final int e;
        public final int f;
        public final boolean g;

        public e(b bVar, ArrayList arrayList, int[] iArr, int[] iArr2) {
            int i;
            d dVar;
            int i2;
            this.a = arrayList;
            this.b = iArr;
            this.c = iArr2;
            Arrays.fill(iArr, 0);
            Arrays.fill(iArr2, 0);
            this.d = bVar;
            int iE = bVar.e();
            this.e = iE;
            int iD = bVar.d();
            this.f = iD;
            this.g = true;
            d dVar2 = arrayList.isEmpty() ? null : (d) arrayList.get(0);
            if (dVar2 == null || dVar2.a != 0 || dVar2.b != 0) {
                arrayList.add(0, new d(0, 0, 0));
            }
            arrayList.add(new d(iE, iD, 0));
            Iterator it = arrayList.iterator();
            while (it.hasNext()) {
                d dVar3 = (d) it.next();
                for (int i3 = 0; i3 < dVar3.c; i3++) {
                    int i4 = dVar3.a + i3;
                    int i5 = dVar3.b + i3;
                    int i6 = bVar.a(i4, i5) ? 1 : 2;
                    iArr[i4] = (i5 << 4) | i6;
                    iArr2[i5] = (i4 << 4) | i6;
                }
            }
            if (this.g) {
                Iterator it2 = arrayList.iterator();
                int i7 = 0;
                while (it2.hasNext()) {
                    d dVar4 = (d) it2.next();
                    while (true) {
                        i = dVar4.a;
                        if (i7 < i) {
                            if (iArr[i7] == 0) {
                                int size = arrayList.size();
                                int i8 = 0;
                                int i9 = 0;
                                while (true) {
                                    if (i8 < size) {
                                        dVar = (d) arrayList.get(i8);
                                        while (true) {
                                            i2 = dVar.b;
                                            if (i9 < i2) {
                                                if (iArr2[i9] == 0 && bVar.b(i7, i9)) {
                                                    int i10 = bVar.a(i7, i9) ? 8 : 4;
                                                    iArr[i7] = (i9 << 4) | i10;
                                                    iArr2[i9] = i10 | (i7 << 4);
                                                } else {
                                                    i9++;
                                                }
                                            }
                                        }
                                    }
                                    i9 = dVar.c + i2;
                                    i8++;
                                }
                            }
                            i7++;
                        }
                    }
                    i7 = dVar4.c + i;
                }
            }
        }

        public static g b(ArrayDeque arrayDeque, int i, boolean z) {
            g gVar;
            Iterator it = arrayDeque.iterator();
            while (true) {
                if (!it.hasNext()) {
                    gVar = null;
                    break;
                }
                gVar = (g) it.next();
                if (gVar.a == i && gVar.c == z) {
                    it.remove();
                    break;
                }
            }
            while (it.hasNext()) {
                g gVar2 = (g) it.next();
                if (z) {
                    gVar2.b--;
                } else {
                    gVar2.b++;
                }
            }
            return gVar;
        }

        public final void a(androidx.recyclerview.widget.b bVar) {
            int[] iArr;
            b bVar2;
            int i;
            int i2;
            ArrayList arrayList;
            e eVar = this;
            v80 v80Var = new v80(bVar);
            ArrayDeque arrayDeque = new ArrayDeque();
            ArrayList arrayList2 = eVar.a;
            boolean z = true;
            int size = arrayList2.size() - 1;
            int i3 = eVar.e;
            int i4 = eVar.f;
            int i5 = i3;
            while (size >= 0) {
                d dVar = (d) arrayList2.get(size);
                int i6 = dVar.a;
                int i7 = dVar.c;
                int i8 = i6 + i7;
                int i9 = dVar.b;
                int i10 = i9 + i7;
                while (true) {
                    iArr = eVar.b;
                    bVar2 = eVar.d;
                    boolean z2 = z;
                    i = 0;
                    if (i5 <= i8) {
                        break;
                    }
                    i5--;
                    int i11 = iArr[i5];
                    if ((i11 & 12) != 0) {
                        arrayList = arrayList2;
                        int i12 = i11 >> 4;
                        g gVarB = b(arrayDeque, i12, false);
                        if (gVarB != null) {
                            int i13 = (i3 - gVarB.b) - 1;
                            v80Var.d(i5, i13);
                            if ((i11 & 4) != 0) {
                                v80Var.c(i13, z2 ? 1 : 0, bVar2.c(i5, i12));
                            }
                        } else {
                            arrayDeque.add(new g(i5, (i3 - i5) - (z2 ? 1 : 0), z2));
                        }
                    } else {
                        arrayList = arrayList2;
                        v80Var.b(i5, z2 ? 1 : 0);
                        i3--;
                    }
                    arrayList2 = arrayList;
                    z = true;
                }
                ArrayList arrayList3 = arrayList2;
                while (i4 > i10) {
                    i4--;
                    int i14 = eVar.c[i4];
                    if ((i14 & 12) != 0) {
                        int i15 = i14 >> 4;
                        g gVarB2 = b(arrayDeque, i15, true);
                        if (gVarB2 == null) {
                            arrayDeque.add(new g(i4, i3 - i5, false));
                            i2 = 0;
                        } else {
                            i2 = 0;
                            v80Var.d((i3 - gVarB2.b) - 1, i5);
                            if ((i14 & 4) != 0) {
                                v80Var.c(i5, 1, bVar2.c(i15, i4));
                            }
                        }
                    } else {
                        i2 = i;
                        v80Var.a(i5, 1);
                        i3++;
                    }
                    eVar = this;
                    i = i2;
                }
                int i16 = i9;
                int i17 = i6;
                while (i < i7) {
                    if ((iArr[i17] & 15) == 2) {
                        v80Var.c(i17, 1, bVar2.c(i17, i16));
                    }
                    i17++;
                    i16++;
                    i++;
                }
                size--;
                eVar = this;
                z = true;
                i4 = i9;
                i5 = i6;
                arrayList2 = arrayList3;
            }
            v80Var.e();
        }
    }

    public static abstract class f<T> {
        public abstract boolean a(Object obj, Object obj2);

        public abstract boolean b(Object obj, Object obj2);

        public Object c(Object obj, Object obj2) {
            return null;
        }
    }

    public static class g {
        public final int a;
        public int b;
        public final boolean c;

        public g(int i, int i2, boolean z) {
            this.a = i;
            this.b = i2;
            this.c = z;
        }
    }

    public static class h {
        public int a;
        public int b;
        public int c;
        public int d;

        public final int a() {
            return this.d - this.c;
        }

        public final int b() {
            return this.b - this.a;
        }
    }

    public static class i {
        public int a;
        public int b;
        public int c;
        public int d;
        public boolean e;

        public final int a() {
            return Math.min(this.c - this.a, this.d - this.b);
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:30:0x00e1 A[ADDED_TO_REGION] */
    /* JADX WARN: Removed duplicated region for block: B:33:0x00ed  */
    /* JADX WARN: Removed duplicated region for block: B:44:0x0112  */
    /* JADX WARN: Removed duplicated region for block: B:52:0x0137  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static e a(b bVar) {
        c cVar;
        i iVar;
        int i2;
        h hVar;
        int[] iArr;
        int i3;
        int i4;
        int i5;
        boolean z;
        i iVar2;
        i iVar3;
        int iA;
        int i6;
        int i7;
        int i8;
        int i9;
        int i10;
        int iA2;
        int i11;
        int i12;
        int i13;
        int i14;
        int i15;
        int i16;
        int i17;
        int iE = bVar.e();
        int iD = bVar.d();
        ArrayList arrayList = new ArrayList();
        ArrayList arrayList2 = new ArrayList();
        h hVar2 = new h();
        int i18 = 0;
        hVar2.a = 0;
        hVar2.b = iE;
        hVar2.c = 0;
        hVar2.d = iD;
        arrayList2.add(hVar2);
        int i19 = iE + iD;
        int i20 = 1;
        int i21 = (((i19 + 1) / 2) * 2) + 1;
        c cVar2 = new c(i21);
        c cVar3 = new c(i21);
        ArrayList arrayList3 = new ArrayList();
        while (true) {
            boolean zIsEmpty = arrayList2.isEmpty();
            int[] iArr2 = cVar2.a;
            int[] iArr3 = cVar3.a;
            if (zIsEmpty) {
                Collections.sort(arrayList, a);
                return new e(bVar, arrayList, iArr2, iArr3);
            }
            h hVar3 = (h) arrayList2.remove(arrayList2.size() - i20);
            if (hVar3.b() < i20 || hVar3.a() < i20) {
                cVar = cVar3;
                iVar = null;
            } else {
                int iB = ((hVar3.b() + hVar3.a()) + i20) / 2;
                int i22 = hVar3.a;
                int i23 = cVar2.b;
                iArr2[i20 + i23] = i22;
                int i24 = hVar3.b;
                int i25 = cVar3.b;
                iArr3[i20 + i25] = i24;
                int i26 = i18;
                while (i26 < iB) {
                    int i27 = Math.abs(hVar3.b() - hVar3.a()) % 2 == i20 ? i20 : i18;
                    int iB2 = hVar3.b() - hVar3.a();
                    int i28 = -i26;
                    int i29 = i28;
                    while (true) {
                        if (i29 > i26) {
                            iArr = iArr2;
                            i3 = iB;
                            i4 = i23;
                            i5 = i25;
                            z = false;
                            iVar2 = null;
                            break;
                        }
                        if (i29 != i28) {
                            if (i29 != i26) {
                                i10 = i29;
                                i3 = iB;
                                if (cVar2.a(i10 + 1) > cVar2.a(i10 - 1)) {
                                }
                                i12 = i27;
                                i13 = ((i11 - hVar3.a) + hVar3.c) - i10;
                                if (i26 == 0 || i11 != iA2) {
                                    i14 = i11;
                                    i15 = i13;
                                } else {
                                    i14 = i11;
                                    i15 = i13 - 1;
                                }
                                int i30 = i23;
                                i16 = i13;
                                i17 = i14;
                                i4 = i30;
                                i5 = i25;
                                while (i17 < hVar3.b && i16 < hVar3.d && bVar.b(i17, i16)) {
                                    i17++;
                                    i16++;
                                }
                                iArr2[i10 + i4] = i17;
                                if (i12 == 0) {
                                    int i31 = iB2 - i10;
                                    iArr = iArr2;
                                    if (i31 >= i28 + 1 && i31 <= i26 - 1 && cVar3.a(i31) <= i17) {
                                        iVar2 = new i();
                                        iVar2.a = iA2;
                                        iVar2.b = i15;
                                        iVar2.c = i17;
                                        iVar2.d = i16;
                                        z = false;
                                        iVar2.e = false;
                                        break;
                                    }
                                } else {
                                    iArr = iArr2;
                                }
                                i29 = i10 + 2;
                                iB = i3;
                                i27 = i12;
                                i23 = i4;
                                i25 = i5;
                                iArr2 = iArr;
                            } else {
                                i10 = i29;
                                i3 = iB;
                            }
                            iA2 = cVar2.a(i10 - 1);
                            i11 = iA2 + 1;
                            i12 = i27;
                            i13 = ((i11 - hVar3.a) + hVar3.c) - i10;
                            if (i26 == 0) {
                                i14 = i11;
                                i15 = i13;
                                int i302 = i23;
                                i16 = i13;
                                i17 = i14;
                                i4 = i302;
                                i5 = i25;
                                while (i17 < hVar3.b) {
                                    i17++;
                                    i16++;
                                }
                                iArr2[i10 + i4] = i17;
                                if (i12 == 0) {
                                }
                                i29 = i10 + 2;
                                iB = i3;
                                i27 = i12;
                                i23 = i4;
                                i25 = i5;
                                iArr2 = iArr;
                            }
                        } else {
                            i10 = i29;
                            i3 = iB;
                        }
                        iA2 = cVar2.a(i10 + 1);
                        i11 = iA2;
                        i12 = i27;
                        i13 = ((i11 - hVar3.a) + hVar3.c) - i10;
                        if (i26 == 0) {
                        }
                    }
                    if (iVar2 != null) {
                        cVar = cVar3;
                        iVar = iVar2;
                        break;
                    }
                    boolean z2 = (hVar3.b() - hVar3.a()) % 2 == 0 ? true : z;
                    int iB3 = hVar3.b() - hVar3.a();
                    int i32 = i28;
                    while (true) {
                        if (i32 > i26) {
                            cVar = cVar3;
                            iVar3 = null;
                            break;
                        }
                        if (i32 == i28 || (i32 != i26 && cVar3.a(i32 + 1) < cVar3.a(i32 - 1))) {
                            iA = cVar3.a(i32 + 1);
                            i6 = iA;
                        } else {
                            iA = cVar3.a(i32 - 1);
                            i6 = iA - 1;
                        }
                        int i33 = hVar3.d - ((hVar3.b - i6) - i32);
                        if (i26 == 0 || i6 != iA) {
                            i7 = i33;
                        } else {
                            i7 = i33;
                            i33++;
                        }
                        int i34 = i7;
                        cVar = cVar3;
                        int i35 = i6;
                        int i36 = i34;
                        boolean z3 = z2;
                        while (i35 > hVar3.a && i36 > hVar3.c) {
                            i8 = iB3;
                            if (!bVar.b(i35 - 1, i36 - 1)) {
                                break;
                            }
                            i35--;
                            i36--;
                            iB3 = i8;
                        }
                        i8 = iB3;
                        iArr3[i32 + i5] = i35;
                        if (z3 && (i9 = i8 - i32) >= i28 && i9 <= i26 && cVar2.a(i9) >= i35) {
                            iVar3 = new i();
                            iVar3.a = i35;
                            iVar3.b = i36;
                            iVar3.c = iA;
                            iVar3.d = i33;
                            iVar3.e = true;
                            break;
                        }
                        i32 += 2;
                        cVar3 = cVar;
                        z2 = z3;
                        iB3 = i8;
                    }
                    if (iVar3 != null) {
                        iVar = iVar3;
                        break;
                    }
                    i26++;
                    cVar3 = cVar;
                    iB = i3;
                    i23 = i4;
                    i25 = i5;
                    iArr2 = iArr;
                    i20 = 1;
                    i18 = 0;
                }
                cVar = cVar3;
                iVar = null;
            }
            if (iVar != null) {
                if (iVar.a() > 0) {
                    int i37 = iVar.d;
                    int i38 = iVar.b;
                    int i39 = i37 - i38;
                    int i40 = iVar.c;
                    int i41 = iVar.a;
                    int i42 = i40 - i41;
                    arrayList.add(i39 != i42 ? iVar.e ? new d(i41, i38, iVar.a()) : i39 > i42 ? new d(i41, i38 + 1, iVar.a()) : new d(i41 + 1, i38, iVar.a()) : new d(i41, i38, i42));
                }
                if (arrayList3.isEmpty()) {
                    hVar = new h();
                    i2 = 1;
                } else {
                    i2 = 1;
                    hVar = (h) arrayList3.remove(arrayList3.size() - 1);
                }
                hVar.a = hVar3.a;
                hVar.c = hVar3.c;
                hVar.b = iVar.a;
                hVar.d = iVar.b;
                arrayList2.add(hVar);
                hVar3.b = hVar3.b;
                hVar3.d = hVar3.d;
                hVar3.a = iVar.c;
                hVar3.c = iVar.d;
                arrayList2.add(hVar3);
            } else {
                i2 = 1;
                arrayList3.add(hVar3);
            }
            cVar3 = cVar;
            i20 = i2;
            i18 = 0;
        }
    }
}
