/********************************************************************* Author: Dana Vrajitoru, IUSB, CS Class: C307 I308 File name: HashTable.java Last updated: March 2022 Description: Implementation of a hash table. **********************************************************************/ package hashTable; public class HashTable { // Default capacity if the table is created with the default constructor. static final int DEF_CAPACITY = 307; // A prime number ListWordC [] storage; int capacity; int numObjects; // Default constructor public HashTable() { numObjects = 0; init(DEF_CAPACITY); }// HashTable() // Constructor with given initial capacity. public HashTable(int size) { init(size); numObjects = 0; } // HashTable() // Delete all the objects without deleting the storage. public void clear() { for (int i = 0; i < capacity; i++) storage[i].clear(); // delete all the nodes in the list numObjects = 0; } // clear() // Initialize the storage with a given size public void init(int size) { capacity = size; storage = new ListWordC[capacity]; for (int i = 0; i < capacity; i++) storage[i] = new ListWordC(); } // init() // Checks if the access to a given position is safe in the // storage. This means if the position is smaller than the capacity // and non negative. private boolean isValid(int position) { return (position >= 0 && position < capacity); } // isValid() // Prints all the objects in the table. public void print() { int i; System.out.println("The table has " + numObjects + " objects."); if (numObjects > 0) { System.out.println("Here are the objects:"); for (i = 0; i < capacity; i++) { if (!storage[i].isEmpty()) System.out.println("At position: " + i); for (WordCounter wordc : storage[i].list) { System.out.println("\t" + wordc.toString()); } } } } // print() // Resizes the table and rehashes it, moving each stored element to // the new index corresponding to the resized hash function. public void resize(int newSize) { int i, newIndex; ListWordC save = new ListWordC(); if (capacity == newSize) return; // save the elements from the old storage to the save list for (i = 0; i < capacity; i++) { // splice concatenates storage[i] to save at the end // and deletes the content of storage[i] for (WordCounter wdc : storage[i].list) { save.insert(wdc); } } clear(); init(newSize); // now rehash everything for (WordCounter wdc : save.list) { newIndex = hashing(wdc.key()); // based on new size storage[newIndex].insert(wdc); } save.clear(); // delete the saved list } // resize() // The most important function of the class. This function takes a // string (a key) and determines the index in the storage array where // an object with that key should go. // // This should use the actual capacity of the storage array and not // the default value for this capacity. private int hashing(String k) { if (k.length() > 0) // Statements included for compilation. return (int)(k.charAt(0)) % capacity; else return 0; // Code to be modified by the student. } // hashing() // Public functions to be implemented by the student. // The function checks if an object with the key of wordc exists // in the table already, and returns false if so without changing // the table. // If the key has not been used in the table before, it inserts // wordc in the table and returns true. public boolean insert(WordCounter wordc) { int position = hashing(wordc.key()); if (!isValid(position)) { System.err.println("Invalid position in insert: " + position); return false; } else { // we don't want to modify wordc itself in case the word is already there WordCounter copyWord = new WordCounter(); if (storage[position].findWord(copyWord, wordc.key())) { return false; // the key is already there } else { storage[position].insert(new WordCounter(wordc)); numObjects++; return true; } } } // insert() // This function should first get the hash index for the string, // then call an appropriate function from the list at this index. // // If the an object containing the key k exists in the table, the // function returns true and a copy of the object in the parameter // wordc. If such an object does not exist, the function returns // false and wordc is not changed. // // wordc is an output parameter: the object found containing k // must be copied into it. public boolean access(WordCounter wordc, String k) { return true; // Statement included for compilation. // Code to be provided by the student. } // access() // This function should first get the hash index for the string k, // then call the private remove function with this index. // // The function returns true if an object containing the key k can // be found in the table and is removed from it, and false if not. // // wordc is an output parameter: the removed object must be copied // into it. public boolean remove(WordCounter wordc, String k) { return true; // Statement included for compilation. // Code to be provided by the student. } // remove() // This function should print a statistic of usage of each index in // the table in terms of number of objects stored at each index, for // those indexes where the list is not empty. It should not output // individual elements. // Note: you should only need one loop in this function, and no // iterator. Use the method size() for each element of the vector to // print the number of nodes in that list. public void statistic() { // Code to be provided by the student. } // statistic() }