-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathHashMap.cpp
More file actions
121 lines (95 loc) · 3.03 KB
/
HashMap.cpp
File metadata and controls
121 lines (95 loc) · 3.03 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
#include "HashMap.hpp"
using namespace std;
HashMapEntry::HashMapEntry() {
key = "";
value = "";
offset = 0;
}
HashMapEntry::HashMapEntry(const string& k, const string& v, const streampos& offset) {
key = k;
value = v;
next = nullptr;
this->offset = offset;
}
HashMap::HashMap() {
entries = new HashMapEntry*[TABLE_SIZE];
// Initialize all entries to nullptr
for (size_t i = 0; i < TABLE_SIZE; i++) {
entries[i] = nullptr;
}
}
HashMap::~HashMap() {
// Delete all entries
for (size_t i = 0; i < TABLE_SIZE; i++) {
HashMapEntry* current = entries[i];
while (current != nullptr) {
HashMapEntry* next = current->next;
delete current;
current = next;
}
}
// Delete the array
delete[] entries;
}
size_t HashMap::hashFunction1(const string& key) {
size_t hash = 0;
for (char c : key) {
hash = (hash * prime) + c; // Horner's rule
}
return hash % TABLE_SIZE;
}
size_t HashMap::hashFunction2(const string& key) {
// Use a prime number smaller than TABLE_SIZE
return (prime - (hashFunction1(key) % (prime - 1))) % TABLE_SIZE;
}
// Insert a key-value pair into the HashMap using chaining
void HashMap::insert(const string& key, const string& value, const streampos& offset) {
size_t index = hashFunction1(key);
// Check if the index is empty (no collision)
if (entries[index] == nullptr) {
entries[index] = new HashMapEntry(key, value, offset);
}
else {
// Collision occurred, insert at the beginning of the linked list
HashMapEntry* newEntry = new HashMapEntry(key, value, offset);
newEntry->next = entries[index];
entries[index] = newEntry;
}
}
string HashMap::getValue(const string& key) {
size_t index = hashFunction1(key);
// Search for the key in the linked list at the index
HashMapEntry* current = entries[index];
while (current != nullptr) {
if (current->key == key) {
return current->value; // Return the value if the key is found
}
current = current->next;
}
return "Key not found"; // Return a default value if the key is not found
}
string& HashMap::operator [] (const string& key) {
size_t index = hashFunction1(key);
// Search for the key in the linked list at the index
HashMapEntry* current = entries[index];
while (current != nullptr) {
if (current->key == key) {
return current->value; // Return the value if the key is found
}
current = current->next;
}
throw exception("Key not found"); // Return a default value if the key is not found
}
HashMapEntry* HashMap::getEntry(const string& key)
{
size_t index = hashFunction1(key);
// Search for the key in the linked list at the index
HashMapEntry* current = entries[index];
while (current != nullptr) {
if (current->key == key) {
return current; // Return the value if the key is found
}
current = current->next;
}
return nullptr; // Return a default value if the key is not found
}