// A simple bitset class. // (c) 1998 McGraw-Hill package structure; public class BitSet { protected final int bitsPerInt = 32; protected final int initialCapacity = 256; protected int data[]; protected int allocated; public BitSet() // post: constructs an empty set of small integers { clear(initialCapacity); } public BitSet(int count) // post: constructs an empty set with count potential elements { clear(count); } public void add(int i) // pre: i >= 0 // post: i is added to the set { extend(i); int index = indexOf(i); int offset = offsetOf(i); data[index] |= 1<= 0 // post: removes i from set if present { if (probe(i)) { int index = indexOf(i); int offset = offsetOf(i); data[index] &= ~(1<= 0 // post: returns true iff i in set { return probe(i) && (0 != (data[indexOf(i)] & (1<= 0 // post: returns index of integer containing bit b { return b/bitsPerInt; } protected int offsetOf(int bit) // pre: bit >= 0 // post: returns bit position of bit in word { return bit%bitsPerInt; } protected void extend(int bit) // pre: bit >= 0 // post: ensures set is large enough to contain bit { if (!probe(bit)) { int index = indexOf(bit); int newData[]; int newAllocated = allocated; int i; while (newAllocated <= index) newAllocated *= 2; newData = new int[newAllocated]; for (i = 0; i < allocated; i++) { newData[i] = data[i]; } for (i = allocated; i < newAllocated; i++) { newData[i] = 0; } data = newData; allocated = newAllocated; } } protected boolean probe(int bit) // pre: bit >= 0 // post: returns true if set is large enough to contain bit { int index = indexOf(bit); return data.length > index; } public String toString() // post: returns string representation of set { StringBuffer s = new StringBuffer(); int i; s.append(""); return s.toString(); } }