24 #ifndef TSL_HTRIE_MAP_H 25 #define TSL_HTRIE_MAP_H 29 #include <initializer_list> 52 class Hash =
tsl::ah::str_hash<CharT>,
53 class KeySizeT = std::uint16_t>
57 using is_iterator =
tsl::detail_array_hash::is_iterator<U>;
62 using char_type =
typename ht::char_type;
63 using mapped_type = T;
64 using key_size_type =
typename ht::key_size_type;
65 using size_type =
typename ht::size_type;
66 using hasher =
typename ht::hasher;
67 using iterator =
typename ht::iterator;
68 using const_iterator =
typename ht::const_iterator;
69 using prefix_iterator =
typename ht::prefix_iterator;
70 using const_prefix_iterator =
typename ht::const_prefix_iterator;
73 explicit htrie_map(
const Hash& hash = Hash()): m_ht(hash, ht::HASH_NODE_DEFAULT_MAX_LOAD_FACTOR,
74 ht::DEFAULT_BURST_THRESHOLD)
79 const Hash& hash = Hash()): m_ht(hash, ht::HASH_NODE_DEFAULT_MAX_LOAD_FACTOR,
84 template<
class InputIt,
typename std::enable_if<is_iterator<InputIt>::value>::type* =
nullptr>
86 const Hash& hash = Hash()):
htrie_map(hash)
93 #ifdef TSL_HT_HAS_STRING_VIEW 100 htrie_map(std::initializer_list<std::pair<
const CharT*, T>> init,
101 const Hash& hash = Hash()):
htrie_map(hash)
109 #ifdef TSL_HT_HAS_STRING_VIEW 117 htrie_map& operator=(std::initializer_list<std::pair<
const CharT*, T>> ilist) {
130 iterator
begin()
noexcept {
return m_ht.begin(); }
131 const_iterator
begin()
const noexcept {
return m_ht.begin(); }
132 const_iterator
cbegin()
const noexcept {
return m_ht.cbegin(); }
134 iterator
end()
noexcept {
return m_ht.end(); }
135 const_iterator
end()
const noexcept {
return m_ht.end(); }
136 const_iterator
cend()
const noexcept {
return m_ht.cend(); }
142 bool empty()
const noexcept {
return m_ht.empty(); }
143 size_type
size()
const noexcept {
return m_ht.size(); }
144 size_type
max_size()
const noexcept {
return m_ht.max_size(); }
145 size_type
max_key_size()
const noexcept {
return m_ht.max_key_size(); }
156 void clear()
noexcept { m_ht.clear(); }
160 std::pair<iterator,
bool>
insert_ks(
const CharT* key, size_type key_size,
const T& value) {
161 return m_ht.insert(key, key_size, value);
163 #ifdef TSL_HT_HAS_STRING_VIEW 168 std::pair<iterator,
bool> insert(
const CharT* key,
const T& value) {
169 return m_ht.insert(key, std::strlen(key), value);
172 std::pair<iterator,
bool> insert(
const std::basic_string<CharT>& key,
const T& value) {
173 return m_ht.insert(key.data(), key.size(), value);
179 std::pair<iterator,
bool>
insert_ks(
const CharT* key, size_type key_size, T&& value) {
180 return m_ht.insert(key, key_size, std::move(value));
182 #ifdef TSL_HT_HAS_STRING_VIEW 187 std::pair<iterator,
bool> insert(
const CharT* key, T&& value) {
188 return m_ht.insert(key, std::strlen(key), std::move(value));
191 std::pair<iterator,
bool> insert(
const std::basic_string<CharT>& key, T&& value) {
192 return m_ht.insert(key.data(), key.size(), std::move(value));
198 template<
class InputIt,
typename std::enable_if<is_iterator<InputIt>::value>::type* =
nullptr>
199 void insert(InputIt first, InputIt last) {
200 for(
auto it = first; it != last; ++it) {
207 #ifdef TSL_HT_HAS_STRING_VIEW 212 void insert(std::initializer_list<std::pair<
const CharT*, T>> ilist) {
213 insert(ilist.begin(), ilist.end());
219 template<
class... Args>
220 std::pair<iterator,
bool>
emplace_ks(
const CharT* key, size_type key_size, Args&&... args) {
221 return m_ht.insert(key, key_size, std::forward<Args>(args)...);
223 #ifdef TSL_HT_HAS_STRING_VIEW 224 template<
class...
Args>
229 template<
class... Args>
230 std::pair<iterator,
bool> emplace(
const CharT* key, Args&&... args) {
231 return m_ht.insert(key, std::strlen(key), std::forward<Args>(args)...);
234 template<
class... Args>
235 std::pair<iterator,
bool> emplace(
const std::basic_string<CharT>& key, Args&&... args) {
236 return m_ht.insert(key.data(), key.size(), std::forward<Args>(args)...);
242 iterator
erase(const_iterator pos) {
return m_ht.erase(pos); }
243 iterator
erase(const_iterator first, const_iterator last) {
return m_ht.erase(first, last); }
247 size_type
erase_ks(
const CharT* key, size_type key_size) {
248 return m_ht.erase(key, key_size);
250 #ifdef TSL_HT_HAS_STRING_VIEW 255 size_type erase(
const CharT* key) {
256 return m_ht.erase(key, std::strlen(key));
259 size_type erase(
const std::basic_string<CharT>& key) {
260 return m_ht.erase(key.data(), key.size());
270 return m_ht.erase_prefix(prefix, prefix_size);
272 #ifdef TSL_HT_HAS_STRING_VIEW 283 size_type erase_prefix(
const CharT* prefix) {
284 return m_ht.erase_prefix(prefix, std::strlen(prefix));
290 size_type erase_prefix(
const std::basic_string<CharT>& prefix) {
291 return m_ht.erase_prefix(prefix.data(), prefix.size());
302 T&
at_ks(
const CharT* key, size_type key_size) {
return m_ht.at(key, key_size); }
303 const T&
at_ks(
const CharT* key, size_type key_size)
const {
return m_ht.at(key, key_size); }
305 #ifdef TSL_HT_HAS_STRING_VIEW 309 T& at(
const CharT* key) {
return m_ht.at(key, std::strlen(key)); }
310 const T& at(
const CharT* key)
const {
return m_ht.at(key, std::strlen(key)); }
312 T& at(
const std::basic_string<CharT>& key) {
return m_ht.at(key.data(), key.size()); }
313 const T& at(
const std::basic_string<CharT>& key)
const {
return m_ht.at(key.data(), key.size()); }
318 #ifdef TSL_HT_HAS_STRING_VIEW 321 T& operator[](
const CharT* key) {
return m_ht.access_operator(key, std::strlen(key)); }
322 T& operator[](
const std::basic_string<CharT>& key) {
return m_ht.access_operator(key.data(), key.size()); }
327 size_type
count_ks(
const CharT* key, size_type key_size)
const {
return m_ht.count(key, key_size); }
328 #ifdef TSL_HT_HAS_STRING_VIEW 331 size_type count(
const CharT* key)
const {
return m_ht.count(key, std::strlen(key)); }
332 size_type count(
const std::basic_string<CharT>& key)
const {
return m_ht.count(key.data(), key.size()); }
337 iterator
find_ks(
const CharT* key, size_type key_size) {
338 return m_ht.find(key, key_size);
341 const_iterator
find_ks(
const CharT* key, size_type key_size)
const {
342 return m_ht.find(key, key_size);
344 #ifdef TSL_HT_HAS_STRING_VIEW 353 iterator find(
const CharT* key) {
354 return m_ht.find(key, std::strlen(key));
357 const_iterator find(
const CharT* key)
const {
358 return m_ht.find(key, std::strlen(key));
361 iterator find(
const std::basic_string<CharT>& key) {
362 return m_ht.find(key.data(), key.size());
365 const_iterator find(
const std::basic_string<CharT>& key)
const {
366 return m_ht.find(key.data(), key.size());
372 std::pair<iterator, iterator>
equal_range_ks(
const CharT* key, size_type key_size) {
373 return m_ht.equal_range(key, key_size);
376 std::pair<const_iterator, const_iterator>
equal_range_ks(
const CharT* key, size_type key_size)
const {
377 return m_ht.equal_range(key, key_size);
379 #ifdef TSL_HT_HAS_STRING_VIEW 388 std::pair<iterator, iterator> equal_range(
const CharT* key) {
389 return m_ht.equal_range(key, std::strlen(key));
392 std::pair<const_iterator, const_iterator> equal_range(
const CharT* key)
const {
393 return m_ht.equal_range(key, std::strlen(key));
396 std::pair<iterator, iterator> equal_range(
const std::basic_string<CharT>& key) {
397 return m_ht.equal_range(key.data(), key.size());
400 std::pair<const_iterator, const_iterator> equal_range(
const std::basic_string<CharT>& key)
const {
401 return m_ht.equal_range(key.data(), key.size());
411 return m_ht.equal_prefix_range(prefix, prefix_size);
417 std::pair<const_prefix_iterator, const_prefix_iterator>
equal_prefix_range_ks(
const CharT* prefix, size_type prefix_size)
const {
418 return m_ht.equal_prefix_range(prefix, prefix_size);
420 #ifdef TSL_HT_HAS_STRING_VIEW 438 std::pair<prefix_iterator, prefix_iterator> equal_prefix_range(
const CharT* prefix) {
439 return m_ht.equal_prefix_range(prefix, std::strlen(prefix));
445 std::pair<const_prefix_iterator, const_prefix_iterator> equal_prefix_range(
const CharT* prefix)
const {
446 return m_ht.equal_prefix_range(prefix, std::strlen(prefix));
452 std::pair<prefix_iterator, prefix_iterator> equal_prefix_range(
const std::basic_string<CharT>& prefix) {
453 return m_ht.equal_prefix_range(prefix.data(), prefix.size());
459 std::pair<const_prefix_iterator, const_prefix_iterator> equal_prefix_range(
const std::basic_string<CharT>& prefix)
const {
460 return m_ht.equal_prefix_range(prefix.data(), prefix.size());
482 return m_ht.longest_prefix(key, key_size);
489 return m_ht.longest_prefix(key, key_size);
491 #ifdef TSL_HT_HAS_STRING_VIEW 509 iterator longest_prefix(
const CharT* key) {
510 return m_ht.longest_prefix(key, std::strlen(key));
516 const_iterator longest_prefix(
const CharT* key)
const {
517 return m_ht.longest_prefix(key, std::strlen(key));
523 iterator longest_prefix(
const std::basic_string<CharT>& key) {
524 return m_ht.longest_prefix(key.data(), key.size());
530 const_iterator longest_prefix(
const std::basic_string<CharT>& key)
const {
531 return m_ht.longest_prefix(key.data(), key.size());
572 template<
class Serializer>
574 m_ht.serialize(serializer);
596 template<
class Deserializer>
599 map.m_ht.deserialize(deserializer, hash_compatible);
605 if(lhs.size() != rhs.size()) {
609 std::string key_buffer;
610 for(
auto it = lhs.cbegin(); it != lhs.cend(); ++it) {
613 const auto it_element_rhs = rhs.find(key_buffer);
614 if(it_element_rhs == rhs.cend() || it.value() != it_element_rhs.value()) {
623 return !operator==(lhs, rhs);
631 template<
class U,
class V>
632 void insert_pair(
const std::pair<U, V>& value) {
633 insert(value.first, value.second);
636 template<
class U,
class V>
637 void insert_pair(std::pair<U, V>&& value) {
638 insert(value.first, std::move(value.second));
const_iterator longest_prefix(const std::basic_string_view< CharT > &key) const
Definition: htrie_map.h:502
std::pair< iterator, iterator > equal_range_ks(const CharT *key, size_type key_size)
Definition: htrie_map.h:372
iterator erase(const_iterator first, const_iterator last)
Definition: htrie_map.h:243
htrie_map(const Hash &hash=Hash())
Definition: htrie_map.h:73
iterator find_ks(const CharT *key, size_type key_size)
Definition: htrie_map.h:337
const_iterator find_ks(const CharT *key, size_type key_size) const
Definition: htrie_map.h:341
friend void swap(htrie_map &lhs, htrie_map &rhs)
Definition: htrie_map.h:626
Definition: htrie_hash.h:68
float max_load_factor() const
Definition: htrie_map.h:540
iterator longest_prefix_ks(const CharT *key, size_type key_size)
Definition: htrie_map.h:481
std::pair< iterator, bool > insert_ks(const CharT *key, size_type key_size, T &&value)
Definition: htrie_map.h:179
hasher hash_function() const
Definition: htrie_map.h:554
void serialize(Serializer &serializer) const
Definition: htrie_map.h:573
iterator erase(const_iterator pos)
Definition: htrie_map.h:242
size_type max_key_size() const noexcept
Definition: htrie_map.h:145
size_type max_size() const noexcept
Definition: htrie_map.h:144
const_iterator cbegin() const noexcept
Definition: htrie_map.h:132
const T & at_ks(const CharT *key, size_type key_size) const
Definition: htrie_map.h:303
std::pair< prefix_iterator, prefix_iterator > equal_prefix_range_ks(const CharT *prefix, size_type prefix_size)
Definition: htrie_map.h:410
std::pair< iterator, bool > emplace_ks(const CharT *key, size_type key_size, Args &&... args)
Definition: htrie_map.h:220
const_iterator end() const noexcept
Definition: htrie_map.h:135
std::pair< const_iterator, const_iterator > equal_range_ks(const CharT *key, size_type key_size) const
Definition: htrie_map.h:376
iterator begin() noexcept
Definition: htrie_map.h:130
void max_load_factor(float ml)
Definition: htrie_map.h:541
void clear() noexcept
Definition: htrie_map.h:156
size_type erase_ks(const CharT *key, size_type key_size)
Definition: htrie_map.h:247
size_type burst_threshold() const
Definition: htrie_map.h:547
size_type erase_prefix_ks(const CharT *prefix, size_type prefix_size)
Definition: htrie_map.h:269
const_iterator cend() const noexcept
Definition: htrie_map.h:136
const_iterator begin() const noexcept
Definition: htrie_map.h:131
htrie_map(InputIt first, InputIt last, const Hash &hash=Hash())
Definition: htrie_map.h:85
const_iterator longest_prefix_ks(const CharT *key, size_type key_size) const
Definition: htrie_map.h:488
static htrie_map deserialize(Deserializer &deserializer, bool hash_compatible=false)
Definition: htrie_map.h:597
bool empty() const noexcept
Definition: htrie_map.h:142
htrie_map(size_type burst_threshold, const Hash &hash=Hash())
Definition: htrie_map.h:78
size_type size() const noexcept
Definition: htrie_map.h:143
std::pair< const_prefix_iterator, const_prefix_iterator > equal_prefix_range_ks(const CharT *prefix, size_type prefix_size) const
Definition: htrie_map.h:417
Definition: htrie_hash.h:130
std::pair< iterator, bool > insert_ks(const CharT *key, size_type key_size, const T &value)
Definition: htrie_map.h:160
friend bool operator==(const htrie_map &lhs, const htrie_map &rhs)
Definition: htrie_map.h:604
Definition: htrie_hash.h:70
friend bool operator!=(const htrie_map &lhs, const htrie_map &rhs)
Definition: htrie_map.h:622
T & at_ks(const CharT *key, size_type key_size)
Definition: htrie_map.h:302
size_type count_ks(const CharT *key, size_type key_size) const
Definition: htrie_map.h:327
void shrink_to_fit()
Definition: htrie_map.h:150
void swap(htrie_map &other)
Definition: htrie_map.h:297
void insert(InputIt first, InputIt last)
Definition: htrie_map.h:199
iterator end() noexcept
Definition: htrie_map.h:134
void burst_threshold(size_type threshold)
Definition: htrie_map.h:548