double_hashing Namespace Reference

An implementation of hash table using double hashing algorithm. More...

## Classes

struct | Entry

## Typedefs

using | Entry = struct Entry

## Functions

bool | putProber (const Entry &entry, int key)
bool | searchingProber (const Entry &entry, int key)
void | add (int key)
size_t | hashFxn (int key)
| Hash a key. Uses the STL library's `std::hash()` function. More...
size_t | otherHashFxn (int key)
| Used for second hash function. More...
int | doubleHash (int key, bool searching)
| Performs double hashing to resolve collisions. More...
void | display ()
void | rehash ()
void | remove (int key)
void | addInfo (int key)
void | removalInfo (int key)

## Variables

int | notPresent
std::vector< Entry > | table
int | totalSize
int | tomb = -1
int | size
bool | rehashing

## Detailed Description

An implementation of hash table using double hashing algorithm.

## Function Documentation

## add()

void double_hashing::add | ( | int | key | ) |

Checks for load factor here

Parameters

  key | key value to add to the table

{
// auto* entry = new Entry();
// entry->key = key;
int index = doubleHash(key, false);
table[index].key = key;
// Load factor greater than 0.5 causes resizing
if (++size / static_cast<double>(totalSize) >= 0.5) {
rehash();
}
}
double_hashing::doubleHash
int doubleHash(int key, bool searching)
Performs double hashing to resolve collisions.
Definition: double_hash_hash_table.cpp:71
double_hashing::rehash
void rehash()
Definition: double_hash_hash_table.cpp:161
Here is the call graph for this function:

## addInfo()

void double_hashing::addInfo | ( | int | key | ) |

Information about the adding process

Parameters

  key | key value to add to table

{
std::cout << "Initial table: ";
display();
std::cout << std::endl;
std::cout << "hash of " << key << " is " << hashFxn(key) << " % "
<< totalSize << " == " << hashFxn(key) % totalSize;
std::cout << std::endl;
add(key);
std::cout << "New table: ";
display();
}
std::cout
std::endl
T endl(T... args)
double_hashing::hashFxn
size_t hashFxn(int key)
Hash a key. Uses the STL library's std::hash() function.
Definition: double_hash_hash_table.cpp:47
double_hashing::display
void display()
Definition: double_hash_hash_table.cpp:143
add
std::string add(std::string a, std::string b)
Definition: string_fibonacci.cpp:24
Here is the call graph for this function:

## display()

void double_hashing::display | ( |  | ) |

Displays the table

Returns
  None

{
for (int i = 0; i < totalSize; i++) {
if (table[i].key == notPresent) {
std::cout << " Empty ";
} else if (table[i].key == tomb) {
std::cout << " Tomb ";
} else {
std::cout << " ";
std::cout << table[i].key;
std::cout << " ";
}
}
std::cout << std::endl;
}
Here is the call graph for this function:

## doubleHash()

int double_hashing::doubleHash | ( | int | key,
|  | bool | searching
| ) |  |

Performs double hashing to resolve collisions.

Parameters

  key | key value to apply double-hash on
  searching | `true` to check for conflicts

Returns
  Index of key when found
  new hash if no conflicts present

{
int hash = static_cast<int>(hashFxn(key));
int i = 0;
Entry entry;
do {
int index =
static_cast<int>(hash + (i * otherHashFxn(key))) % totalSize;
entry = table[index];
if (searching) {
if (entry.key == notPresent) {
return notPresent;
}
if (searchingProber(entry, key)) {
std::cout << "Found key!" << std::endl;
return index;
}
std::cout << "Found tombstone or equal hash, checking next"
<< std::endl;
i++;
} else {
if (putProber(entry, key)) {
if (!rehashing) {
std::cout << "Spot found!" << std::endl;
}
return index;
}
if (!rehashing) {
std::cout << "Spot taken, looking at next (next index:"
<< " "
<< static_cast<int>(hash + (i * otherHashFxn(key))) %
totalSize
<< ")" << std::endl;
}
i++;
}
if (i == totalSize * 100) {
std::cout << "DoubleHash probe failed" << std::endl;
return notPresent;
}
} while (entry.key != notPresent);
return notPresent;
}
double_hashing::searchingProber
bool searchingProber(const Entry &entry, int key)
Definition: double_hash_hash_table.cpp:133
double_hashing::otherHashFxn
size_t otherHashFxn(int key)
Used for second hash function.
Definition: double_hash_hash_table.cpp:58
double_hashing::putProber
bool putProber(const Entry &entry, int key)
Definition: double_hash_hash_table.cpp:120
double_hashing::Entry
Definition: double_hash_hash_table.cpp:36
double_hashing::Entry::key
int key
key value
Definition: double_hash_hash_table.cpp:38
Here is the call graph for this function:

## hashFxn()

size_t double_hashing::hashFxn | ( | int | key | ) |

Hash a key. Uses the STL library's `std::hash()` function.

Parameters

  key | value to hash

Returns
  hash value of the key

{
std::hash<int> hash;
return hash(key);
}
std::hash

## otherHashFxn()

size_t double_hashing::otherHashFxn | ( | int | key | ) |

Used for second hash function.

Parameters

  key | key value to hash

Returns
  hash value of the key

{
std::hash<int> hash;
return 1 + (7 - (hash(key) % 7));
}

## putProber()

bool double_hashing::putProber | ( | const Entry & | entry,
|  | int | key
| ) |  |

Finds empty spot in a vector

Parameters

  entry | vector to search in
  key | key to search for

Returns
  `true` if key is not present or is a `toumb`
  `false` is already occupied

{
if (entry.key == notPresent || entry.key == tomb) {
return true;
}
return false;
}

## rehash()

void double_hashing::rehash | ( |  | ) |

Rehashes the table into a bigger table

Returns
  None

{
// Necessary so wall of add info isn't printed all at once
rehashing = true;
int oldSize = totalSize;
std::vector<Entry> oldTable(table);
// Really this should use the next prime number greater than totalSize * 2
table = std::vector<Entry>(totalSize * 2);
totalSize *= 2;
for (int i = 0; i < oldSize; i++) {
if (oldTable[i].key != -1 && oldTable[i].key != notPresent) {
size--; // Size stays the same (add increments size)
add(oldTable[i].key);
}
}
// delete[] oldTable;
// oldTable.reset();
rehashing = false;
std::cout << "Table was rehashed, new size is: " << totalSize << std::endl;
}
std::vector
Here is the call graph for this function:

## removalInfo()

void double_hashing::removalInfo | ( | int | key | ) |

Information about removal process

Parameters

  key | key value to remove from table

{
std::cout << "Initial table: ";
display();
std::cout << std::endl;
std::cout << "hash of " << key << " is " << hashFxn(key) << " % "
<< totalSize << " == " << hashFxn(key) % totalSize;
std::cout << std::endl;
remove(key);
std::cout << "New table: ";
display();
}
double_hashing::remove
void remove(int key)
Definition: double_hash_hash_table.cpp:199
Here is the call graph for this function:

## remove()

void double_hashing::remove | ( | int | key | ) |

Removes key. Leaves tombstone upon removal.

Parameters

  key | key value to remove

{
int index = doubleHash(key, true);
if (index == notPresent) {
std::cout << "key not found" << std::endl;
}
table[index].key = tomb;
std::cout << "Removal successful, leaving tombstone" << std::endl;
size--;
}
Here is the call graph for this function:

## searchingProber()

bool double_hashing::searchingProber | ( | const Entry & | entry,
|  | int | key
| ) |  |

Looks for a matching key

Parameters

  entry | vector to search in
  key | key value to search

Returns
  `true` if found
  `false` if not found

{
if (entry.key == key) {
return true;
}
return false;
}