4.2
This commit is contained in:
parent
8737713cd9
commit
73b9c1a8af
|
|
@ -4,59 +4,91 @@
|
||||||
HashTable::HashTable() {
|
HashTable::HashTable() {
|
||||||
this->size = 0;
|
this->size = 0;
|
||||||
this->entries = nullptr;
|
this->entries = nullptr;
|
||||||
this->setHashFunction("lastNumber");
|
this->setHashFunction("mod17");
|
||||||
|
this->numberOfEntries = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
HashTable::HashFunction::HashFunction(std::string name, hashFunctionPtr algorithm, int size) {
|
HashTable::HashFunction::HashFunction(std::string name,
|
||||||
|
hashFunctionPtr algorithm, int size) {
|
||||||
this->name = name;
|
this->name = name;
|
||||||
this->algorithm = algorithm;
|
this->algorithm = algorithm;
|
||||||
this->size = size;
|
this->size = size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void HashTable::insert(PbEntry *entry) {
|
||||||
void HashTable::insert(PbEntry* entry) {
|
|
||||||
int hashValue = this->hashFunction(entry->telephoneNumber);
|
int hashValue = this->hashFunction(entry->telephoneNumber);
|
||||||
hashValue = hashValue % this->size;
|
hashValue = hashValue % this->size;
|
||||||
|
|
||||||
if(this->entries[hashValue].key == "" || this->entries[hashValue].key == "deleted") {
|
if (this->entries[hashValue].key == "" ||
|
||||||
|
this->entries[hashValue].key == "deleted") {
|
||||||
this->entries[hashValue].key = entry->telephoneNumber;
|
this->entries[hashValue].key = entry->telephoneNumber;
|
||||||
this->entries[hashValue].phonebookEntry = entry;
|
this->entries[hashValue].phonebookEntry = entry;
|
||||||
|
this->numberOfEntries++;
|
||||||
} else {
|
} else {
|
||||||
bool inserted = false;
|
bool inserted = false;
|
||||||
int i = (hashValue + 1) % this->size;
|
int i = (hashValue + 1) % this->size;
|
||||||
while(inserted == false) {
|
while (inserted == false) {
|
||||||
if(i == hashValue) {
|
if (i == hashValue) {
|
||||||
std::cout << "HashTable full!" << std::endl;
|
std::cout << "HashTable full!" << std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(this->entries[i].key == "" || this->entries[i].key == "deleted") {
|
if (this->entries[i].key == "" || this->entries[i].key == "deleted") {
|
||||||
this->entries[i].key = entry->telephoneNumber;
|
this->entries[i].key = entry->telephoneNumber;
|
||||||
this->entries[i].phonebookEntry = entry;
|
this->entries[i].phonebookEntry = entry;
|
||||||
|
this->numberOfEntries++;
|
||||||
inserted = true;
|
inserted = true;
|
||||||
}
|
}
|
||||||
i = (i + 1) % this->size;
|
i = (i + 1) % this->size;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
std::cout << "Number of current Entries: " << this->numberOfEntries
|
||||||
|
<< ", LoadFactor: " << this->getloadFactor() << std::endl;
|
||||||
|
if (this->getloadFactor() > 2.0 / 3.0) {
|
||||||
|
if (this->size <= 17) {
|
||||||
|
std::cout << "Upgrading to mod37" << std::endl;
|
||||||
|
this->setHashFunction("mod37");
|
||||||
|
} else if (this->size <= 37) {
|
||||||
|
std::cout << "Upgrading to mod71!" << std::endl;
|
||||||
|
this->setHashFunction("mod71");
|
||||||
|
} else {
|
||||||
|
std::cout << "Cannot upgrade further. Desastrous Failure imminent!!"
|
||||||
|
<< std::endl;
|
||||||
|
if (this->numberOfEntries == this->size) {
|
||||||
|
std::cout
|
||||||
|
<< "Could not insert. Hashtable is full and can not be expanded"
|
||||||
|
<< std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void HashTable::print() {
|
void HashTable::print() {
|
||||||
std::cout << "=====================================" << std::endl;
|
std::cout << "=====================================" << std::endl;
|
||||||
for(int i = 0; i < this->size; i++) {
|
for (int i = 0; i < this->size; i++) {
|
||||||
std::cout << i << " :\t" << this->entries[i].key << std::endl;
|
std::cout << i << " :\t" << this->entries[i].key << std::endl;
|
||||||
|
|
||||||
}
|
}
|
||||||
std::cout << "=====================================" << std::endl;
|
std::cout << "=====================================" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void HashTable::setHashFunction(std::string algorithmName) {
|
void HashTable::setHashFunction(std::string algorithmName) {
|
||||||
for(int i = 0; i < 4; i++) {
|
for (int i = 0; i < 6; i++) {
|
||||||
if(this->availableAlgorithms[i].name == algorithmName) {
|
if (this->availableAlgorithms[i].name == algorithmName) {
|
||||||
|
HashNode *oldEntries = this->entries;
|
||||||
|
int oldsize = this->size;
|
||||||
this->hashFunction = this->availableAlgorithms[i].algorithm;
|
this->hashFunction = this->availableAlgorithms[i].algorithm;
|
||||||
this->size = this->availableAlgorithms[i].size;
|
this->size = this->availableAlgorithms[i].size;
|
||||||
if(this->entries != nullptr) {
|
this->numberOfEntries = 0;
|
||||||
delete[] this->entries;
|
|
||||||
}
|
|
||||||
this->entries = new HashNode[this->size];
|
this->entries = new HashNode[this->size];
|
||||||
|
if (oldEntries != nullptr) {
|
||||||
|
for (int i = 0; i < oldsize; i++) {
|
||||||
|
if (oldEntries[i].phonebookEntry != nullptr) {
|
||||||
|
|
||||||
|
this->insert(oldEntries[i].phonebookEntry);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
delete[] oldEntries;
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -64,19 +96,22 @@ void HashTable::setHashFunction(std::string algorithmName) {
|
||||||
std::cerr << "The Hash Function provided was not found!" << std::endl;
|
std::cerr << "The Hash Function provided was not found!" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
const PbEntry* HashTable::search(std::string key) {
|
const PbEntry *HashTable::search(std::string key) {
|
||||||
int hashValue = this->hashFunction(key); // key is "hashed" and stored in the variable int hashValue
|
int hashValue = this->hashFunction(
|
||||||
|
key); // key is "hashed" and stored in the variable int hashValue
|
||||||
hashValue = hashValue % this->size;
|
hashValue = hashValue % this->size;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while(this->entries[hashValue].key != key && this->entries[hashValue].key != "" && i <= this->size) {
|
while (this->entries[hashValue].key != key &&
|
||||||
|
this->entries[hashValue].key != "" && i <= this->size) {
|
||||||
hashValue = (hashValue + 1) % this->size;
|
hashValue = (hashValue + 1) % this->size;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(this->entries[hashValue].key == key) {
|
if (this->entries[hashValue].key == key) {
|
||||||
return this->entries[hashValue].phonebookEntry;
|
return this->entries[hashValue].phonebookEntry;
|
||||||
} else {
|
} else {
|
||||||
std::cout << "Entry with telephone number " << key << " not found!" << std::endl;
|
std::cout << "Entry with telephone number " << key << " not found!"
|
||||||
|
<< std::endl;
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -85,51 +120,62 @@ bool HashTable::deleteItem(std::string key) {
|
||||||
int hashValue = this->hashFunction(key);
|
int hashValue = this->hashFunction(key);
|
||||||
hashValue = hashValue % this->size;
|
hashValue = hashValue % this->size;
|
||||||
|
|
||||||
while(this->entries[hashValue].key != key && this->entries[hashValue].key != "") {
|
while (this->entries[hashValue].key != key &&
|
||||||
|
this->entries[hashValue].key != "") {
|
||||||
hashValue = (hashValue + 1) % this->size;
|
hashValue = (hashValue + 1) % this->size;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(this->entries[hashValue].key == key) {
|
if (this->entries[hashValue].key == key) {
|
||||||
this->entries[hashValue].key = "deleted";
|
this->entries[hashValue].key = "deleted";
|
||||||
this->entries[hashValue].phonebookEntry = nullptr;
|
this->entries[hashValue].phonebookEntry = nullptr;
|
||||||
delete this->entries[hashValue].phonebookEntry;
|
delete this->entries[hashValue].phonebookEntry;
|
||||||
|
this->numberOfEntries--;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
float HashTable::getloadFactor() {
|
||||||
|
return (float)this->numberOfEntries / this->size;
|
||||||
|
}
|
||||||
|
|
||||||
/* algorithm that "hashes" the last digit of a number */
|
/* algorithm that "hashes" the last digit of a number */
|
||||||
int lastNumber(std::string telNr) {
|
int lastNumber(std::string telNr) {
|
||||||
if(telNr.length() <=0) {
|
if (telNr.length() <= 0) {
|
||||||
std::cerr << "Number '" << telNr << "' does not contain enough digits. At least 1 needed!";
|
std::cerr << "Number '" << telNr
|
||||||
|
<< "' does not contain enough digits. At least 1 needed!";
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
return std::stoi(std::string(telNr.end()-1, telNr.end()));
|
return std::stoi(std::string(telNr.end() - 1, telNr.end()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* algorithm that "hashes" the first three digits of a number */
|
/* algorithm that "hashes" the first three digits of a number */
|
||||||
int firstThreeNumbers(std::string telNr) {
|
int firstThreeNumbers(std::string telNr) {
|
||||||
if(telNr.length() < 3) {
|
if (telNr.length() < 3) {
|
||||||
std::cerr << "Number '" << telNr << "' does not contain enough digits. At least 3 needed!";
|
std::cerr << "Number '" << telNr
|
||||||
|
<< "' does not contain enough digits. At least 3 needed!";
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
return std::stoi(std::string(telNr.begin(), telNr.begin()+3));
|
return std::stoi(std::string(telNr.begin(), telNr.begin() + 3));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* algorithm that "hashes" the last three digits of a number */
|
/* algorithm that "hashes" the last three digits of a number */
|
||||||
int lastThreeNumbers(std::string telNr) {
|
int lastThreeNumbers(std::string telNr) {
|
||||||
if(telNr.length() < 3) {
|
if (telNr.length() < 3) {
|
||||||
std::cerr << "Number '" << telNr << "' does not contain enough digits. At least 3 needed!";
|
std::cerr << "Number '" << telNr
|
||||||
|
<< "' does not contain enough digits. At least 3 needed!";
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
return std::stoi(std::string(telNr.end()-3, telNr.end()));
|
return std::stoi(std::string(telNr.end() - 3, telNr.end()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* algorithm that "hashes" the through mod17 */
|
/* algorithm that "hashes" the through mod17 */
|
||||||
int mod17(std::string telNr) {
|
int mod17(std::string telNr) { return (int)(std::stol(telNr) % 17); }
|
||||||
return (int)(std::stol(telNr)%17);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/* algorithm that "hashes" the through mod37 */
|
||||||
|
int mod37(std::string telNr) { return (int)(std::stol(telNr) % 37); }
|
||||||
|
|
||||||
|
/* algorithm that "hashes" the through mod71 */
|
||||||
|
int mod71(std::string telNr) { return (int)(std::stol(telNr) % 71); }
|
||||||
|
|
|
||||||
|
|
@ -1,44 +1,65 @@
|
||||||
#include <string>
|
|
||||||
#include "pbEntry.h"
|
#include "pbEntry.h"
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
typedef int (*hashFunctionPtr)(std::string); // typedefinition of a pointer for a hashfunction; they all need conform to the guideline: int <functionName>(std::string <param>)
|
typedef int (*hashFunctionPtr)(
|
||||||
int lastNumber(std::string telNr); // hash algorithm that uses only the last digit of a given telNr
|
std::string); // typedefinition of a pointer for a hashfunction; they all
|
||||||
int firstThreeNumbers(std::string telNr); // hash algorithm that uses the first three digits of a given telNr
|
// need conform to the guideline: int
|
||||||
int lastThreeNumbers(std::string telNr); // hash algorithm that uses the last three digits of a given telNr
|
// <functionName>(std::string <param>)
|
||||||
|
int lastNumber(std::string telNr); // hash algorithm that uses only the last
|
||||||
|
// digit of a given telNr
|
||||||
|
int firstThreeNumbers(std::string telNr); // hash algorithm that uses the first
|
||||||
|
// three digits of a given telNr
|
||||||
|
int lastThreeNumbers(std::string telNr); // hash algorithm that uses the last
|
||||||
|
// three digits of a given telNr
|
||||||
int mod17(std::string telNr); // hash algorithm that takes a given telNr mod17
|
int mod17(std::string telNr); // hash algorithm that takes a given telNr mod17
|
||||||
|
|
||||||
|
int mod37(std::string telNr); // hash algorithm that takes a given telNr mod37
|
||||||
|
|
||||||
|
int mod71(std::string telNr); // hash algorithm that takes a given telNr mod71
|
||||||
|
|
||||||
class HashTable {
|
class HashTable {
|
||||||
private:
|
private:
|
||||||
class HashFunction { // private inner class to make hash functions managable
|
class HashFunction { // private inner class to make hash functions managable
|
||||||
public:
|
public:
|
||||||
int size;
|
int size;
|
||||||
std::string name;
|
std::string name;
|
||||||
hashFunctionPtr algorithm;
|
hashFunctionPtr algorithm;
|
||||||
|
|
||||||
HashFunction(std::string name, hashFunctionPtr algorithm, int size); // constructor
|
HashFunction(std::string name, hashFunctionPtr algorithm,
|
||||||
|
int size); // constructor
|
||||||
};
|
};
|
||||||
|
|
||||||
HashFunction availableAlgorithms[4] = { {"lastNumber", lastNumber, 10}, {"firstThreeNumbers", firstThreeNumbers, 1000}, {"lastThreeNumbers", lastThreeNumbers, 1000}, {"mod17", mod17, 17}};
|
HashFunction availableAlgorithms[6] = {
|
||||||
|
{"lastNumber", lastNumber, 10},
|
||||||
|
{"firstThreeNumbers", firstThreeNumbers, 1000},
|
||||||
|
{"lastThreeNumbers", lastThreeNumbers, 1000},
|
||||||
|
{"mod17", mod17, 17},
|
||||||
|
{"mod37", mod37, 37},
|
||||||
|
{"mod71", mod71, 71}};
|
||||||
|
|
||||||
class HashNode { // private inner class for HashNodes that have a key (which will be the telephone number) and a value of type PbEntry
|
class HashNode { // private inner class for HashNodes that have a key (which
|
||||||
|
// will be the telephone number) and a value of type PbEntry
|
||||||
public:
|
public:
|
||||||
std::string key = "";
|
std::string key = "";
|
||||||
PbEntry* phonebookEntry;
|
PbEntry *phonebookEntry;
|
||||||
|
HashNode() : key(""), phonebookEntry(nullptr) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
int size; // m in lecture
|
int size; // m in lecture
|
||||||
int numberOfEntries; // n in lecture
|
int numberOfEntries; // n in lecture
|
||||||
HashNode* entries; // nodes of the hash table
|
HashNode *entries; // nodes of the hash table
|
||||||
hashFunctionPtr hashFunction; // pointer to the hash algorithm used is stored in a private variable of type hashFunctionPtr - see beginning of this file
|
hashFunctionPtr hashFunction; // pointer to the hash algorithm used is stored
|
||||||
|
// in a private variable of type
|
||||||
|
// hashFunctionPtr - see beginning of this file
|
||||||
|
|
||||||
|
void insert(PbEntry *entry);
|
||||||
void insert(PbEntry* entry);
|
const PbEntry *search(std::string key);
|
||||||
const PbEntry* search(std::string key);
|
|
||||||
bool deleteItem(std::string key);
|
bool deleteItem(std::string key);
|
||||||
void print();
|
void print();
|
||||||
void setHashFunction(std::string name);
|
void setHashFunction(std::string name);
|
||||||
|
float getloadFactor();
|
||||||
|
|
||||||
HashTable(); // default constructor
|
HashTable(); // default constructor
|
||||||
};
|
};
|
||||||
|
|
@ -1,7 +1,20 @@
|
||||||
#include "hashTable.h"
|
#include "hashTable.h"
|
||||||
#include "pbEntry.h"
|
#include "pbEntry.h"
|
||||||
#include <string>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
// Function to generate random strings for our Wares. See:
|
||||||
|
// https://stackoverflow.com/a/12468109
|
||||||
|
std::string random_string(size_t length) {
|
||||||
|
auto randchar = []() -> char {
|
||||||
|
const char charset[] = "0123456789";
|
||||||
|
const size_t max_index = (sizeof(charset) - 1);
|
||||||
|
return charset[rand() % max_index];
|
||||||
|
};
|
||||||
|
std::string str(length, 0);
|
||||||
|
std::generate_n(str.begin(), length, randchar);
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
PbEntry entry1("3277971", "Norma Noe", "North Lyme Drive 30");
|
PbEntry entry1("3277971", "Norma Noe", "North Lyme Drive 30");
|
||||||
|
|
@ -15,7 +28,11 @@ int main() {
|
||||||
ht.insert(&entry3);
|
ht.insert(&entry3);
|
||||||
ht.insert(&entry4);
|
ht.insert(&entry4);
|
||||||
ht.insert(&entry5);
|
ht.insert(&entry5);
|
||||||
|
for (int i = 0; i < 60; i++) {
|
||||||
|
ht.insert(new PbEntry(random_string(7), "test", "test"));
|
||||||
|
}
|
||||||
ht.print();
|
ht.print();
|
||||||
|
std::cout << ht.getloadFactor() << std::endl;
|
||||||
|
|
||||||
// ht.setHashFunction("mod17");
|
// ht.setHashFunction("mod17");
|
||||||
// ht.insert(&entry1);
|
// ht.insert(&entry1);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue