-
Notifications
You must be signed in to change notification settings - Fork 1
/
dagdict.cpp
100 lines (88 loc) · 2.46 KB
/
dagdict.cpp
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
#include <algorithm>
#include "dagdict.hpp"
DAGDict::DAGDict() {}
DAGDict::DAGDict(
std::vector<std::pair<std::string, std::vector<std::string>>> codes) {
while (codes.size()) {
auto code = std::move(codes.back());
codes.pop_back();
insert(code.first, std::move(code.second));
}
}
void DAGDict::insert(
const std::string& key,
std::vector<std::string>&& elements) {
auto dag = this;
for (auto c : key) {
if (not dag->graph.contains(c)) {
dag->graph[c] = new DAGDict();
}
dag = dag->graph[c];
}
dag->elements = elements;
}
std::vector<std::string> DAGDict::get_all(const std::string& prefix) const {
if (prefix.empty()) {
return {};
}
auto dag = (DAGDict*)this;
for (auto c : prefix) {
if (not dag->graph.contains(c)) {
return {};
}
dag = dag->graph[c];
}
auto out = dag->child_elements(prefix);
std::sort(out.begin(), out.end());
auto last = std::unique(out.begin(), out.end());
out.erase(last, out.end());
return out;
}
std::vector<std::string> DAGDict::child_elements(
const std::string& prefix) const {
std::vector<std::string> out;
out.insert(
out.end(),
std::make_move_iterator(elements.begin()),
std::make_move_iterator(elements.end()));
for (auto [key, val] : graph) {
std::string new_prefix = prefix + key;
auto r = val->child_elements(new_prefix);
out.insert(
out.end(),
std::make_move_iterator(r.begin()),
std::make_move_iterator(r.end()));
r.clear();
}
return out;
}
std::vector<std::string> DAGDict::possible_keys(
const std::string& prefix) const {
if (prefix.empty()) {
return {};
}
auto dag = (DAGDict*)this;
for (auto c : prefix) {
if (not dag->graph.contains(c)) {
return {};
}
dag = dag->graph[c];
}
return dag->child_keys(prefix);
}
std::vector<std::string> DAGDict::child_keys(const std::string& prefix) const {
std::vector<std::string> out;
if (elements.size()) {
out.push_back(prefix);
}
for (auto [key, val] : graph) {
std::string new_prefix = prefix + key;
auto r = val->child_keys(new_prefix);
out.insert(
out.end(),
std::make_move_iterator(r.begin()),
std::make_move_iterator(r.end()));
r.clear();
}
return out;
}