/*
 * @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;
import net.ranides.assira.generic.CompareUtils;

/**
 * A generic hash map implementation.
 *
 * <P>Instances of this class use a hash table to represent a map. The table is
 * enlarged as needed by doubling its size when new entries are created, but it
 * is <em>never</em> made smaller (even on a {@link #clear()}). A family of {@linkplain #trim() trimming
 * methods} lets you control the size of the table; this is particularly useful
 * if you reuse instances of this class.
 */
public class HashMap<K, V> extends AHashMap<K, V> {

    private static final long serialVersionUID = 0L;
    
    /**
     * Creates a new hash map.
     *
     * <p>The actual table size will be the least power of two greater than
     * <code>expected</code>/
     * <code>f</code>.
     *
     * @param expected the expected number of elements in the hash set.
     * @param factor the load factor.
     */
    @SuppressWarnings("unchecked")
    public HashMap(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 HashMap(int expected) {
        super(expected, HashCollection.LOAD_FACTOR);
    }

    /**
     * Creates a new hash map with initial expected
     * {@link Hash#DEFAULT_INITIAL_SIZE} entries and
     * {@link Hash#DEFAULT_LOAD_FACTOR} as load factor.
     */
    public HashMap() {
        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 HashMap(Map<? extends K, ? 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 HashMap(Map<? extends K, ? extends V> values) {
        this(values, HashCollection.LOAD_FACTOR);
    }

    /**
     * Creates a new hash map using the elements of two parallel arrays.
     *
     * @param keys the array of keys of the new hash map.
     * @param values the array of corresponding values in the new hash map.
     * @param factor the load factor.
     * @throws IllegalArgumentException if <code>k</code> and <code>v</code>
     * have different lengths.
     */
    public HashMap(K[] keys, V values[], float factor) {
        this(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++) {
            this.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 keys of the new hash map.
     * @param values the array of corresponding values in the new hash map.
     * @throws IllegalArgumentException if <code>k</code> and <code>v</code>
     * have different lengths.
     */
    public HashMap(K[] keys, V[] values) {
        this(keys, values, HashCollection.LOAD_FACTOR);
    }
    
    @Override
    protected int hash(K key) {
        return key == null ? 0x87fcd5c : HashUtils.murmurHash3(key.hashCode());
    }

    @Override
    protected boolean compare(K key1, V value1, K key2, V value2) {
        return CompareUtils.equals(key1, key2);
    }

}
