Class IntSet

java.lang.Object
it.unimi.di.prog2.h14.IntSet
All Implemented Interfaces:
Iterable<Integer>
Direct Known Subclasses:
MaxIntSet

public class IntSet extends Object implements Iterable<Integer>
IntSets are mutable, unbounded sets of integers.

A typical IntSet is \( S = \{x_1, \ldots, x_n \} \).

  • Field Details

    • els

      private final List<Integer> els
      The List containing this set elements.
  • Constructor Details

    • IntSet

      public IntSet()
      Initializes this set to be empty.

      Builds the set \( S = \varnothing \).

    • IntSet

      public IntSet(IntSet other)
      A *copy constructor*.
      Parameters:
      other - the IntSet to copy from.
  • Method Details

    • repOk

      private boolean repOk()
    • getIndex

      private int getIndex(int x)
      Looks for a given element in this set.
      Parameters:
      x - the element to look for.
      Returns:
      the index where x appears in els if the element belongs to this set, or -1
    • insert

      public void insert(int x)
      Adds the given element to this set.

      This method modifies the object, that is: \( S' = S \cup \{ x \} \).

      Parameters:
      x - the element to be added.
    • remove

      public void remove(int x)
      Removes the given element from this set.

      This method modifies the object, that is: \( S' = S \setminus \{ x \} \).

      Parameters:
      x - the element to be removed.
    • isIn

      public boolean isIn(int x)
      Tells if the given element is in this set.

      Answers the question \( x\in S \).

      Parameters:
      x - the element to look for.
      Returns:
      whether the given element belongs to this set, or not.
    • size

      public int size()
      Returns the cardinality of this set.

      Responds with \( |S| \).

      Returns:
      the size of this set.
    • choose

      public int choose() throws EmptyException
      Returns an element from this set.
      Returns:
      an arbitrary element from this set.
      Throws:
      EmptyException - if this set is empty.
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • iterator

      public Iterator<Integer> iterator()
      Specified by:
      iterator in interface Iterable<Integer>