Skip to content

Commit 693f190

Browse files
Владимир ЧижВладимир Чиж
authored andcommitted
moved to std::reference_wrapper>
1 parent 77d9dfa commit 693f190

2 files changed

Lines changed: 24 additions & 14 deletions

File tree

srcbpatch/trie.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,35 @@
22

33
void Trie::insert(std::string_view key, std::string_view value)
44
{
5-
TrieNode* node = &root;
5+
std::reference_wrapper<TrieNode> node = root;
66
for (char character : key)
77
{
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);
1013
}
11-
node->target = value;
14+
node.get().target = value;
1215
}
1316

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
1518
{
16-
TrieNode* node = &root;
19+
std::reference_wrapper<const TrieNode> node = root;
1720
for (char c: cachedData)
1821
{
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())
2124
{
2225
return std::make_pair(std::string_view(), false);
2326
}
2427
node = res->second.get();
2528
}
2629

2730
// full match
28-
if (node->target)
31+
if (node.get().target)
2932
{
30-
return std::make_pair(node->target.value(), true);
33+
return std::make_pair(node.get().target.value(), true);
3134
}
3235

3336
return std::make_pair(std::string_view(), false);

srcbpatch/trie.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@
22

33
#include <string_view>
44

5+
/// @brief A node of prefix Trie, each node contains current target (if exist) and child nodes in children.
6+
/// And doesn't contain a char of current node
57
class TrieNode {
68
public:
7-
std::unordered_map<char, std::unique_ptr<TrieNode>> children;
9+
/// a list of child nodes of current node.
10+
std::unordered_map<char, std::reference_wrapper<TrieNode>> children;
11+
/// target of curretn node (if there are node target at this node in our lexemmePairs --> target is std::nullopt)
812
std::optional<std::string_view> target;
9-
};
13+
};
1014

1115
/// @brief Frefix tree class to speed up UniformLexemeReplacer::DoReplace
12-
class Trie {
16+
class Trie final{
1317
public:
1418
/// <summary>
1519
/// Adds a new key-value pair in prefix tree
@@ -23,8 +27,11 @@ class Trie {
2327
/// </summary>
2428
/// <param name="cachedData"> key to find </param>
2529
/// <returns>string_view: target, bool: FullMatch</returns>
26-
[[nodiscard]] std::pair<std::string_view, bool> searchFullMatch(const std::span<const char>& cachedData);
30+
[[nodiscard]] std::pair<std::string_view, bool> searchFullMatch(const std::span<const char>& cachedData) const;
2731

2832
private:
33+
/// a root node, doesn't contains target value
2934
TrieNode root;
35+
/// holds all the nodes of Trie. While default Trie uses pointers, we are going to user reference_wrapper to this deque elements
36+
std::deque<TrieNode> nodes;
3037
};

0 commit comments

Comments
 (0)