Repository navigation
Expand file tree
/
Copy pathHashTable.cpp
More file actions
258 lines (213 loc) · 7.49 KB
/
Copy pathHashTable.cpp
File metadata and controls
258 lines (213 loc) · 7.49 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// ====================================================================================
// HashTable
// Author : Mahsa Fazli
// Polynomial hash + Open addressing (linear probing) + Tombstones + Dynamic resizing
// ====================================================================================
#include <iostream>
#include <string>
#include <vector>
#include <limits>
#include <iomanip>
using namespace std;
class HashTable {
private:
enum State { EMPTY, OCCUPIED, DELETED };
struct Entry {
string key;
string value;
State state;
Entry() : key(""), value(""), state(EMPTY) {}
};
vector<Entry> table;
int capacity;
int countOccupied; // number of OCCUPIED entries
int countDeleted; // number of DELETED entries (tombstones)
// Polynomial hashing parameters
const long long p = 31; // prime base
const long long MOD = 1000000007; // large prime modulo
long long polynomialHash(const string& s) const {
long long hashValue = 0;
long long pPower = 1;
for (char c : s) {
long long x = (unsigned char)c + 1; // includes spaces too
hashValue = (hashValue + x * pPower) % MOD;
pPower = (pPower * p) % MOD;
}
return hashValue;
}
int indexForKey(const string& key) const {
return (int)(polynomialHash(key) % capacity);
}
double loadFactor() const {
return (double)countOccupied / capacity;
}
double usedFactor() const {
// includes tombstones; if too many tombstones, performance drops
return (double)(countOccupied + countDeleted) / capacity;
}
void rehash(int newCapacity) {
vector<Entry> oldTable = table;
table.clear();
table.resize(newCapacity);
capacity = newCapacity;
countOccupied = 0;
countDeleted = 0;
for (int i = 0; i < (int)oldTable.size(); i++) {
if (oldTable[i].state == OCCUPIED) {
insert(oldTable[i].key, oldTable[i].value);
}
}
}
public:
HashTable(int cap = 11) {
capacity = cap;
table.resize(capacity);
countOccupied = 0;
countDeleted = 0;
}
// Task 1: show hash value
long long getHash(const string& key) const {
return polynomialHash(key);
}
// Task 2: insert (linear probing + tombstone reuse + update if exists)
bool insert(const string& key, const string& value) {
// Resize to keep operations efficient
if (loadFactor() > 0.70 || usedFactor() > 0.85) {
rehash(capacity * 2 + 1);
}
int start = indexForKey(key);
int firstDeletedIndex = -1;
for (int i = 0; i < capacity; i++) {
int idx = (start + i) % capacity;
if (table[idx].state == OCCUPIED) {
if (table[idx].key == key) {
// Update existing key
table[idx].value = value;
return true;
}
}
else if (table[idx].state == DELETED) {
if (firstDeletedIndex == -1) firstDeletedIndex = idx;
}
else { // EMPTY
int target = (firstDeletedIndex != -1) ? firstDeletedIndex : idx;
// If we insert into a tombstone, reduce tombstone count
if (table[target].state == DELETED) {
countDeleted--;
}
table[target].key = key;
table[target].value = value;
table[target].state = OCCUPIED;
countOccupied++;
return true;
}
}
return false; // should be rare due to resizing
}
// Task 2: search
bool search(const string& key, string& outValue) const {
int start = indexForKey(key);
for (int i = 0; i < capacity; i++) {
int idx = (start + i) % capacity;
if (table[idx].state == EMPTY) {
return false; // stop only on EMPTY
}
if (table[idx].state == OCCUPIED && table[idx].key == key) {
outValue = table[idx].value;
return true;
}
// DELETED or OCCUPIED with different key -> keep probing
}
return false;
}
// Task 2: deletion (tombstone)
bool remove(const string& key) {
int start = indexForKey(key);
for (int i = 0; i < capacity; i++) {
int idx = (start + i) % capacity;
if (table[idx].state == EMPTY) {
return false; // not found
}
if (table[idx].state == OCCUPIED && table[idx].key == key) {
table[idx].state = DELETED; // tombstone
countOccupied--;
countDeleted++;
// Optional cleanup: if too many tombstones, rehash to clean table
if (usedFactor() > 0.85) {
rehash(capacity); // same size, just cleans tombstones
}
return true;
}
}
return false;
}
// show updated record
void printRecords() const {
const int wKey = 22;
const int wVal = 22;
auto line = [&]() {
cout << "+" << string(wKey, '-') << "+" << string(wVal, '-') << "+\n";
};
line();
cout << "|" << left << setw(wKey) << " Key"
<< "|" << left << setw(wVal) << " Value" << "|\n";
line();
bool any = false;
for (int i = 0; i < capacity; i++) {
if (table[i].state == OCCUPIED) {
cout << "|" << left << setw(wKey) << (" " + table[i].key)
<< "|" << left << setw(wVal) << (" " + table[i].value) << "|\n";
any = true;
}
}
if (!any) {
cout << "|" << left << setw(wKey) << " (no records)"
<< "|" << left << setw(wVal) << " " << "|\n";
}
line();
}
// Back-End view:
void printBackEndTable() const {
const int wIndex = 7;
const int wKey = 14;
const int wValue = 14;
const int wState = 12;
auto line = [&]() {
cout << "+"
<< string(wIndex, '-') << "+"
<< string(wKey, '-') << "+"
<< string(wValue, '-') << "+"
<< string(wState, '-') << "+\n";
};
line();
cout << "|"
<< left << setw(wIndex) << "Index"
<< "|" << setw(wKey) << "Key"
<< "|" << setw(wValue) << "Value"
<< "|" << setw(wState) << "State"
<< "|\n";
line();
for (int i = 0; i < capacity; i++) {
cout << "|"
<< left << setw(wIndex) << i
<< "|";
if (table[i].state == EMPTY) {
cout << setw(wKey) << ""
<< "|" << setw(wValue) << ""
<< "|" << setw(wState) << "EMPTY";
}
else if (table[i].state == DELETED) {
cout << setw(wKey) << "(deleted)"
<< "|" << setw(wValue) << ""
<< "|" << setw(wState) << "TOMBSTONE";
}
else {
cout << setw(wKey) << table[i].key
<< "|" << setw(wValue) << table[i].value
<< "|" << setw(wState) << "OCCUPIED";
}
cout << "|\n";
}
line();
}
};