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

import net.ranides.assira.generic.HashUtils;
import java.util.Map;
import net.ranides.assira.collection.utils.HashCollection;

/**
 * A type-specific hash map with primitive int mkeys.
 */
public class IntHashMap<V> extends AIntHashMap<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 IntHashMap(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 IntHashMap(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 IntHashMap() {
        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 IntHashMap(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 IntHashMap(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 IntHashMap(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 IntHashMap(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 IntHashMap(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 IntHashMap(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;
	}

}
