-
Notifications
You must be signed in to change notification settings - Fork 0
/
HashTable.h
144 lines (112 loc) · 3.44 KB
/
HashTable.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//
// Created by Michael Castle on 5/1/22.
//
#ifndef INC_22S_FINAL_PROJ_HASHTABLE_H
#define INC_22S_FINAL_PROJ_HASHTABLE_H
//class HashTable{
//
//private:
// std::list<std::pair<std::string,std::vector<int>>> table[10000];
//
//
//
//};
#include <iostream>
#include <list>
using namespace std;
class HashTable{
private:
list<pair<string, vector<string>>>* table;
double tableSize;
int total_elements;
float max_load_factor;
// Hash function to calculate hash for a value:
int hash(string key) {
std::hash<string> hashString;
int hashed_key = hashString(key);
return abs(hashed_key % (int)total_elements);
}
public:
// Constructor to create a hash table with 'n' indices:
HashTable(){
max_load_factor = 0.7;
total_elements = 500;
tableSize = 0;
table = new list<pair<string, vector<string>>>[total_elements];
}
// Insert data in the hash table:
void insertElement(string key, vector<string> value){
pair<string, vector<string>> temp;
temp.first = key;
temp.second = value;
int hashKey = hash(key);
if(table[hashKey].empty()) {
tableSize++;
}
table[hashKey].push_back(temp);
if(tableSize/((double)total_elements) > max_load_factor){
tableSize = 0;
list<pair<string, vector<string>>>* tmpTable;
tmpTable = table;
total_elements = total_elements * 3;
table = new list<pair<string, vector<string>>>[total_elements];
list<pair<string, vector<string>>>* oldList;
int hashNum = hash(key);
for(int i = 0; i < total_elements/3; i++){
oldList = &tmpTable[i];
while(!oldList->empty()) {
insertElement(oldList->front().first,oldList->front().second);
oldList->remove(oldList->front());
}
}
delete[] tmpTable;
}
}
vector<string> find(string key){
list<pair<string, vector<string>>> tmpList;
int hashNum = hash(key);
tmpList = table[hashNum];
while(!tmpList.empty()) {
if(tmpList.front().first==key) {
return tmpList.front().second;
}
tmpList.remove(tmpList.front());
}
vector<string> empty;
return empty;
}
///DO NOT USE THIS THING IS BAD!
vector<string>* findPoint(string key) {
list<pair<string, vector<string>>> tmpList;
int hashNum = hash(key);
tmpList = table[hashNum];
while(!tmpList.empty()) {
if(tmpList.front().first==key) {
return &tmpList.front().second;
}
tmpList.remove(tmpList.front());
}
return nullptr;
}
void appendID(string key, string ID) {
int hashNum = hash(key);
std::list<pair<string, vector<string>>>::iterator it;
for(it = table[hashNum].begin(); it != table[hashNum].end(); it++) {
if(it->first == key) {
it->second.push_back(ID);
}
}
}
~HashTable() {
delete[] table;
}
// Remove data from the hash table:
// void remove(string key){
// auto it = find(key);
// if(it != table[hash(key)].end()){
// table[hash(key)].erase(it);
// total_elements--;
// }
// }
};
#endif //INC_22S_FINAL_PROJ_HASHTABLE_H