hopscotch-map
bhopscotch_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_BHOPSCOTCH_MAP_H
25 #define TSL_BHOPSCOTCH_MAP_H
26 
27 
28 #include <algorithm>
29 #include <cstddef>
30 #include <functional>
31 #include <initializer_list>
32 #include <map>
33 #include <memory>
34 #include <type_traits>
35 #include <utility>
36 #include "hopscotch_hash.h"
37 
38 
39 namespace tsl {
40 
41 
42 /**
43  * Similar to tsl::hopscotch_map but instead of using a list for overflowing elements it uses
44  * a binary search tree. It thus needs an additional template parameter Compare. Compare should
45  * be arithmetically coherent with KeyEqual.
46  *
47  * The binary search tree allows the map to have a worst-case scenario of O(log n) for search
48  * and delete, even if the hash function maps all the elements to the same bucket.
49  * For insert, the amortized worst case is O(log n), but the worst case is O(n) in case of rehash.
50  *
51  * This makes the map resistant to DoS attacks (but doesn't preclude you to have a good hash function,
52  * as an element in the bucket array is faster to retrieve than in the tree).
53  *
54  * @copydoc hopscotch_map
55  */
56 template<class Key,
57  class T,
58  class Hash = std::hash<Key>,
59  class KeyEqual = std::equal_to<Key>,
60  class Compare = std::less<Key>,
61  class Allocator = std::allocator<std::pair<const Key, T>>,
62  unsigned int NeighborhoodSize = 62,
63  bool StoreHash = false,
64  class GrowthPolicy = tsl::hh::power_of_two_growth_policy<2>>
66 private:
67  template<typename U>
68  using has_is_transparent = tsl::detail_hopscotch_hash::has_is_transparent<U>;
69 
70  class KeySelect {
71  public:
72  using key_type = Key;
73 
74  const key_type& operator()(const std::pair<const Key, T>& key_value) const {
75  return key_value.first;
76  }
77 
78  const key_type& operator()(std::pair<const Key, T>& key_value) {
79  return key_value.first;
80  }
81  };
82 
83  class ValueSelect {
84  public:
85  using value_type = T;
86 
87  const value_type& operator()(const std::pair<const Key, T>& key_value) const {
88  return key_value.second;
89  }
90 
91  value_type& operator()(std::pair<Key, T>& key_value) {
92  return key_value.second;
93  }
94  };
95 
96 
97  // TODO Not optimal as we have to use std::pair<const Key, T> as ValueType which forbid
98  // us to move the key in the bucket array, we have to use copy. Optimize.
99  using overflow_container_type = std::map<Key, T, Compare, Allocator>;
100  using ht = detail_hopscotch_hash::hopscotch_hash<std::pair<const Key, T>, KeySelect, ValueSelect,
101  Hash, KeyEqual,
102  Allocator, NeighborhoodSize,
103  StoreHash, GrowthPolicy,
104  overflow_container_type>;
105 
106 public:
107  using key_type = typename ht::key_type;
108  using mapped_type = T;
109  using value_type = typename ht::value_type;
110  using size_type = typename ht::size_type;
111  using difference_type = typename ht::difference_type;
112  using hasher = typename ht::hasher;
113  using key_equal = typename ht::key_equal;
114  using key_compare = Compare;
115  using allocator_type = typename ht::allocator_type;
116  using reference = typename ht::reference;
117  using const_reference = typename ht::const_reference;
118  using pointer = typename ht::pointer;
119  using const_pointer = typename ht::const_pointer;
120  using iterator = typename ht::iterator;
121  using const_iterator = typename ht::const_iterator;
122 
123 
124  /*
125  * Constructors
126  */
127  bhopscotch_map() : bhopscotch_map(ht::DEFAULT_INIT_BUCKETS_SIZE) {
128  }
129 
130  explicit bhopscotch_map(size_type bucket_count,
131  const Hash& hash = Hash(),
132  const KeyEqual& equal = KeyEqual(),
133  const Allocator& alloc = Allocator(),
134  const Compare& comp = Compare()) :
135  m_ht(bucket_count, hash, equal, alloc, ht::DEFAULT_MAX_LOAD_FACTOR, comp)
136  {
137  }
138 
139  bhopscotch_map(size_type bucket_count,
140  const Allocator& alloc) : bhopscotch_map(bucket_count, Hash(), KeyEqual(), alloc)
141  {
142  }
143 
144  bhopscotch_map(size_type bucket_count,
145  const Hash& hash,
146  const Allocator& alloc) : bhopscotch_map(bucket_count, hash, KeyEqual(), alloc)
147  {
148  }
149 
150  explicit bhopscotch_map(const Allocator& alloc) : bhopscotch_map(ht::DEFAULT_INIT_BUCKETS_SIZE, alloc) {
151  }
152 
153  template<class InputIt>
154  bhopscotch_map(InputIt first, InputIt last,
155  size_type bucket_count = ht::DEFAULT_INIT_BUCKETS_SIZE,
156  const Hash& hash = Hash(),
157  const KeyEqual& equal = KeyEqual(),
158  const Allocator& alloc = Allocator()) : bhopscotch_map(bucket_count, hash, equal, alloc)
159  {
160  insert(first, last);
161  }
162 
163  template<class InputIt>
164  bhopscotch_map(InputIt first, InputIt last,
165  size_type bucket_count,
166  const Allocator& alloc) : bhopscotch_map(first, last, bucket_count, Hash(), KeyEqual(), alloc)
167  {
168  }
169 
170  template<class InputIt>
171  bhopscotch_map(InputIt first, InputIt last,
172  size_type bucket_count,
173  const Hash& hash,
174  const Allocator& alloc) : bhopscotch_map(first, last, bucket_count, hash, KeyEqual(), alloc)
175  {
176  }
177 
178  bhopscotch_map(std::initializer_list<value_type> init,
179  size_type bucket_count = ht::DEFAULT_INIT_BUCKETS_SIZE,
180  const Hash& hash = Hash(),
181  const KeyEqual& equal = KeyEqual(),
182  const Allocator& alloc = Allocator()) :
183  bhopscotch_map(init.begin(), init.end(), bucket_count, hash, equal, alloc)
184  {
185  }
186 
187  bhopscotch_map(std::initializer_list<value_type> init,
188  size_type bucket_count,
189  const Allocator& alloc) :
190  bhopscotch_map(init.begin(), init.end(), bucket_count, Hash(), KeyEqual(), alloc)
191  {
192  }
193 
194  bhopscotch_map(std::initializer_list<value_type> init,
195  size_type bucket_count,
196  const Hash& hash,
197  const Allocator& alloc) :
198  bhopscotch_map(init.begin(), init.end(), bucket_count, hash, KeyEqual(), alloc)
199  {
200  }
201 
202 
203  bhopscotch_map& operator=(std::initializer_list<value_type> ilist) {
204  m_ht.clear();
205 
206  m_ht.reserve(ilist.size());
207  m_ht.insert(ilist.begin(), ilist.end());
208 
209  return *this;
210  }
211 
212  allocator_type get_allocator() const { return m_ht.get_allocator(); }
213 
214 
215  /*
216  * Iterators
217  */
218  iterator begin() noexcept { return m_ht.begin(); }
219  const_iterator begin() const noexcept { return m_ht.begin(); }
220  const_iterator cbegin() const noexcept { return m_ht.cbegin(); }
221 
222  iterator end() noexcept { return m_ht.end(); }
223  const_iterator end() const noexcept { return m_ht.end(); }
224  const_iterator cend() const noexcept { return m_ht.cend(); }
225 
226 
227  /*
228  * Capacity
229  */
230  bool empty() const noexcept { return m_ht.empty(); }
231  size_type size() const noexcept { return m_ht.size(); }
232  size_type max_size() const noexcept { return m_ht.max_size(); }
233 
234  /*
235  * Modifiers
236  */
237  void clear() noexcept { m_ht.clear(); }
238 
239 
240 
241 
242  std::pair<iterator, bool> insert(const value_type& value) {
243  return m_ht.insert(value);
244  }
245 
246  template<class P, typename std::enable_if<std::is_constructible<value_type, P&&>::value>::type* = nullptr>
247  std::pair<iterator, bool> insert(P&& value) {
248  return m_ht.insert(std::forward<P>(value));
249  }
250 
251  std::pair<iterator, bool> insert(value_type&& value) {
252  return m_ht.insert(std::move(value));
253  }
254 
255 
256  iterator insert(const_iterator hint, const value_type& value) {
257  return m_ht.insert(hint, value);
258  }
259 
260  template<class P, typename std::enable_if<std::is_constructible<value_type, P&&>::value>::type* = nullptr>
261  iterator insert(const_iterator hint, P&& value) {
262  return m_ht.insert(hint, std::forward<P>(value));
263  }
264 
265  iterator insert(const_iterator hint, value_type&& value) {
266  return m_ht.insert(hint, std::move(value));
267  }
268 
269 
270  template<class InputIt>
271  void insert(InputIt first, InputIt last) {
272  m_ht.insert(first, last);
273  }
274 
275  void insert(std::initializer_list<value_type> ilist) {
276  m_ht.insert(ilist.begin(), ilist.end());
277  }
278 
279 
280 
281 
282  template<class M>
283  std::pair<iterator, bool> insert_or_assign(const key_type& k, M&& obj) {
284  return m_ht.insert_or_assign(k, std::forward<M>(obj));
285  }
286 
287  template<class M>
288  std::pair<iterator, bool> insert_or_assign(key_type&& k, M&& obj) {
289  return m_ht.insert_or_assign(std::move(k), std::forward<M>(obj));
290  }
291 
292  template<class M>
293  iterator insert_or_assign(const_iterator hint, const key_type& k, M&& obj) {
294  return m_ht.insert_or_assign(hint, k, std::forward<M>(obj));
295  }
296 
297  template<class M>
298  iterator insert_or_assign(const_iterator hint, key_type&& k, M&& obj) {
299  return m_ht.insert_or_assign(hint, std::move(k), std::forward<M>(obj));
300  }
301 
302 
303 
304  /**
305  * Due to the way elements are stored, emplace will need to move or copy the key-value once.
306  * The method is equivalent to insert(value_type(std::forward<Args>(args)...));
307  *
308  * Mainly here for compatibility with the std::unordered_map interface.
309  */
310  template<class... Args>
311  std::pair<iterator, bool> emplace(Args&&... args) {
312  return m_ht.emplace(std::forward<Args>(args)...);
313  }
314 
315 
316 
317 
318  /**
319  * Due to the way elements are stored, emplace_hint will need to move or copy the key-value once.
320  * The method is equivalent to insert(hint, value_type(std::forward<Args>(args)...));
321  *
322  * Mainly here for compatibility with the std::unordered_map interface.
323  */
324  template<class... Args>
325  iterator emplace_hint(const_iterator hint, Args&&... args) {
326  return m_ht.emplace_hint(hint, std::forward<Args>(args)...);
327  }
328 
329 
330 
331 
332  template<class... Args>
333  std::pair<iterator, bool> try_emplace(const key_type& k, Args&&... args) {
334  return m_ht.try_emplace(k, std::forward<Args>(args)...);
335  }
336 
337  template<class... Args>
338  std::pair<iterator, bool> try_emplace(key_type&& k, Args&&... args) {
339  return m_ht.try_emplace(std::move(k), std::forward<Args>(args)...);
340  }
341 
342  template<class... Args>
343  iterator try_emplace(const_iterator hint, const key_type& k, Args&&... args) {
344  return m_ht.try_emplace(hint, k, std::forward<Args>(args)...);
345  }
346 
347  template<class... Args>
348  iterator try_emplace(const_iterator hint, key_type&& k, Args&&... args) {
349  return m_ht.try_emplace(hint, std::move(k), std::forward<Args>(args)...);
350  }
351 
352 
353 
354 
355  iterator erase(iterator pos) { return m_ht.erase(pos); }
356  iterator erase(const_iterator pos) { return m_ht.erase(pos); }
357  iterator erase(const_iterator first, const_iterator last) { return m_ht.erase(first, last); }
358  size_type erase(const key_type& key) { return m_ht.erase(key); }
359 
360  /**
361  * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
362  * as hash_function()(key). Usefull to speed-up the lookup to the value if you already have the hash.
363  */
364  size_type erase(const key_type& key, std::size_t precalculated_hash) {
365  return m_ht.erase(key, precalculated_hash);
366  }
367 
368  /**
369  * This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent
370  * and Compare::is_transparent exist.
371  * If so, K must be hashable and comparable to Key.
372  */
373  template<class K, class KE = KeyEqual, class CP = Compare,
374  typename std::enable_if<has_is_transparent<KE>::value && has_is_transparent<CP>::value>::type* = nullptr>
375  size_type erase(const K& key) { return m_ht.erase(key); }
376 
377  /**
378  * @copydoc erase(const K& key)
379  *
380  * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
381  * as hash_function()(key). Usefull to speed-up the lookup to the value if you already have the hash.
382  */
383  template<class K, class KE = KeyEqual, class CP = Compare,
384  typename std::enable_if<has_is_transparent<KE>::value && has_is_transparent<CP>::value>::type* = nullptr>
385  size_type erase(const K& key, std::size_t precalculated_hash) { return m_ht.erase(key, precalculated_hash); }
386 
387 
388 
389 
390  void swap(bhopscotch_map& other) { other.m_ht.swap(m_ht); }
391 
392  /*
393  * Lookup
394  */
395  T& at(const Key& key) { return m_ht.at(key); }
396 
397  /**
398  * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
399  * as hash_function()(key). Usefull to speed-up the lookup if you already have the hash.
400  */
401  T& at(const Key& key, std::size_t precalculated_hash) { return m_ht.at(key, precalculated_hash); }
402 
403  const T& at(const Key& key) const { return m_ht.at(key); }
404 
405  /**
406  * @copydoc at(const Key& key, std::size_t precalculated_hash)
407  */
408  const T& at(const Key& key, std::size_t precalculated_hash) const { return m_ht.at(key, precalculated_hash); }
409 
410  /**
411  * This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent
412  * and Compare::is_transparent exist.
413  * If so, K must be hashable and comparable to Key.
414  */
415  template<class K, class KE = KeyEqual, class CP = Compare,
416  typename std::enable_if<has_is_transparent<KE>::value && has_is_transparent<CP>::value>::type* = nullptr>
417  T& at(const K& key) { return m_ht.at(key); }
418 
419  /**
420  * @copydoc at(const K& key)
421  *
422  * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
423  * as hash_function()(key). Usefull to speed-up the lookup if you already have the hash.
424  */
425  template<class K, class KE = KeyEqual, class CP = Compare,
426  typename std::enable_if<has_is_transparent<KE>::value && has_is_transparent<CP>::value>::type* = nullptr>
427  T& at(const K& key, std::size_t precalculated_hash) { return m_ht.at(key, precalculated_hash); }
428 
429  /**
430  * @copydoc at(const K& key)
431  */
432  template<class K, class KE = KeyEqual, class CP = Compare,
433  typename std::enable_if<has_is_transparent<KE>::value && has_is_transparent<CP>::value>::type* = nullptr>
434  const T& at(const K& key) const { return m_ht.at(key); }
435 
436  /**
437  * @copydoc at(const K& key, std::size_t precalculated_hash)
438  */
439  template<class K, class KE = KeyEqual, class CP = Compare,
440  typename std::enable_if<has_is_transparent<KE>::value && has_is_transparent<CP>::value>::type* = nullptr>
441  const T& at(const K& key, std::size_t precalculated_hash) const { return m_ht.at(key, precalculated_hash); }
442 
443 
444 
445 
446  T& operator[](const Key& key) { return m_ht[key]; }
447  T& operator[](Key&& key) { return m_ht[std::move(key)]; }
448 
449 
450 
451 
452  size_type count(const Key& key) const { return m_ht.count(key); }
453 
454  /**
455  * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
456  * as hash_function()(key). Usefull to speed-up the lookup if you already have the hash.
457  */
458  size_type count(const Key& key, std::size_t precalculated_hash) const { return m_ht.count(key, precalculated_hash); }
459 
460  /**
461  * This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent
462  * and Compare::is_transparent exist.
463  * If so, K must be hashable and comparable to Key.
464  */
465  template<class K, class KE = KeyEqual, class CP = Compare,
466  typename std::enable_if<has_is_transparent<KE>::value && has_is_transparent<CP>::value>::type* = nullptr>
467  size_type count(const K& key) const { return m_ht.count(key); }
468 
469  /**
470  * @copydoc count(const K& key) const
471  *
472  * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
473  * as hash_function()(key). Usefull to speed-up the lookup if you already have the hash.
474  */
475  template<class K, class KE = KeyEqual, class CP = Compare,
476  typename std::enable_if<has_is_transparent<KE>::value && has_is_transparent<CP>::value>::type* = nullptr>
477  size_type count(const K& key, std::size_t precalculated_hash) const { return m_ht.count(key, precalculated_hash); }
478 
479 
480 
481 
482  iterator find(const Key& key) { return m_ht.find(key); }
483 
484  /**
485  * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
486  * as hash_function()(key). Usefull to speed-up the lookup if you already have the hash.
487  */
488  iterator find(const Key& key, std::size_t precalculated_hash) { return m_ht.find(key, precalculated_hash); }
489 
490  const_iterator find(const Key& key) const { return m_ht.find(key); }
491 
492  /**
493  * @copydoc find(const Key& key, std::size_t precalculated_hash)
494  */
495  const_iterator find(const Key& key, std::size_t precalculated_hash) const { return m_ht.find(key, precalculated_hash); }
496 
497  /**
498  * This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent
499  * and Compare::is_transparent exist.
500  * If so, K must be hashable and comparable to Key.
501  */
502  template<class K, class KE = KeyEqual, class CP = Compare,
503  typename std::enable_if<has_is_transparent<KE>::value && has_is_transparent<CP>::value>::type* = nullptr>
504  iterator find(const K& key) { return m_ht.find(key); }
505 
506  /**
507  * @copydoc find(const K& key)
508  *
509  * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
510  * as hash_function()(key). Usefull to speed-up the lookup if you already have the hash.
511  */
512  template<class K, class KE = KeyEqual, class CP = Compare,
513  typename std::enable_if<has_is_transparent<KE>::value && has_is_transparent<CP>::value>::type* = nullptr>
514  iterator find(const K& key, std::size_t precalculated_hash) { return m_ht.find(key, precalculated_hash); }
515 
516  /**
517  * @copydoc find(const K& key)
518  */
519  template<class K, class KE = KeyEqual, class CP = Compare,
520  typename std::enable_if<has_is_transparent<KE>::value && has_is_transparent<CP>::value>::type* = nullptr>
521  const_iterator find(const K& key) const { return m_ht.find(key); }
522 
523  /**
524  * @copydoc find(const K& key, std::size_t precalculated_hash)
525  */
526  template<class K, class KE = KeyEqual, class CP = Compare,
527  typename std::enable_if<has_is_transparent<KE>::value && has_is_transparent<CP>::value>::type* = nullptr>
528  const_iterator find(const K& key, std::size_t precalculated_hash) const { return m_ht.find(key, precalculated_hash); }
529 
530 
531 
532 
533  std::pair<iterator, iterator> equal_range(const Key& key) { return m_ht.equal_range(key); }
534 
535  /**
536  * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
537  * as hash_function()(key). Usefull to speed-up the lookup if you already have the hash.
538  */
539  std::pair<iterator, iterator> equal_range(const Key& key, std::size_t precalculated_hash) {
540  return m_ht.equal_range(key, precalculated_hash);
541  }
542 
543  std::pair<const_iterator, const_iterator> equal_range(const Key& key) const { return m_ht.equal_range(key); }
544 
545  /**
546  * @copydoc equal_range(const Key& key, std::size_t precalculated_hash)
547  */
548  std::pair<const_iterator, const_iterator> equal_range(const Key& key, std::size_t precalculated_hash) const {
549  return m_ht.equal_range(key, precalculated_hash);
550  }
551 
552  /**
553  * This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent
554  * and Compare::is_transparent exist.
555  * If so, K must be hashable and comparable to Key.
556  */
557  template<class K, class KE = KeyEqual, class CP = Compare,
558  typename std::enable_if<has_is_transparent<KE>::value && has_is_transparent<CP>::value>::type* = nullptr>
559  std::pair<iterator, iterator> equal_range(const K& key) { return m_ht.equal_range(key); }
560 
561  /**
562  * @copydoc equal_range(const K& key)
563  *
564  * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
565  * as hash_function()(key). Usefull to speed-up the lookup if you already have the hash.
566  */
567  template<class K, class KE = KeyEqual, class CP = Compare,
568  typename std::enable_if<has_is_transparent<KE>::value && has_is_transparent<CP>::value>::type* = nullptr>
569  std::pair<iterator, iterator> equal_range(const K& key, std::size_t precalculated_hash) {
570  return m_ht.equal_range(key, precalculated_hash);
571  }
572 
573  /**
574  * @copydoc equal_range(const K& key)
575  */
576  template<class K, class KE = KeyEqual, class CP = Compare,
577  typename std::enable_if<has_is_transparent<KE>::value && has_is_transparent<CP>::value>::type* = nullptr>
578  std::pair<const_iterator, const_iterator> equal_range(const K& key) const { return m_ht.equal_range(key); }
579 
580  /**
581  * @copydoc equal_range(const K& key, std::size_t precalculated_hash)
582  */
583  template<class K, class KE = KeyEqual, class CP = Compare,
584  typename std::enable_if<has_is_transparent<KE>::value && has_is_transparent<CP>::value>::type* = nullptr>
585  std::pair<const_iterator, const_iterator> equal_range(const K& key, std::size_t precalculated_hash) const {
586  return m_ht.equal_range(key, precalculated_hash);
587  }
588 
589 
590 
591 
592  /*
593  * Bucket interface
594  */
595  size_type bucket_count() const { return m_ht.bucket_count(); }
596  size_type max_bucket_count() const { return m_ht.max_bucket_count(); }
597 
598 
599  /*
600  * Hash policy
601  */
602  float load_factor() const { return m_ht.load_factor(); }
603  float max_load_factor() const { return m_ht.max_load_factor(); }
604  void max_load_factor(float ml) { m_ht.max_load_factor(ml); }
605 
606  void rehash(size_type count_) { m_ht.rehash(count_); }
607  void reserve(size_type count_) { m_ht.reserve(count_); }
608 
609 
610  /*
611  * Observers
612  */
613  hasher hash_function() const { return m_ht.hash_function(); }
614  key_equal key_eq() const { return m_ht.key_eq(); }
615  key_compare key_comp() const { return m_ht.key_comp(); }
616 
617  /*
618  * Other
619  */
620 
621  /**
622  * Convert a const_iterator to an iterator.
623  */
624  iterator mutable_iterator(const_iterator pos) {
625  return m_ht.mutable_iterator(pos);
626  }
627 
628  size_type overflow_size() const noexcept { return m_ht.overflow_size(); }
629 
630  friend bool operator==(const bhopscotch_map& lhs, const bhopscotch_map& rhs) {
631  if(lhs.size() != rhs.size()) {
632  return false;
633  }
634 
635  for(const auto& element_lhs : lhs) {
636  const auto it_element_rhs = rhs.find(element_lhs.first);
637  if(it_element_rhs == rhs.cend() || element_lhs.second != it_element_rhs->second) {
638  return false;
639  }
640  }
641 
642  return true;
643  }
644 
645  friend bool operator!=(const bhopscotch_map& lhs, const bhopscotch_map& rhs) {
646  return !operator==(lhs, rhs);
647  }
648 
649  friend void swap(bhopscotch_map& lhs, bhopscotch_map& rhs) {
650  lhs.swap(rhs);
651  }
652 
653 
654 
655 private:
656  ht m_ht;
657 };
658 
659 
660 /**
661  * Same as `tsl::bhopscotch_map<Key, T, Hash, KeyEqual, Compare, Allocator, NeighborhoodSize, StoreHash, tsl::hh::prime_growth_policy>`.
662  */
663 template<class Key,
664  class T,
665  class Hash = std::hash<Key>,
666  class KeyEqual = std::equal_to<Key>,
667  class Compare = std::less<Key>,
668  class Allocator = std::allocator<std::pair<const Key, T>>,
669  unsigned int NeighborhoodSize = 62,
670  bool StoreHash = false>
671 using bhopscotch_pg_map = bhopscotch_map<Key, T, Hash, KeyEqual, Compare, Allocator, NeighborhoodSize, StoreHash, tsl::hh::prime_growth_policy>;
672 
673 } // end namespace tsl
674 
675 #endif
std::pair< iterator, bool > insert_or_assign(key_type &&k, M &&obj)
Definition: bhopscotch_map.h:288
const_iterator cbegin() const noexcept
Definition: bhopscotch_map.h:220
size_type bucket_count() const
Definition: bhopscotch_map.h:595
std::pair< iterator, bool > insert(const value_type &value)
Definition: bhopscotch_map.h:242
iterator find(const K &key)
Definition: bhopscotch_map.h:504
void insert(InputIt first, InputIt last)
Definition: bhopscotch_map.h:271
T & at(const Key &key)
Definition: bhopscotch_map.h:395
iterator begin() noexcept
Definition: bhopscotch_map.h:218
size_type overflow_size() const noexcept
Definition: bhopscotch_map.h:628
void swap(bhopscotch_map &other)
Definition: bhopscotch_map.h:390
std::pair< const_iterator, const_iterator > equal_range(const Key &key) const
Definition: bhopscotch_map.h:543
bhopscotch_map()
Definition: bhopscotch_map.h:127
Definition: hopscotch_hash.h:69
T & operator[](Key &&key)
Definition: bhopscotch_map.h:447
Definition: bhopscotch_map.h:39
iterator find(const Key &key, std::size_t precalculated_hash)
Definition: bhopscotch_map.h:488
std::pair< iterator, iterator > equal_range(const Key &key)
Definition: bhopscotch_map.h:533
T & operator[](const Key &key)
Definition: bhopscotch_map.h:446
const T & at(const K &key) const
Definition: bhopscotch_map.h:434
bhopscotch_map(InputIt first, InputIt last, size_type bucket_count, const Allocator &alloc)
Definition: bhopscotch_map.h:164
std::pair< iterator, bool > insert(P &&value)
Definition: bhopscotch_map.h:247
std::pair< iterator, iterator > equal_range(const K &key, std::size_t precalculated_hash)
Definition: bhopscotch_map.h:569
iterator mutable_iterator(const_iterator pos)
Definition: bhopscotch_map.h:624
void insert(std::initializer_list< value_type > ilist)
Definition: bhopscotch_map.h:275
bool empty() const noexcept
Definition: bhopscotch_map.h:230
bhopscotch_map(const Allocator &alloc)
Definition: bhopscotch_map.h:150
const_iterator find(const K &key, std::size_t precalculated_hash) const
Definition: bhopscotch_map.h:528
const T & at(const Key &key, std::size_t precalculated_hash) const
Definition: bhopscotch_map.h:408
bhopscotch_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: bhopscotch_map.h:178
std::pair< const_iterator, const_iterator > equal_range(const K &key) const
Definition: bhopscotch_map.h:578
iterator erase(const_iterator pos)
Definition: bhopscotch_map.h:356
const_iterator end() const noexcept
Definition: bhopscotch_map.h:223
bhopscotch_map(InputIt first, InputIt last, size_type bucket_count, const Hash &hash, const Allocator &alloc)
Definition: bhopscotch_map.h:171
key_compare key_comp() const
Definition: bhopscotch_map.h:615
const_iterator begin() const noexcept
Definition: bhopscotch_map.h:219
const_iterator find(const K &key) const
Definition: bhopscotch_map.h:521
iterator end() noexcept
Definition: bhopscotch_map.h:222
bhopscotch_map(size_type bucket_count, const Hash &hash=Hash(), const KeyEqual &equal=KeyEqual(), const Allocator &alloc=Allocator(), const Compare &comp=Compare())
Definition: bhopscotch_map.h:130
bhopscotch_map & operator=(std::initializer_list< value_type > ilist)
Definition: bhopscotch_map.h:203
friend bool operator==(const bhopscotch_map &lhs, const bhopscotch_map &rhs)
Definition: bhopscotch_map.h:630
iterator find(const Key &key)
Definition: bhopscotch_map.h:482
const_iterator find(const Key &key, std::size_t precalculated_hash) const
Definition: bhopscotch_map.h:495
const T & at(const Key &key) const
Definition: bhopscotch_map.h:403
friend bool operator!=(const bhopscotch_map &lhs, const bhopscotch_map &rhs)
Definition: bhopscotch_map.h:645
const T & at(const K &key, std::size_t precalculated_hash) const
Definition: bhopscotch_map.h:441
iterator try_emplace(const_iterator hint, const key_type &k, Args &&... args)
Definition: bhopscotch_map.h:343
hasher hash_function() const
Definition: bhopscotch_map.h:613
std::pair< iterator, bool > insert(value_type &&value)
Definition: bhopscotch_map.h:251
size_type erase(const key_type &key)
Definition: bhopscotch_map.h:358
size_type erase(const key_type &key, std::size_t precalculated_hash)
Definition: bhopscotch_map.h:364
std::pair< iterator, bool > try_emplace(key_type &&k, Args &&... args)
Definition: bhopscotch_map.h:338
T & at(const Key &key, std::size_t precalculated_hash)
Definition: bhopscotch_map.h:401
iterator find(const K &key, std::size_t precalculated_hash)
Definition: bhopscotch_map.h:514
void reserve(size_type count_)
Definition: bhopscotch_map.h:607
T & at(const K &key, std::size_t precalculated_hash)
Definition: bhopscotch_map.h:427
size_type max_size() const noexcept
Definition: bhopscotch_map.h:232
std::pair< iterator, bool > emplace(Args &&... args)
Definition: bhopscotch_map.h:311
const_iterator find(const Key &key) const
Definition: bhopscotch_map.h:490
iterator emplace_hint(const_iterator hint, Args &&... args)
Definition: bhopscotch_map.h:325
iterator insert_or_assign(const_iterator hint, key_type &&k, M &&obj)
Definition: bhopscotch_map.h:298
Definition: hopscotch_growth_policy.h:49
iterator erase(iterator pos)
Definition: bhopscotch_map.h:355
size_type erase(const K &key, std::size_t precalculated_hash)
Definition: bhopscotch_map.h:385
size_type count(const Key &key) const
Definition: bhopscotch_map.h:452
bhopscotch_map(size_type bucket_count, const Hash &hash, const Allocator &alloc)
Definition: bhopscotch_map.h:144
Definition: hopscotch_growth_policy.h:247
iterator insert(const_iterator hint, value_type &&value)
Definition: bhopscotch_map.h:265
iterator insert(const_iterator hint, P &&value)
Definition: bhopscotch_map.h:261
allocator_type get_allocator() const
Definition: bhopscotch_map.h:212
iterator insert(const_iterator hint, const value_type &value)
Definition: bhopscotch_map.h:256
std::pair< iterator, bool > insert_or_assign(const key_type &k, M &&obj)
Definition: bhopscotch_map.h:283
bhopscotch_map(std::initializer_list< value_type > init, size_type bucket_count, const Allocator &alloc)
Definition: bhopscotch_map.h:187
iterator erase(const_iterator first, const_iterator last)
Definition: bhopscotch_map.h:357
void max_load_factor(float ml)
Definition: bhopscotch_map.h:604
const_iterator cend() const noexcept
Definition: bhopscotch_map.h:224
bhopscotch_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: bhopscotch_map.h:154
Definition: bhopscotch_map.h:65
size_type size() const noexcept
Definition: bhopscotch_map.h:231
size_type erase(const K &key)
Definition: bhopscotch_map.h:375
std::pair< const_iterator, const_iterator > equal_range(const K &key, std::size_t precalculated_hash) const
Definition: bhopscotch_map.h:585
size_type count(const K &key, std::size_t precalculated_hash) const
Definition: bhopscotch_map.h:477
std::pair< const_iterator, const_iterator > equal_range(const Key &key, std::size_t precalculated_hash) const
Definition: bhopscotch_map.h:548
std::pair< iterator, bool > try_emplace(const key_type &k, Args &&... args)
Definition: bhopscotch_map.h:333
size_type count(const Key &key, std::size_t precalculated_hash) const
Definition: bhopscotch_map.h:458
void clear() noexcept
Definition: bhopscotch_map.h:237
iterator try_emplace(const_iterator hint, key_type &&k, Args &&... args)
Definition: bhopscotch_map.h:348
float load_factor() const
Definition: bhopscotch_map.h:602
std::pair< iterator, iterator > equal_range(const Key &key, std::size_t precalculated_hash)
Definition: bhopscotch_map.h:539
iterator insert_or_assign(const_iterator hint, const key_type &k, M &&obj)
Definition: bhopscotch_map.h:293
bhopscotch_map(size_type bucket_count, const Allocator &alloc)
Definition: bhopscotch_map.h:139
size_type count(const K &key) const
Definition: bhopscotch_map.h:467
std::pair< iterator, iterator > equal_range(const K &key)
Definition: bhopscotch_map.h:559
T & at(const K &key)
Definition: bhopscotch_map.h:417
Definition: hopscotch_growth_policy.h:40
size_type max_bucket_count() const
Definition: bhopscotch_map.h:596
friend void swap(bhopscotch_map &lhs, bhopscotch_map &rhs)
Definition: bhopscotch_map.h:649
void rehash(size_type count_)
Definition: bhopscotch_map.h:606
bhopscotch_map(std::initializer_list< value_type > init, size_type bucket_count, const Hash &hash, const Allocator &alloc)
Definition: bhopscotch_map.h:194
key_equal key_eq() const
Definition: bhopscotch_map.h:614
float max_load_factor() const
Definition: bhopscotch_map.h:603