linear_probing Namespace Reference

An implementation of hash table using linear probing 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...
int | linearProbe (int key, bool searching)
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 linear probing algorithm.

## Function Documentation

## add()

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

Adds entry using linear probing. Checks for load factor here

Parameters

  key | key value to hash and add

{
int index = linearProbe(key, false);
table[index].key = key;
// Load factor greater than 0.5 causes resizing
if (++size / static_cast<double>(totalSize) >= 0.5) {
rehash();
}
}
linear_probing::linearProbe
int linearProbe(int key, bool searching)
Definition: linear_probing_hash_table.cpp:55
linear_probing::rehash
void rehash()
Definition: linear_probing_hash_table.cpp:138
Here is the call graph for this function:

## addInfo()

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

Information about the adding process

Parameters

  key | key value to hash and add

{
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)
linear_probing::hashFxn
size_t hashFxn(int key)
Hash a key. Uses the STL library's std::hash() function.
Definition: linear_probing_hash_table.cpp:46
linear_probing::display
void display()
Definition: linear_probing_hash_table.cpp:120
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 linear_probing::display | ( |  | ) |

Function to 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:

## hashFxn()

size_t linear_probing::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

## linearProbe()

int linear_probing::linearProbe | ( | int | key,
|  | bool | searching
| ) |  |

Performs linear probing to resolve collisions

Parameters

  key | key value to hash

Returns
  hash value of the key

{
int hash = static_cast<int>(hashFxn(key));
int i = 0;
Entry entry;
do {
int index = static_cast<int>((hash + i) % 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" << std::endl;
}
i++;
}
if (i == totalSize) {
std::cout << "Linear probe failed" << std::endl;
return notPresent;
}
} while (entry.key != notPresent);
return notPresent;
}
linear_probing::putProber
bool putProber(const Entry &entry, int key)
Definition: linear_probing_hash_table.cpp:98
linear_probing::searchingProber
bool searchingProber(const Entry &entry, int key)
Definition: linear_probing_hash_table.cpp:110
linear_probing::Entry
Definition: linear_probing_hash_table.cpp:35
linear_probing::Entry::key
int key
key value
Definition: linear_probing_hash_table.cpp:37
Here is the call graph for this function:

## putProber()

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

Finds empty spot

Parameters

  entry | instance to check in
  key | key value to hash

Returns
  hash value of the key

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

## rehash()

void linear_probing::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
totalSize *= 2;
table = std::vector<Entry>(totalSize);
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;
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 linear_probing::removalInfo | ( | int | key | ) |

Information about removal process

Parameters

  key | key value to hash and remove

{
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();
}
linear_probing::remove
void remove(int key)
Definition: linear_probing_hash_table.cpp:173
Here is the call graph for this function:

## remove()

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

Removes key. Leaves tombstone upon removal.

Parameters

  key | key value to hash and remove

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

## searchingProber()

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

Looks for a matching key

Parameters

  entry | instance to check in
  key | key value to hash

Returns
  hash value of the key

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