-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashmap.hpp
67 lines (54 loc) · 1.47 KB
/
hashmap.hpp
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
#include <functional>
#include <array>
#include <forward_list>
#include <cinttypes>
#include <algorithm>
template<typename T, typename comp_t=std::equal_to<T>, uint64_t arrsize = 1307>
class HashMap {
using hashfunc_t = std::function<uint64_t(const T&)>;
using list_t = std::forward_list<T>;
using array_t = std::array<list_t, arrsize>;
private:
array_t arr;
const hashfunc_t hash;
const comp_t comp;
public:
HashMap(hashfunc_t hashfunc) : hash(hashfunc), comp() {}
void insert(const T& val) noexcept {
const uint64_t id = hash(val) % arrsize;
list_t& list = arr[id];
for (auto it = list.begin(); it != list.end(); ++it)
if (comp(*it, val))
return;
list.push_front(val);
}
void erase(const T& val) noexcept {
const uint64_t id = hash(val) % arrsize;
list_t& list = arr[id];
auto prev = list.before_begin();
for (auto it = list.begin(); it != list.end(); ++it) {
if (comp(*it, val)) {
list.erase_after(prev);
return;
}
++prev;
}
}
bool contains(const T& val) const noexcept {
const uint64_t id = hash(val) % arrsize;
const list_t& list = arr[id];
for (auto it = list.begin(); it != list.end(); ++it)
if (comp(*it, val))
return true;
return false;
}
uint64_t get_arrsize() const noexcept {
return arrsize;
}
std::array<uint64_t, arrsize> get_stats() {
std::array<uint64_t, arrsize> retval;
for (uint64_t i = 0; i < arrsize; ++i)
retval[i] = std::distance(arr[i].begin(), arr[i].end());
return retval;
}
};