robin-map
robin_map.h
Go to the documentation of this file.
1 /**
2  * MIT License
3  *
4  * Copyright (c) 2017 Tessil
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #ifndef TSL_ROBIN_MAP_H
25 #define TSL_ROBIN_MAP_H
26 
27 
28 #include <cstddef>
29 #include <functional>
30 #include <initializer_list>
31 #include <memory>
32 #include <type_traits>
33 #include <utility>
34 #include "robin_hash.h"
35 
36 
37 namespace tsl {
38 
39 
40 /**
41  * Implementation of a hash map using open-adressing and the robin hood hashing algorithm with backward shift deletion.
42  *
43  * For operations modifying the hash map (insert, erase, rehash, ...), the strong exception guarantee
44  * is only guaranteed when the expression `std::is_nothrow_swappable<std::pair<Key, T>>::value &&
45  * std::is_nothrow_move_constructible<std::pair<Key, T>>::value` is true, otherwise if an exception
46  * is thrown during the swap or the move, the hash map may end up in a undefined state. Per the standard
47  * a `Key` or `T` with a noexcept copy constructor and no move constructor also satisfies the
48  * `std::is_nothrow_move_constructible<std::pair<Key, T>>::value` criterion (and will thus guarantee the
49  * strong exception for the map).
50  *
51  * When `StoreHash` is true, 32 bits of the hash are stored alongside the values. It can improve
52  * the performance during lookups if the `KeyEqual` function takes time (if it engenders a cache-miss for example)
53  * as we then compare the stored hashes before comparing the keys. When `tsl::rh::power_of_two_growth_policy` is used
54  * as `GrowthPolicy`, it may also speed-up the rehash process as we can avoid to recalculate the hash.
55  * When it is detected that storing the hash will not incur any memory penality due to alignement (i.e.
56  * `sizeof(tsl::detail_robin_hash::bucket_entry<ValueType, true>) ==
57  * sizeof(tsl::detail_robin_hash::bucket_entry<ValueType, false>)`) and `tsl::rh::power_of_two_growth_policy` is
58  * used, the hash will be stored even if `StoreHash` is false so that we can speed-up the rehash (but it will
59  * not be used on lookups unless `StoreHash` is true).
60  *
61  * `GrowthPolicy` defines how the map grows and consequently how a hash value is mapped to a bucket.
62  * By default the map uses `tsl::rh::power_of_two_growth_policy`. This policy keeps the number of buckets
63  * to a power of two and uses a mask to map the hash to a bucket instead of the slow modulo.
64  * Other growth policies are available and you may define your own growth policy,
65  * check `tsl::rh::power_of_two_growth_policy` for the interface.
66  *
67  * `std::pair<Key, T>` must be swappable.
68  *
69  * `Key` and `T` must be copy and/or move constructible.
70  *
71  * If the destructor of `Key` or `T` throws an exception, the behaviour of the class is undefined.
72  *
73  * Iterators invalidation:
74  * - clear, operator=, reserve, rehash: always invalidate the iterators.
75  * - insert, emplace, emplace_hint, operator[]: if there is an effective insert, invalidate the iterators.
76  * - erase: always invalidate the iterators.
77  */
78 template<class Key,
79  class T,
80  class Hash = std::hash<Key>,
81  class KeyEqual = std::equal_to<Key>,
82  class Allocator = std::allocator<std::pair<Key, T>>,
83  bool StoreHash = false,
84  class GrowthPolicy = tsl::rh::power_of_two_growth_policy<2>>
85 class robin_map {
86 private:
87  template<typename U>
88  using has_is_transparent = tsl::detail_robin_hash::has_is_transparent<U>;
89 
90  class KeySelect {
91  public:
92  using key_type = Key;
93 
94  const key_type& operator()(const std::pair<Key, T>& key_value) const noexcept {
95  return key_value.first;
96  }
97 
98  key_type& operator()(std::pair<Key, T>& key_value) noexcept {
99  return key_value.first;
100  }
101  };
102 
103  class ValueSelect {
104  public:
105  using value_type = T;
106 
107  const value_type& operator()(const std::pair<Key, T>& key_value) const noexcept {
108  return key_value.second;
109  }
110 
111  value_type& operator()(std::pair<Key, T>& key_value) noexcept {
112  return key_value.second;
113  }
114  };
115 
116  using ht = detail_robin_hash::robin_hash<std::pair<Key, T>, KeySelect, ValueSelect,
117  Hash, KeyEqual, Allocator, StoreHash, GrowthPolicy>;
118 
119 public:
120  using key_type = typename ht::key_type;
121  using mapped_type = T;
122  using value_type = typename ht::value_type;
123  using size_type = typename ht::size_type;
124  using difference_type = typename ht::difference_type;
125  using hasher = typename ht::hasher;
126  using key_equal = typename ht::key_equal;
127  using allocator_type = typename ht::allocator_type;
128  using reference = typename ht::reference;
129  using const_reference = typename ht::const_reference;
130  using pointer = typename ht::pointer;
131  using const_pointer = typename ht::const_pointer;
132  using iterator = typename ht::iterator;
133  using const_iterator = typename ht::const_iterator;
134 
135 
136 public:
137  /*
138  * Constructors
139  */
140  robin_map(): robin_map(ht::DEFAULT_INIT_BUCKETS_SIZE) {
141  }
142 
143  explicit robin_map(size_type bucket_count,
144  const Hash& hash = Hash(),
145  const KeyEqual& equal = KeyEqual(),
146  const Allocator& alloc = Allocator()):
147  m_ht(bucket_count, hash, equal, alloc)
148  {
149  }
150 
151  robin_map(size_type bucket_count,
152  const Allocator& alloc): robin_map(bucket_count, Hash(), KeyEqual(), alloc)
153  {
154  }
155 
156  robin_map(size_type bucket_count,
157  const Hash& hash,
158  const Allocator& alloc): robin_map(bucket_count, hash, KeyEqual(), alloc)
159  {
160  }
161 
162  explicit robin_map(const Allocator& alloc): robin_map(ht::DEFAULT_INIT_BUCKETS_SIZE, alloc) {
163  }
164 
165  template<class InputIt>
166  robin_map(InputIt first, InputIt last,
167  size_type bucket_count = ht::DEFAULT_INIT_BUCKETS_SIZE,
168  const Hash& hash = Hash(),
169  const KeyEqual& equal = KeyEqual(),
170  const Allocator& alloc = Allocator()): robin_map(bucket_count, hash, equal, alloc)
171  {
172  insert(first, last);
173  }
174 
175  template<class InputIt>
176  robin_map(InputIt first, InputIt last,
177  size_type bucket_count,
178  const Allocator& alloc): robin_map(first, last, bucket_count, Hash(), KeyEqual(), alloc)
179  {
180  }
181 
182  template<class InputIt>
183  robin_map(InputIt first, InputIt last,
184  size_type bucket_count,
185  const Hash& hash,
186  const Allocator& alloc): robin_map(first, last, bucket_count, hash, KeyEqual(), alloc)
187  {
188  }
189 
190  robin_map(std::initializer_list<value_type> init,
191  size_type bucket_count = ht::DEFAULT_INIT_BUCKETS_SIZE,
192  const Hash& hash = Hash(),
193  const KeyEqual& equal = KeyEqual(),
194  const Allocator& alloc = Allocator()):
195  robin_map(init.begin(), init.end(), bucket_count, hash, equal, alloc)
196  {
197  }
198 
199  robin_map(std::initializer_list<value_type> init,
200  size_type bucket_count,
201  const Allocator& alloc):
202  robin_map(init.begin(), init.end(), bucket_count, Hash(), KeyEqual(), alloc)
203  {
204  }
205 
206  robin_map(std::initializer_list<value_type> init,
207  size_type bucket_count,
208  const Hash& hash,
209  const Allocator& alloc):
210  robin_map(init.begin(), init.end(), bucket_count, hash, KeyEqual(), alloc)
211  {
212  }
213 
214  robin_map& operator=(std::initializer_list<value_type> ilist) {
215  m_ht.clear();
216 
217  m_ht.reserve(ilist.size());
218  m_ht.insert(ilist.begin(), ilist.end());
219 
220  return *this;
221  }
222 
223  allocator_type get_allocator() const { return m_ht.get_allocator(); }
224 
225 
226  /*
227  * Iterators
228  */
229  iterator begin() noexcept { return m_ht.begin(); }
230  const_iterator begin() const noexcept { return m_ht.begin(); }
231  const_iterator cbegin() const noexcept { return m_ht.cbegin(); }
232 
233  iterator end() noexcept { return m_ht.end(); }
234  const_iterator end() const noexcept { return m_ht.end(); }
235  const_iterator cend() const noexcept { return m_ht.cend(); }
236 
237 
238  /*
239  * Capacity
240  */
241  bool empty() const noexcept { return m_ht.empty(); }
242  size_type size() const noexcept { return m_ht.size(); }
243  size_type max_size() const noexcept { return m_ht.max_size(); }
244 
245  /*
246  * Modifiers
247  */
248  void clear() noexcept { m_ht.clear(); }
249 
250 
251 
252  std::pair<iterator, bool> insert(const value_type& value) {
253  return m_ht.insert(value);
254  }
255 
256  template<class P, typename std::enable_if<std::is_constructible<value_type, P&&>::value>::type* = nullptr>
257  std::pair<iterator, bool> insert(P&& value) {
258  return m_ht.emplace(std::forward<P>(value));
259  }
260 
261  std::pair<iterator, bool> insert(value_type&& value) {
262  return m_ht.insert(std::move(value));
263  }
264 
265 
266  iterator insert(const_iterator hint, const value_type& value) {
267  return m_ht.insert_hint(hint, value);
268  }
269 
270  template<class P, typename std::enable_if<std::is_constructible<value_type, P&&>::value>::type* = nullptr>
271  iterator insert(const_iterator hint, P&& value) {
272  return m_ht.emplace_hint(hint, std::forward<P>(value));
273  }
274 
275  iterator insert(const_iterator hint, value_type&& value) {
276  return m_ht.insert_hint(hint, std::move(value));
277  }
278 
279 
280  template<class InputIt>
281  void insert(InputIt first, InputIt last) {
282  m_ht.insert(first, last);
283  }
284 
285  void insert(std::initializer_list<value_type> ilist) {
286  m_ht.insert(ilist.begin(), ilist.end());
287  }
288 
289 
290 
291 
292  template<class M>
293  std::pair<iterator, bool> insert_or_assign(const key_type& k, M&& obj) {
294  return m_ht.insert_or_assign(k, std::forward<M>(obj));
295  }
296 
297  template<class M>
298  std::pair<iterator, bool> insert_or_assign(key_type&& k, M&& obj) {
299  return m_ht.insert_or_assign(std::move(k), std::forward<M>(obj));
300  }
301 
302  template<class M>
303  iterator insert_or_assign(const_iterator hint, const key_type& k, M&& obj) {
304  return m_ht.insert_or_assign(hint, k, std::forward<M>(obj));
305  }
306 
307  template<class M>
308  iterator insert_or_assign(const_iterator hint, key_type&& k, M&& obj) {
309  return m_ht.insert_or_assign(hint, std::move(k), std::forward<M>(obj));
310  }
311 
312 
313 
314  /**
315  * Due to the way elements are stored, emplace will need to move or copy the key-value once.
316  * The method is equivalent to insert(value_type(std::forward<Args>(args)...));
317  *
318  * Mainly here for compatibility with the std::unordered_map interface.
319  */
320  template<class... Args>
321  std::pair<iterator, bool> emplace(Args&&... args) {
322  return m_ht.emplace(std::forward<Args>(args)...);
323  }
324 
325 
326 
327  /**
328  * Due to the way elements are stored, emplace_hint will need to move or copy the key-value once.
329  * The method is equivalent to insert(hint, value_type(std::forward<Args>(args)...));
330  *
331  * Mainly here for compatibility with the std::unordered_map interface.
332  */
333  template<class... Args>
334  iterator emplace_hint(const_iterator hint, Args&&... args) {
335  return m_ht.emplace_hint(hint, std::forward<Args>(args)...);
336  }
337 
338 
339 
340 
341  template<class... Args>
342  std::pair<iterator, bool> try_emplace(const key_type& k, Args&&... args) {
343  return m_ht.try_emplace(k, std::forward<Args>(args)...);
344  }
345 
346  template<class... Args>
347  std::pair<iterator, bool> try_emplace(key_type&& k, Args&&... args) {
348  return m_ht.try_emplace(std::move(k), std::forward<Args>(args)...);
349  }
350 
351  template<class... Args>
352  iterator try_emplace(const_iterator hint, const key_type& k, Args&&... args) {
353  return m_ht.try_emplace_hint(hint, k, std::forward<Args>(args)...);
354  }
355 
356  template<class... Args>
357  iterator try_emplace(const_iterator hint, key_type&& k, Args&&... args) {
358  return m_ht.try_emplace_hint(hint, std::move(k), std::forward<Args>(args)...);
359  }
360 
361 
362 
363 
364  iterator erase(iterator pos) { return m_ht.erase(pos); }
365  iterator erase(const_iterator pos) { return m_ht.erase(pos); }
366  iterator erase(const_iterator first, const_iterator last) { return m_ht.erase(first, last); }
367  size_type erase(const key_type& key) { return m_ht.erase(key); }
368 
369  /**
370  * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
371  * as hash_function()(key). Usefull to speed-up the lookup to the value if you already have the hash.
372  */
373  size_type erase(const key_type& key, std::size_t precalculated_hash) {
374  return m_ht.erase(key, precalculated_hash);
375  }
376 
377  /**
378  * This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent exists.
379  * If so, K must be hashable and comparable to Key.
380  */
381  template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
382  size_type erase(const K& key) { return m_ht.erase(key); }
383 
384  /**
385  * @copydoc erase(const K& key)
386  *
387  * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
388  * as hash_function()(key). Usefull to speed-up the lookup to the value if you already have the hash.
389  */
390  template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
391  size_type erase(const K& key, std::size_t precalculated_hash) {
392  return m_ht.erase(key, precalculated_hash);
393  }
394 
395 
396 
397  void swap(robin_map& other) { other.m_ht.swap(m_ht); }
398 
399 
400 
401  /*
402  * Lookup
403  */
404  T& at(const Key& key) { return m_ht.at(key); }
405 
406  /**
407  * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
408  * as hash_function()(key). Usefull to speed-up the lookup if you already have the hash.
409  */
410  T& at(const Key& key, std::size_t precalculated_hash) { return m_ht.at(key, precalculated_hash); }
411 
412 
413  const T& at(const Key& key) const { return m_ht.at(key); }
414 
415  /**
416  * @copydoc at(const Key& key, std::size_t precalculated_hash)
417  */
418  const T& at(const Key& key, std::size_t precalculated_hash) const { return m_ht.at(key, precalculated_hash); }
419 
420 
421  /**
422  * This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent exists.
423  * If so, K must be hashable and comparable to Key.
424  */
425  template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
426  T& at(const K& key) { return m_ht.at(key); }
427 
428  /**
429  * @copydoc at(const K& key)
430  *
431  * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
432  * as hash_function()(key). Usefull to speed-up the lookup if you already have the hash.
433  */
434  template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
435  T& at(const K& key, std::size_t precalculated_hash) { return m_ht.at(key, precalculated_hash); }
436 
437 
438  /**
439  * @copydoc at(const K& key)
440  */
441  template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
442  const T& at(const K& key) const { return m_ht.at(key); }
443 
444  /**
445  * @copydoc at(const K& key, std::size_t precalculated_hash)
446  */
447  template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
448  const T& at(const K& key, std::size_t precalculated_hash) const { return m_ht.at(key, precalculated_hash); }
449 
450 
451 
452 
453  T& operator[](const Key& key) { return m_ht[key]; }
454  T& operator[](Key&& key) { return m_ht[std::move(key)]; }
455 
456 
457 
458 
459  size_type count(const Key& key) const { return m_ht.count(key); }
460 
461  /**
462  * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
463  * as hash_function()(key). Usefull to speed-up the lookup if you already have the hash.
464  */
465  size_type count(const Key& key, std::size_t precalculated_hash) const {
466  return m_ht.count(key, precalculated_hash);
467  }
468 
469  /**
470  * This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent exists.
471  * If so, K must be hashable and comparable to Key.
472  */
473  template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
474  size_type count(const K& key) const { return m_ht.count(key); }
475 
476  /**
477  * @copydoc count(const K& key) const
478  *
479  * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
480  * as hash_function()(key). Usefull to speed-up the lookup if you already have the hash.
481  */
482  template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
483  size_type count(const K& key, std::size_t precalculated_hash) const { return m_ht.count(key, precalculated_hash); }
484 
485 
486 
487 
488  iterator find(const Key& key) { return m_ht.find(key); }
489 
490  /**
491  * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
492  * as hash_function()(key). Usefull to speed-up the lookup if you already have the hash.
493  */
494  iterator find(const Key& key, std::size_t precalculated_hash) { return m_ht.find(key, precalculated_hash); }
495 
496  const_iterator find(const Key& key) const { return m_ht.find(key); }
497 
498  /**
499  * @copydoc find(const Key& key, std::size_t precalculated_hash)
500  */
501  const_iterator find(const Key& key, std::size_t precalculated_hash) const {
502  return m_ht.find(key, precalculated_hash);
503  }
504 
505  /**
506  * This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent exists.
507  * If so, K must be hashable and comparable to Key.
508  */
509  template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
510  iterator find(const K& key) { return m_ht.find(key); }
511 
512  /**
513  * @copydoc find(const K& key)
514  *
515  * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
516  * as hash_function()(key). Usefull to speed-up the lookup if you already have the hash.
517  */
518  template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
519  iterator find(const K& key, std::size_t precalculated_hash) { return m_ht.find(key, precalculated_hash); }
520 
521  /**
522  * @copydoc find(const K& key)
523  */
524  template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
525  const_iterator find(const K& key) const { return m_ht.find(key); }
526 
527  /**
528  * @copydoc find(const K& key)
529  *
530  * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
531  * as hash_function()(key). Usefull to speed-up the lookup if you already have the hash.
532  */
533  template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
534  const_iterator find(const K& key, std::size_t precalculated_hash) const {
535  return m_ht.find(key, precalculated_hash);
536  }
537 
538 
539 
540 
541  std::pair<iterator, iterator> equal_range(const Key& key) { return m_ht.equal_range(key); }
542 
543  /**
544  * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
545  * as hash_function()(key). Usefull to speed-up the lookup if you already have the hash.
546  */
547  std::pair<iterator, iterator> equal_range(const Key& key, std::size_t precalculated_hash) {
548  return m_ht.equal_range(key, precalculated_hash);
549  }
550 
551  std::pair<const_iterator, const_iterator> equal_range(const Key& key) const { return m_ht.equal_range(key); }
552 
553  /**
554  * @copydoc equal_range(const Key& key, std::size_t precalculated_hash)
555  */
556  std::pair<const_iterator, const_iterator> equal_range(const Key& key, std::size_t precalculated_hash) const {
557  return m_ht.equal_range(key, precalculated_hash);
558  }
559 
560  /**
561  * This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent exists.
562  * If so, K must be hashable and comparable to Key.
563  */
564  template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
565  std::pair<iterator, iterator> equal_range(const K& key) { return m_ht.equal_range(key); }
566 
567 
568  /**
569  * @copydoc equal_range(const K& key)
570  *
571  * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
572  * as hash_function()(key). Usefull to speed-up the lookup if you already have the hash.
573  */
574  template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
575  std::pair<iterator, iterator> equal_range(const K& key, std::size_t precalculated_hash) {
576  return m_ht.equal_range(key, precalculated_hash);
577  }
578 
579  /**
580  * @copydoc equal_range(const K& key)
581  */
582  template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
583  std::pair<const_iterator, const_iterator> equal_range(const K& key) const { return m_ht.equal_range(key); }
584 
585  /**
586  * @copydoc equal_range(const K& key, std::size_t precalculated_hash)
587  */
588  template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
589  std::pair<const_iterator, const_iterator> equal_range(const K& key, std::size_t precalculated_hash) const {
590  return m_ht.equal_range(key, precalculated_hash);
591  }
592 
593 
594 
595 
596  /*
597  * Bucket interface
598  */
599  size_type bucket_count() const { return m_ht.bucket_count(); }
600  size_type max_bucket_count() const { return m_ht.max_bucket_count(); }
601 
602 
603  /*
604  * Hash policy
605  */
606  float load_factor() const { return m_ht.load_factor(); }
607 
608  float min_load_factor() const { return m_ht.min_load_factor(); }
609  float max_load_factor() const { return m_ht.max_load_factor(); }
610 
611  /**
612  * Set the `min_load_factor` to `ml`. When the `load_factor` of the map goes
613  * below `min_load_factor` after some erase operations, the map will be
614  * shrunk when an insertion occurs. The erase method itself never shrinks
615  * the map.
616  *
617  * The default value of `min_load_factor` is 0.0f, the map never shrinks by default.
618  */
619  void min_load_factor(float ml) { m_ht.min_load_factor(ml); }
620  void max_load_factor(float ml) { m_ht.max_load_factor(ml); }
621 
622  void rehash(size_type count) { m_ht.rehash(count); }
623  void reserve(size_type count) { m_ht.reserve(count); }
624 
625 
626  /*
627  * Observers
628  */
629  hasher hash_function() const { return m_ht.hash_function(); }
630  key_equal key_eq() const { return m_ht.key_eq(); }
631 
632  /*
633  * Other
634  */
635 
636  /**
637  * Convert a const_iterator to an iterator.
638  */
639  iterator mutable_iterator(const_iterator pos) {
640  return m_ht.mutable_iterator(pos);
641  }
642 
643  friend bool operator==(const robin_map& lhs, const robin_map& rhs) {
644  if(lhs.size() != rhs.size()) {
645  return false;
646  }
647 
648  for(const auto& element_lhs: lhs) {
649  const auto it_element_rhs = rhs.find(element_lhs.first);
650  if(it_element_rhs == rhs.cend() || element_lhs.second != it_element_rhs->second) {
651  return false;
652  }
653  }
654 
655  return true;
656  }
657 
658  friend bool operator!=(const robin_map& lhs, const robin_map& rhs) {
659  return !operator==(lhs, rhs);
660  }
661 
662  friend void swap(robin_map& lhs, robin_map& rhs) {
663  lhs.swap(rhs);
664  }
665 
666 private:
667  ht m_ht;
668 };
669 
670 
671 /**
672  * Same as `tsl::robin_map<Key, T, Hash, KeyEqual, Allocator, StoreHash, tsl::rh::prime_growth_policy>`.
673  */
674 template<class Key,
675  class T,
676  class Hash = std::hash<Key>,
677  class KeyEqual = std::equal_to<Key>,
678  class Allocator = std::allocator<std::pair<Key, T>>,
679  bool StoreHash = false>
680 using robin_pg_map = robin_map<Key, T, Hash, KeyEqual, Allocator, StoreHash, tsl::rh::prime_growth_policy>;
681 
682 } // end namespace tsl
683 
684 #endif
robin_map(size_type bucket_count, const Hash &hash=Hash(), const KeyEqual &equal=KeyEqual(), const Allocator &alloc=Allocator())
Definition: robin_map.h:143
size_type max_bucket_count() const
Definition: robin_map.h:600
iterator end() noexcept
Definition: robin_map.h:233
Definition: robin_growth_policy.h:69
float min_load_factor() const
Definition: robin_map.h:608
hasher hash_function() const
Definition: robin_map.h:629
iterator find(const K &key, std::size_t precalculated_hash)
Definition: robin_map.h:519
const T & at(const K &key, std::size_t precalculated_hash) const
Definition: robin_map.h:448
std::pair< iterator, bool > insert(P &&value)
Definition: robin_map.h:257
T & operator[](const Key &key)
Definition: robin_map.h:453
iterator find(const Key &key, std::size_t precalculated_hash)
Definition: robin_map.h:494
T & at(const K &key)
Definition: robin_map.h:426
const T & at(const K &key) const
Definition: robin_map.h:442
size_type erase(const K &key, std::size_t precalculated_hash)
Definition: robin_map.h:391
friend bool operator!=(const robin_map &lhs, const robin_map &rhs)
Definition: robin_map.h:658
Definition: robin_growth_policy.h:68
iterator erase(const_iterator first, const_iterator last)
Definition: robin_map.h:366
const_iterator find(const Key &key) const
Definition: robin_map.h:496
std::pair< iterator, bool > insert(const value_type &value)
Definition: robin_map.h:252
void insert(InputIt first, InputIt last)
Definition: robin_map.h:281
const_iterator find(const K &key, std::size_t precalculated_hash) const
Definition: robin_map.h:534
iterator begin() noexcept
Definition: robin_map.h:229
const_iterator begin() const noexcept
Definition: robin_map.h:230
std::pair< iterator, iterator > equal_range(const Key &key, std::size_t precalculated_hash)
Definition: robin_map.h:547
void max_load_factor(float ml)
Definition: robin_map.h:620
void swap(robin_map &other)
Definition: robin_map.h:397
const_iterator end() const noexcept
Definition: robin_map.h:234
iterator insert(const_iterator hint, value_type &&value)
Definition: robin_map.h:275
size_type count(const K &key) const
Definition: robin_map.h:474
bool empty() const noexcept
Definition: robin_map.h:241
size_type bucket_count() const
Definition: robin_map.h:599
std::pair< iterator, iterator > equal_range(const Key &key)
Definition: robin_map.h:541
T & operator[](Key &&key)
Definition: robin_map.h:454
Definition: robin_map.h:85
size_type max_size() const noexcept
Definition: robin_map.h:243
const T & at(const Key &key, std::size_t precalculated_hash) const
Definition: robin_map.h:418
iterator try_emplace(const_iterator hint, const key_type &k, Args &&... args)
Definition: robin_map.h:352
std::pair< iterator, bool > insert_or_assign(const key_type &k, M &&obj)
Definition: robin_map.h:293
size_type count(const K &key, std::size_t precalculated_hash) const
Definition: robin_map.h:483
robin_map & operator=(std::initializer_list< value_type > ilist)
Definition: robin_map.h:214
std::pair< iterator, bool > try_emplace(const key_type &k, Args &&... args)
Definition: robin_map.h:342
iterator insert_or_assign(const_iterator hint, const key_type &k, M &&obj)
Definition: robin_map.h:303
key_equal key_eq() const
Definition: robin_map.h:630
void min_load_factor(float ml)
Definition: robin_map.h:619
void rehash(size_type count)
Definition: robin_map.h:622
robin_map(size_type bucket_count, const Allocator &alloc)
Definition: robin_map.h:151
robin_map()
Definition: robin_map.h:140
std::pair< iterator, bool > try_emplace(key_type &&k, Args &&... args)
Definition: robin_map.h:347
T & at(const Key &key, std::size_t precalculated_hash)
Definition: robin_map.h:410
size_type erase(const K &key)
Definition: robin_map.h:382
iterator insert(const_iterator hint, P &&value)
Definition: robin_map.h:271
std::pair< iterator, iterator > equal_range(const K &key, std::size_t precalculated_hash)
Definition: robin_map.h:575
float load_factor() const
Definition: robin_map.h:606
iterator erase(const_iterator pos)
Definition: robin_map.h:365
std::pair< const_iterator, const_iterator > equal_range(const K &key) const
Definition: robin_map.h:583
const_iterator find(const K &key) const
Definition: robin_map.h:525
robin_map(std::initializer_list< value_type > init, size_type bucket_count, const Allocator &alloc)
Definition: robin_map.h:199
const T & at(const Key &key) const
Definition: robin_map.h:413
const_iterator find(const Key &key, std::size_t precalculated_hash) const
Definition: robin_map.h:501
robin_map(InputIt first, InputIt last, size_type bucket_count=ht::DEFAULT_INIT_BUCKETS_SIZE, const Hash &hash=Hash(), const KeyEqual &equal=KeyEqual(), const Allocator &alloc=Allocator())
Definition: robin_map.h:166
std::pair< const_iterator, const_iterator > equal_range(const Key &key, std::size_t precalculated_hash) const
Definition: robin_map.h:556
T & at(const Key &key)
Definition: robin_map.h:404
allocator_type get_allocator() const
Definition: robin_map.h:223
iterator find(const Key &key)
Definition: robin_map.h:488
robin_map(std::initializer_list< value_type > init, size_type bucket_count=ht::DEFAULT_INIT_BUCKETS_SIZE, const Hash &hash=Hash(), const KeyEqual &equal=KeyEqual(), const Allocator &alloc=Allocator())
Definition: robin_map.h:190
robin_map(InputIt first, InputIt last, size_type bucket_count, const Hash &hash, const Allocator &alloc)
Definition: robin_map.h:183
std::pair< iterator, bool > insert_or_assign(key_type &&k, M &&obj)
Definition: robin_map.h:298
iterator find(const K &key)
Definition: robin_map.h:510
void reserve(size_type count)
Definition: robin_map.h:623
const_iterator cbegin() const noexcept
Definition: robin_map.h:231
friend bool operator==(const robin_map &lhs, const robin_map &rhs)
Definition: robin_map.h:643
size_type count(const Key &key) const
Definition: robin_map.h:459
T & at(const K &key, std::size_t precalculated_hash)
Definition: robin_map.h:435
iterator emplace_hint(const_iterator hint, Args &&... args)
Definition: robin_map.h:334
size_type count(const Key &key, std::size_t precalculated_hash) const
Definition: robin_map.h:465
Definition: robin_growth_policy.h:276
robin_map(std::initializer_list< value_type > init, size_type bucket_count, const Hash &hash, const Allocator &alloc)
Definition: robin_map.h:206
std::pair< iterator, iterator > equal_range(const K &key)
Definition: robin_map.h:565
void insert(std::initializer_list< value_type > ilist)
Definition: robin_map.h:285
robin_map(const Allocator &alloc)
Definition: robin_map.h:162
void clear() noexcept
Definition: robin_map.h:248
Definition: robin_hash.h:317
robin_map(InputIt first, InputIt last, size_type bucket_count, const Allocator &alloc)
Definition: robin_map.h:176
iterator insert(const_iterator hint, const value_type &value)
Definition: robin_map.h:266
friend void swap(robin_map &lhs, robin_map &rhs)
Definition: robin_map.h:662
size_type size() const noexcept
Definition: robin_map.h:242
size_type erase(const key_type &key)
Definition: robin_map.h:367
std::pair< const_iterator, const_iterator > equal_range(const K &key, std::size_t precalculated_hash) const
Definition: robin_map.h:589
std::pair< iterator, bool > insert(value_type &&value)
Definition: robin_map.h:261
iterator erase(iterator pos)
Definition: robin_map.h:364
iterator insert_or_assign(const_iterator hint, key_type &&k, M &&obj)
Definition: robin_map.h:308
robin_map(size_type bucket_count, const Hash &hash, const Allocator &alloc)
Definition: robin_map.h:156
Definition: robin_growth_policy.h:78
float max_load_factor() const
Definition: robin_map.h:609
const_iterator cend() const noexcept
Definition: robin_map.h:235
iterator mutable_iterator(const_iterator pos)
Definition: robin_map.h:639
size_type erase(const key_type &key, std::size_t precalculated_hash)
Definition: robin_map.h:373
Definition: robin_hash.h:47
iterator try_emplace(const_iterator hint, key_type &&k, Args &&... args)
Definition: robin_map.h:357
std::pair< iterator, bool > emplace(Args &&... args)
Definition: robin_map.h:321
std::pair< const_iterator, const_iterator > equal_range(const Key &key) const
Definition: robin_map.h:551