first commit
This commit is contained in:
337
CS1322/p3/HashTable.java
Normal file
337
CS1322/p3/HashTable.java
Normal file
@@ -0,0 +1,337 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* HashTable.java
|
||||
*
|
||||
* Revisions: 1.0 Oct. 06, 2002
|
||||
* Created the HashTable class
|
||||
* 1.1 Oct. 10, 2002
|
||||
* Compiled, finished, Commented
|
||||
*
|
||||
* </PRE>
|
||||
*
|
||||
* Collaboration Statement:
|
||||
* I worked on the homework assignment alone, using only
|
||||
* course materials.
|
||||
*
|
||||
* Created with JCreatorLE, some indents are off when viewed through notepad
|
||||
* or EMACS
|
||||
*
|
||||
* @author <A HREF="mailto:gtg184g@mail.gatech.edu">Jose Manuel Caban</A>
|
||||
* @version Version 1.0, Oct. 06, 2002
|
||||
*/
|
||||
|
||||
typedef struct HashNode
|
||||
{
|
||||
HashNode* next;
|
||||
int key;
|
||||
int data;
|
||||
};
|
||||
|
||||
HashNode[] hTable;
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
|
||||
public HashTable(int size)
|
||||
{
|
||||
hTable = new HashNode[size];
|
||||
}
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
/**
|
||||
*add data to a given location
|
||||
*@param data added to location key
|
||||
*@param key, the location to store data
|
||||
*/
|
||||
public void add(Object key, Object data)
|
||||
{
|
||||
int iKey = Math.abs(key.hashCode()) % hTable.length;
|
||||
|
||||
if(hTable[iKey] == null)
|
||||
{
|
||||
hTable[iKey] = new HashNode(key,data);
|
||||
}
|
||||
else
|
||||
{
|
||||
HashNode temp = new HashNode(key,data);
|
||||
temp.setNext(hTable[iKey]);
|
||||
hTable[iKey] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*@return the data located
|
||||
*@param key
|
||||
*/
|
||||
public Object get(Object key)
|
||||
{
|
||||
int iKey = Math.abs(key.hashCode()) % hTable.length;
|
||||
|
||||
if(hTable[iKey] == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
HashNode current = hTable[iKey];
|
||||
while(current != null)
|
||||
{
|
||||
if(current.getKey().equals(key))
|
||||
{
|
||||
return current.getData();
|
||||
}
|
||||
current = current.getNext();
|
||||
}
|
||||
return null;
|
||||
}//end get(@key)
|
||||
|
||||
/**
|
||||
*Remove hashNode at key and return data
|
||||
*@param key, the node to be removed
|
||||
*/
|
||||
public Object remove(Object key)
|
||||
{
|
||||
int iKey = Math.abs(key.hashCode()) % hTable.length;
|
||||
|
||||
if(hTable[iKey] == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
HashNode current = hTable[iKey];
|
||||
if(current.getKey().equals(key))
|
||||
{
|
||||
hTable[iKey] = current.getNext();
|
||||
return current.getData();
|
||||
}
|
||||
|
||||
else
|
||||
{ //if(the list is long and annoying)
|
||||
//set predefs
|
||||
HashNode hPrev = current; //a little longer and its hPrevInstance
|
||||
current = current.getNext();
|
||||
|
||||
while(current != null)
|
||||
{
|
||||
if(current.getKey().equals(key))
|
||||
{
|
||||
hPrev.setNext(current.getNext());
|
||||
return current.getData();
|
||||
}
|
||||
current = current.getNext();
|
||||
hPrev = hPrev.getNext();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}//end remove(@key)
|
||||
|
||||
/**
|
||||
*@return enumeration of of HashTable
|
||||
*/
|
||||
public Enumeration elements()
|
||||
{
|
||||
Vector vTemp = new Vector();
|
||||
HashNode hCurr;
|
||||
|
||||
for(int i=0; i<hTable.length; i++)
|
||||
{
|
||||
hCurr = hTable[i];
|
||||
while(hCurr != null)
|
||||
{
|
||||
vTemp.add(hCurr.getData());
|
||||
hCurr = hCurr.getNext();
|
||||
}
|
||||
}
|
||||
return vTemp.elements();
|
||||
}
|
||||
|
||||
/**
|
||||
*Rehash Hashtable to iSize
|
||||
*@param iSize, size to be resized to
|
||||
*/
|
||||
public void rehash(int iSize)
|
||||
{
|
||||
//by the way, I like the fact we're using Hungarian notation now;
|
||||
//Microsoft is more useful than the LUG@GATECH would like to think :)
|
||||
//did I already say this in Zoo.java?
|
||||
|
||||
//don't waste CPU cycles
|
||||
if(hTable.length == 0)
|
||||
{
|
||||
hTable = new HashNode[iSize];
|
||||
return;
|
||||
}
|
||||
|
||||
HashNode[] hTemp = new HashNode[hTable.length];
|
||||
|
||||
for(int i=0; i<hTable.length; i++)
|
||||
{
|
||||
hTemp[i] = hTable[i];
|
||||
}
|
||||
|
||||
//define cars needed
|
||||
hTable = new HashNode[iSize];
|
||||
|
||||
HashNode hCurr;
|
||||
|
||||
for(int i=0; i<hTemp.length; i++)
|
||||
{
|
||||
hCurr = hTemp[i];
|
||||
while(hCurr != null)
|
||||
{
|
||||
add(hCurr.getKey(),hCurr.getData());
|
||||
hCurr = hCurr.getNext();
|
||||
}
|
||||
}
|
||||
}//end rehash(int)
|
||||
|
||||
/**
|
||||
*@return size of the hashtable
|
||||
*/
|
||||
public int size()
|
||||
{
|
||||
return hTable.length;
|
||||
}
|
||||
|
||||
/**
|
||||
*@return String version of Hashtable
|
||||
*/
|
||||
public String toString()
|
||||
{
|
||||
String reallyLong = "";
|
||||
HashNode hCurr;
|
||||
|
||||
for(int i=0; i<hTable.length; i++)
|
||||
{
|
||||
hCurr = hTable[i];
|
||||
while(hCurr != null)
|
||||
{
|
||||
reallyLong += hCurr.getData().toString() + "\n";
|
||||
hCurr = hCurr.getNext();
|
||||
}
|
||||
|
||||
}
|
||||
return reallyLong;
|
||||
}
|
||||
|
||||
/**
|
||||
*Adds x elements to the HashTable
|
||||
*!!!Meant for Debugging only!!!
|
||||
*/
|
||||
public void addRandom(int iHowMany)
|
||||
{
|
||||
|
||||
//you know this was fairly creative
|
||||
//I believe this deserves extra credit
|
||||
//or at least send me money
|
||||
for(int i=0; i<iHowMany; i++)
|
||||
{
|
||||
|
||||
//this will force fast systems like mine to have
|
||||
//a different value near every iteration
|
||||
//hell, handling a 3D matrix in Java even caused some delay
|
||||
//on my dual mp2100+, what an inefficient VM sun makes :(
|
||||
//I was going to multithread this, one thread adding 2 starting at
|
||||
//zero, the other adding 2 starting at 1, but then I got lazy...
|
||||
int[][][] hehehe = new int[100][100][100];
|
||||
for(int x=0; x<100; x++)
|
||||
{
|
||||
for(int y=0; y<100; y++)
|
||||
{
|
||||
for(int z=0; z<100; z++)
|
||||
{
|
||||
hehehe[x][y][z] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
hehehe = new int[1][1][1];
|
||||
//end
|
||||
|
||||
switch((int)(System.currentTimeMillis() % 10))
|
||||
{
|
||||
case 0:
|
||||
add(new Integer((int)(System.currentTimeMillis()%10)),
|
||||
new Integer(6));
|
||||
break;
|
||||
case 1:
|
||||
add(new Integer((int)(System.currentTimeMillis()%10)),
|
||||
new Mail("HAHA","HA","Joe"));
|
||||
break;
|
||||
case 2:
|
||||
add(new String("Fido"),new Integer(2));
|
||||
break;
|
||||
case 3:
|
||||
add(new String("Alfonso"),new Integer(45));
|
||||
break;
|
||||
case 4:
|
||||
add(new Double(3.14),new Float(3.14));
|
||||
break;
|
||||
case 5:
|
||||
add(new Integer(43),new Float(6));
|
||||
break;
|
||||
case 6:
|
||||
add(new String("This"),new String("is"));
|
||||
break;
|
||||
case 7:
|
||||
add(new String("getting"),new String("way"));
|
||||
break;
|
||||
case 8:
|
||||
add(new String("too"),new String("long"));
|
||||
break;
|
||||
case 9:
|
||||
add(new String("ha"),new String("laziness"));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}//end addRandom(int)
|
||||
|
||||
/****************************************************************/
|
||||
|
||||
/**
|
||||
* Debugging main for class HashTable.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args)
|
||||
{
|
||||
HashTable hashey = new HashTable(5);
|
||||
|
||||
//test add
|
||||
hashey.addRandom(15);
|
||||
System.out.println(hashey);
|
||||
|
||||
hashey = new HashTable(5);
|
||||
hashey.addRandom(12);
|
||||
|
||||
System.out.println("Testing elements");
|
||||
Enumeration e = hashey.elements();
|
||||
while(e.hasMoreElements())
|
||||
{
|
||||
System.out.println(e.nextElement());
|
||||
}
|
||||
|
||||
|
||||
|
||||
//hehehehehehhehehe, lets test this sucker for REAL
|
||||
//don't try this at home :)
|
||||
//approx. runtime on a Tiger MPX dual MP2100+ 1.5GB RAM
|
||||
//thats 100% CPU utilaztion at 1.733GHz IPC3
|
||||
//around 60 seconds (im guessing)
|
||||
//:)
|
||||
hashey = new HashTable(200);
|
||||
hashey.addRandom(2000);
|
||||
|
||||
System.out.println(hashey);
|
||||
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class HashTable
|
||||
Reference in New Issue
Block a user