-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTable.cpp
More file actions
executable file
·208 lines (186 loc) · 4.22 KB
/
HashTable.cpp
File metadata and controls
executable file
·208 lines (186 loc) · 4.22 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
/**********************************
* FILE NAME: HashTable.cpp
*
* DESCRIPTION: Hash Table class definition (Revised 2020)
**********************************/
#include "HashTable.h"
HashTable::HashTable() {}
HashTable::~HashTable() {}
/**
* FUNCTION NAME: create
*
* DESCRIPTION: This function inserts they (key,value) pair into the local hash table
*
* RETURNS:
* true on SUCCESS
* false in FAILURE
*/
bool HashTable::create(string key, string value) {
hashTable.emplace(key, value);
return true;
}
/**
* FUNCTION NAME: read
*
* DESCRIPTION: This function searches for the key in the hash table
*
* RETURNS:
* string value if found
* else it returns a NULL
*/
string HashTable::read(string key) {
map<string, string>::iterator search;
search = hashTable.find(key);
if ( search != hashTable.end() ) {
// Value found
return search->second;
}
else {
// Value not found
return "";
}
}
/**
* FUNCTION NAME: update
*
* DESCRIPTION: This function updates the given key with the updated value passed in
* if the key is found
*
* RETURNS:
* true on SUCCESS
* false on FAILURE
*/
bool HashTable::update(string key, string newValue) {
map<string, string>::iterator update;
if (read(key).empty()) {
// Key not found
return false;
}
// Key found
//update = hashTable.at(key) = newValue;
hashTable.at(key) = newValue;
// Update successful
return true;
}
/**
* FUNCTION NAME: deleteKey
*
* DESCRIPTION: This function deletes the given key and the corresponding value if the key is found
*
* RETURNS:
* true on SUCCESS
* false on FAILURE
*/
bool HashTable::deleteKey(string key) {
uint eraseCount = 0;
if (read(key).empty()) {
// Key not found
return false;
}
eraseCount = hashTable.erase(key);
if ( eraseCount < 1 ) {
// Could not erase
return false;
}
// Delete was successful
return true;
}
/**
* FUNCTION NAME: isEmpty
*
* DESCRIPTION: Returns if the hash table is empty
*
* RETURNS:
* true if hash table is empty
* false otherwise
*/
bool HashTable::isEmpty() {
return hashTable.empty();
}
/**
* FUNCTION NAME: currentSize
*
* DESCRIPTION: Returns the current size of the hash table
*
* RETURNS:
* size of the table as unit
*/
unsigned long HashTable::currentSize() {
return (unsigned long)hashTable.size();
}
/**
* FUNCTION NAME: clear
*
* DESCRIPTION: Clear all contents from the hash table
*/
void HashTable::clear() {
hashTable.clear();
}
/**
* FUNCTION NAME: count
*
* DESCRIPTION: Returns the count of the number of values for the passed in key
*
* RETURNS:
* unsigned long count (Should be always 1)
*/
unsigned long HashTable::count(string key) {
return (unsigned long) hashTable.count(key);
}
/**
* FUNCTION: retPrimaryPairs
*
* DESCRIPTION: This function returns all KV pairs that stores primary data copies
*
* RETURN:
* vector<string>
*/
vector<pair<string, string>> HashTable::retPrimaryPairs() {
vector<pair<string, string>> primaryPairs;
for ( auto it = hashTable.begin(); it != hashTable.end(); ++it ) {
Entry entry(it->second);
if ( PRIMARY == entry.replica ) {
pair<string, string> temp= make_pair(it->first, entry.value);
primaryPairs.emplace_back(temp);
}
}
return primaryPairs;
}
/**
* FUNCTION: retSecondaryPairs
*
* DESCRIPTION: This function returns all KV pairs that stores secondary data copies
*
* RETURN:
* vector<string>
*/
vector<pair<string, string>> HashTable::retSecondaryPairs() {
vector<pair<string, string>> secondaryPairs;
for ( auto it = hashTable.begin(); it != hashTable.end(); ++it ) {
Entry entry(it->second);
if ( SECONDARY == entry.replica ) {
pair<string, string> temp= make_pair(it->first, entry.value);
secondaryPairs.emplace_back(temp);
}
}
return secondaryPairs;
}
/**
* FUNCTION: retTertiaryPairs
*
* DESCRIPTION: This function returns all KV pairs that stores tertiary data copies
*
* RETURN:
* vector<string>
*/
vector<pair<string, string>> HashTable::retTertiaryPairs() {
vector<pair<string, string>> tertiaryPairs;
for ( auto it = hashTable.begin(); it != hashTable.end(); ++it ) {
Entry entry(it->second);
if ( TERTIARY == entry.replica ) {
pair<string, string> temp= make_pair(it->first, entry.value);
tertiaryPairs.emplace_back(temp);
}
}
return tertiaryPairs;
}