/*
 * @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 java.util.Collection;
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
 * (<a href="http://www.msu.edu/~pfaffben/avl/">...</a>). 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}.
 *
 * @param <K> type of key
 * @param <V> type of value
 */
@SuppressWarnings({
    "PMD.AvoidReassigningParameters",
    "PMD.ExcessiveClassLength"
})
public final class RBTreeMultiMap<K, V> extends ARBTreeMap<K, V> implements MultiMap<K, V> {

    private static final long serialVersionUID = 1L;

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

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

    /**
     * Creates a new tree map copying a given map.
     *
     * @param values a {@link Map} to be copied into the new tree map.
     */
    public RBTreeMultiMap(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 RBTreeMultiMap(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} and {@code v}
     * have different lengths.
     */
    public RBTreeMultiMap(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} and {@code v}
     * have different lengths.
     */
    public RBTreeMultiMap(K[] keys, V[] values) {
        this(keys, values, null);
    }

    @SuppressWarnings("unchecked")
    @Override
    public Collection<V> getAll(Object key) {
        K last = after((K)key);
        return submap((K)key, false, last, last==null).values();
    }
    
}
