156 using traits_type = TraitsT;
158 typedef KeyT key_type;
159 typedef typename key_type::value_type key_unit_type;
160 typedef ValueT value_type;
161 typedef size_t size_type;
170 typedef std::map<key_unit_type, trie_node> children_type;
172 children_type children;
177 trie_node(
const trie_node& other);
178 trie_node(trie_node&& other);
180 void swap(trie_node& other);
183 template<
bool IsConst>
186 using _is_const = std::bool_constant<IsConst>;
188 using child_pos_type =
193 trie_node_type* node;
194 child_pos_type child_pos;
196 stack_item(trie_node_type* _node,
const child_pos_type& _child_pos) : node(_node), child_pos(_child_pos)
199 bool operator==(
const stack_item& r)
const
201 return node == r.node && child_pos == r.child_pos;
204 bool operator!=(
const stack_item& r)
const
206 return !operator==(r);
210 using const_node_stack_type = std::vector<stack_item<true>>;
211 using node_stack_type = std::vector<stack_item<false>>;
217 class const_node_type
219 friend class trie_map;
221 const trie_node* m_ref_node =
nullptr;
223 const_node_type(
const trie_node* ref_node);
227 const_node_type(
const const_node_type& other);
229 const_node_type& operator=(
const const_node_type& other);
274 const_node_type
child(key_unit_type c)
const;
286 const_iterator begin()
const;
290 const_iterator end()
const;
296 bool operator==(
const trie_map& other)
const;
297 bool operator!=(
const trie_map& other)
const;
314 void insert(
const key_type& key, value_type value);
324 void insert(
const key_unit_type* key, size_type len, value_type value);
335 bool erase(
const key_unit_type* key, size_type len);
345 const_iterator
find(
const key_type& key)
const;
357 const_iterator
find(
const key_unit_type* input, size_type len)
const;
367 iterator
find(
const key_type& key);
379 iterator
find(
const key_unit_type* input, size_type len);
405 search_results
prefix_search(
const key_unit_type* prefix, size_type len)
const;
414 bool empty() const noexcept;
442 void insert_into_tree(trie_node& node, const key_unit_type* key, const key_unit_type* key_end, value_type value);
444 const trie_node* find_prefix_node(
445 const trie_node& node, const key_unit_type* prefix, const key_unit_type* prefix_end) const;
447 template<
bool IsConst>
448 void find_prefix_node_with_stack(
449 std::vector<stack_item<IsConst>>& node_stack, mdds::detail::const_t<trie_node, IsConst>& node,
450 const key_unit_type* prefix, const key_unit_type* prefix_end) const;
452 template<
bool IsConst>
453 key_type build_key_buffer_from_node_stack(const std::vector<stack_item<IsConst>>& node_stack) const;
455 void count_values(size_type& n, const trie_node& node) const;
457 bool descend_for_equality(const trie_node& left, const trie_node& right) const;
476 using pack_value_type =
typename TraitsT::pack_value_type;
477 using packed_type = std::vector<pack_value_type>;
481 friend class trie_map<KeyT, ValueT, TraitsT>;
485 using traits_type = TraitsT;
486 typedef KeyT key_type;
487 typedef typename key_type::value_type key_unit_type;
488 typedef ValueT value_type;
489 typedef size_t size_type;
499 const key_unit_type* key;
503 entry(
const key_unit_type* _key, size_type _keylen, value_type _value)
504 : key(_key), keylen(_keylen), value(_value)
509 using value_store_type = std::deque<value_type>;
512 static constexpr auto null_value = std::numeric_limits<pack_value_type>::max();
514 static constexpr auto max_value_pos = null_value - 1u;
519 const value_type* value;
521 std::deque<trie_node*> children;
523 trie_node(key_unit_type _key) : key(_key), value(nullptr)
529 const value_store_type* value_store =
nullptr;
531 const pack_value_type* node_pos =
nullptr;
532 const pack_value_type* child_pos =
nullptr;
533 const pack_value_type* child_end =
nullptr;
536 const value_store_type* _value_store,
const pack_value_type* _node_pos,
const pack_value_type* _child_pos,
537 const pack_value_type* _child_end)
538 : value_store(_value_store), node_pos(_node_pos), child_pos(_child_pos), child_end(_child_end)
541 bool operator==(
const stack_item& other)
const
543 return value_store == other.value_store && node_pos == other.node_pos && child_pos == other.child_pos &&
544 child_end == other.child_end;
547 bool operator!=(
const stack_item& other)
const
549 return !operator==(other);
552 bool has_value()
const
554 return *node_pos != null_value;
557 pack_value_type get_value_pos()
const
563 typedef std::vector<stack_item> node_stack_type;
564 typedef std::deque<trie_node> node_pool_type;
565 typedef std::vector<std::tuple<size_t, key_unit_type>> child_offsets_type;
573 class const_node_type
575 friend class packed_trie_map;
577 const packed_type* m_packed =
nullptr;
578 const value_store_type* m_value_store =
nullptr;
579 const pack_value_type* m_pos =
nullptr;
581 const_node_type(
const packed_type* packed,
const value_store_type* value_store,
const pack_value_type* p);
584 const_node_type() =
default;
585 const_node_type(
const const_node_type& other) =
default;
587 const_node_type& operator=(
const const_node_type& other);
632 const_node_type
child(key_unit_type c)
const;
665 packed_trie_map(
const packed_trie_map& other);
667 packed_trie_map(packed_trie_map&& other);
669 packed_trie_map& operator=(packed_trie_map other);
671 bool operator==(
const packed_trie_map& other)
const;
673 bool operator!=(
const packed_trie_map& other)
const;
675 const_iterator begin()
const;
677 const_iterator end()
const;
679 const_iterator cbegin()
const;
681 const_iterator cend()
const;
691 const_iterator
find(
const key_type& key)
const;
703 const_iterator
find(
const key_unit_type* input, size_type len)
const;
728 search_results
prefix_search(
const key_unit_type* prefix, size_type len)
const;
735 size_type
size() const noexcept;
737 bool empty() const noexcept;
739 void swap(packed_trie_map& other);
753 template<typename FuncT = trie::value_serializer<value_type>>
762 template<typename FuncT = trie::value_serializer<value_type>>
774 void dump_trie_traversal(std::ostream& os) const;
776 node_stack_type get_root_stack() const;
779 trie_node& root, node_pool_type& node_pool, const entry* start, const entry* end, size_type pos);
781 size_type compact_node(const trie_node& node);
783 template<typename ModeT, typename NodeT>
784 size_type compact_node(ModeT, NodeT& node);
786 void check_value_size_or_throw() const;
788 size_type push_value_to_store(trie::detail::copy_to_pack, const value_type& value);
789 size_type push_value_to_store(trie::detail::move_to_pack, value_type& value);
791 void push_child_offsets(size_type offset, const child_offsets_type& child_offsets);
794 void compact(const trie_node& root);
796 template<typename ModeT, typename NodeT>
797 void compact(ModeT, NodeT& root);
799 template<typename _Handler>
800 void traverse_tree(_Handler hdl) const;
803 value_store_type m_value_store;
804 packed_type m_packed;