// 
// Decompiled by Procyon v0.6.0
// 

package it.unimi.dsi.fastutil.chars;

import it.unimi.dsi.fastutil.ints.IntSpliterators;
import it.unimi.dsi.fastutil.ints.IntSpliterator;
import java.util.function.IntConsumer;
import it.unimi.dsi.fastutil.ints.IntIterator;
import it.unimi.dsi.fastutil.ints.AbstractIntCollection;
import it.unimi.dsi.fastutil.objects.ObjectSpliterators;
import java.util.Spliterator;
import it.unimi.dsi.fastutil.objects.ObjectSpliterator;
import java.util.function.Consumer;
import java.util.NoSuchElementException;
import it.unimi.dsi.fastutil.objects.AbstractObjectSet;
import java.util.Set;
import java.util.Collection;
import it.unimi.dsi.fastutil.objects.ObjectSet;
import java.io.ObjectInputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Iterator;
import java.util.Map;
import it.unimi.dsi.fastutil.objects.ObjectIterator;
import it.unimi.dsi.fastutil.ints.IntArrays;
import it.unimi.dsi.fastutil.ints.IntCollection;
import java.io.Serializable;

public class Char2IntArrayMap extends AbstractChar2IntMap implements Serializable, Cloneable
{
    private static final long serialVersionUID = 1L;
    protected transient char[] key;
    protected transient int[] value;
    protected int size;
    protected transient Char2IntMap.FastEntrySet entries;
    protected transient CharSet keys;
    protected transient IntCollection values;
    
    public Char2IntArrayMap(final char[] key, final int[] value) {
        this.key = key;
        this.value = value;
        this.size = key.length;
        if (key.length != value.length) {
            throw new IllegalArgumentException("Keys and values have different lengths (" + key.length + ", " + value.length + ")");
        }
    }
    
    public Char2IntArrayMap() {
        this.key = CharArrays.EMPTY_ARRAY;
        this.value = IntArrays.EMPTY_ARRAY;
    }
    
    public Char2IntArrayMap(final int capacity) {
        this.key = new char[capacity];
        this.value = new int[capacity];
    }
    
    public Char2IntArrayMap(final Char2IntMap m) {
        this(m.size());
        int i = 0;
        for (final Char2IntMap.Entry e : m.char2IntEntrySet()) {
            this.key[i] = e.getCharKey();
            this.value[i] = e.getIntValue();
            ++i;
        }
        this.size = i;
    }
    
    public Char2IntArrayMap(final Map<? extends Character, ? extends Integer> m) {
        this(m.size());
        int i = 0;
        for (final Map.Entry<? extends Character, ? extends Integer> e : m.entrySet()) {
            this.key[i] = (char)e.getKey();
            this.value[i] = (int)e.getValue();
            ++i;
        }
        this.size = i;
    }
    
    public Char2IntArrayMap(final char[] key, final int[] value, final int size) {
        this.key = key;
        this.value = value;
        this.size = size;
        if (key.length != value.length) {
            throw new IllegalArgumentException("Keys and values have different lengths (" + key.length + ", " + value.length + ")");
        }
        if (size > key.length) {
            throw new IllegalArgumentException("The provided size (" + size + ") is larger than or equal to the backing-arrays size (" + key.length + ")");
        }
    }
    
    @Override
    public Char2IntMap.FastEntrySet char2IntEntrySet() {
        if (this.entries == null) {
            this.entries = new EntrySet();
        }
        return this.entries;
    }
    
    private int findKey(final char k) {
        final char[] key = this.key;
        int i = this.size;
        while (i-- != 0) {
            if (key[i] == k) {
                return i;
            }
        }
        return -1;
    }
    
    @Override
    public int get(final char k) {
        final char[] key = this.key;
        int i = this.size;
        while (i-- != 0) {
            if (key[i] == k) {
                return this.value[i];
            }
        }
        return this.defRetValue;
    }
    
    @Override
    public int size() {
        return this.size;
    }
    
    @Override
    public void clear() {
        this.size = 0;
    }
    
    @Override
    public boolean containsKey(final char k) {
        return this.findKey(k) != -1;
    }
    
    @Override
    public boolean containsValue(final int v) {
        int i = this.size;
        while (i-- != 0) {
            if (this.value[i] == v) {
                return true;
            }
        }
        return false;
    }
    
    @Override
    public boolean isEmpty() {
        return this.size == 0;
    }
    
    @Override
    public int put(final char k, final int v) {
        final int oldKey = this.findKey(k);
        if (oldKey != -1) {
            final int oldValue = this.value[oldKey];
            this.value[oldKey] = v;
            return oldValue;
        }
        if (this.size == this.key.length) {
            final char[] newKey = new char[(this.size == 0) ? 2 : (this.size * 2)];
            final int[] newValue = new int[(this.size == 0) ? 2 : (this.size * 2)];
            int i = this.size;
            while (i-- != 0) {
                newKey[i] = this.key[i];
                newValue[i] = this.value[i];
            }
            this.key = newKey;
            this.value = newValue;
        }
        this.key[this.size] = k;
        this.value[this.size] = v;
        ++this.size;
        return this.defRetValue;
    }
    
    @Override
    public int remove(final char k) {
        final int oldPos = this.findKey(k);
        if (oldPos == -1) {
            return this.defRetValue;
        }
        final int oldValue = this.value[oldPos];
        final int tail = this.size - oldPos - 1;
        System.arraycopy(this.key, oldPos + 1, this.key, oldPos, tail);
        System.arraycopy(this.value, oldPos + 1, this.value, oldPos, tail);
        --this.size;
        return oldValue;
    }
    
    @Override
    public CharSet keySet() {
        if (this.keys == null) {
            this.keys = new KeySet();
        }
        return this.keys;
    }
    
    @Override
    public IntCollection values() {
        if (this.values == null) {
            this.values = new ValuesCollection();
        }
        return this.values;
    }
    
    public Char2IntArrayMap clone() {
        Char2IntArrayMap c;
        try {
            c = (Char2IntArrayMap)super.clone();
        }
        catch (final CloneNotSupportedException cantHappen) {
            throw new InternalError();
        }
        c.key = this.key.clone();
        c.value = this.value.clone();
        c.entries = null;
        c.keys = null;
        c.values = null;
        return c;
    }
    
    private void writeObject(final ObjectOutputStream s) throws IOException {
        s.defaultWriteObject();
        for (int i = 0, max = this.size; i < max; ++i) {
            s.writeChar(this.key[i]);
            s.writeInt(this.value[i]);
        }
    }
    
    private void readObject(final ObjectInputStream s) throws IOException, ClassNotFoundException {
        s.defaultReadObject();
        this.key = new char[this.size];
        this.value = new int[this.size];
        for (int i = 0; i < this.size; ++i) {
            this.key[i] = s.readChar();
            this.value[i] = s.readInt();
        }
    }
    
    private final class EntrySet extends AbstractObjectSet<Char2IntMap.Entry> implements Char2IntMap.FastEntrySet
    {
        @Override
        public ObjectIterator<Char2IntMap.Entry> iterator() {
            return new ObjectIterator<Char2IntMap.Entry>() {
                int curr = -1;
                int next = 0;
                
                @Override
                public boolean hasNext() {
                    return this.next < Char2IntArrayMap.this.size;
                }
                
                @Override
                public Char2IntMap.Entry next() {
                    if (!this.hasNext()) {
                        throw new NoSuchElementException();
                    }
                    final char[] key = Char2IntArrayMap.this.key;
                    final int next = this.next;
                    this.curr = next;
                    return new BasicEntry(key[next], Char2IntArrayMap.this.value[this.next++]);
                }
                
                @Override
                public void remove() {
                    if (this.curr == -1) {
                        throw new IllegalStateException();
                    }
                    this.curr = -1;
                    final int tail = Char2IntArrayMap.this.size-- - this.next--;
                    System.arraycopy(Char2IntArrayMap.this.key, this.next + 1, Char2IntArrayMap.this.key, this.next, tail);
                    System.arraycopy(Char2IntArrayMap.this.value, this.next + 1, Char2IntArrayMap.this.value, this.next, tail);
                }
                
                @Override
                public void forEachRemaining(final Consumer<? super Char2IntMap.Entry> action) {
                    final int max = Char2IntArrayMap.this.size;
                    while (this.next < max) {
                        final char[] key = Char2IntArrayMap.this.key;
                        final int next = this.next;
                        this.curr = next;
                        action.accept(new BasicEntry(key[next], Char2IntArrayMap.this.value[this.next++]));
                    }
                }
            };
        }
        
        @Override
        public ObjectIterator<Char2IntMap.Entry> fastIterator() {
            return new ObjectIterator<Char2IntMap.Entry>() {
                int next = 0;
                int curr = -1;
                final BasicEntry entry = new BasicEntry();
                
                @Override
                public boolean hasNext() {
                    return this.next < Char2IntArrayMap.this.size;
                }
                
                @Override
                public Char2IntMap.Entry next() {
                    if (!this.hasNext()) {
                        throw new NoSuchElementException();
                    }
                    final BasicEntry entry = this.entry;
                    final char[] key = Char2IntArrayMap.this.key;
                    final int next = this.next;
                    this.curr = next;
                    entry.key = key[next];
                    this.entry.value = Char2IntArrayMap.this.value[this.next++];
                    return this.entry;
                }
                
                @Override
                public void remove() {
                    if (this.curr == -1) {
                        throw new IllegalStateException();
                    }
                    this.curr = -1;
                    final int tail = Char2IntArrayMap.this.size-- - this.next--;
                    System.arraycopy(Char2IntArrayMap.this.key, this.next + 1, Char2IntArrayMap.this.key, this.next, tail);
                    System.arraycopy(Char2IntArrayMap.this.value, this.next + 1, Char2IntArrayMap.this.value, this.next, tail);
                }
                
                @Override
                public void forEachRemaining(final Consumer<? super Char2IntMap.Entry> action) {
                    final int max = Char2IntArrayMap.this.size;
                    while (this.next < max) {
                        final BasicEntry entry = this.entry;
                        final char[] key = Char2IntArrayMap.this.key;
                        final int next = this.next;
                        this.curr = next;
                        entry.key = key[next];
                        this.entry.value = Char2IntArrayMap.this.value[this.next++];
                        action.accept(this.entry);
                    }
                }
            };
        }
        
        @Override
        public ObjectSpliterator<Char2IntMap.Entry> spliterator() {
            return new EntrySetSpliterator(0, Char2IntArrayMap.this.size);
        }
        
        @Override
        public void forEach(final Consumer<? super Char2IntMap.Entry> action) {
            for (int i = 0, max = Char2IntArrayMap.this.size; i < max; ++i) {
                action.accept(new BasicEntry(Char2IntArrayMap.this.key[i], Char2IntArrayMap.this.value[i]));
            }
        }
        
        @Override
        public void fastForEach(final Consumer<? super Char2IntMap.Entry> action) {
            final BasicEntry entry = new BasicEntry();
            for (int i = 0, max = Char2IntArrayMap.this.size; i < max; ++i) {
                entry.key = Char2IntArrayMap.this.key[i];
                entry.value = Char2IntArrayMap.this.value[i];
                action.accept(entry);
            }
        }
        
        @Override
        public int size() {
            return Char2IntArrayMap.this.size;
        }
        
        @Override
        public boolean contains(final Object o) {
            if (!(o instanceof Map.Entry)) {
                return false;
            }
            final Map.Entry<?, ?> e = (Map.Entry<?, ?>)o;
            if (e.getKey() == null || !(e.getKey() instanceof Character)) {
                return false;
            }
            if (e.getValue() == null || !(e.getValue() instanceof Integer)) {
                return false;
            }
            final char k = (char)e.getKey();
            return Char2IntArrayMap.this.containsKey(k) && Char2IntArrayMap.this.get(k) == (int)e.getValue();
        }
        
        @Override
        public boolean remove(final Object o) {
            if (!(o instanceof Map.Entry)) {
                return false;
            }
            final Map.Entry<?, ?> e = (Map.Entry<?, ?>)o;
            if (e.getKey() == null || !(e.getKey() instanceof Character)) {
                return false;
            }
            if (e.getValue() == null || !(e.getValue() instanceof Integer)) {
                return false;
            }
            final char k = (char)e.getKey();
            final int v = (int)e.getValue();
            final int oldPos = Char2IntArrayMap.this.findKey(k);
            if (oldPos == -1 || v != Char2IntArrayMap.this.value[oldPos]) {
                return false;
            }
            final int tail = Char2IntArrayMap.this.size - oldPos - 1;
            System.arraycopy(Char2IntArrayMap.this.key, oldPos + 1, Char2IntArrayMap.this.key, oldPos, tail);
            System.arraycopy(Char2IntArrayMap.this.value, oldPos + 1, Char2IntArrayMap.this.value, oldPos, tail);
            final Char2IntArrayMap this$0 = Char2IntArrayMap.this;
            --this$0.size;
            return true;
        }
        
        final class EntrySetSpliterator extends ObjectSpliterators.EarlyBindingSizeIndexBasedSpliterator<Char2IntMap.Entry> implements ObjectSpliterator<Char2IntMap.Entry>
        {
            EntrySetSpliterator(final int pos, final int maxPos) {
                super(pos, maxPos);
            }
            
            @Override
            public int characteristics() {
                return 16465;
            }
            
            @Override
            protected final Char2IntMap.Entry get(final int location) {
                return new BasicEntry(Char2IntArrayMap.this.key[location], Char2IntArrayMap.this.value[location]);
            }
            
            @Override
            protected final EntrySetSpliterator makeForSplit(final int pos, final int maxPos) {
                return new EntrySetSpliterator(pos, maxPos);
            }
        }
    }
    
    private final class KeySet extends AbstractCharSet
    {
        @Override
        public boolean contains(final char k) {
            return Char2IntArrayMap.this.findKey(k) != -1;
        }
        
        @Override
        public boolean remove(final char k) {
            final int oldPos = Char2IntArrayMap.this.findKey(k);
            if (oldPos == -1) {
                return false;
            }
            final int tail = Char2IntArrayMap.this.size - oldPos - 1;
            System.arraycopy(Char2IntArrayMap.this.key, oldPos + 1, Char2IntArrayMap.this.key, oldPos, tail);
            System.arraycopy(Char2IntArrayMap.this.value, oldPos + 1, Char2IntArrayMap.this.value, oldPos, tail);
            final Char2IntArrayMap this$0 = Char2IntArrayMap.this;
            --this$0.size;
            return true;
        }
        
        @Override
        public CharIterator iterator() {
            return new CharIterator() {
                int pos = 0;
                
                @Override
                public boolean hasNext() {
                    return this.pos < Char2IntArrayMap.this.size;
                }
                
                @Override
                public char nextChar() {
                    if (!this.hasNext()) {
                        throw new NoSuchElementException();
                    }
                    return Char2IntArrayMap.this.key[this.pos++];
                }
                
                @Override
                public void remove() {
                    if (this.pos == 0) {
                        throw new IllegalStateException();
                    }
                    final int tail = Char2IntArrayMap.this.size - this.pos;
                    System.arraycopy(Char2IntArrayMap.this.key, this.pos, Char2IntArrayMap.this.key, this.pos - 1, tail);
                    System.arraycopy(Char2IntArrayMap.this.value, this.pos, Char2IntArrayMap.this.value, this.pos - 1, tail);
                    final Char2IntArrayMap this$0 = Char2IntArrayMap.this;
                    --this$0.size;
                    --this.pos;
                }
                
                @Override
                public void forEachRemaining(final CharConsumer action) {
                    final int max = Char2IntArrayMap.this.size;
                    while (this.pos < max) {
                        action.accept(Char2IntArrayMap.this.key[this.pos++]);
                    }
                }
            };
        }
        
        @Override
        public CharSpliterator spliterator() {
            return new KeySetSpliterator(0, Char2IntArrayMap.this.size);
        }
        
        @Override
        public void forEach(final CharConsumer action) {
            for (int i = 0, max = Char2IntArrayMap.this.size; i < max; ++i) {
                action.accept(Char2IntArrayMap.this.key[i]);
            }
        }
        
        @Override
        public int size() {
            return Char2IntArrayMap.this.size;
        }
        
        @Override
        public void clear() {
            Char2IntArrayMap.this.clear();
        }
        
        final class KeySetSpliterator extends CharSpliterators.EarlyBindingSizeIndexBasedSpliterator implements CharSpliterator
        {
            KeySetSpliterator(final int pos, final int maxPos) {
                super(pos, maxPos);
            }
            
            @Override
            public int characteristics() {
                return 16721;
            }
            
            @Override
            protected final char get(final int location) {
                return Char2IntArrayMap.this.key[location];
            }
            
            @Override
            protected final KeySetSpliterator makeForSplit(final int pos, final int maxPos) {
                return new KeySetSpliterator(pos, maxPos);
            }
            
            @Override
            public void forEachRemaining(final CharConsumer action) {
                final int max = Char2IntArrayMap.this.size;
                while (this.pos < max) {
                    action.accept(Char2IntArrayMap.this.key[this.pos++]);
                }
            }
        }
    }
    
    private final class ValuesCollection extends AbstractIntCollection
    {
        @Override
        public boolean contains(final int v) {
            return Char2IntArrayMap.this.containsValue(v);
        }
        
        @Override
        public IntIterator iterator() {
            return new IntIterator() {
                int pos = 0;
                
                @Override
                public boolean hasNext() {
                    return this.pos < Char2IntArrayMap.this.size;
                }
                
                @Override
                public int nextInt() {
                    if (!this.hasNext()) {
                        throw new NoSuchElementException();
                    }
                    return Char2IntArrayMap.this.value[this.pos++];
                }
                
                @Override
                public void remove() {
                    if (this.pos == 0) {
                        throw new IllegalStateException();
                    }
                    final int tail = Char2IntArrayMap.this.size - this.pos;
                    System.arraycopy(Char2IntArrayMap.this.key, this.pos, Char2IntArrayMap.this.key, this.pos - 1, tail);
                    System.arraycopy(Char2IntArrayMap.this.value, this.pos, Char2IntArrayMap.this.value, this.pos - 1, tail);
                    final Char2IntArrayMap this$0 = Char2IntArrayMap.this;
                    --this$0.size;
                    --this.pos;
                }
                
                @Override
                public void forEachRemaining(final IntConsumer action) {
                    final int max = Char2IntArrayMap.this.size;
                    while (this.pos < max) {
                        action.accept(Char2IntArrayMap.this.value[this.pos++]);
                    }
                }
            };
        }
        
        @Override
        public IntSpliterator spliterator() {
            return new ValuesSpliterator(0, Char2IntArrayMap.this.size);
        }
        
        @Override
        public void forEach(final IntConsumer action) {
            for (int i = 0, max = Char2IntArrayMap.this.size; i < max; ++i) {
                action.accept(Char2IntArrayMap.this.value[i]);
            }
        }
        
        @Override
        public int size() {
            return Char2IntArrayMap.this.size;
        }
        
        @Override
        public void clear() {
            Char2IntArrayMap.this.clear();
        }
        
        final class ValuesSpliterator extends IntSpliterators.EarlyBindingSizeIndexBasedSpliterator implements IntSpliterator
        {
            ValuesSpliterator(final int pos, final int maxPos) {
                super(pos, maxPos);
            }
            
            @Override
            public int characteristics() {
                return 16720;
            }
            
            @Override
            protected final int get(final int location) {
                return Char2IntArrayMap.this.value[location];
            }
            
            @Override
            protected final ValuesSpliterator makeForSplit(final int pos, final int maxPos) {
                return new ValuesSpliterator(pos, maxPos);
            }
            
            @Override
            public void forEachRemaining(final IntConsumer action) {
                final int max = Char2IntArrayMap.this.size;
                while (this.pos < max) {
                    action.accept(Char2IntArrayMap.this.value[this.pos++]);
                }
            }
        }
    }
}
