int countCollisions(const HashTable<Movie>& table) { int tableSize = table.getSize(); int chainSize = 0; int collisions = 0; while (tableSize-- > 0) { chainSize = table.getChainSize(tableSize); if (chainSize) collisions += (chainSize - 1); } return collisions; }
void buildTable(HashTable<Movie>& table, fstream& movies, fstream& output) { int MPAC; string title; while (movies.peek() != -1) { movies >> MPAC; getline(movies, title); Movie movie(MPAC, title); table.insert(movie); output << movie << " is being added" << endl; output << "\tThe hashed location is " << hash(movie) << endl; if (table.getChainSize(hash(movie)) > 1) { output << "\tThere was a collision loading " << movie << endl; output << "\tIt collided with " << table.getChainHead(hash(movie)) << endl; } else output << "\tThere was no collision loading " << movie << endl; output << LINE << endl; } }
void findMovie(const HashTable<Movie>& table) { int MPAC; while (getMPAC(MPAC)) { Movie movie = table.retrieve(Movie(MPAC)); int size = table.getChainSize(hash(movie)); Movie collisons[size]; table.getChain(hash(movie), collisons, movie); int index = 0; cout << LINE << endl; cout << "Will search for " << MPAC << endl; while (index < size - 1) cout << "\tThere was a collision here with " << collisons[index++] << endl; if (movie.MPAC != -1) cout << "Retrieved from hash table: " << movie << endl; else cout << "Could not find " << MPAC << endl; } }