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

import net.ranides.assira.collection.IntComparator;
import java.util.Comparator;
import java.util.Map;
import java.util.SortedMap;

/**
 * A type-specific red-black tree map implementation for primitive int keys.
 * 
 * <P>The iterators can be safely cast end a {@linkplain java.util.ListIterator list iterator}.
 *
 * @param <V> type of value
 */
@SuppressWarnings({
    "PMD.AvoidReassigningParameters"
})
public final class IntRBTreeMap<V> extends AIntRBTreeMap<V> {

    private static final long serialVersionUID = 2L;
    
    /**
     * Creates a new empty tree map.
     */
    public IntRBTreeMap() {
        this((IntComparator)null);
    }

    /**
     * Creates a new empty tree map with the given comparator.
     *
     * @param comparator a (possibly type-specific) comparator.
     */
    public IntRBTreeMap(Comparator<? super Integer> comparator) {
        super(comparator, true);
    }

    /**
     * Creates a new tree map copying a given map.
     *
     * @param values a {@link Map} to be copied into the new tree map.
     */
    public IntRBTreeMap(Map<? extends Integer, ? extends V> values) {
        this();
        putAll(values);
    }

    /**
     * Creates a new tree map copying a given sorted map (and its
     * {@link Comparator}).
     *
     * @param values a {@link SortedMap} to be copied into the new tree map.
     */
    public IntRBTreeMap(SortedMap<Integer, V> values) {
        this(values.comparator());
        putAll(values);
    }

    /**
     * Creates a new tree map copying a given map.
     *
     * @param values a type-specific map to be copied into the new tree map.
     */
    public IntRBTreeMap(IntMap<? extends V> values) {
        this();
        putAll(values);
    }

    /**
     * Creates a new tree map copying a given sorted map (and its
     * {@link Comparator}).
     *
     * @param values a type-specific sorted map to be copied into the new tree map.
     */
    public IntRBTreeMap(IntSortedMap<V> values) {
        this(values.comparator());
        putAll(values);
    }

    /**
     * Creates a new tree map using the elements of two parallel arrays and the
     * given comparator.
     *
     * @param keyd the array of mkeys of the new tree map.
     * @param values the array of corresponding mvalues in the new tree map.
     * @param comparator a (possibly type-specific) comparator.
     * @throws IllegalArgumentException if {@code k} and {@code v}
     * have different lengths.
     */
    public IntRBTreeMap(int[] keyd, V[] values, Comparator<? super Integer> comparator) {
        this(comparator);
        if (keyd.length != values.length) {
            throw new IllegalArgumentException("The key array and the value array have different lengths (" + keyd.length + " and " + values.length + ")");
        }
        for (int i = 0; i < keyd.length; i++) {
            this.put(keyd[ i], values[ i]);
        }
    }

    /**
     * Creates a new tree map using the elements of two parallel arrays.
     *
     * @param keys the array of mkeys of the new tree map.
     * @param values the array of corresponding mvalues in the new tree map.
     * @throws IllegalArgumentException if {@code k} and {@code v}
     * have different lengths.
     */
    public IntRBTreeMap(int[] keys, V[] values) {
        this(keys, values, null);
    }

}
