-
Notifications
You must be signed in to change notification settings - Fork 0
/
HashTable.cpp
187 lines (174 loc) · 3.82 KB
/
HashTable.cpp
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* HashTable.cpp
*
* Created on: Apr 22, 2020
* Author: jaken
*/
#include "HashTable.h"
unsigned HashTable::hashFunction(int key) {
return key % NUM_BUCKETS;
}
void HashTable::resolve_chain(HTNode* newNode, unsigned index) {
//std::cout << "chain resolution " << index << std::endl;
collisions++;
HTNode* temp = table[index];
while(true) {
if(temp->next == NULL) {
temp->next = newNode;
break;
}
else {
temp = temp->next;
}
}
}
void HashTable::resolve_linear(HTNode* newNode, unsigned index) {
collisions++;
//std::cout << "linear resolution " << index << std::endl;
while(true) {
index++;
if(index == unsigned(NUM_BUCKETS)) {
index = 0;
}
if(table[index] == NULL) {
table[index] = newNode;
break;
}
}
}
void HashTable::resolve_quadratic(HTNode* newNode, unsigned index) {
collisions++;
int i = 1;
while(true) {
index += i + (i^2);
if(index >= unsigned(NUM_BUCKETS)) {
index -= NUM_BUCKETS;
}
if(table[index] == NULL) {
table[index] = newNode;
break;
}
i++;
}
}
HashTable::HashTable() {
table = new HTNode*[NUM_BUCKETS];
for(int n=0; n<NUM_BUCKETS; n++) {
table[n] = NULL;
}
}
void HashTable::insert(int key) {
unsigned index = hashFunction(key);
HTNode* newItem = new HTNode; newItem->key = key;
if(table[index] == NULL) {
table[index] = newItem;
}
else {
resolve_chain(newItem, index);
}
}
void HashTable::insert(int key, std::string resolution) {
unsigned index = hashFunction(key);
HTNode* newItem = new HTNode; newItem->key = key;
if(table[index] == NULL) {
table[index] = newItem;
}
else {
if(resolution == "chain") {
resolve_chain(newItem, index);
return;
}
if(resolution == "linear") {
resolve_linear(newItem, index);
return;
}
if(resolution == "quadratic") {
resolve_quadratic(newItem, index);
return;
}
std::cout << "Invalid resolution type." << std::endl;
return;
}
}
HTNode* HashTable::search(int key, std::string mode = "chain") {
//search mode for a hash table with chain collision resolution
if(mode == "chain") {
unsigned index = hashFunction(key);
if(table[index] == NULL) {
std::cout << "Table does not contain this value." << std::endl;
return NULL;
}
//jump to location; if keys don't match, check attached chain
HTNode* temp = table[index];
while(temp != NULL) {
if(temp->key == key) {
return temp;
}
else {
temp = temp->next;
}
}
std::cout << "Table does not contain this value." << std::endl;
return NULL;
}
//for linear collision resolution
if(mode == "linear") {
unsigned index = hashFunction(key);
if(table[index] == NULL) {
std::cout << "Table does not contain this value." << std::endl;
return NULL;
}
//crawl indices until located correct value.
//null values shouldn't be an issue here.
HTNode* temp;
while(temp != NULL) {
temp = table[index];
if(temp->key == key) {
return temp;
}
else {
index++;
}
}
std::cout << "Table does not contain this value." << std::endl;
return NULL;
}
//for quadratic resolution
if(mode == "quadratic") {
unsigned index = hashFunction(key);
if(table[index] == NULL) {
std::cout << "Table does not contain this value." << std::endl;
return NULL;
}
//jump indices until located correct value.
HTNode* temp;
int i = 1;
while(temp != NULL) {
temp = table[index];
if(temp->key == key) {
return temp;
}
else {
index += i + (i^2);
i++;
}
}
std::cout << "Table does not contain this value." << std::endl;
return NULL;
}
return NULL;
}
void HashTable::displayTable() {
for(int n=0; n<NUM_BUCKETS; n++) {
HTNode* temp = table[n];
while(temp != NULL) {
std::cout << n << ": ";
std::cout << temp->key << std::endl;
temp = temp->next;
}
}
std::cout << std::endl;
}
HashTable::~HashTable() {
// TODO Auto-generated destructor stub
}