| 186 | |
| 187 | template <typename T> |
| 188 | T * |
| 189 | Trie<T>::Search(const char *key, int key_len /* = -1 */) const |
| 190 | { |
| 191 | _CheckArgs(key, key_len); |
| 192 | |
| 193 | const Node *found_node = nullptr; |
| 194 | const Node *curr_node = &m_root; |
| 195 | int i = 0; |
| 196 | |
| 197 | while (curr_node) { |
| 198 | if (dbg_ctl_search.on()) { |
| 199 | DbgPrint(dbg_ctl_search, "Visiting node..."); |
| 200 | curr_node->Print(dbg_ctl_search); |
| 201 | } |
| 202 | if (curr_node->occupied) { |
| 203 | if (!found_node || curr_node->rank <= found_node->rank) { |
| 204 | found_node = curr_node; |
| 205 | } |
| 206 | } |
| 207 | if (i == key_len) { |
| 208 | break; |
| 209 | } |
| 210 | curr_node = curr_node->GetChild(key[i]); |
| 211 | ++i; |
| 212 | } |
| 213 | |
| 214 | if (found_node) { |
| 215 | Dbg(dbg_ctl_search, "Returning element with rank %d", found_node->rank); |
| 216 | return found_node->value; |
| 217 | } |
| 218 | |
| 219 | return nullptr; |
| 220 | } |
| 221 | |
| 222 | template <typename T> |
| 223 | void |