-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash_map.cpp
More file actions
127 lines (101 loc) · 2.99 KB
/
Copy pathhash_map.cpp
File metadata and controls
127 lines (101 loc) · 2.99 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
//
// Created by Evgeny Savinov on 09/01/2017.
//
#include <bits/stdc++.h>
// Simple open-addressing hash map for integer keys.
//#pragma GCC optimize ("O3")
using namespace std;
template<class K, class V, int D = 20,
unsigned long long to_xor = 10778448979374848739ull,
unsigned long long to_mul = 14774228369217122871ull>
struct HashMap {
enum State {
free,
used
};
struct Node {
K key;
V value{V()};
State state{State::free};
};
Node nodes[1 << D];
size_t size;
size_t hash(const K& k) const {
return ((k ^ to_xor) * to_mul) & ((1 << D) - 1);
}
HashMap() {
size = 0;
}
// returns slot for key k (inserts if empty)
Node &find(const K& k) {
size_t h = hash(k);
while (nodes[h].state == State::used && nodes[h].key != k) {
h = (h + 1) & ((1 << D) - 1);
}
return nodes[h];
}
// const version of find
const Node &find(const K& k) const {
size_t h = hash(k);
while (nodes[h].state == State::used && nodes[h].key != k) {
h = (h + 1) & ((1 << D) - 1);
}
return nodes[h];
}
// access value by key (inserts new element if needed)
V& operator[](const K& k) {
Node &node = find(k);
size += node.state == State::free;
node.state = State::used;
node.key = k;
return node.value;
}
// returns true if key k exists in the map
bool count(const K& k) const {
const Node &node = find(k);
return node.state == State::used;
}
};
int main() {
HashMap<long long, long long, 21> h;
unordered_map<long long, long long> h2;
mt19937_64 gen;
const int W = 1000000;
vector <long long> keys(W);
for (auto &k : keys) k = gen();
vector <long long> values(W);
for (auto &v : values) v = gen();
auto start = clock() / 1.0 / CLOCKS_PER_SEC;
for(int i = 0; i < keys.size(); ++i) {
h[keys[i]] = values[i];
}
auto end = clock() / 1.0 / CLOCKS_PER_SEC;
cerr << "my hash write: " << (end - start) << " sec" << endl;
start = clock() / 1.0 / CLOCKS_PER_SEC;
for(int i = 0; i < keys.size(); ++i) {
h2[keys[i]] = values[i];
}
end = clock() / 1.0 / CLOCKS_PER_SEC;
cerr << "unordered hash write: " << (end - start) << " sec" << endl;
for (long long k : keys) {
assert(h[k] == h2[k]);
}
const int R = 10000000;
start = clock() / 1.0 / CLOCKS_PER_SEC;
int cnt = 0;
for(int _ = 0; _ < R; ++_) {
long long k = gen();
cnt += h.count(k);
}
end = clock() / 1.0 / CLOCKS_PER_SEC;
cerr << "my hash read: " << (end - start) << " sec" << endl;
start = clock() / 1.0 / CLOCKS_PER_SEC;
cnt = 0;
for(int _ = 0; _ < R; ++_) {
long long k = gen();
cnt -= h2.count(k);
}
end = clock() / 1.0 / CLOCKS_PER_SEC;
cerr << "unordered hash read: " << (end - start) << " sec" << endl;
assert(cnt == 0);
}