-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhash_map.hpp
More file actions
560 lines (474 loc) · 18 KB
/
hash_map.hpp
File metadata and controls
560 lines (474 loc) · 18 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
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
#pragma once
#include <functional>
#include <memory>
#include <utility>
#include <type_traits>
namespace fefu
{
template<typename T>
class allocator {
public:
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using pointer = T*;
using const_pointer = const T*;
using reference = typename std::add_lvalue_reference<T>::type;
using const_reference = typename std::add_lvalue_reference<const T>::type;
using value_type = T;
allocator() noexcept;
allocator(const allocator&) noexcept;
template <class U>
allocator(const allocator<U>&) noexcept;
~allocator();
pointer allocate(size_type);
void deallocate(pointer p, size_type n) noexcept;
};
template<typename ValueType>
class hash_map_iterator {
public:
using iterator_category = std::forward_iterator_tag;
using value_type = ValueType;
using difference_type = std::ptrdiff_t;
using reference = ValueType&;
using pointer = ValueType*;
hash_map_iterator() noexcept;
hash_map_iterator(const hash_map_iterator& other) noexcept;
reference operator*() const;
pointer operator->() const;
// prefix ++
hash_map_iterator& operator++();
// postfix ++
hash_map_iterator operator++(int);
friend bool operator==(const hash_map_iterator<ValueType>&, const hash_map_iterator<ValueType>&);
friend bool operator!=(const hash_map_iterator<ValueType>&, const hash_map_iterator<ValueType>&);
};
template<typename ValueType>
class hash_map_const_iterator {
// Shouldn't give non const references on value
public:
using iterator_category = std::forward_iterator_tag;
using value_type = ValueType;
using difference_type = std::ptrdiff_t;
using reference = const ValueType&;
using pointer = const ValueType*;
hash_map_const_iterator() noexcept;
hash_map_const_iterator(const hash_map_const_iterator& other) noexcept;
hash_map_const_iterator(const hash_map_iterator<ValueType>& other) noexcept;
reference operator*() const;
pointer operator->() const;
// prefix ++
hash_map_const_iterator& operator++();
// postfix ++
hash_map_const_iterator operator++(int);
friend bool operator==(const hash_map_const_iterator<ValueType>&, const hash_map_const_iterator<ValueType>&);
friend bool operator!=(const hash_map_const_iterator<ValueType>&, const hash_map_const_iterator<ValueType>&);
};
template<typename K, typename T,
typename Hash = std::hash<K>,
typename Pred = std::equal_to<K>,
typename Alloc = allocator<std::pair<const K, T>>>
class hash_map
{
public:
using key_type = K;
using mapped_type = T;
using hasher = Hash;
using key_equal = Pred;
using allocator_type = Alloc;
using value_type = std::pair<const key_type, mapped_type>;
using reference = value_type&;
using const_reference = const value_type&;
using iterator = hash_map_iterator<value_type>;
using const_iterator = hash_map_const_iterator<value_type>;
using size_type = std::size_t;
/// Default constructor.
hash_map() = default;
/**
* @brief Default constructor creates no elements.
* @param n Minimal initial number of buckets.
*/
explicit hash_map(size_type n);
/**
* @brief Builds an %hash_map from a range.
* @param first An input iterator.
* @param last An input iterator.
* @param n Minimal initial number of buckets.
*
* Create an %hash_map consisting of copies of the elements from
* [first,last). This is linear in N (where N is
* distance(first,last)).
*/
template<typename InputIterator>
hash_map(InputIterator first, InputIterator last,
size_type n = 0);
/// Copy constructor.
hash_map(const hash_map&);
/// Move constructor.
hash_map(hash_map&&);
/**
* @brief Creates an %hash_map with no elements.
* @param a An allocator object.
*/
explicit hash_map(const allocator_type& a);
/*
* @brief Copy constructor with allocator argument.
* @param uset Input %hash_map to copy.
* @param a An allocator object.
*/
hash_map(const hash_map& umap,
const allocator_type& a);
/*
* @brief Move constructor with allocator argument.
* @param uset Input %hash_map to move.
* @param a An allocator object.
*/
hash_map(hash_map&& umap,
const allocator_type& a);
/**
* @brief Builds an %hash_map from an initializer_list.
* @param l An initializer_list.
* @param n Minimal initial number of buckets.
*
* Create an %hash_map consisting of copies of the elements in the
* list. This is linear in N (where N is @a l.size()).
*/
hash_map(std::initializer_list<value_type> l,
size_type n = 0);
/// Copy assignment operator.
hash_map& operator=(const hash_map&);
/// Move assignment operator.
hash_map& operator=(hash_map&&);
/**
* @brief %hash_map list assignment operator.
* @param l An initializer_list.
*
* This function fills an %hash_map with copies of the elements in
* the initializer list @a l.
*
* Note that the assignment completely changes the %hash_map and
* that the resulting %hash_map's size is the same as the number
* of elements assigned.
*/
hash_map& operator=(std::initializer_list<value_type> l);
/// Returns the allocator object used by the %hash_map.
allocator_type get_allocator() const noexcept;
// size and capacity:
/// Returns true if the %hash_map is empty.
bool empty() const noexcept;
/// Returns the size of the %hash_map.
size_type size() const noexcept;
/// Returns the maximum size of the %hash_map.
size_type max_size() const noexcept;
// iterators.
/**
* Returns a read/write iterator that points to the first element in the
* %hash_map.
*/
iterator begin() noexcept;
//@{
/**
* Returns a read-only (constant) iterator that points to the first
* element in the %hash_map.
*/
const_iterator begin() const noexcept;
const_iterator cbegin() const noexcept;
/**
* Returns a read/write iterator that points one past the last element in
* the %hash_map.
*/
iterator end() noexcept;
//@{
/**
* Returns a read-only (constant) iterator that points one past the last
* element in the %hash_map.
*/
const_iterator end() const noexcept;
const_iterator cend() const noexcept;
//@}
// modifiers.
/**
* @brief Attempts to build and insert a std::pair into the
* %hash_map.
*
* @param args Arguments used to generate a new pair instance (see
* std::piecewise_contruct for passing arguments to each
* part of the pair constructor).
*
* @return A pair, of which the first element is an iterator that points
* to the possibly inserted pair, and the second is a bool that
* is true if the pair was actually inserted.
*
* This function attempts to build and insert a (key, value) %pair into
* the %hash_map.
* An %hash_map relies on unique keys and thus a %pair is only
* inserted if its first element (the key) is not already present in the
* %hash_map.
*
* Insertion requires amortized constant time.
*/
template<typename... _Args>
std::pair<iterator, bool> emplace(_Args&&... args);
/**
* @brief Attempts to build and insert a std::pair into the
* %hash_map.
*
* @param k Key to use for finding a possibly existing pair in
* the hash_map.
* @param args Arguments used to generate the .second for a
* new pair instance.
*
* @return A pair, of which the first element is an iterator that points
* to the possibly inserted pair, and the second is a bool that
* is true if the pair was actually inserted.
*
* This function attempts to build and insert a (key, value) %pair into
* the %hash_map.
* An %hash_map relies on unique keys and thus a %pair is only
* inserted if its first element (the key) is not already present in the
* %hash_map.
* If a %pair is not inserted, this function has no effect.
*
* Insertion requires amortized constant time.
*/
template <typename... _Args>
std::pair<iterator, bool> try_emplace(const key_type& k, _Args&&... args);
// move-capable overload
template <typename... _Args>
std::pair<iterator, bool> try_emplace(key_type&& k, _Args&&... args);
//@{
/**
* @brief Attempts to insert a std::pair into the %hash_map.
* @param x Pair to be inserted (see std::make_pair for easy
* creation of pairs).
*
* @return A pair, of which the first element is an iterator that
* points to the possibly inserted pair, and the second is
* a bool that is true if the pair was actually inserted.
*
* This function attempts to insert a (key, value) %pair into the
* %hash_map. An %hash_map relies on unique keys and thus a
* %pair is only inserted if its first element (the key) is not already
* present in the %hash_map.
*
* Insertion requires amortized constant time.
*/
std::pair<iterator, bool> insert(const value_type& x);
std::pair<iterator, bool> insert(value_type&& x);
//@}
/**
* @brief A template function that attempts to insert a range of
* elements.
* @param first Iterator pointing to the start of the range to be
* inserted.
* @param last Iterator pointing to the end of the range.
*
* Complexity similar to that of the range constructor.
*/
template<typename _InputIterator>
void insert(_InputIterator first, _InputIterator last);
/**
* @brief Attempts to insert a list of elements into the %hash_map.
* @param l A std::initializer_list<value_type> of elements
* to be inserted.
*
* Complexity similar to that of the range constructor.
*/
void insert(std::initializer_list<value_type> l);
/**
* @brief Attempts to insert a std::pair into the %hash_map.
* @param k Key to use for finding a possibly existing pair in
* the map.
* @param obj Argument used to generate the .second for a pair
* instance.
*
* @return A pair, of which the first element is an iterator that
* points to the possibly inserted pair, and the second is
* a bool that is true if the pair was actually inserted.
*
* This function attempts to insert a (key, value) %pair into the
* %hash_map. An %hash_map relies on unique keys and thus a
* %pair is only inserted if its first element (the key) is not already
* present in the %hash_map.
* If the %pair was already in the %hash_map, the .second of
* the %pair is assigned from obj.
*
* Insertion requires amortized constant time.
*/
template <typename _Obj>
std::pair<iterator, bool> insert_or_assign(const key_type& k, _Obj&& obj);
// move-capable overload
template <typename _Obj>
std::pair<iterator, bool> insert_or_assign(key_type&& k, _Obj&& obj);
//@{
/**
* @brief Erases an element from an %hash_map.
* @param position An iterator pointing to the element to be erased.
* @return An iterator pointing to the element immediately following
* @a position prior to the element being erased. If no such
* element exists, end() is returned.
*
* This function erases an element, pointed to by the given iterator,
* from an %hash_map.
* Note that this function only erases the element, and that if the
* element is itself a pointer, the pointed-to memory is not touched in
* any way. Managing the pointer is the user's responsibility.
*/
iterator erase(const_iterator position);
// LWG 2059.
iterator erase(iterator position);
//@}
/**
* @brief Erases elements according to the provided key.
* @param x Key of element to be erased.
* @return The number of elements erased.
*
* This function erases all the elements located by the given key from
* an %hash_map. For an %hash_map the result of this function
* can only be 0 (not present) or 1 (present).
* Note that this function only erases the element, and that if the
* element is itself a pointer, the pointed-to memory is not touched in
* any way. Managing the pointer is the user's responsibility.
*/
size_type erase(const key_type& x);
/**
* @brief Erases a [first,last) range of elements from an
* %hash_map.
* @param first Iterator pointing to the start of the range to be
* erased.
* @param last Iterator pointing to the end of the range to
* be erased.
* @return The iterator @a last.
*
* This function erases a sequence of elements from an %hash_map.
* Note that this function only erases the elements, and that if
* the element is itself a pointer, the pointed-to memory is not touched
* in any way. Managing the pointer is the user's responsibility.
*/
iterator erase(const_iterator first, const_iterator last);
/**
* Erases all elements in an %hash_map.
* Note that this function only erases the elements, and that if the
* elements themselves are pointers, the pointed-to memory is not touched
* in any way. Managing the pointer is the user's responsibility.
*/
void clear() noexcept;
/**
* @brief Swaps data with another %hash_map.
* @param x An %hash_map of the same element and allocator
* types.
*
* This exchanges the elements between two %hash_map in constant
* time.
* Note that the global std::swap() function is specialized such that
* std::swap(m1,m2) will feed to this function.
*/
void swap(hash_map& x);
template<typename _H2, typename _P2>
void merge(hash_map<K, T, _H2, _P2, Alloc>& source);
template<typename _H2, typename _P2>
void merge(hash_map<K, T, _H2, _P2, Alloc>&& source);
// observers.
/// Returns the hash functor object with which the %hash_map was
/// constructed.
Hash hash_function() const;
/// Returns the key comparison object with which the %hash_map was
/// constructed.
Pred key_eq() const;
// lookup.
//@{
/**
* @brief Tries to locate an element in an %hash_map.
* @param x Key to be located.
* @return Iterator pointing to sought-after element, or end() if not
* found.
*
* This function takes a key and tries to locate the element with which
* the key matches. If successful the function returns an iterator
* pointing to the sought after element. If unsuccessful it returns the
* past-the-end ( @c end() ) iterator.
*/
iterator find(const key_type& x);
const_iterator find(const key_type& x) const;
//@}
/**
* @brief Finds the number of elements.
* @param x Key to count.
* @return Number of elements with specified key.
*
* This function only makes sense for %unordered_multimap; for
* %hash_map the result will either be 0 (not present) or 1
* (present).
*/
size_type count(const key_type& x) const;
/**
* @brief Finds whether an element with the given key exists.
* @param x Key of elements to be located.
* @return True if there is any element with the specified key.
*/
bool contains(const key_type& x) const;
//@{
/**
* @brief Subscript ( @c [] ) access to %hash_map data.
* @param k The key for which data should be retrieved.
* @return A reference to the data of the (key,data) %pair.
*
* Allows for easy lookup with the subscript ( @c [] )operator. Returns
* data associated with the key specified in subscript. If the key does
* not exist, a pair with that key is created using default values, which
* is then returned.
*
* Lookup requires constant time.
*/
mapped_type& operator[](const key_type& k);
mapped_type& operator[](key_type&& k);
//@}
//@{
/**
* @brief Access to %hash_map data.
* @param k The key for which data should be retrieved.
* @return A reference to the data whose key is equal to @a k, if
* such a data is present in the %hash_map.
* @throw std::out_of_range If no such data is present.
*/
mapped_type& at(const key_type& k);
const mapped_type& at(const key_type& k) const;
//@}
// bucket interface.
/// Returns the number of buckets of the %hash_map.
size_type bucket_count() const noexcept;
/*
* @brief Returns the bucket index of a given element.
* @param _K A key instance.
* @return The key bucket index.
*/
size_type bucket(const key_type& _K) const;
// hash policy.
/// Returns the average number of elements per bucket.
float load_factor() const noexcept;
/// Returns a positive number that the %hash_map tries to keep the
/// load factor less than or equal to.
float max_load_factor() const noexcept;
/**
* @brief Change the %hash_map maximum load factor.
* @param z The new maximum load factor.
*/
void max_load_factor(float z);
/**
* @brief May rehash the %hash_map.
* @param n The new number of buckets.
*
* Rehash will occur only if the new number of buckets respect the
* %hash_map maximum load factor.
*/
void rehash(size_type n);
/**
* @brief Prepare the %hash_map for a specified number of
* elements.
* @param n Number of elements required.
*
* Same as rehash(ceil(n / max_load_factor())).
*/
void reserve(size_type n);
bool operator==(const hash_map& other) const;
};
} // namespace fefu