Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

please attach little comments. I do want to understand how it works. thank you t

ID: 3817560 • Letter: P

Question

please attach little comments. I do want to understand how it works.

thank you

template <typename KEYTYPE, typename VALTYPE>
class Hashtable
{
   private:
       // main hash table table
       //vector< type > _hash;

       /**
       * Rehash the table into a larger table when the load factor is too large
       */
       void rehash() {

       }

       /**
       * Function that takes the key (a string or int) and returns the hash key
       * This function needs to be implemented for several types it could be used with!
       */
       int hash_function(int key) {
           cout << " Hashing with int type keys." << endl;
       }

       int hash_function(string key) {
           cout << " Hashing with string type keys." << endl;
       }

      
   public:
       /**
       * Basic constructor
       */
       Hashtable( int startingSize = 101 )
       {

       }

       /**
       * Add an element to the hash table
       */
       bool insert(KEYTYPE key, VALTYPE val) {
           // Currently unimplemented
       }

       /**
       * Return whether a given key is present in the hash table
       */
       bool contains(KEYTYPE key) {
           return false;
       }


       /**
       * Completely remove key from hash table
       * Returns number of elements removed
       */
       int remove(KEYTYPE key) {
           // Doesn't actually remove anything yet
       }

       /**
       * Searches the hash and returns a pointer
       * Pointer to Word if found, or nullptr if nothing matches
       */
       VALTYPE* find(KEYTYPE key) {
           return nullptr;
       }

       /**
       * Return current number of elements in hash table
       */
       int size() {
           return(-1);
       }

       /**
       * Return true if hash table is empty, false otherwise
       */
       bool empty()
       {
      
               return(false);
       }

       /**
       * Calculates the current load factor for the hash
       */
       float load_factor() {
           //return _hash.load_factor();
           return (-1.0);
       }

       /**
       * Returns current number of buckets (elements in vector)
       */
       int bucket_count() {
           return (-1);
       }

       /**
       * Deletes all elements in the hash
       */
       void clear() {
           // Does nothing yet
       }

};

Explanation / Answer

Here a template class of the element so that I can use various variable types for the hash map. This isthe code for the element.

our hash function should be a template function on the key type, implemented outside of your container class. You can then specialize the template function for each key type you're actually using the hash map with. This turns the type check from run-time to compile-time, making it both faster and safer.

Inside your container implementation you can then use the generic hash function to generate a hash value for your key.