package finiteAutomata; import java.util.Vector; /** * Data structure which allows for insertions, deletions, and finds in * constant average time. * *

This Hashtable class uses separate chaining to store Associations * (key-value pairs). Any non-null object can be used as the key (which * is used as input to the hashing function). To successfully store and * retrieve values from a hashtable, the objects used as keys must * implement the hashCode method and the equals method. To store an * Association, apply the hash function to its key and store the * Association in the resulting list (first check that an equivalent * Association is not already present). To retrieve an Association, * apply the hash function to its key and search for it in the resulting * list. If the hashtable is appropriately long and if the hash function * does a good job of distributing the keys evenly among the hashing * locations, then all the lists should be short and it will be very * quick to find, insert, and delete based on keys. * *

Data Fields: * A Hashtable is based on an array "table" of Vectors. The integer field * "tableSize" gives the length of the array, and should be a prime number * (to help insure even distribution for the hashing function). * The integer field "size" tells how many Associations are currently * stored; load factor can thus be calculated by size / tableSize. (Load * factor around 1.0 is ideal for efficient use of time and space in open * chaining.) * * @author Barbara Wahl * @version Fall 2007 */ public class Hashtable { // ** DATA FIELDS ** /** * Vector lists for storing Assocations */ protected Vector[] table; /** * length of the "table" array */ protected int tableSize; /** * number of Associations currently stored */ protected int size; // ** CONSTRUCTORS ** /** * 1-parameter constructor. * @param n table size * @pre n > 0 * @post creates an empty Hashtable with tableSize = n */ public Hashtable(int n) { Assert.pre(n > 0,"n is positive"); table = new Vector[n]; for(int i=0; i