Class IdHashKey
java.lang.Object
org.troy.capstone.data_structures.item_table.IdHashKey
- All Implemented Interfaces:
Cloneable
Class representing a hash key for item IDs, using universal hashing to minimize collisions in the item table. The hash code is computed by collapsing the string ID into an integer using a
Rabin-Karp style polynomial rolling hash method, and then applying a universal hash function with random coefficients I and J and a prime modulus P. This approach helps ensure a good distribution of hash values even for similar string IDs, which is important for maintaining efficient lookups in the item table. Implements Cloneable to allow for deep copy of IdHashKey instances.-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static BigIntegerA random coefficient for the universal hash function, chosen uniformly from 1 to P-1.private static BigIntegerA random coefficient for the universal hash function, chosen uniformly from 0 to P-1.private static final BigIntegerA prime number larger than the maximum possible hash value from collapsing the strings, to ensure good distribution in universal hashing.private static final int961 entries, 0.75 load factor -> 2048 table size 2048 is the lowest power of 2 above 961/0.75.private final StringThe original string value of the item ID, stored for equality checks and potential debugging purposes. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionclone()Returns a string representation of theIdHashKey, including the original string value and the computed hash code.private BigIntegerRabin-Karp style string to int collapse, using polynomial rolling hash method.booleanChecks if thisIdHashKeyis equal to another object.(package private) static BigIntegergetI()Getter for the random coefficient I used in the universal hash function.(package private) static BigIntegergetJ()Getter for the random coefficient J used in the universal hash function.(package private) static BigIntegergetP()Getter for the prime modulus P used in the universal hash function.getValue()Getter for the original string value of the item ID.inthashCode()Using universal hashing code from textbook, with small alterations.
-
Field Details
-
value
The original string value of the item ID, stored for equality checks and potential debugging purposes. -
P
A prime number larger than the maximum possible hash value from collapsing the strings, to ensure good distribution in universal hashing. -
I
A random coefficient for the universal hash function, chosen uniformly from 1 to P-1. Used as the multiplier in the universal hash formula. Not final to allow changing in testing. -
J
A random coefficient for the universal hash function, chosen uniformly from 0 to P-1. Used as the additive constant in the universal hash formula. Not final to allow changing in testing. -
TABLE_SIZE
private static final int TABLE_SIZE961 entries, 0.75 load factor -> 2048 table size 2048 is the lowest power of 2 above 961/0.75. The table size is chosen to be a power of 2 for efficient modulo operations.- See Also:
-
-
Constructor Details
-
IdHashKey
Constructor for the IdHashKey class.- Parameters:
value- The original string value of the item ID to be hashed.
-
-
Method Details
-
getValue
Getter for the original string value of the item ID.- Returns:
- The original string value of the item ID.
-
getI
Getter for the random coefficient I used in the universal hash function.- Returns:
- The random coefficient I.
-
getJ
Getter for the random coefficient J used in the universal hash function.- Returns:
- The random coefficient J.
-
getP
Getter for the prime modulus P used in the universal hash function.- Returns:
- The prime modulus P.
-
hashCode
-
collapseStringToInt
Rabin-Karp style string to int collapse, using polynomial rolling hash method. Algorithm source is [3].- Parameters:
str- The string to collapse into an integer hash value.- Returns:
- A hash value representing the input string collapsed via Rabin-Karp rolling hash.
- Preconditions:
- str is not null and only contains ASCII characters from '!' to '~' (printable characters excluding space and delete).
-
equals
-
clone
Returns a string representation of theIdHashKey, including the original string value and the computed hash code.- Overrides:
clonein classObject- Returns:
- A string representation of the
IdHashKey. - Throws:
CloneNotSupportedException
-