-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhash_map.hpp
612 lines (515 loc) · 19.5 KB
/
hash_map.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
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
// By Emil Ernerfeldt 2014-2016
// LICENSE:
// This software is dual-licensed to the public domain and under the following
// license: you are granted a perpetual, irrevocable license to copy, modify,
// publish, and distribute this file as you see fit.
#pragma once
#include <cstdlib>
#include <iterator>
#include <utility>
namespace emilib {
/// like std::equal_to but no need to #include <functional>
template<typename T>
struct HashMapEqualTo
{
constexpr bool operator()(const T &lhs, const T &rhs) const
{
return lhs == rhs;
}
};
/// A cache-friendly hash table with open addressing, linear probing and power-of-two capacity
template <typename KeyT, typename ValueT, typename HashT = std::hash<KeyT>, typename CompT = HashMapEqualTo<KeyT>>
class HashMap
{
private:
using MyType = HashMap<KeyT, ValueT, HashT, CompT>;
using PairT = std::pair<KeyT, ValueT>;
public:
using size_type = size_t;
using value_type = PairT;
using reference = PairT&;
using const_reference = const PairT&;
class iterator
{
public:
using iterator_category = std::forward_iterator_tag;
using difference_type = size_t;
using distance_type = size_t;
using value_type = std::pair<KeyT, ValueT>;
using pointer = value_type*;
using reference = value_type&;
iterator() { }
iterator(MyType* hash_map, size_t bucket) : _map(hash_map), _bucket(bucket)
{
}
iterator& operator++()
{
this->goto_next_element();
return *this;
}
iterator operator++(int)
{
size_t old_index = _bucket;
this->goto_next_element();
return iterator(_map, old_index);
}
reference operator*() const
{
return _map->_pairs[_bucket];
}
pointer operator->() const
{
return _map->_pairs + _bucket;
}
bool operator==(const iterator& rhs)
{
//DCHECK_EQ_F(_map, rhs._map);
return this->_bucket == rhs._bucket;
}
bool operator!=(const iterator& rhs)
{
//DCHECK_EQ_F(_map, rhs._map);
return this->_bucket != rhs._bucket;
}
private:
void goto_next_element()
{
//DCHECK_LT_F(_bucket, _map->_num_buckets);
do {
_bucket++;
} while (_bucket < _map->_num_buckets && _map->_states[_bucket] != State::FILLED);
}
//private:
//friend class MyType;
public:
MyType* _map;
size_t _bucket;
};
class const_iterator
{
public:
using iterator_category = std::forward_iterator_tag;
using difference_type = size_t;
using distance_type = size_t;
using value_type = const std::pair<KeyT, ValueT>;
using pointer = value_type*;
using reference = value_type&;
const_iterator() { }
const_iterator(iterator proto) : _map(proto._map), _bucket(proto._bucket)
{
}
const_iterator(const MyType* hash_map, size_t bucket) : _map(hash_map), _bucket(bucket)
{
}
const_iterator& operator++()
{
this->goto_next_element();
return *this;
}
const_iterator operator++(int)
{
size_t old_index = _bucket;
this->goto_next_element();
return const_iterator(_map, old_index);
}
reference operator*() const
{
return _map->_pairs[_bucket];
}
pointer operator->() const
{
return _map->_pairs + _bucket;
}
bool operator==(const const_iterator& rhs)
{
//DCHECK_EQ_F(_map, rhs._map);
return this->_bucket == rhs._bucket;
}
bool operator!=(const const_iterator& rhs)
{
//DCHECK_EQ_F(_map, rhs._map);
return this->_bucket != rhs._bucket;
}
private:
void goto_next_element()
{
//DCHECK_LT_F(_bucket, _map->_num_buckets);
do {
_bucket++;
} while (_bucket < _map->_num_buckets && _map->_states[_bucket] != State::FILLED);
}
//private:
//friend class MyType;
public:
const MyType* _map;
size_t _bucket;
};
// ------------------------------------------------------------------------
HashMap() = default;
HashMap(const HashMap& other)
{
reserve(other.size());
insert(cbegin(other), cend(other));
}
HashMap(HashMap&& other)
{
*this = std::move(other);
}
HashMap& operator=(const HashMap& other)
{
clear();
reserve(other.size());
insert(cbegin(other), cend(other));
return *this;
}
void operator=(HashMap&& other)
{
this->swap(other);
}
~HashMap()
{
for (size_t bucket=0; bucket<_num_buckets; ++bucket) {
if (_states[bucket] == State::FILLED) {
_pairs[bucket].~PairT();
}
}
free(_states);
free(_pairs);
}
void swap(HashMap& other)
{
std::swap(_hasher, other._hasher);
std::swap(_comp, other._comp);
std::swap(_states, other._states);
std::swap(_pairs, other._pairs);
std::swap(_num_buckets, other._num_buckets);
std::swap(_num_filled, other._num_filled);
std::swap(_max_probe_length, other._max_probe_length);
std::swap(_mask, other._mask);
}
// -------------------------------------------------------------
iterator begin()
{
size_t bucket = 0;
while (bucket<_num_buckets && _states[bucket] != State::FILLED) {
++bucket;
}
return iterator(this, bucket);
}
const_iterator begin() const
{
size_t bucket = 0;
while (bucket<_num_buckets && _states[bucket] != State::FILLED) {
++bucket;
}
return const_iterator(this, bucket);
}
iterator end()
{ return iterator(this, _num_buckets); }
const_iterator end() const
{ return const_iterator(this, _num_buckets); }
size_t size() const
{
return _num_filled;
}
bool empty() const
{
return _num_filled==0;
}
// ------------------------------------------------------------
iterator find(const KeyT& key)
{
auto bucket = this->find_filled_bucket(key);
if (bucket == (size_t)-1) {
return this->end();
}
return iterator(this, bucket);
}
const_iterator find(const KeyT& key) const
{
auto bucket = this->find_filled_bucket(key);
if (bucket == (size_t)-1)
{
return this->end();
}
return const_iterator(this, bucket);
}
bool contains(const KeyT& k) const
{
return find_filled_bucket(k) != (size_t)-1;
}
size_t count(const KeyT& k) const
{
return find_filled_bucket(k) != (size_t)-1 ? 1 : 0;
}
/// Returns the matching ValueT or nullptr if k isn't found.
ValueT* try_get(const KeyT& k)
{
auto bucket = find_filled_bucket(k);
if (bucket != (size_t)-1) {
return &_pairs[bucket].second;
} else {
return nullptr;
}
}
/// Const version of the above
const ValueT* try_get(const KeyT& k) const
{
auto bucket = find_filled_bucket(k);
if (bucket != (size_t)-1) {
return &_pairs[bucket].second;
} else {
return nullptr;
}
}
/// Convenience function.
const ValueT get_or_return_default(const KeyT& k) const
{
const ValueT* ret = try_get(k);
if (ret) {
return *ret;
} else {
return ValueT();
}
}
// -----------------------------------------------------
/// Returns a pair consisting of an iterator to the inserted element
/// (or to the element that prevented the insertion)
/// and a bool denoting whether the insertion took place.
std::pair<iterator, bool> insert(const KeyT& key, const ValueT& value)
{
check_expand_need();
auto bucket = find_or_allocate(key);
if (_states[bucket] == State::FILLED) {
return { iterator(this, bucket), false };
} else {
_states[bucket] = State::FILLED;
new(_pairs + bucket) PairT(key, value);
_num_filled++;
return { iterator(this, bucket), true };
}
}
std::pair<iterator, bool> insert(const std::pair<KeyT, ValueT>& p)
{
return insert(p.first, p.second);
}
void insert(const_iterator begin, const_iterator end)
{
for (; begin != end; ++begin) {
insert(begin->first, begin->second);
}
}
/// Same as above, but contains(key) MUST be false
void insert_unique(KeyT&& key, ValueT&& value)
{
//DCHECK_F(!contains(key));
check_expand_need();
auto bucket = find_empty_bucket(key);
_states[bucket] = State::FILLED;
new(_pairs + bucket) PairT(std::move(key), std::move(value));
_num_filled++;
}
void insert_unique(std::pair<KeyT, ValueT>&& p)
{
insert_unique(std::move(p.first), std::move(p.second));
}
/// Return the old value or ValueT() if it didn't exist.
ValueT set_get(const KeyT& key, const ValueT& new_value)
{
check_expand_need();
auto bucket = find_or_allocate(key);
// Check if inserting a new value rather than overwriting an old entry
if (_states[bucket] == State::FILLED) {
ValueT old_value = _pairs[bucket].second;
_pairs[bucket] = new_value.second;
return old_value;
} else {
_states[bucket] = State::FILLED;
new(_pairs + bucket) PairT(key, new_value);
_num_filled++;
return ValueT();
}
}
/// Like std::map<KeyT,ValueT>::operator[].
ValueT& operator[](const KeyT& key)
{
check_expand_need();
auto bucket = find_or_allocate(key);
/* Check if inserting a new value rather than overwriting an old entry */
if (_states[bucket] != State::FILLED) {
_states[bucket] = State::FILLED;
new(_pairs + bucket) PairT(key, ValueT());
_num_filled++;
}
return _pairs[bucket].second;
}
// -------------------------------------------------------
/// Erase an element from the hash table.
/// return false if element was not found
bool erase(const KeyT& key)
{
auto bucket = find_filled_bucket(key);
if (bucket != (size_t)-1) {
_states[bucket] = State::ACTIVE;
_pairs[bucket].~PairT();
_num_filled -= 1;
return true;
} else {
return false;
}
}
/// Erase an element using an iterator.
/// Returns an iterator to the next element (or end()).
iterator erase(iterator it)
{
//DCHECK_EQ_F(it._map, this);
//DCHECK_LT_F(it._bucket, _num_buckets);
_states[it._bucket] = State::ACTIVE;
_pairs[it._bucket].~PairT();
_num_filled -= 1;
return ++it;
}
/// Remove all elements, keeping full capacity.
void clear()
{
for (size_t bucket=0; bucket<_num_buckets; ++bucket) {
if (_states[bucket] == State::FILLED) {
_states[bucket] = State::INACTIVE;
_pairs[bucket].~PairT();
}
}
_num_filled = 0;
_max_probe_length = -1;
}
/// Make room for this many elements
void reserve(size_t num_elems)
{
size_t required_buckets = num_elems + num_elems/2 + 1;
if (required_buckets <= _num_buckets) {
return;
}
size_t num_buckets = 4;
while (num_buckets < required_buckets) { num_buckets *= 2; }
auto new_states = (State*)malloc(num_buckets * sizeof(State));
auto new_pairs = (PairT*)malloc(num_buckets * sizeof(PairT));
if (!new_states || !new_pairs) {
free(new_states);
free(new_pairs);
throw std::bad_alloc();
}
//auto old_num_filled = _num_filled;
auto old_num_buckets = _num_buckets;
auto old_states = _states;
auto old_pairs = _pairs;
_num_filled = 0;
_num_buckets = num_buckets;
_mask = _num_buckets - 1;
_states = new_states;
_pairs = new_pairs;
std::fill_n(_states, num_buckets, State::INACTIVE);
_max_probe_length = -1;
for (size_t src_bucket=0; src_bucket<old_num_buckets; src_bucket++) {
if (old_states[src_bucket] == State::FILLED) {
auto& src_pair = old_pairs[src_bucket];
auto dst_bucket = find_empty_bucket(src_pair.first);
//DCHECK_NE_F(dst_bucket, (size_t)-1);
//DCHECK_NE_F(_states[dst_bucket], State::FILLED);
_states[dst_bucket] = State::FILLED;
new(_pairs + dst_bucket) PairT(std::move(src_pair));
_num_filled += 1;
src_pair.~PairT();
}
}
//DCHECK_EQ_F(old_num_filled, _num_filled);
free(old_states);
free(old_pairs);
}
private:
// Can we fit another element?
void check_expand_need()
{
reserve(_num_filled + 1);
}
// Find the bucket with this key, or return nullptr
size_t find_filled_bucket(const KeyT& key) const
{
if (empty()) { return (size_t)-1; } // Optimization
auto hash_value = _hasher(key);
for (int offset=0; offset<=_max_probe_length; ++offset) {
auto bucket = (hash_value + offset) & _mask;
if (_states[bucket] == State::FILLED && _comp(_pairs[bucket].first, key)) {
return bucket;
}
if (_states[bucket] == State::INACTIVE) {
return (size_t)-1; // End of the chain!
}
}
return (size_t)-1;
}
// Find the bucket with this key, or return a good empty bucket to place the key in.
// In the latter case, the bucket is expected to be filled.
size_t find_or_allocate(const KeyT& key)
{
auto hash_value = _hasher(key);
size_t hole = (size_t)-1;
int offset=0;
for (; offset<=_max_probe_length; ++offset) {
auto bucket = (hash_value + offset) & _mask;
if (_states[bucket] == State::FILLED) {
if (_comp(_pairs[bucket].first, key)) {
return bucket;
}
} else if (_states[bucket] == State::INACTIVE) {
return bucket;
} else {
// ACTIVE: keep searching
if (hole == (size_t)-1) {
hole = bucket;
}
}
}
// No key found - but maybe a hole for it
//DCHECK_EQ_F(offset, _max_probe_length+1);
if (hole != (size_t)-1) {
return hole;
}
// No hole found within _max_probe_length
for (; ; ++offset) {
auto bucket = (hash_value + offset) & _mask;
if (_states[bucket] != State::FILLED) {
_max_probe_length = offset;
return bucket;
}
}
}
// key is not in this map. Find a place to put it.
size_t find_empty_bucket(const KeyT& key)
{
auto hash_value = _hasher(key);
for (int offset=0; ; ++offset) {
auto bucket = (hash_value + offset) & _mask;
if (_states[bucket] != State::FILLED) {
if (offset > _max_probe_length) {
_max_probe_length = offset;
}
return bucket;
}
}
}
private:
enum class State : uint8_t
{
INACTIVE, // Never been touched
ACTIVE, // Is inside a search-chain, but is empty
FILLED // Is set with key/value
};
HashT _hasher;
CompT _comp;
State* _states = nullptr;
PairT* _pairs = nullptr;
size_t _num_buckets = 0;
size_t _num_filled = 0;
int _max_probe_length = -1; // Our longest bucket-brigade is this long. ONLY when we have zero elements is this ever negative (-1).
size_t _mask = 0; // _num_buckets minus one
};
} // namespace emilib