package kotlin.reflect.w.internal.y0.i;

import java.lang.Comparable;
import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

/* JADX INFO: loaded from: classes2.dex */
public class u<K extends Comparable<K>, V> extends AbstractMap<K, V> {

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final /* synthetic */ int f8298f = 0;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public final int f8299m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public List<u<K, V>.b> f8300n = Collections.emptyList();

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public Map<K, V> f8301o = Collections.emptyMap();

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    public boolean f8302p;

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public volatile u<K, V>.d f8303q;

    public static class a {
        public static final Iterator<Object> a = new C0157a();

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        public static final Iterable<Object> f8304b = new b();

        /* JADX INFO: renamed from: r.i0.w.b.y0.i.u$a$a, reason: collision with other inner class name */
        public static class C0157a implements Iterator<Object> {
            @Override // java.util.Iterator
            public boolean hasNext() {
                return false;
            }

            @Override // java.util.Iterator
            public Object next() {
                throw new NoSuchElementException();
            }

            @Override // java.util.Iterator
            public void remove() {
                throw new UnsupportedOperationException();
            }
        }

        public static class b implements Iterable<Object> {
            @Override // java.lang.Iterable
            public Iterator<Object> iterator() {
                return a.a;
            }
        }
    }

    public class b implements Comparable<u<K, V>.b>, Map.Entry<K, V> {

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        public final K f8305f;

        /* JADX INFO: renamed from: m, reason: collision with root package name */
        public V f8306m;

        public b(K k2, V v2) {
            this.f8305f = k2;
            this.f8306m = v2;
        }

        public b(u uVar, Map.Entry<K, V> entry) {
            K key = entry.getKey();
            V value = entry.getValue();
            u.this = uVar;
            this.f8305f = key;
            this.f8306m = value;
        }

        @Override // java.lang.Comparable
        public int compareTo(Object obj) {
            return this.f8305f.compareTo(((b) obj).f8305f);
        }

        @Override // java.util.Map.Entry
        public boolean equals(Object obj) {
            if (obj == this) {
                return true;
            }
            if (!(obj instanceof Map.Entry)) {
                return false;
            }
            Map.Entry entry = (Map.Entry) obj;
            K k2 = this.f8305f;
            Object key = entry.getKey();
            if (k2 == null ? key == null : k2.equals(key)) {
                V v2 = this.f8306m;
                Object value = entry.getValue();
                if (v2 == null ? value == null : v2.equals(value)) {
                    return true;
                }
            }
            return false;
        }

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

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

        @Override // java.util.Map.Entry
        public int hashCode() {
            K k2 = this.f8305f;
            int iHashCode = k2 == null ? 0 : k2.hashCode();
            V v2 = this.f8306m;
            return iHashCode ^ (v2 != null ? v2.hashCode() : 0);
        }

        @Override // java.util.Map.Entry
        public V setValue(V v2) {
            u uVar = u.this;
            int i2 = u.f8298f;
            uVar.c();
            V v3 = this.f8306m;
            this.f8306m = v2;
            return v3;
        }

        public String toString() {
            String strValueOf = String.valueOf(this.f8305f);
            String strValueOf2 = String.valueOf(this.f8306m);
            StringBuilder sb = new StringBuilder(strValueOf2.length() + strValueOf.length() + 1);
            sb.append(strValueOf);
            sb.append("=");
            sb.append(strValueOf2);
            return sb.toString();
        }
    }

    public class c implements Iterator<Map.Entry<K, V>> {

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        public int f8308f = -1;

        /* JADX INFO: renamed from: m, reason: collision with root package name */
        public boolean f8309m;

        /* JADX INFO: renamed from: n, reason: collision with root package name */
        public Iterator<Map.Entry<K, V>> f8310n;

        public c(t tVar) {
        }

        public final Iterator<Map.Entry<K, V>> b() {
            if (this.f8310n == null) {
                this.f8310n = u.this.f8301o.entrySet().iterator();
            }
            return this.f8310n;
        }

        @Override // java.util.Iterator
        public boolean hasNext() {
            return this.f8308f + 1 < u.this.f8300n.size() || b().hasNext();
        }

        @Override // java.util.Iterator
        public Object next() {
            this.f8309m = true;
            int i2 = this.f8308f + 1;
            this.f8308f = i2;
            return i2 < u.this.f8300n.size() ? u.this.f8300n.get(this.f8308f) : b().next();
        }

        @Override // java.util.Iterator
        public void remove() {
            if (!this.f8309m) {
                throw new IllegalStateException("remove() was called before next()");
            }
            this.f8309m = false;
            u uVar = u.this;
            int i2 = u.f8298f;
            uVar.c();
            if (this.f8308f >= u.this.f8300n.size()) {
                b().remove();
                return;
            }
            u uVar2 = u.this;
            int i3 = this.f8308f;
            this.f8308f = i3 - 1;
            uVar2.i(i3);
        }
    }

    public class d extends AbstractSet<Map.Entry<K, V>> {
        public d(t tVar) {
        }

        /* JADX WARN: Multi-variable type inference failed */
        @Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
        public boolean add(Object obj) {
            Map.Entry entry = (Map.Entry) obj;
            if (contains(entry)) {
                return false;
            }
            u.this.h((Comparable) entry.getKey(), entry.getValue());
            return true;
        }

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

        @Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
        public boolean contains(Object obj) {
            Map.Entry entry = (Map.Entry) obj;
            Object obj2 = u.this.get(entry.getKey());
            Object value = entry.getValue();
            return obj2 == value || (obj2 != null && obj2.equals(value));
        }

        @Override // java.util.AbstractCollection, java.util.Collection, java.lang.Iterable, java.util.Set
        public Iterator<Map.Entry<K, V>> iterator() {
            return new c(null);
        }

        @Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
        public boolean remove(Object obj) {
            Map.Entry entry = (Map.Entry) obj;
            if (!contains(entry)) {
                return false;
            }
            u.this.remove(entry.getKey());
            return true;
        }

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

    public u(int i2, t tVar) {
        this.f8299m = i2;
    }

    public final int a(K k2) {
        int size = this.f8300n.size() - 1;
        if (size >= 0) {
            int iCompareTo = k2.compareTo(this.f8300n.get(size).f8305f);
            if (iCompareTo > 0) {
                return -(size + 2);
            }
            if (iCompareTo == 0) {
                return size;
            }
        }
        int i2 = 0;
        while (i2 <= size) {
            int i3 = (i2 + size) / 2;
            int iCompareTo2 = k2.compareTo(this.f8300n.get(i3).f8305f);
            if (iCompareTo2 < 0) {
                size = i3 - 1;
            } else {
                if (iCompareTo2 <= 0) {
                    return i3;
                }
                i2 = i3 + 1;
            }
        }
        return -(i2 + 1);
    }

    public final void c() {
        if (this.f8302p) {
            throw new UnsupportedOperationException();
        }
    }

    @Override // java.util.AbstractMap, java.util.Map
    public void clear() {
        c();
        if (!this.f8300n.isEmpty()) {
            this.f8300n.clear();
        }
        if (this.f8301o.isEmpty()) {
            return;
        }
        this.f8301o.clear();
    }

    @Override // java.util.AbstractMap, java.util.Map
    public boolean containsKey(Object obj) {
        Comparable comparable = (Comparable) obj;
        return a(comparable) >= 0 || this.f8301o.containsKey(comparable);
    }

    public Map.Entry<K, V> d(int i2) {
        return this.f8300n.get(i2);
    }

    public int e() {
        return this.f8300n.size();
    }

    @Override // java.util.AbstractMap, java.util.Map
    public Set<Map.Entry<K, V>> entrySet() {
        if (this.f8303q == null) {
            this.f8303q = new d(null);
        }
        return this.f8303q;
    }

    public Iterable<Map.Entry<K, V>> f() {
        return this.f8301o.isEmpty() ? (Iterable<Map.Entry<K, V>>) a.f8304b : this.f8301o.entrySet();
    }

    public final SortedMap<K, V> g() {
        c();
        if (this.f8301o.isEmpty() && !(this.f8301o instanceof TreeMap)) {
            this.f8301o = new TreeMap();
        }
        return (SortedMap) this.f8301o;
    }

    @Override // java.util.AbstractMap, java.util.Map
    public V get(Object obj) {
        Comparable comparable = (Comparable) obj;
        int iA = a(comparable);
        return iA >= 0 ? this.f8300n.get(iA).f8306m : this.f8301o.get(comparable);
    }

    public V h(K k2, V v2) {
        c();
        int iA = a(k2);
        if (iA >= 0) {
            u<K, V>.b bVar = this.f8300n.get(iA);
            u.this.c();
            V v3 = bVar.f8306m;
            bVar.f8306m = v2;
            return v3;
        }
        c();
        if (this.f8300n.isEmpty() && !(this.f8300n instanceof ArrayList)) {
            this.f8300n = new ArrayList(this.f8299m);
        }
        int i2 = -(iA + 1);
        if (i2 >= this.f8299m) {
            return g().put(k2, v2);
        }
        int size = this.f8300n.size();
        int i3 = this.f8299m;
        if (size == i3) {
            u<K, V>.b bVarRemove = this.f8300n.remove(i3 - 1);
            g().put(bVarRemove.f8305f, bVarRemove.f8306m);
        }
        this.f8300n.add(i2, new b(k2, v2));
        return null;
    }

    public final V i(int i2) {
        c();
        V v2 = this.f8300n.remove(i2).f8306m;
        if (!this.f8301o.isEmpty()) {
            Iterator<Map.Entry<K, V>> it = g().entrySet().iterator();
            this.f8300n.add(new b(this, it.next()));
            it.remove();
        }
        return v2;
    }

    @Override // java.util.AbstractMap, java.util.Map
    public V remove(Object obj) {
        c();
        Comparable comparable = (Comparable) obj;
        int iA = a(comparable);
        if (iA >= 0) {
            return i(iA);
        }
        if (this.f8301o.isEmpty()) {
            return null;
        }
        return this.f8301o.remove(comparable);
    }

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