mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
named namespace
This commit is contained in:
parent
d89b2df2f0
commit
e09637fb30
@ -10,8 +10,14 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace { // keep the code local to this file by assigning them to an unnamed
|
/**
|
||||||
|
* @addtogroup 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
|
||||||
using Entry = struct Entry;
|
using Entry = struct Entry;
|
||||||
bool putProber(const Entry& entry, int key);
|
bool putProber(const Entry& entry, int key);
|
||||||
@ -26,19 +32,27 @@ 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
|
||||||
|
* @param key key value to hash
|
||||||
|
* @returns hash of the key
|
||||||
|
*/
|
||||||
size_t hashFxn(int 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>(hashFxn(key));
|
int hash = static_cast<int>(hashFxn(key));
|
||||||
int i = 0;
|
int i = 0;
|
||||||
@ -83,7 +97,12 @@ int quadraticProbe(int key, bool searching) {
|
|||||||
return notPresent;
|
return notPresent;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Finds empty spot
|
/** Finds empty spot
|
||||||
|
* @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) {
|
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;
|
||||||
@ -91,7 +110,12 @@ bool putProber(const Entry& entry, int key) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Looks for a matching key
|
/** Looks for a matching key
|
||||||
|
* @param entry Instance of table entry
|
||||||
|
* @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) {
|
bool searchingProber(const Entry& entry, int key) {
|
||||||
if (entry.key == key) {
|
if (entry.key == key) {
|
||||||
return true;
|
return true;
|
||||||
@ -99,7 +123,11 @@ bool searchingProber(const Entry& entry, int key) {
|
|||||||
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) {
|
||||||
@ -108,7 +136,9 @@ Entry find(int key) {
|
|||||||
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) {
|
||||||
@ -124,7 +154,9 @@ void display() {
|
|||||||
std::cout << std::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;
|
||||||
@ -144,7 +176,9 @@ void rehash() {
|
|||||||
std::cout << "Table was rehashed, new size is: " << totalSize << std::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) {
|
||||||
int index = quadraticProbe(key, false);
|
int index = quadraticProbe(key, false);
|
||||||
table[index].key = key;
|
table[index].key = key;
|
||||||
@ -154,7 +188,9 @@ void add(int key) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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) {
|
||||||
@ -165,7 +201,9 @@ void remove(int key) {
|
|||||||
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) {
|
||||||
std::cout << "Initial table: ";
|
std::cout << "Initial table: ";
|
||||||
display();
|
display();
|
||||||
@ -178,7 +216,9 @@ void addInfo(int key) {
|
|||||||
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) {
|
||||||
std::cout << "Initial table: ";
|
std::cout << "Initial table: ";
|
||||||
display();
|
display();
|
||||||
@ -190,9 +230,19 @@ void removalInfo(int key) {
|
|||||||
std::cout << "New table: ";
|
std::cout << "New table: ";
|
||||||
display();
|
display();
|
||||||
}
|
}
|
||||||
} // namespace
|
|
||||||
|
|
||||||
// I/O
|
} // namespace quadratic_probing
|
||||||
|
/**
|
||||||
|
* @}
|
||||||
|
*/
|
||||||
|
|
||||||
|
using quadratic_probing::Entry;
|
||||||
|
using quadratic_probing::table;
|
||||||
|
using quadratic_probing::totalSize;
|
||||||
|
|
||||||
|
/** Main function
|
||||||
|
* @returns None
|
||||||
|
*/
|
||||||
int main() {
|
int main() {
|
||||||
int cmd = 0, hash = 0, key = 0;
|
int cmd = 0, hash = 0, key = 0;
|
||||||
std::cout << "Enter the initial size of Hash Table. = ";
|
std::cout << "Enter the initial size of Hash Table. = ";
|
||||||
@ -214,18 +264,20 @@ int main() {
|
|||||||
case 1:
|
case 1:
|
||||||
std::cout << "Enter key to add = ";
|
std::cout << "Enter key to add = ";
|
||||||
std::cin >> key;
|
std::cin >> key;
|
||||||
addInfo(key);
|
quadratic_probing::addInfo(key);
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
std::cout << "Enter key to remove = ";
|
std::cout << "Enter key to remove = ";
|
||||||
std::cin >> key;
|
std::cin >> key;
|
||||||
removalInfo(key);
|
quadratic_probing::removalInfo(key);
|
||||||
break;
|
break;
|
||||||
case 3: {
|
case 3: {
|
||||||
std::cout << "Enter key to search = ";
|
std::cout << "Enter key to search = ";
|
||||||
std::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(
|
||||||
|
key, true)];
|
||||||
|
if (entry.key == quadratic_probing::notPresent) {
|
||||||
std::cout << "Key not present";
|
std::cout << "Key not present";
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -233,10 +285,11 @@ int main() {
|
|||||||
case 4:
|
case 4:
|
||||||
std::cout << "Enter element to generate hash = ";
|
std::cout << "Enter element to generate hash = ";
|
||||||
std::cin >> key;
|
std::cin >> key;
|
||||||
std::cout << "Hash of " << key << " is = " << hashFxn(key);
|
std::cout << "Hash of " << key
|
||||||
|
<< " is = " << quadratic_probing::hashFxn(key);
|
||||||
break;
|
break;
|
||||||
case 5:
|
case 5:
|
||||||
display();
|
quadratic_probing::display();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
loop = false;
|
loop = false;
|
||||||
|
Loading…
Reference in New Issue
Block a user