-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash_table_iterator.sa
More file actions
368 lines (298 loc) · 9.29 KB
/
Copy pathhash_table_iterator.sa
File metadata and controls
368 lines (298 loc) · 9.29 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// Hash Table with Iterator Implementation in C++
#include <iostream>
#include <vector>
#include <list>
#include <iterator>
#include <string>
using namespace std;
template<typename K, typename V>
class IterableHashTable {
private:
struct Entry {
K key;
V value;
Entry(const K& k, const V& v) : key(k), value(v) {}
};
vector<list<Entry>> table;
size_t capacity;
size_t size;
size_t hashFunction(const K& key) const {
hash<K> hasher;
return hasher(key) % capacity;
}
void rehash() {
vector<list<Entry>> oldTable = table;
size_t oldCapacity = capacity;
capacity *= 2;
table.clear();
table.resize(capacity);
size = 0;
for (size_t i = 0; i < oldCapacity; ++i) {
for (const Entry& entry : oldTable[i]) {
insert(entry.key, entry.value);
}
}
}
public:
IterableHashTable(size_t initialCapacity = 16) : capacity(initialCapacity), size(0) {
table.resize(capacity);
}
void insert(const K& key, const V& value) {
if (static_cast<double>(size) / capacity > 0.75) {
rehash();
}
size_t index = hashFunction(key);
for (Entry& entry : table[index]) {
if (entry.key == key) {
entry.value = value;
return;
}
}
table[index].emplace_back(key, value);
size++;
}
bool remove(const K& key) {
size_t index = hashFunction(key);
for (auto it = table[index].begin(); it != table[index].end(); ++it) {
if (it->key == key) {
table[index].erase(it);
size--;
return true;
}
}
return false;
}
V* find(const K& key) {
size_t index = hashFunction(key);
for (Entry& entry : table[index]) {
if (entry.key == key) {
return &entry.value;
}
}
return nullptr;
}
bool contains(const K& key) const {
size_t index = hashFunction(key);
for (const Entry& entry : table[index]) {
if (entry.key == key) {
return true;
}
}
return false;
}
size_t getSize() const { return size; }
size_t getCapacity() const { return capacity; }
// Iterator implementation
class Iterator {
private:
vector<list<Entry>>* table;
size_t bucketIndex;
typename list<Entry>::iterator listIt;
bool atEnd;
void advanceToNextValid() {
while (bucketIndex < table->size()) {
if (listIt != (*table)[bucketIndex].end()) {
break;
}
bucketIndex++;
if (bucketIndex < table->size()) {
listIt = (*table)[bucketIndex].begin();
}
}
if (bucketIndex >= table->size()) {
atEnd = true;
}
}
public:
Iterator(vector<list<Entry>>* t, bool end = false)
: table(t), bucketIndex(0), atEnd(end) {
if (!end && !table->empty()) {
listIt = (*table)[0].begin();
advanceToNextValid();
} else {
atEnd = true;
}
}
Iterator& operator++() {
if (atEnd) return *this;
++listIt;
advanceToNextValid();
return *this;
}
Iterator operator++(int) {
Iterator temp = *this;
++(*this);
return temp;
}
Entry& operator*() {
return *listIt;
}
Entry* operator->() {
return &(*listIt);
}
bool operator==(const Iterator& other) const {
return atEnd == other.atEnd &&
(atEnd || (bucketIndex == other.bucketIndex && listIt == other.listIt));
}
bool operator!=(const Iterator& other) const {
return !(*this == other);
}
};
Iterator begin() {
return Iterator(&table);
}
Iterator end() {
return Iterator(&table, true);
}
// Const iterator
class ConstIterator {
private:
const vector<list<Entry>>* table;
size_t bucketIndex;
typename list<Entry>::const_iterator listIt;
bool atEnd;
void advanceToNextValid() {
while (bucketIndex < table->size()) {
if (listIt != (*table)[bucketIndex].end()) {
break;
}
bucketIndex++;
if (bucketIndex < table->size()) {
listIt = (*table)[bucketIndex].begin();
}
}
if (bucketIndex >= table->size()) {
atEnd = true;
}
}
public:
ConstIterator(const vector<list<Entry>>* t, bool end = false)
: table(t), bucketIndex(0), atEnd(end) {
if (!end && !table->empty()) {
listIt = (*table)[0].begin();
advanceToNextValid();
} else {
atEnd = true;
}
}
ConstIterator& operator++() {
if (atEnd) return *this;
++listIt;
advanceToNextValid();
return *this;
}
ConstIterator operator++(int) {
ConstIterator temp = *this;
++(*this);
return temp;
}
const Entry& operator*() const {
return *listIt;
}
const Entry* operator->() const {
return &(*listIt);
}
bool operator==(const ConstIterator& other) const {
return atEnd == other.atEnd &&
(atEnd || (bucketIndex == other.bucketIndex && listIt == other.listIt));
}
bool operator!=(const ConstIterator& other) const {
return !(*this == other);
}
};
ConstIterator begin() const {
return ConstIterator(&table);
}
ConstIterator end() const {
return ConstIterator(&table, true);
}
ConstIterator cbegin() const {
return begin();
}
ConstIterator cend() const {
return end();
}
void display() const {
cout << "Iterable Hash Table:" << endl;
cout << "Size: " << size << ", Capacity: " << capacity << endl;
cout << "Contents:" << endl;
for (const auto& entry : *this) {
cout << "[" << entry.key << ": " << entry.value << "] ";
}
cout << endl;
}
vector<K> getKeys() const {
vector<K> keys;
for (const auto& entry : *this) {
keys.push_back(entry.key);
}
return keys;
}
vector<V> getValues() const {
vector<V> values;
for (const auto& entry : *this) {
values.push_back(entry.value);
}
return values;
}
};
// Range-based for loop support example
template<typename K, typename V>
void printHashTable(const IterableHashTable<K, V>& ht) {
cout << "Printing using range-based for loop:" << endl;
for (const auto& entry : ht) {
cout << entry.key << " -> " << entry.value << endl;
}
}
// STL algorithm compatibility example
template<typename K, typename V>
V findMaxValue(const IterableHashTable<K, V>& ht) {
if (ht.getSize() == 0) {
return V{};
}
auto maxIt = max_element(ht.begin(), ht.end(),
[](const auto& a, const auto& b) {
return a.value < b.value;
});
return maxIt->value;
}
int main() {
IterableHashTable<string, int> iht;
iht.insert("apple", 5);
iht.insert("banana", 3);
iht.insert("orange", 7);
iht.insert("grape", 2);
iht.insert("mango", 4);
iht.display();
cout << endl;
cout << "=== Iterator Demo ===" << endl;
cout << "Using iterator:" << endl;
for (auto it = iht.begin(); it != iht.end(); ++it) {
cout << it->key << " -> " << it->value << endl;
}
cout << "\nUsing const iterator:" << endl;
for (auto it = iht.cbegin(); it != iht.cend(); ++it) {
cout << it->key << " -> " << it->value << endl;
}
cout << "\n=== Range-based for loop ===" << endl;
printHashTable(iht);
cout << "\n=== STL Algorithm Compatibility ===" << endl;
cout << "Max value: " << findMaxValue(iht) << endl;
cout << "Count of entries with value > 3: " <<
count_if(iht.begin(), iht.end(),
[](const auto& entry) { return entry.value > 3; }) << endl;
cout << "All entries (using for_each):" << endl;
for_each(iht.begin(), iht.end(),
[](const auto& entry) {
cout << entry.key << ":" << entry.value << " ";
});
cout << endl;
cout << "\n=== Find and Modify ===" << endl;
auto it = find_if(iht.begin(), iht.end(),
[](const auto& entry) { return entry.key == "banana"; });
if (it != iht.end()) {
cout << "Found banana with value: " << it->value << endl;
// Note: Can't modify through const_iterator, but could through regular iterator
}
return 0;
}