// How easy is it to make a good hash function? // If we are using chaining to resolve collisions, and the // table has H entries and we have inserted N random // things, then it is obvious that the average length // of one of the linked lists will be N/H. // If we can estimate the amound of data N, perhaps we // could set H=N when creating the table, and we'd // expect everything to be fast. The average look-up // operation would have to search a linked list of // length one. // Except there is more to it than that. An average of // 1 can be achieved in many ways. 999 empty lists plus // one of length 1,000 has an average of 1, but is useless. // 1,000 lists all of length exactly 1 would be perfect // but unrealistic to expect. A bad hash function would // fail to distribute records evenly and would result in // lots of empty lists and a few long ones, ruining the // search time. // Experiment: // create a hash table of size 10,000 and insert 10,000 // random strings. See how evenly distributed they are. // First experimental hash function multiplies together // the ASCII codes of all the characters in a string. // is it good enough? #include #include #include #include #include string alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "; string random_string() { int len = random()%15+6; string s=""; for (int i=0; i