/*
 * @author Ranides Atterwim <ranides@gmail.com>
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */

package net.ranides.assira.collection.maps;

import java.util.AbstractSet;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import net.ranides.assira.collection.IntComparator;
import net.ranides.assira.collection.arrays.ArraySearch;
import net.ranides.assira.collection.arrays.IntArrays;
import net.ranides.assira.collection.arrays.NativeArraySort;
import net.ranides.assira.collection.iterators.IntIterator;
import net.ranides.assira.collection.iterators.RandomAccessIterator;
import net.ranides.assira.collection.sets.AIntSet;
import net.ranides.assira.collection.sets.IntSet;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public class IntArrayMap<V> extends AIntMap<V> {

    private static final long serialVersionUID = 1L;
    
    private final Class<?> type;
    private final int[] keys;
    private final V[] vars;
    
    private final IntComparator cmp;
    
    public IntArrayMap(int[] keys, V[] vars) {
        this(keys, vars, IntComparator.ASC);
    }

    public IntArrayMap(int[] keys, V[] vars, IntComparator comparator) {
        this.type = keys.getClass().getComponentType();
        this.keys = keys.clone();
        this.vars = vars.clone();
        this.cmp = comparator;
        sort();
    }
    
    private void sort() {
        NativeArraySort.quickSort(0, keys.length, (a,b) -> cmp.compare(keys[a],keys[b]), (a,b) -> {
            swap(keys, a, b);
            swap(vars, a, b);
        });
    }
    
    private static <T> void swap(int[] array, int a, int b) {
        int t = array[a];
        array[a] = array[b];
        array[b] = t;
    }
    
    private static <T> void swap(T[] array, int a, int b) {
        T t = array[a];
        array[a] = array[b];
        array[b] = t;
    }

    @Override
    public Set<IntEntry<V>> fastEntrySet() {
        return new AbstractSet<IntEntry<V>>() {
            @Override
            public Iterator<IntEntry<V>> iterator() {
                return new AIterator(0);
            }

            @Override
            public int size() {
                return keys.length;
            }

        };
    }
    
    
    
    @Override
    public Collection<V> values() {
        return Arrays.asList(vars);
    }

    @Override
    public IntSet keySet() {
        return new AKeys();
    }

    @Override
    public V get(int key) {
        if(!type.isInstance(key)) {
            return null;
        }
        int i = ArraySearch.binarySearch(keys, key, cmp);
        return i<0 ? null : vars[i];
    }

    @Override
    public boolean containsKey(int key) {
        if(!type.isInstance(key)) {
            return false;
        }
        int i = ArraySearch.binarySearch(keys, key, cmp);
        return i >= 0;
    }

    @Override
    public boolean containsValue(Object value) {
        if (value==null) {
            for (int i=0; i<vars.length; i++) {
                if (vars[i]==null) { return true; }
            }
        } else {
            for(int i=0; i<vars.length; i++) {
                if( value.equals(vars[i]) ) { return true; }
            }
        }
        return false;
    }
    
    @Override
    public int size() {
        return keys.length;
    }
    
    private class AEntry implements IntEntry<V> {
    
        private final int index;

        public AEntry(int index) {
            this.index = index;
        }

        @Override
        public int getIntKey() {
            return keys[index];
        }

        @Override
        public V getValue() {
            return vars[index];
        }

        @Override
        public V setValue(V value) {
            V prev = vars[index];
            vars[index] = value;
            return prev;
        }
        
    }
    
    private class AIterator extends RandomAccessIterator<IntEntry<V>> {

        public AIterator(int index) {
            super(index);
        }

        @Override
        protected int size() {
            return keys.length;
        }

        @Override
        protected IntEntry<V> get(int index) {
            return new AEntry(index);
        }

    }

    private class AKeys extends AIntSet {
        
        private static final long serialVersionUID = 1L;

        @Override
        public IntIterator iterator() {
            return IntArrays.iterator(keys);
        }

        @Override
        public int size() {
            return keys.length;
        }

        @Override
        public boolean contains(Object o) {
            return containsKey(o);
        }
    }

}
