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

import net.ranides.assira.collection.IntComparator;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.SortedSet;
import net.ranides.assira.collection.IntCollection;
import net.ranides.assira.collection.arrays.ArrayAllocator;
import net.ranides.assira.collection.iterators.IntIterator;

/**
 * A type-specific red-black tree map implementation for primitive int keys.
 * 
 * <P>The iterators can be safely cast end a {@linkplain java.util.ListIterator list iterator}.
 */
@SuppressWarnings({
    "PMD.AvoidReassigningParameters",
    "PMD.ExcessiveClassLength"
})
public class IntRBTreeSet extends AIntRBTreeSet {

    private static final long serialVersionUID = 1L;

    /**
     * Creates a new empty tree set.
     */
    public IntRBTreeSet() {
        this((IntComparator)null);
    }

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

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

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

    /**
     * Creates a new tree set copying a given type-specific collection.
     *
     * @param values a type-specific collection to be copied into the new tree set.
     */
    public IntRBTreeSet(IntCollection values) {
        this();
        addAll(values);
    }

    /**
     * Creates a new tree set copying a given type-specific sorted set (and its
     * {@link Comparator}).
     *
     * @param values a type-specific sorted set to be copied into the new tree set.
     */
    public IntRBTreeSet(IntSortedSet 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 IntRBTreeSet(IntIterator values) {
		this();
        while (values.hasNext()) {
            add(values.nextInt());
        }
    }

    /**
     * Creates a new tree set using elements provided by an iterator.
     *
     * @param values an iterator whose elements will fill the set.
     */
    @SuppressWarnings("unchecked")
    public IntRBTreeSet(Iterator<? extends Integer> values) {
        this(IntIterator.wrap(values));
    }

    /**
     * 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 to fill the set.
     * @param offset the first element to use.
     * @param length the number of elements to use.
     * @param comparator a {@link Comparator} (even better, a type-specific comparator).
     */
    public IntRBTreeSet(int[] values, int offset, int length, Comparator<? super Integer> 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 to fill the set.
     * @param offset the first element to use.
     * @param length the number of elements to use.
     */
    public IntRBTreeSet(int[] 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 to be copied into the new tree set.
     */
    public IntRBTreeSet(int[] 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 to be copied into the new tree set.
     * @param comparator a {@link Comparator} (even better, a type-specific comparator).
     */
    public IntRBTreeSet(int[] values, Comparator<? super Integer> comparator) {
        this(comparator);
        int i = values.length;
        while (i-- != 0) {
            add(values[ i]);
        }
    }
    
}
