-
Notifications
You must be signed in to change notification settings - Fork 0
/
data-structs.cc
259 lines (228 loc) · 4.8 KB
/
data-structs.cc
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
#include <iostream>
#include <stdexcept>
// Stack
template <typename T>
struct Node {
T val;
Node<T> *next;
};
template <typename T>
class Stack {
Node<T> *top;
public:
Stack();
~Stack();
void push(T val);
T pop();
T peek();
bool is_empty();
};
template <typename T>
Stack<T>::Stack() {
top = nullptr;
}
template <typename T>
Stack<T>::~Stack() {
while (!is_empty())
pop();
}
template <typename T>
void Stack<T>::push(T val) {
Node<T> *n = new Node<T>;
n->val = val;
n->next = top;
top = n;
}
template <typename T>
T Stack<T>::pop() {
T val = peek();
Node<T> *cur = top;
top = top->next;
delete cur;
return val;
}
template <typename T>
T Stack<T>::peek() {
if (is_empty()) {
throw std::out_of_range("Stack is empty");
}
return top->val;
}
template <typename T>
bool Stack<T>::is_empty() {
if (top == nullptr)
return true;
return false;
}
// Hash Table
#define MAP_MAXLOADFACTOR 0.75
template <typename T>
struct Entry {
std::string key;
T val;
};
// Toy hashing function
static size_t makeHash(std::string key) {
size_t hash = 4226724;
size_t pos = 0;
for (char &c : key) {
hash ^= c << pos;
pos = (pos + 1) % sizeof(size_t);
}
return hash;
}
template <typename T>
static int insertEntry(Node<Entry<T>> **slots, size_t cap, Entry<T> entry) {
Node<Entry<T>> *cur;
size_t pos = makeHash(entry.key) % cap;
for (cur = slots[pos]; cur != nullptr; cur = cur->next) {
if (entry.key == cur->val.key) {
cur->val = entry;
return 0;
}
}
cur = new Node<Entry<T>>;
cur->val = entry;
cur->next = slots[pos];
slots[pos] = cur;
return 1;
}
template <typename T>
static void insertNodeWithoutSearch(Node<Entry<T>> **slots, size_t cap, Node<Entry<T>> *node) {
size_t pos = makeHash(node->val.key) % cap;
node->next = slots[pos];
slots[pos] = node;
}
template <typename T>
class Map {
Node<Entry<T>> **slots;
size_t cap;
size_t entries;
void grow();
public:
Map();
~Map();
void set(std::string key, T val);
T get(std::string key);
void remove(std::string key);
};
template <typename T>
Map<T>::Map() {
cap = 16;
entries = 0;
slots = new Node<Entry<T>>*[cap]();
}
template <typename T>
Map<T>::~Map() {
for (size_t i = 0; i < cap; i++) {
if (slots[i] == nullptr)
continue;
Node<Entry<T>> *prev, *cur;
prev = slots[i];
for (cur = prev->next; cur != nullptr; cur = cur->next) {
delete prev;
prev = cur;
}
delete prev;
}
delete slots;
}
template <typename T>
void Map<T>::grow() {
size_t newcap = cap * 2;
Node<Entry<T>> *cur, *next;
Node<Entry<T>> **new_slots = new Node<Entry<T>>*[newcap]();
for (size_t i = 0; i < cap; i++) {
for (cur = slots[i]; cur != nullptr; cur = next) {
next = cur->next;
insertNodeWithoutSearch(new_slots, newcap, cur);
}
}
delete slots;
slots = new_slots;
cap = newcap;
}
template <typename T>
void Map<T>::set(std::string key, T val) {
if (((float)entries / (float)cap) >= MAP_MAXLOADFACTOR)
grow();
Entry<T> entry = {key, val};
entries += insertEntry(slots, cap, entry);
}
template <typename T>
T Map<T>::get(std::string key) {
Node<Entry<T>> *cur;
size_t pos = makeHash(key) % cap;
for (cur = slots[pos]; cur != nullptr; cur = cur->next) {
if (key == cur->val.key)
return cur->val.val;
}
throw std::out_of_range("Key not found");
}
template <typename T>
void Map<T>::remove(std::string key) {
Node<Entry<T>> *cur;
Node<Entry<T>> *prev = nullptr;
size_t pos = makeHash(key) % cap;
for (cur = slots[pos]; cur != nullptr; cur = cur->next) {
if (key == cur->val.key) {
if (prev == nullptr)
slots[pos] = cur->next;
else
prev->next = cur->next;
delete cur;
return;
}
prev = cur;
}
}
int main() {
Stack<int> st;
/* Stack test */
st.push(1);
st.push(2);
st.push(3);
st.push(4);
st.push(5);
st.push(6);
st.push(7);
while (!st.is_empty())
std::cout << st.pop() << std::endl;
/* Hashmap test */
Map<int> map;
map.set("1>", 1);
map.set("2>", 2);
map.set("3>", 3);
map.set("4>", 4);
map.set("5>", 5);
map.set("6>", 6);
map.set("7>", 7);
map.set("8>", 8);
map.set("9>", 9);
std::cout << map.get("1>") << std::endl;
std::cout << map.get("2>") << std::endl;
std::cout << map.get("3>") << std::endl;
std::cout << map.get("4>") << std::endl;
std::cout << map.get("5>") << std::endl;
std::cout << map.get("6>") << std::endl;
std::cout << map.get("7>") << std::endl;
std::cout << map.get("8>") << std::endl;
std::cout << map.get("9>") << std::endl;
map.remove("1>");
try {
map.get("1>");
std::cout << "Failed: key '1>' was removed\n";
} catch (const std::out_of_range& e) {
std::cout << "Success: key: '1>' was removed\n";
}
map.set("2>", 22);
std::cout << map.get("2>") << std::endl;
map.remove("2>");
try {
map.get("2>");
std::cout << "Failed: key '2>' was removed\n";
} catch (const std::out_of_range& e) {
std::cout << "Success: key: '2>' was removed\n";
}
return 0;
}