-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathARTrie.c
222 lines (209 loc) · 6.06 KB
/
ARTrie.c
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
/*
* Copyright (C) 2010 University of Tartu
* Authors: Reina Käärik, Siim Orasmaa, Kristo Tammeoja, Jaak Vilo
* Contact: siim . orasmaa {at} ut . ee
*
* This file is part of Generalized Edit Distance Tool.
*
* Generalized Edit Distance Tool is free software: you can redistribute
* it and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Generalized Edit Distance Tool is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Generalized Edit Distance Tool.
* If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "ARTrie.h"
/* Creates new ARTrie */
ARTrie *createARTrie(){
ARTrie *root;
root = (ARTrie*)malloc(sizeof(ARTrie));
if(root == NULL)
abort();
root->firstNode = NULL;
return root;
}
/* Creates new ARTNode */
ARTNode *newARTNode(wchar_t a){
ARTNode *artN;
artN = (ARTNode *)malloc(sizeof(ARTNode));
if(artN == NULL)
abort();
artN->label = a;
artN->value = DBL_MAX;
artN->rightNode = NULL;
artN->nextNode = NULL;
artN->prevNode = NULL;
return artN;
}
/* Adds an add/remove transformation with given weight into the trie */
int addToARTrie(ARTrie *art, wchar_t *string, int strLen, double value){
ARTNode *tmp;
/* If trie is still empty ... */
if(art->firstNode == NULL){
art->firstNode = newARTNode(*string);
tmp = art->firstNode;
string = string + 1;
strLen--;
if(strLen == 0){
tmp->value = value;
return 0;
}
while(strLen > 1){
tmp->nextNode = (ARTNode *)newARTNode(*string);
tmp->nextNode->prevNode = tmp;
tmp = tmp->nextNode;
string = string +1;
strLen--;
}
tmp->nextNode = (ARTNode *)newARTNode(*string);
tmp->nextNode->value = value;
tmp->nextNode->prevNode = tmp;
return 0;
}
tmp = art->firstNode;
ARTNode *prev;
prev = NULL;
while(strLen > 0){
/* We are trying to follow an existing path in the trie ... */
/* ... reached end of the path and end of our string */
if(strLen == 1 && tmp->label == *string && value < tmp->value){
/* We want that value of the node is always as small as it can be, as
* the generalized edit distance also uses only smallest values. Here
* we replace an existing value with smaller one.
*/
tmp->value = value;
return 0;
}
/* ... path continues */
else if(tmp->label == *string && tmp->nextNode != NULL){
prev = tmp;
tmp = tmp->nextNode;
string = string + 1;
strLen--;
}
/* ... reached end of the path, but our string does not end */
else if(tmp->label == *string && tmp->nextNode == NULL){
string = string + 1;
strLen--;
while(strLen > 1){
tmp->nextNode = (ARTNode *)newARTNode(*string);
tmp->nextNode->prevNode = tmp;
tmp = tmp->nextNode;
string = string +1;
strLen--;
}
tmp->nextNode = (ARTNode *)newARTNode(*string);
tmp->nextNode->prevNode = tmp;
tmp->nextNode->value = value;
return 0;
}
/* ... if labels do not match, try to switch the track (look next nodes
in the same level) ... */
else if(tmp->rightNode != NULL)
tmp = tmp->rightNode;
else break;
}
/* ... existing path not found, so it must be created */
tmp->rightNode = newARTNode(*string);
tmp = tmp->rightNode;
tmp->prevNode = prev;
if(strLen == 1)
tmp->value = value;
else{
string = string +1;
strLen--;
while(strLen > 1){
tmp->nextNode = (ARTNode *)newARTNode(*string);
tmp->nextNode->prevNode = tmp;
tmp = tmp->nextNode;
string = string + 1;
strLen--;
}
tmp->nextNode = (ARTNode *)newARTNode(*string);
tmp->nextNode->value = value;
tmp->nextNode->prevNode = tmp;
}
return 0;
}
/* debug: print the trie */
int showARTrie(ARTrie *t){
ARTNode *nodes[100]; /* NB! a predefined size, increase if necessary */
ARTNode *tmp;
ARTNode *current;
int j = 0;
int i = 1;
int k =0;
char *preC;
size_t preCLen;
for(k = 0; k < 100; k++){
nodes[k] = NULL;
}
tmp = t->firstNode;
nodes[0] = tmp;
while(tmp->rightNode != NULL){
nodes[i] = tmp->rightNode;
i++;
tmp = tmp->rightNode;
}
while(nodes[j] != NULL){
current = nodes[j];
preCLen = wctomb(NULL ,current->label);
if((preC = (char *)malloc(preCLen + 1)) == NULL){
puts("Error: Could not allocate memory");
exit(1);
}
wctomb(preC ,current->label);
preC[preCLen] = '\0';
printf("\n %s", preC);
if(current->value != DBL_MAX)
printf(" , %f", current->value);
/* could not go further down on the branch */
if(current->nextNode == NULL){
j++; continue;
}
else { /* can go further down on the branch*/
nodes[i] = current->nextNode;
i++;
tmp = current->nextNode;
while(tmp->rightNode != NULL){
nodes[i] = tmp->rightNode;
i++;
tmp = tmp->rightNode;
}
j++;
}
}
puts("\n");
return 0;
}
// Releases memory under ARTNode and all of its children
void freeARTNode(ARTNode *node){
ARTNode *tmp;
tmp = node;
while (tmp != NULL){
if (tmp->rightNode != NULL){
freeARTNode(tmp->rightNode);
}
ARTNode *next;
next = tmp->nextNode;
free(tmp);
tmp = next;
}
}
// Releases memory under ARTrie and all of its children
void freeARTrie(ARTrie *art){
ARTNode *tmp;
tmp = art->firstNode;
if (tmp != NULL){
freeARTNode(tmp);
}
free(art);
}