-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDSHashTable.h
322 lines (283 loc) · 7.31 KB
/
DSHashTable.h
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
//
// Created by Sammy Timmins on 11/15/20.
//
/**
* All made by Sam Timmins
*/
#ifndef SEARCH_ENGINE_DSHASHTABLE_H
#define SEARCH_ENGINE_DSHASHTABLE_H
#include <list>
#include <vector>
#include <iostream>
template <typename Key, typename Value>
class DSHashTable
{
private:
int size = 50000;
int count = 0;
std::vector<std::list<std::pair<Key, Value>>> table;
int hashFunction(Key key);
void resize();
public:
DSHashTable();
~DSHashTable() = default;
DSHashTable(const DSHashTable ©);
DSHashTable& operator=(const DSHashTable ©);
Value& operator[](Key keyToGet);
void insert(const std::pair<Key, Value> pair);
void insert(const Key &key, const Value value);
void remove(const std::pair<Key, Value> pair);
Value& get(const Key &keyToGet);
bool find(const Key &keyToFind, const Value &valueToFind);
bool find (const Key &keyToFind);
int getSize();
int getCount();
int getHash(const Key &keyToGet);
void clear();
void outputJSON(std::ostream &os);
};
/**
* calculates the hash value of the key
*/
template <typename Key, typename Value>
int DSHashTable<Key, Value>::hashFunction(Key key)
{
std::hash<Key> hasher;
size_t hashValue = hasher(key);
int x = hashValue % size;
return x;
}
/**
* resize function
* resizes the hash table when the proportion of count to size == 1
* hash table size doubles
*/
template<typename Key, typename Value>
void DSHashTable<Key, Value>::resize()
{
std::vector<std::list<std::pair<Key, Value>>> temp(size);
for(int i = 0; i < size; i++)
{
temp[i] = table[i];
}
int oldSize = size;
size *= 2;
table.clear();
count = 0;
table = std::vector<std::list<std::pair<Key, Value>>>(size);
for(int i = 0; i < oldSize; i++)
{
typename std::list<std::pair<Key, Value>>::iterator it;
for(it = temp[i].begin(); it != temp[i].end(); it++)
{
std::pair<Key, Value> rehash = *it;
this->insert(rehash);
}
}
}
/**
* default constructor
* initializes hash table size to 50,000
*/
template <typename Key, typename Value>
DSHashTable<Key, Value>::DSHashTable()
{
table = std::vector<std::list<std::pair<Key, Value>>>(size);
}
/**
* copy constructor
*/
template<typename Key, typename Value>
DSHashTable<Key, Value>::DSHashTable(const DSHashTable ©)
{
size = copy.size;
count = copy.count;
table = copy.table;
}
/**
* overloaded assignment operator
*/
template<typename Key, typename Value>
DSHashTable<Key, Value> &DSHashTable<Key, Value>::operator=(const DSHashTable ©)
{
size = copy.size;
count = copy.count;
table = copy.table;
return *this;
}
/**
* returns the value at the given key
*/
template<typename Key, typename Value>
Value &DSHashTable<Key, Value>::operator[](Key keyToGet)
{
int index = hashFunction(keyToGet);
typename std::list<std::pair<Key, Value>>::iterator it;
for(it = table[index].begin(); it != table[index].end(); it++)
{
if(keyToGet == it->first)
{
return it->second;
}
}
std::pair<Key, Value> newPair;
newPair.first = keyToGet;
this->insert(newPair);
return newPair.second;
}
/**
* inserts the key value pair into the table
*/
template<typename Key, typename Value>
void DSHashTable<Key, Value>::insert(const std::pair<Key, Value> pair)
{
int index = hashFunction(pair.first);
table[index].push_back(pair);
count++;
if((double)count / (double)size == 1.0)
{
resize();
}
}
/**
* inserts key value pair to table
*/
template<typename Key, typename Value>
void DSHashTable<Key, Value>::insert(const Key &key, const Value value)
{
std::pair<Key, Value> insert(key, value);
this->insert(insert);
}
/**
* removes key value pair from the table
*/
template<typename Key, typename Value>
void DSHashTable<Key, Value>::remove(const std::pair<Key, Value> pair)
{
int index = hashFunction(pair.first);
table[index].remove(pair);
count--;
}
/**
* returns the key value pair object from the table
*/
template<typename Key, typename Value>
Value& DSHashTable<Key, Value>::get(const Key &keyToGet)
{
int index = hashFunction(keyToGet);
typename std::list<std::pair<Key, Value>>::iterator it;
for(it = table[index].begin(); it != table[index].end(); it++)
{
if(keyToGet == it->first)
{
return it->second;
}
}
std::pair<Key, Value> newPair;
newPair.first = keyToGet;
this->insert(newPair);
return newPair.second;
}
/**
* returns a bool for if the key value pair exists
*/
template<typename Key, typename Value>
bool DSHashTable<Key, Value>::find(const Key &keyToFind, const Value &valueToFind) {
int index = hashFunction(keyToFind);
typename std::list<std::pair<Key, Value>>::iterator it;
for(it = table[index].begin(); it != table[index].end(); it++)
{
if(keyToFind == it->first)
{
if(valueToFind == it->second)
{
return true;
}
}
}
return false;
}
/**
* returns a bool for if the key exists
*/
template<typename Key, typename Value>
bool DSHashTable<Key, Value>::find(const Key &keyToFind) {
int index = hashFunction(keyToFind);
typename std::list<std::pair<Key, Value>>::iterator it;
for(it = table[index].begin(); it != table[index].end(); it++)
{
if(keyToFind == it->first)
{
return true;
}
}
return false;
}
/**
* returns table size
*/
template<typename Key, typename Value>
int DSHashTable<Key, Value>::getSize()
{
return size;
}
/**
* returns number of items in the table
*/
template<typename Key, typename Value>
int DSHashTable<Key, Value>::getCount()
{
return count;
}
/**
* returns the hash value of the given key
*/
template<typename Key, typename Value>
int DSHashTable<Key, Value>::getHash(const Key &keyToGet)
{
return hashFunction(keyToGet);
}
template<typename Key, typename Value>
void DSHashTable<Key, Value>::clear()
{
count = 0;
size = 50000;
table.clear();
}
template<typename Key, typename Value>
void DSHashTable<Key, Value>::outputJSON(std::ostream &os)
{
int lastIndex;
for(lastIndex = table.size() - 1; lastIndex > 0; lastIndex--)
{
if(table[lastIndex].size() != 0)
{
break;
}
}
typename std::vector<std::list<std::pair<Key, Value>>>::iterator itr = table.begin();
for(int i = 0; i < lastIndex; i++)
{
if(itr->size() != 0)
{
typename std::list<std::pair<Key, Value>>::iterator it;
for(it = itr->begin(); it != itr->end(); it++)
{
std::pair<Key, Value> author = *it;
os << "\t\t{\n\t\t\t\"name\": \"" << author.first << "\",\n"
<< "\t\t\t\"ids\": [\n" << author.second
<< "\t\t\t]\n\t\t}," << std::endl;
}
}
itr++;
}
typename std::list<std::pair<Key, Value>>::iterator it = table[lastIndex].begin();
for(it = itr->begin(); it != itr->end(); it++)
{
std::pair<Key, Value> author = *it;
os << "\t\t{\n\t\t\t\"name\": \"" << author.first << "\",\n"
<< "\t\t\t\"ids\": [\n" << author.second
<< "\t\t\t]\n\t\t}" << std::endl;
}
}
#endif //SEARCH_ENGINE_DSHASHTABLE_H