Class ItemHashMap
Custom
HashMap implementation for storing Item instances with their IDs as keys, using a custom universal hash function (IdHashKey) to optimize bucket distribution for the specific set of item IDs in our dataset.- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K, V> -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final floatMaximum load factor for theItemHashMap.private static final intTable size for theItemHashMap, chosen to be a power of 2 for efficient modulo operations, and large enough to maintain a load factor of 0.75 for our expected number of items (961). -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateItemHashMap(int data_size) Make initial capacity so that we have just over a 0.75 load factor. -
Method Summary
Modifier and TypeMethodDescriptionprivate voidaddAllItems(Table table) Adds all rows from a tablesaw Table to the hashmap as Items.private voidAdds an item to the hashmap given a tablesaw Row.copy()Creates a deep copy of theItemHashMap.static ItemHashMapCreates anItemHashMapfrom aTable.int[]getFreshBucketSizeCount(List<String> itemIds, boolean useCustomHash) Calculate bucket distribution with current I and J values (fresh hash calculation).Retrieves an item from theItemHashMapgiven its ID.Retrieves the item IDs of theItemHashMapas a list of strings.Retrieves the items of theItemHashMapas a list ofItemobjects.Retrieves the keys of theItemHashMapas a list ofIdHashKeyobjects.intgetSize()Retrieves the number of items in the repository.private voidPrints a table comparing the distribution of bucket sizes (number of buckets with 0 items, 1 item, 2 items, etc.) for the custom hash function vs Java's built inStringhashCode, using the same item IDs.Methods inherited from class HashMap
clear, clone, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, forEach, get, getOrDefault, isEmpty, keySet, merge, newHashMap, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size, valuesMethods inherited from class AbstractMap
equals, hashCode, toString
-
Field Details
-
MAX_LOAD_FACTOR
private static final float MAX_LOAD_FACTORMaximum load factor for theItemHashMap.- See Also:
-
TABLE_SIZE
private static final int TABLE_SIZETable size for theItemHashMap, chosen to be a power of 2 for efficient modulo operations, and large enough to maintain a load factor of 0.75 for our expected number of items (961).- See Also:
-
-
Constructor Details
-
ItemHashMap
private ItemHashMap(int data_size) Make initial capacity so that we have just over a 0.75 load factor.- Parameters:
data_size- The number ofIteminstances that will be added to the map, used to get a 0.75 load factor.- Preconditions:
- data_size is a positive integer.
-
-
Method Details
-
fromTable
Creates anItemHashMapfrom aTable. The map is initialized with the optimal hash parameters for the current item IDs.- Parameters:
table- Atablesaw Tablecontaining the item data, with each row representing an item.- Returns:
- itemMap An
ItemHashMapcontaining all items from the table, with hash parameters optimized for the item IDs in the table. - Preconditions:
- table is not null and contains the expected columns for creating
Iteminstances (ID, Name, etc.), and contains no duplicate IDs.
-
addItem
Adds an item to the hashmap given a tablesaw Row.- Parameters:
itemRow- A Row from atablesaw Tablecontaining item info.- Preconditions:
itemRowis not null and contains the expected columns for creating anIteminstance (ID, Name, etc.).
-
addAllItems
Adds all rows from a tablesaw Table to the hashmap as Items.- Parameters:
table- Atablesaw Tablewith each row being an item to add to the map.- Preconditions:
tableis not null and contains the expected columns for creatingIteminstances (ID, Name, etc.), and contains no duplicate IDs.
-
getItem
Retrieves an item from theItemHashMapgiven its ID. Prints a message if the item is not found in the map. -
getFreshBucketSizeCount
Calculate bucket distribution with current I and J values (fresh hash calculation).- Parameters:
itemIds- A list of item IDs to calculate the bucket distribution for.useCustomHash- Whether to use the customIdHashKeyhash function or the built-inStringhashCode.- Returns:
bucketSizeCountsAn array where the value at index N is the number of buckets that have N items in them, according to the specified hash function.- Preconditions:
itemIdsis not null and contains valid item IDs.
-
getKeysAsList
-
getItemIdsAsList
-
getItemsAsList
Retrieves the items of theItemHashMapas a list ofItemobjects.- Specified by:
getItemsAsListin interfaceItemRepo- Returns:
- A list of
Itemobjects representing the values in theItemHashMap.
-
printBucketSizeCountsCustomVsBuiltIn
private void printBucketSizeCountsCustomVsBuiltIn()Prints a table comparing the distribution of bucket sizes (number of buckets with 0 items, 1 item, 2 items, etc.) for the custom hash function vs Java's built inStringhashCode, using the same item IDs. This allows us to see how well our custom universal hash function is performing in terms of distributing items across buckets compared to the built-in hash function.- Preconditions:
findBestHashParameters()has already been called to optimize I and J for the current item IDs, the internal state of theItemHashMapis not modified between the two distribution calculations (i.e. no items are added or removed), the same item IDs are used for both calculations, and the map must be filled.
-
copy
Creates a deep copy of theItemHashMap.- Returns:
- A new
ItemHashMapcontaining the same entries as the original. - Throws:
CloneNotSupportedException- if any of the keys or values in the map do not support cloning.
-
getSize
-