24 #ifndef TSL_HOPSCOTCH_MAP_H 25 #define TSL_HOPSCOTCH_MAP_H 31 #include <initializer_list> 34 #include <type_traits> 73 class Hash = std::hash<Key>,
74 class KeyEqual = std::equal_to<Key>,
75 class Allocator = std::allocator<std::pair<Key, T>>,
76 unsigned int NeighborhoodSize = 62,
77 bool StoreHash =
false,
88 const key_type& operator()(
const std::pair<Key, T>& key_value)
const {
89 return key_value.first;
92 key_type& operator()(std::pair<Key, T>& key_value) {
93 return key_value.first;
101 const value_type& operator()(
const std::pair<Key, T>& key_value)
const {
102 return key_value.second;
105 value_type& operator()(std::pair<Key, T>& key_value) {
106 return key_value.second;
111 using overflow_container_type = std::list<std::pair<Key, T>, Allocator>;
114 Allocator, NeighborhoodSize,
115 StoreHash, GrowthPolicy,
116 overflow_container_type>;
119 using key_type =
typename ht::key_type;
120 using mapped_type = T;
121 using value_type =
typename ht::value_type;
122 using size_type =
typename ht::size_type;
123 using difference_type =
typename ht::difference_type;
124 using hasher =
typename ht::hasher;
125 using key_equal =
typename ht::key_equal;
126 using allocator_type =
typename ht::allocator_type;
127 using reference =
typename ht::reference;
128 using const_reference =
typename ht::const_reference;
129 using pointer =
typename ht::pointer;
130 using const_pointer =
typename ht::const_pointer;
131 using iterator =
typename ht::iterator;
132 using const_iterator =
typename ht::const_iterator;
143 const Hash& hash = Hash(),
144 const KeyEqual& equal = KeyEqual(),
145 const Allocator& alloc = Allocator()) :
146 m_ht(bucket_count, hash, equal, alloc, ht::DEFAULT_MAX_LOAD_FACTOR)
151 const Allocator& alloc) :
hopscotch_map(bucket_count, Hash(), KeyEqual(), alloc)
157 const Allocator& alloc) :
hopscotch_map(bucket_count, hash, KeyEqual(), alloc)
164 template<
class InputIt>
166 size_type bucket_count = ht::DEFAULT_INIT_BUCKETS_SIZE,
167 const Hash& hash = Hash(),
168 const KeyEqual& equal = KeyEqual(),
169 const Allocator& alloc = Allocator()) :
hopscotch_map(bucket_count, hash, equal, alloc)
174 template<
class InputIt>
176 size_type bucket_count,
177 const Allocator& alloc) :
hopscotch_map(first, last, bucket_count, Hash(), KeyEqual(), alloc)
181 template<
class InputIt>
183 size_type bucket_count,
185 const Allocator& alloc) :
hopscotch_map(first, last, bucket_count, hash, KeyEqual(), alloc)
190 size_type bucket_count = ht::DEFAULT_INIT_BUCKETS_SIZE,
191 const Hash& hash = Hash(),
192 const KeyEqual& equal = KeyEqual(),
193 const Allocator& alloc = Allocator()) :
194 hopscotch_map(init.begin(), init.end(), bucket_count, hash, equal, alloc)
199 size_type bucket_count,
200 const Allocator& alloc) :
201 hopscotch_map(init.begin(), init.end(), bucket_count, Hash(), KeyEqual(), alloc)
206 size_type bucket_count,
208 const Allocator& alloc) :
209 hopscotch_map(init.begin(), init.end(), bucket_count, hash, KeyEqual(), alloc)
217 m_ht.reserve(ilist.size());
218 m_ht.insert(ilist.begin(), ilist.end());
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(); }
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(); }
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(); }
248 void clear()
noexcept { m_ht.clear(); }
253 std::pair<iterator,
bool>
insert(
const value_type& value) {
254 return m_ht.insert(value);
257 template<
class P,
typename std::enable_if<std::is_constructible<value_type, P&&>::value>::type* =
nullptr>
258 std::pair<iterator,
bool>
insert(P&& value) {
259 return m_ht.insert(std::forward<P>(value));
262 std::pair<iterator,
bool>
insert(value_type&& value) {
263 return m_ht.insert(std::move(value));
267 iterator
insert(const_iterator hint,
const value_type& value) {
268 return m_ht.insert(hint, value);
271 template<
class P,
typename std::enable_if<std::is_constructible<value_type, P&&>::value>::type* =
nullptr>
272 iterator
insert(const_iterator hint, P&& value) {
273 return m_ht.insert(hint, std::forward<P>(value));
276 iterator
insert(const_iterator hint, value_type&& value) {
277 return m_ht.insert(hint, std::move(value));
281 template<
class InputIt>
282 void insert(InputIt first, InputIt last) {
283 m_ht.insert(first, last);
286 void insert(std::initializer_list<value_type> ilist) {
287 m_ht.insert(ilist.begin(), ilist.end());
295 return m_ht.insert_or_assign(k, std::forward<M>(obj));
300 return m_ht.insert_or_assign(std::move(k), std::forward<M>(obj));
305 return m_ht.insert_or_assign(hint, k, std::forward<M>(obj));
310 return m_ht.insert_or_assign(hint, std::move(k), std::forward<M>(obj));
322 template<
class... Args>
323 std::pair<iterator,
bool>
emplace(Args&&... args) {
324 return m_ht.emplace(std::forward<Args>(args)...);
336 template<
class... Args>
338 return m_ht.emplace_hint(hint, std::forward<Args>(args)...);
344 template<
class... Args>
345 std::pair<iterator,
bool>
try_emplace(
const key_type& k, Args&&... args) {
346 return m_ht.try_emplace(k, std::forward<Args>(args)...);
349 template<
class... Args>
350 std::pair<iterator,
bool>
try_emplace(key_type&& k, Args&&... args) {
351 return m_ht.try_emplace(std::move(k), std::forward<Args>(args)...);
354 template<
class... Args>
355 iterator
try_emplace(const_iterator hint,
const key_type& k, Args&&... args) {
356 return m_ht.try_emplace(hint, k, std::forward<Args>(args)...);
359 template<
class... Args>
360 iterator
try_emplace(const_iterator hint, key_type&& k, Args&&... args) {
361 return m_ht.try_emplace(hint, std::move(k), std::forward<Args>(args)...);
367 iterator
erase(iterator pos) {
return m_ht.erase(pos); }
368 iterator
erase(const_iterator pos) {
return m_ht.erase(pos); }
369 iterator
erase(const_iterator first, const_iterator last) {
return m_ht.erase(first, last); }
370 size_type
erase(
const key_type& key) {
return m_ht.erase(key); }
376 size_type
erase(
const key_type& key, std::size_t precalculated_hash) {
377 return m_ht.erase(key, precalculated_hash);
384 template<
class K,
class KE = KeyEqual,
typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
385 size_type
erase(
const K& key) {
return m_ht.erase(key); }
393 template<
class K,
class KE = KeyEqual,
typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
394 size_type
erase(
const K& key, std::size_t precalculated_hash) {
395 return m_ht.erase(key, precalculated_hash);
406 T&
at(
const Key& key) {
return m_ht.at(key); }
412 T&
at(
const Key& key, std::size_t precalculated_hash) {
return m_ht.at(key, precalculated_hash); }
415 const T&
at(
const Key& key)
const {
return m_ht.at(key); }
420 const T&
at(
const Key& key, std::size_t precalculated_hash)
const {
return m_ht.at(key, precalculated_hash); }
427 template<
class K,
class KE = KeyEqual,
typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
428 T&
at(
const K& key) {
return m_ht.at(key); }
436 template<
class K,
class KE = KeyEqual,
typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
437 T&
at(
const K& key, std::size_t precalculated_hash) {
return m_ht.at(key, precalculated_hash); }
443 template<
class K,
class KE = KeyEqual,
typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
444 const T&
at(
const K& key)
const {
return m_ht.at(key); }
449 template<
class K,
class KE = KeyEqual,
typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
450 const T&
at(
const K& key, std::size_t precalculated_hash)
const {
return m_ht.at(key, precalculated_hash); }
455 T&
operator[](
const Key& key) {
return m_ht[key]; }
456 T&
operator[](Key&& key) {
return m_ht[std::move(key)]; }
461 size_type
count(
const Key& key)
const {
return m_ht.count(key); }
467 size_type
count(
const Key& key, std::size_t precalculated_hash)
const {
468 return m_ht.count(key, precalculated_hash);
475 template<
class K,
class KE = KeyEqual,
typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
476 size_type
count(
const K& key)
const {
return m_ht.count(key); }
484 template<
class K,
class KE = KeyEqual,
typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
485 size_type
count(
const K& key, std::size_t precalculated_hash)
const {
return m_ht.count(key, precalculated_hash); }
490 iterator
find(
const Key& key) {
return m_ht.find(key); }
496 iterator
find(
const Key& key, std::size_t precalculated_hash) {
return m_ht.find(key, precalculated_hash); }
498 const_iterator
find(
const Key& key)
const {
return m_ht.find(key); }
503 const_iterator
find(
const Key& key, std::size_t precalculated_hash)
const {
504 return m_ht.find(key, precalculated_hash);
511 template<
class K,
class KE = KeyEqual,
typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
512 iterator
find(
const K& key) {
return m_ht.find(key); }
520 template<
class K,
class KE = KeyEqual,
typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
521 iterator
find(
const K& key, std::size_t precalculated_hash) {
return m_ht.find(key, precalculated_hash); }
526 template<
class K,
class KE = KeyEqual,
typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
527 const_iterator
find(
const K& key)
const {
return m_ht.find(key); }
535 template<
class K,
class KE = KeyEqual,
typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
536 const_iterator
find(
const K& key, std::size_t precalculated_hash)
const {
537 return m_ht.find(key, precalculated_hash);
543 std::pair<iterator, iterator>
equal_range(
const Key& key) {
return m_ht.equal_range(key); }
549 std::pair<iterator, iterator>
equal_range(
const Key& key, std::size_t precalculated_hash) {
550 return m_ht.equal_range(key, precalculated_hash);
553 std::pair<const_iterator, const_iterator>
equal_range(
const Key& key)
const {
return m_ht.equal_range(key); }
558 std::pair<const_iterator, const_iterator>
equal_range(
const Key& key, std::size_t precalculated_hash)
const {
559 return m_ht.equal_range(key, precalculated_hash);
566 template<
class K,
class KE = KeyEqual,
typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
567 std::pair<iterator, iterator>
equal_range(
const K& key) {
return m_ht.equal_range(key); }
576 template<
class K,
class KE = KeyEqual,
typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
577 std::pair<iterator, iterator>
equal_range(
const K& key, std::size_t precalculated_hash) {
578 return m_ht.equal_range(key, precalculated_hash);
584 template<
class K,
class KE = KeyEqual,
typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
585 std::pair<const_iterator, const_iterator>
equal_range(
const K& key)
const {
return m_ht.equal_range(key); }
590 template<
class K,
class KE = KeyEqual,
typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
591 std::pair<const_iterator, const_iterator>
equal_range(
const K& key, std::size_t precalculated_hash)
const {
592 return m_ht.equal_range(key, precalculated_hash);
612 void rehash(size_type count_) { m_ht.rehash(count_); }
613 void reserve(size_type count_) { m_ht.reserve(count_); }
620 key_equal
key_eq()
const {
return m_ht.key_eq(); }
630 return m_ht.mutable_iterator(pos);
636 if(lhs.size() != rhs.size()) {
640 for(
const auto& element_lhs : lhs) {
641 const auto it_element_rhs = rhs.find(element_lhs.first);
642 if(it_element_rhs == rhs.cend() || element_lhs.second != it_element_rhs->second) {
651 return !operator==(lhs, rhs);
670 class Hash = std::hash<Key>,
671 class KeyEqual = std::equal_to<Key>,
672 class Allocator = std::allocator<std::pair<Key, T>>,
673 unsigned int NeighborhoodSize = 62,
674 bool StoreHash =
false>
void insert(InputIt first, InputIt last)
Definition: hopscotch_map.h:282
hopscotch_map(size_type bucket_count, const Allocator &alloc)
Definition: hopscotch_map.h:150
iterator mutable_iterator(const_iterator pos)
Definition: hopscotch_map.h:629
hopscotch_map()
Definition: hopscotch_map.h:139
const_iterator cend() const noexcept
Definition: hopscotch_map.h:235
size_type max_size() const noexcept
Definition: hopscotch_map.h:243
friend bool operator!=(const hopscotch_map &lhs, const hopscotch_map &rhs)
Definition: hopscotch_map.h:650
size_type overflow_size() const noexcept
Definition: hopscotch_map.h:633
const_iterator cbegin() const noexcept
Definition: hopscotch_map.h:231
T & operator[](Key &&key)
Definition: hopscotch_map.h:456
Definition: hopscotch_hash.h:69
hopscotch_map(const Allocator &alloc)
Definition: hopscotch_map.h:161
std::pair< iterator, bool > insert(const value_type &value)
Definition: hopscotch_map.h:253
const T & at(const Key &key) const
Definition: hopscotch_map.h:415
Definition: bhopscotch_map.h:39
hopscotch_map(size_type bucket_count, const Hash &hash=Hash(), const KeyEqual &equal=KeyEqual(), const Allocator &alloc=Allocator())
Definition: hopscotch_map.h:142
key_equal key_eq() const
Definition: hopscotch_map.h:620
float load_factor() const
Definition: hopscotch_map.h:608
hopscotch_map(InputIt first, InputIt last, size_type bucket_count, const Hash &hash, const Allocator &alloc)
Definition: hopscotch_map.h:182
std::pair< const_iterator, const_iterator > equal_range(const Key &key) const
Definition: hopscotch_map.h:553
iterator insert(const_iterator hint, value_type &&value)
Definition: hopscotch_map.h:276
void clear() noexcept
Definition: hopscotch_map.h:248
iterator insert(const_iterator hint, P &&value)
Definition: hopscotch_map.h:272
const_iterator find(const K &key) const
Definition: hopscotch_map.h:527
std::pair< iterator, iterator > equal_range(const Key &key, std::size_t precalculated_hash)
Definition: hopscotch_map.h:549
iterator insert(const_iterator hint, const value_type &value)
Definition: hopscotch_map.h:267
size_type erase(const key_type &key)
Definition: hopscotch_map.h:370
iterator insert_or_assign(const_iterator hint, key_type &&k, M &&obj)
Definition: hopscotch_map.h:309
void max_load_factor(float ml)
Definition: hopscotch_map.h:610
allocator_type get_allocator() const
Definition: hopscotch_map.h:223
size_type bucket_count() const
Definition: hopscotch_map.h:601
iterator insert_or_assign(const_iterator hint, const key_type &k, M &&obj)
Definition: hopscotch_map.h:304
size_type size() const noexcept
Definition: hopscotch_map.h:242
hopscotch_map(std::initializer_list< value_type > init, size_type bucket_count, const Allocator &alloc)
Definition: hopscotch_map.h:198
iterator erase(const_iterator first, const_iterator last)
Definition: hopscotch_map.h:369
size_type count(const K &key, std::size_t precalculated_hash) const
Definition: hopscotch_map.h:485
size_type max_bucket_count() const
Definition: hopscotch_map.h:602
hasher hash_function() const
Definition: hopscotch_map.h:619
friend bool operator==(const hopscotch_map &lhs, const hopscotch_map &rhs)
Definition: hopscotch_map.h:635
hopscotch_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: hopscotch_map.h:165
std::pair< const_iterator, const_iterator > equal_range(const K &key, std::size_t precalculated_hash) const
Definition: hopscotch_map.h:591
std::pair< iterator, iterator > equal_range(const Key &key)
Definition: hopscotch_map.h:543
T & at(const Key &key)
Definition: hopscotch_map.h:406
size_type count(const Key &key) const
Definition: hopscotch_map.h:461
iterator find(const Key &key)
Definition: hopscotch_map.h:490
const_iterator find(const Key &key) const
Definition: hopscotch_map.h:498
std::pair< iterator, iterator > equal_range(const K &key)
Definition: hopscotch_map.h:567
iterator end() noexcept
Definition: hopscotch_map.h:233
const_iterator find(const K &key, std::size_t precalculated_hash) const
Definition: hopscotch_map.h:536
std::pair< iterator, bool > insert(P &&value)
Definition: hopscotch_map.h:258
size_type erase(const K &key)
Definition: hopscotch_map.h:385
T & at(const K &key, std::size_t precalculated_hash)
Definition: hopscotch_map.h:437
iterator find(const Key &key, std::size_t precalculated_hash)
Definition: hopscotch_map.h:496
T & operator[](const Key &key)
Definition: hopscotch_map.h:455
size_type erase(const K &key, std::size_t precalculated_hash)
Definition: hopscotch_map.h:394
Definition: hopscotch_growth_policy.h:49
iterator emplace_hint(const_iterator hint, Args &&... args)
Definition: hopscotch_map.h:337
iterator find(const K &key)
Definition: hopscotch_map.h:512
std::pair< iterator, bool > emplace(Args &&... args)
Definition: hopscotch_map.h:323
size_type count(const K &key) const
Definition: hopscotch_map.h:476
std::pair< const_iterator, const_iterator > equal_range(const K &key) const
Definition: hopscotch_map.h:585
Definition: hopscotch_growth_policy.h:247
void swap(hopscotch_map &other)
Definition: hopscotch_map.h:401
const_iterator find(const Key &key, std::size_t precalculated_hash) const
Definition: hopscotch_map.h:503
hopscotch_map(size_type bucket_count, const Hash &hash, const Allocator &alloc)
Definition: hopscotch_map.h:155
Definition: hopscotch_map.h:79
std::pair< iterator, bool > try_emplace(key_type &&k, Args &&... args)
Definition: hopscotch_map.h:350
size_type erase(const key_type &key, std::size_t precalculated_hash)
Definition: hopscotch_map.h:376
const T & at(const K &key, std::size_t precalculated_hash) const
Definition: hopscotch_map.h:450
const T & at(const Key &key, std::size_t precalculated_hash) const
Definition: hopscotch_map.h:420
std::pair< iterator, iterator > equal_range(const K &key, std::size_t precalculated_hash)
Definition: hopscotch_map.h:577
const_iterator begin() const noexcept
Definition: hopscotch_map.h:230
iterator try_emplace(const_iterator hint, const key_type &k, Args &&... args)
Definition: hopscotch_map.h:355
iterator find(const K &key, std::size_t precalculated_hash)
Definition: hopscotch_map.h:521
float max_load_factor() const
Definition: hopscotch_map.h:609
hopscotch_map(std::initializer_list< value_type > init, size_type bucket_count, const Hash &hash, const Allocator &alloc)
Definition: hopscotch_map.h:205
std::pair< iterator, bool > insert_or_assign(const key_type &k, M &&obj)
Definition: hopscotch_map.h:294
hopscotch_map(InputIt first, InputIt last, size_type bucket_count, const Allocator &alloc)
Definition: hopscotch_map.h:175
const T & at(const K &key) const
Definition: hopscotch_map.h:444
hopscotch_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: hopscotch_map.h:189
size_type count(const Key &key, std::size_t precalculated_hash) const
Definition: hopscotch_map.h:467
void insert(std::initializer_list< value_type > ilist)
Definition: hopscotch_map.h:286
std::pair< iterator, bool > insert(value_type &&value)
Definition: hopscotch_map.h:262
iterator begin() noexcept
Definition: hopscotch_map.h:229
void rehash(size_type count_)
Definition: hopscotch_map.h:612
iterator try_emplace(const_iterator hint, key_type &&k, Args &&... args)
Definition: hopscotch_map.h:360
std::pair< const_iterator, const_iterator > equal_range(const Key &key, std::size_t precalculated_hash) const
Definition: hopscotch_map.h:558
const_iterator end() const noexcept
Definition: hopscotch_map.h:234
std::pair< iterator, bool > insert_or_assign(key_type &&k, M &&obj)
Definition: hopscotch_map.h:299
Definition: hopscotch_growth_policy.h:40
void reserve(size_type count_)
Definition: hopscotch_map.h:613
bool empty() const noexcept
Definition: hopscotch_map.h:241
T & at(const K &key)
Definition: hopscotch_map.h:428
friend void swap(hopscotch_map &lhs, hopscotch_map &rhs)
Definition: hopscotch_map.h:654
T & at(const Key &key, std::size_t precalculated_hash)
Definition: hopscotch_map.h:412
std::pair< iterator, bool > try_emplace(const key_type &k, Args &&... args)
Definition: hopscotch_map.h:345
iterator erase(const_iterator pos)
Definition: hopscotch_map.h:368
iterator erase(iterator pos)
Definition: hopscotch_map.h:367
hopscotch_map & operator=(std::initializer_list< value_type > ilist)
Definition: hopscotch_map.h:214