-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashMapDatabase.cpp
More file actions
79 lines (70 loc) · 1.48 KB
/
Copy pathHashMapDatabase.cpp
File metadata and controls
79 lines (70 loc) · 1.48 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
#include "HashMapDatabase.h"
// constructor
HashMapDatabase::HashMapDatabase()
{
//used for hashing function
Customers.resize(HASHSIZE);
}
// destructor
HashMapDatabase::~HashMapDatabase()
{
clear();
}
// Add a customer to the hashtable of customers
bool HashMapDatabase::add(Customer* C)
{
if (getCustomer(C->CustomerId) == nullptr)
{
Customers[C->CustomerId] = C;
return true;
}
//already inside hashmap
return false;
}
// removes a customer from the database (not tested fully)
bool HashMapDatabase::remove(int ID)
{
//might cause a memoryleak
if (getCustomer(ID) != nullptr)
{
Customer* temp = Customers[ID];
delete(temp);
Customers[ID] = nullptr;
return true;
}
//does not exist
return false;
}
// retrieves a customer object from the database
// corresponding to the correct id
Customer* HashMapDatabase::getCustomer(int ID)
{
//this works in case of nullptr too because this will return nullptr
int index = getHash(ID);
Customer* temp = Customers[index];
return temp;
}
// clears the entire Map of customer pointers
bool HashMapDatabase::clear()
{
for (auto& Customer : Customers)
{
delete Customer;
}
Customers.clear();
return true;
}
// returns the hash of the customer
// due to perfect hashing and using their ID as
// a hash, we just return their ID here
int HashMapDatabase::getHash(int ID)
{
return ID;
}
// prints all customers
void HashMapDatabase::printAllCustomers()
{
cout << "CUSTOMERS:" << endl;
for (auto X : Customers)
cout << X << endl;
}