24 #ifndef TSL_SPARSE_MAP_H 25 #define TSL_SPARSE_MAP_H 30 #include <initializer_list> 32 #include <type_traits> 76 class Hash = std::hash<Key>,
77 class KeyEqual = std::equal_to<Key>,
78 class Allocator = std::allocator<std::pair<Key, T>>,
91 const key_type& operator()(
const std::pair<Key, T>& key_value)
const noexcept {
92 return key_value.first;
95 key_type& operator()(std::pair<Key, T>& key_value)
noexcept {
96 return key_value.first;
102 using value_type = T;
104 const value_type& operator()(
const std::pair<Key, T>& key_value)
const noexcept {
105 return key_value.second;
108 value_type& operator()(std::pair<Key, T>& key_value)
noexcept {
109 return key_value.second;
114 Hash, KeyEqual, Allocator, GrowthPolicy,
115 ExceptionSafety, Sparsity,
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):
sparse_map(bucket_count, Hash(), KeyEqual(), alloc)
157 const Allocator& alloc):
sparse_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()):
sparse_map(bucket_count, hash, equal, alloc)
174 template<
class InputIt>
176 size_type bucket_count,
177 const Allocator& alloc):
sparse_map(first, last, bucket_count, Hash(), KeyEqual(), alloc)
181 template<
class InputIt>
183 size_type bucket_count,
185 const Allocator& alloc):
sparse_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 sparse_map(init.begin(), init.end(), bucket_count, hash, equal, alloc)
199 size_type bucket_count,
200 const Allocator& alloc):
201 sparse_map(init.begin(), init.end(), bucket_count, Hash(), KeyEqual(), alloc)
206 size_type bucket_count,
208 const Allocator& alloc):
209 sparse_map(init.begin(), init.end(), bucket_count, hash, KeyEqual(), alloc)
216 m_ht.reserve(ilist.size());
217 m_ht.insert(ilist.begin(), ilist.end());
228 iterator
begin()
noexcept {
return m_ht.begin(); }
229 const_iterator
begin()
const noexcept {
return m_ht.begin(); }
230 const_iterator
cbegin()
const noexcept {
return m_ht.cbegin(); }
232 iterator
end()
noexcept {
return m_ht.end(); }
233 const_iterator
end()
const noexcept {
return m_ht.end(); }
234 const_iterator
cend()
const noexcept {
return m_ht.cend(); }
240 bool empty()
const noexcept {
return m_ht.empty(); }
241 size_type
size()
const noexcept {
return m_ht.size(); }
242 size_type
max_size()
const noexcept {
return m_ht.max_size(); }
247 void clear()
noexcept { m_ht.clear(); }
251 std::pair<iterator,
bool>
insert(
const value_type& value) {
252 return m_ht.insert(value);
255 template<
class P,
typename std::enable_if<std::is_constructible<value_type, P&&>::value>::type* =
nullptr>
256 std::pair<iterator,
bool>
insert(P&& value) {
257 return m_ht.emplace(std::forward<P>(value));
260 std::pair<iterator,
bool>
insert(value_type&& value) {
261 return m_ht.insert(std::move(value));
265 iterator
insert(const_iterator hint,
const value_type& value) {
266 return m_ht.insert(hint, value);
269 template<
class P,
typename std::enable_if<std::is_constructible<value_type, P&&>::value>::type* =
nullptr>
270 iterator
insert(const_iterator hint, P&& value) {
271 return m_ht.emplace_hint(hint, std::forward<P>(value));
274 iterator
insert(const_iterator hint, value_type&& value) {
275 return m_ht.insert(hint, std::move(value));
279 template<
class InputIt>
280 void insert(InputIt first, InputIt last) {
281 m_ht.insert(first, last);
284 void insert(std::initializer_list<value_type> ilist) {
285 m_ht.insert(ilist.begin(), ilist.end());
293 return m_ht.insert_or_assign(k, std::forward<M>(obj));
298 return m_ht.insert_or_assign(std::move(k), std::forward<M>(obj));
303 return m_ht.insert_or_assign(hint, k, std::forward<M>(obj));
308 return m_ht.insert_or_assign(hint, std::move(k), std::forward<M>(obj));
319 template<
class... Args>
320 std::pair<iterator,
bool>
emplace(Args&&... args) {
321 return m_ht.emplace(std::forward<Args>(args)...);
332 template<
class... Args>
334 return m_ht.emplace_hint(hint, std::forward<Args>(args)...);
340 template<
class... Args>
341 std::pair<iterator,
bool>
try_emplace(
const key_type& k, Args&&... args) {
342 return m_ht.try_emplace(k, std::forward<Args>(args)...);
345 template<
class... Args>
346 std::pair<iterator,
bool>
try_emplace(key_type&& k, Args&&... args) {
347 return m_ht.try_emplace(std::move(k), std::forward<Args>(args)...);
350 template<
class... Args>
351 iterator
try_emplace(const_iterator hint,
const key_type& k, Args&&... args) {
352 return m_ht.try_emplace(hint, k, std::forward<Args>(args)...);
355 template<
class... Args>
356 iterator
try_emplace(const_iterator hint, key_type&& k, Args&&... args) {
357 return m_ht.try_emplace(hint, std::move(k), std::forward<Args>(args)...);
363 iterator
erase(iterator pos) {
return m_ht.erase(pos); }
364 iterator
erase(const_iterator pos) {
return m_ht.erase(pos); }
365 iterator
erase(const_iterator first, const_iterator last) {
return m_ht.erase(first, last); }
366 size_type
erase(
const key_type& key) {
return m_ht.erase(key); }
373 size_type
erase(
const key_type& key, std::size_t precalculated_hash) {
374 return m_ht.erase(key, precalculated_hash);
381 template<
class K,
class KE = KeyEqual,
typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
382 size_type
erase(
const K& key) {
return m_ht.erase(key); }
391 template<
class K,
class KE = KeyEqual,
typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
392 size_type
erase(
const K& key, std::size_t precalculated_hash) {
393 return m_ht.erase(key, precalculated_hash);
405 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); }
437 template<
class K,
class KE = KeyEqual,
typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
438 T&
at(
const K& key, std::size_t precalculated_hash) {
return m_ht.at(key, precalculated_hash); }
444 template<
class K,
class KE = KeyEqual,
typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
445 const T&
at(
const K& key)
const {
return m_ht.at(key); }
450 template<
class K,
class KE = KeyEqual,
typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
451 const T&
at(
const K& key, std::size_t precalculated_hash)
const {
return m_ht.at(key, precalculated_hash); }
456 T&
operator[](
const Key& key) {
return m_ht[key]; }
457 T&
operator[](Key&& key) {
return m_ht[std::move(key)]; }
462 size_type
count(
const Key& key)
const {
return m_ht.count(key); }
469 size_type
count(
const Key& key, std::size_t precalculated_hash)
const {
470 return m_ht.count(key, precalculated_hash);
477 template<
class K,
class KE = KeyEqual,
typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
478 size_type
count(
const K& key)
const {
return m_ht.count(key); }
487 template<
class K,
class KE = KeyEqual,
typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
488 size_type
count(
const K& key, std::size_t precalculated_hash)
const {
return m_ht.count(key, precalculated_hash); }
493 iterator
find(
const Key& key) {
return m_ht.find(key); }
500 iterator
find(
const Key& key, std::size_t precalculated_hash) {
return m_ht.find(key, precalculated_hash); }
502 const_iterator
find(
const Key& key)
const {
return m_ht.find(key); }
507 const_iterator
find(
const Key& key, std::size_t precalculated_hash)
const {
508 return m_ht.find(key, precalculated_hash);
515 template<
class K,
class KE = KeyEqual,
typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
516 iterator
find(
const K& key) {
return m_ht.find(key); }
525 template<
class K,
class KE = KeyEqual,
typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
526 iterator
find(
const K& key, std::size_t precalculated_hash) {
return m_ht.find(key, precalculated_hash); }
531 template<
class K,
class KE = KeyEqual,
typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
532 const_iterator
find(
const K& key)
const {
return m_ht.find(key); }
541 template<
class K,
class KE = KeyEqual,
typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
542 const_iterator
find(
const K& key, std::size_t precalculated_hash)
const {
543 return m_ht.find(key, precalculated_hash);
549 std::pair<iterator, iterator>
equal_range(
const Key& key) {
return m_ht.equal_range(key); }
556 std::pair<iterator, iterator>
equal_range(
const Key& key, std::size_t precalculated_hash) {
557 return m_ht.equal_range(key, precalculated_hash);
560 std::pair<const_iterator, const_iterator>
equal_range(
const Key& key)
const {
return m_ht.equal_range(key); }
565 std::pair<const_iterator, const_iterator>
equal_range(
const Key& key, std::size_t precalculated_hash)
const {
566 return m_ht.equal_range(key, precalculated_hash);
573 template<
class K,
class KE = KeyEqual,
typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
574 std::pair<iterator, iterator>
equal_range(
const K& key) {
return m_ht.equal_range(key); }
584 template<
class K,
class KE = KeyEqual,
typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
585 std::pair<iterator, iterator>
equal_range(
const K& key, std::size_t precalculated_hash) {
586 return m_ht.equal_range(key, precalculated_hash);
592 template<
class K,
class KE = KeyEqual,
typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
593 std::pair<const_iterator, const_iterator>
equal_range(
const K& key)
const {
return m_ht.equal_range(key); }
598 template<
class K,
class KE = KeyEqual,
typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
599 std::pair<const_iterator, const_iterator>
equal_range(
const K& key, std::size_t precalculated_hash)
const {
600 return m_ht.equal_range(key, precalculated_hash);
620 void rehash(size_type count) { m_ht.rehash(count); }
621 void reserve(size_type count) { m_ht.reserve(count); }
628 key_equal
key_eq()
const {
return m_ht.key_eq(); }
638 return m_ht.mutable_iterator(pos);
650 template<
class Serializer>
652 m_ht.serialize(serializer);
672 template<
class Deserializer>
675 map.m_ht.deserialize(deserializer, hash_compatible);
681 if(lhs.size() != rhs.size()) {
685 for(
const auto& element_lhs: lhs) {
686 const auto it_element_rhs = rhs.find(element_lhs.first);
687 if(it_element_rhs == rhs.cend() || element_lhs.second != it_element_rhs->second) {
696 return !operator==(lhs, rhs);
713 class Hash = std::hash<Key>,
714 class KeyEqual = std::equal_to<Key>,
715 class Allocator = std::allocator<std::pair<Key, T>>>
iterator insert_or_assign(const_iterator hint, const key_type &k, M &&obj)
Definition: sparse_map.h:302
sparse_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: sparse_map.h:165
T & operator[](Key &&key)
Definition: sparse_map.h:457
iterator try_emplace(const_iterator hint, const key_type &k, Args &&... args)
Definition: sparse_map.h:351
iterator find(const K &key, std::size_t precalculated_hash)
Definition: sparse_map.h:526
std::pair< iterator, bool > try_emplace(const key_type &k, Args &&... args)
Definition: sparse_map.h:341
void serialize(Serializer &serializer) const
Definition: sparse_map.h:651
sparse_map(const Allocator &alloc)
Definition: sparse_map.h:161
allocator_type get_allocator() const
Definition: sparse_map.h:222
sparse_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: sparse_map.h:189
std::pair< iterator, iterator > equal_range(const K &key, std::size_t precalculated_hash)
Definition: sparse_map.h:585
std::pair< iterator, bool > insert(value_type &&value)
Definition: sparse_map.h:260
std::pair< const_iterator, const_iterator > equal_range(const Key &key, std::size_t precalculated_hash) const
Definition: sparse_map.h:565
Definition: sparse_growth_policy.h:39
iterator begin() noexcept
Definition: sparse_map.h:228
std::pair< const_iterator, const_iterator > equal_range(const K &key, std::size_t precalculated_hash) const
Definition: sparse_map.h:599
size_type count(const Key &key, std::size_t precalculated_hash) const
Definition: sparse_map.h:469
T & at(const Key &key)
Definition: sparse_map.h:405
iterator find(const Key &key)
Definition: sparse_map.h:493
const T & at(const Key &key, std::size_t precalculated_hash) const
Definition: sparse_map.h:420
iterator emplace_hint(const_iterator hint, Args &&... args)
Definition: sparse_map.h:333
friend bool operator!=(const sparse_map &lhs, const sparse_map &rhs)
Definition: sparse_map.h:695
sparse_map(InputIt first, InputIt last, size_type bucket_count, const Hash &hash, const Allocator &alloc)
Definition: sparse_map.h:182
const T & at(const K &key, std::size_t precalculated_hash) const
Definition: sparse_map.h:451
void insert(std::initializer_list< value_type > ilist)
Definition: sparse_map.h:284
probing
Definition: sparse_hash.h:63
const_iterator find(const K &key) const
Definition: sparse_map.h:532
Definition: sparse_map.h:82
hasher hash_function() const
Definition: sparse_map.h:627
iterator end() noexcept
Definition: sparse_map.h:232
size_type max_bucket_count() const
Definition: sparse_map.h:610
sparse_map(size_type bucket_count, const Hash &hash, const Allocator &alloc)
Definition: sparse_map.h:155
sparse_map(size_type bucket_count, const Hash &hash=Hash(), const KeyEqual &equal=KeyEqual(), const Allocator &alloc=Allocator())
Definition: sparse_map.h:142
std::pair< iterator, iterator > equal_range(const K &key)
Definition: sparse_map.h:574
std::pair< iterator, iterator > equal_range(const Key &key)
Definition: sparse_map.h:549
iterator try_emplace(const_iterator hint, key_type &&k, Args &&... args)
Definition: sparse_map.h:356
std::pair< iterator, bool > insert(const value_type &value)
Definition: sparse_map.h:251
float max_load_factor() const
Definition: sparse_map.h:617
const_iterator find(const K &key, std::size_t precalculated_hash) const
Definition: sparse_map.h:542
std::pair< const_iterator, const_iterator > equal_range(const Key &key) const
Definition: sparse_map.h:560
sparsity
Definition: sparse_hash.h:73
T & operator[](const Key &key)
Definition: sparse_map.h:456
exception_safety
Definition: sparse_hash.h:68
size_type count(const K &key) const
Definition: sparse_map.h:478
std::pair< iterator, iterator > equal_range(const Key &key, std::size_t precalculated_hash)
Definition: sparse_map.h:556
friend void swap(sparse_map &lhs, sparse_map &rhs)
Definition: sparse_map.h:699
T & at(const Key &key, std::size_t precalculated_hash)
Definition: sparse_map.h:412
size_type erase(const key_type &key)
Definition: sparse_map.h:366
sparse_map(InputIt first, InputIt last, size_type bucket_count, const Allocator &alloc)
Definition: sparse_map.h:175
const_iterator find(const Key &key) const
Definition: sparse_map.h:502
void clear() noexcept
Definition: sparse_map.h:247
std::pair< iterator, bool > try_emplace(key_type &&k, Args &&... args)
Definition: sparse_map.h:346
float load_factor() const
Definition: sparse_map.h:616
const_iterator find(const Key &key, std::size_t precalculated_hash) const
Definition: sparse_map.h:507
iterator mutable_iterator(const_iterator pos)
Definition: sparse_map.h:637
sparse_map(std::initializer_list< value_type > init, size_type bucket_count, const Hash &hash, const Allocator &alloc)
Definition: sparse_map.h:205
Definition: sparse_growth_policy.h:40
size_type erase(const K &key, std::size_t precalculated_hash)
Definition: sparse_map.h:392
size_type bucket_count() const
Definition: sparse_map.h:609
size_type erase(const key_type &key, std::size_t precalculated_hash)
Definition: sparse_map.h:373
iterator insert(const_iterator hint, P &&value)
Definition: sparse_map.h:270
iterator erase(const_iterator pos)
Definition: sparse_map.h:364
void insert(InputIt first, InputIt last)
Definition: sparse_map.h:280
const_iterator cend() const noexcept
Definition: sparse_map.h:234
size_type max_size() const noexcept
Definition: sparse_map.h:242
void rehash(size_type count)
Definition: sparse_map.h:620
iterator insert(const_iterator hint, const value_type &value)
Definition: sparse_map.h:265
void reserve(size_type count)
Definition: sparse_map.h:621
void swap(sparse_map &other)
Definition: sparse_map.h:398
T & at(const K &key, std::size_t precalculated_hash)
Definition: sparse_map.h:438
size_type count(const Key &key) const
Definition: sparse_map.h:462
sparse_map(std::initializer_list< value_type > init, size_type bucket_count, const Allocator &alloc)
Definition: sparse_map.h:198
iterator erase(iterator pos)
Definition: sparse_map.h:363
size_type size() const noexcept
Definition: sparse_map.h:241
iterator find(const K &key)
Definition: sparse_map.h:516
const_iterator end() const noexcept
Definition: sparse_map.h:233
std::pair< iterator, bool > insert(P &&value)
Definition: sparse_map.h:256
T & at(const K &key)
Definition: sparse_map.h:428
iterator insert_or_assign(const_iterator hint, key_type &&k, M &&obj)
Definition: sparse_map.h:307
Definition: sparse_growth_policy.h:247
const T & at(const Key &key) const
Definition: sparse_map.h:415
friend bool operator==(const sparse_map &lhs, const sparse_map &rhs)
Definition: sparse_map.h:680
sparse_map()
Definition: sparse_map.h:139
std::pair< iterator, bool > insert_or_assign(const key_type &k, M &&obj)
Definition: sparse_map.h:292
size_type count(const K &key, std::size_t precalculated_hash) const
Definition: sparse_map.h:488
size_type erase(const K &key)
Definition: sparse_map.h:382
Definition: sparse_hash.h:193
sparse_map & operator=(std::initializer_list< value_type > ilist)
Definition: sparse_map.h:213
key_equal key_eq() const
Definition: sparse_map.h:628
std::pair< iterator, bool > insert_or_assign(key_type &&k, M &&obj)
Definition: sparse_map.h:297
const_iterator begin() const noexcept
Definition: sparse_map.h:229
std::pair< const_iterator, const_iterator > equal_range(const K &key) const
Definition: sparse_map.h:593
bool empty() const noexcept
Definition: sparse_map.h:240
Definition: sparse_growth_policy.h:49
iterator erase(const_iterator first, const_iterator last)
Definition: sparse_map.h:365
const T & at(const K &key) const
Definition: sparse_map.h:445
sparse_map(size_type bucket_count, const Allocator &alloc)
Definition: sparse_map.h:150
static sparse_map deserialize(Deserializer &deserializer, bool hash_compatible=false)
Definition: sparse_map.h:673
Definition: sparse_hash.h:937
void max_load_factor(float ml)
Definition: sparse_map.h:618
const_iterator cbegin() const noexcept
Definition: sparse_map.h:230
iterator insert(const_iterator hint, value_type &&value)
Definition: sparse_map.h:274
std::pair< iterator, bool > emplace(Args &&... args)
Definition: sparse_map.h:320
iterator find(const Key &key, std::size_t precalculated_hash)
Definition: sparse_map.h:500