unsigned int hashCode(const char *str) {
unsigned int val = 0 ;
const unsigned int thirtyThree = 33 ; // magic number from textbook
int i = 0 ;
while (str[i] != '\0') {
val = val * thirtyThree + str[i] ;
i++ ;
}
return val ;
}
//Download: words.h.
//
//Note that the items in the words[] array are const char * strings and
// not C++ strings.
#ifndef _WORDS_H_
#define _WORDS_H_
const int numWords = 58109 ;
const char *words[numWords] = {
"aardvark",
"aardwolf",
"aaron",
"aback",
"abacus",
"abaft",
"abalone",
"abandon",
"abandoned",
"abandonment",
"abandons",
.
.
.
"zoomed",
"zooming",
"zooms",
"zooplankton",
"zoos",
"zulu",
"zulus"
} ;
#endif
// There is an array of prime numbers between 100 and 200,000
// in the primes.h file.
//
// Download: primes.h.
const int numPrimes = 17959 ;
const int primes[numPrimes] = {
101,
103,
107,
109,
113,
127,
131,
137,
139,
149,
151,
157,
.
.
.
199873,
199877,
199889,
199909,
199921,
199931,
199933,
199961,
199967,
199999
} ;
// File: test1.cpp
//
// Testing basic insert, find and remove without
// triggering incremental rehashing
//
#include
#include
using namespace std ;
#include "HashTable.h"
int main() {
HashTable T(107) ;
T.insert("tributes") ; // 21
T.insert("skulduggery") ; // 22
T.insert("convulse") ; // 23
T.insert("frothed") ; // 24
T.insert("horrify") ; // 25
T.insert("blackmailers") ; // 26
T.insert("defenestrated") ; // 27
T.insert("garrison") ; // 23 -> 28
T.insert("lidless") ; // 22 -> 29
// T.insert("eye") ; // 21 -> 30
cout << "----------------------------------------------------\n" ;
cout << "Original hash table\n" ;
cout << "----------------------------------------------------\n" ;
T.dump() ;
cout << "----------------------------------------------------\n" ;
cout << "\n\nDo some find() and remove() operations...\n\n" ;
const char *str ;
char *ptr ;
bool found ;
if( T.find(str="skulduggery") ) {
cout << "Found " << str << endl ;
} else {
cout << "Did not find " << str << endl ;
}
if( T.find(str="lidless") ) {
cout << "Found " << str << endl ;
} else {
cout << "Did not find " << str << endl ;
}
if( T.find(str="defenestrated") ) {
cout << "Found " << str << endl ;
} else {
cout << "Did not find " << str << endl ;
}
if( T.find(str="peircing") ) {
cout << "Found " << str << endl ;
} else {
cout << "Did not find " << str << endl ;
}
ptr = T.remove(str="garrison") ;
if (ptr == NULL) {
cout << "String " << str << " not found, not removed\n" ;
} else {
cout << "Removed string = " << ptr << endl ;
free(ptr) ;
}
ptr = T.remove(str="infractions") ;
if (ptr == NULL) {
cout << "String " << str << " not found, not removed\n" ;
} else {
cout << "Removed string = " << ptr << endl ;
free(ptr) ;
}
if( T.find(str="garrison") ) {
cout << "Found " << str << endl ;
} else {
cout << "Did not find " << str << endl ;
}
if( T.find(str="lidless") ) {
cout << "Found " << str << endl ;
} else {
cout << "Did not find " << str << endl ;
}
cout << "\n\n" ;
cout << "----------------------------------------------------\n" ;
cout << "Hash table after finds and removes\n" ;
cout << "----------------------------------------------------\n" ;
T.dump() ;
cout << "----------------------------------------------------\n" ;
cout << "\n\nNext insert should reuse DELETED slots...\n\n" ;
T.insert("undying") ; // 25 -> 28
cout << "\n\n" ;
cout << "----------------------------------------------------\n" ;
cout << "Final hash table\n" ;
cout << "----------------------------------------------------\n" ;
T.dump() ;
cout << "----------------------------------------------------\n" ;
return 0 ;
}