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