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

package it.unimi.dsi.fastutil.bytes;

import it.unimi.dsi.fastutil.longs.LongSpliterators;
import it.unimi.dsi.fastutil.longs.LongSpliterator;
import java.util.function.LongConsumer;
import it.unimi.dsi.fastutil.longs.LongIterator;
import it.unimi.dsi.fastutil.longs.AbstractLongCollection;
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.longs.LongArrays;
import it.unimi.dsi.fastutil.longs.LongCollection;
import java.io.Serializable;

public class Byte2LongArrayMap extends AbstractByte2LongMap implements Serializable, Cloneable
{
    private static final long serialVersionUID = 1L;
    protected transient byte[] key;
    protected transient long[] value;
    protected int size;
    protected transient Byte2LongMap.FastEntrySet entries;
    protected transient ByteSet keys;
    protected transient LongCollection values;
    
    public Byte2LongArrayMap(final byte[] key, final long[] 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 Byte2LongArrayMap() {
        this.key = ByteArrays.EMPTY_ARRAY;
        this.value = LongArrays.EMPTY_ARRAY;
    }
    
    public Byte2LongArrayMap(final int capacity) {
        this.key = new byte[capacity];
        this.value = new long[capacity];
    }
    
    public Byte2LongArrayMap(final Byte2LongMap m) {
        this(m.size());
        int i = 0;
        for (final Byte2LongMap.Entry e : m.byte2LongEntrySet()) {
            this.key[i] = e.getByteKey();
            this.value[i] = e.getLongValue();
            ++i;
        }
        this.size = i;
    }
    
    public Byte2LongArrayMap(final Map<? extends Byte, ? extends Long> m) {
        this(m.size());
        int i = 0;
        for (final Map.Entry<? extends Byte, ? extends Long> e : m.entrySet()) {
            this.key[i] = (byte)e.getKey();
            this.value[i] = (long)e.getValue();
            ++i;
        }
        this.size = i;
    }
    
    public Byte2LongArrayMap(final byte[] key, final long[] 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 Byte2LongMap.FastEntrySet byte2LongEntrySet() {
        if (this.entries == null) {
            this.entries = new EntrySet();
        }
        return this.entries;
    }
    
    private int findKey(final byte k) {
        final byte[] key = this.key;
        int i = this.size;
        while (i-- != 0) {
            if (key[i] == k) {
                return i;
            }
        }
        return -1;
    }
    
    @Override
    public long get(final byte k) {
        final byte[] 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 byte k) {
        return this.findKey(k) != -1;
    }
    
    @Override
    public boolean containsValue(final long 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 long put(final byte k, final long v) {
        final int oldKey = this.findKey(k);
        if (oldKey != -1) {
            final long oldValue = this.value[oldKey];
            this.value[oldKey] = v;
            return oldValue;
        }
        if (this.size == this.key.length) {
            final byte[] newKey = new byte[(this.size == 0) ? 2 : (this.size * 2)];
            final long[] newValue = new long[(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 long remove(final byte k) {
        final int oldPos = this.findKey(k);
        if (oldPos == -1) {
            return this.defRetValue;
        }
        final long 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 ByteSet keySet() {
        if (this.keys == null) {
            this.keys = new KeySet();
        }
        return this.keys;
    }
    
    @Override
    public LongCollection values() {
        if (this.values == null) {
            this.values = new ValuesCollection();
        }
        return this.values;
    }
    
    public Byte2LongArrayMap clone() {
        Byte2LongArrayMap c;
        try {
            c = (Byte2LongArrayMap)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.writeByte(this.key[i]);
            s.writeLong(this.value[i]);
        }
    }
    
    private void readObject(final ObjectInputStream s) throws IOException, ClassNotFoundException {
        s.defaultReadObject();
        this.key = new byte[this.size];
        this.value = new long[this.size];
        for (int i = 0; i < this.size; ++i) {
            this.key[i] = s.readByte();
            this.value[i] = s.readLong();
        }
    }
    
    private final class EntrySet extends AbstractObjectSet<Byte2LongMap.Entry> implements Byte2LongMap.FastEntrySet
    {
        @Override
        public ObjectIterator<Byte2LongMap.Entry> iterator() {
            return new ObjectIterator<Byte2LongMap.Entry>() {
                int curr = -1;
                int next = 0;
                
                @Override
                public boolean hasNext() {
                    return this.next < Byte2LongArrayMap.this.size;
                }
                
                @Override
                public Byte2LongMap.Entry next() {
                    if (!this.hasNext()) {
                        throw new NoSuchElementException();
                    }
                    final byte[] key = Byte2LongArrayMap.this.key;
                    final int next = this.next;
                    this.curr = next;
                    return new BasicEntry(key[next], Byte2LongArrayMap.this.value[this.next++]);
                }
                
                @Override
                public void remove() {
                    if (this.curr == -1) {
                        throw new IllegalStateException();
                    }
                    this.curr = -1;
                    final int tail = Byte2LongArrayMap.this.size-- - this.next--;
                    System.arraycopy(Byte2LongArrayMap.this.key, this.next + 1, Byte2LongArrayMap.this.key, this.next, tail);
                    System.arraycopy(Byte2LongArrayMap.this.value, this.next + 1, Byte2LongArrayMap.this.value, this.next, tail);
                }
                
                @Override
                public void forEachRemaining(final Consumer<? super Byte2LongMap.Entry> action) {
                    final int max = Byte2LongArrayMap.this.size;
                    while (this.next < max) {
                        final byte[] key = Byte2LongArrayMap.this.key;
                        final int next = this.next;
                        this.curr = next;
                        action.accept(new BasicEntry(key[next], Byte2LongArrayMap.this.value[this.next++]));
                    }
                }
            };
        }
        
        @Override
        public ObjectIterator<Byte2LongMap.Entry> fastIterator() {
            return new ObjectIterator<Byte2LongMap.Entry>() {
                int next = 0;
                int curr = -1;
                final BasicEntry entry = new BasicEntry();
                
                @Override
                public boolean hasNext() {
                    return this.next < Byte2LongArrayMap.this.size;
                }
                
                @Override
                public Byte2LongMap.Entry next() {
                    if (!this.hasNext()) {
                        throw new NoSuchElementException();
                    }
                    final BasicEntry entry = this.entry;
                    final byte[] key = Byte2LongArrayMap.this.key;
                    final int next = this.next;
                    this.curr = next;
                    entry.key = key[next];
                    this.entry.value = Byte2LongArrayMap.this.value[this.next++];
                    return this.entry;
                }
                
                @Override
                public void remove() {
                    if (this.curr == -1) {
                        throw new IllegalStateException();
                    }
                    this.curr = -1;
                    final int tail = Byte2LongArrayMap.this.size-- - this.next--;
                    System.arraycopy(Byte2LongArrayMap.this.key, this.next + 1, Byte2LongArrayMap.this.key, this.next, tail);
                    System.arraycopy(Byte2LongArrayMap.this.value, this.next + 1, Byte2LongArrayMap.this.value, this.next, tail);
                }
                
                @Override
                public void forEachRemaining(final Consumer<? super Byte2LongMap.Entry> action) {
                    final int max = Byte2LongArrayMap.this.size;
                    while (this.next < max) {
                        final BasicEntry entry = this.entry;
                        final byte[] key = Byte2LongArrayMap.this.key;
                        final int next = this.next;
                        this.curr = next;
                        entry.key = key[next];
                        this.entry.value = Byte2LongArrayMap.this.value[this.next++];
                        action.accept(this.entry);
                    }
                }
            };
        }
        
        @Override
        public ObjectSpliterator<Byte2LongMap.Entry> spliterator() {
            return new EntrySetSpliterator(0, Byte2LongArrayMap.this.size);
        }
        
        @Override
        public void forEach(final Consumer<? super Byte2LongMap.Entry> action) {
            for (int i = 0, max = Byte2LongArrayMap.this.size; i < max; ++i) {
                action.accept(new BasicEntry(Byte2LongArrayMap.this.key[i], Byte2LongArrayMap.this.value[i]));
            }
        }
        
        @Override
        public void fastForEach(final Consumer<? super Byte2LongMap.Entry> action) {
            final BasicEntry entry = new BasicEntry();
            for (int i = 0, max = Byte2LongArrayMap.this.size; i < max; ++i) {
                entry.key = Byte2LongArrayMap.this.key[i];
                entry.value = Byte2LongArrayMap.this.value[i];
                action.accept(entry);
            }
        }
        
        @Override
        public int size() {
            return Byte2LongArrayMap.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 Byte)) {
                return false;
            }
            if (e.getValue() == null || !(e.getValue() instanceof Long)) {
                return false;
            }
            final byte k = (byte)e.getKey();
            return Byte2LongArrayMap.this.containsKey(k) && Byte2LongArrayMap.this.get(k) == (long)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 Byte)) {
                return false;
            }
            if (e.getValue() == null || !(e.getValue() instanceof Long)) {
                return false;
            }
            final byte k = (byte)e.getKey();
            final long v = (long)e.getValue();
            final int oldPos = Byte2LongArrayMap.this.findKey(k);
            if (oldPos == -1 || v != Byte2LongArrayMap.this.value[oldPos]) {
                return false;
            }
            final int tail = Byte2LongArrayMap.this.size - oldPos - 1;
            System.arraycopy(Byte2LongArrayMap.this.key, oldPos + 1, Byte2LongArrayMap.this.key, oldPos, tail);
            System.arraycopy(Byte2LongArrayMap.this.value, oldPos + 1, Byte2LongArrayMap.this.value, oldPos, tail);
            final Byte2LongArrayMap this$0 = Byte2LongArrayMap.this;
            --this$0.size;
            return true;
        }
        
        final class EntrySetSpliterator extends ObjectSpliterators.EarlyBindingSizeIndexBasedSpliterator<Byte2LongMap.Entry> implements ObjectSpliterator<Byte2LongMap.Entry>
        {
            EntrySetSpliterator(final int pos, final int maxPos) {
                super(pos, maxPos);
            }
            
            @Override
            public int characteristics() {
                return 16465;
            }
            
            @Override
            protected final Byte2LongMap.Entry get(final int location) {
                return new BasicEntry(Byte2LongArrayMap.this.key[location], Byte2LongArrayMap.this.value[location]);
            }
            
            @Override
            protected final EntrySetSpliterator makeForSplit(final int pos, final int maxPos) {
                return new EntrySetSpliterator(pos, maxPos);
            }
        }
    }
    
    private final class KeySet extends AbstractByteSet
    {
        @Override
        public boolean contains(final byte k) {
            return Byte2LongArrayMap.this.findKey(k) != -1;
        }
        
        @Override
        public boolean remove(final byte k) {
            final int oldPos = Byte2LongArrayMap.this.findKey(k);
            if (oldPos == -1) {
                return false;
            }
            final int tail = Byte2LongArrayMap.this.size - oldPos - 1;
            System.arraycopy(Byte2LongArrayMap.this.key, oldPos + 1, Byte2LongArrayMap.this.key, oldPos, tail);
            System.arraycopy(Byte2LongArrayMap.this.value, oldPos + 1, Byte2LongArrayMap.this.value, oldPos, tail);
            final Byte2LongArrayMap this$0 = Byte2LongArrayMap.this;
            --this$0.size;
            return true;
        }
        
        @Override
        public ByteIterator iterator() {
            return new ByteIterator() {
                int pos = 0;
                
                @Override
                public boolean hasNext() {
                    return this.pos < Byte2LongArrayMap.this.size;
                }
                
                @Override
                public byte nextByte() {
                    if (!this.hasNext()) {
                        throw new NoSuchElementException();
                    }
                    return Byte2LongArrayMap.this.key[this.pos++];
                }
                
                @Override
                public void remove() {
                    if (this.pos == 0) {
                        throw new IllegalStateException();
                    }
                    final int tail = Byte2LongArrayMap.this.size - this.pos;
                    System.arraycopy(Byte2LongArrayMap.this.key, this.pos, Byte2LongArrayMap.this.key, this.pos - 1, tail);
                    System.arraycopy(Byte2LongArrayMap.this.value, this.pos, Byte2LongArrayMap.this.value, this.pos - 1, tail);
                    final Byte2LongArrayMap this$0 = Byte2LongArrayMap.this;
                    --this$0.size;
                    --this.pos;
                }
                
                @Override
                public void forEachRemaining(final ByteConsumer action) {
                    final int max = Byte2LongArrayMap.this.size;
                    while (this.pos < max) {
                        action.accept(Byte2LongArrayMap.this.key[this.pos++]);
                    }
                }
            };
        }
        
        @Override
        public ByteSpliterator spliterator() {
            return new KeySetSpliterator(0, Byte2LongArrayMap.this.size);
        }
        
        @Override
        public void forEach(final ByteConsumer action) {
            for (int i = 0, max = Byte2LongArrayMap.this.size; i < max; ++i) {
                action.accept(Byte2LongArrayMap.this.key[i]);
            }
        }
        
        @Override
        public int size() {
            return Byte2LongArrayMap.this.size;
        }
        
        @Override
        public void clear() {
            Byte2LongArrayMap.this.clear();
        }
        
        final class KeySetSpliterator extends ByteSpliterators.EarlyBindingSizeIndexBasedSpliterator implements ByteSpliterator
        {
            KeySetSpliterator(final int pos, final int maxPos) {
                super(pos, maxPos);
            }
            
            @Override
            public int characteristics() {
                return 16721;
            }
            
            @Override
            protected final byte get(final int location) {
                return Byte2LongArrayMap.this.key[location];
            }
            
            @Override
            protected final KeySetSpliterator makeForSplit(final int pos, final int maxPos) {
                return new KeySetSpliterator(pos, maxPos);
            }
            
            @Override
            public void forEachRemaining(final ByteConsumer action) {
                final int max = Byte2LongArrayMap.this.size;
                while (this.pos < max) {
                    action.accept(Byte2LongArrayMap.this.key[this.pos++]);
                }
            }
        }
    }
    
    private final class ValuesCollection extends AbstractLongCollection
    {
        @Override
        public boolean contains(final long v) {
            return Byte2LongArrayMap.this.containsValue(v);
        }
        
        @Override
        public LongIterator iterator() {
            return new LongIterator() {
                int pos = 0;
                
                @Override
                public boolean hasNext() {
                    return this.pos < Byte2LongArrayMap.this.size;
                }
                
                @Override
                public long nextLong() {
                    if (!this.hasNext()) {
                        throw new NoSuchElementException();
                    }
                    return Byte2LongArrayMap.this.value[this.pos++];
                }
                
                @Override
                public void remove() {
                    if (this.pos == 0) {
                        throw new IllegalStateException();
                    }
                    final int tail = Byte2LongArrayMap.this.size - this.pos;
                    System.arraycopy(Byte2LongArrayMap.this.key, this.pos, Byte2LongArrayMap.this.key, this.pos - 1, tail);
                    System.arraycopy(Byte2LongArrayMap.this.value, this.pos, Byte2LongArrayMap.this.value, this.pos - 1, tail);
                    final Byte2LongArrayMap this$0 = Byte2LongArrayMap.this;
                    --this$0.size;
                    --this.pos;
                }
                
                @Override
                public void forEachRemaining(final LongConsumer action) {
                    final int max = Byte2LongArrayMap.this.size;
                    while (this.pos < max) {
                        action.accept(Byte2LongArrayMap.this.value[this.pos++]);
                    }
                }
            };
        }
        
        @Override
        public LongSpliterator spliterator() {
            return new ValuesSpliterator(0, Byte2LongArrayMap.this.size);
        }
        
        @Override
        public void forEach(final LongConsumer action) {
            for (int i = 0, max = Byte2LongArrayMap.this.size; i < max; ++i) {
                action.accept(Byte2LongArrayMap.this.value[i]);
            }
        }
        
        @Override
        public int size() {
            return Byte2LongArrayMap.this.size;
        }
        
        @Override
        public void clear() {
            Byte2LongArrayMap.this.clear();
        }
        
        final class ValuesSpliterator extends LongSpliterators.EarlyBindingSizeIndexBasedSpliterator implements LongSpliterator
        {
            ValuesSpliterator(final int pos, final int maxPos) {
                super(pos, maxPos);
            }
            
            @Override
            public int characteristics() {
                return 16720;
            }
            
            @Override
            protected final long get(final int location) {
                return Byte2LongArrayMap.this.value[location];
            }
            
            @Override
            protected final ValuesSpliterator makeForSplit(final int pos, final int maxPos) {
                return new ValuesSpliterator(pos, maxPos);
            }
            
            @Override
            public void forEachRemaining(final LongConsumer action) {
                final int max = Byte2LongArrayMap.this.size;
                while (this.pos < max) {
                    action.accept(Byte2LongArrayMap.this.value[this.pos++]);
                }
            }
        }
    }
}
