Class SparseIntSet

  • All Implemented Interfaces:
    IntSet, java.io.Serializable
    Direct Known Subclasses:
    MutableSparseIntSet

    public class SparseIntSet
    extends java.lang.Object
    implements IntSet
    A sparse ordered, duplicate-free, fully-encapsulated set of integers; not necessary mutable
    See Also:
    Serialized Form
    • Field Detail

      • elements

        protected int[] elements
        The backing store of int arrays
      • size

        protected int size
        The number of entries in the backing store that are valid.
    • Constructor Detail

      • SparseIntSet

        protected SparseIntSet​(int size)
      • SparseIntSet

        protected SparseIntSet​(int[] backingArray)
        Subclasses should use this with extreme care. Do not allow the backing array to escape elsewhere.
      • SparseIntSet

        public SparseIntSet()
        Subclasses should use this with extreme care.
      • SparseIntSet

        public SparseIntSet​(IntSet S)
                     throws java.lang.IllegalArgumentException
        Throws:
        java.lang.IllegalArgumentException
    • Method Detail

      • contains

        public final boolean contains​(int x)
        Does this set contain value x?
        Specified by:
        contains in interface IntSet
        Returns:
        true iff this set contains integer i
        See Also:
        IntSet.contains(int)
      • getIndex

        public final int getIndex​(int x)
        Returns:
        index i s.t. elements[i] == x, or -1 if not found.
      • size

        public final int size()
        Specified by:
        size in interface IntSet
        Returns:
        the number of elements in this set
      • isEmpty

        public final boolean isEmpty()
        Specified by:
        isEmpty in interface IntSet
        Returns:
        true iff this set is empty
      • elementAt

        public final int elementAt​(int idx)
                            throws java.util.NoSuchElementException
        Throws:
        java.util.NoSuchElementException
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • parseIntArray

        public static int[] parseIntArray​(java.lang.String str)
        Reverse of toString(): "{2,3}" -> [2,3]
        Throws:
        java.lang.IllegalArgumentException - if str is null
      • singleton

        public static SparseIntSet singleton​(int i)
      • intersection

        public IntSet intersection​(IntSet that)
        Description copied from interface: IntSet
        This implementation must not despoil the original value of "this"
        Specified by:
        intersection in interface IntSet
        Returns:
        a new IntSet which is the intersection of this and that
      • union

        public IntSet union​(IntSet that)
        Description copied from interface: IntSet
        This implementation must not despoil the original value of "this"
        Specified by:
        union in interface IntSet
        Returns:
        a new IntSet containing all elements of this and that
      • max

        public final int max()
                      throws java.lang.IllegalStateException
        Specified by:
        max in interface IntSet
        Returns:
        the largest element in the set
        Throws:
        java.lang.IllegalStateException
      • foreach

        public void foreach​(IntSetAction action)
        Description copied from interface: IntSet
        Invoke an action on each element of the Set
        Specified by:
        foreach in interface IntSet
      • foreachExcluding

        public void foreachExcluding​(IntSet X,
                                     IntSetAction action)
        Description copied from interface: IntSet
        Invoke an action on each element of the Set, excluding elements of Set X
        Specified by:
        foreachExcluding in interface IntSet
      • add

        public static SparseIntSet add​(SparseIntSet s,
                                       int j)
        Returns:
        a new sparse int set which adds j to s
        Throws:
        java.lang.IllegalArgumentException - if s is null
      • isSubset

        public boolean isSubset​(IntSet that)
        Specified by:
        isSubset in interface IntSet
        Returns:
        true iff this is a subset of that.
      • containsAny

        public boolean containsAny​(IntSet set)
        Specified by:
        containsAny in interface IntSet
        Returns:
        true iff this set contains integer i
      • containsAny

        public boolean containsAny​(SparseIntSet set)
                            throws java.lang.IllegalArgumentException
        Throws:
        java.lang.IllegalArgumentException
      • toIntArray

        public int[] toIntArray()
        Returns:
        contents as an int[]