Class IntArraySet

  • All Implemented Interfaces:
    IntCollection, IntIterable, IntSet, java.io.Serializable, java.lang.Cloneable, java.lang.Iterable<java.lang.Integer>, java.util.Collection<java.lang.Integer>, java.util.Set<java.lang.Integer>

    public class IntArraySet
    extends AbstractIntSet
    implements java.io.Serializable, java.lang.Cloneable
    A simple, brute-force implementation of a set based on a backing array.

    The main purpose of this implementation is that of wrapping cleanly the brute-force approach to the storage of a very small number of items: just put them into an array and scan linearly to find an item.

    See Also:
    Serialized Form
    • Constructor Detail

      • IntArraySet

        public IntArraySet​(int[] a)
        Creates a new array set using the given backing array. The resulting set will have as many elements as the array.

        It is the responsibility of the caller to ensure that the elements of a are distinct.

        Parameters:
        a - the backing array.
      • IntArraySet

        public IntArraySet()
        Creates a new empty array set.
      • IntArraySet

        public IntArraySet​(int capacity)
        Creates a new empty array set of given initial capacity.
        Parameters:
        capacity - the initial capacity.
      • IntArraySet

        public IntArraySet​(IntCollection c)
        Creates a new array set copying the contents of a given collection.
        Parameters:
        c - a collection.
      • IntArraySet

        public IntArraySet​(java.util.Collection<? extends java.lang.Integer> c)
        Creates a new array set copying the contents of a given set.
        Parameters:
        c - a collection.
      • IntArraySet

        public IntArraySet​(IntSet c)
        Creates a new array set copying the contents of a given collection.
        Parameters:
        c - a collection.
      • IntArraySet

        public IntArraySet​(java.util.Set<? extends java.lang.Integer> c)
        Creates a new array set copying the contents of a given set.
        Parameters:
        c - a collection.
      • IntArraySet

        public IntArraySet​(int[] a,
                           int size)
        Creates a new array set using the given backing array and the given number of elements of the array.

        It is the responsibility of the caller to ensure that the first size elements of a are distinct.

        Parameters:
        a - the backing array.
        size - the number of valid elements in a.
    • Method Detail

      • of

        public static IntArraySet of()
        Creates a new empty array set.
        Returns:
        a new empty array set.
      • of

        public static IntArraySet of​(int e)
        Creates a new array set using the element given.
        Parameters:
        e - the element that the returned set will contain.
        Returns:
        a new array set containing e.
      • of

        public static IntArraySet of​(int... a)
        Creates a new array set using an array of elements.

        Unlike the array accepting constructors, this method will throw if duplicate elements are encountered. This adds a non-trivial validation burden. Use ofUnchecked() if you know your input has no duplicates, which will skip this validation.

        Parameters:
        a - the backing array of the returned array set.
        Returns:
        a new array set containing the elements in a.
        Throws:
        java.lang.IllegalArgumentException - if there were duplicate entries.
      • ofUnchecked

        public static IntArraySet ofUnchecked()
        Creates a new empty array set.
        Returns:
        a new empty array set.
      • ofUnchecked

        public static IntArraySet ofUnchecked​(int... a)
        Creates a new array set using an array of elements.

        It is the responsibility of the caller to ensure that the elements of a are distinct.

        Parameters:
        a - the backing array of the returned array set.
        Returns:
        a new array set containing the elements in a.
      • iterator

        public IntIterator iterator()
        Description copied from interface: IntCollection
        Returns a type-specific iterator on the elements of this collection.
        Specified by:
        iterator in interface java.util.Collection<java.lang.Integer>
        Specified by:
        iterator in interface IntCollection
        Specified by:
        iterator in interface IntIterable
        Specified by:
        iterator in interface IntSet
        Specified by:
        iterator in interface java.lang.Iterable<java.lang.Integer>
        Specified by:
        iterator in interface java.util.Set<java.lang.Integer>
        Specified by:
        iterator in class AbstractIntSet
        Returns:
        a type-specific iterator on the elements of this collection.
        See Also:
        Iterable.iterator()
      • spliterator

        public IntSpliterator spliterator()
        Returns a type-specific spliterator on the elements of this set.

        Set spliterators must report at least Spliterator.DISTINCT.

        See Set.spliterator() for more documentation on the requirements of the returned spliterator.

        In addition to the usual trait of DISTINCT for sets, the returned spliterator will also report the trait ORDERED.

        The returned spliterator is late-binding; it will track structural changes after the current item, up until the first trySplit(), at which point the maximum index will be fixed.
        Structural changes before the current item or after the first trySplit() will result in unspecified behavior.

        Specified by:
        spliterator in interface java.util.Collection<java.lang.Integer>
        Specified by:
        spliterator in interface IntCollection
        Specified by:
        spliterator in interface IntIterable
        Specified by:
        spliterator in interface IntSet
        Specified by:
        spliterator in interface java.lang.Iterable<java.lang.Integer>
        Specified by:
        spliterator in interface java.util.Set<java.lang.Integer>
        Returns:
        a type-specific spliterator on the elements of this collection.
      • size

        public int size()
        Specified by:
        size in interface java.util.Collection<java.lang.Integer>
        Specified by:
        size in interface java.util.Set<java.lang.Integer>
        Specified by:
        size in class java.util.AbstractCollection<java.lang.Integer>
      • remove

        public boolean remove​(int k)
        Description copied from class: AbstractIntSet
        Removes an element from this set. Delegates to the type-specific rem() method implemented by type-specific abstract Collection superclass.
        Specified by:
        remove in interface IntSet
        Overrides:
        remove in class AbstractIntSet
        See Also:
        Collection.remove(Object)
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Collection<java.lang.Integer>
        Specified by:
        clear in interface java.util.Set<java.lang.Integer>
        Overrides:
        clear in class java.util.AbstractCollection<java.lang.Integer>
      • isEmpty

        public boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Collection<java.lang.Integer>
        Specified by:
        isEmpty in interface java.util.Set<java.lang.Integer>
        Overrides:
        isEmpty in class java.util.AbstractCollection<java.lang.Integer>
      • toIntArray

        public int[] toIntArray()
        Description copied from interface: IntCollection
        Returns a primitive type array containing the items of this collection.
        Specified by:
        toIntArray in interface IntCollection
        Overrides:
        toIntArray in class AbstractIntCollection
        Returns:
        a primitive type array containing the items of this collection.
        See Also:
        Collection.toArray()
      • toArray

        public int[] toArray​(int[] a)
        Description copied from interface: IntCollection
        Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array.

        Note that, contrarily to Collection.toArray(Object[]), this methods just writes all elements of this collection: no special value will be added after the last one.

        Specified by:
        toArray in interface IntCollection
        Overrides:
        toArray in class AbstractIntCollection
        Parameters:
        a - if this array is big enough, it will be used to store this collection.
        Returns:
        a primitive type array containing the items of this collection.
        See Also:
        Collection.toArray(Object[])
      • clone

        public IntArraySet clone()
        Returns a deep copy of this set.

        This method performs a deep copy of this array set; the data stored in the set, however, is not cloned. Note that this makes a difference only for object keys.

        Returns:
        a deep copy of this set.