// // Created by Clayton Cafiero on 1/25/21. // #include #include "LinearProbing.h" #include "DictionaryItem.h" /** * Horner hashing function * char is an integral type -- that is, it's value is stored as * an integer. (We're going to pretend Unicode and multibyte * encodings and all that stuff doesn't exist.) */ unsigned long hornerHash(std::string key, unsigned long tableSize) { unsigned long hashVal = 0; for (const char& c : key) { hashVal = hashVal * 37 + c; } return hashVal % tableSize; } std::string getKey(const DictionaryItem& d) { return d.getWord(); } int main() { const int TABLE_SIZE = 17; LinearProbing hashTable(TABLE_SIZE, getKey, hornerHash); DictionaryItem d1("dentist", "a person who takes care of teeth"); hashTable.insert(d1); DictionaryItem d2("giggle", "a high-pitched laugh"); hashTable.insert(d2); DictionaryItem d3("potato", "a starchy tuber of the nightshade family"); hashTable.insert(d3); DictionaryItem d4("shoe", "an outer covering worn on the foot"); hashTable.insert(d4); DictionaryItem d5("yak", "a wooly ox"); hashTable.insert(d5); DictionaryItem d6("axle", "a rod on which a wheel turns"); hashTable.insert(d6); DictionaryItem d7("cone", "a solid with a circular base and apex"); hashTable.insert(d7); DictionaryItem d8("fish", "a cold-blooded aquatic vertebrate"); hashTable.insert(d8); hashTable.printTable(); std::cout << std::endl; std::cout << "dentist: " << hornerHash("dentist", TABLE_SIZE) << std::endl; std::cout << "giggle: " << hornerHash("giggle", TABLE_SIZE) << std::endl; std::cout << "potato: " << hornerHash("potato", TABLE_SIZE) << std::endl; std::cout << "shoe: " << hornerHash("shoe", TABLE_SIZE) << std::endl; std::cout << "yak: " << hornerHash("yak", TABLE_SIZE) << std::endl; std::cout << "axle: " << hornerHash("axle", TABLE_SIZE) << std::endl; std::cout << "cone: " << hornerHash("cone", TABLE_SIZE) << std::endl; std::cout << "fish: " << hornerHash("fish", TABLE_SIZE) << std::endl; return 0; }