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

package it.unimi.dsi.fastutil.bytes;

import it.unimi.dsi.fastutil.doubles.DoubleSpliterators;
import it.unimi.dsi.fastutil.doubles.DoubleSpliterator;
import java.util.function.DoubleConsumer;
import it.unimi.dsi.fastutil.doubles.DoubleIterator;
import it.unimi.dsi.fastutil.doubles.AbstractDoubleCollection;
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.doubles.DoubleArrays;
import it.unimi.dsi.fastutil.doubles.DoubleCollection;
import java.io.Serializable;

public class Byte2DoubleArrayMap extends AbstractByte2DoubleMap implements Serializable, Cloneable
{
    private static final long serialVersionUID = 1L;
    protected transient byte[] key;
    protected transient double[] value;
    protected int size;
    protected transient Byte2DoubleMap.FastEntrySet entries;
    protected transient ByteSet keys;
    protected transient DoubleCollection values;
    
    public Byte2DoubleArrayMap(final byte[] key, final double[] 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 Byte2DoubleArrayMap() {
        this.key = ByteArrays.EMPTY_ARRAY;
        this.value = DoubleArrays.EMPTY_ARRAY;
    }
    
    public Byte2DoubleArrayMap(final int capacity) {
        this.key = new byte[capacity];
        this.value = new double[capacity];
    }
    
    public Byte2DoubleArrayMap(final Byte2DoubleMap m) {
        this(m.size());
        int i = 0;
        for (final Byte2DoubleMap.Entry e : m.byte2DoubleEntrySet()) {
            this.key[i] = e.getByteKey();
            this.value[i] = e.getDoubleValue();
            ++i;
        }
        this.size = i;
    }
    
    public Byte2DoubleArrayMap(final Map<? extends Byte, ? extends Double> m) {
        this(m.size());
        int i = 0;
        for (final Map.Entry<? extends Byte, ? extends Double> e : m.entrySet()) {
            this.key[i] = (byte)e.getKey();
            this.value[i] = (double)e.getValue();
            ++i;
        }
        this.size = i;
    }
    
    public Byte2DoubleArrayMap(final byte[] key, final double[] 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 Byte2DoubleMap.FastEntrySet byte2DoubleEntrySet() {
        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 double 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 double v) {
        int i = this.size;
        while (i-- != 0) {
            if (Double.doubleToLongBits(this.value[i]) == Double.doubleToLongBits(v)) {
                return true;
            }
        }
        return false;
    }
    
    @Override
    public boolean isEmpty() {
        return this.size == 0;
    }
    
    @Override
    public double put(final byte k, final double v) {
        final int oldKey = this.findKey(k);
        if (oldKey != -1) {
            final double 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 double[] newValue = new double[(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 double remove(final byte k) {
        final int oldPos = this.findKey(k);
        if (oldPos == -1) {
            return this.defRetValue;
        }
        final double 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 DoubleCollection values() {
        if (this.values == null) {
            this.values = new ValuesCollection();
        }
        return this.values;
    }
    
    public Byte2DoubleArrayMap clone() {
        Byte2DoubleArrayMap c;
        try {
            c = (Byte2DoubleArrayMap)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.writeDouble(this.value[i]);
        }
    }
    
    private void readObject(final ObjectInputStream s) throws IOException, ClassNotFoundException {
        s.defaultReadObject();
        this.key = new byte[this.size];
        this.value = new double[this.size];
        for (int i = 0; i < this.size; ++i) {
            this.key[i] = s.readByte();
            this.value[i] = s.readDouble();
        }
    }
    
    private final class EntrySet extends AbstractObjectSet<Byte2DoubleMap.Entry> implements Byte2DoubleMap.FastEntrySet
    {
        @Override
        public ObjectIterator<Byte2DoubleMap.Entry> iterator() {
            return new ObjectIterator<Byte2DoubleMap.Entry>() {
                int curr = -1;
                int next = 0;
                
                @Override
                public boolean hasNext() {
                    return this.next < Byte2DoubleArrayMap.this.size;
                }
                
                @Override
                public Byte2DoubleMap.Entry next() {
                    if (!this.hasNext()) {
                        throw new NoSuchElementException();
                    }
                    final byte[] key = Byte2DoubleArrayMap.this.key;
                    final int next = this.next;
                    this.curr = next;
                    return new BasicEntry(key[next], Byte2DoubleArrayMap.this.value[this.next++]);
                }
                
                @Override
                public void remove() {
                    if (this.curr == -1) {
                        throw new IllegalStateException();
                    }
                    this.curr = -1;
                    final int tail = Byte2DoubleArrayMap.this.size-- - this.next--;
                    System.arraycopy(Byte2DoubleArrayMap.this.key, this.next + 1, Byte2DoubleArrayMap.this.key, this.next, tail);
                    System.arraycopy(Byte2DoubleArrayMap.this.value, this.next + 1, Byte2DoubleArrayMap.this.value, this.next, tail);
                }
                
                @Override
                public void forEachRemaining(final Consumer<? super Byte2DoubleMap.Entry> action) {
                    final int max = Byte2DoubleArrayMap.this.size;
                    while (this.next < max) {
                        final byte[] key = Byte2DoubleArrayMap.this.key;
                        final int next = this.next;
                        this.curr = next;
                        action.accept(new BasicEntry(key[next], Byte2DoubleArrayMap.this.value[this.next++]));
                    }
                }
            };
        }
        
        @Override
        public ObjectIterator<Byte2DoubleMap.Entry> fastIterator() {
            return new ObjectIterator<Byte2DoubleMap.Entry>() {
                int next = 0;
                int curr = -1;
                final BasicEntry entry = new BasicEntry();
                
                @Override
                public boolean hasNext() {
                    return this.next < Byte2DoubleArrayMap.this.size;
                }
                
                @Override
                public Byte2DoubleMap.Entry next() {
                    if (!this.hasNext()) {
                        throw new NoSuchElementException();
                    }
                    final BasicEntry entry = this.entry;
                    final byte[] key = Byte2DoubleArrayMap.this.key;
                    final int next = this.next;
                    this.curr = next;
                    entry.key = key[next];
                    this.entry.value = Byte2DoubleArrayMap.this.value[this.next++];
                    return this.entry;
                }
                
                @Override
                public void remove() {
                    if (this.curr == -1) {
                        throw new IllegalStateException();
                    }
                    this.curr = -1;
                    final int tail = Byte2DoubleArrayMap.this.size-- - this.next--;
                    System.arraycopy(Byte2DoubleArrayMap.this.key, this.next + 1, Byte2DoubleArrayMap.this.key, this.next, tail);
                    System.arraycopy(Byte2DoubleArrayMap.this.value, this.next + 1, Byte2DoubleArrayMap.this.value, this.next, tail);
                }
                
                @Override
                public void forEachRemaining(final Consumer<? super Byte2DoubleMap.Entry> action) {
                    final int max = Byte2DoubleArrayMap.this.size;
                    while (this.next < max) {
                        final BasicEntry entry = this.entry;
                        final byte[] key = Byte2DoubleArrayMap.this.key;
                        final int next = this.next;
                        this.curr = next;
                        entry.key = key[next];
                        this.entry.value = Byte2DoubleArrayMap.this.value[this.next++];
                        action.accept(this.entry);
                    }
                }
            };
        }
        
        @Override
        public ObjectSpliterator<Byte2DoubleMap.Entry> spliterator() {
            return new EntrySetSpliterator(0, Byte2DoubleArrayMap.this.size);
        }
        
        @Override
        public void forEach(final Consumer<? super Byte2DoubleMap.Entry> action) {
            for (int i = 0, max = Byte2DoubleArrayMap.this.size; i < max; ++i) {
                action.accept(new BasicEntry(Byte2DoubleArrayMap.this.key[i], Byte2DoubleArrayMap.this.value[i]));
            }
        }
        
        @Override
        public void fastForEach(final Consumer<? super Byte2DoubleMap.Entry> action) {
            final BasicEntry entry = new BasicEntry();
            for (int i = 0, max = Byte2DoubleArrayMap.this.size; i < max; ++i) {
                entry.key = Byte2DoubleArrayMap.this.key[i];
                entry.value = Byte2DoubleArrayMap.this.value[i];
                action.accept(entry);
            }
        }
        
        @Override
        public int size() {
            return Byte2DoubleArrayMap.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 Double)) {
                return false;
            }
            final byte k = (byte)e.getKey();
            return Byte2DoubleArrayMap.this.containsKey(k) && Double.doubleToLongBits(Byte2DoubleArrayMap.this.get(k)) == Double.doubleToLongBits((double)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 Double)) {
                return false;
            }
            final byte k = (byte)e.getKey();
            final double v = (double)e.getValue();
            final int oldPos = Byte2DoubleArrayMap.this.findKey(k);
            if (oldPos == -1 || Double.doubleToLongBits(v) != Double.doubleToLongBits(Byte2DoubleArrayMap.this.value[oldPos])) {
                return false;
            }
            final int tail = Byte2DoubleArrayMap.this.size - oldPos - 1;
            System.arraycopy(Byte2DoubleArrayMap.this.key, oldPos + 1, Byte2DoubleArrayMap.this.key, oldPos, tail);
            System.arraycopy(Byte2DoubleArrayMap.this.value, oldPos + 1, Byte2DoubleArrayMap.this.value, oldPos, tail);
            final Byte2DoubleArrayMap this$0 = Byte2DoubleArrayMap.this;
            --this$0.size;
            return true;
        }
        
        final class EntrySetSpliterator extends ObjectSpliterators.EarlyBindingSizeIndexBasedSpliterator<Byte2DoubleMap.Entry> implements ObjectSpliterator<Byte2DoubleMap.Entry>
        {
            EntrySetSpliterator(final int pos, final int maxPos) {
                super(pos, maxPos);
            }
            
            @Override
            public int characteristics() {
                return 16465;
            }
            
            @Override
            protected final Byte2DoubleMap.Entry get(final int location) {
                return new BasicEntry(Byte2DoubleArrayMap.this.key[location], Byte2DoubleArrayMap.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 Byte2DoubleArrayMap.this.findKey(k) != -1;
        }
        
        @Override
        public boolean remove(final byte k) {
            final int oldPos = Byte2DoubleArrayMap.this.findKey(k);
            if (oldPos == -1) {
                return false;
            }
            final int tail = Byte2DoubleArrayMap.this.size - oldPos - 1;
            System.arraycopy(Byte2DoubleArrayMap.this.key, oldPos + 1, Byte2DoubleArrayMap.this.key, oldPos, tail);
            System.arraycopy(Byte2DoubleArrayMap.this.value, oldPos + 1, Byte2DoubleArrayMap.this.value, oldPos, tail);
            final Byte2DoubleArrayMap this$0 = Byte2DoubleArrayMap.this;
            --this$0.size;
            return true;
        }
        
        @Override
        public ByteIterator iterator() {
            return new ByteIterator() {
                int pos = 0;
                
                @Override
                public boolean hasNext() {
                    return this.pos < Byte2DoubleArrayMap.this.size;
                }
                
                @Override
                public byte nextByte() {
                    if (!this.hasNext()) {
                        throw new NoSuchElementException();
                    }
                    return Byte2DoubleArrayMap.this.key[this.pos++];
                }
                
                @Override
                public void remove() {
                    if (this.pos == 0) {
                        throw new IllegalStateException();
                    }
                    final int tail = Byte2DoubleArrayMap.this.size - this.pos;
                    System.arraycopy(Byte2DoubleArrayMap.this.key, this.pos, Byte2DoubleArrayMap.this.key, this.pos - 1, tail);
                    System.arraycopy(Byte2DoubleArrayMap.this.value, this.pos, Byte2DoubleArrayMap.this.value, this.pos - 1, tail);
                    final Byte2DoubleArrayMap this$0 = Byte2DoubleArrayMap.this;
                    --this$0.size;
                    --this.pos;
                }
                
                @Override
                public void forEachRemaining(final ByteConsumer action) {
                    final int max = Byte2DoubleArrayMap.this.size;
                    while (this.pos < max) {
                        action.accept(Byte2DoubleArrayMap.this.key[this.pos++]);
                    }
                }
            };
        }
        
        @Override
        public ByteSpliterator spliterator() {
            return new KeySetSpliterator(0, Byte2DoubleArrayMap.this.size);
        }
        
        @Override
        public void forEach(final ByteConsumer action) {
            for (int i = 0, max = Byte2DoubleArrayMap.this.size; i < max; ++i) {
                action.accept(Byte2DoubleArrayMap.this.key[i]);
            }
        }
        
        @Override
        public int size() {
            return Byte2DoubleArrayMap.this.size;
        }
        
        @Override
        public void clear() {
            Byte2DoubleArrayMap.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 Byte2DoubleArrayMap.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 = Byte2DoubleArrayMap.this.size;
                while (this.pos < max) {
                    action.accept(Byte2DoubleArrayMap.this.key[this.pos++]);
                }
            }
        }
    }
    
    private final class ValuesCollection extends AbstractDoubleCollection
    {
        @Override
        public boolean contains(final double v) {
            return Byte2DoubleArrayMap.this.containsValue(v);
        }
        
        @Override
        public DoubleIterator iterator() {
            return new DoubleIterator() {
                int pos = 0;
                
                @Override
                public boolean hasNext() {
                    return this.pos < Byte2DoubleArrayMap.this.size;
                }
                
                @Override
                public double nextDouble() {
                    if (!this.hasNext()) {
                        throw new NoSuchElementException();
                    }
                    return Byte2DoubleArrayMap.this.value[this.pos++];
                }
                
                @Override
                public void remove() {
                    if (this.pos == 0) {
                        throw new IllegalStateException();
                    }
                    final int tail = Byte2DoubleArrayMap.this.size - this.pos;
                    System.arraycopy(Byte2DoubleArrayMap.this.key, this.pos, Byte2DoubleArrayMap.this.key, this.pos - 1, tail);
                    System.arraycopy(Byte2DoubleArrayMap.this.value, this.pos, Byte2DoubleArrayMap.this.value, this.pos - 1, tail);
                    final Byte2DoubleArrayMap this$0 = Byte2DoubleArrayMap.this;
                    --this$0.size;
                    --this.pos;
                }
                
                @Override
                public void forEachRemaining(final DoubleConsumer action) {
                    final int max = Byte2DoubleArrayMap.this.size;
                    while (this.pos < max) {
                        action.accept(Byte2DoubleArrayMap.this.value[this.pos++]);
                    }
                }
            };
        }
        
        @Override
        public DoubleSpliterator spliterator() {
            return new ValuesSpliterator(0, Byte2DoubleArrayMap.this.size);
        }
        
        @Override
        public void forEach(final DoubleConsumer action) {
            for (int i = 0, max = Byte2DoubleArrayMap.this.size; i < max; ++i) {
                action.accept(Byte2DoubleArrayMap.this.value[i]);
            }
        }
        
        @Override
        public int size() {
            return Byte2DoubleArrayMap.this.size;
        }
        
        @Override
        public void clear() {
            Byte2DoubleArrayMap.this.clear();
        }
        
        final class ValuesSpliterator extends DoubleSpliterators.EarlyBindingSizeIndexBasedSpliterator implements DoubleSpliterator
        {
            ValuesSpliterator(final int pos, final int maxPos) {
                super(pos, maxPos);
            }
            
            @Override
            public int characteristics() {
                return 16720;
            }
            
            @Override
            protected final double get(final int location) {
                return Byte2DoubleArrayMap.this.value[location];
            }
            
            @Override
            protected final ValuesSpliterator makeForSplit(final int pos, final int maxPos) {
                return new ValuesSpliterator(pos, maxPos);
            }
            
            @Override
            public void forEachRemaining(final DoubleConsumer action) {
                final int max = Byte2DoubleArrayMap.this.size;
                while (this.pos < max) {
                    action.accept(Byte2DoubleArrayMap.this.value[this.pos++]);
                }
            }
        }
    }
}
