forked from lnthach/SAX-SEQL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mytrie.h
261 lines (206 loc) · 5.58 KB
/
mytrie.h
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
* mytrie.h
*
* Created on: 29 Aug 2016
* Author: thachln
*/
#ifndef MYTRIE_H_
#define MYTRIE_H_
#include <string>
#include <cstring>
#include <vector>
#include <iostream>
#include <fstream>
#include <cmath>
#include <map>
#define ALPHABET_SIZE (26)
class Trie {
private:
struct TNode
{
struct TNode *children[ALPHABET_SIZE];
char key;
std::string pattern;
bool isLeaf;
double val;
int size;
int id;
} ;
struct TNode *root;
unsigned int next_id = 1;
unsigned int trie_size = 0;
void print(struct TNode *node){
std::cout << node->key << ":" << node->pattern << ":" << node->val;
if(node->size > 0){
std::cout << " [ ";
for (int i = 0; i < node->size;i++){
print(node->children[i]);
}
std::cout << " ] ";
}
}
void deleteBranch(struct TNode *node){
if(node->size > 0){
for (int i = 0; i < node->size;i++){
deleteBranch(node->children[i]);
}
}
delete node;
}
// calculate the distance between two keys
double distance(char a, char b){
return 1.0*std::abs(a - b);
}
double compute_xi(double d, double max_distance){
if (d <= max_distance){
// return 1.0;
return 1.0 - d/(max_distance + 1);
}
return 0.0;
}
bool isExitNode(struct TNode *node){
return (node->id > 0);
}
public:
Trie(){
root = new struct TNode();
root->key = ' ';
root->val = 0.0;
root->size = 0;
root->pattern = "";
root->id = -1;
}
~Trie(){
deleteBranch(root);
}
int getSize(){
return trie_size;
}
// insert new pattern to the trie
bool insert(std::string pattern, double score){
struct TNode *currentNode = root;
for (int i = 0; i < pattern.length();i++){
bool not_found = true;
for (int j = 0; j < currentNode->size;j++){
if (currentNode->children[j]->key == pattern[i]){
currentNode = currentNode->children[j];
not_found = false;
break;
}
}
if (not_found){
if (currentNode->size >= ALPHABET_SIZE){
return false;
} else {
struct TNode *newNode = new struct TNode();
newNode->key = pattern[i];
newNode->isLeaf = false;
newNode->val = 0.0;
newNode->size = 0;
newNode->pattern = "";
newNode->id = -1;
currentNode->children[currentNode->size] = newNode;
currentNode->size++;
currentNode = newNode;
trie_size++;
}
}
}
currentNode->val = score;
currentNode->pattern = pattern;
currentNode->id = next_id;
next_id++;
return true;
}
// for testing: print the whole trie
void printTrie(){
std::cout << std::endl;
print(root);
std::cout << std::endl;
}
void printPatterns(){
std::cout << std::endl;
std::vector<struct TNode *> expanding_nodes;
expanding_nodes.push_back(root);
struct TNode * cur_node;
while(!expanding_nodes.empty()){
cur_node = expanding_nodes.back();
expanding_nodes.pop_back();
if (isExitNode(cur_node)){
std::cout << cur_node->pattern << " " << cur_node->id << " " << cur_node->val << std::endl;
}
for (int i = 0; i < cur_node->size;i++){
expanding_nodes.push_back(cur_node->children[i]);
}
}
std::cout << std::endl;
}
bool load_seql_trie (const char *file){
std::ifstream infile(file);
std::string s;
std::string v;
double val;
// skip the first line
if (!(infile >> s)){
std::cout << "Wrong input" << std::endl;
return false;
}
while(infile >> v >> s){
insert(s,std::stod(v));
}
return true;
}
double naive_search(const char *line, double max_distance, std::vector<int> &output){
unsigned int len = strlen (line);
double r = 0.0;
std::vector<struct TNode *> expanding_nodes;
std::vector<int> next_positions;
std::vector<double> distances;
std::map<struct TNode *,double> results;
//int st_pos = 0;
for (unsigned int st_pos = 0; st_pos < len; st_pos++){
//while (line[st_pos] != '\0' && line[st_pos] != NULL){
expanding_nodes.push_back(root);
next_positions.push_back(st_pos);
distances.push_back(0.0);
while(!expanding_nodes.empty()){
// fetch unvisited node
unsigned int cur_pos = next_positions.back(); // advance to the next position
double cur_distance = distances.back();
struct TNode * cur_node = expanding_nodes.back();
next_positions.pop_back();
distances.pop_back();
expanding_nodes.pop_back();
//if (line[st_pos] != '\0' && line[st_pos] != NULL){
if (cur_pos < len && !isspace(line[cur_pos])){
char next_char = line[cur_pos];
for (int i = 0; i < cur_node->size;i++){
double new_dist = distance(cur_node->children[i]->key,next_char);
if (cur_distance + new_dist <= max_distance){
// insert unvisited node
expanding_nodes.push_back(cur_node->children[i]);
distances.push_back(cur_distance + new_dist);
next_positions.push_back(cur_pos + 1);
if (isExitNode(cur_node->children[i])){ // found a pattern
// update results
if (results.count(cur_node->children[i]) == 0 || results[cur_node->children[i]] > cur_distance + new_dist){
results[cur_node->children[i]] = cur_distance + new_dist;
//std::cout << cur_node->children[i]->id << ":" << cur_distance + new_dist << ":"<< cur_node->children[i]->val << std::endl;
}
//std::cout << cur_node->children[i]->pattern << ":" << cur_distance + new_dist << ":"<< cur_node->children[i]->val << std::endl;
}
}
}
}
}
}
for(auto const &ent1 : results) {
// ent1.first is the first key
//std::cout << ent1.first->pattern << ":" << ent1.first->val << std::endl;
output.push_back(ent1.first->id);
r += ent1.first->val * compute_xi(ent1.second,max_distance);
}
return r;
}
};
#endif /* MYTRIE_H_ */