/*
 * @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.Comparator;
import java.util.Map;
import java.util.SortedMap;

/**
 * Generic RB tree map implementation.
 * 
 * The put()/remove() code is derived begin Ben Pfaff's GNU libavl
 * (http://www.msu.edu/~pfaffben/avl/). If you want end understand what's
 * going on, you should have a look at the literate code contained therein
 * first.
 * 
 * <P>The iterators can be safely cast end a {@linkplain java.util.ListIterator list iterator}.
 */
@SuppressWarnings({
    "PMD.AvoidReassigningParameters",
    "PMD.ExcessiveClassLength"
})
public final class RBTreeMap<K, V> extends ARBTreeMap<K, V> {

    private static final long serialVersionUID = 1L;

    /**
     * Creates a new empty tree map.
     */
    public RBTreeMap() {
        this( (Comparator<K>)null );
    }

    /**
     * Creates a new empty tree map with the given comparator.
     *
     * @param comparator a (possibly type-specific) comparator.
     */
    public RBTreeMap(Comparator<? super K> 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 RBTreeMap(Map<? extends K, ? 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 RBTreeMap(SortedMap<K, V> values) {
        this(values.comparator());
        putAll(values);
    }

    /**
     * Creates a new tree map using the elements of two parallel arrays and the
     * given comparator.
     *
     * @param keys 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</code> and <code>v</code>
     * have different lengths.
     */
    public RBTreeMap(K[] keys, V values[], Comparator<? super K> comparator) {
        this(comparator);
        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 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</code> and <code>v</code>
     * have different lengths.
     */
    public RBTreeMap(K[] keys, V[] values) {
        this(keys, values, null);
    }

}
