24 #ifndef TSL_ARRAY_MAP_H 25 #define TSL_ARRAY_MAP_H 29 #include <initializer_list> 32 #include <type_traits> 67 bool StoreNullTerminator =
true,
68 class KeySizeT = std::uint16_t,
69 class IndexSizeT = std::uint32_t,
77 KeySizeT, IndexSizeT, GrowthPolicy>;
80 using char_type =
typename ht::char_type;
81 using mapped_type = T;
82 using key_size_type =
typename ht::key_size_type;
83 using index_size_type =
typename ht::index_size_type;
84 using size_type =
typename ht::size_type;
85 using hasher =
typename ht::hasher;
86 using key_equal =
typename ht::key_equal;
87 using iterator =
typename ht::iterator;
88 using const_iterator =
typename ht::const_iterator;
95 const Hash& hash = Hash()): m_ht(bucket_count, hash, ht::DEFAULT_MAX_LOAD_FACTOR)
99 template<
class InputIt,
typename std::enable_if<is_iterator<InputIt>::value>::type* =
nullptr>
101 size_type bucket_count = ht::DEFAULT_INIT_BUCKET_COUNT,
102 const Hash& hash = Hash()):
array_map(bucket_count, hash)
109 #ifdef TSL_AH_HAS_STRING_VIEW 117 array_map(std::initializer_list<std::pair<
const CharT*, T>> init,
118 size_type bucket_count = ht::DEFAULT_INIT_BUCKET_COUNT,
119 const Hash& hash = Hash()):
array_map(bucket_count, hash)
127 #ifdef TSL_AH_HAS_STRING_VIEW 152 iterator
begin()
noexcept {
return m_ht.begin(); }
153 const_iterator
begin()
const noexcept {
return m_ht.begin(); }
154 const_iterator
cbegin()
const noexcept {
return m_ht.cbegin(); }
156 iterator
end()
noexcept {
return m_ht.end(); }
157 const_iterator
end()
const noexcept {
return m_ht.end(); }
158 const_iterator
cend()
const noexcept {
return m_ht.cend(); }
165 bool empty()
const noexcept {
return m_ht.empty(); }
166 size_type
size()
const noexcept {
return m_ht.size(); }
167 size_type
max_size()
const noexcept {
return m_ht.max_size(); }
168 size_type
max_key_size()
const noexcept {
return m_ht.max_key_size(); }
176 void clear()
noexcept { m_ht.clear(); }
180 #ifdef TSL_AH_HAS_STRING_VIEW 185 std::pair<iterator,
bool>
insert(
const CharT* key,
const T& value) {
186 return m_ht.emplace(key, std::char_traits<CharT>::length(key), value);
189 std::pair<iterator,
bool>
insert(
const std::basic_string<CharT>& key,
const T& value) {
190 return m_ht.emplace(key.data(), key.size(), value);
193 std::pair<iterator,
bool>
insert_ks(
const CharT* key, size_type key_size,
const T& value) {
194 return m_ht.emplace(key, key_size, value);
199 #ifdef TSL_AH_HAS_STRING_VIEW 204 std::pair<iterator,
bool>
insert(
const CharT* key, T&& value) {
205 return m_ht.emplace(key, std::char_traits<CharT>::length(key), std::move(value));
208 std::pair<iterator,
bool>
insert(
const std::basic_string<CharT>& key, T&& value) {
209 return m_ht.emplace(key.data(), key.size(), std::move(value));
212 std::pair<iterator,
bool>
insert_ks(
const CharT* key, size_type key_size, T&& value) {
213 return m_ht.emplace(key, key_size, std::move(value));
218 template<
class InputIt,
typename std::enable_if<is_iterator<InputIt>::value>::type* =
nullptr>
219 void insert(InputIt first, InputIt last) {
220 if(std::is_base_of<std::forward_iterator_tag,
221 typename std::iterator_traits<InputIt>::iterator_category>::value)
223 const auto nb_elements_insert = std::distance(first, last);
224 const std::size_t nb_free_buckets = std::size_t(
float(
bucket_count())*max_load_factor()) -
size();
226 if(nb_elements_insert > 0 && nb_free_buckets < std::size_t(nb_elements_insert)) {
231 for(
auto it = first; it != last; ++it) {
238 #ifdef TSL_AH_HAS_STRING_VIEW 243 void insert(std::initializer_list<std::pair<
const CharT*, T>> ilist) {
244 insert(ilist.begin(), ilist.end());
250 #ifdef TSL_AH_HAS_STRING_VIEW 258 return m_ht.insert_or_assign(key, std::char_traits<CharT>::length(key), std::forward<M>(obj));
263 return m_ht.insert_or_assign(key.data(), key.size(), std::forward<M>(obj));
268 return m_ht.insert_or_assign(key, key_size, std::forward<M>(obj));
273 #ifdef TSL_AH_HAS_STRING_VIEW 274 template<
class...
Args>
279 template<
class... Args>
280 std::pair<iterator,
bool>
emplace(
const CharT* key, Args&&... args) {
281 return m_ht.emplace(key, std::char_traits<CharT>::length(key), std::forward<Args>(args)...);
284 template<
class... Args>
285 std::pair<iterator,
bool>
emplace(
const std::basic_string<CharT>& key, Args&&... args) {
286 return m_ht.emplace(key.data(), key.size(), std::forward<Args>(args)...);
289 template<
class... Args>
290 std::pair<iterator,
bool>
emplace_ks(
const CharT* key, size_type key_size, Args&&... args) {
291 return m_ht.emplace(key, key_size, std::forward<Args>(args)...);
305 iterator
erase(const_iterator pos) {
return m_ht.erase(pos); }
310 iterator
erase(const_iterator first, const_iterator last) {
return m_ht.erase(first, last); }
314 #ifdef TSL_AH_HAS_STRING_VIEW 326 return m_ht.erase(key, std::char_traits<CharT>::length(key));
332 size_type
erase(
const std::basic_string<CharT>& key) {
333 return m_ht.erase(key.data(), key.size());
339 size_type
erase_ks(
const CharT* key, size_type key_size) {
340 return m_ht.erase(key, key_size);
345 #ifdef TSL_AH_HAS_STRING_VIEW 356 size_type
erase(
const CharT* key, std::size_t precalculated_hash) {
357 return m_ht.erase(key, std::char_traits<CharT>::length(key), precalculated_hash);
363 size_type
erase(
const std::basic_string<CharT>& key, std::size_t precalculated_hash) {
364 return m_ht.erase(key.data(), key.size(), precalculated_hash);
373 size_type
erase_ks(
const CharT* key, size_type key_size, std::size_t precalculated_hash) {
374 return m_ht.erase(key, key_size, precalculated_hash);
386 #ifdef TSL_AH_HAS_STRING_VIEW 395 T&
at(
const CharT* key) {
396 return m_ht.at(key, std::char_traits<CharT>::length(key));
399 const T&
at(
const CharT* key)
const {
400 return m_ht.at(key, std::char_traits<CharT>::length(key));
403 T&
at(
const std::basic_string<CharT>& key) {
404 return m_ht.at(key.data(), key.size());
407 const T&
at(
const std::basic_string<CharT>& key)
const {
408 return m_ht.at(key.data(), key.size());
411 T&
at_ks(
const CharT* key, size_type key_size) {
412 return m_ht.at(key, key_size);
415 const T&
at_ks(
const CharT* key, size_type key_size)
const {
416 return m_ht.at(key, key_size);
421 #ifdef TSL_AH_HAS_STRING_VIEW 439 T&
at(
const CharT* key, std::size_t precalculated_hash) {
440 return m_ht.at(key, std::char_traits<CharT>::length(key), precalculated_hash);
446 const T&
at(
const CharT* key, std::size_t precalculated_hash)
const {
447 return m_ht.at(key, std::char_traits<CharT>::length(key), precalculated_hash);
453 T&
at(
const std::basic_string<CharT>& key, std::size_t precalculated_hash) {
454 return m_ht.at(key.data(), key.size(), precalculated_hash);
460 const T&
at(
const std::basic_string<CharT>& key, std::size_t precalculated_hash)
const {
461 return m_ht.at(key.data(), key.size(), precalculated_hash);
468 T&
at_ks(
const CharT* key, size_type key_size, std::size_t precalculated_hash) {
469 return m_ht.at(key, key_size, precalculated_hash);
475 const T&
at_ks(
const CharT* key, size_type key_size, std::size_t precalculated_hash)
const {
476 return m_ht.at(key, key_size, precalculated_hash);
481 #ifdef TSL_AH_HAS_STRING_VIEW 484 T&
operator[](
const CharT* key) {
return m_ht.access_operator(key, std::char_traits<CharT>::length(key)); }
485 T&
operator[](
const std::basic_string<CharT>& key) {
return m_ht.access_operator(key.data(), key.size()); }
490 #ifdef TSL_AH_HAS_STRING_VIEW 495 size_type
count(
const CharT* key)
const {
496 return m_ht.count(key, std::char_traits<CharT>::length(key));
499 size_type
count(
const std::basic_string<CharT>& key)
const {
500 return m_ht.count(key.data(), key.size());
503 size_type
count_ks(
const CharT* key, size_type key_size)
const {
504 return m_ht.count(key, key_size);
509 #ifdef TSL_AH_HAS_STRING_VIEW 520 size_type
count(
const CharT* key, std::size_t precalculated_hash)
const {
521 return m_ht.count(key, std::char_traits<CharT>::length(key), precalculated_hash);
527 size_type
count(
const std::basic_string<CharT>& key, std::size_t precalculated_hash)
const {
528 return m_ht.count(key.data(), key.size(), precalculated_hash);
535 size_type
count_ks(
const CharT* key, size_type key_size, std::size_t precalculated_hash)
const {
536 return m_ht.count(key, key_size, precalculated_hash);
541 #ifdef TSL_AH_HAS_STRING_VIEW 550 iterator
find(
const CharT* key) {
551 return m_ht.find(key, std::char_traits<CharT>::length(key));
554 const_iterator
find(
const CharT* key)
const {
555 return m_ht.find(key, std::char_traits<CharT>::length(key));
558 iterator
find(
const std::basic_string<CharT>& key) {
559 return m_ht.find(key.data(), key.size());
562 const_iterator
find(
const std::basic_string<CharT>& key)
const {
563 return m_ht.find(key.data(), key.size());
566 iterator
find_ks(
const CharT* key, size_type key_size) {
567 return m_ht.find(key, key_size);
570 const_iterator
find_ks(
const CharT* key, size_type key_size)
const {
571 return m_ht.find(key, key_size);
576 #ifdef TSL_AH_HAS_STRING_VIEW 594 iterator
find(
const CharT* key, std::size_t precalculated_hash) {
595 return m_ht.find(key, std::char_traits<CharT>::length(key), precalculated_hash);
601 const_iterator
find(
const CharT* key, std::size_t precalculated_hash)
const {
602 return m_ht.find(key, std::char_traits<CharT>::length(key), precalculated_hash);
608 iterator
find(
const std::basic_string<CharT>& key, std::size_t precalculated_hash) {
609 return m_ht.find(key.data(), key.size(), precalculated_hash);
615 const_iterator
find(
const std::basic_string<CharT>& key, std::size_t precalculated_hash)
const {
616 return m_ht.find(key.data(), key.size(), precalculated_hash);
623 iterator
find_ks(
const CharT* key, size_type key_size, std::size_t precalculated_hash) {
624 return m_ht.find(key, key_size, precalculated_hash);
630 const_iterator
find_ks(
const CharT* key, size_type key_size, std::size_t precalculated_hash)
const {
631 return m_ht.find(key, key_size, precalculated_hash);
636 #ifdef TSL_AH_HAS_STRING_VIEW 646 return m_ht.equal_range(key, std::char_traits<CharT>::length(key));
649 std::pair<const_iterator, const_iterator>
equal_range(
const CharT* key)
const {
650 return m_ht.equal_range(key, std::char_traits<CharT>::length(key));
653 std::pair<iterator, iterator>
equal_range(
const std::basic_string<CharT>& key) {
654 return m_ht.equal_range(key.data(), key.size());
657 std::pair<const_iterator, const_iterator>
equal_range(
const std::basic_string<CharT>& key)
const {
658 return m_ht.equal_range(key.data(), key.size());
661 std::pair<iterator, iterator>
equal_range_ks(
const CharT* key, size_type key_size) {
662 return m_ht.equal_range(key, key_size);
665 std::pair<const_iterator, const_iterator>
equal_range_ks(
const CharT* key, size_type key_size)
const {
666 return m_ht.equal_range(key, key_size);
671 #ifdef TSL_AH_HAS_STRING_VIEW 689 std::pair<iterator, iterator>
equal_range(
const CharT* key, std::size_t precalculated_hash) {
690 return m_ht.equal_range(key, std::char_traits<CharT>::length(key), precalculated_hash);
696 std::pair<const_iterator, const_iterator>
equal_range(
const CharT* key, std::size_t precalculated_hash)
const {
697 return m_ht.equal_range(key, std::char_traits<CharT>::length(key), precalculated_hash);
703 std::pair<iterator, iterator>
equal_range(
const std::basic_string<CharT>& key, std::size_t precalculated_hash) {
704 return m_ht.equal_range(key.data(), key.size(), precalculated_hash);
710 std::pair<const_iterator, const_iterator>
equal_range(
const std::basic_string<CharT>& key, std::size_t precalculated_hash)
const {
711 return m_ht.equal_range(key.data(), key.size(), precalculated_hash);
718 std::pair<iterator, iterator>
equal_range_ks(
const CharT* key, size_type key_size, std::size_t precalculated_hash) {
719 return m_ht.equal_range(key, key_size, precalculated_hash);
725 std::pair<const_iterator, const_iterator>
equal_range_ks(
const CharT* key, size_type key_size, std::size_t precalculated_hash)
const {
726 return m_ht.equal_range(key, key_size, precalculated_hash);
745 void rehash(size_type count) { m_ht.rehash(count); }
746 void reserve(size_type count) { m_ht.reserve(count); }
753 key_equal
key_eq()
const {
return m_ht.key_eq(); }
762 iterator
mutable_iterator(const_iterator it)
noexcept {
return m_ht.mutable_iterator(it); }
774 template<
class Serializer>
776 m_ht.serialize(serializer);
797 template<
class Deserializer>
800 map.m_ht.deserialize(deserializer, hash_compatible);
806 if(lhs.size() != rhs.size()) {
810 for(
auto it = lhs.cbegin(); it != lhs.cend(); ++it) {
811 const auto it_element_rhs = rhs.find_ks(it.key(), it.key_size());
812 if(it_element_rhs == rhs.cend() || it.value() != it_element_rhs.value()) {
821 return !operator==(lhs, rhs);
829 template<
class U,
class V>
830 void insert_pair(
const std::pair<U, V>& value) {
831 insert(value.first, value.second);
834 template<
class U,
class V>
835 void insert_pair(std::pair<U, V>&& value) {
836 insert(value.first, std::move(value.second));
851 template<
class CharT,
855 bool StoreNullTerminator =
true,
856 class KeySizeT = std::uint16_t,
857 class IndexSizeT = std::uint32_t>
858 using array_pg_map =
array_map<CharT, T, Hash, KeyEqual, StoreNullTerminator,
size_type erase_ks(const CharT *key, size_type key_size, std::size_t precalculated_hash)
Definition: array_map.h:373
std::pair< const_iterator, const_iterator > equal_range(const CharT *key) const
Definition: array_map.h:649
T & at(const CharT *key, std::size_t precalculated_hash)
Definition: array_map.h:439
iterator find(const std::basic_string< CharT > &key, std::size_t precalculated_hash)
Definition: array_map.h:608
std::pair< iterator, bool > insert(const std::basic_string< CharT > &key, T &&value)
Definition: array_map.h:208
std::pair< iterator, bool > insert_ks(const CharT *key, size_type key_size, const T &value)
Definition: array_map.h:193
Definition: array_growth_policy.h:40
const T & at(const std::basic_string< CharT > &key, std::size_t precalculated_hash) const
Definition: array_map.h:460
size_type erase(const std::basic_string< CharT > &key)
Definition: array_map.h:332
array_map(size_type bucket_count, const Hash &hash=Hash())
Definition: array_map.h:94
size_type count(const std::basic_string< CharT > &key) const
Definition: array_map.h:499
array_map(std::initializer_list< std::pair< const CharT *, T >> init, size_type bucket_count=ht::DEFAULT_INIT_BUCKET_COUNT, const Hash &hash=Hash())
Definition: array_map.h:117
static const size_type MAX_KEY_SIZE
Definition: array_map.h:840
iterator find(const std::basic_string< CharT > &key)
Definition: array_map.h:558
T & at(const CharT *key)
Definition: array_map.h:395
void swap(array_map &other)
Definition: array_map.h:379
Definition: array_growth_policy.h:39
std::pair< const_iterator, const_iterator > equal_range_ks(const CharT *key, size_type key_size) const
Definition: array_map.h:665
size_type count_ks(const CharT *key, size_type key_size, std::size_t precalculated_hash) const
Definition: array_map.h:535
const_iterator find(const std::basic_string< CharT > &key) const
Definition: array_map.h:562
float load_factor() const
Definition: array_map.h:741
void clear() noexcept
Definition: array_map.h:176
Definition: array_hash.h:117
size_type max_bucket_count() const
Definition: array_map.h:735
std::pair< iterator, bool > emplace_ks(const CharT *key, size_type key_size, Args &&... args)
Definition: array_map.h:290
std::pair< iterator, iterator > equal_range(const std::basic_string< CharT > &key)
Definition: array_map.h:653
size_type size() const noexcept
Definition: array_map.h:166
const T & at(const CharT *key) const
Definition: array_map.h:399
size_type max_size() const noexcept
Definition: array_map.h:167
const_iterator find(const CharT *key) const
Definition: array_map.h:554
std::pair< iterator, iterator > equal_range(const CharT *key)
Definition: array_map.h:645
std::pair< iterator, bool > insert_or_assign(const CharT *key, M &&obj)
Definition: array_map.h:257
void reserve(size_type count)
Definition: array_map.h:746
iterator end() noexcept
Definition: array_map.h:156
size_type bucket_count() const
Definition: array_map.h:734
Definition: array_hash.h:102
hasher hash_function() const
Definition: array_map.h:752
friend void swap(array_map &lhs, array_map &rhs)
Definition: array_map.h:824
T & operator[](const CharT *key)
Definition: array_map.h:484
T & at(const std::basic_string< CharT > &key)
Definition: array_map.h:403
std::pair< iterator, bool > insert(const std::basic_string< CharT > &key, const T &value)
Definition: array_map.h:189
iterator find_ks(const CharT *key, size_type key_size)
Definition: array_map.h:566
Definition: array_map.h:71
iterator mutable_iterator(const_iterator it) noexcept
Definition: array_map.h:762
std::pair< iterator, bool > insert_or_assign(const std::basic_string< CharT > &key, M &&obj)
Definition: array_map.h:262
std::pair< iterator, iterator > equal_range(const std::basic_string< CharT > &key, std::size_t precalculated_hash)
Definition: array_map.h:703
static array_map deserialize(Deserializer &deserializer, bool hash_compatible=false)
Definition: array_map.h:798
const_iterator find(const CharT *key, std::size_t precalculated_hash) const
Definition: array_map.h:601
size_type erase(const std::basic_string< CharT > &key, std::size_t precalculated_hash)
Definition: array_map.h:363
std::pair< iterator, bool > insert(const CharT *key, const T &value)
Definition: array_map.h:185
std::pair< iterator, iterator > equal_range(const CharT *key, std::size_t precalculated_hash)
Definition: array_map.h:689
void insert(std::initializer_list< std::pair< const CharT *, T >> ilist)
Definition: array_map.h:243
iterator erase(const_iterator pos)
Definition: array_map.h:305
std::pair< const_iterator, const_iterator > equal_range_ks(const CharT *key, size_type key_size, std::size_t precalculated_hash) const
Definition: array_map.h:725
iterator find_ks(const CharT *key, size_type key_size, std::size_t precalculated_hash)
Definition: array_map.h:623
T & at_ks(const CharT *key, size_type key_size, std::size_t precalculated_hash)
Definition: array_map.h:468
std::pair< iterator, bool > emplace(const CharT *key, Args &&... args)
Definition: array_map.h:280
void rehash(size_type count)
Definition: array_map.h:745
const T & at_ks(const CharT *key, size_type key_size, std::size_t precalculated_hash) const
Definition: array_map.h:475
T & at(const std::basic_string< CharT > &key, std::size_t precalculated_hash)
Definition: array_map.h:453
size_type count(const CharT *key, std::size_t precalculated_hash) const
Definition: array_map.h:520
void max_load_factor(float ml)
Definition: array_map.h:743
T & operator[](const std::basic_string< CharT > &key)
Definition: array_map.h:485
void insert(InputIt first, InputIt last)
Definition: array_map.h:219
const_iterator cend() const noexcept
Definition: array_map.h:158
std::pair< iterator, iterator > equal_range_ks(const CharT *key, size_type key_size, std::size_t precalculated_hash)
Definition: array_map.h:718
array_map(InputIt first, InputIt last, size_type bucket_count=ht::DEFAULT_INIT_BUCKET_COUNT, const Hash &hash=Hash())
Definition: array_map.h:100
const_iterator cbegin() const noexcept
Definition: array_map.h:154
size_type erase_ks(const CharT *key, size_type key_size)
Definition: array_map.h:339
friend bool operator!=(const array_map &lhs, const array_map &rhs)
Definition: array_map.h:820
friend bool operator==(const array_map &lhs, const array_map &rhs)
Definition: array_map.h:805
T & at_ks(const CharT *key, size_type key_size)
Definition: array_map.h:411
std::pair< const_iterator, const_iterator > equal_range(const std::basic_string< CharT > &key) const
Definition: array_map.h:657
iterator find(const CharT *key)
Definition: array_map.h:550
const_iterator find(const std::basic_string< CharT > &key, std::size_t precalculated_hash) const
Definition: array_map.h:615
Definition: array_growth_policy.h:49
Definition: array_growth_policy.h:247
key_equal key_eq() const
Definition: array_map.h:753
const_iterator end() const noexcept
Definition: array_map.h:157
size_type erase(const CharT *key, std::size_t precalculated_hash)
Definition: array_map.h:356
std::pair< iterator, bool > insert(const CharT *key, T &&value)
Definition: array_map.h:204
float max_load_factor() const
Definition: array_map.h:742
array_map & operator=(std::initializer_list< std::pair< const CharT *, T >> ilist)
Definition: array_map.h:137
const T & at(const CharT *key, std::size_t precalculated_hash) const
Definition: array_map.h:446
size_type count_ks(const CharT *key, size_type key_size) const
Definition: array_map.h:503
std::pair< const_iterator, const_iterator > equal_range(const std::basic_string< CharT > &key, std::size_t precalculated_hash) const
Definition: array_map.h:710
array_map()
Definition: array_map.h:91
iterator erase(const_iterator first, const_iterator last)
Definition: array_map.h:310
size_type count(const std::basic_string< CharT > &key, std::size_t precalculated_hash) const
Definition: array_map.h:527
size_type count(const CharT *key) const
Definition: array_map.h:495
std::pair< iterator, bool > insert_ks(const CharT *key, size_type key_size, T &&value)
Definition: array_map.h:212
void serialize(Serializer &serializer) const
Definition: array_map.h:775
bool empty() const noexcept
Definition: array_map.h:165
Definition: array_hash.h:742
const T & at_ks(const CharT *key, size_type key_size) const
Definition: array_map.h:415
std::pair< iterator, iterator > equal_range_ks(const CharT *key, size_type key_size)
Definition: array_map.h:661
const_iterator find_ks(const CharT *key, size_type key_size) const
Definition: array_map.h:570
const_iterator begin() const noexcept
Definition: array_map.h:153
std::pair< const_iterator, const_iterator > equal_range(const CharT *key, std::size_t precalculated_hash) const
Definition: array_map.h:696
iterator find(const CharT *key, std::size_t precalculated_hash)
Definition: array_map.h:594
const T & at(const std::basic_string< CharT > &key) const
Definition: array_map.h:407
Definition: array_hash.h:77
std::pair< iterator, bool > insert_or_assign_ks(const CharT *key, size_type key_size, M &&obj)
Definition: array_map.h:267
size_type erase(const CharT *key)
Definition: array_map.h:325
const_iterator find_ks(const CharT *key, size_type key_size, std::size_t precalculated_hash) const
Definition: array_map.h:630
void shrink_to_fit()
Definition: array_map.h:169
std::pair< iterator, bool > emplace(const std::basic_string< CharT > &key, Args &&... args)
Definition: array_map.h:285
iterator begin() noexcept
Definition: array_map.h:152
size_type max_key_size() const noexcept
Definition: array_map.h:168