Merge pull request #978 from kvedala/hash

[enhancement] add `Hashing` folder to cmake
This commit is contained in:
Ayaan Khan 2020-07-31 15:18:22 +05:30 committed by GitHub
commit 62562abce3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 706 additions and 451 deletions

View File

@ -30,6 +30,7 @@ add_subdirectory(math)
add_subdirectory(others) add_subdirectory(others)
add_subdirectory(search) add_subdirectory(search)
add_subdirectory(ciphers) add_subdirectory(ciphers)
add_subdirectory(hashing)
add_subdirectory(strings) add_subdirectory(strings)
add_subdirectory(sorting) add_subdirectory(sorting)
add_subdirectory(geometry) add_subdirectory(geometry)

18
hashing/CMakeLists.txt Normal file
View File

@ -0,0 +1,18 @@
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
# with full pathname. RELATIVE may makes it easier to extract an executable name
# automatically.
file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp )
# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c )
# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES)
foreach( testsourcefile ${APP_SOURCES} )
# I used a simple string replace, to cut off .cpp.
string( REPLACE ".cpp" "" testname ${testsourcefile} )
add_executable( ${testname} ${testsourcefile} )
set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX)
if(OpenMP_CXX_FOUND)
target_link_libraries(${testname} OpenMP::OpenMP_CXX)
endif()
install(TARGETS ${testname} DESTINATION "bin/hash")
endforeach( testsourcefile ${APP_SOURCES} )

View File

@ -1,110 +1,179 @@
#include <math.h> /**
* @file chaining.cpp
* @author [vasutomar](https://github.com/vasutomar)
* @author [Krishna Vedala](https://github.com/kvedala)
* @brief Implementation of [hash
* chains](https://en.wikipedia.org/wiki/Hash_chain).
*/
#include <cmath>
#include <iostream> #include <iostream>
using namespace std; #include <memory>
#include <vector>
struct Node { /**
int data; * @brief Chain class with a given modulus
struct Node *next; */
} * head[100], *curr; class hash_chain {
private:
/**
* @brief Define a linked node
*/
using Node = struct Node {
int data{}; ///< data stored in the node
std::shared_ptr<struct Node> next; ///< pointer to the next node
};
void init() { std::vector<std::shared_ptr<Node>> head; ///< array of nodes
for (int i = 0; i < 100; i++) head[i] = NULL; int _mod; ///< modulus of the class
}
void add(int x, int h) { public:
struct Node *temp = new Node; /**
temp->data = x; * @brief Construct a new chain object
temp->next = NULL; *
if (!head[h]) { * @param mod modulus of the chain
head[h] = temp; */
curr = head[h]; explicit hash_chain(int mod) : _mod(mod) {
} else { while (mod--) head.push_back(nullptr);
curr = head[h];
while (curr->next) curr = curr->next;
curr->next = temp;
} }
}
void display(int mod) { /**
struct Node *temp; * @brief create and add a new node with a give value and at a given height
int i; *
for (i = 0; i < mod; i++) { * @param x value at the new node
if (!head[i]) { * @param h height of the node
cout << "Key " << i << " is empty" << endl; */
void add(int x, int h) {
std::shared_ptr<Node> curr;
std::shared_ptr<Node> temp(new Node);
temp->data = x;
temp->next = nullptr;
if (!head[h]) {
head[h] = temp;
curr = head[h];
} else { } else {
cout << "Key " << i << " has values = "; curr = head[h];
temp = head[i]; while (curr->next) curr = curr->next;
while (temp->next) { curr->next = temp;
cout << temp->data << " ";
temp = temp->next;
}
cout << temp->data;
cout << endl;
} }
} }
}
int hash(int x, int mod) { return x % mod; } /**
* @brief Display the chain
void find(int x, int h) { */
struct Node *temp = head[h]; void display() {
if (!head[h]) { std::shared_ptr<Node> temp = nullptr;
cout << "Element not found"; int i = 0;
return; for (i = 0; i < _mod; i++) {
if (!head[i]) {
std::cout << "Key " << i << " is empty" << std::endl;
} else {
std::cout << "Key " << i << " has values = ";
temp = head[i];
while (temp->next) {
std::cout << temp->data << " ";
temp = temp->next;
}
std::cout << temp->data;
std::cout << std::endl;
}
}
} }
while (temp->data != x && temp->next) temp = temp->next;
if (temp->next)
cout << "Element found";
else {
if (temp->data == x)
cout << "Element found";
else
cout << "Element not found";
}
}
int main(void) { /**
init(); * @brief Compute the hash of a value for current chain
int c, x, mod, h; *
cout << "Enter the size of Hash Table. = "; * @param x value to compute modulus of
cin >> mod; * @return modulus of `x`
* @note declared as a
* [`virtual`](https://en.cppreference.com/w/cpp/language/virtual) so that
* custom implementations of the class can modify the hash function.
*/
virtual int hash(int x) const { return x % _mod; }
/**
* @brief Find if a value and corresponding hash exist
*
* @param x value to search for
* @param h corresponding hash key
* @returns `true` if element found
* @returns `false` if element not found
*/
bool find(int x, int h) const {
std::shared_ptr<Node> temp = head[h];
if (!head[h]) {
// index does not exist!
std::cout << "Element not found";
return false;
}
// scan for data value
while (temp->data != x && temp->next) temp = temp->next;
if (temp->next) {
std::cout << "Element found";
return true;
}
// implicit else condition
// i.e., temp->next == nullptr
if (temp->data == x) {
std::cout << "Element found";
return true;
}
// further implicit else condition
std::cout << "Element not found";
return false;
}
};
/** Main function
* @returns `0` always
*/
int main() {
int c = 0, x = 0, mod = 0, h = 0;
std::cout << "Enter the size of Hash Table. = ";
std::cin >> mod;
hash_chain mychain(mod);
bool loop = true; bool loop = true;
while (loop) { while (loop) {
cout << endl; std::cout << std::endl;
cout << "PLEASE CHOOSE -" << endl; std::cout << "PLEASE CHOOSE -" << std::endl;
cout << "1. Add element." << endl; std::cout << "1. Add element." << std::endl;
cout << "2. Find element." << endl; std::cout << "2. Find element." << std::endl;
cout << "3. Generate Hash." << endl; std::cout << "3. Generate Hash." << std::endl;
cout << "4. Display Hash table." << endl; std::cout << "4. Display Hash table." << std::endl;
cout << "5. Exit." << endl; std::cout << "5. Exit." << std::endl;
cin >> c; std::cin >> c;
switch (c) { switch (c) {
case 1: case 1:
cout << "Enter element to add = "; std::cout << "Enter element to add = ";
cin >> x; std::cin >> x;
h = hash(x, mod); h = mychain.hash(x);
h = fabs(h); h = std::abs(h);
add(x, h); mychain.add(x, h);
break; break;
case 2: case 2:
cout << "Enter element to search = "; std::cout << "Enter element to search = ";
cin >> x; std::cin >> x;
h = hash(x, mod); h = mychain.hash(x);
find(x, h); mychain.find(x, h);
break; break;
case 3: case 3:
cout << "Enter element to generate hash = "; std::cout << "Enter element to generate hash = ";
cin >> x; std::cin >> x;
cout << "Hash of " << x << " is = " << hash(x, mod); std::cout << "Hash of " << x << " is = " << mychain.hash(x);
break; break;
case 4: case 4:
display(mod); mychain.display();
break; break;
default: default:
loop = false; loop = false;
break; break;
} }
cout << endl; std::cout << std::endl;
} }
/*add(1,&head1); /*add(1,&head1);
add(2,&head1); add(2,&head1);

View File

@ -1,128 +1,170 @@
// Copyright 2019 /**
* @file double_hash_hash_table.cpp
#include <stdlib.h> * @author [achance6](https://github.com/achance6)
#include <functional> * @author [Krishna Vedala](https://github.com/kvedala)
* @brief Storage mechanism using [double-hashed
* keys](https://en.wikipedia.org/wiki/Double_hashing).
* @note The implementation can be optimized by using OOP style.
*/
#include <iostream> #include <iostream>
#include <string> #include <memory>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::string;
/**
* @addtogroup open_addressing Open Addressing
* @{
* @namespace double_hashing
* @brief An implementation of hash table using [double
* hashing](https://en.wikipedia.org/wiki/Double_hashing) algorithm.
*/
namespace double_hashing {
// fwd declarations // fwd declarations
struct Entry; using Entry = struct Entry;
bool putProber(Entry entry, int key); bool putProber(const Entry& entry, int key);
bool searchingProber(Entry entry, int key); bool searchingProber(const Entry& entry, int key);
void add(int key); void add(int key);
// globals // Undocumented globals
int notPresent; int notPresent;
struct Entry* table; std::vector<Entry> table;
int totalSize; int totalSize;
int tomb = -1; int tomb = -1;
int size; int size;
bool rehashing; bool rehashing;
// Node that holds key /** Node object that holds key */
struct Entry { struct Entry {
explicit Entry(int key = notPresent) : key(key) {} explicit Entry(int key = notPresent) : key(key) {} ///< constructor
int key; int key; ///< key value
}; };
// Hash a key /**
int hashFxn(int key) { * @brief Hash a key. Uses the STL library's `std::hash()` function.
*
* @param key value to hash
* @return hash value of the key
*/
size_t hashFxn(int key) {
std::hash<int> hash; std::hash<int> hash;
return hash(key); return hash(key);
} }
// Used for second hash function /**
int otherHashFxn(int key) { * @brief Used for second hash function
*
* @param key key value to hash
* @return hash value of the key
*/
size_t otherHashFxn(int key) {
std::hash<int> hash; std::hash<int> hash;
return 1 + (7 - (hash(key) % 7)); return 1 + (7 - (hash(key) % 7));
} }
// Performs double hashing to resolve collisions /**
* @brief Performs double hashing to resolve collisions
*
* @param key key value to apply double-hash on
* @param searching `true` to check for conflicts
* @return Index of key when found
* @return new hash if no conflicts present
*/
int doubleHash(int key, bool searching) { int doubleHash(int key, bool searching) {
int hash = static_cast<int>(fabs(hashFxn(key))); int hash = static_cast<int>(hashFxn(key));
int i = 0; int i = 0;
Entry entry; Entry entry;
do { do {
int index = static_cast<int>(fabs((hash + (i * otherHashFxn(key))))) % int index =
totalSize; static_cast<int>(hash + (i * otherHashFxn(key))) % totalSize;
entry = table[index]; entry = table[index];
if (searching) { if (searching) {
if (entry.key == notPresent) { if (entry.key == notPresent) {
return notPresent; return notPresent;
} }
if (searchingProber(entry, key)) { if (searchingProber(entry, key)) {
cout << "Found key!" << endl; std::cout << "Found key!" << std::endl;
return index; return index;
} }
cout << "Found tombstone or equal hash, checking next" << endl; std::cout << "Found tombstone or equal hash, checking next"
<< std::endl;
i++; i++;
} else { } else {
if (putProber(entry, key)) { if (putProber(entry, key)) {
if (!rehashing) if (!rehashing) {
cout << "Spot found!" << endl; std::cout << "Spot found!" << std::endl;
}
return index; return index;
} }
if (!rehashing) if (!rehashing) {
cout << "Spot taken, looking at next (next index:" std::cout << "Spot taken, looking at next (next index:"
<< " " << " "
<< static_cast<int>( << static_cast<int>(hash + (i * otherHashFxn(key))) %
fabs((hash + (i * otherHashFxn(key))))) % totalSize
totalSize << ")" << std::endl;
<< ")" << endl; }
i++; i++;
} }
if (i == totalSize * 100) { if (i == totalSize * 100) {
cout << "DoubleHash probe failed" << endl; std::cout << "DoubleHash probe failed" << std::endl;
return notPresent; return notPresent;
} }
} while (entry.key != notPresent); } while (entry.key != notPresent);
return notPresent; return notPresent;
} }
// Finds empty spot /** Finds empty spot in a vector
bool putProber(Entry entry, int key) { * @param entry vector to search in
* @param key key to search for
* @returns `true` if key is not present or is a `toumb`
* @returns `false` is already occupied
*/
bool putProber(const Entry& entry, int key) {
if (entry.key == notPresent || entry.key == tomb) { if (entry.key == notPresent || entry.key == tomb) {
return true; return true;
} }
return false; return false;
} }
// Looks for a matching key /** Looks for a matching key
bool searchingProber(Entry entry, int key) { * @param entry vector to search in
if (entry.key == key) * @param key key value to search
* @returns `true` if found
* @returns `false` if not found
*/
bool searchingProber(const Entry& entry, int key) {
if (entry.key == key) {
return true; return true;
}
return false; return false;
} }
// Displays the table /** Displays the table
* @returns None
*/
void display() { void display() {
for (int i = 0; i < totalSize; i++) { for (int i = 0; i < totalSize; i++) {
if (table[i].key == notPresent) { if (table[i].key == notPresent) {
cout << " Empty "; std::cout << " Empty ";
} else if (table[i].key == tomb) { } else if (table[i].key == tomb) {
cout << " Tomb "; std::cout << " Tomb ";
} else { } else {
cout << " "; std::cout << " ";
cout << table[i].key; std::cout << table[i].key;
cout << " "; std::cout << " ";
} }
} }
cout << endl; std::cout << std::endl;
} }
// Rehashes the table into a bigger table /** Rehashes the table into a bigger table
* @returns None
*/
void rehash() { void rehash() {
// Necessary so wall of add info isn't printed all at once // Necessary so wall of add info isn't printed all at once
rehashing = true; rehashing = true;
int oldSize = totalSize; int oldSize = totalSize;
Entry* oldTable = table; std::vector<Entry> oldTable(table);
// Really this should use the next prime number greater than totalSize * 2 // Really this should use the next prime number greater than totalSize * 2
table = new Entry[totalSize * 2]; table = std::vector<Entry>(totalSize * 2);
totalSize *= 2; totalSize *= 2;
for (int i = 0; i < oldSize; i++) { for (int i = 0; i < oldSize; i++) {
if (oldTable[i].key != -1 && oldTable[i].key != notPresent) { if (oldTable[i].key != -1 && oldTable[i].key != notPresent) {
@ -130,112 +172,132 @@ void rehash() {
add(oldTable[i].key); add(oldTable[i].key);
} }
} }
delete[] oldTable; // delete[] oldTable;
// oldTable.reset();
rehashing = false; rehashing = false;
cout << "Table was rehashed, new size is: " << totalSize << endl; std::cout << "Table was rehashed, new size is: " << totalSize << std::endl;
} }
// Checks for load factor here /** Checks for load factor here
* @param key key value to add to the table
*/
void add(int key) { void add(int key) {
Entry* entry = new Entry(); // auto* entry = new Entry();
entry->key = key; // entry->key = key;
int index = doubleHash(key, false); int index = doubleHash(key, false);
table[index] = *entry; table[index].key = key;
// Load factor greater than 0.5 causes resizing // Load factor greater than 0.5 causes resizing
if (++size / static_cast<double>(totalSize) >= 0.5) { if (++size / static_cast<double>(totalSize) >= 0.5) {
rehash(); rehash();
} }
} }
// Removes key. Leaves tombstone upon removal. /** Removes key. Leaves tombstone upon removal.
* @param key key value to remove
*/
void remove(int key) { void remove(int key) {
int index = doubleHash(key, true); int index = doubleHash(key, true);
if (index == notPresent) { if (index == notPresent) {
cout << "key not found" << endl; std::cout << "key not found" << std::endl;
} }
table[index].key = tomb; table[index].key = tomb;
cout << "Removal successful, leaving tombstone" << endl; std::cout << "Removal successful, leaving tombstone" << std::endl;
size--; size--;
} }
// Information about the adding process /** Information about the adding process
* @param key key value to add to table
*/
void addInfo(int key) { void addInfo(int key) {
cout << "Initial table: "; std::cout << "Initial table: ";
display(); display();
cout << endl; std::cout << std::endl;
cout << "hash of " << key << " is " << hashFxn(key) << " % " << totalSize std::cout << "hash of " << key << " is " << hashFxn(key) << " % "
<< " == " << fabs(hashFxn(key) % totalSize); << totalSize << " == " << hashFxn(key) % totalSize;
cout << endl; std::cout << std::endl;
add(key); add(key);
cout << "New table: "; std::cout << "New table: ";
display(); display();
} }
// Information about removal process /** Information about removal process
* @param key key value to remove from table
*/
void removalInfo(int key) { void removalInfo(int key) {
cout << "Initial table: "; std::cout << "Initial table: ";
display(); display();
cout << endl; std::cout << std::endl;
cout << "hash of " << key << " is " << hashFxn(key) << " % " << totalSize std::cout << "hash of " << key << " is " << hashFxn(key) << " % "
<< " == " << hashFxn(key) % totalSize; << totalSize << " == " << hashFxn(key) % totalSize;
cout << endl; std::cout << std::endl;
remove(key); remove(key);
cout << "New table: "; std::cout << "New table: ";
display(); display();
} }
} // namespace double_hashing
/**
* @}
*/
// I/O using double_hashing::Entry;
int main(void) { using double_hashing::table;
int cmd, hash, key; using double_hashing::totalSize;
cout << "Enter the initial size of Hash Table. = ";
cin >> totalSize; /** Main program
table = new Entry[totalSize]; * @returns 0 on success
*/
int main() {
int cmd = 0, hash = 0, key = 0;
std::cout << "Enter the initial size of Hash Table. = ";
std::cin >> totalSize;
table = std::vector<Entry>(totalSize);
bool loop = true; bool loop = true;
while (loop) { while (loop) {
system("pause"); std::cout << std::endl;
cout << endl; std::cout << "PLEASE CHOOSE -" << std::endl;
cout << "PLEASE CHOOSE -" << endl; std::cout << "1. Add key. (Numeric only)" << std::endl;
cout << "1. Add key. (Numeric only)" << endl; std::cout << "2. Remove key." << std::endl;
cout << "2. Remove key." << endl; std::cout << "3. Find key." << std::endl;
cout << "3. Find key." << endl; std::cout << "4. Generate Hash. (Numeric only)" << std::endl;
cout << "4. Generate Hash. (Numeric only)" << endl; std::cout << "5. Display Hash table." << std::endl;
cout << "5. Display Hash table." << endl; std::cout << "6. Exit." << std::endl;
cout << "6. Exit." << endl; std::cin >> cmd;
cin >> cmd;
switch (cmd) { switch (cmd) {
case 1: case 1:
cout << "Enter key to add = "; std::cout << "Enter key to add = ";
cin >> key; std::cin >> key;
addInfo(key); double_hashing::addInfo(key);
break; break;
case 2: case 2:
cout << "Enter key to remove = "; std::cout << "Enter key to remove = ";
cin >> key; std::cin >> key;
removalInfo(key); double_hashing::removalInfo(key);
break; break;
case 3: { case 3: {
cout << "Enter key to search = "; std::cout << "Enter key to search = ";
cin >> key; std::cin >> key;
Entry entry = table[doubleHash(key, true)]; Entry entry = table[double_hashing::doubleHash(key, true)];
if (entry.key == notPresent) { if (entry.key == double_hashing::notPresent) {
cout << "Key not present"; std::cout << "Key not present";
}
break;
} }
break; case 4:
std::cout << "Enter element to generate hash = ";
std::cin >> key;
std::cout << "Hash of " << key
<< " is = " << double_hashing::hashFxn(key);
break;
case 5:
double_hashing::display();
break;
default:
loop = false;
break;
// delete[] table;
} }
case 4: std::cout << std::endl;
cout << "Enter element to generate hash = ";
cin >> key;
cout << "Hash of " << key << " is = " << fabs(hashFxn(key));
break;
case 5:
display();
break;
default:
loop = false;
break;
delete[] table;
}
cout << endl;
} }
return 0; return 0;
} }

View File

@ -1,229 +1,277 @@
// Copyright 2019 /**
* @file
#include <stdlib.h> * @author [achance6](https://github.com/achance6)
#include <functional> * @author [Krishna Vedala](https://github.com/kvedala)
* @brief Storage mechanism using [linear probing
* hash](https://en.wikipedia.org/wiki/Linear_probing) keys.
* @note The implementation can be optimized by using OOP style.
*/
#include <iostream> #include <iostream>
#include <string> #include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::string;
/**
* @addtogroup open_addressing Open Addressing
* @{
* @namespace linear_probing
* @brief An implementation of hash table using [linear
* probing](https://en.wikipedia.org/wiki/Linear_probing) algorithm.
*/
namespace linear_probing {
// fwd declarations // fwd declarations
struct Entry; using Entry = struct Entry;
bool putProber(Entry entry, int key); bool putProber(const Entry& entry, int key);
bool searchingProber(Entry entry, int key); bool searchingProber(const Entry& entry, int key);
void add(int key); void add(int key);
// globals // Undocumented globals
int notPresent; int notPresent;
struct Entry* table; std::vector<Entry> table;
int totalSize; int totalSize;
int tomb = -1; int tomb = -1;
int size; int size;
bool rehashing; bool rehashing;
// Node that holds key /** Node object that holds key */
struct Entry { struct Entry {
explicit Entry(int key = notPresent) : key(key) {} explicit Entry(int key = notPresent) : key(key) {} ///< constructor
int key; int key; ///< key value
}; };
// Hash a key /**
int hashFxn(int key) { * @brief Hash a key. Uses the STL library's `std::hash()` function.
*
* @param key value to hash
* @return hash value of the key
*/
size_t hashFxn(int key) {
std::hash<int> hash; std::hash<int> hash;
return hash(key); return hash(key);
} }
// Performs linear probing to resolve collisions /** Performs linear probing to resolve collisions
* @param key key value to hash
* @return hash value of the key
*/
int linearProbe(int key, bool searching) { int linearProbe(int key, bool searching) {
int hash = static_cast<int>(fabs(hashFxn(key))); int hash = static_cast<int>(hashFxn(key));
int i = 0; int i = 0;
Entry entry; Entry entry;
do { do {
int index = static_cast<int>(fabs((hash + i) % totalSize)); int index = static_cast<int>((hash + i) % totalSize);
entry = table[index]; entry = table[index];
if (searching) { if (searching) {
if (entry.key == notPresent) { if (entry.key == notPresent) {
return notPresent; return notPresent;
} }
if (searchingProber(entry, key)) { if (searchingProber(entry, key)) {
cout << "Found key!" << endl; std::cout << "Found key!" << std::endl;
return index; return index;
} }
cout << "Found tombstone or equal hash, checking next" << endl; std::cout << "Found tombstone or equal hash, checking next"
<< std::endl;
i++; i++;
} else { } else {
if (putProber(entry, key)) { if (putProber(entry, key)) {
if (!rehashing) if (!rehashing) {
cout << "Spot found!" << endl; std::cout << "Spot found!" << std::endl;
}
return index; return index;
} }
if (!rehashing) if (!rehashing) {
cout << "Spot taken, looking at next" << endl; std::cout << "Spot taken, looking at next" << std::endl;
}
i++; i++;
} }
if (i == totalSize) { if (i == totalSize) {
cout << "Linear probe failed" << endl; std::cout << "Linear probe failed" << std::endl;
return notPresent; return notPresent;
} }
} while (entry.key != notPresent); } while (entry.key != notPresent);
return notPresent; return notPresent;
} }
// Finds empty spot /** Finds empty spot
bool putProber(Entry entry, int key) { * @param entry instance to check in
* @param key key value to hash
* @return hash value of the key
*/
bool putProber(const Entry& entry, int key) {
if (entry.key == notPresent || entry.key == tomb) { if (entry.key == notPresent || entry.key == tomb) {
return true; return true;
} }
return false; return false;
} }
// Looks for a matching key /** Looks for a matching key
bool searchingProber(Entry entry, int key) { * @param entry instance to check in
if (entry.key == key) * @param key key value to hash
* @return hash value of the key
*/
bool searchingProber(const Entry& entry, int key) {
if (entry.key == key) {
return true; return true;
}
return false; return false;
} }
// Displays the table /** Function to displays the table
* @returns none
*/
void display() { void display() {
for (int i = 0; i < totalSize; i++) { for (int i = 0; i < totalSize; i++) {
if (table[i].key == notPresent) { if (table[i].key == notPresent) {
cout << " Empty "; std::cout << " Empty ";
} else if (table[i].key == tomb) { } else if (table[i].key == tomb) {
cout << " Tomb "; std::cout << " Tomb ";
} else { } else {
cout << " "; std::cout << " ";
cout << table[i].key; std::cout << table[i].key;
cout << " "; std::cout << " ";
} }
} }
cout << endl; std::cout << std::endl;
} }
// Rehashes the table into a bigger table /** Rehashes the table into a bigger table
* @returns None
*/
void rehash() { void rehash() {
// Necessary so wall of add info isn't printed all at once // Necessary so wall of add info isn't printed all at once
rehashing = true; rehashing = true;
int oldSize = totalSize; int oldSize = totalSize;
Entry* oldTable = table; std::vector<Entry> oldTable(table);
// Really this should use the next prime number greater than totalSize * 2 // Really this should use the next prime number greater than totalSize *
table = new Entry[totalSize * 2]; // 2
totalSize *= 2; totalSize *= 2;
table = std::vector<Entry>(totalSize);
for (int i = 0; i < oldSize; i++) { for (int i = 0; i < oldSize; i++) {
if (oldTable[i].key != -1 && oldTable[i].key != notPresent) { if (oldTable[i].key != -1 && oldTable[i].key != notPresent) {
size--; // Size stays the same (add increments size) size--; // Size stays the same (add increments size)
add(oldTable[i].key); add(oldTable[i].key);
} }
} }
delete[] oldTable; // delete[] oldTable;
rehashing = false; rehashing = false;
cout << "Table was rehashed, new size is: " << totalSize << endl; std::cout << "Table was rehashed, new size is: " << totalSize << std::endl;
} }
// Adds entry using linear probing. Checks for load factor here /** Adds entry using linear probing. Checks for load factor here
* @param key key value to hash and add
*/
void add(int key) { void add(int key) {
Entry* entry = new Entry();
entry->key = key;
int index = linearProbe(key, false); int index = linearProbe(key, false);
table[index] = *entry; table[index].key = key;
// Load factor greater than 0.5 causes resizing // Load factor greater than 0.5 causes resizing
if (++size / static_cast<double>(totalSize) >= 0.5) { if (++size / static_cast<double>(totalSize) >= 0.5) {
rehash(); rehash();
} }
} }
// Removes key. Leaves tombstone upon removal. /** Removes key. Leaves tombstone upon removal.
* @param key key value to hash and remove
*/
void remove(int key) { void remove(int key) {
int index = linearProbe(key, true); int index = linearProbe(key, true);
if (index == notPresent) { if (index == notPresent) {
cout << "key not found" << endl; std::cout << "key not found" << std::endl;
} }
cout << "Removal Successful, leaving tomb" << endl; std::cout << "Removal Successful, leaving tomb" << std::endl;
table[index].key = tomb; table[index].key = tomb;
size--; size--;
} }
// Information about the adding process /** Information about the adding process
* @param key key value to hash and add
*/
void addInfo(int key) { void addInfo(int key) {
cout << "Initial table: "; std::cout << "Initial table: ";
display(); display();
cout << endl; std::cout << std::endl;
cout << "hash of " << key << " is " << hashFxn(key) << " % " << totalSize std::cout << "hash of " << key << " is " << hashFxn(key) << " % "
<< " == " << fabs(hashFxn(key) % totalSize); << totalSize << " == " << hashFxn(key) % totalSize;
cout << endl; std::cout << std::endl;
add(key); add(key);
cout << "New table: "; std::cout << "New table: ";
display(); display();
} }
// Information about removal process /** Information about removal process
* @param key key value to hash and remove
*/
void removalInfo(int key) { void removalInfo(int key) {
cout << "Initial table: "; std::cout << "Initial table: ";
display(); display();
cout << endl; std::cout << std::endl;
cout << "hash of " << key << " is " << hashFxn(key) << " % " << totalSize std::cout << "hash of " << key << " is " << hashFxn(key) << " % "
<< " == " << hashFxn(key) % totalSize; << totalSize << " == " << hashFxn(key) % totalSize;
cout << endl; std::cout << std::endl;
remove(key); remove(key);
cout << "New table: "; std::cout << "New table: ";
display(); display();
} }
} // namespace linear_probing
/**
* @}
*/
// I/O using linear_probing::Entry;
int main(void) { using linear_probing::table;
int cmd, hash, key; using linear_probing::totalSize;
cout << "Enter the initial size of Hash Table. = ";
cin >> totalSize; /** Main function
table = new Entry[totalSize]; * @returns 0 on success
*/
int main() {
int cmd = 0, hash = 0, key = 0;
std::cout << "Enter the initial size of Hash Table. = ";
std::cin >> totalSize;
table = std::vector<Entry>(totalSize);
bool loop = true; bool loop = true;
while (loop) { while (loop) {
system("pause"); std::cout << std::endl;
cout << endl; std::cout << "PLEASE CHOOSE -" << std::endl;
cout << "PLEASE CHOOSE -" << endl; std::cout << "1. Add key. (Numeric only)" << std::endl;
cout << "1. Add key. (Numeric only)" << endl; std::cout << "2. Remove key." << std::endl;
cout << "2. Remove key." << endl; std::cout << "3. Find key." << std::endl;
cout << "3. Find key." << endl; std::cout << "4. Generate Hash. (Numeric only)" << std::endl;
cout << "4. Generate Hash. (Numeric only)" << endl; std::cout << "5. Display Hash table." << std::endl;
cout << "5. Display Hash table." << endl; std::cout << "6. Exit." << std::endl;
cout << "6. Exit." << endl; std::cin >> cmd;
cin >> cmd;
switch (cmd) { switch (cmd) {
case 1: case 1:
cout << "Enter key to add = "; std::cout << "Enter key to add = ";
cin >> key; std::cin >> key;
addInfo(key); linear_probing::addInfo(key);
break; break;
case 2: case 2:
cout << "Enter key to remove = "; std::cout << "Enter key to remove = ";
cin >> key; std::cin >> key;
removalInfo(key); linear_probing::removalInfo(key);
break; break;
case 3: { case 3: {
cout << "Enter key to search = "; std::cout << "Enter key to search = ";
cin >> key; std::cin >> key;
Entry entry = table[linearProbe(key, true)]; Entry entry = table[linear_probing::linearProbe(key, true)];
if (entry.key == notPresent) { if (entry.key == linear_probing::notPresent) {
cout << "Key not present"; std::cout << "Key not present";
}
break;
} }
break; case 4:
std::cout << "Enter element to generate hash = ";
std::cin >> key;
std::cout << "Hash of " << key
<< " is = " << linear_probing::hashFxn(key);
break;
case 5:
linear_probing::display();
break;
default:
loop = false;
break;
// delete[] table;
} }
case 4: std::cout << std::endl;
cout << "Enter element to generate hash = ";
cin >> key;
cout << "Hash of " << key << " is = " << fabs(hashFxn(key));
break;
case 5:
display();
break;
default:
loop = false;
break;
delete[] table;
}
cout << endl;
} }
return 0; return 0;
} }

View File

@ -1,244 +1,301 @@
// Copyright 2019 /**
* @file
#include <stdlib.h> * @author [achance6](https://github.com/achance6)
* @author [Krishna Vedala](https://github.com/kvedala)
* @brief Storage mechanism using [quadratic probing
* hash](https://en.wikipedia.org/wiki/Quadratic_probing) keys.
* @note The implementation can be optimized by using OOP style.
*/
#include <cmath> #include <cmath>
#include <functional>
#include <iostream> #include <iostream>
#include <string> #include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::string;
/**
* @addtogroup open_addressing Open Addressing
* @{
* @namespace quadratic_probing
* @brief An implementation of hash table using [quadratic
* probing](https://en.wikipedia.org/wiki/Quadratic_probing) algorithm.
*/
namespace quadratic_probing {
// fwd declarations // fwd declarations
struct Entry; using Entry = struct Entry;
bool putProber(Entry entry, int key); bool putProber(const Entry& entry, int key);
bool searchingProber(Entry entry, int key); bool searchingProber(const Entry& entry, int key);
void add(int key); void add(int key);
// globals // globals
int notPresent; int notPresent;
struct Entry* table; std::vector<Entry> table;
int totalSize; int totalSize;
int tomb = -1; int tomb = -1;
int size; int size;
bool rehashing; bool rehashing;
// Node that holds key /** Node that holds key
*/
struct Entry { struct Entry {
explicit Entry(int key = notPresent) : key(key) {} explicit Entry(int key = notPresent) : key(key) {} ///< constructor
int key; int key; ///< key value
}; };
// Hash a key /** Hash a key
int hashFxn(int key) { * @param key key value to hash
* @returns hash of the key
*/
size_t hashFxn(int key) {
std::hash<int> hash; std::hash<int> hash;
return hash(key); return hash(key);
} }
// Performs quadratic probing to resolve collisions /** Performs quadratic probing to resolve collisions
* @param key key value to search/probe
* @param searching `true` if only searching, `false1 if assigning
* @returns value of `notPresent`.
*/
int quadraticProbe(int key, bool searching) { int quadraticProbe(int key, bool searching) {
int hash = static_cast<int>(fabs(hashFxn(key))); int hash = static_cast<int>(hashFxn(key));
int i = 0; int i = 0;
Entry entry; Entry entry;
do { do {
int index = std::round(fabs( size_t index =
(hash + static_cast<int>(std::round(std::pow(i, 2)))) % totalSize)); (hash + static_cast<size_t>(std::round(std::pow(i, 2)))) %
totalSize;
entry = table[index]; entry = table[index];
if (searching) { if (searching) {
if (entry.key == notPresent) { if (entry.key == notPresent) {
return notPresent; return notPresent;
} }
if (searchingProber(entry, key)) { if (searchingProber(entry, key)) {
cout << "Found key!" << endl; std::cout << "Found key!" << std::endl;
return index; return index;
} }
cout << "Found tombstone or equal hash, checking next" << endl; std::cout << "Found tombstone or equal hash, checking next"
<< std::endl;
i++; i++;
} else { } else {
if (putProber(entry, key)) { if (putProber(entry, key)) {
if (!rehashing) if (!rehashing) {
cout << "Spot found!" << endl; std::cout << "Spot found!" << std::endl;
}
return index; return index;
} }
if (!rehashing) { if (!rehashing) {
cout << "Spot taken, looking at next (next index = " std::cout << "Spot taken, looking at next (next index = "
<< std::round(fabs((hash + static_cast<int>(std::round( << (hash + static_cast<size_t>(
std::pow(i + 1, 2)))) % std::round(std::pow(i + 1, 2)))) %
totalSize)) totalSize
<< endl; << std::endl;
} }
i++; i++;
} }
if (i == totalSize * 100) { if (i == totalSize * 100) {
cout << "Quadratic probe failed (infinite loop)" << endl; std::cout << "Quadratic probe failed (infinite loop)" << std::endl;
return notPresent; return notPresent;
} }
} while (entry.key != notPresent); } while (entry.key != notPresent);
return notPresent; return notPresent;
} }
// Finds empty spot /** Finds empty spot
bool putProber(Entry entry, int key) { * @param entry Instance of table entry
* @param key key value to search/probe
* @returns `true` if key is present
* @returns `false` if key is absent
*/
bool putProber(const Entry& entry, int key) {
if (entry.key == notPresent || entry.key == tomb) { if (entry.key == notPresent || entry.key == tomb) {
return true; return true;
} }
return false; return false;
} }
// Looks for a matching key /** Looks for a matching key
bool searchingProber(Entry entry, int key) { * @param entry Instance of table entry
if (entry.key == key) * @param key key value to search/probe
* @returns `true` if key matches the entry
* @returns `false` if key does not match the entry
*/
bool searchingProber(const Entry& entry, int key) {
if (entry.key == key) {
return true; return true;
}
return false; return false;
} }
// Helper /** Get the entry instance corresponding to a key
* @param key key value to search/probe
* @returns if present, the entry instance
* @returns if not present, a new instance
*/
Entry find(int key) { Entry find(int key) {
int index = quadraticProbe(key, true); int index = quadraticProbe(key, true);
if (index == notPresent) if (index == notPresent) {
return Entry(); return Entry();
}
return table[index]; return table[index];
} }
// Displays the table /** Displays the table
* @returns None
*/
void display() { void display() {
for (int i = 0; i < totalSize; i++) { for (int i = 0; i < totalSize; i++) {
if (table[i].key == notPresent) { if (table[i].key == notPresent) {
cout << " Empty "; std::cout << " Empty ";
} else if (table[i].key == tomb) { } else if (table[i].key == tomb) {
cout << " Tomb "; std::cout << " Tomb ";
} else { } else {
cout << " "; std::cout << " ";
cout << table[i].key; std::cout << table[i].key;
cout << " "; std::cout << " ";
} }
} }
cout << endl; std::cout << std::endl;
} }
// Rehashes the table into a bigger table /** Rehashes the table into a bigger table
* @returns none
*/
void rehash() { void rehash() {
// Necessary so wall of add info isn't printed all at once // Necessary so wall of add info isn't printed all at once
rehashing = true; rehashing = true;
int oldSize = totalSize; int oldSize = totalSize;
Entry* oldTable = table; std::vector<Entry> oldTable(table);
// Really this should use the next prime number greater than totalSize * 2 // Really this should use the next prime number greater than totalSize * 2
table = new Entry[totalSize * 2];
totalSize *= 2; totalSize *= 2;
table = std::vector<Entry>(totalSize);
for (int i = 0; i < oldSize; i++) { for (int i = 0; i < oldSize; i++) {
if (oldTable[i].key != -1 && oldTable[i].key != notPresent) { if (oldTable[i].key != -1 && oldTable[i].key != notPresent) {
size--; // Size stays the same (add increments size) size--; // Size stays the same (add increments size)
add(oldTable[i].key); add(oldTable[i].key);
} }
} }
delete[] oldTable; // delete[] oldTable;
rehashing = false; rehashing = false;
cout << "Table was rehashed, new size is: " << totalSize << endl; std::cout << "Table was rehashed, new size is: " << totalSize << std::endl;
} }
// Checks for load factor here /** Checks for load factor here
* @param key key value to hash and add to table
*/
void add(int key) { void add(int key) {
Entry* entry = new Entry();
entry->key = key;
int index = quadraticProbe(key, false); int index = quadraticProbe(key, false);
table[index] = *entry; table[index].key = key;
// Load factor greater than 0.5 causes resizing // Load factor greater than 0.5 causes resizing
if (++size / static_cast<double>(totalSize) >= 0.5) { if (++size / static_cast<double>(totalSize) >= 0.5) {
rehash(); rehash();
} }
} }
// Removes key. Leaves tombstone upon removal. /** Removes key. Leaves tombstone upon removal.
* @param key key value to hash and remove from table
*/
void remove(int key) { void remove(int key) {
int index = quadraticProbe(key, true); int index = quadraticProbe(key, true);
if (index == notPresent) { if (index == notPresent) {
cout << "key not found" << endl; std::cout << "key not found" << std::endl;
} }
table[index].key = tomb; table[index].key = tomb;
cout << "Removal successful, leaving tombstone" << endl; std::cout << "Removal successful, leaving tombstone" << std::endl;
size--; size--;
} }
// Information about the adding process /** Information about the adding process
* @param key key value to hash and add to table
*/
void addInfo(int key) { void addInfo(int key) {
cout << "Initial table: "; std::cout << "Initial table: ";
display(); display();
cout << endl; std::cout << std::endl;
cout << "hash of " << key << " is " << hashFxn(key) << " % " << totalSize std::cout << "hash of " << key << " is " << hashFxn(key) << " % "
<< " == " << fabs(hashFxn(key) % totalSize); << totalSize << " == " << hashFxn(key) % totalSize;
cout << endl; std::cout << std::endl;
add(key); add(key);
cout << "New table: "; std::cout << "New table: ";
display(); display();
} }
// Information about removal process /** Information about removal process
* @param key key value to hash and remove from table
*/
void removalInfo(int key) { void removalInfo(int key) {
cout << "Initial table: "; std::cout << "Initial table: ";
display(); display();
cout << endl; std::cout << std::endl;
cout << "hash of " << key << " is " << hashFxn(key) << " % " << totalSize std::cout << "hash of " << key << " is " << hashFxn(key) << " % "
<< " == " << hashFxn(key) % totalSize; << totalSize << " == " << hashFxn(key) % totalSize;
cout << endl; std::cout << std::endl;
remove(key); remove(key);
cout << "New table: "; std::cout << "New table: ";
display(); display();
} }
// I/O } // namespace quadratic_probing
int main(void) { /**
int cmd, hash, key; * @}
cout << "Enter the initial size of Hash Table. = "; */
cin >> totalSize;
table = new Entry[totalSize]; using quadratic_probing::Entry;
using quadratic_probing::table;
using quadratic_probing::totalSize;
/** Main function
* @returns None
*/
int main() {
int cmd = 0, hash = 0, key = 0;
std::cout << "Enter the initial size of Hash Table. = ";
std::cin >> totalSize;
table = std::vector<Entry>(totalSize);
bool loop = true; bool loop = true;
while (loop) { while (loop) {
system("pause"); std::cout << std::endl;
cout << endl; std::cout << "PLEASE CHOOSE -" << std::endl;
cout << "PLEASE CHOOSE -" << endl; std::cout << "1. Add key. (Numeric only)" << std::endl;
cout << "1. Add key. (Numeric only)" << endl; std::cout << "2. Remove key." << std::endl;
cout << "2. Remove key." << endl; std::cout << "3. Find key." << std::endl;
cout << "3. Find key." << endl; std::cout << "4. Generate Hash. (Numeric only)" << std::endl;
cout << "4. Generate Hash. (Numeric only)" << endl; std::cout << "5. Display Hash table." << std::endl;
cout << "5. Display Hash table." << endl; std::cout << "6. Exit." << std::endl;
cout << "6. Exit." << endl; std::cin >> cmd;
cin >> cmd;
switch (cmd) { switch (cmd) {
case 1: case 1:
cout << "Enter key to add = "; std::cout << "Enter key to add = ";
cin >> key; std::cin >> key;
addInfo(key); quadratic_probing::addInfo(key);
break; break;
case 2: case 2:
cout << "Enter key to remove = "; std::cout << "Enter key to remove = ";
cin >> key; std::cin >> key;
removalInfo(key); quadratic_probing::removalInfo(key);
break; break;
case 3: { case 3: {
cout << "Enter key to search = "; std::cout << "Enter key to search = ";
cin >> key; std::cin >> key;
Entry entry = table[quadraticProbe(key, true)]; quadratic_probing::Entry entry =
if (entry.key == notPresent) { quadratic_probing::table[quadratic_probing::quadraticProbe(
cout << "Key not present"; key, true)];
if (entry.key == quadratic_probing::notPresent) {
std::cout << "Key not present";
}
break;
} }
break; case 4:
std::cout << "Enter element to generate hash = ";
std::cin >> key;
std::cout << "Hash of " << key
<< " is = " << quadratic_probing::hashFxn(key);
break;
case 5:
quadratic_probing::display();
break;
default:
loop = false;
break;
// delete[] table;
} }
case 4: std::cout << std::endl;
cout << "Enter element to generate hash = ";
cin >> key;
cout << "Hash of " << key << " is = " << fabs(hashFxn(key));
break;
case 5:
display();
break;
default:
loop = false;
break;
delete[] table;
}
cout << endl;
} }
return 0; return 0;
} }