Set handling in Java and C#

The following is the specification of a Java set handling class useful in developing translators. A closely equivalent C# version appears lower on this page.

These classes - named IntSet - are functionally equivalent to the ones described in the text book, where they were named SymSet. In Java 1.4 one could not have "varargs" parameters, which are useful here. The SymSet classes are still available, but it is recommended that students replace references in pracs to the SymSet class with references to the IntSet class, and then both of the Java or C# versions behave in the same way, permitting constructor calls like

    IntSet mySet = new IntSet(3, 67, 34);               // Java 1.5 et seq       and   C# 1.0 et seq
rather than the clumsier
    SymSet mySet = new SymSet(new int[] {3, 67, 34} );  // Java 1.4
  import library.*;

  class IntSet
  // Simple set class
  // P.D. Terry (p.terry@ru.ac.za)

    public IntSet()
    // Empty set constructor

    public IntSet(int ... members)
    // Variable args constructor  IntSet(a)  IntSet(a,b) etc

    public Object clone()
    // Value copy

    public IntSet copy() {
    // Another value copy

    public boolean equals(IntSet s)
    // Value comparison

    public void incl(int i)
    // Includes i in set

    public void excl(int i)
    // Excludes i from set

    public boolean contains(int i)
    // Returns true if i is a member of set

    public boolean isEmpty()
    // Returns true if set is empty

    public int members()
    // Returns number of members in set

    public IntSet union(IntSet s)
    // Set union

    public IntSet intersection(IntSet s)
    // Set intersection

    public IntSet difference(IntSet s)
    // Set difference

    public IntSet symDiff(IntSet s)
    // Set symmetric difference

    public void write()
    // Simple display of set on StdOut

    public String toString()
    // Returns string representation of set

  } // IntSet


The following is the specification of a C# set handling class useful in developing translators. An closely equivalent Java version appears earlier on this page.

  using Library;

  class IntSet
  // Simple set class
  // P.D. Terry (p.terry@ru.ac.za)

    public IntSet()
    // Empty set constructor

    public IntSet(params int[] members)
    // Variable args constructor  IntSet(a)  IntSet(a,b) etc

    public IntSet Clone()
    // Value copy

    public bool Equals(IntSet s)
    // Value comparison of this set with s

    public void Incl(int i)
    // Includes i in this set

    public void Excl(int i)
    // Excludes i from this set

    public bool Contains(int i)
    // Returns true if i is a member of this set

    public bool Contains(IntSet s) {
    // Returns true if s is a subset of this set

    public bool IsEmpty()
    // Returns true if this set is empty

    public bool IsFull() {
    // Returns true if this set is a universe set

    public void Fill()
    // Creates a full universe set for this set

    public void Clear() {
    // Clear this set

    public int Members()
    // Returns number of members in this set

    public IntSet Union(IntSet s)
    // Set union of this set with s

    public IntSet Difference(IntSet s)
    // Set difference of this set with s

    public IntSet Intersection(IntSet s)
    // Set intersection of this set with s

    public IntSet SymDiff(IntSet s)
    // Set symmetric difference of this set with s

    public override string ToString()
    // Returns string representation of this set as a set of ints

    public string ToCharSetString() {
    // Returns string representation of this set as a set of chars

  } // IntSet


Earlier versions as used in the text book (but avoid these!)

The following is the specification of a Java 1.4 set handling class useful in developing translators. A closely equivalent C# 1.0 version appears lower on this page.

  import library.*;

  class SymSet
  // Simple set class
  // P.D. Terry (p.terry@ru.ac.za)

    public SymSet()
    // Empty set constructor

    public SymSet(int[] members)
    // Reasonable approximation to variable args constructor
    // Usage - SymSet(new int[] {1})  SymSet(new int[] {a, b, c}) etc

    public Object clone()
    // Value copy

    public SymSet copy() {
    // Another value copy

    public boolean equals(Symset s)
    // Value comparison

    public void incl(int i)
    // Includes i in set

    public void excl(int i)
    // Excludes i from set

    public boolean contains(int i)
    // Returns true if i is a member of set

    public boolean isEmpty()
    // Returns true if set is empty

    public int members()
    // Returns number of members in set

    public SymSet union(SymSet s)
    // Set union

    public SymSet intersection(SymSet s)
    // Set intersection

    public SymSet difference(SymSet s)
    // Set difference

    public SymSet symDiff(SymSet s)
    // Set symmetric difference

    public void write()
    // Simple display of set on StdOut

    public String toString()
    // Returns string representation of set

  } // SymSet


The following is the specification of a C# set handling class useful in developing translators. An closely equivalent Java version appears earlier on this page.

  using Library;

  class SymSet
  // Simple set class
  // P.D. Terry (p.terry@ru.ac.za)

    public SymSet()
    // Empty set constructor

    public SymSet(params int[] members)
    // Variable args constructor  SymSet(a)  SymSet(a,b) etc

    public SymSet Clone()
    // Value copy

    public bool Equals(Symset s)
    // Value comparison

    public void Incl(int i)
    // Includes i in set

    public void Excl(int i)
    // Excludes i from set

    public bool Contains(int i)
    // Returns true if i is a member of set

    public bool IsEmpty()
    // Returns true if set is empty

    public void Fill()
    // Creates a full universe set

    public int Members()
    // Returns number of members in set

    public SymSet Union(SymSet s)
    // Set union

    public SymSet Intersection(SymSet s)
    // Set intersection

    public SymSet Difference(SymSet s)
    // Set difference

    public SymSet SymDiff(SymSet s)
    // Set symmetric difference

    public void Write()
    // Simple display of set on StdOut

    public override string ToString()
    // Returns string representation of set

  } // SymSet
Updated: 2012/08/22