forked from BYUCS235/Hashmap-Lab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashmap.cpp
More file actions
185 lines (158 loc) · 3.99 KB
/
Hashmap.cpp
File metadata and controls
185 lines (158 loc) · 3.99 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
#include "Hashmap.h"
unsigned int Hashmap::hash(string key) const
{
unsigned int int_key = (2*BUCKETS)/3;
for(char c : key)
{
int_key *= 2;
int_key += int(c);
while(int_key >= BUCKETS)
int_key = int_key % BUCKETS + ((int_key - int_key % BUCKETS)/BUCKETS);
}
return int_key;
}
Hashmap::Hashmap()
{
for(int i = 0; i < BUCKETS; i++)
buckets[i] = nullptr;
mapSize = 0;
}
Hashmap::~Hashmap()
{
clear();
}
void Hashmap::insert(string key, int value)
{
int hashed_index = hash(key);
// Empty bucket
if(buckets[hashed_index] == nullptr)
{
buckets[hashed_index] = newNode(key, value);
return;
}
// Non-empty buckets
for(Node* this_node = buckets[hashed_index]; this_node != nullptr; this_node = this_node->next)
{
if(this_node->key == key)
{
this_node->value = value;
return;
}
if(this_node->next == nullptr)
{
this_node->next = newNode(key, value);
this_node->next->prev = this_node;
return;
}
}
throw runtime_error("Unexpected behavior: value neither added nor updated in Hashmap::insert");
}
Hashmap::Node* Hashmap::newNode(string key, int value)
{
mapSize++;
Node* temp_node = new Node();
temp_node->key = key;
temp_node->value = value;
return temp_node;
}
bool Hashmap::contains(string key) const
{
int hashed_index = hash(key);
for(Node* this_node = buckets[hashed_index]; this_node != nullptr; this_node = this_node->next)
if(this_node->key == key)
return true;
return false;
}
int Hashmap::get(string key) const
{
int hashed_index = hash(key);
for(Node* this_node = buckets[hashed_index]; this_node != nullptr; this_node = this_node->next)
if(this_node->key == key)
return this_node->value;
throw invalid_argument(key + " not found in hashmap");
}
int& Hashmap::operator [](string key)
{
int hashed_index = hash(key);
// Empty bucket
if(buckets[hashed_index] == nullptr)
{
buckets[hashed_index] = newNode(key, 0);
return buckets[hashed_index]->value;
}
// Non-empty bucket
for(Node* this_node = buckets[hashed_index]; this_node != nullptr; this_node = this_node->next)
{
if(this_node->key == key)
return this_node->value;
if(this_node->next == nullptr)
{
this_node->next = newNode(key, 0);
this_node->next->prev = this_node;
}
}
throw runtime_error("Unexpected behavior: value neither found nor added in Hashmap::operator[]");
}
bool Hashmap::remove(string key)
{
int hashed_index = hash(key);
for(Node* this_node = buckets[hashed_index]; this_node != nullptr; this_node = this_node->next)
{
if(this_node->key == key)
{
((this_node->prev == nullptr) ? buckets[hashed_index] : this_node->prev->next) = this_node->next;
if(this_node->next != nullptr)
this_node->next->prev = this_node->prev;
mapSize--;
delete this_node;
return true;
}
}
return false;
}
void Hashmap::clear()
{
for(int i = 0; i < BUCKETS; i++)
{
Node* this_node = buckets[i];
while(this_node != nullptr)
{
Node* temp_node = this_node;
this_node = this_node->next;
delete temp_node;
}
buckets[i] = nullptr;
}
mapSize = 0;
}
string Hashmap::toString() const
{
stringstream output;
for(int i = 0; i < BUCKETS; i++)
{
output << "[" << i << "]";
for(Node* this_node = buckets[i]; this_node != nullptr; this_node = this_node->next)
output << " " << this_node->key << " => " << this_node->value;
output << endl;
}
return output.str();
}
int Hashmap::size() const
{
return mapSize;
}
string Hashmap::toSortedString() const
{
stringstream output;
priority_queue<Node*, vector<Node*>, NodeCompare> nodeHeap;
for(int i = 0; i < BUCKETS; i++)
for(Node* this_node = buckets[i]; this_node != nullptr; this_node = this_node->next)
nodeHeap.push(this_node);
while(!nodeHeap.empty())
{
Node* this_node = nodeHeap.top();
output << this_node->key << " => " << this_node->value << endl;
nodeHeap.pop();
}
return output.str();
}