-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashmap.hpp
329 lines (264 loc) · 9.28 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
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
// © 2017-2020 Erik Rigtorp <[email protected]>
// SPDX-License-Identifier: MIT
/*
HashMap
A high performance hash map. Uses open addressing with linear
probing.
Advantages:
- Predictable performance. Doesn't use the allocator unless load factor
grows beyond 50%. Linear probing ensures cash efficency.
- Deletes items by rearranging items and marking slots as empty instead of
marking items as deleted. This is keeps performance high when there
is a high rate of churn (many paired inserts and deletes) since otherwise
most slots would be marked deleted and probing would end up scanning
most of the table.
Disadvantages:
- Significant performance degradation at high load factors.
- Maximum load factor hard coded to 50%, memory inefficient.
- Memory is not reclaimed on erase.
*/
#pragma once
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <stdexcept>
#include <vector>
namespace rigtorp {
template <typename Key, typename T, typename Hash = std::hash<Key>,
typename KeyEqual = std::equal_to<void>,
typename Allocator = std::allocator<std::pair<Key, T>>>
class HashMap {
public:
using key_type = Key;
using mapped_type = T;
using value_type = std::pair<Key, T>;
using size_type = std::size_t;
using hasher = Hash;
using key_equal = KeyEqual;
using allocator_type = Allocator;
using reference = value_type &;
using const_reference = const value_type &;
using buckets = std::vector<value_type, allocator_type>;
template <typename ContT, typename IterVal> struct hm_iterator {
using difference_type = std::ptrdiff_t;
using value_type = IterVal;
using pointer = value_type *;
using reference = value_type &;
using iterator_category = std::forward_iterator_tag;
bool operator==(const hm_iterator &other) const {
return other.hm_ == hm_ && other.idx_ == idx_;
}
bool operator!=(const hm_iterator &other) const {
return !(other == *this);
}
hm_iterator &operator++() {
++idx_;
advance_past_empty();
return *this;
}
reference operator*() const { return hm_->buckets_[idx_]; }
pointer operator->() const { return &hm_->buckets_[idx_]; }
private:
explicit hm_iterator(ContT *hm) : hm_(hm) { advance_past_empty(); }
explicit hm_iterator(ContT *hm, size_type idx) : hm_(hm), idx_(idx) {}
template <typename OtherContT, typename OtherIterVal>
hm_iterator(const hm_iterator<OtherContT, OtherIterVal> &other)
: hm_(other.hm_), idx_(other.idx_) {}
void advance_past_empty() {
while (idx_ < hm_->buckets_.size() &&
key_equal()(hm_->buckets_[idx_].first, hm_->empty_key_)) {
++idx_;
}
}
ContT *hm_ = nullptr;
typename ContT::size_type idx_ = 0;
friend ContT;
};
using iterator = hm_iterator<HashMap, value_type>;
using const_iterator = hm_iterator<const HashMap, const value_type>;
public:
HashMap(size_type bucket_count, key_type empty_key,
const allocator_type &alloc = allocator_type())
: empty_key_(empty_key), buckets_(alloc) {
size_t pow2 = 1;
while (pow2 < bucket_count) {
pow2 <<= 1;
}
buckets_.resize(pow2, std::make_pair(empty_key_, T()));
}
HashMap(const HashMap &other, size_type bucket_count)
: HashMap(bucket_count, other.empty_key_, other.get_allocator()) {
for (auto it = other.begin(); it != other.end(); ++it) {
insert(*it);
}
}
allocator_type get_allocator() const noexcept {
return buckets_.get_allocator();
}
// Iterators
iterator begin() noexcept { return iterator(this); }
const_iterator begin() const noexcept { return const_iterator(this); }
const_iterator cbegin() const noexcept { return const_iterator(this); }
iterator end() noexcept { return iterator(this, buckets_.size()); }
const_iterator end() const noexcept {
return const_iterator(this, buckets_.size());
}
const_iterator cend() const noexcept {
return const_iterator(this, buckets_.size());
}
// Capacity
bool empty() const noexcept { return size() == 0; }
size_type size() const noexcept { return size_; }
size_type max_size() const noexcept { return buckets_.max_size() / 2; }
// Modifiers
void clear() noexcept {
for (auto &b : buckets_) {
if (b.first != empty_key_) {
b.first = empty_key_;
}
}
size_ = 0;
}
std::pair<iterator, bool> insert(const value_type &value) {
return emplace_impl(value.first, value.second);
}
std::pair<iterator, bool> insert(value_type &&value) {
return emplace_impl(value.first, std::move(value.second));
}
template <typename... Args>
std::pair<iterator, bool> emplace(Args &&... args) {
return emplace_impl(std::forward<Args>(args)...);
}
void erase(iterator it) { erase_impl(it); }
size_type erase(const key_type &key) { return erase_impl(key); }
template <typename K> size_type erase(const K &x) { return erase_impl(x); }
void swap(HashMap &other) noexcept {
std::swap(buckets_, other.buckets_);
std::swap(size_, other.size_);
std::swap(empty_key_, other.empty_key_);
}
// Lookup
mapped_type &at(const key_type &key) { return at_impl(key); }
template <typename K> mapped_type &at(const K &x) { return at_impl(x); }
const mapped_type &at(const key_type &key) const { return at_impl(key); }
template <typename K> const mapped_type &at(const K &x) const {
return at_impl(x);
}
mapped_type &operator[](const key_type &key) {
return emplace_impl(key).first->second;
}
size_type count(const key_type &key) const { return count_impl(key); }
template <typename K> size_type count(const K &x) const {
return count_impl(x);
}
iterator find(const key_type &key) { return find_impl(key); }
template <typename K> iterator find(const K &x) { return find_impl(x); }
const_iterator find(const key_type &key) const { return find_impl(key); }
template <typename K> const_iterator find(const K &x) const {
return find_impl(x);
}
// Bucket interface
size_type bucket_count() const noexcept { return buckets_.size(); }
size_type max_bucket_count() const noexcept { return buckets_.max_size(); }
// Hash policy
void rehash(size_type count) {
count = std::max(count, size() * 2);
HashMap other(*this, count);
swap(other);
}
void reserve(size_type count) {
if (count * 2 > buckets_.size()) {
rehash(count * 2);
}
}
// Observers
hasher hash_function() const { return hasher(); }
key_equal key_eq() const { return key_equal(); }
private:
template <typename K, typename... Args>
std::pair<iterator, bool> emplace_impl(const K &key, Args &&... args) {
assert(!key_equal()(empty_key_, key) && "empty key shouldn't be used");
reserve(size_ + 1);
for (size_t idx = key_to_idx(key);; idx = probe_next(idx)) {
if (key_equal()(buckets_[idx].first, empty_key_)) {
buckets_[idx].second = mapped_type(std::forward<Args>(args)...);
buckets_[idx].first = key;
size_++;
return {iterator(this, idx), true};
} else if (key_equal()(buckets_[idx].first, key)) {
return {iterator(this, idx), false};
}
}
}
void erase_impl(iterator it) {
size_t bucket = it.idx_;
for (size_t idx = probe_next(bucket);; idx = probe_next(idx)) {
if (key_equal()(buckets_[idx].first, empty_key_)) {
buckets_[bucket].first = empty_key_;
size_--;
return;
}
size_t ideal = key_to_idx(buckets_[idx].first);
if (diff(bucket, ideal) < diff(idx, ideal)) {
// swap, bucket is closer to ideal than idx
buckets_[bucket] = buckets_[idx];
bucket = idx;
}
}
}
template <typename K> size_type erase_impl(const K &key) {
auto it = find_impl(key);
if (it != end()) {
erase_impl(it);
return 1;
}
return 0;
}
template <typename K> mapped_type &at_impl(const K &key) {
iterator it = find_impl(key);
if (it != end()) {
return it->second;
}
throw std::out_of_range("HashMap::at");
}
template <typename K> const mapped_type &at_impl(const K &key) const {
return const_cast<HashMap *>(this)->at_impl(key);
}
template <typename K> size_t count_impl(const K &key) const {
return find_impl(key) == end() ? 0 : 1;
}
template <typename K> iterator find_impl(const K &key) {
assert(!key_equal()(empty_key_, key) && "empty key shouldn't be used");
for (size_t idx = key_to_idx(key);; idx = probe_next(idx)) {
if (key_equal()(buckets_[idx].first, key)) {
return iterator(this, idx);
}
if (key_equal()(buckets_[idx].first, empty_key_)) {
return end();
}
}
}
template <typename K> const_iterator find_impl(const K &key) const {
return const_cast<HashMap *>(this)->find_impl(key);
}
template <typename K>
size_t key_to_idx(const K &key) const noexcept(noexcept(hasher()(key))) {
const size_t mask = buckets_.size() - 1;
return hasher()(key) & mask;
}
size_t probe_next(size_t idx) const noexcept {
const size_t mask = buckets_.size() - 1;
return (idx + 1) & mask;
}
size_t diff(size_t a, size_t b) const noexcept {
const size_t mask = buckets_.size() - 1;
return (buckets_.size() + (a - b)) & mask;
}
private:
key_type empty_key_;
buckets buckets_;
size_t size_ = 0;
};
} // namespace rigtorp