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

package it.unimi.dsi.fastutil.shorts;

import it.unimi.dsi.fastutil.chars.CharSpliterators;
import it.unimi.dsi.fastutil.chars.CharSpliterator;
import it.unimi.dsi.fastutil.chars.CharConsumer;
import it.unimi.dsi.fastutil.chars.CharIterator;
import it.unimi.dsi.fastutil.chars.AbstractCharCollection;
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.chars.CharArrays;
import it.unimi.dsi.fastutil.chars.CharCollection;
import java.io.Serializable;

public class Short2CharArrayMap extends AbstractShort2CharMap implements Serializable, Cloneable
{
    private static final long serialVersionUID = 1L;
    protected transient short[] key;
    protected transient char[] value;
    protected int size;
    protected transient Short2CharMap.FastEntrySet entries;
    protected transient ShortSet keys;
    protected transient CharCollection values;
    
    public Short2CharArrayMap(final short[] key, final char[] 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 Short2CharArrayMap() {
        this.key = ShortArrays.EMPTY_ARRAY;
        this.value = CharArrays.EMPTY_ARRAY;
    }
    
    public Short2CharArrayMap(final int capacity) {
        this.key = new short[capacity];
        this.value = new char[capacity];
    }
    
    public Short2CharArrayMap(final Short2CharMap m) {
        this(m.size());
        int i = 0;
        for (final Short2CharMap.Entry e : m.short2CharEntrySet()) {
            this.key[i] = e.getShortKey();
            this.value[i] = e.getCharValue();
            ++i;
        }
        this.size = i;
    }
    
    public Short2CharArrayMap(final Map<? extends Short, ? extends Character> m) {
        this(m.size());
        int i = 0;
        for (final Map.Entry<? extends Short, ? extends Character> e : m.entrySet()) {
            this.key[i] = (short)e.getKey();
            this.value[i] = (char)e.getValue();
            ++i;
        }
        this.size = i;
    }
    
    public Short2CharArrayMap(final short[] key, final char[] 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 Short2CharMap.FastEntrySet short2CharEntrySet() {
        if (this.entries == null) {
            this.entries = new EntrySet();
        }
        return this.entries;
    }
    
    private int findKey(final short k) {
        final short[] key = this.key;
        int i = this.size;
        while (i-- != 0) {
            if (key[i] == k) {
                return i;
            }
        }
        return -1;
    }
    
    @Override
    public char get(final short k) {
        final short[] 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 short k) {
        return this.findKey(k) != -1;
    }
    
    @Override
    public boolean containsValue(final char 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 char put(final short k, final char v) {
        final int oldKey = this.findKey(k);
        if (oldKey != -1) {
            final char oldValue = this.value[oldKey];
            this.value[oldKey] = v;
            return oldValue;
        }
        if (this.size == this.key.length) {
            final short[] newKey = new short[(this.size == 0) ? 2 : (this.size * 2)];
            final char[] newValue = new char[(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 char remove(final short k) {
        final int oldPos = this.findKey(k);
        if (oldPos == -1) {
            return this.defRetValue;
        }
        final char 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 ShortSet keySet() {
        if (this.keys == null) {
            this.keys = new KeySet();
        }
        return this.keys;
    }
    
    @Override
    public CharCollection values() {
        if (this.values == null) {
            this.values = new ValuesCollection();
        }
        return this.values;
    }
    
    public Short2CharArrayMap clone() {
        Short2CharArrayMap c;
        try {
            c = (Short2CharArrayMap)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.writeShort(this.key[i]);
            s.writeChar(this.value[i]);
        }
    }
    
    private void readObject(final ObjectInputStream s) throws IOException, ClassNotFoundException {
        s.defaultReadObject();
        this.key = new short[this.size];
        this.value = new char[this.size];
        for (int i = 0; i < this.size; ++i) {
            this.key[i] = s.readShort();
            this.value[i] = s.readChar();
        }
    }
    
    private final class EntrySet extends AbstractObjectSet<Short2CharMap.Entry> implements Short2CharMap.FastEntrySet
    {
        @Override
        public ObjectIterator<Short2CharMap.Entry> iterator() {
            return new ObjectIterator<Short2CharMap.Entry>() {
                int curr = -1;
                int next = 0;
                
                @Override
                public boolean hasNext() {
                    return this.next < Short2CharArrayMap.this.size;
                }
                
                @Override
                public Short2CharMap.Entry next() {
                    if (!this.hasNext()) {
                        throw new NoSuchElementException();
                    }
                    final short[] key = Short2CharArrayMap.this.key;
                    final int next = this.next;
                    this.curr = next;
                    return new BasicEntry(key[next], Short2CharArrayMap.this.value[this.next++]);
                }
                
                @Override
                public void remove() {
                    if (this.curr == -1) {
                        throw new IllegalStateException();
                    }
                    this.curr = -1;
                    final int tail = Short2CharArrayMap.this.size-- - this.next--;
                    System.arraycopy(Short2CharArrayMap.this.key, this.next + 1, Short2CharArrayMap.this.key, this.next, tail);
                    System.arraycopy(Short2CharArrayMap.this.value, this.next + 1, Short2CharArrayMap.this.value, this.next, tail);
                }
                
                @Override
                public void forEachRemaining(final Consumer<? super Short2CharMap.Entry> action) {
                    final int max = Short2CharArrayMap.this.size;
                    while (this.next < max) {
                        final short[] key = Short2CharArrayMap.this.key;
                        final int next = this.next;
                        this.curr = next;
                        action.accept(new BasicEntry(key[next], Short2CharArrayMap.this.value[this.next++]));
                    }
                }
            };
        }
        
        @Override
        public ObjectIterator<Short2CharMap.Entry> fastIterator() {
            return new ObjectIterator<Short2CharMap.Entry>() {
                int next = 0;
                int curr = -1;
                final BasicEntry entry = new BasicEntry();
                
                @Override
                public boolean hasNext() {
                    return this.next < Short2CharArrayMap.this.size;
                }
                
                @Override
                public Short2CharMap.Entry next() {
                    if (!this.hasNext()) {
                        throw new NoSuchElementException();
                    }
                    final BasicEntry entry = this.entry;
                    final short[] key = Short2CharArrayMap.this.key;
                    final int next = this.next;
                    this.curr = next;
                    entry.key = key[next];
                    this.entry.value = Short2CharArrayMap.this.value[this.next++];
                    return this.entry;
                }
                
                @Override
                public void remove() {
                    if (this.curr == -1) {
                        throw new IllegalStateException();
                    }
                    this.curr = -1;
                    final int tail = Short2CharArrayMap.this.size-- - this.next--;
                    System.arraycopy(Short2CharArrayMap.this.key, this.next + 1, Short2CharArrayMap.this.key, this.next, tail);
                    System.arraycopy(Short2CharArrayMap.this.value, this.next + 1, Short2CharArrayMap.this.value, this.next, tail);
                }
                
                @Override
                public void forEachRemaining(final Consumer<? super Short2CharMap.Entry> action) {
                    final int max = Short2CharArrayMap.this.size;
                    while (this.next < max) {
                        final BasicEntry entry = this.entry;
                        final short[] key = Short2CharArrayMap.this.key;
                        final int next = this.next;
                        this.curr = next;
                        entry.key = key[next];
                        this.entry.value = Short2CharArrayMap.this.value[this.next++];
                        action.accept(this.entry);
                    }
                }
            };
        }
        
        @Override
        public ObjectSpliterator<Short2CharMap.Entry> spliterator() {
            return new EntrySetSpliterator(0, Short2CharArrayMap.this.size);
        }
        
        @Override
        public void forEach(final Consumer<? super Short2CharMap.Entry> action) {
            for (int i = 0, max = Short2CharArrayMap.this.size; i < max; ++i) {
                action.accept(new BasicEntry(Short2CharArrayMap.this.key[i], Short2CharArrayMap.this.value[i]));
            }
        }
        
        @Override
        public void fastForEach(final Consumer<? super Short2CharMap.Entry> action) {
            final BasicEntry entry = new BasicEntry();
            for (int i = 0, max = Short2CharArrayMap.this.size; i < max; ++i) {
                entry.key = Short2CharArrayMap.this.key[i];
                entry.value = Short2CharArrayMap.this.value[i];
                action.accept(entry);
            }
        }
        
        @Override
        public int size() {
            return Short2CharArrayMap.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 Short)) {
                return false;
            }
            if (e.getValue() == null || !(e.getValue() instanceof Character)) {
                return false;
            }
            final short k = (short)e.getKey();
            return Short2CharArrayMap.this.containsKey(k) && Short2CharArrayMap.this.get(k) == (char)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 Short)) {
                return false;
            }
            if (e.getValue() == null || !(e.getValue() instanceof Character)) {
                return false;
            }
            final short k = (short)e.getKey();
            final char v = (char)e.getValue();
            final int oldPos = Short2CharArrayMap.this.findKey(k);
            if (oldPos == -1 || v != Short2CharArrayMap.this.value[oldPos]) {
                return false;
            }
            final int tail = Short2CharArrayMap.this.size - oldPos - 1;
            System.arraycopy(Short2CharArrayMap.this.key, oldPos + 1, Short2CharArrayMap.this.key, oldPos, tail);
            System.arraycopy(Short2CharArrayMap.this.value, oldPos + 1, Short2CharArrayMap.this.value, oldPos, tail);
            final Short2CharArrayMap this$0 = Short2CharArrayMap.this;
            --this$0.size;
            return true;
        }
        
        final class EntrySetSpliterator extends ObjectSpliterators.EarlyBindingSizeIndexBasedSpliterator<Short2CharMap.Entry> implements ObjectSpliterator<Short2CharMap.Entry>
        {
            EntrySetSpliterator(final int pos, final int maxPos) {
                super(pos, maxPos);
            }
            
            @Override
            public int characteristics() {
                return 16465;
            }
            
            @Override
            protected final Short2CharMap.Entry get(final int location) {
                return new BasicEntry(Short2CharArrayMap.this.key[location], Short2CharArrayMap.this.value[location]);
            }
            
            @Override
            protected final EntrySetSpliterator makeForSplit(final int pos, final int maxPos) {
                return new EntrySetSpliterator(pos, maxPos);
            }
        }
    }
    
    private final class KeySet extends AbstractShortSet
    {
        @Override
        public boolean contains(final short k) {
            return Short2CharArrayMap.this.findKey(k) != -1;
        }
        
        @Override
        public boolean remove(final short k) {
            final int oldPos = Short2CharArrayMap.this.findKey(k);
            if (oldPos == -1) {
                return false;
            }
            final int tail = Short2CharArrayMap.this.size - oldPos - 1;
            System.arraycopy(Short2CharArrayMap.this.key, oldPos + 1, Short2CharArrayMap.this.key, oldPos, tail);
            System.arraycopy(Short2CharArrayMap.this.value, oldPos + 1, Short2CharArrayMap.this.value, oldPos, tail);
            final Short2CharArrayMap this$0 = Short2CharArrayMap.this;
            --this$0.size;
            return true;
        }
        
        @Override
        public ShortIterator iterator() {
            return new ShortIterator() {
                int pos = 0;
                
                @Override
                public boolean hasNext() {
                    return this.pos < Short2CharArrayMap.this.size;
                }
                
                @Override
                public short nextShort() {
                    if (!this.hasNext()) {
                        throw new NoSuchElementException();
                    }
                    return Short2CharArrayMap.this.key[this.pos++];
                }
                
                @Override
                public void remove() {
                    if (this.pos == 0) {
                        throw new IllegalStateException();
                    }
                    final int tail = Short2CharArrayMap.this.size - this.pos;
                    System.arraycopy(Short2CharArrayMap.this.key, this.pos, Short2CharArrayMap.this.key, this.pos - 1, tail);
                    System.arraycopy(Short2CharArrayMap.this.value, this.pos, Short2CharArrayMap.this.value, this.pos - 1, tail);
                    final Short2CharArrayMap this$0 = Short2CharArrayMap.this;
                    --this$0.size;
                    --this.pos;
                }
                
                @Override
                public void forEachRemaining(final ShortConsumer action) {
                    final int max = Short2CharArrayMap.this.size;
                    while (this.pos < max) {
                        action.accept(Short2CharArrayMap.this.key[this.pos++]);
                    }
                }
            };
        }
        
        @Override
        public ShortSpliterator spliterator() {
            return new KeySetSpliterator(0, Short2CharArrayMap.this.size);
        }
        
        @Override
        public void forEach(final ShortConsumer action) {
            for (int i = 0, max = Short2CharArrayMap.this.size; i < max; ++i) {
                action.accept(Short2CharArrayMap.this.key[i]);
            }
        }
        
        @Override
        public int size() {
            return Short2CharArrayMap.this.size;
        }
        
        @Override
        public void clear() {
            Short2CharArrayMap.this.clear();
        }
        
        final class KeySetSpliterator extends ShortSpliterators.EarlyBindingSizeIndexBasedSpliterator implements ShortSpliterator
        {
            KeySetSpliterator(final int pos, final int maxPos) {
                super(pos, maxPos);
            }
            
            @Override
            public int characteristics() {
                return 16721;
            }
            
            @Override
            protected final short get(final int location) {
                return Short2CharArrayMap.this.key[location];
            }
            
            @Override
            protected final KeySetSpliterator makeForSplit(final int pos, final int maxPos) {
                return new KeySetSpliterator(pos, maxPos);
            }
            
            @Override
            public void forEachRemaining(final ShortConsumer action) {
                final int max = Short2CharArrayMap.this.size;
                while (this.pos < max) {
                    action.accept(Short2CharArrayMap.this.key[this.pos++]);
                }
            }
        }
    }
    
    private final class ValuesCollection extends AbstractCharCollection
    {
        @Override
        public boolean contains(final char v) {
            return Short2CharArrayMap.this.containsValue(v);
        }
        
        @Override
        public CharIterator iterator() {
            return new CharIterator() {
                int pos = 0;
                
                @Override
                public boolean hasNext() {
                    return this.pos < Short2CharArrayMap.this.size;
                }
                
                @Override
                public char nextChar() {
                    if (!this.hasNext()) {
                        throw new NoSuchElementException();
                    }
                    return Short2CharArrayMap.this.value[this.pos++];
                }
                
                @Override
                public void remove() {
                    if (this.pos == 0) {
                        throw new IllegalStateException();
                    }
                    final int tail = Short2CharArrayMap.this.size - this.pos;
                    System.arraycopy(Short2CharArrayMap.this.key, this.pos, Short2CharArrayMap.this.key, this.pos - 1, tail);
                    System.arraycopy(Short2CharArrayMap.this.value, this.pos, Short2CharArrayMap.this.value, this.pos - 1, tail);
                    final Short2CharArrayMap this$0 = Short2CharArrayMap.this;
                    --this$0.size;
                    --this.pos;
                }
                
                @Override
                public void forEachRemaining(final CharConsumer action) {
                    final int max = Short2CharArrayMap.this.size;
                    while (this.pos < max) {
                        action.accept(Short2CharArrayMap.this.value[this.pos++]);
                    }
                }
            };
        }
        
        @Override
        public CharSpliterator spliterator() {
            return new ValuesSpliterator(0, Short2CharArrayMap.this.size);
        }
        
        @Override
        public void forEach(final CharConsumer action) {
            for (int i = 0, max = Short2CharArrayMap.this.size; i < max; ++i) {
                action.accept(Short2CharArrayMap.this.value[i]);
            }
        }
        
        @Override
        public int size() {
            return Short2CharArrayMap.this.size;
        }
        
        @Override
        public void clear() {
            Short2CharArrayMap.this.clear();
        }
        
        final class ValuesSpliterator extends CharSpliterators.EarlyBindingSizeIndexBasedSpliterator implements CharSpliterator
        {
            ValuesSpliterator(final int pos, final int maxPos) {
                super(pos, maxPos);
            }
            
            @Override
            public int characteristics() {
                return 16720;
            }
            
            @Override
            protected final char get(final int location) {
                return Short2CharArrayMap.this.value[location];
            }
            
            @Override
            protected final ValuesSpliterator makeForSplit(final int pos, final int maxPos) {
                return new ValuesSpliterator(pos, maxPos);
            }
            
            @Override
            public void forEachRemaining(final CharConsumer action) {
                final int max = Short2CharArrayMap.this.size;
                while (this.pos < max) {
                    action.accept(Short2CharArrayMap.this.value[this.pos++]);
                }
            }
        }
    }
}
