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

package it.unimi.dsi.fastutil.objects;

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 java.util.Spliterator;
import java.util.function.Consumer;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.Collection;
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.doubles.DoubleArrays;
import it.unimi.dsi.fastutil.doubles.DoubleCollection;
import java.io.Serializable;

public class Reference2DoubleArrayMap<K> extends AbstractReference2DoubleMap<K> implements Serializable, Cloneable
{
    private static final long serialVersionUID = 1L;
    protected transient Object[] key;
    protected transient double[] value;
    protected int size;
    protected transient Reference2DoubleMap.FastEntrySet<K> entries;
    protected transient ReferenceSet<K> keys;
    protected transient DoubleCollection values;
    
    public Reference2DoubleArrayMap(final Object[] 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 Reference2DoubleArrayMap() {
        this.key = ObjectArrays.EMPTY_ARRAY;
        this.value = DoubleArrays.EMPTY_ARRAY;
    }
    
    public Reference2DoubleArrayMap(final int capacity) {
        this.key = new Object[capacity];
        this.value = new double[capacity];
    }
    
    public Reference2DoubleArrayMap(final Reference2DoubleMap<K> m) {
        this(m.size());
        int i = 0;
        for (final Reference2DoubleMap.Entry<K> e : m.reference2DoubleEntrySet()) {
            this.key[i] = e.getKey();
            this.value[i] = e.getDoubleValue();
            ++i;
        }
        this.size = i;
    }
    
    public Reference2DoubleArrayMap(final Map<? extends K, ? extends Double> m) {
        this(m.size());
        int i = 0;
        for (final Map.Entry<? extends K, ? extends Double> e : m.entrySet()) {
            this.key[i] = e.getKey();
            this.value[i] = (double)e.getValue();
            ++i;
        }
        this.size = i;
    }
    
    public Reference2DoubleArrayMap(final Object[] 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 Reference2DoubleMap.FastEntrySet<K> reference2DoubleEntrySet() {
        if (this.entries == null) {
            this.entries = new EntrySet();
        }
        return this.entries;
    }
    
    private int findKey(final Object k) {
        final Object[] key = this.key;
        int i = this.size;
        while (i-- != 0) {
            if (key[i] == k) {
                return i;
            }
        }
        return -1;
    }
    
    @Override
    public double getDouble(final Object k) {
        final Object[] 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() {
        int i = this.size;
        while (i-- != 0) {
            this.key[i] = null;
        }
        this.size = 0;
    }
    
    @Override
    public boolean containsKey(final Object 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 K 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 Object[] newKey = new Object[(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 removeDouble(final Object 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;
        this.key[this.size] = null;
        return oldValue;
    }
    
    @Override
    public ReferenceSet<K> 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 Reference2DoubleArrayMap<K> clone() {
        Reference2DoubleArrayMap<K> c;
        try {
            c = (Reference2DoubleArrayMap)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.writeObject(this.key[i]);
            s.writeDouble(this.value[i]);
        }
    }
    
    private void readObject(final ObjectInputStream s) throws IOException, ClassNotFoundException {
        s.defaultReadObject();
        this.key = new Object[this.size];
        this.value = new double[this.size];
        for (int i = 0; i < this.size; ++i) {
            this.key[i] = s.readObject();
            this.value[i] = s.readDouble();
        }
    }
    
    private final class EntrySet extends AbstractObjectSet<Reference2DoubleMap.Entry<K>> implements Reference2DoubleMap.FastEntrySet<K>
    {
        @Override
        public ObjectIterator<Reference2DoubleMap.Entry<K>> iterator() {
            return new ObjectIterator<Reference2DoubleMap.Entry<K>>() {
                int curr = -1;
                int next = 0;
                
                @Override
                public boolean hasNext() {
                    return this.next < Reference2DoubleArrayMap.this.size;
                }
                
                @Override
                public Reference2DoubleMap.Entry<K> next() {
                    if (!this.hasNext()) {
                        throw new NoSuchElementException();
                    }
                    final Object[] key = Reference2DoubleArrayMap.this.key;
                    final int next = this.next;
                    this.curr = next;
                    return new BasicEntry<K>(key[next], Reference2DoubleArrayMap.this.value[this.next++]);
                }
                
                @Override
                public void remove() {
                    if (this.curr == -1) {
                        throw new IllegalStateException();
                    }
                    this.curr = -1;
                    final int tail = Reference2DoubleArrayMap.this.size-- - this.next--;
                    System.arraycopy(Reference2DoubleArrayMap.this.key, this.next + 1, Reference2DoubleArrayMap.this.key, this.next, tail);
                    System.arraycopy(Reference2DoubleArrayMap.this.value, this.next + 1, Reference2DoubleArrayMap.this.value, this.next, tail);
                    Reference2DoubleArrayMap.this.key[Reference2DoubleArrayMap.this.size] = null;
                }
                
                @Override
                public void forEachRemaining(final Consumer<? super Reference2DoubleMap.Entry<K>> action) {
                    final int max = Reference2DoubleArrayMap.this.size;
                    while (this.next < max) {
                        final Object[] key = Reference2DoubleArrayMap.this.key;
                        final int next = this.next;
                        this.curr = next;
                        action.accept((Object)new BasicEntry(key[next], Reference2DoubleArrayMap.this.value[this.next++]));
                    }
                }
            };
        }
        
        @Override
        public ObjectIterator<Reference2DoubleMap.Entry<K>> fastIterator() {
            return new ObjectIterator<Reference2DoubleMap.Entry<K>>() {
                int next = 0;
                int curr = -1;
                final BasicEntry<K> entry = new BasicEntry<K>();
                
                @Override
                public boolean hasNext() {
                    return this.next < Reference2DoubleArrayMap.this.size;
                }
                
                @Override
                public Reference2DoubleMap.Entry<K> next() {
                    if (!this.hasNext()) {
                        throw new NoSuchElementException();
                    }
                    final BasicEntry<K> entry = this.entry;
                    final Object[] key = Reference2DoubleArrayMap.this.key;
                    final int next = this.next;
                    this.curr = next;
                    entry.key = (K)key[next];
                    this.entry.value = Reference2DoubleArrayMap.this.value[this.next++];
                    return this.entry;
                }
                
                @Override
                public void remove() {
                    if (this.curr == -1) {
                        throw new IllegalStateException();
                    }
                    this.curr = -1;
                    final int tail = Reference2DoubleArrayMap.this.size-- - this.next--;
                    System.arraycopy(Reference2DoubleArrayMap.this.key, this.next + 1, Reference2DoubleArrayMap.this.key, this.next, tail);
                    System.arraycopy(Reference2DoubleArrayMap.this.value, this.next + 1, Reference2DoubleArrayMap.this.value, this.next, tail);
                    Reference2DoubleArrayMap.this.key[Reference2DoubleArrayMap.this.size] = null;
                }
                
                @Override
                public void forEachRemaining(final Consumer<? super Reference2DoubleMap.Entry<K>> action) {
                    final int max = Reference2DoubleArrayMap.this.size;
                    while (this.next < max) {
                        final BasicEntry<K> entry = this.entry;
                        final Object[] key = Reference2DoubleArrayMap.this.key;
                        final int next = this.next;
                        this.curr = next;
                        entry.key = (K)key[next];
                        this.entry.value = Reference2DoubleArrayMap.this.value[this.next++];
                        action.accept(this.entry);
                    }
                }
            };
        }
        
        @Override
        public ObjectSpliterator<Reference2DoubleMap.Entry<K>> spliterator() {
            return new EntrySetSpliterator(0, Reference2DoubleArrayMap.this.size);
        }
        
        @Override
        public void forEach(final Consumer<? super Reference2DoubleMap.Entry<K>> action) {
            for (int i = 0, max = Reference2DoubleArrayMap.this.size; i < max; ++i) {
                action.accept((Object)new BasicEntry((K)Reference2DoubleArrayMap.this.key[i], Reference2DoubleArrayMap.this.value[i]));
            }
        }
        
        @Override
        public void fastForEach(final Consumer<? super Reference2DoubleMap.Entry<K>> action) {
            final BasicEntry<K> entry = new BasicEntry<K>();
            for (int i = 0, max = Reference2DoubleArrayMap.this.size; i < max; ++i) {
                entry.key = (K)Reference2DoubleArrayMap.this.key[i];
                entry.value = Reference2DoubleArrayMap.this.value[i];
                action.accept(entry);
            }
        }
        
        @Override
        public int size() {
            return Reference2DoubleArrayMap.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.getValue() == null || !(e.getValue() instanceof Double)) {
                return false;
            }
            final K k = (K)e.getKey();
            return Reference2DoubleArrayMap.this.containsKey(k) && Double.doubleToLongBits(Reference2DoubleArrayMap.this.getDouble(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.getValue() == null || !(e.getValue() instanceof Double)) {
                return false;
            }
            final K k = (K)e.getKey();
            final double v = (double)e.getValue();
            final int oldPos = Reference2DoubleArrayMap.this.findKey(k);
            if (oldPos == -1 || Double.doubleToLongBits(v) != Double.doubleToLongBits(Reference2DoubleArrayMap.this.value[oldPos])) {
                return false;
            }
            final int tail = Reference2DoubleArrayMap.this.size - oldPos - 1;
            System.arraycopy(Reference2DoubleArrayMap.this.key, oldPos + 1, Reference2DoubleArrayMap.this.key, oldPos, tail);
            System.arraycopy(Reference2DoubleArrayMap.this.value, oldPos + 1, Reference2DoubleArrayMap.this.value, oldPos, tail);
            final Reference2DoubleArrayMap this$0 = Reference2DoubleArrayMap.this;
            --this$0.size;
            Reference2DoubleArrayMap.this.key[Reference2DoubleArrayMap.this.size] = null;
            return true;
        }
        
        final class EntrySetSpliterator extends ObjectSpliterators.EarlyBindingSizeIndexBasedSpliterator<Reference2DoubleMap.Entry<K>> implements ObjectSpliterator<Reference2DoubleMap.Entry<K>>
        {
            EntrySetSpliterator(final int pos, final int maxPos) {
                super(pos, maxPos);
            }
            
            @Override
            public int characteristics() {
                return 16465;
            }
            
            @Override
            protected final Reference2DoubleMap.Entry<K> get(final int location) {
                return new BasicEntry<K>((K)Reference2DoubleArrayMap.this.key[location], Reference2DoubleArrayMap.this.value[location]);
            }
            
            @Override
            protected final EntrySetSpliterator makeForSplit(final int pos, final int maxPos) {
                return new EntrySetSpliterator(pos, maxPos);
            }
        }
    }
    
    private final class KeySet extends AbstractReferenceSet<K>
    {
        @Override
        public boolean contains(final Object k) {
            return Reference2DoubleArrayMap.this.findKey(k) != -1;
        }
        
        @Override
        public boolean remove(final Object k) {
            final int oldPos = Reference2DoubleArrayMap.this.findKey(k);
            if (oldPos == -1) {
                return false;
            }
            final int tail = Reference2DoubleArrayMap.this.size - oldPos - 1;
            System.arraycopy(Reference2DoubleArrayMap.this.key, oldPos + 1, Reference2DoubleArrayMap.this.key, oldPos, tail);
            System.arraycopy(Reference2DoubleArrayMap.this.value, oldPos + 1, Reference2DoubleArrayMap.this.value, oldPos, tail);
            final Reference2DoubleArrayMap this$0 = Reference2DoubleArrayMap.this;
            --this$0.size;
            Reference2DoubleArrayMap.this.key[Reference2DoubleArrayMap.this.size] = null;
            return true;
        }
        
        @Override
        public ObjectIterator<K> iterator() {
            return new ObjectIterator<K>() {
                int pos = 0;
                
                @Override
                public boolean hasNext() {
                    return this.pos < Reference2DoubleArrayMap.this.size;
                }
                
                @Override
                public K next() {
                    if (!this.hasNext()) {
                        throw new NoSuchElementException();
                    }
                    return (K)Reference2DoubleArrayMap.this.key[this.pos++];
                }
                
                @Override
                public void remove() {
                    if (this.pos == 0) {
                        throw new IllegalStateException();
                    }
                    final int tail = Reference2DoubleArrayMap.this.size - this.pos;
                    System.arraycopy(Reference2DoubleArrayMap.this.key, this.pos, Reference2DoubleArrayMap.this.key, this.pos - 1, tail);
                    System.arraycopy(Reference2DoubleArrayMap.this.value, this.pos, Reference2DoubleArrayMap.this.value, this.pos - 1, tail);
                    final Reference2DoubleArrayMap this$0 = Reference2DoubleArrayMap.this;
                    --this$0.size;
                    --this.pos;
                    Reference2DoubleArrayMap.this.key[Reference2DoubleArrayMap.this.size] = null;
                }
                
                @Override
                public void forEachRemaining(final Consumer<? super K> action) {
                    final int max = Reference2DoubleArrayMap.this.size;
                    while (this.pos < max) {
                        action.accept((Object)Reference2DoubleArrayMap.this.key[this.pos++]);
                    }
                }
            };
        }
        
        @Override
        public ObjectSpliterator<K> spliterator() {
            return new KeySetSpliterator(0, Reference2DoubleArrayMap.this.size);
        }
        
        @Override
        public void forEach(final Consumer<? super K> action) {
            for (int i = 0, max = Reference2DoubleArrayMap.this.size; i < max; ++i) {
                action.accept((Object)Reference2DoubleArrayMap.this.key[i]);
            }
        }
        
        @Override
        public int size() {
            return Reference2DoubleArrayMap.this.size;
        }
        
        @Override
        public void clear() {
            Reference2DoubleArrayMap.this.clear();
        }
        
        final class KeySetSpliterator extends ObjectSpliterators.EarlyBindingSizeIndexBasedSpliterator<K> implements ObjectSpliterator<K>
        {
            KeySetSpliterator(final int pos, final int maxPos) {
                super(pos, maxPos);
            }
            
            @Override
            public int characteristics() {
                return 16465;
            }
            
            @Override
            protected final K get(final int location) {
                return (K)Reference2DoubleArrayMap.this.key[location];
            }
            
            @Override
            protected final KeySetSpliterator makeForSplit(final int pos, final int maxPos) {
                return new KeySetSpliterator(pos, maxPos);
            }
            
            @Override
            public void forEachRemaining(final Consumer<? super K> action) {
                final int max = Reference2DoubleArrayMap.this.size;
                while (this.pos < max) {
                    action.accept((Object)Reference2DoubleArrayMap.this.key[this.pos++]);
                }
            }
        }
    }
    
    private final class ValuesCollection extends AbstractDoubleCollection
    {
        @Override
        public boolean contains(final double v) {
            return Reference2DoubleArrayMap.this.containsValue(v);
        }
        
        @Override
        public DoubleIterator iterator() {
            return new DoubleIterator() {
                int pos = 0;
                
                @Override
                public boolean hasNext() {
                    return this.pos < Reference2DoubleArrayMap.this.size;
                }
                
                @Override
                public double nextDouble() {
                    if (!this.hasNext()) {
                        throw new NoSuchElementException();
                    }
                    return Reference2DoubleArrayMap.this.value[this.pos++];
                }
                
                @Override
                public void remove() {
                    if (this.pos == 0) {
                        throw new IllegalStateException();
                    }
                    final int tail = Reference2DoubleArrayMap.this.size - this.pos;
                    System.arraycopy(Reference2DoubleArrayMap.this.key, this.pos, Reference2DoubleArrayMap.this.key, this.pos - 1, tail);
                    System.arraycopy(Reference2DoubleArrayMap.this.value, this.pos, Reference2DoubleArrayMap.this.value, this.pos - 1, tail);
                    final Reference2DoubleArrayMap this$0 = Reference2DoubleArrayMap.this;
                    --this$0.size;
                    --this.pos;
                    Reference2DoubleArrayMap.this.key[Reference2DoubleArrayMap.this.size] = null;
                }
                
                @Override
                public void forEachRemaining(final DoubleConsumer action) {
                    final int max = Reference2DoubleArrayMap.this.size;
                    while (this.pos < max) {
                        action.accept(Reference2DoubleArrayMap.this.value[this.pos++]);
                    }
                }
            };
        }
        
        @Override
        public DoubleSpliterator spliterator() {
            return new ValuesSpliterator(0, Reference2DoubleArrayMap.this.size);
        }
        
        @Override
        public void forEach(final DoubleConsumer action) {
            for (int i = 0, max = Reference2DoubleArrayMap.this.size; i < max; ++i) {
                action.accept(Reference2DoubleArrayMap.this.value[i]);
            }
        }
        
        @Override
        public int size() {
            return Reference2DoubleArrayMap.this.size;
        }
        
        @Override
        public void clear() {
            Reference2DoubleArrayMap.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 Reference2DoubleArrayMap.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 = Reference2DoubleArrayMap.this.size;
                while (this.pos < max) {
                    action.accept(Reference2DoubleArrayMap.this.value[this.pos++]);
                }
            }
        }
    }
}
