Figure 5.4, page 183 // A Hash routine for strings // Polynomial function using Horner's rule int hash ( const string & key, int tableSize ) { int hashVal = 0; // f(k) : string -> int for ( int i = 0; i < key.length(); i++ ) hashVal = 37 * hashval + key [ i ]; // g(k) : int -> {0, 1, 2, .... tableSize - 1} hashVal %= tableSize; // oops -- maybe overflow if ( hashVal < 0 ) hashVal += tableSize; return hashVal; }