/*
 * @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.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import net.ranides.assira.generic.HashUtils;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.function.BinaryOperator;
import net.ranides.assira.collection.ACollection;
import net.ranides.assira.collection.iterators.IteratorUtils;
import net.ranides.assira.collection.sets.IntHashSet;
import net.ranides.assira.collection.sets.IntSet;
import net.ranides.assira.collection.utils.HashCollection;
import net.ranides.assira.functional.Fold;
import net.ranides.assira.generic.CompareUtils;

/**
 * A type-specific hash map with primitive int mkeys.
 */
public class IntHashMultiMap<V> extends AIntHashMap<V> implements IntMultiMap<V> {

    private static final long serialVersionUID = 6L;

    /**
     * Creates a new hash map.
     *
     * <p>The actual table size will be the least power of two greater than
     * <code>expected</code>/
     * <code>factor</code>.
     *
     * @param expected the expected number of elements in the hash set.
     * @param f the load factor.
     */
    @SuppressWarnings("unchecked")
    public IntHashMultiMap(int expected, float factor) {
        super(expected, factor);
    }

    /**
     * Creates a new hash map with {@link Hash#DEFAULT_LOAD_FACTOR} as load
     * factor.
     *
     * @param expected the expected number of elements in the hash map.
     */
    public IntHashMultiMap(int expected) {
        super(expected, HashCollection.LOAD_FACTOR);
    }

    /**
     * Creates a new hash map with initial expected
     * {@link Hash#DEFAULT_INITIAL_SIZE} mentries and
     * {@link Hash#DEFAULT_LOAD_FACTOR} as load factor.
     */
    public IntHashMultiMap() {
        super(HashCollection.INITIAL_SIZE, HashCollection.LOAD_FACTOR);
    }

    /**
     * Creates a new hash map copying a given one.
     *
     * @param values a {@link Map} to be copied into the new hash map.
     * @param factor the load factor.
     */
    public IntHashMultiMap(Map<? extends Integer, ? extends V> values, float factor) {
        super(values.size(), factor);
        putAll(values);
    }

    /**
     * Creates a new hash map with {@link Hash#DEFAULT_LOAD_FACTOR} as load
     * factor copying a given one.
     *
     * @param values a {@link Map} to be copied into the new hash map.
     */
    public IntHashMultiMap(Map<? extends Integer, ? extends V> values) {
        this(values, HashCollection.LOAD_FACTOR);
    }

    /**
     * Creates a new hash map copying a given type-specific one.
     *
     * @param values a type-specific map to be copied into the new hash map.
     * @param factor the load factor.
     */
    public IntHashMultiMap(IntMap<V> values, float factor) {
        this(values.size(), factor);
        putAll(values);
    }

    /**
     * Creates a new hash map with {@link Hash#DEFAULT_LOAD_FACTOR} as load
     * factor copying a given type-specific one.
     *
     * @param values a type-specific map to be copied into the new hash map.
     */
    public IntHashMultiMap(IntMap<V> values) {
        this(values, HashCollection.LOAD_FACTOR);
    }

    /**
     * Creates a new hash map using the elements of two parallel arrays.
     *
     * @param keys the array of mkeys of the new hash map.
     * @param values the array of corresponding mvalues in the new hash map.
     * @param factor the load factor.
     * @throws IllegalArgumentException if <code>k</code> and <code>v</code>
     * have different lengths.
     */
    public IntHashMultiMap(int[] keys, V[] values, float factor) {
        super(keys.length, factor);
        if (keys.length != values.length) {
            throw new IllegalArgumentException("The key array and the value array have different lengths (" + keys.length + " and " + values.length + ")");
        }
        for (int i = 0; i < keys.length; i++) {
            put(keys[ i], values[ i]);
        }
    }

    /**
     * Creates a new hash map with {@link Hash#DEFAULT_LOAD_FACTOR} as load
     * factor using the elements of two parallel arrays.
     *
     * @param keys the array of mkeys of the new hash map.
     * @param values the array of corresponding mvalues in the new hash map.
     * @throws IllegalArgumentException if <code>k</code> and <code>v</code>
     * have different lengths.
     */
    public IntHashMultiMap(int[] keys, V[] values) {
        this(keys, values, HashCollection.LOAD_FACTOR);
    }
	
    @Override
	protected final int hash(int key) {
		return HashUtils.murmurHash3(key);
	}
    
    @Override
    protected final boolean compare(int key1, V value1, int key2, V value2) {
		return key1 == key2 && CompareUtils.equals(value1, value2);
	}
    
    @SuppressWarnings("unchecked")
    @Override
    public Collection<V> getAll(int key) {
        return new MCollection(key);
    }

    @Override
    public V get(int key, BinaryOperator<V> fold) {
        return Fold.apply(new MIterator(key), fold);
    }

    @SuppressWarnings("unchecked")
    @Override
    public boolean containsEntry(int key, Object value) {
        return used[ifind(key, (V)value)];
    }

    @Override
    public void putAll(int key, Iterable<? extends V> values) {
        for(V value : values) {
            put(key, value);
        }
    }

    @Override
    public Collection<V> removeAll(int key) {
        List<V> list = new ArrayList<>();
        int pos;
        while(used[pos = ifind(key)]) {
            list.add(ishift(pos));
        }
        return list;
    }
    
    @Override
    public IntSet uniqueKeySet() {
        return new IntHashSet(keySet());
    }

    private class MCollection extends ACollection<V> {
        
        private final int key;

        public MCollection(int key) {
            this.key = key;
        }

        @Override
        public Iterator<V> iterator() {
            return new MIterator(key);
        }

        @Override
        public int size() {
            return IteratorUtils.size(iterator());
        }

        @Override
        public boolean isEmpty() {
            return !iterator().hasNext();
        }
        
    }
    
    private class MIterator implements Iterator<V> {
        
        private int pos;
        private final int key;
        
        public MIterator(int key) {
            this.key = key;
            this.pos = ifind(key);
        }

        @Override
        public boolean hasNext() {
            return used[pos];
        }

        @Override
        public V next() {
            if(!used[pos]) {
                throw new NoSuchElementException();
            }
            V last = values[pos];
            do {
                pos = inext(pos);
            } while(used[pos] && !compare(key, keys[pos]));
            return last;
        }
        
    }

}
