-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathPrefixTree.cpp
More file actions
185 lines (151 loc) · 3.73 KB
/
Copy pathPrefixTree.cpp
File metadata and controls
185 lines (151 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "PrefixTree.hpp"
#include <deque>
#include <algorithm>
PrefixTree::PrefixTree()
:m_root(std::make_shared<Node>())
{
}
void PrefixTree::addWord(const std::vector<uint32_t>& word)
{
std::shared_ptr<Node> node = m_root;
const size_t len = word.size();
for (size_t i = 0; i < len; ++i)
{
const uint32_t c = word[i];
auto iter = std::find_if(node->children.begin(), node->children.end(), [&](const std::pair<uint32_t, std::shared_ptr<Node>>& p) {return p.first == c; });
if (iter == node->children.end())
{
std::shared_ptr<Node> newNode = std::make_shared<Node>();
node->children.push_back(std::make_pair(c, newNode));
node = newNode;
}
else
{
node = iter->second;
}
if (i + 1 == len)
{
node->word = word;
}
}
}
void PrefixTree::addWords(const std::vector<std::vector<uint32_t>>& words)
{
for (const auto& w : words)
{
addWord(w);
}
}
void PrefixTree::allWordsAdded()
{
std::deque<std::shared_ptr<Node>> nodes = { m_root };
while (!nodes.empty())
{
// current node
auto& node = nodes.front();
// sort children by label to allow binary search
std::sort
(
node->children.begin()
,node->children.end()
,[](const std::pair<uint32_t, std::shared_ptr<Node>>& lhs, const std::pair<uint32_t, std::shared_ptr<Node>>& rhs) {return lhs.first < rhs.first; }
);
// go over all child nodes
for (const auto& kv : node->children)
{
// add node
nodes.push_back(kv.second);
}
// remove current node from queue
nodes.pop_front();
}
}
bool PrefixTree::isWord(const std::vector<uint32_t>& text) const
{
std::shared_ptr<Node> node = getNode(text);
if (!node)
{
return false;
}
return !node->word.empty();
}
std::vector<uint32_t> PrefixTree::getNextChars(const std::vector<uint32_t>& text) const
{
std::vector<uint32_t> res;
std::shared_ptr<Node> node = getNode(text);
if (!node)
{
return res;
}
for (const auto kv : node->children)
{
res.push_back(kv.first);
}
return res;
}
std::vector<std::vector<uint32_t>> PrefixTree::getNextWords(const std::vector<uint32_t>& text) const
{
// search start node, that is the node representing the given prefix
std::vector<std::vector<uint32_t>> res;
const auto startNode = getNode(text);
if (!startNode)
{
return res;
}
// cache for level 1
const bool isLevel1 = text.size() == 1;
if (isLevel1)
{
std::lock_guard<std::mutex> lock(m_mutex);
const auto cacheIter = m_level1Cache.find(text[0]);
if (cacheIter != m_level1Cache.end())
{
return cacheIter->second;
}
}
// search all words starting with the given prefix
std::deque<std::shared_ptr<Node>> nodes = { startNode };
while (!nodes.empty())
{
// current node
const auto& node = nodes.front();
// go over all child nodes
for (const auto& kv : node->children)
{
// add node
nodes.push_back(kv.second);
}
// remember current prefix if it is a word
if (!node->word.empty())
{
res.push_back(node->word);
}
// remove current node from queue
nodes.pop_front();
}
// put result for level 1 into cache
if (isLevel1)
{
std::lock_guard<std::mutex> lock(m_mutex);
m_level1Cache[text[0]] = res;
}
return res;
}
std::shared_ptr<PrefixTree::Node> PrefixTree::getNode(const std::vector<uint32_t>& text) const
{
// start with root
std::shared_ptr<Node> node = m_root;
for (const auto c : text)
{
// find child element representing current char (binary search)
auto iter = std::lower_bound(node->children.begin(), node->children.end(), c, [](const std::pair<uint32_t, std::shared_ptr<Node>>& p, uint32_t val) {return p.first < val; });
if (iter == node->children.end() || iter->first > c)
{
// not found
return std::shared_ptr<PrefixTree::Node>();
}
// continue with the child node
node = iter->second;
}
return node;
}