Algorithmen_Datenstrukturen/Uebung 4/Uebung4_2/hashTableDynamic.h

65 lines
2.4 KiB
C++

#include "pbEntry.h"
#include <string>
#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>)
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 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 {
private:
class HashFunction { // private inner class to make hash functions managable
public:
int size;
std::string name;
hashFunctionPtr algorithm;
HashFunction(std::string name, hashFunctionPtr algorithm,
int size); // constructor
};
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
public:
std::string key = "";
PbEntry *phonebookEntry;
HashNode() : key(""), phonebookEntry(nullptr) {}
};
public:
int size; // m in lecture
int numberOfEntries; // n in lecture
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
void insert(PbEntry *entry);
const PbEntry *search(std::string key);
bool deleteItem(std::string key);
void print();
void setHashFunction(std::string name);
float getloadFactor();
HashTable(); // default constructor
};