/** * Horner hash demo * * Lisa Dion * 2020-Oct-28 * * Clayton Cafiero * 2021-Jan-16 * Minor changes: * - eliminate use of `using` * - minor revisons to Horner hash function * - use different keys for testing */ #include /** * 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 hash = 0; for (const char& letter : key) { hash = hash * 37 + letter; } return hash % tableSize; } int main() { const unsigned long TABLE_SIZE = 53; std::cout << hornerHash("Hello, World!", TABLE_SIZE) << std::endl; std::cout << hornerHash("UVM CS 2240", TABLE_SIZE) << std::endl; std::cout << hornerHash("Who is Egbert Porcupine?", TABLE_SIZE) << std::endl; std::cout << hornerHash("Universitas Viridis Montis", TABLE_SIZE) << std::endl; return 0; }