-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash_map.h
535 lines (463 loc) · 20.4 KB
/
hash_map.h
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
/* THOR - THOR Template Library
* Joshua M. Kriegshauser
*
* hash_map.h
*
* This file defines (mostly) STL-compatible hash_map and hash_multimap associative containers
*
* Changes/Extensions:
* - Unlike STL, the ordering of items in hash_map and hash_multimap are treated like a linked list.
* * Iteration order will not change when the bucket count changes.
* * New items inserted will be added to the back of the list (i.e. end()).
* * Items can be reorganized within the list using move().
* * Since the list is independent of hashing, matching keys (for hash_multimap) will not necessarily be
* encountered sequentially when iterating. If grouped matching keys are desired, call begin as such: begin(true).
* Note that all other ordering aside from grouped keys is not guaranteed.
* - There is no template parameter for EqualsFunc. Therefore, an operator == MUST be defined
* for types used as keys.
* * Subsequently, key_equals, key_eq() and the constructors that pass key_equals are not implemented.
* - bucket_count() will always return powers-of-two whereas some other implementations use
* prime numbers. The power-of-two implementation is faster.
* - The insert(InputIterator, InputIterator) function has been renamed insert_range.
* - While insert(value_type) is supported, it is not the best way to insert elements.
* Consider using the templatized insert() functions. These functions pass up to four
* parameters directly to the constructor meaning that there is no copy construction
* of the Value. Additionally, if more than four parameters are required, insert_placement()
* can be used with placement new to construct the item.
* * In the case of hash_map, if the key/value pair already exists, the key/value pair is destroyed
* and re-constructed.
* * In the case of hash_map, the insert() functions return an iterator, so it is impossible
* to tell whether the key previously existed from the insert() function call alone.
* - Raw pointer support:
* * delete_all() will delete the Value only (not the Key) for all items in the container, followed by a clear().
* - equal_range() supports an optional count parameter
* - A PartitionPolicy can be used to control the bucketizing scheme (base2, prime, etc)
*
* hash_map/hash_multimap - Non-ordered associative containers
* Time:
* insert - constant (average; linear worst case)
* find - constant (average; linear worst case)
* erase (iterator) - constant (average; linear worst case)
* erase (key) - linear on count(k) average, linear on size() worst case
* insert (single value) - amortized constant time; linear worst case. May cause a resize()
* resize - linear
* iteration - linear
* Iterator invalidation:
* erase - invalidates only erased iterators
* insert - no iterators are invalidated
* resize - same as insert
* Usage suggestions:
* hash_map - Use for storing/retrieving unique key/value pairs
* hash_multimap - Use for storing/retrieving non-unique key/value pairs
* * If sorted order is desired, consider map/multimap which have worse time characteristics
*/
#ifndef THOR_HASH_MAP_H
#define THOR_HASH_MAP_H
#pragma once
#ifndef THOR_HASHTABLE_H
#include "hashtable.h"
#endif
#ifndef THOR_FUNCTION_H
#include "function.h"
#endif
#ifndef THOR_HASH_FUNCS_H
#include "hash_funcs.h"
#endif
#ifndef THOR_SORT_H
#include "sort.h"
#endif
#ifndef THOR_PAIR_H
#include "pair.h"
#endif
#ifndef THOR_LIST_H
#include "list.h"
#endif
#ifndef THOR_POLICY_H
#include "policy.h"
#endif
namespace thor
{
// thor::hash_map
template
<
class Key,
class Data,
class HashFunc = hash<Key>,
class PartitionPolicy = policy::base2_partition
> class hash_map
{
public:
typedef Key key_type;
typedef Data data_type;
typedef pair<const key_type, data_type> value_type;
typedef HashFunc hasher;
private:
typedef hashtable<key_type, value_type, hasher, select1st<value_type>, PartitionPolicy> hashtable_type;
hashtable_type m_hashtable;
public:
typedef typename hashtable_type::pointer pointer;
typedef typename hashtable_type::const_pointer const_pointer;
typedef typename hashtable_type::reference reference;
typedef typename hashtable_type::const_reference const_reference;
typedef thor_size_type size_type;
typedef thor_diff_type difference_type;
typedef typename hashtable_type::iterator iterator;
typedef typename hashtable_type::const_iterator const_iterator;
typedef typename hashtable_type::reverse_iterator reverse_iterator;
typedef typename hashtable_type::const_reverse_iterator const_reverse_iterator;
// constructors
hash_map()
{}
hash_map(size_type n) :
m_hashtable(n)
{}
hash_map(size_type n, const hasher& h) :
m_hashtable(n, h)
{}
template <class InputIterator> hash_map(InputIterator first, InputIterator last)
{
m_hashtable.insert_unique(first, last);
}
template <class InputIterator> hash_map(InputIterator first, InputIterator last, size_type n) :
m_hashtable(n)
{
m_hashtable.insert_unique(first, last);
}
template <class InputIterator> hash_map(InputIterator first, InputIterator last, size_type n, const hasher& h) :
m_hashtable(n, h)
{
m_hashtable.insert_unique(first, last);
}
hash_map(const hash_map& rhs) :
m_hashtable(rhs.m_hashtable)
{}
~hash_map()
{}
// iteration
iterator begin(bool mode_hash=false) { return m_hashtable.begin(mode_hash); }
iterator end() { return m_hashtable.end(); }
const_iterator begin(bool mode_hash=false) const { return m_hashtable.begin(mode_hash); }
const_iterator end() const { return m_hashtable.end(); }
reverse_iterator rbegin(bool mode_hash=false) { return m_hashtable.rbegin(mode_hash); }
reverse_iterator rend() { return m_hashtable.rend(); }
const_reverse_iterator rbegin(bool mode_hash=false) const { return m_hashtable.rbegin(mode_hash); }
const_reverse_iterator rend() const { return m_hashtable.rend(); }
// size
size_type size() const { return m_hashtable.size(); }
size_type max_size() const { return m_hashtable.max_size(); }
bool empty() const { return m_hashtable.empty(); }
size_type bucket_count() const { return m_hashtable.bucket_count(); }
void resize(size_type n) { m_hashtable.resize(n); }
const hasher& hash_funct() const { return m_hashtable.hash_funct(); }
hash_map& operator = (const hash_map& rhs) { m_hashtable = rhs.m_hashtable; return *this; }
void swap(hash_map& rhs) { m_hashtable.swap(rhs.m_hashtable); }
// insertion
pair<iterator, bool> insert(const value_type& x) { return m_hashtable.insert_unique(x); }
template <class InputIterator> void insert_range(InputIterator first, InputIterator last) { m_hashtable.insert_unique(first, last); }
// insert extensions
iterator insert(const Key& k)
{
value_type* v = m_hashtable.key_insert_unique(k);
typetraits<value_type>::construct(v, k);
return m_hashtable.iterator_from_value_type(*v);
}
template <class T1> iterator insert(const Key& k, const T1& t1)
{
value_type* v = m_hashtable.key_insert_unique(k);
new (v) value_type(k, t1);
return m_hashtable.iterator_from_value_type(*v);
}
template <class T1, class T2> iterator insert(const Key& k, const T1& t1, const T2& t2)
{
value_type* v = m_hashtable.key_insert_unique(k);
new (v) value_type(k, t1, t2);
return m_hashtable.iterator_from_value_type(*v);
}
template <class T1, class T2, class T3> iterator insert(const Key& k, const T1& t1, const T2& t2, const T3& t3)
{
value_type* v = m_hashtable.key_insert_unique(k);
new (v) value_type(k, t1, t2, t3);
return m_hashtable.iterator_from_value_type(*v);
}
template <class T1, class T2, class T3, class T4> iterator insert(const Key& k, const T1& t1, const T2& t2, const T3& t3, const T4& t4)
{
value_type* v = m_hashtable.key_insert_unique(k);
new (v) value_type(k, t1, t2, t3, t4);
return m_hashtable.iterator_from_value_type(*v);
}
// Requires the use of placement new to construct the Value.
// Example: new (l.insert_placement(key)) Value(arg1, arg2);
void* insert_placement(const Key& k)
{
value_type* v = m_hashtable.key_insert_unique(k);
typetraits<Key>::construct(&const_cast<Key&>(v->first), k);
return &v->second;
}
void move(iterator which, iterator pos) { m_hashtable.move(which, pos); }
// erasing
void erase(iterator pos) { m_hashtable.erase(pos); }
size_type erase(const key_type& k) { return m_hashtable.erase(k); }
void erase(iterator first, iterator last) { m_hashtable.erase(first, last); }
void clear() { m_hashtable.clear(); }
void delete_all()
{
for (iterator iter(begin()); iter != end(); ++iter)
{
delete (*iter).second;
}
clear();
}
// search
const_iterator find(const key_type& k) const { return m_hashtable.find(k); }
iterator find(const key_type& k) { return m_hashtable.find(k); }
size_type count(const key_type& k) const { return find(k) == end() ? 0 : 1; }
pair<const_iterator, const_iterator> equal_range(const key_type& k) const { return m_hashtable.equal_range(k); }
pair<iterator, iterator> equal_range(const key_type& k) { return m_hashtable.equal_range(k); }
data_type& operator[](const key_type& k) { return (*insert(value_type(k)).first).second; }
};
// thor::hash_multimap
template
<
class Key,
class Data,
class HashFunc = hash<Key>,
class PartitionPolicy = policy::base2_partition
> class hash_multimap
{
public:
typedef Key key_type;
typedef Data data_type;
typedef pair<const key_type, data_type> value_type;
typedef HashFunc hasher;
private:
typedef hashtable<key_type, value_type, hasher, select1st<value_type>, PartitionPolicy> hashtable_type;
hashtable_type m_hashtable;
public:
typedef typename hashtable_type::pointer pointer;
typedef typename hashtable_type::const_pointer const_pointer;
typedef typename hashtable_type::reference reference;
typedef typename hashtable_type::const_reference const_reference;
typedef thor_size_type size_type;
typedef thor_diff_type difference_type;
typedef typename hashtable_type::iterator iterator;
typedef typename hashtable_type::const_iterator const_iterator;
typedef typename hashtable_type::reverse_iterator reverse_iterator;
typedef typename hashtable_type::const_reverse_iterator const_reverse_iterator;
// constructors
hash_multimap()
{}
hash_multimap(size_type n) :
m_hashtable(n)
{}
hash_multimap(size_type n, const hasher& h) :
m_hashtable(n, h)
{}
template <class InputIterator> hash_multimap(InputIterator first, InputIterator last)
{
m_hashtable.insert_equal(first, last);
}
template <class InputIterator> hash_multimap(InputIterator first, InputIterator last, size_type n) :
m_hashtable(n)
{
m_hashtable.insert_equal(first, last);
}
template <class InputIterator> hash_multimap(InputIterator first, InputIterator last, size_type n, const hasher& h) :
m_hashtable(n, h)
{
m_hashtable.insert_equal(first, last);
}
hash_multimap(const hash_multimap& rhs) :
m_hashtable(rhs.m_hashtable)
{}
~hash_multimap()
{}
// iteration
iterator begin(bool mode_hash=false) { return m_hashtable.begin(mode_hash); }
iterator end() { return m_hashtable.end(); }
const_iterator begin(bool mode_hash=false) const { return m_hashtable.begin(mode_hash); }
const_iterator end() const { return m_hashtable.end(); }
reverse_iterator rbegin(bool mode_hash=false) { return m_hashtable.rbegin(mode_hash); }
reverse_iterator rend() { return m_hashtable.rend(); }
const_reverse_iterator rbegin(bool mode_hash=false) const { return m_hashtable.rbegin(mode_hash); }
const_reverse_iterator rend() const { return m_hashtable.rend(); }
// size
size_type size() const { return m_hashtable.size(); }
size_type max_size() const { return m_hashtable.max_size(); }
bool empty() const { return m_hashtable.empty(); }
size_type bucket_count() const { return m_hashtable.bucket_count(); }
void resize(size_type n) { m_hashtable.resize(n); }
const hasher& hash_funct() const { return m_hashtable.hash_funct(); }
hash_multimap& operator = (const hash_multimap& rhs) { m_hashtable = rhs.m_hashtable; return *this; }
void swap(hash_multimap& rhs) { m_hashtable.swap(rhs.m_hashtable); }
// insertion
iterator insert(const value_type& x) { return m_hashtable.insert_equal(x); }
template <class InputIterator> void insert_range(InputIterator first, InputIterator last) { m_hashtable.insert_equal(first, last); }
// insert extensions
iterator insert(const Key& k)
{
value_type* v = m_hashtable.key_insert_equal(k);
typetraits<value_type>::construct(v, k);
return m_hashtable.iterator_from_value_type(*v);
}
template <class T1> iterator insert(const Key& k, const T1& t1)
{
value_type* v = m_hashtable.key_insert_equal(k);
new (v) value_type(k, t1);
return m_hashtable.iterator_from_value_type(*v);
}
template <class T1, class T2> iterator insert(const Key& k, const T1& t1, const T2& t2)
{
value_type* v = m_hashtable.key_insert_equal(k);
new (v) value_type(k, t1, t2);
return m_hashtable.iterator_from_value_type(*v);
}
template <class T1, class T2, class T3> iterator insert(const Key& k, const T1& t1, const T2& t2, const T3& t3)
{
value_type* v = m_hashtable.key_insert_equal(k);
new (v) value_type(k, t1, t2, t3);
return m_hashtable.iterator_from_value_type(*v);
}
template <class T1, class T2, class T3, class T4> iterator insert(const Key& k, const T1& t1, const T2& t2, const T3& t3, const T4& t4)
{
value_type* v = m_hashtable.key_insert_equal(k);
new (v) value_type(k, t1, t2, t3, t4);
return m_hashtable.iterator_from_value_type(*v);
}
// Requires the use of placement new to construct the Value.
// Example: new (l.insert_placement(key)) Value(arg1, arg2);
void* insert_placement(const Key& k)
{
value_type* v = m_hashtable.key_insert_equal(k);
typetraits<Key>::construct(&const_cast<Key&>(v->first), k);
return &v->second;
}
void move(iterator which, iterator pos) { m_hashtable.move(which, pos); }
// erasing
void erase(iterator pos) { m_hashtable.erase(pos); }
size_type erase(const key_type& k) { return m_hashtable.erase(k); }
void erase(iterator first, iterator last) { m_hashtable.erase(first, last); }
void clear() { m_hashtable.clear(); }
void delete_all()
{
for (iterator iter(begin()); iter != end(); ++iter)
{
delete (*iter).second;
}
clear();
}
// searching
const_iterator find(const key_type& k) const { return m_hashtable.find(k); }
iterator find(const key_type& k) { return m_hashtable.find(k); }
size_type count(const key_type& k) const { return m_hashtable.count(k); }
pair<const_iterator, const_iterator> equal_range(const key_type& k, size_type* count = 0) const { return m_hashtable.equal_range(k, count); }
pair<iterator, iterator> equal_range(const key_type& k, size_type* count = 0) { return m_hashtable.equal_range(k, count); }
};
// Swap specializations
template <class Key, class Data, class HashFunc> void swap(hash_map<Key, Data, HashFunc>& lhs, hash_map<Key, Data, HashFunc>& rhs)
{
lhs.swap(rhs);
}
template <class Key, class Data, class HashFunc> void swap(hash_multimap<Key, Data, HashFunc>& lhs, hash_multimap<Key, Data, HashFunc>& rhs)
{
lhs.swap(rhs);
}
} // namespace thor
// Global comparators
template <class Key, class Data, class HashFunc> bool operator == (const thor::hash_map<Key,Data,HashFunc>& lhs,
const thor::hash_map<Key,Data,HashFunc>& rhs)
{
typedef thor::hash_map<Key,Data,HashFunc> hashmaptype;
if (!(lhs.size() == rhs.size()))
{
// Early out if size doesn't match
return false;
}
for (typename hashmaptype::const_iterator iter(lhs.begin(true));
iter != lhs.end();
++iter)
{
typename hashmaptype::const_iterator rhs_iter(rhs.find((*iter).first));
if (rhs_iter == rhs.end())
{
return false;
}
if (!((*iter).second == (*rhs_iter).second))
{
return false;
}
}
return true;
}
template <class Key, class Data, class HashFunc> bool operator != (const thor::hash_map<Key,Data,HashFunc>& lhs,
const thor::hash_map<Key,Data,HashFunc>& rhs)
{
return !(lhs == rhs);
}
template <class Key, class Data, class HashFunc> bool operator == (const thor::hash_multimap<Key,Data,HashFunc>& lhs,
const thor::hash_multimap<Key,Data,HashFunc>& rhs)
{
typedef thor::hash_multimap<Key,Data,HashFunc> hashmaptype;
if (!(lhs.size() == rhs.size()))
{
// Early out if size doesn't match
return false;
}
typedef thor::list<typename hashmaptype::const_iterator, 64> listtype;
listtype l;
typename hashmaptype::const_iterator iter(lhs.begin(true));
while(iter != lhs.end())
{
// This is somewhat nightmarish. There's nothing to guarantee the values for a given key are in the same order
// in both hash_multimaps. Therefore, we have a bit of an O(n^2) problem to verify.
thor::pair<typename hashmaptype::const_iterator, typename hashmaptype::const_iterator> range = rhs.equal_range((*iter).first);
// Convert to a vector of iterators
THOR_DEBUG_ASSERT(l.empty());
while (range.first != range.second)
{
l.push_back(range.first);
++range.first;
}
for (;;)
{
// Find the value in the vector and remove it.
if (l.empty())
{
// No more matching keys in rhs
return false;
}
bool found = false;
for (typename listtype::iterator listiter = l.begin(); listiter != l.end(); ++listiter)
{
if ((**listiter).second == (*iter).second)
{
l.erase(listiter);
found = true;
break;
}
}
if (!found)
{
// Key/value pair not found in rhs; hash_multimaps aren't equal
return false;
}
// Break loop when out of keys
const typename hashmaptype::key_type& lhs_key = (*iter++).first;
if (iter == lhs.end() || !(lhs_key == (*iter).first))
{
break;
}
}
if (!l.empty())
{
// Remaining key/value pairs in rhs; hash_multimaps aren't equal
return false;
}
}
return true;
}
template <class Key, class Data, class HashFunc> bool operator != (const thor::hash_multimap<Key,Data,HashFunc>& lhs,
const thor::hash_multimap<Key,Data,HashFunc>& rhs)
{
return !(lhs == rhs);
}
#endif