-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMap.cpp
More file actions
525 lines (416 loc) · 12.2 KB
/
Map.cpp
File metadata and controls
525 lines (416 loc) · 12.2 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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
#include "Map.h"
#include <iostream>
#include <string>
using KeyType = std::string;
using ValueType = double;
// BST Class Functions:
// Function already declared in Map.h:
// BST();
Map::BST::BST(const BST& rhs)
{
if (rhs.root == nullptr) {
root = nullptr;
return;
}
auxCopyConstructor(root, rhs.root);
}
void Map::BST::auxCopyConstructor(Node*& lhsN, Node* rhsN)
{
if (rhsN == nullptr)
return;
lhsN = new Node(rhsN->key, rhsN->val);
auxCopyConstructor(lhsN->left, rhsN->left);
auxCopyConstructor(lhsN->right, rhsN->right);
}
/*
Map::BST& Map::BST::operator=(const BST& rhs) { return *this; }
void Map::BST::auxAssignmentOperator(Node*& lhs, Node* rhs) { }
*/
Map::BST::~BST()
{
auxDestructor(root);
}
void Map::BST::auxDestructor(Node* currentNode)
{
if (currentNode == nullptr)
return;
auxDestructor(currentNode->left);
auxDestructor(currentNode->right);
delete currentNode;
}
bool Map::BST::add(KeyType key, ValueType val)
{
if (root == nullptr) {
root = new Node(key, val);
return true;
}
return auxAdd(key, val, root);
}
bool Map::BST::auxAdd(KeyType key, ValueType val, Node*& currentNode)
{
if (currentNode == nullptr) {
currentNode = new Node(key, val);
return true;
}
// Left Traversal
if (currentNode->key > key) {
if (currentNode->left == nullptr) {
currentNode->left = new Node(key, val);
return false;
}
else {
return auxAdd(key, val, currentNode->left);
}
}
// Right Traversal
else if (currentNode->key < key) {
if (currentNode->right == nullptr) {
currentNode->right = new Node(key, val);
return false;
}
else {
return auxAdd(key, val, currentNode->right);
}
}
// Duplicate key
else if (currentNode->key == key) {
return false;
}
return false;
}
Map::Node* Map::BST::findNode(KeyType key)
{
return auxFindNode(key, root);
}
Map::Node* Map::BST::auxFindNode(KeyType key, Node* currentNode)
{
if (currentNode == nullptr || key == currentNode->key) {
return currentNode;
}
// Search left
if (key < currentNode->key) {
return auxFindNode(key, currentNode->left);
}
// Search right
return auxFindNode(key, currentNode->right);
}
Map::Node* Map::BST::findIndex(int& i, KeyType& key, ValueType& value, Node* currentNode) const {
if (currentNode == nullptr || i == 0)
return currentNode;
Node* leftResult = findIndex(i, key, value, currentNode->left);
if (leftResult != nullptr)
return leftResult;
if (i == 0) {
key = currentNode->key;
value = currentNode->val;
return currentNode;
}
// In-order Traversal
i--;
return findIndex(i, key, value, currentNode->right);
}
bool Map::BST::removeNode(const KeyType& key, Node*& currentNode)
{
if (currentNode == nullptr) {
//std::cout << key << " is not in tree\n";
return false;
}
if (currentNode->key == key) {
// Case 1: No children
if (currentNode->left == nullptr && currentNode->right == nullptr) {
delete currentNode;
currentNode = nullptr;
//std::cout << "\nNo Children: " << key << std::endl;
return true;
}
// Case 2: one child
if (currentNode->left == nullptr || currentNode->right == nullptr) {
//std::cout << "\nOne Child: " << key << std::endl;
Node* temp;
// currentNode has a left child
if (currentNode->left != nullptr)
temp = currentNode->left;
// currentNode has a right child
else
temp = currentNode->right;
delete currentNode;
currentNode = temp;
return true;
}
// Case 3: Two children
// std::cout << "\nTwo Child: " << key << std::endl;
Node* tempParent = currentNode; // Track the parent of the leftmost node
Node* temp = auxRemoveNodeTwoChildrenCase(currentNode->right, tempParent);
currentNode->key = temp->key;
currentNode->val = temp->val;
// Remove the temp node
if (tempParent->left == temp)
tempParent->left = temp->right;
else if (tempParent->right == temp)
tempParent->right = temp->right;
delete temp;
return true;
}
// split up return statement so we don't search both subtrees unecessarily
if (removeNode(key, currentNode->left)) return true;
if (removeNode(key, currentNode->right)) return true;
return false;
}
Map::Node* Map::BST::auxRemoveNodeTwoChildrenCase(Node*& n, Node*& parent)
{
// Finds leftmost in subtree
// Remove from tree
// return its pointer to be deleted in auxRemove
while (n->left != nullptr) {
parent = n;
n = n->left;
}
// Return leftmost node
return n;
}
// HashTable Functions:
// HashTable() constructor defined in Map.h
/*
// Hashtable assignment operator not required for implementation not required for this project
Map::HashTable& Map::HashTable::operator=(const HashTable& rhs)
{
// TODO: insert return statement here
}
*/
Map::HashTable::~HashTable()
{
//go through entire hashtable
for (int index = 0; index < totalSize; index++) {
if (list[index] == nullptr)
continue;
// Delete the BST itself
// Prevent dangling pointer -- I don't think it's neccessary but just in case
delete list[index];
list[index] = nullptr;
}
}
int Map::HashTable::hash(KeyType key) const {
int index = 0;
index = Convert_Key(key);
return abs(index % totalSize);
}
int Map::HashTable::Convert_Key(std::string key) const{
int hashResult = 0;
for (int i = 0; i < key.size(); i++) {
hashResult += key[i];
}
hashResult *= 97;
return hashResult;
}
int Map::HashTable::Convert_Key(double key) const{
int hashResult = int(97 - key) % totalSize;
return abs(hashResult);
}
// Map Functions:
// Map() constructor defined in Map.h
Map::Map(const Map& other)
{
totalValues = other.totalValues;
for (int i = 0; i < hashTable.totalSize; i++) {
if ((other.hashTable.list)[i] == nullptr) {
(hashTable.list)[i] = nullptr;
continue;
}
(hashTable.list)[i] = new BST();
(hashTable.list)[i]->auxCopyConstructor((hashTable.list)[i]->root, (other.hashTable.list)[i]->root);
}
}
const Map& Map::operator=(const Map& rhs)
{
if (this == &rhs) //prevent self-copy
return *this;
// Delete current values
for (int index = 0; index < hashTable.totalSize; index++) {
if (hashTable.list[index] == nullptr)
continue;
// Delete the BST -- BST has its own destructor so this will prevent memory leak
delete hashTable.list[index];
hashTable.list[index] = nullptr; // Prevent dangling pointer
}
// Copy in new values
totalValues = rhs.totalValues;
for (int i = 0; i < hashTable.totalSize; i++) {
if ((rhs.hashTable.list)[i] == nullptr) {
(hashTable.list)[i] = nullptr; // Handled in deletion
continue;
}
(hashTable.list)[i] = new BST();
(hashTable.list)[i]->auxCopyConstructor((hashTable.list)[i]->root, (rhs.hashTable.list)[i]->root);
}
return *this;
}
Map::~Map()
{
// struct HashTable holds all the dynamically allocated memory.
// Therefore cleanup is only necessary when hashTable is deleted
}
int Map::size() const {
return totalValues;
}
bool Map::empty() const
{
return totalValues == 0;
}
bool Map::insert(const KeyType& key, const ValueType& value) {
// check for duplicate keys
int index = hashTable.hash(key);
if (hashTable.list[index] != nullptr && hashTable.list[index]->findNode(key) != nullptr) {
return false; // Check for duplicates before allocating memory
}
if (hashTable.list[index] == nullptr) {
hashTable.list[index] = new BST();
}
//std::cout << "Key " << key << " Index: " << index << std::endl;
hashTable.list[index]->add(key, value);
totalValues++;
return true;
}
bool Map::update(const KeyType& key, const ValueType& value)
{
int index = hashTable.hash(key);
// return false if there is no BST at index
if (hashTable.list[index] == nullptr)
return false;
// check that the tree *has* value
Node* temp = hashTable.list[index]->findNode(key);
if (temp != nullptr) {
temp->val = value;
return true;
}
return false;
}
bool Map::insertOrUpdate(const KeyType& key, const ValueType& value)
{
int index = hashTable.hash(key);
Node* temp = nullptr;
if (hashTable.list[index] != nullptr)
temp = hashTable.list[index]->findNode(key);
if (temp == nullptr) {
insert(key, value);
}
else if (temp != nullptr) {
temp->val = value;
}
return true;
}
bool Map::erase(const KeyType& key)
{
int index = hashTable.hash(key);
if (hashTable.list[index] == nullptr) {
//std::cout << key << " is in nullptr(empty) bucket\n";
return false;
}
bool successfulErasure = (hashTable.list[index])->removeNode(key, hashTable.list[index]->root);
if (successfulErasure)
totalValues--;
return successfulErasure;
}
bool Map::contains(const KeyType& key) const
{
int index = hashTable.hash(key);
if (hashTable.list[index] == nullptr)
return false;
Node* temp = hashTable.list[index]->findNode(key);
if (temp == nullptr) {
return false;
}
return true;
}
bool Map::get(const KeyType& key, ValueType& value) const
{
int index = hashTable.hash(key);
if (hashTable.list[index] == nullptr)
return false;
Node* temp = hashTable.list[index]->findNode(key);
if (temp == nullptr) {
return false;
}
value = temp->val;
return true;
}
bool Map::get(int i, KeyType& key, ValueType& value) const
{
if (i >= totalValues || i > totalValues)
return false;
//search through hashtable BSTs
for (int hashIndex = 0; hashIndex < hashTable.totalSize; hashIndex++) {
if (hashTable.list[hashIndex] == nullptr)
continue;
Node* temp = hashTable.list[hashIndex]->findIndex(i, key, value, hashTable.list[hashIndex]->root);
if (temp == nullptr)
continue;
if (temp != nullptr && i <= 0) {
value = temp->val;
key = temp->key;
return true;
};
}
return false;
}
void Map::swap(Map& other)
{
// Swap the entire list vectors
std::vector<BST*> tempList = hashTable.list;
hashTable.list = other.hashTable.list;
other.hashTable.list = tempList;
// Swap totalValues
int tempTotalValues = totalValues;
totalValues = other.totalValues;
other.totalValues = tempTotalValues;
}
bool combine(const Map& m1, const Map& m2, Map& result) {
result = Map();
bool success = true;
// First pass: go through m1
for (int i = 0; i < m1.size(); i++) {
KeyType k;
ValueType v1;
m1.get(i, k, v1);
ValueType v2;
// key also exists in m2
if (m2.get(k, v2)) {
if (v1 == v2) {
// same value
result.insert(k, v1);
}
// conflict
// do not insert the key at all
else {
success = false;
}
}
// unique to m1
else {
result.insert(k, v1);
}
}
// Second pass: find keys in m2 that werent already handled
for (int i = 0; i < m2.size(); i++) {
KeyType k;
ValueType v;
m2.get(i, k, v);
// unique to m2
if (!m1.contains(k)) {
result.insert(k, v);
}
}
return success;
}
void subtract(const Map& m1, const Map& m2, Map& result) {
// clear result
result = Map();
for (int i = 0; i < m1.size(); i++) {
KeyType k;
ValueType v;
m1.get(i, k, v);
// Only add if key not in m2
if (!m2.contains(k)) {
result.insert(k, v);
}
}
}