Class IntSet

    • Constructor Summary

      Constructors 
      Constructor Description
      IntSet()
      Construct a new empty set.
      IntSet​(int capacity)
      Construct a new empty set, with the given initial allocation size
      IntSet​(int[] fromData)
      Construct a set from an integer array.
      IntSet​(int[] fromData, boolean sorted)
      Construct a set from an integer array.
      IntSet​(IntSet fromSet)
      Construct a copy of an IntSet.
      IntSet​(DataInput inStream)
      FastSerializable constructor.
      IntSet​(String str)
      Construct a set from a toString() representation
    • Constructor Detail

      • IntSet

        public IntSet()
        Construct a new empty set.
      • IntSet

        public IntSet​(int capacity)
        Construct a new empty set, with the given initial allocation size
        Parameters:
        capacity - the initial storage capacity to use; may be adjusted if too small.
      • IntSet

        public IntSet​(IntSet fromSet)
        Construct a copy of an IntSet.
        Parameters:
        fromSet - the set to copy
      • IntSet

        public IntSet​(int[] fromData,
                      boolean sorted)
        Construct a set from an integer array.
        Parameters:
        fromData - the initial contents of the set; not modified
        sorted - if true, fromArray is assumed to be already sorted
      • IntSet

        public IntSet​(int[] fromData)
        Construct a set from an integer array.
        Parameters:
        fromData - an array holding the initial set contents; assumed to be unsorted.
    • Method Detail

      • of

        public static IntSet of​(int... values)
        Construct a set with the given values. This is a separate static method to avoid confusion with the single-int ctor variant.
        Parameters:
        values - values to include in the new set
        Returns:
        a new IntSet
      • emptySet

        public static IntSet emptySet()
        Construct a new empty set. Equivalent to "new IntSet()" but reads a little more nicely.
        Returns:
        a new empty IntSet
      • copyOf

        public static IntSet copyOf​(int[] from)
        Construct a new set that contains the given values.
        Parameters:
        from - an array of unsorted values
        Returns:
        a new IntSet
      • copyOf

        public static IntSet copyOf​(Set<Integer> fromSet)
        Construct a new set that contains a copy of the values in the given set.
        Parameters:
        fromSet - a set of Integers
        Returns:
        a new IntSet
      • clone

        public IntSet clone()
        Performs a deep copy of the set.
        Overrides:
        clone in class Object
      • equals

        public boolean equals​(Object other)
        Set equality.
        Overrides:
        equals in class Object
      • hashCode

        public int hashCode()
        Computes a hash code value for this object.
        Overrides:
        hashCode in class Object
        Returns:
        int hash code
      • isEmpty

        public boolean isEmpty()
        Determines whether this IntSet represents an empty set.
        Returns:
        whether size of set is 0
      • add

        public boolean add​(int val)
        Add a value to the set.
        Parameters:
        val - the value to add
        Returns:
        true if the value was not already present
      • addAll

        public IntSet addAll​(int[] vals)
        Add an array of values to the set.
        Parameters:
        vals - the values to add.
        Returns:
        this IntSet
      • addAll

        public IntSet addAll​(IntSet other)
        Add the values from another IntSet to this set.
        Parameters:
        other - the set of values to add.
        Returns:
        this IntSet.
      • remove

        public boolean remove​(int val)
        Remove a value from the set.
        Parameters:
        val - the value to remove
        Returns:
        true if the value was present (and removed)
      • removeAll

        public IntSet removeAll​(int[] vals)
        Remove an array of values to the set.
        Parameters:
        vals - the values to remove.
        Returns:
        this IntSet
      • removeAll

        public IntSet removeAll​(IntSet other)
        Remove all values in the provided IntSet from this set.
        Parameters:
        other - the set of values to remove from this set.
        Returns:
        this IntSet.
      • contains

        public boolean contains​(int val)
        Check if a value is contained in the set.
        Parameters:
        val - the value to check for
        Returns:
        true if val is present in the set
      • containsAll

        public boolean containsAll​(int[] vals)
        Check if the given values are contained in the set.
        Parameters:
        vals - the values to check for
        Returns:
        true if all the values are present in the set, false otherwise
      • containsAll

        public boolean containsAll​(IntSet other)
        Check if the given values are contained in this set.
        Parameters:
        other - the set of values to check for.
        Returns:
        true if all the values are present in the set, false otherwise
      • fromArray

        public void fromArray​(int[] fromData)
        Sets the integers in this IntSet to be those in the given array.
        Parameters:
        fromData - array of ints
      • fromArray

        public void fromArray​(int[] fromData,
                              boolean sorted)
        Sets the integers in this IntSet to be those in the given array.
        Parameters:
        fromData - array of ints
        sorted - whether the ints in array fromData are in sorted sequence
      • clear

        public void clear()
        Sets this IntSet to represent the empty set.
      • toArray

        public int[] toArray()
        Return an array containing only the set data (sorted).
        Returns:
        a sorted array containing the set members
      • toString

        public String toString()
        Returns a textual representation of this IntSet. The textual representation is of the form [i_1, ..., i_n].
        Overrides:
        toString in class Object
        Returns:
        String
      • appendToStringBuffer

        public StringBuffer appendToStringBuffer​(StringBuffer sb)
        Appends a textual representation of this IntSet to the given StringBuffer. The textual representation is of the form [i_1, ..., i_n].
        Parameters:
        sb - StringBuffer
        Returns:
        the given StringBuffer
      • intersection

        public static IntSet intersection​(IntSet setA,
                                          IntSet setB)
        Return a new set that is the intersection of two other sets. O(n+m).
        Parameters:
        setA - the first set to intersect; null means an empty set
        setB - the second set to intersect; null means an empty set
        Returns:
        a new set (always non-null) containing those members in both setA and setB
      • union

        public static IntSet union​(IntSet setA,
                                   IntSet setB)
        Return a new set that is the union of two other sets. O(n+m).
        Parameters:
        setA - the first set to union; null means an empty set
        setB - the second set to union; null means an empty set
        Returns:
        a new set (always non-null) containing all members of setA and setB
      • complement

        public static IntSet complement​(IntSet superset,
                                        IntSet subset)
        Return a new set that is the complement of a subset within a superset. O(n+m).
        Parameters:
        superset - the superset to perform the complement within; null means the empty set
        subset - the subset to complement; null means the empty set
        Returns:
        a new set (always non-null) containing all members of superset that are not in subset
      • isSupersetOf

        public boolean isSupersetOf​(IntSet subset)
        Determines whether this IntSet is a superset of the given IntSet.
        Parameters:
        subset - the conjectured subset of this IntSet
      • isSubsetOf

        public boolean isSubsetOf​(IntSet superset)
        Determines whether this IntSet is a subset of the given IntSet.
        Parameters:
        superset - the conjectured superset of this IntSet
      • size

        public int size()
        Gets the size of this IntSet.
        Returns:
        int size
      • lowestMember

        public int lowestMember()
        Gets the smallest valued int from this IntSet.
        Returns:
        lowest int
        Throws:
        NoSuchElementException - if this IntSet is empty
      • get

        public int get​(int index)
                throws ArrayIndexOutOfBoundsException
        Get the value at a specified index. As the set is sorted, an index of 0 returns the smallest value in the set, an index of size() - 1 returns the highest value, and any index in between returns the corresponding value.
        Parameters:
        index - the index.
        Returns:
        the value at the index.
        Throws:
        ArrayIndexOutOfBoundsException - if index is less than zero or greater than or equal to the size of the set.