Class AbstractIntSet

java.lang.Object
it.unimi.di.prog2.h18.AbstractIntSet
All Implemented Interfaces:
Iterable<Integer>
Direct Known Subclasses:
IntSet, OrderedIntSet

public abstract class AbstractIntSet extends Object implements Iterable<Integer>
An AbstractIntSet is a mutable, unbounded set of integers.

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

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected int
    The elements of the set.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Creates an empty set.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Returns an element from this set.
    boolean
     
    int
     
    abstract void
    insert(int x)
    Adds the given element to this set.
    boolean
    isIn(int x)
    Tells if the given element is in this set.
    abstract void
    remove(int x)
    Removes the given element from this set.
    int
    Returns the cardinality of this set.
     

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface java.lang.Iterable

    forEach, iterator, spliterator
  • Field Details

    • size

      protected int size
      The elements of the set.
  • Constructor Details

    • AbstractIntSet

      protected AbstractIntSet()
      Creates an empty set.
  • Method Details

    • insert

      public abstract 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 abstract 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 NoSuchElementException
      Returns an element from this set.
      Returns:
      an arbitrary element from this set.
      Throws:
      NoSuchElementException - 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