mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
fix quadratic_probing....
This commit is contained in:
parent
d955a49de9
commit
25efa07f22
@ -1,25 +1,30 @@
|
|||||||
// Copyright 2019
|
/**
|
||||||
|
* @file
|
||||||
#include <stdlib.h>
|
* @author [achance6](https://github.com/achance6)
|
||||||
|
* @author [Krishna Vedala](https://github.com/kvedala)
|
||||||
|
* @brief
|
||||||
|
*/
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <functional>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
using std::cin;
|
using std::cin;
|
||||||
using std::cout;
|
using std::cout;
|
||||||
using std::endl;
|
using std::endl;
|
||||||
using std::string;
|
using std::string;
|
||||||
|
|
||||||
|
namespace { // keep the code local to this file by assigning them to an unnamed
|
||||||
|
|
||||||
// fwd declarations
|
// fwd declarations
|
||||||
struct Entry;
|
using Entry = struct Entry;
|
||||||
bool putProber(Entry entry, int key);
|
bool putProber(Entry entry, int key);
|
||||||
bool searchingProber(Entry entry, int key);
|
bool searchingProber(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;
|
||||||
@ -32,19 +37,20 @@ struct Entry {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Hash a key
|
// Hash a key
|
||||||
int 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
|
||||||
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) {
|
||||||
@ -58,15 +64,16 @@ int quadraticProbe(int key, bool searching) {
|
|||||||
i++;
|
i++;
|
||||||
} else {
|
} else {
|
||||||
if (putProber(entry, key)) {
|
if (putProber(entry, key)) {
|
||||||
if (!rehashing)
|
if (!rehashing) {
|
||||||
cout << "Spot found!" << endl;
|
cout << "Spot found!" << endl;
|
||||||
|
}
|
||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
if (!rehashing) {
|
if (!rehashing) {
|
||||||
cout << "Spot taken, looking at next (next index = "
|
cout << "Spot taken, looking at next (next index = "
|
||||||
<< std::round(fabs((hash + static_cast<int>(std::round(
|
<< (hash +
|
||||||
std::pow(i + 1, 2)))) %
|
static_cast<size_t>(std::round(std::pow(i + 1, 2)))) %
|
||||||
totalSize))
|
totalSize
|
||||||
<< endl;
|
<< endl;
|
||||||
}
|
}
|
||||||
i++;
|
i++;
|
||||||
@ -89,16 +96,18 @@ bool putProber(Entry entry, int key) {
|
|||||||
|
|
||||||
// Looks for a matching key
|
// Looks for a matching key
|
||||||
bool searchingProber(Entry entry, int key) {
|
bool searchingProber(Entry entry, int key) {
|
||||||
if (entry.key == key)
|
if (entry.key == key) {
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper
|
// Helper
|
||||||
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];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -123,27 +132,25 @@ 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;
|
cout << "Table was rehashed, new size is: " << totalSize << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Checks for load factor here
|
// Checks for load factor here
|
||||||
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();
|
||||||
@ -167,7 +174,7 @@ void addInfo(int key) {
|
|||||||
display();
|
display();
|
||||||
cout << endl;
|
cout << endl;
|
||||||
cout << "hash of " << key << " is " << hashFxn(key) << " % " << totalSize
|
cout << "hash of " << key << " is " << hashFxn(key) << " % " << totalSize
|
||||||
<< " == " << fabs(hashFxn(key) % totalSize);
|
<< " == " << hashFxn(key) % totalSize;
|
||||||
cout << endl;
|
cout << endl;
|
||||||
add(key);
|
add(key);
|
||||||
cout << "New table: ";
|
cout << "New table: ";
|
||||||
@ -186,13 +193,14 @@ void removalInfo(int key) {
|
|||||||
cout << "New table: ";
|
cout << "New table: ";
|
||||||
display();
|
display();
|
||||||
}
|
}
|
||||||
|
} // namespace
|
||||||
|
|
||||||
// I/O
|
// I/O
|
||||||
int main(void) {
|
int main() {
|
||||||
int cmd, hash, key;
|
int cmd = 0, hash = 0, key = 0;
|
||||||
cout << "Enter the initial size of Hash Table. = ";
|
cout << "Enter the initial size of Hash Table. = ";
|
||||||
cin >> totalSize;
|
cin >> totalSize;
|
||||||
table = new Entry[totalSize];
|
table = std::vector<Entry>(totalSize);
|
||||||
bool loop = true;
|
bool loop = true;
|
||||||
while (loop) {
|
while (loop) {
|
||||||
system("pause");
|
system("pause");
|
||||||
@ -228,7 +236,7 @@ int main(void) {
|
|||||||
case 4:
|
case 4:
|
||||||
cout << "Enter element to generate hash = ";
|
cout << "Enter element to generate hash = ";
|
||||||
cin >> key;
|
cin >> key;
|
||||||
cout << "Hash of " << key << " is = " << fabs(hashFxn(key));
|
cout << "Hash of " << key << " is = " << hashFxn(key);
|
||||||
break;
|
break;
|
||||||
case 5:
|
case 5:
|
||||||
display();
|
display();
|
||||||
@ -236,7 +244,7 @@ int main(void) {
|
|||||||
default:
|
default:
|
||||||
loop = false;
|
loop = false;
|
||||||
break;
|
break;
|
||||||
delete[] table;
|
// delete[] table;
|
||||||
}
|
}
|
||||||
cout << endl;
|
cout << endl;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user