about summary refs log tree commit diff stats
path: root/prefix_search.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'prefix_search.cpp')
-rw-r--r--prefix_search.cpp35
1 files changed, 35 insertions, 0 deletions
diff --git a/prefix_search.cpp b/prefix_search.cpp new file mode 100644 index 0000000..2603061 --- /dev/null +++ b/prefix_search.cpp
@@ -0,0 +1,35 @@
1#include "prefix_search.h"
2
3void prefix_search::add(std::string prefix)
4{
5 node* cur = ⊤
6 for (int c : prefix)
7 {
8 cur = &cur->children[c];
9 }
10
11 cur->match = true;
12}
13
14int prefix_search::match(std::string in) const
15{
16 int ret = 0;
17 const node* cur = ⊤
18 for (int c : in)
19 {
20 if (cur->children.count(c) == 0)
21 {
22 return 0;
23 }
24
25 cur = &cur->children.at(c);
26 ret++;
27
28 if (cur->match)
29 {
30 return ret;
31 }
32 }
33
34 return 0;
35}