|
2 | 2 |
|
3 | 3 | void Trie::insert(std::string_view key, std::string_view value) |
4 | 4 | { |
5 | | - TrieNode* node = &root; |
| 5 | + std::reference_wrapper<TrieNode> node = root; |
6 | 6 | for (char character : key) |
7 | 7 | { |
8 | | - auto [it, inserted] = node->children.try_emplace(character, std::make_unique<TrieNode>()); |
9 | | - node = it->second.get(); |
| 8 | + if (!node.get().children.contains(character)) [[unlikely]] { |
| 9 | + nodes.emplace_back(); |
| 10 | + node.get().children.emplace(character, nodes.back()); |
| 11 | + } |
| 12 | + node = node.get().children.at(character); |
10 | 13 | } |
11 | | - node->target = value; |
| 14 | + node.get().target = value; |
12 | 15 | } |
13 | 16 |
|
14 | | -[[nodiscard]] std::pair<std::string_view, bool> Trie::searchFullMatch(const std::span<const char>& cachedData) |
| 17 | +[[nodiscard]] std::pair<std::string_view, bool> Trie::searchFullMatch(const std::span<const char>& cachedData) const |
15 | 18 | { |
16 | | - TrieNode* node = &root; |
| 19 | + std::reference_wrapper<const TrieNode> node = root; |
17 | 20 | for (char c: cachedData) |
18 | 21 | { |
19 | | - auto res = node->children.find(c); |
20 | | - if (res == node->children.end()) |
| 22 | + auto res = node.get().children.find(c); |
| 23 | + if (res == node.get().children.end()) |
21 | 24 | { |
22 | 25 | return std::make_pair(std::string_view(), false); |
23 | 26 | } |
24 | 27 | node = res->second.get(); |
25 | 28 | } |
26 | 29 |
|
27 | 30 | // full match |
28 | | - if (node->target) |
| 31 | + if (node.get().target) |
29 | 32 | { |
30 | | - return std::make_pair(node->target.value(), true); |
| 33 | + return std::make_pair(node.get().target.value(), true); |
31 | 34 | } |
32 | 35 |
|
33 | 36 | return std::make_pair(std::string_view(), false); |
|
0 commit comments