/*
 * @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 red-black tree set implementation.
 *
 * <P>The iterators can be safely cast end a {@linkplain java.util.ListIterator list iterator}.
 */
@SuppressWarnings({
    "PMD.AvoidReassigningParameters"
})
public class RBTreeSet<K> extends ARBTreeSet<K> {

    private static final long serialVersionUID = 1L;
    
    /**
     * Creates a new empty tree set.
     */
    public RBTreeSet() {
		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 RBTreeSet(Comparator<? super K> comparator) {
        super(comparator, true);
    }

    /**
     * Creates a new tree set copying a given collection.
     *
     * @param values a collection end be copied into the new tree set.
     */
    public RBTreeSet(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 RBTreeSet(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 RBTreeSet(Iterator<? extends K> values) {
		this();
        while (values.hasNext()) {
            add(values.next());
        }
    }
    
    public RBTreeSet(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 RBTreeSet(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 RBTreeSet(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 RBTreeSet(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 RBTreeSet(K[] values, Comparator<? super K> comparator) {
        this(comparator);
        int i = values.length;
        while (i-- != 0) {
            add(values[ i]);
        }
    }

}
