add comments
This commit is contained in:
1 file changed
+47
-3
@@ -1,6 +1,12 @@
|
||||
package ca.bcit.comp3760.lab03;
|
||||
|
||||
|
||||
/**
|
||||
* HashSimulator to test different hash methods
|
||||
* and their collisions, probing for hash tables.
|
||||
*
|
||||
* @author Braeden Sowinski - A01385066
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class HashSimulator
|
||||
{
|
||||
private static final int CHAR_OFFSET = 64;
|
||||
@@ -13,6 +19,14 @@ public class HashSimulator
|
||||
HASH3
|
||||
}
|
||||
|
||||
/**
|
||||
* H1 hashes a key for a table of a given size
|
||||
* by summing the characters and modding by the size.
|
||||
* Where characters A = 1, B = 2, C = 3, etc.
|
||||
* @param key to hash
|
||||
* @param size of the has table to insert
|
||||
* @return hash of key
|
||||
*/
|
||||
public int H1(final String key, final int size)
|
||||
{
|
||||
int sum;
|
||||
@@ -26,6 +40,16 @@ public class HashSimulator
|
||||
return sum % size;
|
||||
}
|
||||
|
||||
/**
|
||||
* H2 hashes a key for a table of a given size
|
||||
* by summing the character value multiplied by the number
|
||||
* of characters in the alphabet (NUM_CHAR) to the power of
|
||||
* the index of the given character, finally modding by the size.
|
||||
* Where characters A = 1, B = 2, C = 3, etc.
|
||||
* @param key to hash
|
||||
* @param size of the has table to insert
|
||||
* @return hash of key
|
||||
*/
|
||||
public int H2(final String key, final int size)
|
||||
{
|
||||
double sum;
|
||||
@@ -41,6 +65,16 @@ public class HashSimulator
|
||||
return (int) (sum % size);
|
||||
}
|
||||
|
||||
/**
|
||||
* H3 is the same as H2, where the difference is character
|
||||
* values are as seen in ASCII tables for both upper and
|
||||
* lower case characters. We then account for upper and
|
||||
* lower case in the power of character index by multiplying
|
||||
* NUM_CHAR (26) by 2.
|
||||
* @param key to hash
|
||||
* @param size of the has table to insert
|
||||
* @return hash of key
|
||||
*/
|
||||
public int H3(final String key, final int size)
|
||||
{
|
||||
double sum;
|
||||
@@ -49,13 +83,23 @@ public class HashSimulator
|
||||
for (int i = 0; i < key.length(); ++i)
|
||||
{
|
||||
final int charValue;
|
||||
charValue = key.toUpperCase().charAt(i);
|
||||
sum += charValue * Math.pow(NUM_CHAR, i);
|
||||
charValue = key.charAt(i); // use ASCII value instead
|
||||
sum += charValue * Math.pow(NUM_CHAR * 2, i); // lower and uppercase
|
||||
}
|
||||
|
||||
return (int) (sum % size);
|
||||
}
|
||||
|
||||
/**
|
||||
* runHashSimulation takes a list of keys to hash and the size
|
||||
* of a hash table to insert the keys into via the hash.
|
||||
* Simulates hashing each key using each hashing method H1, H2, & H3
|
||||
* then counts collisions and probing for each type of hash into a table
|
||||
* to compare.
|
||||
* @param keys used to testing hashing methods
|
||||
* @param size of testing hash table
|
||||
* @return collision + probe results for each hashing method
|
||||
*/
|
||||
public int[] runHashSimulation(final String[] keys, int size)
|
||||
{
|
||||
final int[] results;
|
||||
|
||||
Reference in new issue
Block a user