forked from awsirkis/nemoStaticLib2.1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHASH_MAP.h
More file actions
495 lines (475 loc) · 14.3 KB
/
Copy pathHASH_MAP.h
File metadata and controls
495 lines (475 loc) · 14.3 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
#ifndef HASH_MAP_H
#define HASH_MAP_H
#if defined(__has_include)
#if __has_include(<functional>)
#include <functional>
#endif
#endif
#define _max_bucket_count 10000000
#define _max_load_factor 0.75
#include <iostream>
template <class key, class Value> class HASH_MAP
{
int _bucket_count = 100;
int array_size = 0;
public:
//-------------------------------class HASH_NODE---------------------------
// Store the keys and values of spaces in the HASH_MAP
// Uses the same key and Value types as HASH_MAP
struct HASH_NODE {
public:
key first = key();
Value second = Value();
//---------------------------(Constructor)-----------------------------
// empty constructor used when allocating new memory for buckets
HASH_NODE() {}
//---------------------------(Destructor)------------------------------
// reset values to the default values
// Assumes destructors in Key and Value will take care of themselves
~HASH_NODE() {
first = key();
second = Value();
}
};
protected:
// Store values in a publicly accessible, but still unchangeable container
// Stored using iterators for speed
// Declared after class HASH_NODE due to declaration order
HASH_NODE* buckets;
public:
//-------------------------------(Constructor)-----------------------------
// Allocates a new array of HASH_NODE to the size of _bucket_count
// Must be dynamically allocated to start with for
// the destructor to function
HASH_MAP<key, Value>() {
buckets = new HASH_NODE[_bucket_count];
};
// End Constructor
//-------------------------------(Destructor)------------------------------
// deletes buckets
// no other memory dynamically allocated
~HASH_MAP<key, Value>() {
delete[] buckets;
};
//-------------------------------(Copy Constructor)------------------------
// Makes new HASH_MAP<key, Value> based on rhs
// Deletes prior version of lhs.buckets
// O(n) all cases
HASH_MAP<key, Value>(const HASH_MAP<key, Value>&H) {
_bucket_count = H._bucket_count;
array_size = H.array_size;
HASH_NODE* temp = new HASH_NODE[H._bucket_count + 1];
auto i = H.begin(), t_start = &temp[0], t = t_start;
for (int j = 0; i < end() && j < H._bucket_count; j++, i++) {
if (i->first != key()) {
t = t_start + getStart(i->first);
t->first = i->first;
t->second = i->second;
}
}
std::swap(temp, buckets); // swap addresses
delete[] temp;
};
HASH_MAP<key, Value>(HASH_MAP<key, Value>&H) {
_bucket_count = H._bucket_count;
array_size = H.array_size;
buckets = H.buckets;
H._bucket_count = 100;
H.array_size = 0;
H.buckets = new HASH_NODE[100];
}
//-------------------------------operator=---------------------------------
// Copies the data from rhs into lhs
// Deletes prior version of lhs.buckets
// O(n) all cases
HASH_MAP<key, Value> operator=(const HASH_MAP<key, Value>& H) {
if (this != &H) {
_bucket_count = H._bucket_count;
array_size = H.array_size;
HASH_NODE* temp = new HASH_NODE[H._bucket_count + 1];
auto i = H.begin(), t_start = &temp[0], t = t_start;
for (int j = 0; i < H.end() && j < H._bucket_count; j++, i++) {
if (i->first != key()) {
t = t_start + getStart(i->first);
t->first = i->first;
t->second = i->second;
}
}
std::swap(temp, buckets); // swap addresses
delete[] temp;
}
return *this;
};
//-------------------------------operator=---------------------------------
// Moves data from rhs into lhs
// Resets rhs
// O(1) all cases
HASH_MAP<key, Value> operator=(HASH_MAP<key, Value> && H) {
if (this != &H) {
_bucket_count = H._bucket_count;
array_size = H.array_size;
delete[] buckets;
buckets = H.buckets;
H._bucket_count = 100;
H.array_size = 0;
H.buckets = new HASH_NODE[100];
}
return *this;
}
//-------------------------------size--------------------------------------
// Returns number of successful insertions
// O(1) all cases
inline int size() const noexcept {
return array_size;
};
//-------------------------------operator[]--------------------------------
// Iterates through to find the Value associated with key k
// returns the value found, if any
// else insert the key and return a reference to that location
// O(1) Average case, O(n) worst case (full map)
Value& operator[](const key& k) noexcept {
auto ptr = begin() + getStart(k);
count_loop:
for (ptr; ptr < end(); ptr++)
if (ptr->first == k)
return ptr->second;
else if (ptr->first == key()) {
array_size++;
ptr->first = k;
if (load_factor() > max_load_factor()) {
rehash();
}
return this->at(k);
}
if (ptr == end()) {
ptr = begin();
goto count_loop;
}
};
//-------------------------------operator[]--------------------------------
// Iterates through to find the Value associated with rhs
// returns the value found, if any
// else return an empty Value
// O(1) Average case, O(n) worst case (full map)
Value& operator[](key&& k) noexcept {
auto ptr = begin() + getStart(k);
count_loop:
for (ptr; ptr < end(); ptr++)
if (ptr->first == k)
return ptr->second;
else if (ptr->first == key()) {
ptr->first = k;
array_size++;
if (load_factor() > max_load_factor()) {
rehash();
}
return this->at(k);
}
if (ptr == end()) {
ptr = begin();
goto count_loop;
}
};
//-------------------------------at----------------------------------------
// Iterates through to find the Value associated with rhs
// returns the value found, if any
// else return an empty Value
// O(1) Average case, O(n) worst case (full map)
Value& at(const key& k) noexcept {
auto ptr = begin() + getStart(k);
count_loop:
for (ptr; ptr < end(); ptr++)
if (ptr->first == k)
return ptr->second;
else if (ptr->first == key()) {
ptr->first = k;
array_size++;
if (load_factor() > max_load_factor()) {
rehash();
}
return this->at(k);
}
if (ptr == end()) {
ptr = begin();
goto count_loop;
}
};
//-------------------------------at----------------------------------------
// Iterates through to find the Value associated with rhs
// returns the value found, if any
// else return an empty Value
// O(1) Average case, O(n) worst case (full map)
Value& at(key&& k) noexcept {
auto ptr = begin() + getStart(k);
count_loop:
for (ptr; ptr < end(); ptr++)
if (ptr->first == k)
return ptr->second;
else if (ptr->first == key()) {
ptr->first = k;
array_size++;
if (load_factor() > max_load_factor()) {
rehash();
}
return this->at(k);
}
if (ptr == end()) {
ptr = begin();
goto count_loop;
}
};
// Graph.cpp - getOrCreateIndex()
//-------------------------------count-------------------------------------
// Returns the number of times rhs is used as an index
// O(1) average case, O(n) worst case
int count(const key& k) const noexcept {
int found = 0;
int start = getStart(k);
auto ptr = begin() + start;
count_loop:
for (ptr; ptr < end(); ptr++) {
if (ptr->first == key()) {
return found;
}
if (ptr->first == k) {
found++;
}
}
if (ptr == end()) {
ptr = begin();
goto count_loop;
}
return found;
};
//-------------------------------begin-------------------------------------
// returns an iterator to the beginning of buckets
// O(1) all cases
inline auto begin() const noexcept {
return &buckets[0];
};
//-------------------------------end---------------------------------------
// returns an iterator to the end of buckets
// O(1) all cases
inline auto end() const noexcept {
return &buckets[_bucket_count];
};
//------------------------------cbegin-------------------------------------
// returns a constant iterator to the beginning of buckets
// O(1) all cases
const inline auto cbegin() const noexcept {
return &buckets[0];
};
//------------------------------cend---------------------------------------
// returns a constant iterator to the end of buckets
// O(1) all cases
const inline auto cend() const noexcept {
return &buckets[_bucket_count];
};
//------------------------------insert-------------------------------------
// inserts rhs into buckets
// will not insert if rhs.first is already in buckets
// O(1) average case, O(n + n^2) worst case (forced to rehash)
void insert(std::pair<key, Value> p) noexcept {
//if (count(p.first))
// return;
array_size++;
int start = getStart(p.first);
auto ptr = begin() + start;
insert_loop:
for (ptr; ptr < end(); ptr++) {
// if ptr reaches the end, go to the beginning
if (ptr->first == key()) {
ptr->first = p.first;
ptr->second = p.second;
break;
}
}
if (ptr == end()) {
ptr = begin();
goto insert_loop;
}
// if the load factor is too high
if (load_factor() > max_load_factor()) {
rehash();
}
int z = 0;
};
//------------------------------bucket_count-------------------------------
// returns the size of buckets
// O(1) all cases
inline size_t bucket_count() const noexcept {
return _bucket_count;
};
//------------------------------max_bucket_count---------------------------
// returns the maximum number of buckets allowed
// O(1) all cases
inline size_t max_bucket_count() const noexcept {
return _max_bucket_count;
};
//------------------------------load_factor--------------------------------
// returns the percentage of buckets that is filled
// O(1) all cases
inline double load_factor() const noexcept {
return size() / (double)bucket_count();
};
//------------------------------max_load_factor----------------------------
// returns the maximum percentage of buckets that are allowed to be filled
// O(1) all cases
inline float max_load_factor() const noexcept {
return _max_load_factor;
};
//------------------------------rehash-------------------------------------
// resizes buckets to twice the current size of _max_bucket_count,
// whichever is lower
// O(n) average case, O(n^2) worst case
void rehash() noexcept {
if (_bucket_count == _max_bucket_count) {
return;
}
if (_bucket_count * 2 < _max_bucket_count) {
_bucket_count *= 2;
}
else if (_bucket_count < _max_bucket_count) {
_bucket_count = _max_bucket_count;
}
else {
std::cerr << "<<<<ERROR: MEMORY OVERFLOW, OVER 1,000,000 ELEMENTS INSERTED>>>>" << std::endl;
}
HASH_NODE* temp = new HASH_NODE[_bucket_count + 1];
auto t_start = &temp[0], t_end = &temp[_bucket_count];
auto END = begin() + (_bucket_count / 2);
for (auto i = begin(); i < END; i++) {
if (i->first != key()) {
auto ptr = t_start + getStart(i->first);
rehash_loop:
for (ptr; ptr < t_end; ptr++) {
if (ptr->first == key()) {
ptr->first = i->first;
ptr->second = i->second;
break;
}
}
if (ptr == t_end) {
ptr = t_start;
goto rehash_loop;
}
}
}
std::swap(temp, buckets);
delete[] temp;
};
//------------------------------getStart-----------------------------------
// return an int for the start of accessors, insert() and count()
// O(1) all cases
inline int getStart(const key& k) const noexcept {
return std::hash<key>{}(k) % _bucket_count;
};
//-------------------------------contains----------------------------------
// Returns if rhs is used as an index
// O(1) average case, O(n) worst case
bool contains( key&& k) const noexcept {
int found = 0;
int start = getStart(k);
auto ptr = begin() + start;
count_loop:
for (ptr; ptr < end(); ptr++) {
if (ptr->first == key()) {
return false;
}
if (ptr->first == k) {
return true;
}
}
if (ptr == end()) {
ptr = begin();
goto count_loop;
}
};// end contains
//-------------------------------empty-------------------------------------
// check if the container is empty
// O(1) all cases
inline bool empty() const noexcept{
return !array_size;
};// end empty
//-------------------------------find--------------------------------------
// Check the container for elements
// Returns the element address
// O(1) average case, O(n) worst case
HASH_NODE* find(const key& k){
int found = 0;
int start = getStart(k);
auto ptr = begin() + start;
count_loop:
for (ptr; ptr < end(); ptr++) {
if (ptr->first == key()) {
return end();
}
if (ptr->first == k) {
return ptr;
}
}
if (ptr == end()) {
ptr = begin();
goto count_loop;
}
};// end find
//-------------------------------find--------------------------------------
// Check the container for elements
// Returns the element address
// O(1) average case, O(n) worst case
const auto find(const key& k) const noexcept{
int found = 0;
int start = getStart(k);
auto ptr = begin() + start;
count_loop:
for (ptr; ptr < end(); ptr++) {
if (ptr->first == key()) {
return end();
}
if (ptr->first == k) {
return ptr;
}
}
if (ptr == end()) {
ptr = begin();
goto count_loop;
}
};// end find
//-------------------------------reserve-----------------------------------
// Reserve the memory necessary for rhs number of buckets
// O(n) average case, O(n^2) worst case
void reserve(int& i) noexcept {
HASH_NODE* temp = new HASH_NODE[i / _max_load_factor + 1];
auto t_start = &temp[0], t_end = &temp[i / _max_load_factor + 1];
auto END = begin() + (_bucket_count);
for (auto i = begin(); i < END; i++) {
if (i->first != key()) {
auto ptr = t_start + getStart(i->first);
rehash_loop:
for (ptr; ptr < t_end; ptr++) {
if (ptr->first == key()) {
ptr->first = i->first;
ptr->second = i->second;
break;
}
}
if (ptr == t_end) {
ptr = t_start;
goto rehash_loop;
}
}
}
std::swap(temp, buckets);
_bucket_count = i / _max_load_factor + 1;
delete[] temp;
}; // end reserve
//-------------------------------swap--------------------------------------
// swaps the addresses of lhs and rhs
// O(1) all cases
void swap(HASH_MAP<key, Value> & other) {
HASH_MAP<key, Value>* temp = &other;
other = *this;
this = temp;
};// end swap
}; //end HASH_MAP
#endif