29std::vector<Entry> table;
60 return 1 + (7 - (hash(key) % 7));
72 int hash =
static_cast<int>(
hashFxn(key));
77 static_cast<int>(hash + (i *
otherHashFxn(key))) % totalSize;
80 if (entry.
key == notPresent) {
84 std::cout <<
"Found key!" << std::endl;
87 std::cout <<
"Found tombstone or equal hash, checking next"
93 std::cout <<
"Spot found!" << std::endl;
98 std::cout <<
"Spot taken, looking at next (next index:"
106 if (i == totalSize * 100) {
107 std::cout <<
"DoubleHash probe failed" << std::endl;
110 }
while (entry.
key != notPresent);
121 if (entry.
key == notPresent || entry.
key == tomb) {
134 if (entry.
key == key) {
144 for (
int i = 0; i < totalSize; i++) {
145 if (table[i].key == notPresent) {
146 std::cout <<
" Empty ";
147 }
else if (table[i].key == tomb) {
148 std::cout <<
" Tomb ";
151 std::cout << table[i].key;
155 std::cout << std::endl;
164 int oldSize = totalSize;
165 std::vector<Entry> oldTable(table);
167 table = std::vector<Entry>(totalSize * 2);
169 for (
int i = 0; i < oldSize; i++) {
170 if (oldTable[i].key != -1 && oldTable[i].key != notPresent) {
172 add(oldTable[i].key);
179 std::cout <<
"Table was rehashed, new size is: " << totalSize << std::endl;
189 table[index].key = key;
191 if (++size /
static_cast<double>(totalSize) >= 0.5) {
201 if (index == notPresent) {
202 std::cout <<
"key not found" << std::endl;
204 table[index].key = tomb;
205 std::cout <<
"Removal successful, leaving tombstone" << std::endl;
213 std::cout <<
"Initial table: ";
215 std::cout << std::endl;
216 std::cout <<
"hash of " << key <<
" is " <<
hashFxn(key) <<
" % "
217 << totalSize <<
" == " <<
hashFxn(key) % totalSize;
218 std::cout << std::endl;
220 std::cout <<
"New table: ";
228 std::cout <<
"Initial table: ";
230 std::cout << std::endl;
231 std::cout <<
"hash of " << key <<
" is " <<
hashFxn(key) <<
" % "
232 << totalSize <<
" == " <<
hashFxn(key) % totalSize;
233 std::cout << std::endl;
235 std::cout <<
"New table: ";
244using double_hashing::table;
245using double_hashing::totalSize;
251 int cmd = 0, hash = 0, key = 0;
252 std::cout <<
"Enter the initial size of Hash Table. = ";
253 std::cin >> totalSize;
254 table = std::vector<Entry>(totalSize);
257 std::cout << std::endl;
258 std::cout <<
"PLEASE CHOOSE -" << std::endl;
259 std::cout <<
"1. Add key. (Numeric only)" << std::endl;
260 std::cout <<
"2. Remove key." << std::endl;
261 std::cout <<
"3. Find key." << std::endl;
262 std::cout <<
"4. Generate Hash. (Numeric only)" << std::endl;
263 std::cout <<
"5. Display Hash table." << std::endl;
264 std::cout <<
"6. Exit." << std::endl;
268 std::cout <<
"Enter key to add = ";
273 std::cout <<
"Enter key to remove = ";
278 std::cout <<
"Enter key to search = ";
281 if (entry.
key == double_hashing::notPresent) {
282 std::cout <<
"Key not present";
287 std::cout <<
"Enter element to generate hash = ";
289 std::cout <<
"Hash of " << key
300 std::cout << std::endl;
An implementation of hash table using double hashing algorithm.
size_t hashFxn(int key)
Hash a key. Uses the STL library's std::hash() function.
bool searchingProber(const Entry &entry, int key)
size_t otherHashFxn(int key)
Used for second hash function.
void removalInfo(int key)
int doubleHash(int key, bool searching)
Performs double hashing to resolve collisions.
bool putProber(const Entry &entry, int key)
Entry(int key=notPresent)
constructor