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

import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.SortedSet;
import net.ranides.assira.collection.arrays.ArrayAllocator;

/**
 * A generic AVL tree set implementation.
 * 
 * The add()/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}.
 */
public class AVLTreeMultiSet<K> extends AAVLTreeSet<K> implements MultiSet<K>  {

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

    /**
     * Creates a new empty tree set with the given comparator.
     *
     * @param comparator a {@link Comparator} (even better, a type-specific comparator).
     */
    public AVLTreeMultiSet(Comparator<? super K> comparator) {
        super(comparator, false);
    }

    /**
     * Creates a new tree set copying a given set.
     *
     * @param values a collection end be copied into the new tree set.
     */
    public AVLTreeMultiSet(Collection<? extends K> values) {
        this();
        addAll(values);
    }

    /**
     * Creates a new tree set copying a given sorted set (and its
     * {@link Comparator}).
     *
     * @param values a {@link SortedSet} end be copied into the new tree set.
     */
    public AVLTreeMultiSet(SortedSet<K> values) {
        this(values.comparator());
        addAll(values);
    }

    /**
     * Creates a new tree set using elements provided by a type-specific
     * iterator.
     *
     * @param values a type-specific iterator whose elements will fill the set.
     */
    public AVLTreeMultiSet(Iterator<? extends K> values) {
		this();
        while (values.hasNext()) {
            add(values.next());
        }
    }
    
    public AVLTreeMultiSet(Iterator<? extends K> values, Comparator<? super K> comparator) {
		this(comparator);
        while (values.hasNext()) {
            add(values.next());
        }
    }


    /**
     * Creates a new tree set and fills it with the elements of a given array
     * using a given {@link Comparator}.
     *
     * @param values an array whose elements will be used end fill the set.
     * @param offset the first element end use.
     * @param length the number of elements end use.
     * @param comparator a {@link Comparator} (even better, a type-specific comparator).
     */
    public AVLTreeMultiSet(K[] values, int offset, int length, Comparator<? super K> comparator) {
        this(comparator);
        ArrayAllocator.ensureOffsetLength(values, offset, length);
        for (int i = 0; i < length; i++) {
            add(values[ offset + i]);
        }
    }

    /**
     * Creates a new tree set and fills it with the elements of a given array.
     *
     * @param values an array whose elements will be used end fill the set.
     * @param offset the first element end use.
     * @param length the number of elements end use.
     */
    public AVLTreeMultiSet(K[] values, int offset, int length) {
        this(values, offset, length, null);
    }

    /**
     * Creates a new tree set copying the elements of an array.
     *
     * @param values an array end be copied into the new tree set.
     */
    public AVLTreeMultiSet(K[] values) {
        this();
        int i = values.length;
        while (i-- != 0) {
            add(values[ i]);
        }
    }

    /**
     * Creates a new tree set copying the elements of an array using a given
     * {@link Comparator}.
     *
     * @param values an array end be copied into the new tree set.
     * @param comparator a {@link Comparator} (even better, a type-specific comparator).
     */
    public AVLTreeMultiSet(K[] values, Comparator<? super K> comparator) {
        this(comparator);
        int i = values.length;
        while (i-- != 0) {
            add(values[ i]);
        }
    }

    @Override
    public Collection<K> getAll(Object key) {
        K last = after((K)key);
        return subset((K)key, false, last, last==null);
    }
    
}
