#ifndef HASHTABLE_H_ #define HASHTABLE_H_ 1 // Oldham, Jeffrey D. // 2000Apr18 // CS1321 // Modified by: // Massingill, Berna L. // 2001May // A Hash Table Implementation // We implement an open-chained hash table storing keys only, no // values. #include #include #include // has sqrt() #include // has pair() #include // has UCHAR_MAX // ADD YOUR CODE HERE (if needed; perhaps uncomment these lines) //#include // has find() //#include //#include class hashTable { public: typedef string keyType; // ---- Constructor ---- hashTable(void) { // ADD YOUR CODE HERE } // ---- Member and friend functions ---- // Query if the given key is in the hash table. // Post: returns true if there is an entry for "key" in the table, // false otherwise. bool query(const keyType & key) const { // REPLACE THE FOLLOWING LINE WITH YOUR CODE return false; } // Ensure the given key is in the hash table. // Post: "key" is in the table. If the table already // contained an entry with key "key", nothing changes. void insert(const keyType & key) { // ADD YOUR CODE HERE } // Ensure the given key is not in the hash table. // Post: there is no entry for "key" in the table. If the table // did not previously contain an entry for "key", // nothing changes. void remove(const keyType & key) { // ADD YOUR CODE HERE } // Print the hash table's contents. friend ostream& operator<<(ostream& out, const hashTable & h) { // ADD YOUR CODE HERE return out; } // Print some (internal) info about the hash table: // print table size, number of keys stored in table, // keys with hash codes. void printInfo(ostream & out) const { // ADD YOUR CODE HERE } private: // REPLACE THE FOLLOWING LINE WITH YOUR CODE // (sizePosition should be something suitable for the size of // your table -- e.g., vector::size_type typedef unsigned int sizePosition; // ADD YOUR CODE HERE (if needed) // ---- Member variables ---- // ADD YOUR CODE HERE // ---- "Helper" functions ---- // Convert a key into a vector position. // Pre: tableSize is tableSize to use (if 0, uses current size). // Post: returns a position in the range 0 .. tableSize - 1. sizePosition hash(const keyType & key, sizePosition tableSize = 0) const { if (tableSize == 0) // REPLACE THE FOLLOWING LINE WITH YOUR CODE tableSize = 1; double goldenRatio = (sqrt(5) - 1.0) / 2.0; #ifdef DEBUG sizePosition s = (sizePosition) (floor(tableSize * stringToFraction(key.begin(), key.end(), goldenRatio))); cout << "hash(" << key << ") yielded " << s << ".\n"; return s; #else return (sizePosition) (floor(tableSize * stringToFraction(key.begin(), key.end(), goldenRatio))); #endif // DEBUG } // Return the fractional portion of a double. static double fraction(const double d) { assert(d >= 0.0); double answer = d - floor(d); assert(answer < 1.0); return answer; } // Compute fractional portion of the product of the base-256 value // of the (backwards) string and an irrational number. static double stringToFraction(string::const_iterator pos, string::const_iterator ending, const double irrational) { if (pos == ending) return 0.0; else return fraction(fraction(static_cast(*pos) * irrational) + (UCHAR_MAX+1) * stringToFraction(++pos, ending, irrational)); } // ADD YOUR CODE HERE (if needed) }; #endif // HASHTABLE_H_