package TheoryLabs;
import java.util.*;
/**
* Finite set of integers, used to represent sets of states in finite
* automata. The elements of each IntSet are maintained in ascending order.
*
*
Data Fields: The set of integers is stored in a sorted Vector
* of Integers.
*
*
* IntSet has two special methods to support conversion from NFA to DFA,
* based on binary arithmetic.
* 1. intValue() returns a uniquely-determined integer corresponding
* to this IntSet (assuming all elements are >= 0).
* Example: intValue of [1,3,4] = 2^1 + 2^3 + 2^4 = 33.
* 2. setValue(int n) takes a non-negative integer n and
* returns a uniquely-determined Intset corresponding
* to n.
* Example: setValue of 17 is [0,4] because 17 = 2^0 + 2^4.
*
* @author Barbara Wahl
* @version Fall 2007
*/
public class IntSet {
// ** DATA FIELDS **
/**
* stores the int values as Integers
*/
protected Vector data;
// ** CONSTRUCTORS **
/**
* 0-parameter constructor.
* @post constructs an empty IntSet
*/
public IntSet()
{
data = new Vector();
}
/**
* 1-parameter constructor. Creates a set with one element.
* @param n the element to be included in this IntSet
* @post constructs a new IntSet with one element, n
*/
public IntSet(int n)
{
this();
data.add(new Integer(n));
}
/**
* Copy constructor.
* @param current the Intset to be copied
* @pre current is not null
* @post constructs a new intSet via deep copy of current
*/
public IntSet(IntSet current)
{
// deep copy of Vector current.data
data = new Vector();
for(int i=0; i n
current = (Integer)data.elementAt(pos);
if(current.intValue() > n) break;
}
this.data.insertElementAt(new Integer(n),pos);
}
/**
* Removes a given integer from this IntSet.
* @param n integer to be removed
* @post if found, n is removed from this IntSet
*/
public void remove(int n)
{
data.remove(new Integer(n));
}
/**
* Adds all the elements of "other" to this IntSet, forming
* the union.
* @param other set to be unioned with this IntSet
* @return set union of "this" and "other" (no repetitions,
* ascending order)
* @pre other is not null
* @post this IntSet is changed (unless other is a subset of this)
*/
public IntSet union(IntSet other)
{
// add elements of "other" to "this" & return
for(int i=0; i other.size())
return false;
// return false if find element of this not in other
for(int i=0; i= 0,"this IntSet has no negative values");
for(int i = 0; i= 0 (bijection from W to finite subsets of W).
* @param i the integer to be converted to an IntSet
* @return IntSet determined by expressing i in binary notation;
* if the bit in the 2^j place is non-zero then j is placed in the
* IntSet
*/
public static IntSet setValue(int i)
// For example, if i = 13 then i = 1101 = 2^0 + 2^2 + 2^3, so
// the corresponding IntSet is [0,2,3].
{
// FIRST, find the binary digits to represent i in base 2;
// 1+(log-base-two of i+2) is used to determine
// a sufficient number of digits for i in base 2
int numDigits = 1+ Util.intLogTwo(i+2);
int rem = i; // remainder to be processed
int[] a = new int[numDigits]; // array for the digits, initially all zero
for(int j=0; j