-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdictionary.hpp
141 lines (118 loc) · 3.44 KB
/
dictionary.hpp
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
#ifndef DICTIONARY_HPP___
#define DICTIONARY_HPP___
#include <map>
template <int T>
struct Key {
uint8_t mKey[T];
uint8_t &operator[](uint8_t index) {
return mKey[index];
}
uint8_t operator[](uint8_t index) const {
return mKey[index];
}
};
// Needed to be able to use UL as a Key in std::map
// ------------------------------------------------------------------------------
template <int T>
bool operator<(const Key<T> &x1, const Key<T> &x2) {
bool rval = false;
int i=0;
for (int i=0;i<16;i++) {
if (x1[i] != x2[i]) {
rval = (x1[i] < x2[i]);
break;
}
}
return rval;
}
template <int T>
struct Signature {
uint16_t mPath[T];
uint16_t &operator[](uint16_t index) {
return mPath[index];
}
uint16_t operator[](uint16_t index) const {
return mPath[index];
}
};
struct DictionaryNode {
std::string mValue;
std::map<uint16_t, DictionaryNode *> mChildrenNodes;
~DictionaryNode() {
for (auto &e: mChildrenNodes) {
delete e.second;
}
}
};
template <int T>
struct Dictionary {
Dictionary() {}
~Dictionary() {
for (auto &e: mRootNodes) {
delete e.second;
}
}
std::map<uint16_t, DictionaryNode *> mRootNodes;
void add(const Signature<T> x, std::string name) {
DictionaryNode *current_node;
// Adding the root
auto it = mRootNodes.find(x[0]);
if (it == mRootNodes.end()) {
current_node = new DictionaryNode;
mRootNodes[x[0]] = current_node;
//std::cout << "Creating a new root node for " << x[0] << std::endl;
}
else {
current_node = it->second;
//std::cout << "Following root node for " << x[0] << std::endl;
}
// Adding all other node
for (int i=1;i<T;i++) {
auto it = current_node->mChildrenNodes.find(x[i]);
if (it == current_node->mChildrenNodes.end()) {
DictionaryNode *new_node = new DictionaryNode;
current_node->mChildrenNodes[x[i]] = new_node;
current_node = new_node;
//std::cout << "Creating a new node for " << x[i] << std::endl;
}
else {
current_node = it->second;
//std::cout << "Following node for " << x[i] << std::endl;
}
}
current_node->mValue = name;
}
std::string find(const Key<T> &x) {
std::string rval = "NULL";
bool found = true;
// Find the root
auto it = mRootNodes.find(x[0]);
if (it != mRootNodes.end()) {
DictionaryNode *current_node = it->second;
for (int i=1;i<T;i++) {
// Test if there is a pass-all vertex
auto pa_it = current_node->mChildrenNodes.find(0xFFFF);
// Test if there is a matching vertex
auto it = current_node->mChildrenNodes.find(x[i]);
// If there is a matching node - take it
if (it != current_node->mChildrenNodes.end()) {
current_node = it->second;
}
// Else if there is a pass-all node - take it
else if (pa_it != current_node->mChildrenNodes.end()) {
current_node = pa_it->second;
}
// No Match
else {
found = false;
break;
}
}
if (found) {
rval = current_node->mValue;
}
}
return rval;
}
};
#endif