29 #ifndef INCLUDED_MDDS_TRIE_MAP_HPP
30 #define INCLUDED_MDDS_TRIE_MAP_HPP
32 #include "trie_map_itr.hpp"
55 template<
typename TrieT,
typename PackedT>
64 static constexpr
bool variable_size =
false;
66 static constexpr
size_t value_size =
sizeof(T);
68 static void write(std::ostream& os,
const T& v);
70 static void read(std::istream& is,
size_t n, T& v);
77 static constexpr
bool variable_size =
true;
79 static void write(std::ostream& os,
const T& v);
81 static void read(std::istream& is,
size_t n, T& v);
93 static constexpr
bool variable_size =
true;
95 static void write(std::ostream& os,
const T& v);
97 static void read(std::istream& is,
size_t n, T& v);
107 template<
typename T,
typename U =
void>
113 struct value_serializer<T, typename std::enable_if<mdds::detail::has_value_type<T>::value>::type>
140 enum class dump_structure_type
156 template<
typename KeyT,
typename ValueT,
typename TraitsT>
165 template<
typename KeyT,
typename ValueT,
typename TraitsT = trie::default_traits>
178 using traits_type = TraitsT;
180 typedef KeyT key_type;
181 typedef typename key_type::value_type key_unit_type;
182 typedef ValueT value_type;
183 typedef size_t size_type;
192 typedef std::map<key_unit_type, trie_node> children_type;
194 children_type children;
199 trie_node(
const trie_node& other);
200 trie_node(trie_node&& other);
202 void swap(trie_node& other);
205 template<
bool IsConst>
208 using _is_const = std::bool_constant<IsConst>;
210 using child_pos_type =
215 trie_node_type* node;
216 child_pos_type child_pos;
218 stack_item(trie_node_type* _node,
const child_pos_type& _child_pos) : node(_node), child_pos(_child_pos)
221 bool operator==(
const stack_item& r)
const
223 return node == r.node && child_pos == r.child_pos;
226 bool operator!=(
const stack_item& r)
const
228 return !operator==(r);
232 using const_node_stack_type = std::vector<stack_item<true>>;
233 using node_stack_type = std::vector<stack_item<false>>;
241 friend class trie_map;
243 const trie_node* m_ref_node =
nullptr;
285 const value_type&
value()
const;
316 trie_map& operator=(trie_map other);
318 bool operator==(
const trie_map& other)
const;
319 bool operator!=(
const trie_map& other)
const;
321 void swap(trie_map& other);
336 void insert(
const key_type& key, value_type value);
346 void insert(
const key_unit_type* key, size_type len, value_type value);
357 bool erase(
const key_unit_type* key, size_type len);
401 iterator find(
const key_unit_type* input, size_type len);
427 search_results
prefix_search(
const key_unit_type* prefix, size_type len)
const;
434 size_type
size()
const;
436 bool empty() const noexcept;
464 void insert_into_tree(trie_node& node, const key_unit_type* key, const key_unit_type* key_end, value_type value);
466 const trie_node* find_prefix_node(
467 const trie_node& node, const key_unit_type* prefix, const key_unit_type* prefix_end) const;
469 template<
bool IsConst>
470 void find_prefix_node_with_stack(
471 std::vector<stack_item<IsConst>>& node_stack,
mdds::detail::const_t<trie_node, IsConst>& node,
472 const key_unit_type* prefix, const key_unit_type* prefix_end) const;
474 template<
bool IsConst>
475 key_type build_key_buffer_from_node_stack(const std::vector<stack_item<IsConst>>& node_stack) const;
477 void count_values(size_type& n, const trie_node& node) const;
479 bool descend_for_equality(const trie_node& left, const trie_node& right) const;
495 template<typename KeyT, typename ValueT, typename TraitsT = trie::default_traits>
498 using pack_value_type =
typename TraitsT::pack_value_type;
499 using packed_type = std::vector<pack_value_type>;
503 friend class trie_map<KeyT, ValueT, TraitsT>;
507 using traits_type = TraitsT;
508 typedef KeyT key_type;
509 typedef typename key_type::value_type key_unit_type;
510 typedef ValueT value_type;
511 typedef size_t size_type;
521 const key_unit_type* key;
525 entry(
const key_unit_type* _key, size_type _keylen, value_type _value)
526 : key(_key), keylen(_keylen), value(_value)
531 using value_store_type = std::deque<value_type>;
534 static constexpr
auto null_value = std::numeric_limits<pack_value_type>::max();
536 static constexpr
auto max_value_pos = null_value - 1u;
541 const value_type* value;
543 std::deque<trie_node*> children;
545 trie_node(key_unit_type _key) : key(_key), value(nullptr)
551 const value_store_type* value_store =
nullptr;
553 const pack_value_type* node_pos =
nullptr;
554 const pack_value_type* child_pos =
nullptr;
555 const pack_value_type* child_end =
nullptr;
558 const value_store_type* _value_store,
const pack_value_type* _node_pos,
const pack_value_type* _child_pos,
559 const pack_value_type* _child_end)
560 : value_store(_value_store), node_pos(_node_pos), child_pos(_child_pos), child_end(_child_end)
563 bool operator==(
const stack_item& other)
const
565 return value_store == other.value_store && node_pos == other.node_pos && child_pos == other.child_pos &&
566 child_end == other.child_end;
569 bool operator!=(
const stack_item& other)
const
571 return !operator==(other);
574 bool has_value()
const
576 return *node_pos != null_value;
579 pack_value_type get_value_pos()
const
585 typedef std::vector<stack_item> node_stack_type;
586 typedef std::deque<trie_node> node_pool_type;
587 typedef std::vector<std::tuple<size_t, key_unit_type>> child_offsets_type;
589 packed_trie_map(trie::detail::move_to_pack, trie_map<KeyT, ValueT, TraitsT>& from);
597 friend class packed_trie_map;
599 const packed_type* m_packed =
nullptr;
600 const value_store_type* m_value_store =
nullptr;
601 const pack_value_type* m_pos =
nullptr;
603 const_node_type(
const packed_type* packed,
const value_store_type* value_store,
const pack_value_type* p);
643 const value_type&
value()
const;
672 packed_trie_map(
const entry* entries, size_type entry_size);
687 packed_trie_map(
const packed_trie_map& other);
689 packed_trie_map(packed_trie_map&& other);
691 packed_trie_map& operator=(packed_trie_map other);
693 bool operator==(
const packed_trie_map& other)
const;
695 bool operator!=(
const packed_trie_map& other)
const;
697 const_iterator begin()
const;
699 const_iterator end()
const;
701 const_iterator cbegin()
const;
703 const_iterator cend()
const;
713 const_iterator
find(
const key_type& key)
const;
725 const_iterator
find(
const key_unit_type* input, size_type len)
const;
750 search_results
prefix_search(
const key_unit_type* prefix, size_type len)
const;
757 size_type
size() const noexcept;
759 bool empty() const noexcept;
761 void swap(packed_trie_map& other);
775 template<typename FuncT = trie::value_serializer<value_type>>
776 void save_state(std::ostream& os) const;
784 template<typename FuncT = trie::value_serializer<value_type>>
785 void load_state(std::istream& is);
793 std::
string dump_structure(trie::dump_structure_type type) const;
796 void dump_trie_traversal(std::ostream& os) const;
798 node_stack_type get_root_stack() const;
801 trie_node& root, node_pool_type& node_pool, const
entry* start, const
entry* end, size_type pos);
803 size_type compact_node(const trie_node& node);
805 template<typename ModeT, typename NodeT>
806 size_type compact_node(ModeT, NodeT& node);
808 void check_value_size_or_throw() const;
810 size_type push_value_to_store(trie::detail::copy_to_pack, const value_type& value);
811 size_type push_value_to_store(trie::detail::move_to_pack, value_type& value);
813 void push_child_offsets(size_type offset, const child_offsets_type& child_offsets);
816 void compact(const trie_node& root);
818 template<typename ModeT, typename NodeT>
819 void compact(ModeT, NodeT& root);
821 template<typename _Handler>
822 void traverse_tree(_Handler hdl) const;
825 value_store_type m_value_store;
826 packed_type m_packed;
831 #include "trie_map_def.inl"
Definition: trie_map_itr.hpp:488
Definition: trie_map.hpp:595
const_node_type root_node() const
Definition: trie_map_itr.hpp:339
void insert(const key_type &key, value_type value)
Definition: trie_map.hpp:108
search_results prefix_search(const key_type &prefix) const
Definition: trie_map_itr.hpp:88
Definition: trie_map_itr.hpp:336
uintptr_t pack_value_type
Definition: trie_map.hpp:134
Definition: trie_map.hpp:519
Definition: trie_map_itr.hpp:85
Definition: trie_map.hpp:157
Definition: global.hpp:182
Definition: trie_map.hpp:75
Definition: trie_map.hpp:123
Definition: global.hpp:146
Definition: trie_map.hpp:51
const_node_type child(key_unit_type c) const
Definition: trie_map.hpp:89
Definition: trie_map.hpp:166
Definition: trie_map.hpp:239
Definition: cref_wrapper.hpp:34
Definition: trie_map.hpp:56
Definition: trie_map_itr.hpp:70
Definition: trie_map.hpp:62
Definition: trie_map.hpp:47
bool erase(const key_unit_type *key, size_type len)
const value_type & value() const
const_iterator find(const key_type &key) const
Definition: trie_map_itr.hpp:485