// CMSC 341 - Fall 2019 - Project 5 // hash.h // Templated, hash table implementation. Each buckets is a heap. A // bucket contains objects with the same key values stored as a // max-heap based on priority. Collisions are resolved by linear // probing. // To work with Heap and HashTable, the template class T must provide // the following: // (a) Default constructor // (b) priority function (unsigned priority()) // (c) key function (string key()) // (d) overloaded insertion operator #ifndef _HASH_H #define _HASH_H #include #include "heap.h" using std::string; // Hash function typedef. The hash function must return an 'unsigned // int' value. Reduction mod N is the responsiblity of the caller, // not the hash function. typedef unsigned (*hash_fn)(string); class Grader; template class HashTable { friend Grader; public: // Constructor. Requires table size and hash function. HashTable(unsigned size, hash_fn hash); // Destructor, copy, and assignment ~HashTable(); HashTable(const HashTable& ht); const HashTable& operator=(const HashTable& ht); // In-line functions // Table size; set by constructor unsigned tableSize() const { return _N; } // Number of entries in the table unsigned numEntries() const { return _n; } // Load factor float lambda() const { return ((float) _n) / _N; } // Main hash table functions // insert returns 'true' if successful; 'false' otherwise bool insert(const T& object); // getNext retrieves **and removes** the highest priority order of // type indicated by key. It returns 'true' if successful; 'false' // otherwise. bool getNext(string key, T& obj); // Dump the entire hash table void dump() const; private: unsigned _N; // hash table size unsigned _n; // current number of entries hash_fn _hash; // hash function Heap *_table; // array of heaps // *********************************************** // Private helper function declarations go here! * // *********************************************** }; // ***************************************** // Templated function definitions go here! * // ***************************************** #endif