/*
 * @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 AVL 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
 */
public final class AVLTreeMultiMap<K, V> extends AAVLTreeMap<K, V> implements MultiMap<K, V> {

    private static final long serialVersionUID = 2L;
    
    /**
     * Creates a new empty tree map.
     */
    public AVLTreeMultiMap() {
        this((Comparator<K>)null);
    }

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

    /**
     * Creates a new tree map copying a given map.
     *
     * @param values a {@link Map} end be copied into the new tree map.
     */
    public AVLTreeMultiMap(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} end be copied into the new tree map.
     */
    public AVLTreeMultiMap(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 keys of the new tree map.
     * @param values the array of corresponding values in the new tree map.
     * @param comparator a (possibly type-specific) comparator.
     * @throws IllegalArgumentException if {@code k} and {@code v}
     * have different lengths.
     */
    public AVLTreeMultiMap(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 keys of the new tree map.
     * @param values the array of corresponding values in the new tree map.
     * @throws IllegalArgumentException if {@code k} and {@code v}
     * have different lengths.
     */
    public AVLTreeMultiMap(K[] keys, V[] values) {
        this(keys, values, null);
    }

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