33#define EXT_STRUCTURES_TRIE_HEADER
44
55#include < cstdint>
6+ #include < iostream>
67#include < map>
78#include < memory>
89#include < optional>
@@ -74,6 +75,35 @@ class node {
7475
7576// description of how a node will be splitted
7677template <typename Key>
78+ inline std::ostream& operator <<(std::ostream& os, std::unique_ptr<node<char , std::uint8_t >> const & node) {
79+ if (node == nullptr ) {
80+ os << " no node" << std::endl;
81+ return os;
82+ }
83+
84+ std::cerr << " [" << vec2str (node->prefix ) << " ]" ;
85+ if (node->children )
86+ for (auto const & pair : *(node->children )) std::cerr << " -> " << pair.first ;
87+ os << std::endl;
88+
89+ return os;
90+ }
91+
92+ inline std::ostream& operator <<(std::ostream& os, node<char , std::uint8_t > const * node) {
93+ if (node == nullptr ) {
94+ os << " no node" << std::endl;
95+ return os;
96+ }
97+
98+ std::cerr << " [" << vec2str (node->prefix ) << " ]" ;
99+ if (node->children )
100+ for (auto const & pair : *(node->children )) std::cerr << " -> " << pair.first ;
101+ os << std::endl;
102+
103+ return os;
104+ }
105+
106+ template <typename Key>
77107struct split {
78108 using key_t = Key;
79109 using label_t = typename key_t ::value_type;
@@ -86,6 +116,16 @@ struct split {
86116};
87117
88118template <typename Key>
119+ inline std::ostream& operator <<(std::ostream& os, split<std::vector<char >> const & si) {
120+ os << " = parent_prefix: " << detail_trie::vec2str (si.parent_prefix ) << std::endl;
121+ os << " = split_label: " << (si.split_label .has_value () ? si.split_label .value () : ' x' ) << std::endl;
122+ os << " = split_prefix: " << detail_trie::vec2str (si.split_prefix ) << std::endl;
123+ os << " = insert_label: " << (si.insert_label .has_value () ? si.insert_label .value () : ' x' ) << std::endl;
124+ os << " = insert_prefix: " << detail_trie::vec2str (si.insert_prefix ) << std::endl;
125+ return os;
126+ }
127+
128+ template <typename Key>
89129[[nodiscard]] constexpr std::size_t find_split_point (Key const & a, Key const & b) {
90130 std::size_t rv = 0 ;
91131 while (rv < std::min (a.size (), b.size ())) {
@@ -143,12 +183,14 @@ class trie {
143183
144184 [[nodiscard]] std::pair<node_t *, bool > insert (key_t const & key, std::unique_ptr<value_t > value = nullptr ) {
145185 using namespace detail_trie ;
146-
186+ std::cerr << " #######################################" << std::endl;
187+ std::cerr << " insert: " << vec2str (key) << std::endl;
147188 auto [insert_parent, insert_prefix] = find_insert_parent (root.get (), key);
148189 if (insert_parent == nullptr )
149190 return {nullptr , false };
150191
151192 auto & ip = *insert_parent;
193+ std::cerr << " ip no mod: " << insert_parent;
152194
153195 if (insert_prefix.empty ()) {
154196 // must be inserted into ip
@@ -163,6 +205,7 @@ class trie {
163205 }
164206
165207 split<key_t > si = split_info (insert_parent->prefix , insert_prefix);
208+
166209 if (!si.insert_label .has_value ())
167210 return {insert_parent, false };
168211
@@ -181,9 +224,15 @@ class trie {
181224 ip.prefix = si.parent_prefix ;
182225 ip.is_word = false ;
183226 ip.value = nullptr ;
227+
228+ std::cerr << " split done: " << insert_parent;
229+ for (auto const & pair : *ip.children ) std::cerr << " @ " << pair.first << " - " << pair.second ;
184230 }
185231
186- return ip.insert_node (si.insert_label .value (), si.insert_prefix , value, true );
232+ auto rv = ip.insert_node (si.insert_label .value (), si.insert_prefix , value, true );
233+ std::cerr << " insert done: " << insert_parent;
234+ for (auto const & pair : *ip.children ) std::cerr << " @ " << pair.first << " - " << pair.second ;
235+ return std::move (rv);
187236 }
188237
189238#ifndef EXT_TEST
0 commit comments